|
| 1 | +import { beforeEach, describe, expect, test, vi } from "vitest"; |
| 2 | +import { createPinia, setActivePinia } from "pinia"; |
| 3 | +import serviceControlClient from "@/components/serviceControlClient"; |
| 4 | +import { useAuthStore } from "@/stores/AuthStore"; |
| 5 | + |
| 6 | +describe("AuthStore tests", () => { |
| 7 | + const baseResponse = { |
| 8 | + enabled: true, |
| 9 | + role_based_authorization_enabled: true, |
| 10 | + client_id: "test-client-id", |
| 11 | + authority: "https://login.example.com", |
| 12 | + api_scopes: JSON.stringify(["api://test-audience/.default"]), |
| 13 | + audience: "api://test-audience", |
| 14 | + }; |
| 15 | + |
| 16 | + beforeEach(() => { |
| 17 | + setActivePinia(createPinia()); |
| 18 | + }); |
| 19 | + |
| 20 | + test("uses the composed scopes field from ServiceControl when present", async () => { |
| 21 | + vi.spyOn(serviceControlClient, "fetchTypedFromServiceControl").mockResolvedValue([ |
| 22 | + {} as Response, |
| 23 | + { |
| 24 | + ...baseResponse, |
| 25 | + scopes: "api://test-audience/.default openid profile email", |
| 26 | + }, |
| 27 | + ]); |
| 28 | + |
| 29 | + const store = useAuthStore(); |
| 30 | + await store.refresh(); |
| 31 | + |
| 32 | + expect(store.authConfig?.scope).toBe("api://test-audience/.default openid profile email"); |
| 33 | + }); |
| 34 | + |
| 35 | + test("falls back to assembling scopes from api_scopes when talking to an older ServiceControl", async () => { |
| 36 | + // Older ServiceControl versions don't send the `scopes` field at all. |
| 37 | + vi.spyOn(serviceControlClient, "fetchTypedFromServiceControl").mockResolvedValue([{} as Response, { ...baseResponse }]); |
| 38 | + |
| 39 | + const store = useAuthStore(); |
| 40 | + await store.refresh(); |
| 41 | + |
| 42 | + expect(store.authConfig?.scope).toBe("api://test-audience/.default openid profile email offline_access"); |
| 43 | + }); |
| 44 | +}); |
0 commit comments