diff --git a/tests/integration/auth.test.ts b/tests/integration/auth.test.ts index 647644c..8cc1523 100644 --- a/tests/integration/auth.test.ts +++ b/tests/integration/auth.test.ts @@ -1,4 +1,3 @@ -// @ts-nocheck import { describe, expect, it, spyOn } from "bun:test"; import { AUTH_PREFIX } from "../../src/constants"; import { redis } from "../../src/core/RedisClient"; @@ -13,21 +12,25 @@ describe("Auth Integration", () => { scope: "public", }); - it("should redirect to provider login", async () => { - redis.get.mockImplementation((key) => { + const tenantId = "test-tenant"; + + it("should redirect to provider login with tenantId in state", async () => { + (redis.get as any).mockImplementation((key: string) => { if (key.includes("config:trakt")) return Promise.resolve(mockProviderConfig); return Promise.resolve(null); }); - const res = await app.request(`${AUTH_PREFIX}/trakt/login`); + const res = await app.request(`${AUTH_PREFIX}/trakt/login?tenantId=${tenantId}`); expect(res.status).toBe(302); - expect(res.headers.get("Location")).toContain("trakt.tv/oauth/authorize"); - expect(res.headers.get("Location")).toContain("client_id=trakt-client-id"); + const location = res.headers.get("Location") || ""; + expect(location).toContain("trakt.tv/oauth/authorize"); + expect(location).toContain("client_id=trakt-client-id"); + expect(location).toContain(`state=${tenantId}:trakt`); }); - it("should handle callback and exchange code", async () => { - redis.get.mockImplementation((key) => { + it("should handle callback and exchange code using tenantId from state", async () => { + (redis.get as any).mockImplementation((key: string) => { if (key.includes("config:trakt")) return Promise.resolve(mockProviderConfig); return Promise.resolve(null); }); @@ -41,19 +44,26 @@ describe("Auth Integration", () => { refresh_token: "exchange-refresh-token", expires_in: 3600, }), - }), + } as any), ); - const res = await app.request(`${AUTH_PREFIX}/callback?state=trakt&code=temporary-auth-code`); + const res = await app.request( + `${AUTH_PREFIX}/callback?state=${tenantId}:trakt&code=temporary-auth-code`, + ); expect(res.status).toBe(302); - expect(res.headers.get("Location")).toBe("/app/success?provider=trakt"); - expect(redis.set).toHaveBeenCalled(); + expect(res.headers.get("Location")).toBe(`/app/success?provider=trakt&tenantId=${tenantId}`); + expect(redis.set).toHaveBeenCalledWith( + `tenant:${tenantId}:provider:trakt:access_token`, + "exchange-access-token", + "EX", + 3600, + ); expect(fetchSpy).toHaveBeenCalled(); }); it("should return 404 if provider not configured during login", async () => { - const res = await app.request(`${AUTH_PREFIX}/unknown-provider/login`); + const res = await app.request(`${AUTH_PREFIX}/unknown-provider/login?tenantId=${tenantId}`); expect(res.status).toBe(404); });