|
1 | 1 | import { fakeAsync, TestBed } from '@angular/core/testing'; |
2 | 2 | import { AuthService } from './auth.service'; |
3 | 3 | import { Auth0ClientService } from './auth.client'; |
4 | | -import { Auth0Client, IdToken } from '@auth0/auth0-spa-js'; |
| 4 | +import { |
| 5 | + Auth0Client, |
| 6 | + IdToken, |
| 7 | + ResponseType, |
| 8 | + ConnectAccountRedirectResult, |
| 9 | +} from '@auth0/auth0-spa-js'; |
5 | 10 | import { AbstractNavigator } from './abstract-navigator'; |
6 | 11 | import { |
7 | 12 | bufferCount, |
@@ -60,6 +65,7 @@ describe('AuthService', () => { |
60 | 65 |
|
61 | 66 | jest.spyOn(auth0Client, 'handleRedirectCallback').mockResolvedValue({ |
62 | 67 | appState: undefined, |
| 68 | + response_type: ResponseType.Code, |
63 | 69 | } as any); |
64 | 70 | jest.spyOn(auth0Client, 'loginWithRedirect').mockResolvedValue(); |
65 | 71 | jest.spyOn(auth0Client, 'connectAccountWithRedirect').mockResolvedValue(); |
@@ -513,6 +519,16 @@ describe('AuthService', () => { |
513 | 519 | }); |
514 | 520 | }); |
515 | 521 |
|
| 522 | + it('should handle the callback when connect_code and state are available', (done) => { |
| 523 | + mockWindow.location.search = '?connect_code=123&state=456'; |
| 524 | + const localService = createService(); |
| 525 | + |
| 526 | + loaded(localService).subscribe(() => { |
| 527 | + expect(auth0Client.handleRedirectCallback).toHaveBeenCalledTimes(1); |
| 528 | + done(); |
| 529 | + }); |
| 530 | + }); |
| 531 | + |
516 | 532 | it('should not handle the callback when skipRedirectCallback is true', (done) => { |
517 | 533 | mockWindow.location.search = '?code=123&state=456'; |
518 | 534 | authConfig.skipRedirectCallback = true; |
@@ -1126,6 +1142,90 @@ describe('AuthService', () => { |
1126 | 1142 | }); |
1127 | 1143 | }); |
1128 | 1144 | }); |
| 1145 | + |
| 1146 | + it('should add response_type to appState for regular login', (done) => { |
| 1147 | + const appState = { |
| 1148 | + myValue: 'State to Preserve', |
| 1149 | + }; |
| 1150 | + |
| 1151 | + ( |
| 1152 | + auth0Client.handleRedirectCallback as unknown as jest.SpyInstance |
| 1153 | + ).mockResolvedValue({ |
| 1154 | + appState, |
| 1155 | + response_type: ResponseType.Code, |
| 1156 | + }); |
| 1157 | + |
| 1158 | + const localService = createService(); |
| 1159 | + localService.handleRedirectCallback().subscribe(() => { |
| 1160 | + localService.appState$.subscribe((receivedState) => { |
| 1161 | + expect(receivedState).toEqual({ |
| 1162 | + ...appState, |
| 1163 | + response_type: ResponseType.Code, |
| 1164 | + }); |
| 1165 | + done(); |
| 1166 | + }); |
| 1167 | + }); |
| 1168 | + }); |
| 1169 | + |
| 1170 | + it('should extract connected account data when response_type is ConnectCode', (done) => { |
| 1171 | + const appState = { |
| 1172 | + myValue: 'State to Preserve', |
| 1173 | + }; |
| 1174 | + |
| 1175 | + const connectedAccount = { |
| 1176 | + id: 'abc123', |
| 1177 | + connection: 'google-oauth2', |
| 1178 | + access_type: 'offline' as ConnectAccountRedirectResult['access_type'], |
| 1179 | + created_at: '2024-01-01T00:00:00.000Z', |
| 1180 | + expires_at: '2024-01-02T00:00:00.000Z', |
| 1181 | + }; |
| 1182 | + |
| 1183 | + ( |
| 1184 | + auth0Client.handleRedirectCallback as unknown as jest.SpyInstance |
| 1185 | + ).mockResolvedValue({ |
| 1186 | + appState, |
| 1187 | + response_type: ResponseType.ConnectCode, |
| 1188 | + ...connectedAccount, |
| 1189 | + }); |
| 1190 | + |
| 1191 | + const localService = createService(); |
| 1192 | + localService.handleRedirectCallback().subscribe(() => { |
| 1193 | + localService.appState$.subscribe((receivedState) => { |
| 1194 | + expect(receivedState).toEqual({ |
| 1195 | + ...appState, |
| 1196 | + response_type: ResponseType.ConnectCode, |
| 1197 | + connectedAccount, |
| 1198 | + }); |
| 1199 | + done(); |
| 1200 | + }); |
| 1201 | + }); |
| 1202 | + }); |
| 1203 | + |
| 1204 | + it('should handle connected account redirect without initial appState', (done) => { |
| 1205 | + const connectedAccount = { |
| 1206 | + id: 'xyz789', |
| 1207 | + connection: 'github', |
| 1208 | + access_type: 'offline' as ConnectAccountRedirectResult['access_type'], |
| 1209 | + created_at: '2024-02-01T00:00:00.000Z', |
| 1210 | + expires_at: '2024-02-02T00:00:00.000Z', |
| 1211 | + }; |
| 1212 | + |
| 1213 | + ( |
| 1214 | + auth0Client.handleRedirectCallback as unknown as jest.SpyInstance |
| 1215 | + ).mockResolvedValue({ |
| 1216 | + response_type: ResponseType.ConnectCode, |
| 1217 | + ...connectedAccount, |
| 1218 | + }); |
| 1219 | + |
| 1220 | + const localService = createService(); |
| 1221 | + localService.handleRedirectCallback().subscribe(() => { |
| 1222 | + localService.appState$.subscribe((receivedState) => { |
| 1223 | + expect(receivedState.response_type).toBe(ResponseType.ConnectCode); |
| 1224 | + expect(receivedState.connectedAccount).toEqual(connectedAccount); |
| 1225 | + done(); |
| 1226 | + }); |
| 1227 | + }); |
| 1228 | + }); |
1129 | 1229 | }); |
1130 | 1230 |
|
1131 | 1231 | describe('getDpopNonce', () => { |
|
0 commit comments