2026-04-17 15:09:31 +02:00
|
|
|
import { describe, it, expect } from 'vitest';
|
|
|
|
|
import { openInMemoryForTest } from '../../src/lib/server/db';
|
2026-04-18 08:28:02 +02:00
|
|
|
import {
|
|
|
|
|
addDomain,
|
|
|
|
|
listDomains,
|
|
|
|
|
removeDomain,
|
|
|
|
|
setDomainFavicon,
|
|
|
|
|
updateDomain,
|
|
|
|
|
getDomainById
|
|
|
|
|
} from '../../src/lib/server/domains/repository';
|
2026-04-17 15:09:31 +02:00
|
|
|
import { isDomainAllowed } from '../../src/lib/server/domains/whitelist';
|
|
|
|
|
|
|
|
|
|
describe('allowed domains', () => {
|
|
|
|
|
it('round-trips domains', () => {
|
|
|
|
|
const db = openInMemoryForTest();
|
|
|
|
|
addDomain(db, 'chefkoch.de', 'Chefkoch');
|
|
|
|
|
addDomain(db, 'emmikochteinfach.de', 'Emmi kocht einfach');
|
|
|
|
|
const all = listDomains(db);
|
|
|
|
|
expect(all.map((d) => d.domain).sort()).toEqual(['chefkoch.de', 'emmikochteinfach.de']);
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
it('normalizes www. and case', () => {
|
|
|
|
|
const db = openInMemoryForTest();
|
|
|
|
|
addDomain(db, 'WWW.Chefkoch.DE');
|
|
|
|
|
expect(isDomainAllowed(db, 'https://chefkoch.de/abc')).toBe(true);
|
|
|
|
|
expect(isDomainAllowed(db, 'https://www.chefkoch.de/abc')).toBe(true);
|
|
|
|
|
expect(isDomainAllowed(db, 'https://fake.de/abc')).toBe(false);
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
it('rejects invalid urls', () => {
|
|
|
|
|
const db = openInMemoryForTest();
|
|
|
|
|
addDomain(db, 'chefkoch.de');
|
|
|
|
|
expect(isDomainAllowed(db, 'not a url')).toBe(false);
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
it('removes domains', () => {
|
|
|
|
|
const db = openInMemoryForTest();
|
|
|
|
|
const d = addDomain(db, 'chefkoch.de');
|
|
|
|
|
removeDomain(db, d.id);
|
|
|
|
|
expect(listDomains(db)).toEqual([]);
|
|
|
|
|
});
|
2026-04-18 08:28:02 +02:00
|
|
|
|
|
|
|
|
it('updateDomain changes label without touching favicon', () => {
|
|
|
|
|
const db = openInMemoryForTest();
|
|
|
|
|
const d = addDomain(db, 'chefkoch.de', 'Chefkoch');
|
|
|
|
|
setDomainFavicon(db, d.id, 'favicon-abc.png');
|
|
|
|
|
const updated = updateDomain(db, d.id, { display_name: 'Chefkoch.de' });
|
|
|
|
|
expect(updated?.domain).toBe('chefkoch.de');
|
|
|
|
|
expect(updated?.display_name).toBe('Chefkoch.de');
|
|
|
|
|
expect(updated?.favicon_path).toBe('favicon-abc.png');
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
it('updateDomain resets favicon when the domain itself changes', () => {
|
|
|
|
|
const db = openInMemoryForTest();
|
|
|
|
|
const d = addDomain(db, 'chefkoch.de');
|
|
|
|
|
setDomainFavicon(db, d.id, 'favicon-abc.png');
|
|
|
|
|
const updated = updateDomain(db, d.id, { domain: 'rezeptwelt.de' });
|
|
|
|
|
expect(updated?.domain).toBe('rezeptwelt.de');
|
|
|
|
|
expect(updated?.favicon_path).toBe(null);
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
it('updateDomain returns null for missing id', () => {
|
|
|
|
|
const db = openInMemoryForTest();
|
|
|
|
|
expect(updateDomain(db, 999, { domain: 'x.com' })).toBe(null);
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
it('getDomainById fetches single row', () => {
|
|
|
|
|
const db = openInMemoryForTest();
|
|
|
|
|
const d = addDomain(db, 'chefkoch.de');
|
|
|
|
|
expect(getDomainById(db, d.id)?.domain).toBe('chefkoch.de');
|
|
|
|
|
expect(getDomainById(db, 999)).toBe(null);
|
|
|
|
|
});
|
2026-04-17 15:09:31 +02:00
|
|
|
});
|