mirror of
https://github.com/mauriceboe/TREK.git
synced 2026-11-27 07:07:31 +00:00
refactor(server): make the state ports async and route every reader through the provided store
The process-state ports were meant to let a store shared between processes replace the in-memory one, but they could not. The readers outside the container (the broadcast functions, the backup restore's permissions-cache flush, the pending-code sweep, hand-built consumers) went straight to the module-level in-memory instances, so an overridden provider was used by the gateway and the services while the broadcasts and the restore flush kept using the old store. And every port was synchronous, while a store in the database or on the network is async, so plugging one in would have changed every signature anyway. RateLimitStore, PendingCodeStore, OidcFlowStore and PermissionsCacheStore now return promises, and RateLimitService, OauthService, OidcService, PermissionsService and their callers await them. The in-memory stores still decide synchronously, so who gets a 429, a code or a cached level is unchanged. RoomRegistry stays synchronous: membership holds live sockets and broadcast() is a synchronous call every domain makes, so several processes need a fan-out in front of broadcast(), not a different place for the sets. The process-wide ports get a ProcessStoreSlot. PermissionsModule, OauthModule and RealtimeGatewayModule install the store the container resolved and release it on shutdown, and the free functions, the sweep and hand-built consumers read the slot, so overriding the provider changes every reader. Tests provide fake stores and check that the broadcasts, the restore flush and the sweep use them. Two callers needed more than an await. The transit MCP tools counted in a RateLimitService of their own that no provider could reach; they now take the one RateLimitModule provides (their mcp_transit_* buckets never overlap the REST ones). The link preview claimed its in-flight slot only after the budget check, which now yields, so simultaneous askers would each have been charged; it now claims the slot first, and an asker that joined someone out of budget asks again on its own budget. The unused putPendingCode/takePendingCode helpers are gone.
This commit is contained in:
@@ -36,9 +36,9 @@ beforeAll(async () => {
|
||||
app = nestApp.getHttpAdapter().getInstance();
|
||||
});
|
||||
|
||||
beforeEach(() => {
|
||||
beforeEach(async () => {
|
||||
resetTestDb(testDb);
|
||||
resetRateLimits(nestApp);
|
||||
await resetRateLimits(nestApp);
|
||||
});
|
||||
|
||||
afterAll(async () => {
|
||||
|
||||
@@ -36,9 +36,9 @@ beforeAll(async () => {
|
||||
app = nestApp.getHttpAdapter().getInstance();
|
||||
});
|
||||
|
||||
beforeEach(() => {
|
||||
beforeEach(async () => {
|
||||
resetTestDb(testDb);
|
||||
resetRateLimits(nestApp);
|
||||
await resetRateLimits(nestApp);
|
||||
});
|
||||
|
||||
afterAll(async () => {
|
||||
|
||||
@@ -64,9 +64,9 @@ beforeAll(async () => {
|
||||
await getRegionGeo(['ZZ']);
|
||||
}, 60_000);
|
||||
|
||||
beforeEach(() => {
|
||||
beforeEach(async () => {
|
||||
resetTestDb(testDb);
|
||||
resetRateLimits(nestApp);
|
||||
await resetRateLimits(nestApp);
|
||||
});
|
||||
|
||||
afterAll(async () => {
|
||||
|
||||
@@ -42,10 +42,10 @@ beforeAll(async () => {
|
||||
app = nestApp.getHttpAdapter().getInstance();
|
||||
});
|
||||
|
||||
beforeEach(() => {
|
||||
beforeEach(async () => {
|
||||
resetTestDb(testDb);
|
||||
// Reset rate limiter state between tests so they don't interfere
|
||||
resetRateLimits(nestApp);
|
||||
await resetRateLimits(nestApp);
|
||||
});
|
||||
|
||||
afterAll(async () => {
|
||||
|
||||
@@ -76,9 +76,9 @@ beforeAll(async () => {
|
||||
app = nestApp.getHttpAdapter().getInstance();
|
||||
});
|
||||
|
||||
beforeEach(() => {
|
||||
beforeEach(async () => {
|
||||
resetTestDb(testDb);
|
||||
resetRateLimits(nestApp);
|
||||
await resetRateLimits(nestApp);
|
||||
});
|
||||
|
||||
afterAll(async () => {
|
||||
|
||||
@@ -36,9 +36,9 @@ beforeAll(async () => {
|
||||
app = nestApp.getHttpAdapter().getInstance();
|
||||
});
|
||||
|
||||
beforeEach(() => {
|
||||
beforeEach(async () => {
|
||||
resetTestDb(testDb);
|
||||
resetRateLimits(nestApp);
|
||||
await resetRateLimits(nestApp);
|
||||
// GET /budget/settlement reaches ExchangeRatesService.getRates unconditionally
|
||||
// (budget.service.ts settlement()), and that is a real fetch to
|
||||
// api.frankfurter.dev with a 10 s abort. Without this stub the suite talks to
|
||||
@@ -404,7 +404,7 @@ describe('Reorder budget items', () => {
|
||||
// Restrict budget_edit to trip_owner only
|
||||
testDb.prepare("INSERT OR REPLACE INTO app_settings (key, value) VALUES ('perm_budget_edit', 'trip_owner')").run();
|
||||
const { invalidatePermissionsCache } = await import('../../src/nest/permissions/permissions-cache');
|
||||
invalidatePermissionsCache();
|
||||
await invalidatePermissionsCache();
|
||||
|
||||
const res = await request(app)
|
||||
.put(`/api/trips/${trip.id}/budget/reorder/items`)
|
||||
@@ -414,7 +414,7 @@ describe('Reorder budget items', () => {
|
||||
|
||||
// Restore default
|
||||
testDb.prepare("DELETE FROM app_settings WHERE key = 'perm_budget_edit'").run();
|
||||
invalidatePermissionsCache();
|
||||
await invalidatePermissionsCache();
|
||||
});
|
||||
|
||||
it('BUDGET-013 — owner can reorder budget items — returns 200', async () => {
|
||||
@@ -508,7 +508,7 @@ describe('Budget edit permission enforcement', () => {
|
||||
|
||||
const { invalidatePermissionsCache } = await import('../../src/nest/permissions/permissions-cache');
|
||||
testDb.prepare("INSERT OR REPLACE INTO app_settings (key, value) VALUES ('perm_budget_edit', 'trip_owner')").run();
|
||||
invalidatePermissionsCache();
|
||||
await invalidatePermissionsCache();
|
||||
|
||||
const res = await request(app)
|
||||
.post(`/api/trips/${trip.id}/budget`)
|
||||
@@ -517,7 +517,7 @@ describe('Budget edit permission enforcement', () => {
|
||||
expect(res.status).toBe(403);
|
||||
|
||||
testDb.prepare("DELETE FROM app_settings WHERE key = 'perm_budget_edit'").run();
|
||||
invalidatePermissionsCache();
|
||||
await invalidatePermissionsCache();
|
||||
});
|
||||
|
||||
it('BUDGET-018 — member cannot reorder categories when budget_edit is restricted to trip_owner', async () => {
|
||||
@@ -529,7 +529,7 @@ describe('Budget edit permission enforcement', () => {
|
||||
|
||||
const { invalidatePermissionsCache } = await import('../../src/nest/permissions/permissions-cache');
|
||||
testDb.prepare("INSERT OR REPLACE INTO app_settings (key, value) VALUES ('perm_budget_edit', 'trip_owner')").run();
|
||||
invalidatePermissionsCache();
|
||||
await invalidatePermissionsCache();
|
||||
|
||||
const res = await request(app)
|
||||
.put(`/api/trips/${trip.id}/budget/reorder/categories`)
|
||||
@@ -538,6 +538,6 @@ describe('Budget edit permission enforcement', () => {
|
||||
expect(res.status).toBe(403);
|
||||
|
||||
testDb.prepare("DELETE FROM app_settings WHERE key = 'perm_budget_edit'").run();
|
||||
invalidatePermissionsCache();
|
||||
await invalidatePermissionsCache();
|
||||
});
|
||||
});
|
||||
|
||||
@@ -36,9 +36,9 @@ beforeAll(async () => {
|
||||
app = nestApp.getHttpAdapter().getInstance();
|
||||
});
|
||||
|
||||
beforeEach(() => {
|
||||
beforeEach(async () => {
|
||||
resetTestDb(testDb);
|
||||
resetRateLimits(nestApp);
|
||||
await resetRateLimits(nestApp);
|
||||
});
|
||||
|
||||
afterAll(async () => {
|
||||
|
||||
@@ -55,9 +55,9 @@ beforeAll(async () => {
|
||||
if (!fs.existsSync(uploadsDir)) fs.mkdirSync(uploadsDir, { recursive: true });
|
||||
});
|
||||
|
||||
beforeEach(() => {
|
||||
beforeEach(async () => {
|
||||
resetTestDb(testDb);
|
||||
resetRateLimits(nestApp);
|
||||
await resetRateLimits(nestApp);
|
||||
});
|
||||
|
||||
afterAll(async () => {
|
||||
|
||||
@@ -53,9 +53,9 @@ beforeAll(async () => {
|
||||
app = nestApp.getHttpAdapter().getInstance();
|
||||
});
|
||||
|
||||
beforeEach(() => {
|
||||
beforeEach(async () => {
|
||||
resetTestDb(testDb);
|
||||
resetRateLimits(nestApp);
|
||||
await resetRateLimits(nestApp);
|
||||
// Enable the collections addon (the controller sits behind AddonGuard).
|
||||
testDb.prepare(
|
||||
"INSERT OR REPLACE INTO addons (id, name, description, type, icon, enabled, sort_order) VALUES ('collections', 'Collections', 'Saved places', 'global', 'Bookmark', 1, 40)"
|
||||
|
||||
@@ -36,9 +36,9 @@ beforeAll(async () => {
|
||||
app = nestApp.getHttpAdapter().getInstance();
|
||||
});
|
||||
|
||||
beforeEach(() => {
|
||||
beforeEach(async () => {
|
||||
resetTestDb(testDb);
|
||||
resetRateLimits(nestApp);
|
||||
await resetRateLimits(nestApp);
|
||||
});
|
||||
|
||||
afterAll(async () => {
|
||||
|
||||
@@ -37,7 +37,7 @@ beforeAll(async () => {
|
||||
nestApp = await buildApp();
|
||||
app = nestApp.getHttpAdapter().getInstance();
|
||||
});
|
||||
beforeEach(() => { resetTestDb(testDb); resetRateLimits(nestApp); });
|
||||
beforeEach(async () => { resetTestDb(testDb); await resetRateLimits(nestApp); });
|
||||
afterAll(async () => {
|
||||
await nestApp.close();
|
||||
testDb.close();
|
||||
@@ -378,7 +378,7 @@ describe('Delete day', () => {
|
||||
createDay(testDb, trip.id);
|
||||
testDb.prepare("INSERT OR REPLACE INTO app_settings (key, value) VALUES ('perm_day_edit', 'trip_owner')").run();
|
||||
const { invalidatePermissionsCache } = await import('../../src/nest/permissions/permissions-cache');
|
||||
invalidatePermissionsCache();
|
||||
await invalidatePermissionsCache();
|
||||
try {
|
||||
const res = await request(app)
|
||||
.delete(`/api/trips/${trip.id}/days/${day.id}`)
|
||||
@@ -387,7 +387,7 @@ describe('Delete day', () => {
|
||||
expect(testDb.prepare('SELECT id FROM days WHERE id = ?').get(day.id)).toBeDefined();
|
||||
} finally {
|
||||
testDb.prepare("DELETE FROM app_settings WHERE key = 'perm_day_edit'").run();
|
||||
invalidatePermissionsCache();
|
||||
await invalidatePermissionsCache();
|
||||
}
|
||||
});
|
||||
});
|
||||
|
||||
@@ -51,9 +51,9 @@ beforeAll(async () => {
|
||||
testDb.prepare("INSERT OR REPLACE INTO app_settings (key, value) VALUES ('allowed_file_types', '*')").run();
|
||||
});
|
||||
|
||||
beforeEach(() => {
|
||||
beforeEach(async () => {
|
||||
resetTestDb(testDb);
|
||||
resetRateLimits(nestApp);
|
||||
await resetRateLimits(nestApp);
|
||||
// Re-seed allowed_file_types after reset
|
||||
testDb.prepare("INSERT OR REPLACE INTO app_settings (key, value) VALUES ('allowed_file_types', '*')").run();
|
||||
});
|
||||
|
||||
@@ -63,9 +63,9 @@ beforeAll(async () => {
|
||||
app = nestApp.getHttpAdapter().getInstance();
|
||||
});
|
||||
|
||||
beforeEach(() => {
|
||||
beforeEach(async () => {
|
||||
resetTestDb(testDb);
|
||||
resetRateLimits(nestApp);
|
||||
await resetRateLimits(nestApp);
|
||||
// Providers only count as enabled under an enabled journey addon (migration 84 seeds it off).
|
||||
setAddonEnabled(testDb, 'journey', true);
|
||||
// The migrated snapshot seeds photo_providers.immich.enabled = 0 (an admin must
|
||||
|
||||
@@ -54,10 +54,10 @@ beforeAll(async () => {
|
||||
nestApp = await buildApp();
|
||||
app = nestApp.getHttpAdapter().getInstance();
|
||||
});
|
||||
beforeEach(() => {
|
||||
beforeEach(async () => {
|
||||
resetTestDb(testDb);
|
||||
resetRateLimits(nestApp);
|
||||
invalidatePermissionsCache();
|
||||
await resetRateLimits(nestApp);
|
||||
await invalidatePermissionsCache();
|
||||
// Enable the journey addon.
|
||||
testDb.prepare(
|
||||
"INSERT OR REPLACE INTO addons (id, name, description, type, icon, enabled, sort_order) VALUES ('journey', 'Journey', 'Travel journal', 'global', 'Compass', 1, 35)"
|
||||
|
||||
@@ -51,10 +51,10 @@ beforeAll(async () => {
|
||||
nestApp = await buildApp();
|
||||
app = nestApp.getHttpAdapter().getInstance();
|
||||
});
|
||||
beforeEach(() => {
|
||||
beforeEach(async () => {
|
||||
resetTestDb(testDb);
|
||||
resetRateLimits(nestApp);
|
||||
invalidatePermissionsCache();
|
||||
await resetRateLimits(nestApp);
|
||||
await invalidatePermissionsCache();
|
||||
// Enable the journey addon
|
||||
testDb.prepare(
|
||||
"INSERT OR REPLACE INTO addons (id, name, description, type, icon, enabled, sort_order) VALUES ('journey', 'Journey', 'Travel journal', 'global', 'Compass', 1, 35)"
|
||||
|
||||
@@ -43,9 +43,9 @@ describe('incoming_leg_transport_mode read-path parity', () => {
|
||||
app = nestApp.getHttpAdapter().getInstance();
|
||||
});
|
||||
|
||||
beforeEach(() => {
|
||||
beforeEach(async () => {
|
||||
resetTestDb(testDb2);
|
||||
resetRateLimits(nestApp);
|
||||
await resetRateLimits(nestApp);
|
||||
});
|
||||
|
||||
afterAll(async () => {
|
||||
|
||||
@@ -64,9 +64,9 @@ beforeAll(async () => {
|
||||
};
|
||||
});
|
||||
|
||||
beforeEach(() => {
|
||||
beforeEach(async () => {
|
||||
resetTestDb(testDb);
|
||||
resetRateLimits(nestApp);
|
||||
await resetRateLimits(nestApp);
|
||||
});
|
||||
|
||||
afterAll(async () => {
|
||||
|
||||
@@ -79,9 +79,9 @@ beforeAll(async () => {
|
||||
oauthSvc = new OauthService(t.repo(OauthClients), t.repo(OauthTokens), t.repo(OauthConsents), await createTestAddonsService(testDb), new AuditService(t.repo(AuditLog), t.repo(Users)));
|
||||
});
|
||||
|
||||
beforeEach(() => {
|
||||
beforeEach(async () => {
|
||||
resetTestDb(testDb);
|
||||
resetRateLimits(nestApp);
|
||||
await resetRateLimits(nestApp);
|
||||
});
|
||||
|
||||
afterAll(async () => {
|
||||
|
||||
@@ -214,9 +214,9 @@ beforeAll(async () => {
|
||||
app = nestApp.getHttpAdapter().getInstance();
|
||||
});
|
||||
|
||||
beforeEach(() => {
|
||||
beforeEach(async () => {
|
||||
resetTestDb(testDb);
|
||||
resetRateLimits(nestApp);
|
||||
await resetRateLimits(nestApp);
|
||||
// Providers only count as enabled under an enabled journey addon (migration 84 seeds it off).
|
||||
setAddonEnabled(testDb, 'journey', true);
|
||||
// The migrated snapshot seeds photo_providers.immich.enabled = 0 (an admin must
|
||||
|
||||
@@ -197,9 +197,9 @@ beforeAll(async () => {
|
||||
app = nestApp.getHttpAdapter().getInstance();
|
||||
});
|
||||
|
||||
beforeEach(() => {
|
||||
beforeEach(async () => {
|
||||
resetTestDb(testDb);
|
||||
resetRateLimits(nestApp);
|
||||
await resetRateLimits(nestApp);
|
||||
});
|
||||
|
||||
afterAll(async () => {
|
||||
|
||||
@@ -52,9 +52,9 @@ beforeAll(async () => {
|
||||
app = nestApp.getHttpAdapter().getInstance();
|
||||
});
|
||||
|
||||
beforeEach(() => {
|
||||
beforeEach(async () => {
|
||||
resetTestDb(testDb);
|
||||
resetRateLimits(nestApp);
|
||||
await resetRateLimits(nestApp);
|
||||
// Providers only count as enabled under an enabled journey addon (migration 84 seeds it off).
|
||||
setAddonEnabled(testDb, 'journey', true);
|
||||
// The migrated snapshot seeds photo_providers.immich.enabled = 0 (an admin must
|
||||
|
||||
@@ -36,9 +36,9 @@ beforeAll(async () => {
|
||||
app = nestApp.getHttpAdapter().getInstance();
|
||||
});
|
||||
|
||||
beforeEach(() => {
|
||||
beforeEach(async () => {
|
||||
resetTestDb(testDb);
|
||||
resetRateLimits(nestApp);
|
||||
await resetRateLimits(nestApp);
|
||||
});
|
||||
|
||||
afterAll(async () => {
|
||||
|
||||
@@ -52,9 +52,9 @@ beforeAll(async () => {
|
||||
app = nestApp.getHttpAdapter().getInstance();
|
||||
});
|
||||
|
||||
beforeEach(() => {
|
||||
beforeEach(async () => {
|
||||
resetTestDb(testDb);
|
||||
resetRateLimits(nestApp);
|
||||
await resetRateLimits(nestApp);
|
||||
});
|
||||
|
||||
afterAll(async () => {
|
||||
|
||||
@@ -89,9 +89,9 @@ beforeAll(async () => {
|
||||
containerSideOauth = new OauthService(t.repo(OauthClients), t.repo(OauthTokens), t.repo(OauthConsents), await createTestAddonsService(testDb), new AuditService(t.repo(AuditLog), t.repo(Users)));
|
||||
});
|
||||
|
||||
beforeEach(() => {
|
||||
beforeEach(async () => {
|
||||
resetTestDb(testDb);
|
||||
resetRateLimits(nestApp);
|
||||
await resetRateLimits(nestApp);
|
||||
setMcpEnabled(true);
|
||||
});
|
||||
|
||||
@@ -273,7 +273,7 @@ describe('GET /oauth/authorize — SDK authorizationHandler over trekOAuthProvid
|
||||
// 2. the consent controller writes the code through the container
|
||||
// singleton; the module-scoped pending-code map is what lets the
|
||||
// SDK-side exchange see it.
|
||||
const code = containerSideOauth.createAuthCode({
|
||||
const code = await containerSideOauth.createAuthCode({
|
||||
clientId,
|
||||
userId: user.id,
|
||||
redirectUri: 'https://client.example.com/cb',
|
||||
|
||||
@@ -87,9 +87,9 @@ beforeAll(async () => {
|
||||
containerSideOauth = new OauthService(t.repo(OauthClients), t.repo(OauthTokens), t.repo(OauthConsents), await createTestAddonsService(testDb), new AuditService(t.repo(AuditLog), t.repo(Users)));
|
||||
});
|
||||
|
||||
beforeEach(() => {
|
||||
beforeEach(async () => {
|
||||
resetTestDb(testDb);
|
||||
resetRateLimits(nestApp);
|
||||
await resetRateLimits(nestApp);
|
||||
setMcpEnabled(true);
|
||||
});
|
||||
|
||||
@@ -324,7 +324,7 @@ describe('POST /oauth/token — authorization_code grant', () => {
|
||||
const { verifier, challenge } = makePkce();
|
||||
|
||||
// Create code for client1
|
||||
const code = createAuthCode({
|
||||
const code = await createAuthCode({
|
||||
clientId: r1.client!.client_id as string,
|
||||
userId: user.id,
|
||||
redirectUri: 'https://app1.example.com/cb',
|
||||
@@ -354,7 +354,7 @@ describe('POST /oauth/token — authorization_code grant', () => {
|
||||
const r = await createOAuthClient(user.id, 'App', ['https://app.example.com/cb'], ['trips:read']);
|
||||
const { verifier, challenge } = makePkce();
|
||||
|
||||
const code = createAuthCode({
|
||||
const code = await createAuthCode({
|
||||
clientId: r.client!.client_id as string,
|
||||
userId: user.id,
|
||||
redirectUri: 'https://app.example.com/cb',
|
||||
@@ -383,7 +383,7 @@ describe('POST /oauth/token — authorization_code grant', () => {
|
||||
const r = await createOAuthClient(user.id, 'App', ['https://app.example.com/cb'], ['trips:read']);
|
||||
const { verifier, challenge } = makePkce();
|
||||
|
||||
const code = createAuthCode({
|
||||
const code = await createAuthCode({
|
||||
clientId: r.client!.client_id as string,
|
||||
userId: user.id,
|
||||
redirectUri: 'https://app.example.com/cb',
|
||||
@@ -412,7 +412,7 @@ describe('POST /oauth/token — authorization_code grant', () => {
|
||||
const r = await createOAuthClient(user.id, 'App', ['https://app.example.com/cb'], ['trips:read']);
|
||||
const { challenge } = makePkce();
|
||||
|
||||
const code = createAuthCode({
|
||||
const code = await createAuthCode({
|
||||
clientId: r.client!.client_id as string,
|
||||
userId: user.id,
|
||||
redirectUri: 'https://app.example.com/cb',
|
||||
@@ -441,7 +441,7 @@ describe('POST /oauth/token — authorization_code grant', () => {
|
||||
const r = await createOAuthClient(user.id, 'App', ['https://app.example.com/cb'], ['trips:read']);
|
||||
const { verifier, challenge } = makePkce();
|
||||
|
||||
const code = createAuthCode({
|
||||
const code = await createAuthCode({
|
||||
clientId: r.client!.client_id as string,
|
||||
userId: user.id,
|
||||
redirectUri: 'https://app.example.com/cb',
|
||||
@@ -504,7 +504,7 @@ describe('POST /oauth/token — refresh_token grant', () => {
|
||||
const r = await createOAuthClient(user.id, 'App', ['https://app.example.com/cb'], ['trips:read']);
|
||||
const { verifier, challenge } = makePkce();
|
||||
|
||||
const code = createAuthCode({
|
||||
const code = await createAuthCode({
|
||||
clientId: r.client!.client_id as string,
|
||||
userId: user.id,
|
||||
redirectUri: 'https://app.example.com/cb',
|
||||
@@ -596,7 +596,7 @@ describe('POST /oauth/revoke', () => {
|
||||
const r = await createOAuthClient(user.id, 'App', ['https://app.example.com/cb'], ['trips:read']);
|
||||
const { verifier, challenge } = makePkce();
|
||||
|
||||
const code = createAuthCode({
|
||||
const code = await createAuthCode({
|
||||
clientId: r.client!.client_id as string,
|
||||
userId: user.id,
|
||||
redirectUri: 'https://app.example.com/cb',
|
||||
@@ -1093,7 +1093,7 @@ describe('Sessions — /api/oauth/sessions', () => {
|
||||
const r = await createOAuthClient(user.id, 'App', ['https://app.example.com/cb'], ['trips:read']);
|
||||
const { verifier, challenge } = makePkce();
|
||||
|
||||
const code = createAuthCode({
|
||||
const code = await createAuthCode({
|
||||
clientId: r.client!.client_id as string,
|
||||
userId: user.id,
|
||||
redirectUri: 'https://app.example.com/cb',
|
||||
@@ -1211,7 +1211,7 @@ describe('H1 — PKCE format validation', () => {
|
||||
const r = await createOAuthClient(user.id, 'App', ['https://app.example.com/cb'], ['trips:read']);
|
||||
const { challenge } = makePkce();
|
||||
|
||||
const code = createAuthCode({
|
||||
const code = await createAuthCode({
|
||||
clientId: r.client!.client_id as string,
|
||||
userId: user.id,
|
||||
redirectUri: 'https://app.example.com/cb',
|
||||
@@ -1269,7 +1269,7 @@ describe('H5 — All invalid_grant cases return identical response body', () =>
|
||||
const r = await createOAuthClient(user.id, 'App', ['https://app.example.com/cb'], ['trips:read']);
|
||||
const { verifier, challenge } = makePkce();
|
||||
|
||||
const code = createAuthCode({
|
||||
const code = await createAuthCode({
|
||||
clientId: r.client!.client_id as string,
|
||||
userId: user.id,
|
||||
redirectUri: 'https://app.example.com/cb',
|
||||
@@ -1290,7 +1290,7 @@ describe('H5 — All invalid_grant cases return identical response body', () =>
|
||||
});
|
||||
|
||||
// Redirect URI mismatch (need fresh code since code is single-use)
|
||||
const code2 = createAuthCode({
|
||||
const code2 = await createAuthCode({
|
||||
clientId: r.client!.client_id as string,
|
||||
userId: user.id,
|
||||
redirectUri: 'https://app.example.com/cb',
|
||||
@@ -1427,7 +1427,7 @@ describe('C3 — Refresh token replay detection', () => {
|
||||
const r = await createOAuthClient(user.id, 'App', ['https://app.example.com/cb'], ['trips:read']);
|
||||
const { verifier, challenge } = makePkce();
|
||||
|
||||
const code = createAuthCode({
|
||||
const code = await createAuthCode({
|
||||
clientId: r.client!.client_id as string,
|
||||
userId: user.id,
|
||||
redirectUri: 'https://app.example.com/cb',
|
||||
@@ -1475,7 +1475,7 @@ describe('C3 — Refresh token replay detection', () => {
|
||||
const r = await createOAuthClient(user.id, 'App', ['https://app.example.com/cb'], ['trips:read']);
|
||||
const { verifier, challenge } = makePkce();
|
||||
|
||||
const code = createAuthCode({
|
||||
const code = await createAuthCode({
|
||||
clientId: r.client!.client_id as string,
|
||||
userId: user.id,
|
||||
redirectUri: 'https://app.example.com/cb',
|
||||
@@ -1515,7 +1515,7 @@ describe('C3 — Refresh token replay detection', () => {
|
||||
const r = await createOAuthClient(user.id, 'App', ['https://app.example.com/cb'], ['trips:read']);
|
||||
const { verifier, challenge } = makePkce();
|
||||
|
||||
const code = createAuthCode({
|
||||
const code = await createAuthCode({
|
||||
clientId: r.client!.client_id as string,
|
||||
userId: user.id,
|
||||
redirectUri: 'https://app.example.com/cb',
|
||||
|
||||
@@ -149,9 +149,9 @@ beforeAll(async () => {
|
||||
app = nestApp.getHttpAdapter().getInstance();
|
||||
});
|
||||
|
||||
beforeEach(() => {
|
||||
beforeEach(async () => {
|
||||
resetTestDb(testDb);
|
||||
resetRateLimits(nestApp);
|
||||
await resetRateLimits(nestApp);
|
||||
seen.length = 0;
|
||||
answers.clear();
|
||||
process.env.OIDC_CLIENT_ID = CLIENT_ID;
|
||||
|
||||
@@ -80,9 +80,9 @@ beforeAll(async () => {
|
||||
mockVerifyIdToken = vi.spyOn(oidcSvc, 'verifyIdToken').mockImplementation(async () => undefined as never);
|
||||
});
|
||||
|
||||
beforeEach(() => {
|
||||
beforeEach(async () => {
|
||||
resetTestDb(testDb);
|
||||
resetRateLimits(nestApp);
|
||||
await resetRateLimits(nestApp);
|
||||
vi.clearAllMocks();
|
||||
|
||||
// Set OIDC environment variables for each test
|
||||
@@ -163,7 +163,7 @@ describe('GET /api/auth/oidc/callback', () => {
|
||||
});
|
||||
|
||||
// Create a valid state token
|
||||
const { state } = oidcSvc.createState('http://localhost:3001/api/auth/oidc/callback');
|
||||
const { state } = await oidcSvc.createState('http://localhost:3001/api/auth/oidc/callback');
|
||||
|
||||
const res = await request(app).get(`/api/auth/oidc/callback?code=authcode123&state=${state}`).set('Cookie', `trek_oidc_state=${state}`);
|
||||
|
||||
@@ -177,7 +177,7 @@ describe('GET /api/auth/oidc/callback', () => {
|
||||
mockExchangeCode.mockResolvedValueOnce({ access_token: 'test-access-token', id_token: 'fake.id.token', _ok: true, _status: 200 });
|
||||
mockVerifyIdToken.mockResolvedValueOnce({ ok: true, claims: { sub: 'sub-audited-1' } });
|
||||
mockGetUserInfo.mockResolvedValueOnce({ sub: 'sub-audited-1', email: 'audited@example.com', name: 'Audited', email_verified: true });
|
||||
const { state } = oidcSvc.createState('http://localhost:3001/api/auth/oidc/callback');
|
||||
const { state } = await oidcSvc.createState('http://localhost:3001/api/auth/oidc/callback');
|
||||
|
||||
const res = await request(app).get(`/api/auth/oidc/callback?code=authcode123&state=${state}`).set('Cookie', `trek_oidc_state=${state}`);
|
||||
|
||||
@@ -202,7 +202,7 @@ describe('GET /api/auth/oidc/callback', () => {
|
||||
name: 'New User',
|
||||
});
|
||||
|
||||
const { state } = oidcSvc.createState('http://localhost:3001/api/auth/oidc/callback');
|
||||
const { state } = await oidcSvc.createState('http://localhost:3001/api/auth/oidc/callback');
|
||||
|
||||
const res = await request(app).get(`/api/auth/oidc/callback?code=code999&state=${state}`).set('Cookie', `trek_oidc_state=${state}`);
|
||||
|
||||
@@ -245,7 +245,7 @@ describe('GET /api/auth/oidc/callback', () => {
|
||||
mockDiscover.mockResolvedValueOnce(MOCK_DISCOVERY_DOC);
|
||||
mockExchangeCode.mockResolvedValueOnce({ _ok: false, _status: 400 });
|
||||
|
||||
const { state } = oidcSvc.createState('http://localhost:3001/api/auth/oidc/callback');
|
||||
const { state } = await oidcSvc.createState('http://localhost:3001/api/auth/oidc/callback');
|
||||
|
||||
const res = await request(app).get(`/api/auth/oidc/callback?code=badcode&state=${state}`).set('Cookie', `trek_oidc_state=${state}`);
|
||||
|
||||
@@ -257,7 +257,7 @@ describe('GET /api/auth/oidc/callback', () => {
|
||||
mockDiscover.mockResolvedValueOnce(MOCK_DISCOVERY_DOC);
|
||||
mockExchangeCode.mockResolvedValueOnce({ access_token: 'tok', _ok: true, _status: 200 }); // no id_token
|
||||
|
||||
const { state } = oidcSvc.createState('http://localhost:3001/api/auth/oidc/callback');
|
||||
const { state } = await oidcSvc.createState('http://localhost:3001/api/auth/oidc/callback');
|
||||
|
||||
const res = await request(app).get(`/api/auth/oidc/callback?code=anycode&state=${state}`).set('Cookie', `trek_oidc_state=${state}`);
|
||||
|
||||
@@ -270,7 +270,7 @@ describe('GET /api/auth/oidc/callback', () => {
|
||||
mockExchangeCode.mockResolvedValueOnce({ access_token: 'tok', id_token: 'bad.id.token', _ok: true, _status: 200 });
|
||||
mockVerifyIdToken.mockResolvedValueOnce({ ok: false, error: 'signature_or_claim_mismatch: invalid signature' });
|
||||
|
||||
const { state } = oidcSvc.createState('http://localhost:3001/api/auth/oidc/callback');
|
||||
const { state } = await oidcSvc.createState('http://localhost:3001/api/auth/oidc/callback');
|
||||
|
||||
const res = await request(app).get(`/api/auth/oidc/callback?code=anycode&state=${state}`).set('Cookie', `trek_oidc_state=${state}`);
|
||||
|
||||
@@ -288,7 +288,7 @@ describe('GET /api/auth/oidc/callback', () => {
|
||||
name: 'Alice',
|
||||
});
|
||||
|
||||
const { state } = oidcSvc.createState('http://localhost:3001/api/auth/oidc/callback');
|
||||
const { state } = await oidcSvc.createState('http://localhost:3001/api/auth/oidc/callback');
|
||||
|
||||
const res = await request(app).get(`/api/auth/oidc/callback?code=anycode&state=${state}`).set('Cookie', `trek_oidc_state=${state}`);
|
||||
|
||||
@@ -311,7 +311,7 @@ describe('GET /api/auth/oidc/callback', () => {
|
||||
name: 'Blocked',
|
||||
});
|
||||
|
||||
const { state } = oidcSvc.createState('http://localhost:3001/api/auth/oidc/callback');
|
||||
const { state } = await oidcSvc.createState('http://localhost:3001/api/auth/oidc/callback');
|
||||
|
||||
const res = await request(app).get(`/api/auth/oidc/callback?code=anycode&state=${state}`).set('Cookie', `trek_oidc_state=${state}`);
|
||||
|
||||
@@ -325,7 +325,7 @@ describe('GET /api/auth/oidc/callback', () => {
|
||||
describe('GET /api/auth/oidc/exchange', () => {
|
||||
it('OIDC-011: valid auth code returns JWT and sets cookie', async () => {
|
||||
const fakeToken = 'eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.test.sig';
|
||||
const { code, binding } = oidcSvc.createAuthCode(fakeToken);
|
||||
const { code, binding } = await oidcSvc.createAuthCode(fakeToken);
|
||||
|
||||
const res = await request(app)
|
||||
.get(`/api/auth/oidc/exchange?code=${code}`)
|
||||
@@ -353,7 +353,7 @@ describe('GET /api/auth/oidc/exchange', () => {
|
||||
});
|
||||
|
||||
it('OIDC-013b: a real code without its binding cookie returns 400 and buys nothing', async () => {
|
||||
const { code } = oidcSvc.createAuthCode('valid.but.unbound');
|
||||
const { code } = await oidcSvc.createAuthCode('valid.but.unbound');
|
||||
|
||||
const res = await request(app).get(`/api/auth/oidc/exchange?code=${code}`);
|
||||
|
||||
@@ -365,7 +365,7 @@ describe('GET /api/auth/oidc/exchange', () => {
|
||||
|
||||
it('OIDC-014: auth code is single-use (second use returns 400)', async () => {
|
||||
const fakeToken = 'test.token.here';
|
||||
const { code, binding } = oidcSvc.createAuthCode(fakeToken);
|
||||
const { code, binding } = await oidcSvc.createAuthCode(fakeToken);
|
||||
const cookie = `trek_oidc_exchange=${binding}`;
|
||||
|
||||
// First use: success
|
||||
|
||||
@@ -36,9 +36,9 @@ beforeAll(async () => {
|
||||
app = nestApp.getHttpAdapter().getInstance();
|
||||
});
|
||||
|
||||
beforeEach(() => {
|
||||
beforeEach(async () => {
|
||||
resetTestDb(testDb);
|
||||
resetRateLimits(nestApp);
|
||||
await resetRateLimits(nestApp);
|
||||
});
|
||||
|
||||
afterAll(async () => {
|
||||
|
||||
@@ -52,9 +52,9 @@ beforeAll(async () => {
|
||||
app = nestApp.getHttpAdapter().getInstance();
|
||||
});
|
||||
|
||||
beforeEach(() => {
|
||||
beforeEach(async () => {
|
||||
resetTestDb(testDb);
|
||||
resetRateLimits(nestApp);
|
||||
await resetRateLimits(nestApp);
|
||||
});
|
||||
|
||||
afterAll(async () => {
|
||||
|
||||
@@ -56,10 +56,10 @@ beforeAll(async () => {
|
||||
app = nestApp.getHttpAdapter().getInstance();
|
||||
});
|
||||
|
||||
beforeEach(() => {
|
||||
beforeEach(async () => {
|
||||
resetTestDb(testDb);
|
||||
resetRateLimits(nestApp);
|
||||
invalidatePermissionsCache();
|
||||
await resetRateLimits(nestApp);
|
||||
await invalidatePermissionsCache();
|
||||
// Re-attached per test: one describe below calls vi.restoreAllMocks() in its
|
||||
// afterEach, which would otherwise strip these for every later test.
|
||||
importGoogleList = vi.spyOn(nestApp.get(PlacesService), 'importGoogleList');
|
||||
@@ -984,7 +984,7 @@ describe('Delete place — permission edge cases', () => {
|
||||
|
||||
// Restrict place edits to trip owner only
|
||||
testDb.prepare("INSERT OR REPLACE INTO app_settings (key, value) VALUES ('perm_place_edit', 'trip_owner')").run();
|
||||
invalidatePermissionsCache();
|
||||
await invalidatePermissionsCache();
|
||||
|
||||
const res = await request(app)
|
||||
.delete(`/api/trips/${trip.id}/places/${place.id}`)
|
||||
|
||||
@@ -39,9 +39,9 @@ beforeAll(async () => {
|
||||
app = nestApp.getHttpAdapter().getInstance();
|
||||
});
|
||||
|
||||
beforeEach(() => {
|
||||
beforeEach(async () => {
|
||||
resetTestDb(testDb);
|
||||
resetRateLimits(nestApp);
|
||||
await resetRateLimits(nestApp);
|
||||
});
|
||||
|
||||
afterAll(async () => {
|
||||
|
||||
@@ -36,9 +36,9 @@ beforeAll(async () => {
|
||||
app = nestApp.getHttpAdapter().getInstance();
|
||||
});
|
||||
|
||||
beforeEach(() => {
|
||||
beforeEach(async () => {
|
||||
resetTestDb(testDb);
|
||||
resetRateLimits(nestApp);
|
||||
await resetRateLimits(nestApp);
|
||||
});
|
||||
|
||||
afterAll(async () => {
|
||||
|
||||
@@ -47,9 +47,9 @@ beforeAll(async () => {
|
||||
testDb.prepare("INSERT OR REPLACE INTO app_settings (key, value) VALUES ('allowed_file_types', '*')").run();
|
||||
});
|
||||
|
||||
beforeEach(() => {
|
||||
beforeEach(async () => {
|
||||
resetTestDb(testDb);
|
||||
resetRateLimits(nestApp);
|
||||
await resetRateLimits(nestApp);
|
||||
testDb.prepare("INSERT OR REPLACE INTO app_settings (key, value) VALUES ('allowed_file_types', '*')").run();
|
||||
});
|
||||
|
||||
|
||||
@@ -36,9 +36,9 @@ beforeAll(async () => {
|
||||
app = nestApp.getHttpAdapter().getInstance();
|
||||
});
|
||||
|
||||
beforeEach(() => {
|
||||
beforeEach(async () => {
|
||||
resetTestDb(testDb);
|
||||
resetRateLimits(nestApp);
|
||||
await resetRateLimits(nestApp);
|
||||
});
|
||||
|
||||
afterAll(async () => {
|
||||
|
||||
@@ -69,9 +69,9 @@ beforeAll(async () => {
|
||||
placePhotoCache = new PlacePhotoCacheService(testStorage, t.repo(GooglePlacePhotoMeta), t.repo(Places), t.repo(CollectionPlaces));
|
||||
});
|
||||
|
||||
beforeEach(() => {
|
||||
beforeEach(async () => {
|
||||
resetTestDb(testDb);
|
||||
resetRateLimits(nestApp);
|
||||
await resetRateLimits(nestApp);
|
||||
});
|
||||
|
||||
afterAll(async () => {
|
||||
@@ -309,7 +309,7 @@ describe('Shared trip access', () => {
|
||||
|
||||
const { invalidatePermissionsCache } = await import('../../src/nest/permissions/permissions-cache');
|
||||
testDb.prepare("INSERT OR REPLACE INTO app_settings (key, value) VALUES ('perm_share_manage', 'trip_member')").run();
|
||||
invalidatePermissionsCache();
|
||||
await invalidatePermissionsCache();
|
||||
try {
|
||||
const res = await request(app).get(`/api/trips/${trip.id}/share-link`).set('Cookie', authCookie(member.id));
|
||||
expect(res.status).toBe(200);
|
||||
@@ -317,7 +317,7 @@ describe('Shared trip access', () => {
|
||||
} finally {
|
||||
// Module-scoped cache: leaving it set would decide the next file's tests.
|
||||
testDb.prepare("DELETE FROM app_settings WHERE key = 'perm_share_manage'").run();
|
||||
invalidatePermissionsCache();
|
||||
await invalidatePermissionsCache();
|
||||
}
|
||||
});
|
||||
});
|
||||
|
||||
@@ -36,9 +36,9 @@ beforeAll(async () => {
|
||||
app = nestApp.getHttpAdapter().getInstance();
|
||||
});
|
||||
|
||||
beforeEach(() => {
|
||||
beforeEach(async () => {
|
||||
resetTestDb(testDb);
|
||||
resetRateLimits(nestApp);
|
||||
await resetRateLimits(nestApp);
|
||||
});
|
||||
|
||||
afterAll(async () => {
|
||||
|
||||
@@ -37,10 +37,10 @@ beforeAll(async () => {
|
||||
app = nestApp.getHttpAdapter().getInstance();
|
||||
});
|
||||
|
||||
beforeEach(() => {
|
||||
beforeEach(async () => {
|
||||
resetTestDb(testDb);
|
||||
resetRateLimits(nestApp);
|
||||
invalidatePermissionsCache();
|
||||
await resetRateLimits(nestApp);
|
||||
await invalidatePermissionsCache();
|
||||
});
|
||||
|
||||
afterAll(async () => {
|
||||
|
||||
@@ -55,9 +55,9 @@ beforeAll(async () => {
|
||||
nestApp = await buildApp();
|
||||
app = nestApp.getHttpAdapter().getInstance();
|
||||
});
|
||||
beforeEach(() => {
|
||||
beforeEach(async () => {
|
||||
resetTestDb(testDb);
|
||||
resetRateLimits(nestApp);
|
||||
await resetRateLimits(nestApp);
|
||||
});
|
||||
afterAll(async () => {
|
||||
closeMcpSessions();
|
||||
|
||||
@@ -39,10 +39,10 @@ beforeAll(async () => {
|
||||
nestApp = await buildApp();
|
||||
app = nestApp.getHttpAdapter().getInstance();
|
||||
});
|
||||
beforeEach(() => {
|
||||
beforeEach(async () => {
|
||||
resetTestDb(testDb);
|
||||
resetRateLimits(nestApp);
|
||||
invalidatePermissionsCache();
|
||||
await resetRateLimits(nestApp);
|
||||
await invalidatePermissionsCache();
|
||||
});
|
||||
afterAll(async () => {
|
||||
await nestApp.close();
|
||||
@@ -137,7 +137,7 @@ describe('Create trip', () => {
|
||||
|
||||
// Restrict trip creation to admins only
|
||||
testDb.prepare("INSERT INTO app_settings (key, value) VALUES ('perm_trip_create', 'admin')").run();
|
||||
invalidatePermissionsCache();
|
||||
await invalidatePermissionsCache();
|
||||
|
||||
const res = await request(app)
|
||||
.post('/api/trips')
|
||||
@@ -152,7 +152,7 @@ describe('Create trip', () => {
|
||||
const { user: admin } = createAdmin(testDb);
|
||||
|
||||
testDb.prepare("INSERT INTO app_settings (key, value) VALUES ('perm_trip_create', 'admin')").run();
|
||||
invalidatePermissionsCache();
|
||||
await invalidatePermissionsCache();
|
||||
|
||||
const res = await request(app)
|
||||
.post('/api/trips')
|
||||
@@ -375,7 +375,7 @@ describe('Update trip', () => {
|
||||
|
||||
// Restrict archiving to trip_owner only (this is actually the default, but set explicitly)
|
||||
testDb.prepare("INSERT OR REPLACE INTO app_settings (key, value) VALUES ('perm_trip_archive', 'trip_owner')").run();
|
||||
invalidatePermissionsCache();
|
||||
await invalidatePermissionsCache();
|
||||
|
||||
const res = await request(app)
|
||||
.put(`/api/trips/${trip.id}`)
|
||||
@@ -394,7 +394,7 @@ describe('Update trip', () => {
|
||||
|
||||
// Default trip_edit is trip_owner — members should be blocked
|
||||
testDb.prepare("INSERT OR REPLACE INTO app_settings (key, value) VALUES ('perm_trip_edit', 'trip_owner')").run();
|
||||
invalidatePermissionsCache();
|
||||
await invalidatePermissionsCache();
|
||||
|
||||
const res = await request(app)
|
||||
.put(`/api/trips/${trip.id}`)
|
||||
@@ -896,7 +896,7 @@ describe('Trip members', () => {
|
||||
|
||||
// Restrict member management to trip_owner (default)
|
||||
testDb.prepare("INSERT OR REPLACE INTO app_settings (key, value) VALUES ('perm_member_manage', 'trip_owner')").run();
|
||||
invalidatePermissionsCache();
|
||||
await invalidatePermissionsCache();
|
||||
|
||||
const res = await request(app)
|
||||
.post(`/api/trips/${trip.id}/members`)
|
||||
|
||||
@@ -53,9 +53,9 @@ beforeAll(async () => {
|
||||
app = nestApp.getHttpAdapter().getInstance();
|
||||
});
|
||||
|
||||
beforeEach(() => {
|
||||
beforeEach(async () => {
|
||||
resetTestDb(testDb);
|
||||
resetRateLimits(nestApp);
|
||||
await resetRateLimits(nestApp);
|
||||
});
|
||||
|
||||
afterAll(async () => {
|
||||
|
||||
Reference in New Issue
Block a user