|
| 1 | +import { Status } from "@core/errors/status.codes"; |
| 2 | +import { type GoogleAuthCodeRequest } from "@core/types/auth.types"; |
| 3 | +import { BaseApi } from "@web/common/apis/base/base.api"; |
| 4 | +import { session } from "@web/common/classes/Session"; |
| 5 | +import { GoogleAuthCallbackApi } from "./google-auth-callback.api"; |
| 6 | +import { |
| 7 | + afterEach, |
| 8 | + beforeEach, |
| 9 | + describe, |
| 10 | + expect, |
| 11 | + it, |
| 12 | + mock, |
| 13 | + spyOn, |
| 14 | +} from "bun:test"; |
| 15 | + |
| 16 | +const originalFetch = globalThis.fetch; |
| 17 | + |
| 18 | +const payload: GoogleAuthCodeRequest = { |
| 19 | + clientType: "web", |
| 20 | + redirectURIInfo: { |
| 21 | + redirectURIOnProviderDashboard: |
| 22 | + "http://localhost:9080/auth/google/callback", |
| 23 | + redirectURIQueryParams: { |
| 24 | + code: "auth-code", |
| 25 | + scope: "https://www.googleapis.com/auth/calendar", |
| 26 | + state: "state-1", |
| 27 | + }, |
| 28 | + }, |
| 29 | + thirdPartyId: "google", |
| 30 | +}; |
| 31 | + |
| 32 | +describe("GoogleAuthCallbackApi", () => { |
| 33 | + beforeEach(() => { |
| 34 | + BaseApi.defaults.adapter = undefined; |
| 35 | + }); |
| 36 | + |
| 37 | + afterEach(() => { |
| 38 | + globalThis.fetch = originalFetch; |
| 39 | + BaseApi.defaults.adapter = undefined; |
| 40 | + }); |
| 41 | + |
| 42 | + it("lets the callback flow handle an expired connect session locally", async () => { |
| 43 | + const signOutSpy = spyOn(session, "signOut").mockResolvedValue(undefined); |
| 44 | + globalThis.fetch = mock(async () => |
| 45 | + Promise.resolve( |
| 46 | + new Response(JSON.stringify({ message: "unauthorised" }), { |
| 47 | + status: Status.UNAUTHORIZED, |
| 48 | + }), |
| 49 | + ), |
| 50 | + ) as unknown as typeof fetch; |
| 51 | + |
| 52 | + await expect( |
| 53 | + GoogleAuthCallbackApi.connectGoogle(payload), |
| 54 | + ).rejects.toMatchObject({ |
| 55 | + name: "ApiError", |
| 56 | + response: { |
| 57 | + status: Status.UNAUTHORIZED, |
| 58 | + }, |
| 59 | + }); |
| 60 | + |
| 61 | + expect(signOutSpy).not.toHaveBeenCalled(); |
| 62 | + expect(globalThis.fetch).toHaveBeenCalledWith( |
| 63 | + expect.stringContaining("/auth/google/connect"), |
| 64 | + expect.objectContaining({ |
| 65 | + credentials: "include", |
| 66 | + method: "POST", |
| 67 | + }), |
| 68 | + ); |
| 69 | + |
| 70 | + signOutSpy.mockRestore(); |
| 71 | + }); |
| 72 | + |
| 73 | + it("uses shared session handling for non-recoverable connect session errors", async () => { |
| 74 | + window.history.pushState({}, "", "/day"); |
| 75 | + const signOutSpy = spyOn(session, "signOut").mockResolvedValue(undefined); |
| 76 | + globalThis.fetch = mock(async () => |
| 77 | + Promise.resolve( |
| 78 | + new Response(JSON.stringify({ message: "not found" }), { |
| 79 | + status: Status.NOT_FOUND, |
| 80 | + }), |
| 81 | + ), |
| 82 | + ) as unknown as typeof fetch; |
| 83 | + |
| 84 | + await expect( |
| 85 | + GoogleAuthCallbackApi.connectGoogle(payload), |
| 86 | + ).rejects.toMatchObject({ |
| 87 | + name: "ApiError", |
| 88 | + response: { |
| 89 | + status: Status.NOT_FOUND, |
| 90 | + }, |
| 91 | + }); |
| 92 | + |
| 93 | + expect(signOutSpy).toHaveBeenCalledTimes(1); |
| 94 | + |
| 95 | + signOutSpy.mockRestore(); |
| 96 | + }); |
| 97 | +}); |
0 commit comments