|
| 1 | +import { TestBed } from '@angular/core/testing'; |
| 2 | +import { |
| 3 | + AuthRedirectService, |
| 4 | + AuthService, |
| 5 | + AuthStorageService, |
| 6 | + AuthToken, |
| 7 | + GlobalMessageService, |
| 8 | + OAuthLibWrapperService, |
| 9 | + OccEndpointsService, |
| 10 | + RoutingService, |
| 11 | +} from '@spartacus/core'; |
| 12 | +import { of } from 'rxjs'; |
| 13 | +import { PunchoutAuthHttpHeaderService } from './punchout-auth-http-header.service'; |
| 14 | +import { PunchoutDetectionService } from './punchout-detection.service'; |
| 15 | + |
| 16 | +class MockPunchoutDetectionService |
| 17 | + implements Partial<PunchoutDetectionService> |
| 18 | +{ |
| 19 | + isPunchoutSessionPage(): boolean { |
| 20 | + return true; |
| 21 | + } |
| 22 | +} |
| 23 | + |
| 24 | +class MockAuthService implements Partial<AuthService> { |
| 25 | + coreLogout(): Promise<void> { |
| 26 | + return Promise.resolve(); |
| 27 | + } |
| 28 | +} |
| 29 | + |
| 30 | +class MockAuthStorageService implements Partial<AuthStorageService> { |
| 31 | + getToken() { |
| 32 | + return of({ access_token: 'acc_token' } as AuthToken); |
| 33 | + } |
| 34 | +} |
| 35 | + |
| 36 | +class MockOAuthLibWrapperService implements Partial<OAuthLibWrapperService> {} |
| 37 | + |
| 38 | +class MockRoutingService implements Partial<RoutingService> { |
| 39 | + go = () => Promise.resolve(true); |
| 40 | +} |
| 41 | + |
| 42 | +class MockGlobalMessageService implements Partial<GlobalMessageService> { |
| 43 | + add() {} |
| 44 | + remove() {} |
| 45 | +} |
| 46 | + |
| 47 | +class MockOccEndpointsService implements Partial<OccEndpointsService> { |
| 48 | + getBaseUrl() { |
| 49 | + return 'some-server/occ'; |
| 50 | + } |
| 51 | + getRawEndpointValue(): string { |
| 52 | + return 'some-endpoint'; |
| 53 | + } |
| 54 | +} |
| 55 | + |
| 56 | +class MockAuthRedirectService implements Partial<AuthRedirectService> { |
| 57 | + saveCurrentNavigationUrl = jasmine.createSpy('saveCurrentNavigationUrl'); |
| 58 | +} |
| 59 | + |
| 60 | +describe('PunchoutAuthHttpHeaderService', () => { |
| 61 | + let service: PunchoutAuthHttpHeaderService; |
| 62 | + let authService: AuthService; |
| 63 | + let punchoutDetectionService: PunchoutDetectionService; |
| 64 | + let globalMessageService: GlobalMessageService; |
| 65 | + beforeEach(() => { |
| 66 | + TestBed.configureTestingModule({ |
| 67 | + imports: [], |
| 68 | + providers: [ |
| 69 | + { |
| 70 | + provide: PunchoutDetectionService, |
| 71 | + useClass: MockPunchoutDetectionService, |
| 72 | + }, |
| 73 | + { provide: AuthService, useClass: MockAuthService }, |
| 74 | + { provide: GlobalMessageService, useClass: MockGlobalMessageService }, |
| 75 | + { provide: OccEndpointsService, useClass: MockOccEndpointsService }, |
| 76 | + { provide: AuthStorageService, useClass: MockAuthStorageService }, |
| 77 | + { provide: AuthRedirectService, useClass: MockAuthRedirectService }, |
| 78 | + { |
| 79 | + provide: OAuthLibWrapperService, |
| 80 | + useClass: MockOAuthLibWrapperService, |
| 81 | + }, |
| 82 | + { provide: RoutingService, useClass: MockRoutingService }, |
| 83 | + ], |
| 84 | + }); |
| 85 | + service = TestBed.inject(PunchoutAuthHttpHeaderService); |
| 86 | + authService = TestBed.inject(AuthService); |
| 87 | + punchoutDetectionService = TestBed.inject(PunchoutDetectionService); |
| 88 | + globalMessageService = TestBed.inject(GlobalMessageService); |
| 89 | + }); |
| 90 | + |
| 91 | + it('should be created', () => { |
| 92 | + expect(service).toBeTruthy(); |
| 93 | + }); |
| 94 | + |
| 95 | + it('should handleExpiredRefreshToken call super coreLogout when not Punchout Session page', async () => { |
| 96 | + spyOn(authService, 'coreLogout').and.callThrough(); |
| 97 | + spyOn(punchoutDetectionService, 'isPunchoutSessionPage').and.returnValue( |
| 98 | + false |
| 99 | + ); |
| 100 | + spyOn(globalMessageService, 'remove').and.callThrough(); |
| 101 | + |
| 102 | + service.handleExpiredRefreshToken(); |
| 103 | + await Promise.resolve(); |
| 104 | + |
| 105 | + expect(authService.coreLogout).toHaveBeenCalled(); |
| 106 | + expect(globalMessageService.remove).not.toHaveBeenCalled(); |
| 107 | + }); |
| 108 | + |
| 109 | + it('should handleExpiredRefreshToken silently logout when Punchout Session page', async () => { |
| 110 | + spyOn(authService, 'coreLogout').and.callThrough(); |
| 111 | + spyOn(punchoutDetectionService, 'isPunchoutSessionPage').and.returnValue( |
| 112 | + true |
| 113 | + ); |
| 114 | + spyOn(globalMessageService, 'remove').and.callThrough(); |
| 115 | + |
| 116 | + service.handleExpiredRefreshToken(); |
| 117 | + await Promise.resolve(); |
| 118 | + |
| 119 | + expect(authService.coreLogout).toHaveBeenCalled(); |
| 120 | + expect(globalMessageService.remove).toHaveBeenCalled(); |
| 121 | + }); |
| 122 | +}); |
0 commit comments