|
| 1 | +// Copyright (c) 2025 ObjectStack. Licensed under the Apache-2.0 license. |
| 2 | +// |
| 3 | +// End-to-end regression for #3361 on the STANDALONE Hono CRUD surface (the |
| 4 | +// minimal server used when `@objectstack/rest` is not mounted). It drives a real |
| 5 | +// request through the perf middleware AND the real `/api/v1/data/:object` |
| 6 | +// handler — whose `resolveCtx` resolves the principal — instead of a handler |
| 7 | +// that calls `allowPerfDisclosure()` by hand "as the dispatcher would" (the gap |
| 8 | +// the existing unit tests left invisible). An admin sending `X-OS-Debug-Timing` |
| 9 | +// must get a `Server-Timing` header; a member and an anonymous caller must not. |
| 10 | + |
| 11 | +import { describe, it, expect } from 'vitest'; |
| 12 | +import { HonoServerPlugin } from './hono-plugin'; |
| 13 | +import type { PluginContext } from '@objectstack/core'; |
| 14 | + |
| 15 | +/** |
| 16 | + * Fake data engine: an UNSCOPED `admin_full_access` grant for `admin1` (the |
| 17 | + * seeded platform admin) vs. a `member_default` grant for `member1`, plus the |
| 18 | + * `widget` rows the data route returns. Every other read resolves empty. |
| 19 | + */ |
| 20 | +const makeQl = () => ({ |
| 21 | + find: async (object: string, opts: any) => { |
| 22 | + const where = opts?.where ?? {}; |
| 23 | + if (object === 'sys_user_permission_set') { |
| 24 | + if (where.user_id === 'admin1') return [{ permission_set_id: 'ps_admin', organization_id: null }]; |
| 25 | + if (where.user_id === 'member1') return [{ permission_set_id: 'ps_member', organization_id: null }]; |
| 26 | + return []; |
| 27 | + } |
| 28 | + if (object === 'sys_permission_set') { |
| 29 | + const ids: string[] = where.id?.$in ?? []; |
| 30 | + return [ |
| 31 | + ids.includes('ps_admin') ? { id: 'ps_admin', name: 'admin_full_access' } : null, |
| 32 | + ids.includes('ps_member') ? { id: 'ps_member', name: 'member_default' } : null, |
| 33 | + ].filter(Boolean); |
| 34 | + } |
| 35 | + if (object === 'widget') return [{ id: '1', name: 'w' }]; |
| 36 | + // sys_member, sys_user — nothing to contribute. |
| 37 | + return []; |
| 38 | + }, |
| 39 | +}); |
| 40 | + |
| 41 | +/** Fake auth service: session keyed off the request's `cookie` header. */ |
| 42 | +const makeAuth = () => ({ |
| 43 | + api: { |
| 44 | + getSession: async ({ headers }: { headers: any }) => { |
| 45 | + const cookie = headers?.get?.('cookie'); |
| 46 | + if (cookie === 'admin') return { user: { id: 'admin1' } }; |
| 47 | + if (cookie === 'member') return { user: { id: 'member1' } }; |
| 48 | + return undefined; |
| 49 | + }, |
| 50 | + }, |
| 51 | +}); |
| 52 | + |
| 53 | +function fakeCtx(services: Record<string, unknown>): PluginContext { |
| 54 | + const map = new Map<string, unknown>(Object.entries(services)); |
| 55 | + return { |
| 56 | + logger: { debug() {}, info() {}, warn() {}, error() {} }, |
| 57 | + registerService: (name: string, svc: unknown) => map.set(name, svc), |
| 58 | + getService: (name: string) => map.get(name), |
| 59 | + hook: () => {}, |
| 60 | + getKernel: () => ({}), |
| 61 | + } as unknown as PluginContext; |
| 62 | +} |
| 63 | + |
| 64 | +async function setup() { |
| 65 | + // serverTiming left at its default (undefined): global mode OFF, the |
| 66 | + // admin-gated per-request path AVAILABLE — the exact `os serve` posture. |
| 67 | + const plugin = new HonoServerPlugin({ cors: false }); |
| 68 | + const ctx = fakeCtx({ objectql: makeQl(), auth: makeAuth() }); |
| 69 | + await (plugin as any).init(ctx); |
| 70 | + // Register the real standard CRUD/data endpoints (normally wired on |
| 71 | + // kernel:ready) so `/api/v1/data/:object` runs its real `resolveCtx`. |
| 72 | + (plugin as any).registerDiscoveryAndCrudEndpoints(ctx); |
| 73 | + const app = (plugin as any).server.getRawApp(); |
| 74 | + return { app }; |
| 75 | +} |
| 76 | + |
| 77 | +describe('Hono standalone data route — admin-gated Server-Timing (#3361 e2e)', () => { |
| 78 | + it('emits Server-Timing for a platform admin sending X-OS-Debug-Timing', async () => { |
| 79 | + const { app } = await setup(); |
| 80 | + const res = await app.request('/api/v1/data/widget', { |
| 81 | + headers: { cookie: 'admin', 'X-OS-Debug-Timing': '1' }, |
| 82 | + }); |
| 83 | + expect(res.status).toBe(200); |
| 84 | + const header = res.headers.get('Server-Timing'); |
| 85 | + expect(header).toBeTruthy(); |
| 86 | + expect(header).toMatch(/(^|, )total;dur=[\d.]+/); |
| 87 | + }); |
| 88 | + |
| 89 | + it('withholds Server-Timing from an ordinary member (same debug header)', async () => { |
| 90 | + const { app } = await setup(); |
| 91 | + const res = await app.request('/api/v1/data/widget', { |
| 92 | + headers: { cookie: 'member', 'X-OS-Debug-Timing': '1' }, |
| 93 | + }); |
| 94 | + expect(res.status).toBe(200); |
| 95 | + expect(res.headers.get('Server-Timing')).toBeNull(); |
| 96 | + }); |
| 97 | + |
| 98 | + it('withholds Server-Timing from an anonymous caller (401, no header)', async () => { |
| 99 | + const { app } = await setup(); |
| 100 | + const res = await app.request('/api/v1/data/widget', { |
| 101 | + headers: { 'X-OS-Debug-Timing': '1' }, |
| 102 | + }); |
| 103 | + // requireAuth defaults on → anonymous is denied, and nothing opened the gate. |
| 104 | + expect(res.status).toBe(401); |
| 105 | + expect(res.headers.get('Server-Timing')).toBeNull(); |
| 106 | + }); |
| 107 | + |
| 108 | + it('does NOT emit for an admin when no debug header is sent (opt-in only)', async () => { |
| 109 | + const { app } = await setup(); |
| 110 | + const res = await app.request('/api/v1/data/widget', { headers: { cookie: 'admin' } }); |
| 111 | + expect(res.status).toBe(200); |
| 112 | + expect(res.headers.get('Server-Timing')).toBeNull(); |
| 113 | + }); |
| 114 | +}); |
0 commit comments