Put the feedback and tickets admin areas behind the shared identity
feedback.auth.ts and tickets.auth.ts each become one binding to requireAppAccess. Everything downstream was already written against requireAdminAuth and res.locals.admin, and both still mean what they meant, so no router or service changed. What changed is the policy: an activated @nachklang.art account is no longer sufficient, an explicit per-app permission is. Three things followed from that and are not obvious from the diff: - APP_ORIGINS gets a production default. It feeds better-auth's trustedOrigins, and this is the first time the tickets and feedback origins matter there - before, the only browser origin that ever reached /admin/auth was the admin app itself. An origin missing from that list fails in a way that is easy to misread: sign-in works, the app works, and only sign-out returns an origin error. - Nothing reads X-Session-* any more; these two files were the last readers, and the calendar module passes its session in query parameters. The headers stay in the CORS allowedHeaders only so a browser still running a pre-cutover bundle gets a clean 401 rather than a preflight failure, and can come out once both frontends are deployed. - 40 admin operations documented a required X-Session-Id/X-Session-Key in swagger. They now declare the AdminSessionCookie scheme the admin module already defined, and each documents a 403 next to its 401. The integration assertions flip as their own comment predicted: one admin cookie opens both /feedback/admin/me and /tickets/admin/me, a user holding only feedback gets 200 and 403 respectively, and a legacy header session gets 401. The unit test that covered the old header authenticator is replaced by one asserting each module is bound to its own app and that neither consults the calendar users service. 163 unit tests and 43 integration tests green. Co-Authored-By: Claude Opus 5 <noreply@anthropic.com>
This commit is contained in:
@@ -0,0 +1,113 @@
|
||||
import {vi, describe, it, expect, beforeEach, type Mock} from 'vitest';
|
||||
import express, {Request, Response} from 'express';
|
||||
|
||||
/**
|
||||
* Shared body for the two cutover tests (2026-09-06). feedback.auth.ts and
|
||||
* tickets.auth.ts used to carry their own header-session authenticator against
|
||||
* the calendar users table; both are now one binding to the shared admin gate.
|
||||
*
|
||||
* What is worth asserting is not how that gate works - admin.middleware.test.ts
|
||||
* owns that - but that each module is bound to *its own* app, and that neither
|
||||
* consults the calendar users service any more. The mocks live in the calling
|
||||
* file because vi.mock is per-module-graph; only the assertions are shared.
|
||||
*/
|
||||
|
||||
export interface BindingMocks {
|
||||
/** auth.api.getSession from the mocked admin.auth.js */
|
||||
getSession: Mock;
|
||||
/** loadAccess from the mocked users.admin.service.js */
|
||||
loadAccess: Mock;
|
||||
/** checkSession from the mocked calendar users.service.js */
|
||||
checkSession: Mock;
|
||||
}
|
||||
|
||||
const makeReq = (): Request => ({headers: {cookie: 'nachklang.session_token=abc'}} as unknown as Request);
|
||||
|
||||
const makeRes = (): Response => {
|
||||
const res: any = {};
|
||||
res.status = vi.fn().mockReturnValue(res);
|
||||
res.send = vi.fn().mockReturnValue(res);
|
||||
res.locals = {};
|
||||
return res as Response;
|
||||
};
|
||||
|
||||
const userWith = (...apps: string[]) => ({
|
||||
id: 'u1',
|
||||
email: 'a@nachklang.art',
|
||||
displayName: 'Anna Admin',
|
||||
disabled: false,
|
||||
permissions: apps.map(app => ({app, role: 'access'})),
|
||||
apps
|
||||
});
|
||||
|
||||
export const describeAdminBinding = (
|
||||
app: string,
|
||||
otherApp: string,
|
||||
middleware: express.RequestHandler,
|
||||
mocks: () => BindingMocks
|
||||
): void => {
|
||||
describe(`${app} requireAdminAuth`, () => {
|
||||
let m: BindingMocks;
|
||||
|
||||
const run = async () => {
|
||||
const res = makeRes();
|
||||
const next = vi.fn();
|
||||
await middleware(makeReq(), res, next);
|
||||
return {res, next};
|
||||
};
|
||||
|
||||
beforeEach(() => {
|
||||
m = mocks();
|
||||
m.getSession.mockReset();
|
||||
m.loadAccess.mockReset();
|
||||
m.checkSession.mockReset();
|
||||
});
|
||||
|
||||
it('responds 401 and does not call next() without a session', async () => {
|
||||
m.getSession.mockResolvedValue(null);
|
||||
|
||||
const {res, next} = await run();
|
||||
|
||||
expect(res.status).toHaveBeenCalledWith(401);
|
||||
expect(next).not.toHaveBeenCalled();
|
||||
});
|
||||
|
||||
it(`responds 403 for a signed-in user who only has ${otherApp}`, async () => {
|
||||
m.getSession.mockResolvedValue({user: {id: 'u1'}});
|
||||
m.loadAccess.mockResolvedValue(userWith(otherApp));
|
||||
|
||||
const {res, next} = await run();
|
||||
|
||||
expect(res.status).toHaveBeenCalledWith(403);
|
||||
expect(next).not.toHaveBeenCalled();
|
||||
});
|
||||
|
||||
it('responds 403 for a disabled user who still holds the permission', async () => {
|
||||
m.getSession.mockResolvedValue({user: {id: 'u1'}});
|
||||
m.loadAccess.mockResolvedValue({...userWith(app), disabled: true});
|
||||
|
||||
const {res, next} = await run();
|
||||
|
||||
expect(res.status).toHaveBeenCalledWith(403);
|
||||
expect(next).not.toHaveBeenCalled();
|
||||
});
|
||||
|
||||
it('sets res.locals.admin and calls next() with the permission', async () => {
|
||||
m.getSession.mockResolvedValue({user: {id: 'u1'}});
|
||||
m.loadAccess.mockResolvedValue(userWith(app));
|
||||
|
||||
const {res, next} = await run();
|
||||
|
||||
expect(next).toHaveBeenCalled();
|
||||
expect(res.locals.admin).toMatchObject({id: 'u1', email: 'a@nachklang.art', displayName: 'Anna Admin'});
|
||||
});
|
||||
|
||||
it('never falls back to a calendar header session', async () => {
|
||||
m.getSession.mockResolvedValue(null);
|
||||
|
||||
await run();
|
||||
|
||||
expect(m.checkSession).not.toHaveBeenCalled();
|
||||
});
|
||||
});
|
||||
};
|
||||
Reference in New Issue
Block a user