|
| 1 | +// Copyright (c) 2026 ObjectStack. Licensed under the Apache-2.0 license. |
| 2 | + |
| 3 | +/** |
| 4 | + * #4117 — the `/auth/*` mount must yield paths it does not own. |
| 5 | + * |
| 6 | + * It claims a whole namespace and used to be TERMINAL: it answered 404 for a path |
| 7 | + * its auth service does not implement. Found by #4116's scan, which no manual grep |
| 8 | + * had managed. Its `/storage/*` twin was ratcheted alongside it and is now gone |
| 9 | + * entirely — #4087/#4112 deleted that bridge after reaching the same conclusion |
| 10 | + * from the other direction ("the wildcard was wider than the two routes it |
| 11 | + * served"). |
| 12 | + * |
| 13 | + * ## What yielding buys HERE, precisely |
| 14 | + * |
| 15 | + * Not what it bought in #4092. There, yielding made another plugin's route |
| 16 | + * reachable. In this adapter it cannot: the `${prefix}/*` dispatcher catch-all |
| 17 | + * is registered right after these two and is DELIBERATELY terminal (ADR-0076 |
| 18 | + * OQ#9, #3576/#3608 — the gate stages live inside `dispatch()`, so splitting it |
| 19 | + * into per-prefix Hono mounts would bypass them). So a route registered later |
| 20 | + * under `/api/auth/*` is swallowed by the catch-all whether or not these two |
| 21 | + * yield, and Hono-route mounting is not this adapter's extension path at all — |
| 22 | + * registering a domain handler is. |
| 23 | + * |
| 24 | + * What yielding buys is that an unowned `/auth/*` or `/storage/*` path now |
| 25 | + * reaches that gated `dispatch()` instead of dead-ending in a 404 built two |
| 26 | + * mounts earlier. A domain handler registered for such a path becomes |
| 27 | + * reachable, which is exactly the adapter's intended mechanism. These tests pin |
| 28 | + * that, and pin that the owners still win everything they own. |
| 29 | + */ |
| 30 | + |
| 31 | +import { describe, it, expect, vi, beforeEach } from 'vitest'; |
| 32 | +import type { Hono } from 'hono'; |
| 33 | + |
| 34 | +const mockDispatcher = { |
| 35 | + getDiscoveryInfo: vi.fn().mockReturnValue({ version: '1.0', endpoints: [] }), |
| 36 | + handleAuth: vi.fn(), |
| 37 | + handleGraphQL: vi.fn(), |
| 38 | + dispatch: vi.fn(), |
| 39 | +}; |
| 40 | + |
| 41 | +vi.mock('@objectstack/runtime', () => ({ |
| 42 | + HttpDispatcher: function HttpDispatcher() { return mockDispatcher; }, |
| 43 | +})); |
| 44 | + |
| 45 | +import { createHonoApp } from './index'; |
| 46 | + |
| 47 | +/** No domain claimed the path — the dispatcher's own ownership signal. */ |
| 48 | +const UNHANDLED = { handled: false }; |
| 49 | +const HANDLED = (body: unknown, status = 200) => ({ handled: true, response: { body, status } }); |
| 50 | + |
| 51 | +/** A kernel whose `auth` service is (or is not) present. */ |
| 52 | +const kernelWith = (authService?: unknown) => ({ |
| 53 | + name: 'test-kernel', |
| 54 | + getService: (n: string) => (n === 'auth' && authService ? authService : undefined), |
| 55 | +}) as any; |
| 56 | + |
| 57 | +const authServiceReturning = (status: number, body: unknown = { from: 'better-auth' }) => ({ |
| 58 | + handleRequest: vi.fn(async () => new Response(JSON.stringify(body), { |
| 59 | + status, |
| 60 | + headers: { 'Content-Type': 'application/json' }, |
| 61 | + })), |
| 62 | +}); |
| 63 | + |
| 64 | +describe('auth mount yields paths better-auth does not own (#4117)', () => { |
| 65 | + beforeEach(() => { |
| 66 | + vi.clearAllMocks(); |
| 67 | + mockDispatcher.dispatch.mockResolvedValue(UNHANDLED); |
| 68 | + mockDispatcher.handleAuth.mockResolvedValue(UNHANDLED); |
| 69 | + }); |
| 70 | + |
| 71 | + it('reaches the gated dispatch when better-auth 404s', async () => { |
| 72 | + // `/auth/me/permissions` is the canonical unowned path: nothing in |
| 73 | + // better-auth serves it. Before #4117 this 404'd here; now the request gets |
| 74 | + // a real dispatch attempt, so a domain handler for it is reachable. |
| 75 | + mockDispatcher.dispatch.mockResolvedValue(HANDLED({ authenticated: true, from: 'dispatch' })); |
| 76 | + const app: Hono = createHonoApp({ kernel: kernelWith(authServiceReturning(404)) }); |
| 77 | + |
| 78 | + const res = await app.request('/api/auth/me/permissions'); |
| 79 | + |
| 80 | + expect(mockDispatcher.dispatch).toHaveBeenCalled(); |
| 81 | + expect(res.status).toBe(200); |
| 82 | + expect(await res.json()).toEqual({ authenticated: true, from: 'dispatch' }); |
| 83 | + }); |
| 84 | + |
| 85 | + it('keeps better-auth winning the paths it DOES own', async () => { |
| 86 | + const app: Hono = createHonoApp({ kernel: kernelWith(authServiceReturning(200, { user: null })) }); |
| 87 | + |
| 88 | + const res = await app.request('/api/auth/get-session'); |
| 89 | + |
| 90 | + expect(res.status).toBe(200); |
| 91 | + expect(await res.json()).toEqual({ user: null }); |
| 92 | + expect(mockDispatcher.dispatch).not.toHaveBeenCalled(); |
| 93 | + }); |
| 94 | + |
| 95 | + it('does NOT yield on 401 — a real answer, not a disclaimer of ownership', async () => { |
| 96 | + const app: Hono = createHonoApp({ kernel: kernelWith(authServiceReturning(401, { error: 'nope' })) }); |
| 97 | + |
| 98 | + const res = await app.request('/api/auth/protected'); |
| 99 | + |
| 100 | + expect(res.status).toBe(401); |
| 101 | + expect(await res.json()).toEqual({ error: 'nope' }); |
| 102 | + expect(mockDispatcher.dispatch).not.toHaveBeenCalled(); |
| 103 | + }); |
| 104 | + |
| 105 | + it('yields on the dispatcher path too, keyed on `handled: false`', async () => { |
| 106 | + // No auth service at all, so the mount falls back to `dispatcher.handleAuth` |
| 107 | + // — whose explicit `handled` flag beats inferring ownership from a status. |
| 108 | + mockDispatcher.dispatch.mockResolvedValue(HANDLED({ currency: 'USD' })); |
| 109 | + const app: Hono = createHonoApp({ kernel: kernelWith() }); |
| 110 | + |
| 111 | + const res = await app.request('/api/auth/me/localization'); |
| 112 | + |
| 113 | + expect(mockDispatcher.handleAuth).toHaveBeenCalled(); |
| 114 | + expect(mockDispatcher.dispatch).toHaveBeenCalled(); |
| 115 | + expect(res.status).toBe(200); |
| 116 | + expect(await res.json()).toEqual({ currency: 'USD' }); |
| 117 | + }); |
| 118 | + |
| 119 | + it('still lets handleAuth answer what it DOES handle', async () => { |
| 120 | + mockDispatcher.handleAuth.mockResolvedValue(HANDLED({ ok: true })); |
| 121 | + const app: Hono = createHonoApp({ kernel: kernelWith() }); |
| 122 | + |
| 123 | + const res = await app.request('/api/auth/login', { method: 'POST' }); |
| 124 | + |
| 125 | + expect(res.status).toBe(200); |
| 126 | + expect(await res.json()).toEqual({ ok: true }); |
| 127 | + expect(mockDispatcher.dispatch).not.toHaveBeenCalled(); |
| 128 | + }); |
| 129 | +}); |
| 130 | + |
| 131 | +describe('the enveloped 404 survives when nothing anywhere claims the path', () => { |
| 132 | + beforeEach(() => vi.clearAllMocks()); |
| 133 | + |
| 134 | + it('still ends in the enveloped 404 when nothing anywhere claims it', async () => { |
| 135 | + mockDispatcher.handleAuth.mockResolvedValue(UNHANDLED); |
| 136 | + mockDispatcher.dispatch.mockResolvedValue(UNHANDLED); |
| 137 | + const app: Hono = createHonoApp({ kernel: kernelWith() }); |
| 138 | + |
| 139 | + const res = await app.request('/api/auth/nope'); |
| 140 | + |
| 141 | + expect(res.status).toBe(404); |
| 142 | + // Not Hono's bare "404 Not Found" text — the platform envelope is preserved. |
| 143 | + expect(await res.json()).toEqual({ success: false, error: { message: 'Not Found', code: 404 } }); |
| 144 | + }); |
| 145 | +}); |
0 commit comments