88b9e20e2d
CI / Generate TypeScript types (push) Successful in 9s
CI / Server unit tests (push) Successful in 21s
CI / Build server (push) Successful in 25s
CI / Build client (push) Successful in 37s
CI / Playwright E2E tests (push) Successful in 1m18s
CI / Build and push Docker image (push) Successful in 39s
CI / Notify (push) Successful in 2s
108 lines
3.9 KiB
TypeScript
108 lines
3.9 KiB
TypeScript
import { resetMemoryStorage } from '../storage/memory';
|
|
import getStorage from '../storage';
|
|
import { getStores, addStore, removeStore } from '../stores';
|
|
|
|
const ADMIN_PW = 'testadmin';
|
|
|
|
/** Vrátí pole názvů obchodů pro snadné porovnání v testech. */
|
|
const names = (stores: { name: string }[]) => stores.map(s => s.name);
|
|
|
|
beforeEach(() => {
|
|
resetMemoryStorage();
|
|
process.env.ADMIN_PASSWORD = ADMIN_PW;
|
|
});
|
|
|
|
afterEach(() => {
|
|
delete process.env.ADMIN_PASSWORD;
|
|
});
|
|
|
|
describe('getStores', () => {
|
|
test('vrátí prázdné pole, pokud seznam neexistuje', async () => {
|
|
const stores = await getStores();
|
|
expect(stores).toEqual([]);
|
|
});
|
|
|
|
test('převede starý formát (pole řetězců) na objekty', async () => {
|
|
// Simulace dat uložených ve starším formátu (před zavedením URL)
|
|
await getStorage().setData('stores', ['McDonald\'s', 'KFC']);
|
|
const stores = await getStores();
|
|
expect(stores).toEqual([{ name: 'McDonald\'s' }, { name: 'KFC' }]);
|
|
});
|
|
});
|
|
|
|
describe('addStore', () => {
|
|
test('přidá obchod se správným heslem', async () => {
|
|
const stores = await addStore('McDonald\'s', ADMIN_PW);
|
|
expect(names(stores)).toContain('McDonald\'s');
|
|
});
|
|
|
|
test('uloží volitelnou URL a ořízne mezery', async () => {
|
|
const stores = await addStore('Bistro', ADMIN_PW, ' https://wolt.com/bistro ');
|
|
expect(stores).toContainEqual({ name: 'Bistro', url: 'https://wolt.com/bistro' });
|
|
});
|
|
|
|
test('bez URL uloží obchod bez pole url', async () => {
|
|
const stores = await addStore('KFC', ADMIN_PW, ' ');
|
|
expect(stores).toContainEqual({ name: 'KFC' });
|
|
});
|
|
|
|
test('odmítne URL s nepovoleným schématem (javascript:)', async () => {
|
|
await expect(addStore('Zlo', ADMIN_PW, 'javascript:alert(1)')).rejects.toThrow('http');
|
|
});
|
|
|
|
test('odmítne nevalidní URL', async () => {
|
|
await expect(addStore('Zlo', ADMIN_PW, 'nfdjska')).rejects.toThrow('Neplatná URL');
|
|
});
|
|
|
|
test('vyhodí UNAUTHORIZED s nesprávným heslem', async () => {
|
|
await expect(addStore('KFC', 'spatne')).rejects.toThrow('UNAUTHORIZED');
|
|
});
|
|
|
|
test('vyhodí UNAUTHORIZED pokud ADMIN_PASSWORD není nastaven', async () => {
|
|
delete process.env.ADMIN_PASSWORD;
|
|
await expect(addStore('KFC', '')).rejects.toThrow('UNAUTHORIZED');
|
|
});
|
|
|
|
test('odmítne prázdný název', async () => {
|
|
await expect(addStore(' ', ADMIN_PW)).rejects.toThrow('prázdný');
|
|
});
|
|
|
|
test('odmítne duplikát (case-insensitive)', async () => {
|
|
await addStore('McDonald\'s', ADMIN_PW);
|
|
await expect(addStore('mcdonald\'s', ADMIN_PW)).rejects.toThrow('existuje');
|
|
});
|
|
|
|
test('vrátí aktualizovaný seznam', async () => {
|
|
await addStore('McDonald\'s', ADMIN_PW);
|
|
const stores = await addStore('KFC', ADMIN_PW);
|
|
expect(stores).toHaveLength(2);
|
|
expect(names(stores)).toContain('McDonald\'s');
|
|
expect(names(stores)).toContain('KFC');
|
|
});
|
|
});
|
|
|
|
describe('removeStore', () => {
|
|
beforeEach(async () => {
|
|
await addStore('McDonald\'s', ADMIN_PW);
|
|
});
|
|
|
|
test('odebere obchod se správným heslem', async () => {
|
|
const stores = await removeStore('McDonald\'s', ADMIN_PW);
|
|
expect(names(stores)).not.toContain('McDonald\'s');
|
|
});
|
|
|
|
test('case-insensitive odebrání', async () => {
|
|
const stores = await removeStore('MCDONALD\'S', ADMIN_PW);
|
|
expect(names(stores)).not.toContain('McDonald\'s');
|
|
});
|
|
|
|
test('vyhodí UNAUTHORIZED s nesprávným heslem', async () => {
|
|
await expect(removeStore('McDonald\'s', 'spatne')).rejects.toThrow('UNAUTHORIZED');
|
|
});
|
|
|
|
test('odebrání neexistujícího obchodu nic nerozbije', async () => {
|
|
const stores = await removeStore('Neexistuje', ADMIN_PW);
|
|
expect(names(stores)).toContain('McDonald\'s');
|
|
});
|
|
});
|