mirror of
https://github.com/mauriceboe/TREK.git
synced 2026-12-03 01:28:55 +00:00
test(server): seed the plugin discovery, permission, feed and stay specs through the factories
Converts the raw prepare() fixtures and reads in the plugin discovery spec, the permissions service spec, the feeds and invite MCP spec and the day accommodations repository spec to the ORM factories and readers. The permission cache cases still write their override rows past savePermissions, now through setAppSetting, so the stale-cache behaviour they pin is unchanged. The day accommodations spec keeps the legacy statements it holds the repository to behind allow markers.
This commit is contained in:
@@ -52,6 +52,8 @@ import {
|
||||
} from '../../../src/nest/permissions/permissions-cache';
|
||||
import { createTestUnitOfWork } from '../../helpers/test-uow';
|
||||
import { createTestOrm, type TestOrm } from '../../helpers/test-orm';
|
||||
import { deleteRows, findRows } from '../../helpers/factories/rows';
|
||||
import { readAppSetting, setAppSetting } from '../../helpers/factories/settings';
|
||||
import { AppSettings } from '../../../src/db/entities/AppSettings.entity';
|
||||
import type { AppSettingsRepository } from '../../../src/db/repositories/AppSettings.repository';
|
||||
|
||||
@@ -67,7 +69,7 @@ beforeAll(async () => {
|
||||
|
||||
beforeEach(async () => {
|
||||
vi.clearAllMocks();
|
||||
testDb.prepare("DELETE FROM app_settings WHERE key LIKE 'perm_%'").run();
|
||||
await deleteRows(t, AppSettings, { key: { $like: 'perm_%' } });
|
||||
t.clear();
|
||||
await svc.invalidatePermissionsCache();
|
||||
});
|
||||
@@ -155,7 +157,7 @@ describe('savePermissions — invalid input is silently skipped', () => {
|
||||
it('PERM-SVC-011: returns skipped array containing invalid action key, writes no row', async () => {
|
||||
const result = await svc.savePermissions({ nonexistent_action: 'trip_member' });
|
||||
expect(result.skipped).toContain('nonexistent_action');
|
||||
const rows = testDb.prepare("SELECT key FROM app_settings WHERE key LIKE 'perm_%'").all();
|
||||
const rows = await findRows(t, AppSettings, { key: { $like: 'perm_%' } });
|
||||
expect(rows).toEqual([]);
|
||||
});
|
||||
|
||||
@@ -163,15 +165,15 @@ describe('savePermissions — invalid input is silently skipped', () => {
|
||||
// trip_delete only allows ['admin', 'trip_owner'], so 'trip_member' is invalid
|
||||
const result = await svc.savePermissions({ trip_delete: 'trip_member' });
|
||||
expect(result.skipped).toContain('trip_delete');
|
||||
const rows = testDb.prepare("SELECT key FROM app_settings WHERE key LIKE 'perm_%'").all();
|
||||
const rows = await findRows(t, AppSettings, { key: { $like: 'perm_%' } });
|
||||
expect(rows).toEqual([]);
|
||||
});
|
||||
});
|
||||
|
||||
describe('corrupt stored levels', () => {
|
||||
it('PERM-SVC-013: an unrecognized stored level is ignored — every reader falls back to the default', async () => {
|
||||
testDb.prepare('INSERT OR REPLACE INTO app_settings (key, value) VALUES (?, ?)').run('perm_trip_edit', 'unknown_level');
|
||||
testDb.prepare('INSERT OR REPLACE INTO app_settings (key, value) VALUES (?, ?)').run('perm_trip_delete', '');
|
||||
await setAppSetting(t, 'perm_trip_edit', 'unknown_level');
|
||||
await setAppSetting(t, 'perm_trip_delete', '');
|
||||
await svc.invalidatePermissionsCache();
|
||||
// Since the quirk fix the corrupt rows never enter the cache, so
|
||||
// getPermissionLevel, getAllPermissions and checkPermission agree on the
|
||||
@@ -185,7 +187,7 @@ describe('corrupt stored levels', () => {
|
||||
|
||||
it('PERM-SVC-021: a stored level outside the action\'s allowedLevels is ignored too', async () => {
|
||||
// trip_edit only allows trip_owner/trip_member — a raw 'everybody' row must not widen it.
|
||||
testDb.prepare('INSERT OR REPLACE INTO app_settings (key, value) VALUES (?, ?)').run('perm_trip_edit', 'everybody');
|
||||
await setAppSetting(t, 'perm_trip_edit', 'everybody');
|
||||
await svc.invalidatePermissionsCache();
|
||||
expect(await svc.getPermissionLevel('trip_edit')).toBe('trip_owner');
|
||||
expect(await svc.checkPermission('trip_edit', 'user', 10, 30, false)).toBe(false);
|
||||
@@ -227,7 +229,7 @@ describe('load failures', () => {
|
||||
|
||||
// The mock is exhausted after the one queued rejection — the next call
|
||||
// falls through to the real (now-working) repository read.
|
||||
testDb.prepare('INSERT OR REPLACE INTO app_settings (key, value) VALUES (?, ?)').run('perm_trip_edit', 'trip_member');
|
||||
await setAppSetting(t, 'perm_trip_edit', 'trip_member');
|
||||
expect(await svc.getPermissionLevel('trip_edit')).toBe('trip_member');
|
||||
expect(await getPermissionsCache()).not.toBe(null);
|
||||
spy.mockRestore();
|
||||
@@ -256,7 +258,7 @@ describe('load failures', () => {
|
||||
|
||||
it('PERM-SVC-023: an all-skipped save writes nothing and leaves the cache untouched', async () => {
|
||||
expect(await svc.getPermissionLevel('trip_edit')).toBe('trip_owner'); // prime the cache
|
||||
testDb.prepare('INSERT OR REPLACE INTO app_settings (key, value) VALUES (?, ?)').run('perm_trip_edit', 'trip_member');
|
||||
await setAppSetting(t, 'perm_trip_edit', 'trip_member');
|
||||
const result = await svc.savePermissions({ bogus: 'trip_member', trip_delete: 'trip_member' });
|
||||
expect(result.skipped).toEqual(['bogus', 'trip_delete']);
|
||||
// No valid entries → no transaction and no cache flush: the raw row above
|
||||
@@ -271,7 +273,7 @@ describe('load failures', () => {
|
||||
|
||||
describe('stored overrides + cache', () => {
|
||||
it('PERM-SVC-014: stored perm_ row overrides the default after invalidation', async () => {
|
||||
testDb.prepare('INSERT OR REPLACE INTO app_settings (key, value) VALUES (?, ?)').run('perm_trip_edit', 'trip_member');
|
||||
await setAppSetting(t, 'perm_trip_edit', 'trip_member');
|
||||
await svc.invalidatePermissionsCache();
|
||||
expect(await svc.getPermissionLevel('trip_edit')).toBe('trip_member');
|
||||
// A plain member now passes what defaults to a trip_owner-only action.
|
||||
@@ -283,16 +285,15 @@ describe('stored overrides + cache', () => {
|
||||
expect(await svc.getPermissionLevel('trip_edit')).toBe('trip_owner');
|
||||
const result = await svc.savePermissions({ trip_edit: 'trip_member' });
|
||||
expect(result.skipped).toEqual([]);
|
||||
const row = testDb.prepare('SELECT value FROM app_settings WHERE key = ?').get('perm_trip_edit') as { value: string };
|
||||
expect(row.value).toBe('trip_member');
|
||||
expect(await readAppSetting(t, 'perm_trip_edit')).toBe('trip_member');
|
||||
// No manual invalidation — savePermissions did it.
|
||||
expect(await svc.getPermissionLevel('trip_edit')).toBe('trip_member');
|
||||
});
|
||||
|
||||
it('PERM-SVC-016: the cache memoizes until invalidated', async () => {
|
||||
expect(await svc.getPermissionLevel('trip_edit')).toBe('trip_owner');
|
||||
// Raw SQL write bypasses savePermissions' self-invalidation → stale value served.
|
||||
testDb.prepare('INSERT OR REPLACE INTO app_settings (key, value) VALUES (?, ?)').run('perm_trip_edit', 'trip_member');
|
||||
// A direct row write bypasses savePermissions' self-invalidation → stale value served.
|
||||
await setAppSetting(t, 'perm_trip_edit', 'trip_member');
|
||||
expect(await svc.getPermissionLevel('trip_edit')).toBe('trip_owner');
|
||||
await svc.invalidatePermissionsCache();
|
||||
expect(await svc.getPermissionLevel('trip_edit')).toBe('trip_member');
|
||||
@@ -322,10 +323,10 @@ describe('module-scoped permissions cache', () => {
|
||||
// (checkPermission for a plain member flips with the stored level).
|
||||
await svc.savePermissions({ trip_edit: 'trip_member' });
|
||||
expect(await secondInstance.checkPermission('trip_edit', 'user', 10, 20, true)).toBe(true);
|
||||
// Raw SQL write, then invalidate through permissions-cache — the plain
|
||||
// A direct row write, then invalidate through permissions-cache — the plain
|
||||
// function backup.impl.ts calls after a restore. Both service instances
|
||||
// must serve the fresh value afterwards.
|
||||
testDb.prepare('INSERT OR REPLACE INTO app_settings (key, value) VALUES (?, ?)').run('perm_trip_edit', 'trip_owner');
|
||||
await setAppSetting(t, 'perm_trip_edit', 'trip_owner');
|
||||
expect(await svc.getPermissionLevel('trip_edit')).toBe('trip_member'); // still cached
|
||||
await invalidateSharedCache();
|
||||
expect(await svc.getPermissionLevel('trip_edit')).toBe('trip_owner');
|
||||
|
||||
Reference in New Issue
Block a user