11import { TestBed , waitForAsync } from '@angular/core/testing' ;
2- import { of } from 'rxjs' ;
2+ import { Observable , of , throwError } from 'rxjs' ;
33import { DataService } from '../api/data.service' ;
44import { DataServiceMock } from '../api/data.service-mock' ;
55import { LoggerService } from '../logging/logger.service' ;
@@ -79,8 +79,8 @@ describe('Configuration Service', () => {
7979 expect ( oidcConfigService ) . toBeTruthy ( ) ;
8080 } ) ;
8181
82- it ( 'should return a promise ' , ( ) => {
83- expect ( oidcConfigService . withConfigs ( [ ] ) ) . toEqual ( jasmine . any ( Promise ) ) ;
82+ it ( 'should return an Observable ' , ( ) => {
83+ expect ( oidcConfigService . withConfigs ( [ ] ) ) . toEqual ( jasmine . any ( Observable ) ) ;
8484 } ) ;
8585
8686 describe ( 'withConfigs' , ( ) => {
@@ -90,9 +90,9 @@ describe('Configuration Service', () => {
9090 const config = { } ;
9191 spyOn ( configValidationService , 'validateConfigs' ) . and . returnValue ( false ) ;
9292
93- const promise = oidcConfigService . withConfigs ( [ config ] ) ;
93+ const configs$ = oidcConfigService . withConfigs ( [ config ] ) ;
9494
95- promise . then ( ( result ) => {
95+ configs$ . subscribe ( ( result ) => {
9696 expect ( result ) . toBeNull ( ) ;
9797 } ) ;
9898 } )
@@ -105,9 +105,9 @@ describe('Configuration Service', () => {
105105 spyOn ( loggerService , 'logError' ) ;
106106 spyOn ( configValidationService , 'validateConfig' ) . and . returnValue ( false ) ;
107107
108- const promise = oidcConfigService . withConfigs ( [ config ] ) ;
108+ const obs$ = oidcConfigService . withConfigs ( [ config ] ) ;
109109
110- promise . then ( ( ) => {
110+ obs$ . subscribe ( ( ) => {
111111 expect ( loggerService . logError ) . toHaveBeenCalled ( ) ;
112112 } ) ;
113113 } )
@@ -118,9 +118,9 @@ describe('Configuration Service', () => {
118118 waitForAsync ( ( ) => {
119119 spyOn ( authWellKnownService , 'getAuthWellKnownEndPoints' ) . and . returnValue ( of ( null ) ) ;
120120 spyOn ( configValidationService , 'validateConfig' ) . and . returnValue ( true ) ;
121- const promise = oidcConfigService . withConfigs ( [ { authority : 'https://please_set' , clientId : 'clientId' } ] ) ;
121+ const obs$ = oidcConfigService . withConfigs ( [ { authority : 'https://please_set' , clientId : 'clientId' } ] ) ;
122122
123- promise . then ( ( result ) => {
123+ obs$ . subscribe ( ( result ) => {
124124 expect ( result [ 0 ] . configId ) . toEqual ( '0-clientId' ) ;
125125 } ) ;
126126 } )
@@ -131,9 +131,9 @@ describe('Configuration Service', () => {
131131 waitForAsync ( ( ) => {
132132 spyOn ( authWellKnownService , 'getAuthWellKnownEndPoints' ) . and . returnValue ( of ( null ) ) ;
133133 spyOn ( configValidationService , 'validateConfig' ) . and . returnValue ( true ) ;
134- const promise = oidcConfigService . withConfigs ( [ { authority : 'https://please_set' , clientId : 'clientId' , configId : 'myConfigId' } ] ) ;
134+ const obs$ = oidcConfigService . withConfigs ( [ { authority : 'https://please_set' , clientId : 'clientId' , configId : 'myConfigId' } ] ) ;
135135
136- promise . then ( ( result ) => {
136+ obs$ . subscribe ( ( result ) => {
137137 expect ( result [ 0 ] . configId ) . toEqual ( 'myConfigId' ) ;
138138 } ) ;
139139 } )
@@ -144,11 +144,11 @@ describe('Configuration Service', () => {
144144 waitForAsync ( ( ) => {
145145 spyOn ( authWellKnownService , 'getAuthWellKnownEndPoints' ) . and . returnValue ( of ( null ) ) ;
146146 spyOn ( configValidationService , 'validateConfig' ) . and . returnValue ( true ) ;
147- const promise = oidcConfigService . withConfigs ( [
147+ const obs$ = oidcConfigService . withConfigs ( [
148148 { authority : 'https://please_set' , clientId : 'clientId' , authWellknownEndpointUrl : 'my-auth-url' } ,
149149 ] ) ;
150150
151- promise . then ( ( result ) => {
151+ obs$ . subscribe ( ( result ) => {
152152 expect ( result [ 0 ] . authWellknownEndpointUrl ) . toEqual ( 'my-auth-url' ) ;
153153 } ) ;
154154 } )
@@ -161,9 +161,9 @@ describe('Configuration Service', () => {
161161 spyOn ( configValidationService , 'validateConfig' ) . and . returnValue ( true ) ;
162162 spyOn ( oidcConfigService as any , 'hasBrowserStorage' ) . and . returnValue ( true ) ;
163163
164- const promise = oidcConfigService . withConfigs ( [ { authority : 'https://please_set' , clientId : 'clientId' } ] ) ;
164+ const obs$ = oidcConfigService . withConfigs ( [ { authority : 'https://please_set' , clientId : 'clientId' } ] ) ;
165165
166- promise . then ( ( result ) => {
166+ obs$ . subscribe ( ( result ) => {
167167 expect ( result ) . toEqual ( [
168168 {
169169 ...DEFAULT_CONFIG ,
@@ -190,9 +190,9 @@ describe('Configuration Service', () => {
190190 spyOn ( configValidationService , 'validateConfig' ) . and . returnValue ( true ) ;
191191 spyOn ( oidcConfigService as any , 'hasBrowserStorage' ) . and . returnValue ( true ) ;
192192
193- const promise = oidcConfigService . withConfigs ( [ config ] ) ;
193+ const obs$ = oidcConfigService . withConfigs ( [ config ] ) ;
194194
195- promise . then ( ( result ) => {
195+ obs$ . subscribe ( ( result ) => {
196196 expect ( eventServiceSpy ) . toHaveBeenCalledWith ( EventTypes . ConfigLoaded , {
197197 ...DEFAULT_CONFIG ,
198198 authority : 'authorityForTesting' ,
@@ -219,9 +219,9 @@ describe('Configuration Service', () => {
219219 const storeWellKnownEndpointsSpy = spyOn ( authWellKnownService , 'storeWellKnownEndpoints' ) ;
220220 spyOn ( oidcConfigService as any , 'hasBrowserStorage' ) . and . returnValue ( true ) ;
221221
222- const promise = oidcConfigService . withConfigs ( [ config ] ) ;
222+ const obs$ = oidcConfigService . withConfigs ( [ config ] ) ;
223223
224- promise . then ( ( ) => {
224+ obs$ . subscribe ( ( ) => {
225225 expect ( storeWellKnownEndpointsSpy ) . toHaveBeenCalledWith ( '0-clientId' , authWellKnown ) ;
226226 expect ( eventServiceSpy ) . toHaveBeenCalledWith ( EventTypes . ConfigLoaded , {
227227 ...DEFAULT_CONFIG ,
@@ -243,22 +243,41 @@ describe('Configuration Service', () => {
243243 spyOn ( storagePersistenceService , 'read' ) . withArgs ( 'authWellKnownEndPoints' , '0-clientId' ) . and . returnValue ( null ) ;
244244 spyOn ( configValidationService , 'validateConfig' ) . and . returnValue ( true ) ;
245245 const getWellKnownEndPointsFromUrlSpy = spyOn ( authWellKnownService , 'getAuthWellKnownEndPoints' ) . and . returnValue ( of ( null ) ) ;
246- const promise = oidcConfigService . withConfigs ( [ config ] ) ;
247- promise . then ( ( ) => {
246+ const obs$ = oidcConfigService . withConfigs ( [ config ] ) ;
247+ obs$ . subscribe ( ( ) => {
248248 expect ( getWellKnownEndPointsFromUrlSpy ) . toHaveBeenCalledWith ( 'authorityForTesting' , '0-clientId' ) ;
249249 } ) ;
250250 } )
251251 ) ;
252252
253+ it (
254+ 'if eagerLoadAuthWellKnownEndpoints is true but call throws error --> Error is thrown' ,
255+ waitForAsync ( ( ) => {
256+ const config = { authority : 'authorityForTesting' , clientId : 'clientId' , eagerLoadAuthWellKnownEndpoints : true } ;
257+ spyOn ( storagePersistenceService , 'read' ) . withArgs ( 'authWellKnownEndPoints' , '0-clientId' ) . and . returnValue ( null ) ;
258+ spyOn ( configValidationService , 'validateConfig' ) . and . returnValue ( true ) ;
259+ spyOn ( authWellKnownService , 'getAuthWellKnownEndPoints' ) . and . returnValue ( throwError ( ( ) => new Error ( 'ErrorError' ) ) ) ;
260+
261+ const obs$ = oidcConfigService . withConfigs ( [ config ] ) ;
262+
263+ obs$ . subscribe ( {
264+ error : ( err ) => {
265+ expect ( err ) . toBeTruthy ( ) ;
266+ expect ( err . message ) . toEqual ( 'Error: ErrorError' ) ;
267+ } ,
268+ } ) ;
269+ } )
270+ ) ;
271+
253272 it (
254273 'if eagerLoadAuthWellKnownEndpoints is false: DO NOT call getAuthWellKnownEndPoints' ,
255274 waitForAsync ( ( ) => {
256275 const config = { authority : 'authorityForTesting' , clientId : 'clientId' , eagerLoadAuthWellKnownEndpoints : false } ;
257276 spyOn ( storagePersistenceService , 'read' ) . withArgs ( 'authWellKnownEndPoints' , '0-clientId' ) . and . returnValue ( null ) ;
258277 const storeWellKnownEndpointsSpy = spyOn ( authWellKnownService , 'getAuthWellKnownEndPoints' ) . and . returnValue ( of ( null ) ) ;
259278 spyOn ( configValidationService , 'validateConfig' ) . and . returnValue ( true ) ;
260- const promise = oidcConfigService . withConfigs ( [ config ] ) ;
261- promise . then ( ( ) => {
279+ const obs$ = oidcConfigService . withConfigs ( [ config ] ) ;
280+ obs$ . subscribe ( ( ) => {
262281 expect ( storeWellKnownEndpointsSpy ) . not . toHaveBeenCalled ( ) ;
263282 } ) ;
264283 } )
@@ -279,7 +298,7 @@ describe('Configuration Service', () => {
279298 spyOn ( configValidationService , 'validateConfig' ) . and . returnValue ( true ) ;
280299 spyOnProperty ( platformProvider , 'isBrowser' ) . and . returnValue ( false ) ;
281300
282- oidcConfigService . withConfigs ( [ config ] ) . then ( ( result ) => {
301+ oidcConfigService . withConfigs ( [ config ] ) . subscribe ( ( result ) => {
283302 expect ( result [ 0 ] . silentRenew ) . toEqual ( false ) ;
284303 expect ( result [ 0 ] . startCheckSession ) . toEqual ( false ) ;
285304 } ) ;
@@ -301,7 +320,7 @@ describe('Configuration Service', () => {
301320 spyOnProperty ( platformProvider , 'isBrowser' ) . and . returnValue ( true ) ;
302321 spyOn ( authWellKnownService , 'getAuthWellKnownEndPoints' ) . and . returnValue ( of ( { issuer : 'issuerForTesting' } ) ) ;
303322
304- oidcConfigService . withConfigs ( [ config ] ) . then ( ( [ { silentRenew, startCheckSession } ] ) => {
323+ oidcConfigService . withConfigs ( [ config ] ) . subscribe ( ( [ { silentRenew, startCheckSession } ] ) => {
305324 expect ( silentRenew ) . toEqual ( true ) ;
306325 expect ( startCheckSession ) . toEqual ( true ) ;
307326 } ) ;
@@ -323,7 +342,7 @@ describe('Configuration Service', () => {
323342 const spy = spyOn ( oidcConfigService as any , 'setSpecialCases' ) ;
324343 spyOn ( authWellKnownService , 'getAuthWellKnownEndPoints' ) . and . returnValue ( of ( { issuer : 'issuerForTesting' } ) ) ;
325344
326- oidcConfigService . withConfigs ( [ config ] ) . then ( ( ) => {
345+ oidcConfigService . withConfigs ( [ config ] ) . subscribe ( ( ) => {
327346 expect ( spy ) . toHaveBeenCalled ( ) ;
328347 } ) ;
329348 } )
@@ -343,8 +362,8 @@ describe('Configuration Service', () => {
343362 spyOn ( configValidationService , 'validateConfig' ) . and . returnValue ( true ) ;
344363 spyOn ( authWellKnownService , 'getAuthWellKnownEndPoints' ) . and . returnValue ( of ( { issuer : 'issuerForTesting' } ) ) ;
345364 const eventServiceSpy = spyOn ( eventsService , 'fireEvent' ) ;
346- const promise = oidcConfigService . withConfigs ( [ config ] ) ;
347- promise . then ( ( ) => {
365+ const obs$ = oidcConfigService . withConfigs ( [ config ] ) ;
366+ obs$ . subscribe ( ( ) => {
348367 expect ( eventServiceSpy ) . toHaveBeenCalledWith ( EventTypes . ConfigLoaded , {
349368 ...DEFAULT_CONFIG ,
350369 ...config ,
@@ -376,7 +395,7 @@ describe('Configuration Service', () => {
376395 spyOn ( configValidationService , 'validateConfig' ) . and . returnValue ( true ) ;
377396 spyOn ( authWellKnownService , 'getAuthWellKnownEndPoints' ) . and . returnValue ( of ( { issuer : 'issuerForTesting' } ) ) ;
378397
379- oidcConfigService . withConfigs ( [ config ] ) . then ( ( result ) => {
398+ oidcConfigService . withConfigs ( [ config ] ) . subscribe ( ( result ) => {
380399 expect ( result ) . toEqual ( [ expected ] ) ;
381400 } ) ;
382401 } )
@@ -412,7 +431,7 @@ describe('Configuration Service', () => {
412431 spyOn ( oidcConfigService as any , 'hasBrowserStorage' ) . and . returnValue ( true ) ;
413432 spyOn ( authWellKnownService , 'getAuthWellKnownEndPoints' ) . and . returnValue ( of ( { issuer : 'issuerForTesting' } ) ) ;
414433
415- oidcConfigService . withConfigs ( [ config ] ) . then ( ( result ) => {
434+ oidcConfigService . withConfigs ( [ config ] ) . subscribe ( ( result ) => {
416435 expect ( result [ 0 ] ) . toEqual ( expected ) ;
417436 } ) ;
418437 } )
0 commit comments