|
| 1 | +// Copyright (c) 2026 ObjectStack. Licensed under the Apache-2.0 license. |
| 2 | +// |
| 3 | +// The print door's impersonation refusal, judged against the REAL auth |
| 4 | +// service on a real showcase boot. |
| 5 | +// |
| 6 | +// `POST /api/v1/print/records` (`@objectstack/service-print`'s door, which the |
| 7 | +// showcase composes) refuses a print asked from an impersonation session: |
| 8 | +// a printed PDF is rendered and archived as the session's user, and in an |
| 9 | +// impersonation session that user is not the one asking. The door recognises |
| 10 | +// such a session by the mark the auth service writes on it — better-auth's |
| 11 | +// admin plugin sets `impersonatedBy` on the session it mints, which |
| 12 | +// `@objectstack/plugin-auth` stores as `sys_session.impersonated_by` — read |
| 13 | +// off the very session read that admitted the caller. |
| 14 | +// |
| 15 | +// The door's unit suite (`print-routes.test.ts`) hands the door a session that |
| 16 | +// carries the mark, so it cannot notice the mark going away. This file is the |
| 17 | +// supplier side: the session comes from the auth service as composed, so a |
| 18 | +// better-auth or plugin-auth change that drops or renames the field turns the |
| 19 | +// first pin red here instead of turning the refusal off in silence. |
| 20 | +// |
| 21 | +// The two pins, on the same admin: |
| 22 | +// |
| 23 | +// - the admin's own session is NOT refused for impersonation: the print |
| 24 | +// reaches the service, which refuses it `SERVICE_UNAVAILABLE` / `503` |
| 25 | +// because this boot configures no print engine; |
| 26 | +// - the impersonation session the admin then starts is refused at the door: |
| 27 | +// `PERMISSION_DENIED` / `403`, saying the session is an impersonation. |
| 28 | +// |
| 29 | +// `/admin/` routes answer only with better-auth's admin plugin on, and |
| 30 | +// `bootStack` exposes no auth-plugin override: `OS_SCIM_ENABLED` is the env |
| 31 | +// knob that reaches it (SCIM forces the admin plugin on, ADR-0134), read when |
| 32 | +// the auth manager is constructed, so it is set before the boot — the same |
| 33 | +// switch `admin-platform-admin-standing.dogfood.test.ts` uses. |
| 34 | +// |
| 35 | +// Not eligible for the shared showcase project: it toggles `process.env`. |
| 36 | + |
| 37 | +import { afterAll, beforeAll, describe, expect, it } from 'vitest'; |
| 38 | +import type { VerifyStack } from '@objectstack/verify'; |
| 39 | +import { bootShowcase } from './showcase-boot.js'; |
| 40 | + |
| 41 | +const SYS = { isSystem: true } as const; |
| 42 | +const PRINT_ENV = ['OS_PRINT_CHROMIUM_PATH', 'OS_PRINT_BROWSER_ENDPOINT', 'OS_PRINT_CONSOLE_URL']; |
| 43 | +const TARGET_EMAIL = 'print-impersonation.target@dogfood.test'; |
| 44 | + |
| 45 | +const rowsOf = (r: unknown): Array<Record<string, unknown>> => |
| 46 | + (Array.isArray(r) ? r : ((r as { value?: unknown })?.value ?? [])) as Array<Record<string, unknown>>; |
| 47 | + |
| 48 | +interface DoorAnswer { |
| 49 | + status: number; |
| 50 | + code: unknown; |
| 51 | + message: unknown; |
| 52 | +} |
| 53 | + |
| 54 | +describe('the print door refuses an impersonation session, on the real auth service', () => { |
| 55 | + let stack: VerifyStack; |
| 56 | + let body: Record<string, string>; |
| 57 | + let targetUserId: string; |
| 58 | + const saved: Record<string, string | undefined> = {}; |
| 59 | + |
| 60 | + async function print(token: string): Promise<DoorAnswer> { |
| 61 | + const res = await stack.apiAs(token, 'POST', '/print/records', body); |
| 62 | + const json = (await res.json().catch(() => ({}))) as { error?: { code?: unknown; message?: unknown } }; |
| 63 | + return { status: res.status, code: json.error?.code, message: json.error?.message }; |
| 64 | + } |
| 65 | + |
| 66 | + beforeAll(async () => { |
| 67 | + saved.OS_SCIM_ENABLED = process.env.OS_SCIM_ENABLED; |
| 68 | + process.env.OS_SCIM_ENABLED = 'true'; |
| 69 | + for (const k of PRINT_ENV) { |
| 70 | + saved[k] = process.env[k]; |
| 71 | + delete process.env[k]; |
| 72 | + } |
| 73 | + stack = await bootShowcase(); |
| 74 | + const ql = await (stack.kernel as unknown as { |
| 75 | + getServiceAsync<T>(n: string): Promise<T>; |
| 76 | + }).getServiceAsync<{ find(o: string, q: unknown): Promise<unknown> }>('objectql'); |
| 77 | + await stack.signUp(TARGET_EMAIL, 'Target-Pass-123'); |
| 78 | + const [target] = rowsOf(await ql.find('sys_user', { where: { email: TARGET_EMAIL }, limit: 1, context: SYS })); |
| 79 | + targetUserId = String(target?.id); |
| 80 | + const [invoice] = rowsOf(await ql.find('showcase_invoice', { limit: 1, context: SYS })); |
| 81 | + body = { |
| 82 | + app: 'com.example.showcase', |
| 83 | + object: 'showcase_invoice', |
| 84 | + recordId: String(invoice?.id), |
| 85 | + page: 'showcase_invoice_document', |
| 86 | + }; |
| 87 | + }, 300_000); |
| 88 | + |
| 89 | + afterAll(async () => { |
| 90 | + await stack?.stop?.(); |
| 91 | + for (const [k, v] of Object.entries(saved)) { |
| 92 | + if (v === undefined) delete process.env[k]; |
| 93 | + else process.env[k] = v; |
| 94 | + } |
| 95 | + }); |
| 96 | + |
| 97 | + it('the admin\'s own session reaches the service, and an impersonation session the admin starts is refused at the door', async () => { |
| 98 | + expect(targetUserId).not.toBe('undefined'); |
| 99 | + const adminToken = await stack.signIn(); |
| 100 | + |
| 101 | + // The admin, as themselves: not an impersonation, so the door hands the |
| 102 | + // print to the service, which has no engine on this boot. |
| 103 | + const own = await print(adminToken); |
| 104 | + expect([own.code, own.status], JSON.stringify(own)).toEqual(['SERVICE_UNAVAILABLE', 503]); |
| 105 | + |
| 106 | + const started = await stack.apiAs(adminToken, 'POST', '/auth/admin/impersonate-user', { userId: targetUserId }); |
| 107 | + expect(started.status, 'the admin plugin did not start an impersonation — no reading below means anything').toBe(200); |
| 108 | + const session = ((await started.json()) as { session?: { token?: unknown } }).session; |
| 109 | + expect(typeof session?.token, 'the impersonation answer carried no session token').toBe('string'); |
| 110 | + |
| 111 | + // The impersonation session: refused at the door, and the refusal says why, |
| 112 | + // so a 403 from anywhere else cannot stand in for this one. |
| 113 | + const impersonated = await print(String(session!.token)); |
| 114 | + expect([impersonated.code, impersonated.status], JSON.stringify(impersonated)).toEqual(['PERMISSION_DENIED', 403]); |
| 115 | + expect(String(impersonated.message)).toMatch(/impersonation/); |
| 116 | + }, 120_000); |
| 117 | +}); |
0 commit comments