|
| 1 | +import { describe, it, expect, vi, beforeEach } from "vitest" |
| 2 | +import { createCallerFactory, auroraRouter } from "../../trpc" |
| 3 | +import { projectRouter } from "./projectRouter" |
| 4 | +import { AuroraPortalContext } from "../../context" |
| 5 | +import { TRPCError } from "@trpc/server" |
| 6 | + |
| 7 | +const mockFetch = vi.fn() |
| 8 | +vi.stubGlobal("fetch", mockFetch) |
| 9 | + |
| 10 | +const makeCtx = (overrides?: Partial<AuroraPortalContext>) => |
| 11 | + ({ |
| 12 | + identityEndpoint: "https://identity.example.com/v3/", |
| 13 | + validateSession: vi.fn().mockReturnValue(true), |
| 14 | + imageMetadataExcludedProperties: [], |
| 15 | + signal: new AbortController().signal, |
| 16 | + openstack: { |
| 17 | + isValid: vi.fn().mockReturnValue(true), |
| 18 | + getToken: vi.fn().mockReturnValue({ authToken: "test-token", tokenData: {} }), |
| 19 | + service: vi.fn(), |
| 20 | + }, |
| 21 | + getUserInfo: vi.fn().mockResolvedValue({ availableDomains: [] }), |
| 22 | + rescopeSession: vi.fn(), |
| 23 | + createSession: vi.fn(), |
| 24 | + terminateSession: vi.fn(), |
| 25 | + ...overrides, |
| 26 | + }) as unknown as AuroraPortalContext |
| 27 | + |
| 28 | +const createCaller = createCallerFactory(auroraRouter({ ...projectRouter })) |
| 29 | + |
| 30 | +const projectsPayload = { |
| 31 | + projects: [ |
| 32 | + { id: "p1", name: "shadowvault", enabled: true, domain_id: "d1" }, |
| 33 | + { id: "p2", name: "ironkeep", enabled: true, domain_id: "d2" }, |
| 34 | + { id: "p3", name: "stormwatch", enabled: true, domain_id: "unknown-domain" }, |
| 35 | + ], |
| 36 | +} |
| 37 | + |
| 38 | +const domainsPayload = { |
| 39 | + domains: [ |
| 40 | + { id: "d1", name: "domain-alpha" }, |
| 41 | + { id: "d2", name: "domain-beta" }, |
| 42 | + ], |
| 43 | +} |
| 44 | + |
| 45 | +describe("projectRouter.getAuthProjects", () => { |
| 46 | + beforeEach(() => { |
| 47 | + vi.clearAllMocks() |
| 48 | + }) |
| 49 | + |
| 50 | + it("attaches domain_name by resolving domain_id", async () => { |
| 51 | + mockFetch |
| 52 | + .mockResolvedValueOnce({ ok: true, json: () => Promise.resolve(projectsPayload) } as Response) |
| 53 | + .mockResolvedValueOnce({ ok: true, json: () => Promise.resolve(domainsPayload) } as Response) |
| 54 | + |
| 55 | + const caller = createCaller(makeCtx()) |
| 56 | + const result = await caller.getAuthProjects() |
| 57 | + |
| 58 | + expect(result?.find((p) => p.id === "p1")?.domain_name).toBe("domain-alpha") |
| 59 | + expect(result?.find((p) => p.id === "p2")?.domain_name).toBe("domain-beta") |
| 60 | + }) |
| 61 | + |
| 62 | + it("leaves domain_name undefined when domain_id has no match", async () => { |
| 63 | + mockFetch |
| 64 | + .mockResolvedValueOnce({ ok: true, json: () => Promise.resolve(projectsPayload) } as Response) |
| 65 | + .mockResolvedValueOnce({ ok: true, json: () => Promise.resolve(domainsPayload) } as Response) |
| 66 | + |
| 67 | + const caller = createCaller(makeCtx()) |
| 68 | + const result = await caller.getAuthProjects() |
| 69 | + |
| 70 | + expect(result?.find((p) => p.id === "p3")?.domain_name).toBeUndefined() |
| 71 | + }) |
| 72 | + |
| 73 | + it("throws UNAUTHORIZED when no openstack session", async () => { |
| 74 | + const caller = createCaller(makeCtx({ openstack: undefined })) |
| 75 | + await expect(caller.getAuthProjects()).rejects.toBeInstanceOf(TRPCError) |
| 76 | + }) |
| 77 | + |
| 78 | + it("still returns projects with empty domain map when domains fetch fails", async () => { |
| 79 | + mockFetch |
| 80 | + .mockResolvedValueOnce({ ok: true, json: () => Promise.resolve(projectsPayload) } as Response) |
| 81 | + .mockRejectedValueOnce(new Error("network error")) |
| 82 | + |
| 83 | + const caller = createCaller(makeCtx()) |
| 84 | + const result = await caller.getAuthProjects() |
| 85 | + |
| 86 | + expect(result).toHaveLength(3) |
| 87 | + expect(result?.every((p) => p.domain_name === undefined)).toBe(true) |
| 88 | + }) |
| 89 | +}) |
0 commit comments