|
1 | | -import { hasAuthParams, loginError, tokenError } from '../src/utils'; |
| 1 | +import { hasAuthParams, loginError, tokenError, deprecateRedirectUri } from '../src/utils'; |
2 | 2 | import { OAuthError } from '../src/errors'; |
3 | 3 |
|
| 4 | +// Define interfaces for testing deprecateRedirectUri function |
| 5 | +interface TestOptionsWithRedirectUri { |
| 6 | + redirectUri?: string; |
| 7 | + authorizationParams?: { |
| 8 | + redirect_uri?: string; |
| 9 | + scope?: string; |
| 10 | + }; |
| 11 | +} |
| 12 | + |
| 13 | +interface TestOptionsWithAuthorizationParams { |
| 14 | + authorizationParams: { |
| 15 | + redirectUri?: string; |
| 16 | + redirect_uri?: string; |
| 17 | + scope?: string; |
| 18 | + }; |
| 19 | +} |
| 20 | + |
| 21 | +interface TestOptionsWithBothRedirectUri { |
| 22 | + redirectUri?: string; |
| 23 | + authorizationParams: { |
| 24 | + scope: string; |
| 25 | + redirect_uri?: string; |
| 26 | + }; |
| 27 | +} |
| 28 | + |
4 | 29 | describe('utils hasAuthParams', () => { |
5 | 30 | it('should not recognise only the code param', async () => { |
6 | 31 | ['?code=1', '?foo=1&code=2', '?code=1&foo=2'].forEach((search) => |
@@ -62,3 +87,75 @@ describe('utils error', () => { |
62 | 87 | }).toThrowError('Login failed'); |
63 | 88 | }); |
64 | 89 | }); |
| 90 | + |
| 91 | +describe('utils deprecateRedirectUri', () => { |
| 92 | + let consoleSpy: jest.SpyInstance; |
| 93 | + |
| 94 | + beforeEach(() => { |
| 95 | + consoleSpy = jest.spyOn(console, 'warn').mockImplementation(() => {}); |
| 96 | + }); |
| 97 | + |
| 98 | + afterEach(() => { |
| 99 | + consoleSpy.mockRestore(); |
| 100 | + }); |
| 101 | + |
| 102 | + it('should handle options with redirectUri', () => { |
| 103 | + const options: TestOptionsWithRedirectUri = { |
| 104 | + redirectUri: 'https://example.com/callback', |
| 105 | + }; |
| 106 | + |
| 107 | + deprecateRedirectUri(options); |
| 108 | + |
| 109 | + expect(consoleSpy).toHaveBeenCalledWith( |
| 110 | + 'Using `redirectUri` has been deprecated, please use `authorizationParams.redirect_uri` instead as `redirectUri` will be no longer supported in a future version' |
| 111 | + ); |
| 112 | + expect(options.authorizationParams?.redirect_uri).toBe('https://example.com/callback'); |
| 113 | + expect(options.redirectUri).toBeUndefined(); |
| 114 | + }); |
| 115 | + |
| 116 | + it('should handle options with authorizationParams.redirectUri', () => { |
| 117 | + const options: TestOptionsWithAuthorizationParams = { |
| 118 | + authorizationParams: { |
| 119 | + redirectUri: 'https://example.com/callback', |
| 120 | + }, |
| 121 | + }; |
| 122 | + |
| 123 | + deprecateRedirectUri(options); |
| 124 | + |
| 125 | + expect(consoleSpy).toHaveBeenCalledWith( |
| 126 | + 'Using `authorizationParams.redirectUri` has been deprecated, please use `authorizationParams.redirect_uri` instead as `authorizationParams.redirectUri` will be removed in a future version' |
| 127 | + ); |
| 128 | + expect(options.authorizationParams.redirect_uri).toBe('https://example.com/callback'); |
| 129 | + expect(options.authorizationParams.redirectUri).toBeUndefined(); |
| 130 | + }); |
| 131 | + |
| 132 | + it('should handle options with both redirectUri and existing authorizationParams', () => { |
| 133 | + const options: TestOptionsWithBothRedirectUri = { |
| 134 | + redirectUri: 'https://example.com/callback', |
| 135 | + authorizationParams: { |
| 136 | + scope: 'openid profile', |
| 137 | + }, |
| 138 | + }; |
| 139 | + |
| 140 | + deprecateRedirectUri(options); |
| 141 | + |
| 142 | + expect(options.authorizationParams.redirect_uri).toBe('https://example.com/callback'); |
| 143 | + expect(options.authorizationParams.scope).toBe('openid profile'); |
| 144 | + expect(options.redirectUri).toBeUndefined(); |
| 145 | + }); |
| 146 | + |
| 147 | + it('should handle undefined options', () => { |
| 148 | + deprecateRedirectUri(undefined); |
| 149 | + expect(consoleSpy).not.toHaveBeenCalled(); |
| 150 | + }); |
| 151 | + |
| 152 | + it('should handle options without redirectUri properties', () => { |
| 153 | + const options = { |
| 154 | + domain: 'example.auth0.com', |
| 155 | + clientId: 'client-id', |
| 156 | + }; |
| 157 | + |
| 158 | + deprecateRedirectUri(options); |
| 159 | + expect(consoleSpy).not.toHaveBeenCalled(); |
| 160 | + }); |
| 161 | +}); |
0 commit comments