|
| 1 | +import type { AccountInfo } from '@azure/msal-browser' |
| 2 | + |
| 3 | +const TENANT_ID = 'tenant-123' |
| 4 | +const OTHER_TENANT = 'other-tenant' |
| 5 | + |
| 6 | +const makeAccount = (tenantId: string, username: string): AccountInfo => |
| 7 | + ({ |
| 8 | + homeAccountId: `${username}.${tenantId}`, |
| 9 | + environment: 'login.microsoftonline.com', |
| 10 | + tenantId, |
| 11 | + username, |
| 12 | + localAccountId: username, |
| 13 | + }) as AccountInfo |
| 14 | + |
| 15 | +const tenantAccount = makeAccount(TENANT_ID, 'user@example.com') |
| 16 | +const otherAccount = makeAccount(OTHER_TENANT, 'other@example.com') |
| 17 | +const authResult = { |
| 18 | + accessToken: 'access-token', |
| 19 | + account: tenantAccount, |
| 20 | + expiresOn: new Date(Date.now() + 3_600_000), |
| 21 | +} |
| 22 | + |
| 23 | +describe('msal utils', () => { |
| 24 | + let mockPca: { |
| 25 | + handleRedirectPromise: jest.Mock |
| 26 | + getActiveAccount: jest.Mock |
| 27 | + getAllAccounts: jest.Mock |
| 28 | + setActiveAccount: jest.Mock |
| 29 | + acquireTokenSilent: jest.Mock |
| 30 | + ssoSilent: jest.Mock |
| 31 | + loginRedirect: jest.Mock |
| 32 | + logoutRedirect: jest.Mock |
| 33 | + } |
| 34 | + |
| 35 | + let initializeMsal: () => Promise<void> |
| 36 | + let loginSilently: () => Promise<boolean> |
| 37 | + let hasCachedAccounts: () => Promise<boolean> |
| 38 | + |
| 39 | + beforeEach(async () => { |
| 40 | + jest.resetModules() |
| 41 | + |
| 42 | + mockPca = { |
| 43 | + handleRedirectPromise: jest.fn().mockResolvedValue(null), |
| 44 | + getActiveAccount: jest.fn().mockReturnValue(null), |
| 45 | + getAllAccounts: jest.fn().mockReturnValue([]), |
| 46 | + setActiveAccount: jest.fn(), |
| 47 | + acquireTokenSilent: jest.fn(), |
| 48 | + ssoSilent: jest.fn(), |
| 49 | + loginRedirect: jest.fn(), |
| 50 | + logoutRedirect: jest.fn(), |
| 51 | + } |
| 52 | + |
| 53 | + jest.doMock('@azure/msal-browser', () => ({ |
| 54 | + createStandardPublicClientApplication: jest.fn().mockResolvedValue(mockPca), |
| 55 | + })) |
| 56 | + |
| 57 | + jest.doMock('@/api/auth', () => ({ |
| 58 | + GetAuthConfig: jest.fn().mockResolvedValue({ |
| 59 | + enabled: true, |
| 60 | + client_id: 'client-id', |
| 61 | + tenant_id: TENANT_ID, |
| 62 | + audience: 'https://audience', |
| 63 | + }), |
| 64 | + })) |
| 65 | + |
| 66 | + const module = await import('@/utils/msal') |
| 67 | + initializeMsal = module.initializeMsal |
| 68 | + loginSilently = module.loginSilently |
| 69 | + hasCachedAccounts = module.hasCachedAccounts |
| 70 | + }) |
| 71 | + |
| 72 | + describe('hasCachedAccounts', () => { |
| 73 | + it('returns false when no accounts are cached', async () => { |
| 74 | + await initializeMsal() |
| 75 | + expect(await hasCachedAccounts()).toBe(false) |
| 76 | + }) |
| 77 | + |
| 78 | + it('returns false when only accounts from other tenants are cached', async () => { |
| 79 | + await initializeMsal() |
| 80 | + mockPca.getAllAccounts.mockReturnValue([otherAccount]) |
| 81 | + expect(await hasCachedAccounts()).toBe(false) |
| 82 | + }) |
| 83 | + |
| 84 | + it('returns true when a cached account matches the configured tenant', async () => { |
| 85 | + await initializeMsal() |
| 86 | + mockPca.getAllAccounts.mockReturnValue([tenantAccount]) |
| 87 | + expect(await hasCachedAccounts()).toBe(true) |
| 88 | + }) |
| 89 | + |
| 90 | + it('returns true when mixed accounts include one for the configured tenant', async () => { |
| 91 | + await initializeMsal() |
| 92 | + mockPca.getAllAccounts.mockReturnValue([otherAccount, tenantAccount]) |
| 93 | + expect(await hasCachedAccounts()).toBe(true) |
| 94 | + }) |
| 95 | + }) |
| 96 | + |
| 97 | + describe('loginSilently', () => { |
| 98 | + it('returns true when acquireTokenSilent succeeds', async () => { |
| 99 | + await initializeMsal() |
| 100 | + mockPca.getAllAccounts.mockReturnValue([tenantAccount]) |
| 101 | + mockPca.acquireTokenSilent.mockResolvedValue(authResult) |
| 102 | + |
| 103 | + expect(await loginSilently()).toBe(true) |
| 104 | + expect(mockPca.ssoSilent).not.toHaveBeenCalled() |
| 105 | + }) |
| 106 | + |
| 107 | + it('falls back to ssoSilent when acquireTokenSilent fails', async () => { |
| 108 | + await initializeMsal() |
| 109 | + mockPca.getAllAccounts.mockReturnValue([tenantAccount]) |
| 110 | + mockPca.acquireTokenSilent.mockRejectedValue(new Error('token expired')) |
| 111 | + mockPca.ssoSilent.mockResolvedValue({ ...authResult }) |
| 112 | + |
| 113 | + expect(await loginSilently()).toBe(true) |
| 114 | + expect(mockPca.ssoSilent).toHaveBeenCalledWith( |
| 115 | + expect.objectContaining({ loginHint: tenantAccount.username }), |
| 116 | + ) |
| 117 | + }) |
| 118 | + |
| 119 | + it('returns false when both acquireTokenSilent and ssoSilent fail', async () => { |
| 120 | + await initializeMsal() |
| 121 | + mockPca.getAllAccounts.mockReturnValue([tenantAccount]) |
| 122 | + mockPca.acquireTokenSilent.mockRejectedValue(new Error('token expired')) |
| 123 | + mockPca.ssoSilent.mockRejectedValue(new Error('sso failed')) |
| 124 | + |
| 125 | + expect(await loginSilently()).toBe(false) |
| 126 | + }) |
| 127 | + |
| 128 | + it('selects the tenant-matching account when multiple accounts are cached', async () => { |
| 129 | + await initializeMsal() |
| 130 | + mockPca.getAllAccounts.mockReturnValue([otherAccount, tenantAccount]) |
| 131 | + mockPca.acquireTokenSilent.mockResolvedValue(authResult) |
| 132 | + |
| 133 | + await loginSilently() |
| 134 | + |
| 135 | + expect(mockPca.setActiveAccount).toHaveBeenCalledWith(tenantAccount) |
| 136 | + expect(mockPca.setActiveAccount).not.toHaveBeenCalledWith(otherAccount) |
| 137 | + }) |
| 138 | + |
| 139 | + it('clears a wrong active account before throwing when no tenant account exists', async () => { |
| 140 | + await initializeMsal() |
| 141 | + mockPca.getActiveAccount.mockReturnValue(otherAccount) |
| 142 | + mockPca.getAllAccounts.mockReturnValue([otherAccount]) |
| 143 | + |
| 144 | + expect(await loginSilently()).toBe(false) |
| 145 | + expect(mockPca.setActiveAccount).toHaveBeenCalledWith(null) |
| 146 | + }) |
| 147 | + }) |
| 148 | +}) |
0 commit comments