|
| 1 | +import { describe, test, expect, vi, beforeEach } from "vitest"; |
| 2 | +import { setActivePinia, createPinia } from "pinia"; |
| 3 | +import routeLinks from "@/router/routeLinks"; |
| 4 | + |
| 5 | +// Capture the OIDC event callbacks useAuth registers, plus a spy on signinRedirect, so the tests |
| 6 | +// can fire "token expired" / "silent renew error" and assert that recovery re-authenticates. |
| 7 | +const signinRedirect = vi.fn().mockResolvedValue(undefined); |
| 8 | +const captured: { expired?: () => void; renewError?: (error: unknown) => void } = {}; |
| 9 | + |
| 10 | +vi.mock("oidc-client-ts", () => ({ |
| 11 | + UserManager: class { |
| 12 | + getUser = vi.fn().mockResolvedValue(null); |
| 13 | + signinRedirect = signinRedirect; |
| 14 | + signinCallback = vi.fn().mockResolvedValue(null); |
| 15 | + signinSilent = vi.fn().mockResolvedValue(null); |
| 16 | + signoutRedirect = vi.fn().mockResolvedValue(undefined); |
| 17 | + removeUser = vi.fn().mockResolvedValue(undefined); |
| 18 | + events = { |
| 19 | + addUserLoaded: vi.fn(), |
| 20 | + addUserUnloaded: vi.fn(), |
| 21 | + addAccessTokenExpiring: vi.fn(), |
| 22 | + addAccessTokenExpired: vi.fn((cb: () => void) => { |
| 23 | + captured.expired = cb; |
| 24 | + }), |
| 25 | + addSilentRenewError: vi.fn((cb: (error: unknown) => void) => { |
| 26 | + captured.renewError = cb; |
| 27 | + }), |
| 28 | + }; |
| 29 | + }, |
| 30 | + WebStorageStateStore: class {}, |
| 31 | +})); |
| 32 | + |
| 33 | +const config = { authority: "https://idp" } as never; |
| 34 | + |
| 35 | +// Fresh module state per test (useAuth keeps a module-singleton UserManager), then run the initial |
| 36 | +// authenticate so the handlers are registered. getUser returns null, so this initial call performs |
| 37 | +// one signinRedirect and leaves isAuthenticating true (as in the real redirect-away flow). |
| 38 | +async function initAuth() { |
| 39 | + const { useAuth } = await import("@/composables/useAuth"); |
| 40 | + const { useAuthStore } = await import("@/stores/AuthStore"); |
| 41 | + const auth = useAuth(); |
| 42 | + await auth.authenticate(config); |
| 43 | + return useAuthStore(); |
| 44 | +} |
| 45 | + |
| 46 | +beforeEach(() => { |
| 47 | + vi.resetModules(); |
| 48 | + signinRedirect.mockClear(); |
| 49 | + captured.expired = undefined; |
| 50 | + captured.renewError = undefined; |
| 51 | + setActivePinia(createPinia()); |
| 52 | + window.location.hash = ""; |
| 53 | +}); |
| 54 | + |
| 55 | +describe("useAuth recovers a lost session from OIDC events", () => { |
| 56 | + test("re-authenticates and clears the stale token when the access token expires", async () => { |
| 57 | + const store = await initAuth(); |
| 58 | + store.setAuthenticating(false); // initial redirect 'returned' |
| 59 | + signinRedirect.mockClear(); |
| 60 | + |
| 61 | + captured.expired!(); |
| 62 | + |
| 63 | + expect(signinRedirect).toHaveBeenCalledTimes(1); |
| 64 | + expect(store.token).toBeNull(); |
| 65 | + }); |
| 66 | + |
| 67 | + test("re-authenticates when silent renewal errors", async () => { |
| 68 | + const store = await initAuth(); |
| 69 | + store.setAuthenticating(false); |
| 70 | + signinRedirect.mockClear(); |
| 71 | + |
| 72 | + captured.renewError!(new Error("silent renew failed")); |
| 73 | + |
| 74 | + expect(signinRedirect).toHaveBeenCalledTimes(1); |
| 75 | + }); |
| 76 | + |
| 77 | + test("does not re-authenticate while an auth flow is already running", async () => { |
| 78 | + const store = await initAuth(); |
| 79 | + expect(store.isAuthenticating).toBe(true); // left true by the initial redirect |
| 80 | + signinRedirect.mockClear(); |
| 81 | + |
| 82 | + captured.expired!(); |
| 83 | + |
| 84 | + expect(signinRedirect).not.toHaveBeenCalled(); |
| 85 | + }); |
| 86 | + |
| 87 | + test("does not re-authenticate on the logged-out route", async () => { |
| 88 | + const store = await initAuth(); |
| 89 | + store.setAuthenticating(false); |
| 90 | + window.location.hash = `#${routeLinks.loggedOut}`; |
| 91 | + signinRedirect.mockClear(); |
| 92 | + |
| 93 | + captured.expired!(); |
| 94 | + |
| 95 | + expect(signinRedirect).not.toHaveBeenCalled(); |
| 96 | + }); |
| 97 | +}); |
0 commit comments