|
| 1 | +import { Controller, Get, SetMetadata } from "@nestjs/common"; |
| 2 | +import request from "supertest"; |
| 3 | +import { afterAll, beforeAll, describe, expect, it, vi } from "vitest"; |
| 4 | +import type { INestApplication } from "@nestjs/common"; |
| 5 | + |
| 6 | +import { OptionalAuth, Roles } from "../../src/index.ts"; |
| 7 | +import { createTestAuth } from "../shared/test-auth.ts"; |
| 8 | +import { createTestApp } from "../shared/test-app.ts"; |
| 9 | +import { testHttpAdapter } from "../shared/http-adapter.ts"; |
| 10 | + |
| 11 | +const FOREIGN_PUBLIC = "test:foreign-public"; |
| 12 | +const SECOND_FOREIGN_PUBLIC = "test:second-foreign-public"; |
| 13 | + |
| 14 | +@Controller("interop") |
| 15 | +@SetMetadata(FOREIGN_PUBLIC, true) |
| 16 | +class InteropController { |
| 17 | + @Get("open") |
| 18 | + open(): { ok: true } { |
| 19 | + return { ok: true }; |
| 20 | + } |
| 21 | + |
| 22 | + @Get("guarded") |
| 23 | + @Roles("admin") |
| 24 | + guarded(): { ok: true } { |
| 25 | + return { ok: true }; |
| 26 | + } |
| 27 | + |
| 28 | + @Get("explicit-open") |
| 29 | + @Roles("admin") |
| 30 | + @SetMetadata(FOREIGN_PUBLIC, true) |
| 31 | + explicitOpen(): { ok: true } { |
| 32 | + return { ok: true }; |
| 33 | + } |
| 34 | + |
| 35 | + @Get("explicit-open-second-key") |
| 36 | + @Roles("admin") |
| 37 | + @SetMetadata(SECOND_FOREIGN_PUBLIC, true) |
| 38 | + explicitOpenSecondKey(): { ok: true } { |
| 39 | + return { ok: true }; |
| 40 | + } |
| 41 | + |
| 42 | + @Get("explicit-open-with-optional") |
| 43 | + @OptionalAuth() |
| 44 | + @SetMetadata(SECOND_FOREIGN_PUBLIC, true) |
| 45 | + explicitOpenWithOptional(): { ok: true } { |
| 46 | + return { ok: true }; |
| 47 | + } |
| 48 | +} |
| 49 | + |
| 50 | +describe(`public metadata interop (${testHttpAdapter})`, () => { |
| 51 | + let app: INestApplication; |
| 52 | + let auth: ReturnType<typeof createTestAuth>; |
| 53 | + |
| 54 | + beforeAll(async () => { |
| 55 | + auth = createTestAuth(); |
| 56 | + app = await createTestApp({ |
| 57 | + forRoot: { |
| 58 | + auth, |
| 59 | + interop: { publicKeys: [FOREIGN_PUBLIC, SECOND_FOREIGN_PUBLIC] }, |
| 60 | + }, |
| 61 | + metadata: { controllers: [InteropController] }, |
| 62 | + }); |
| 63 | + }); |
| 64 | + |
| 65 | + afterAll(async () => { |
| 66 | + await app.close(); |
| 67 | + }); |
| 68 | + |
| 69 | + it("serves a foreign-public route without resolving a session", async () => { |
| 70 | + const getSession = vi.spyOn(auth.api, "getSession"); |
| 71 | + |
| 72 | + await request(app.getHttpServer()).get("/interop/open").expect(200, { ok: true }); |
| 73 | + |
| 74 | + expect(getSession).not.toHaveBeenCalled(); |
| 75 | + getSession.mockRestore(); |
| 76 | + }); |
| 77 | + |
| 78 | + it("lets a handler-level auth requirement override a class-level foreign marker", async () => { |
| 79 | + await request(app.getHttpServer()).get("/interop/guarded").expect(401); |
| 80 | + }); |
| 81 | + |
| 82 | + it("lets a handler-level foreign marker explicitly override that requirement", async () => { |
| 83 | + await request(app.getHttpServer()).get("/interop/explicit-open").expect(200, { ok: true }); |
| 84 | + }); |
| 85 | + |
| 86 | + it("checks every key for a handler-level marker before inherited markers", async () => { |
| 87 | + await request(app.getHttpServer()) |
| 88 | + .get("/interop/explicit-open-second-key") |
| 89 | + .expect(200, { ok: true }); |
| 90 | + }); |
| 91 | + |
| 92 | + it("keeps an explicit foreign-public handler from resolving an optional session", async () => { |
| 93 | + const getSession = vi.spyOn(auth.api, "getSession"); |
| 94 | + |
| 95 | + await request(app.getHttpServer()) |
| 96 | + .get("/interop/explicit-open-with-optional") |
| 97 | + .expect(200, { ok: true }); |
| 98 | + |
| 99 | + expect(getSession).not.toHaveBeenCalled(); |
| 100 | + getSession.mockRestore(); |
| 101 | + }); |
| 102 | +}); |
0 commit comments