Files
website/src/lib/server/owner.test.ts
T
stan 81b0c51672
Deploy production / deploy (push) Successful in 1m30s
prices: fold groceries and CS skins into one price tracker (/trackers/prices) with tabs
- tab registry in #lib/price-trackers (add a tab = route + entry + icon)
- groceries -> /trackers/prices/groceries (owner-only), CS prices -> /trackers/prices/cs (public)
- old /trackers/groceries/* and /projects/cs-prices/* redirect (301 GET, 308 POST) with query
- nav: one prices entry replaces groceries and skins; cyan price-tag icon
2026-10-27 10:38:10 +00:00

84 lines
4.6 KiB
TypeScript

import { describe, expect, it } from 'vitest';
import { isOwner, isOwnerOnlyPath, isOwnerWritePath } from './owner';
import { MATH_PRIVATE } from '#lib/math.js';
const event = (user: Partial<NonNullable<App.Locals['user']>> | null) =>
({ locals: { user, session: null } }) as unknown as { locals: App.Locals };
describe('owner gate', () => {
it('is only the owner account', () => {
expect(isOwner(event(null))).toBe(false);
expect(isOwner(event({ role: 'admin', email: '[email protected]' }))).toBe(false);
expect(isOwner(event({ role: 'user', email: '[email protected]' }))).toBe(true);
});
it('matches owner-only paths', () => {
expect(isOwnerOnlyPath('/trackers/health')).toBe(true);
expect(isOwnerOnlyPath('/trackers/health/')).toBe(true);
expect(isOwnerOnlyPath('/trackers/Health/import')).toBe(true);
expect(isOwnerOnlyPath('/trackers/healthy')).toBe(false);
expect(isOwnerOnlyPath('/trackers')).toBe(false);
expect(isOwnerOnlyPath('/trackers/tasks')).toBe(true);
expect(isOwnerOnlyPath('/trackers/Tasks/')).toBe(true);
expect(isOwnerWritePath('/trackers/tasks')).toBe(true);
expect(isOwnerOnlyPath('/trackers/groceries')).toBe(true);
expect(isOwnerWritePath('/trackers/groceries')).toBe(true);
expect(isOwnerOnlyPath('/trackers/prices/groceries/stats')).toBe(true);
expect(isOwnerWritePath('/trackers/prices/groceries')).toBe(true);
expect(isOwnerOnlyPath('/trackers/prices/cs')).toBe(false);
expect(isOwnerWritePath('/trackers/prices/cs/api/series')).toBe(false);
expect(isOwnerOnlyPath('/trackers/data-holders')).toBe(true);
expect(isOwnerWritePath('/trackers/data-holders')).toBe(true);
expect(isOwnerOnlyPath('/trackers/houses')).toBe(true);
expect(isOwnerWritePath('/trackers/houses')).toBe(true);
});
it('gates the book reader, files and APIs but not the catalogue', () => {
expect(isOwnerOnlyPath('/trackers/books/files/12')).toBe(true);
expect(isOwnerOnlyPath('/trackers/books/12/read')).toBe(true);
expect(isOwnerOnlyPath('/trackers/books/12/progress')).toBe(true);
expect(isOwnerOnlyPath('/trackers/books/12/notes/3')).toBe(true);
expect(isOwnerOnlyPath('/trackers/books')).toBe(false);
expect(isOwnerOnlyPath('/trackers/books/12')).toBe(false);
expect(isOwnerOnlyPath('/trackers/books/covers/12')).toBe(false);
expect(isOwnerOnlyPath('/trackers/books/12/reader-notes-public')).toBe(false);
});
it('gates the math tracker while MATH_PRIVATE is on; writes are always owner-only', () => {
expect(isOwnerOnlyPath('/trackers/math')).toBe(MATH_PRIVATE);
expect(isOwnerOnlyPath('/trackers/math/__data.json')).toBe(MATH_PRIVATE);
expect(isOwnerWritePath('/trackers/math')).toBe(true);
});
it('gates the whole cargill plant tracker, reads and writes', () => {
expect(isOwnerOnlyPath('/trackers/cargill')).toBe(true);
expect(isOwnerOnlyPath('/trackers/cargill/__data.json')).toBe(true);
expect(isOwnerOnlyPath('/trackers/cargill/process-step/cleaning')).toBe(true);
expect(isOwnerOnlyPath('/trackers/cargillx')).toBe(false);
expect(isOwnerWritePath('/trackers/cargill')).toBe(true);
expect(isOwnerWritePath('/trackers/cargill/glossary/ester')).toBe(true);
});
it('shows the radio tracker to everyone but keeps every write mine', () => {
expect(isOwnerOnlyPath('/trackers/radio')).toBe(false);
expect(isOwnerOnlyPath('/trackers/radio/__data.json')).toBe(false);
expect(isOwnerOnlyPath('/trackers/radio/banks.csv')).toBe(false);
expect(isOwnerWritePath('/trackers/radio')).toBe(true);
expect(isOwnerWritePath('/trackers/radio/__data.json')).toBe(true);
expect(isOwnerWritePath('/trackers/radiox')).toBe(false);
});
it('only lets the owner write in owner areas', () => {
expect(isOwnerWritePath('/trackers/books/12')).toBe(true);
expect(isOwnerWritePath('/trackers/books')).toBe(true);
expect(isOwnerWritePath('/trackers/health/import')).toBe(true);
expect(isOwnerWritePath('/trackers/cars/ford/mustang')).toBe(true);
expect(isOwnerOnlyPath('/trackers/cars/ford/mustang')).toBe(false);
expect(isOwnerWritePath('/trackers/planes/boeing/737')).toBe(true);
expect(isOwnerOnlyPath('/trackers/planes/boeing/737')).toBe(false);
expect(isOwnerWritePath('/trackers/trains/siemens/virm')).toBe(true);
expect(isOwnerOnlyPath('/trackers/trains/siemens/virm')).toBe(false);
expect(isOwnerWritePath('/trackers/wildlife/mammals/vulpes-vulpes')).toBe(true);
expect(isOwnerOnlyPath('/trackers/wildlife/mammals/vulpes-vulpes')).toBe(false);
expect(isOwnerWritePath('/trackers/pc')).toBe(true);
expect(isOwnerOnlyPath('/trackers/pc')).toBe(false);
expect(isOwnerWritePath('/trackers/bookshelf')).toBe(false);
expect(isOwnerWritePath('/lists/books')).toBe(false);
});
});