|
| 1 | +import { Auth0Client } from '@auth0/auth0-spa-js'; |
| 2 | +import { WebWebAuthProvider } from '../WebWebAuthProvider'; |
| 3 | +import { finalizeScope } from '../../../../core/utils'; |
| 4 | +import { AuthError } from '../../../../core/models'; |
| 5 | + |
| 6 | +// Mock the direct dependencies |
| 7 | +jest.mock('@auth0/auth0-spa-js'); |
| 8 | +jest.mock('../../../../core/utils/scope'); |
| 9 | + |
| 10 | +const MockAuth0Client = Auth0Client as jest.MockedClass<typeof Auth0Client>; |
| 11 | +const mockFinalizeScope = finalizeScope as jest.Mock; |
| 12 | + |
| 13 | +describe('WebWebAuthProvider', () => { |
| 14 | + let mockSpaClient: jest.Mocked<Auth0Client>; |
| 15 | + let provider: WebWebAuthProvider; |
| 16 | + |
| 17 | + beforeEach(() => { |
| 18 | + jest.clearAllMocks(); |
| 19 | + |
| 20 | + // Create a mock instance of the underlying spa-js client. |
| 21 | + // This is the object that will be passed into our provider's constructor. |
| 22 | + mockSpaClient = new MockAuth0Client({} as any); |
| 23 | + |
| 24 | + // Construct the provider with the mock dependency. |
| 25 | + provider = new WebWebAuthProvider(mockSpaClient); |
| 26 | + |
| 27 | + // Provide a default mock implementation for the scope utility. |
| 28 | + mockFinalizeScope.mockImplementation( |
| 29 | + (scope) => scope || 'openid profile email' |
| 30 | + ); |
| 31 | + }); |
| 32 | + |
| 33 | + describe('authorize', () => { |
| 34 | + it('should call finalizeScope with the provided scope', () => { |
| 35 | + // The promise from authorize() is designed to never resolve, so we don't await it. |
| 36 | + // We are only testing the synchronous calls that happen before the redirect. |
| 37 | + provider.authorize({ scope: 'read:data' }); |
| 38 | + expect(mockFinalizeScope).toHaveBeenCalledWith('read:data'); |
| 39 | + }); |
| 40 | + |
| 41 | + it('should call loginWithRedirect on the spa-js client with all parameters', () => { |
| 42 | + const parameters = { |
| 43 | + audience: 'https://api.example.com', |
| 44 | + connection: 'Username-Password-Authentication', |
| 45 | + redirectUrl: 'https://app.com/callback', |
| 46 | + scope: 'openid profile read:data', |
| 47 | + }; |
| 48 | + |
| 49 | + // Re-configure the mock scope utility for this specific test |
| 50 | + mockFinalizeScope.mockReturnValue('openid profile read:data'); |
| 51 | + |
| 52 | + provider.authorize(parameters); |
| 53 | + |
| 54 | + expect(mockSpaClient.loginWithRedirect).toHaveBeenCalledTimes(1); |
| 55 | + expect(mockSpaClient.loginWithRedirect).toHaveBeenCalledWith({ |
| 56 | + authorizationParams: { |
| 57 | + audience: parameters.audience, |
| 58 | + connection: parameters.connection, |
| 59 | + scope: 'openid profile read:data', |
| 60 | + redirect_uri: parameters.redirectUrl, |
| 61 | + }, |
| 62 | + }); |
| 63 | + }); |
| 64 | + |
| 65 | + it('should throw an error to interrupt the app flow, as it redirects', async () => { |
| 66 | + // This confirms the promise doesn't resolve with credentials, which is correct behavior for a redirect. |
| 67 | + // It should hang until Jest times it out, but we can check if it throws our specific Redirecting error. |
| 68 | + // In your latest implementation, it just returns a hanging promise. Let's test that it doesn't resolve or reject quickly. |
| 69 | + const authorizePromise = provider.authorize({}); |
| 70 | + |
| 71 | + const timeout = new Promise((resolve) => setTimeout(resolve, 100)); |
| 72 | + |
| 73 | + // We expect the test to finish (due to timeout) before the authorizePromise resolves. |
| 74 | + // This confirms it's a hanging promise. |
| 75 | + await expect( |
| 76 | + Promise.race([authorizePromise, timeout]) |
| 77 | + ).resolves.toBeUndefined(); |
| 78 | + }); |
| 79 | + }); |
| 80 | + |
| 81 | + describe('clearSession', () => { |
| 82 | + it("should call the client's logout method with correct parameters", async () => { |
| 83 | + const parameters = { |
| 84 | + returnToUrl: 'https://app.com/logout', |
| 85 | + federated: true, |
| 86 | + }; |
| 87 | + |
| 88 | + await provider.clearSession(parameters); |
| 89 | + |
| 90 | + expect(mockSpaClient.logout).toHaveBeenCalledTimes(1); |
| 91 | + expect(mockSpaClient.logout).toHaveBeenCalledWith({ |
| 92 | + logoutParams: { |
| 93 | + returnTo: parameters.returnToUrl, |
| 94 | + federated: parameters.federated, |
| 95 | + }, |
| 96 | + }); |
| 97 | + }); |
| 98 | + |
| 99 | + it('should throw an AuthError if the client logout fails', async () => { |
| 100 | + const logoutError = { |
| 101 | + error: 'logout_failed', |
| 102 | + error_description: 'Could not log out', |
| 103 | + }; |
| 104 | + mockSpaClient.logout.mockRejectedValue(logoutError); |
| 105 | + |
| 106 | + await expect(provider.clearSession()).rejects.toThrow(AuthError); |
| 107 | + await expect(provider.clearSession()).rejects.toMatchObject({ |
| 108 | + name: 'logout_failed', |
| 109 | + }); |
| 110 | + }); |
| 111 | + }); |
| 112 | + |
| 113 | + describe('cancelWebAuth', () => { |
| 114 | + it('should resolve immediately as it is a no-op on the web', async () => { |
| 115 | + await expect(provider.cancelWebAuth()).resolves.toBeUndefined(); |
| 116 | + expect(mockSpaClient.loginWithRedirect).not.toHaveBeenCalled(); |
| 117 | + expect(mockSpaClient.logout).not.toHaveBeenCalled(); |
| 118 | + }); |
| 119 | + }); |
| 120 | +}); |
0 commit comments