|
| 1 | +/*! |
| 2 | + * @license |
| 3 | + * Copyright © 2005-2025 Hyland Software, Inc. and its affiliates. All rights reserved. |
| 4 | + * |
| 5 | + * Licensed under the Apache License, Version 2.0 (the "License"); |
| 6 | + * you may not use this file except in compliance with the License. |
| 7 | + * You may obtain a copy of the License at |
| 8 | + * |
| 9 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 10 | + * |
| 11 | + * Unless required by applicable law or agreed to in writing, software |
| 12 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 13 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 14 | + * See the License for the specific language governing permissions and |
| 15 | + * limitations under the License. |
| 16 | + */ |
| 17 | + |
| 18 | +import { TestBed } from '@angular/core/testing'; |
| 19 | +import { CrossAppAuthInitializerService } from './cross-app-auth-initializer.service'; |
| 20 | +import { CrossAppAuthIntegrationService } from './cross-app-auth-integration.service'; |
| 21 | + |
| 22 | +describe('CrossAppAuthInitializerService', () => { |
| 23 | + let service: CrossAppAuthInitializerService; |
| 24 | + let mockCrossAppIntegration: jasmine.SpyObj<CrossAppAuthIntegrationService>; |
| 25 | + |
| 26 | + beforeEach(() => { |
| 27 | + const spy = jasmine.createSpyObj('CrossAppAuthIntegrationService', ['initialize', 'processCrossAppAuthRequest']); |
| 28 | + |
| 29 | + TestBed.configureTestingModule({ |
| 30 | + providers: [CrossAppAuthInitializerService, { provide: CrossAppAuthIntegrationService, useValue: spy }] |
| 31 | + }); |
| 32 | + |
| 33 | + service = TestBed.inject(CrossAppAuthInitializerService); |
| 34 | + mockCrossAppIntegration = TestBed.inject(CrossAppAuthIntegrationService) as jasmine.SpyObj<CrossAppAuthIntegrationService>; |
| 35 | + }); |
| 36 | + |
| 37 | + it('should be created', () => { |
| 38 | + expect(service).toBeTruthy(); |
| 39 | + }); |
| 40 | + |
| 41 | + describe('initializeWithSilentLogin', () => { |
| 42 | + it('should successfully initialize and attempt silent login', async () => { |
| 43 | + mockCrossAppIntegration.initialize.and.returnValue(Promise.resolve()); |
| 44 | + mockCrossAppIntegration.processCrossAppAuthRequest.and.returnValue(Promise.resolve(true)); |
| 45 | + |
| 46 | + const result = await service.initializeCrossAppLogin(); |
| 47 | + |
| 48 | + expect(result).toBe(true); |
| 49 | + expect(mockCrossAppIntegration.initialize).toHaveBeenCalled(); |
| 50 | + expect(mockCrossAppIntegration.processCrossAppAuthRequest).toHaveBeenCalled(); |
| 51 | + }); |
| 52 | + |
| 53 | + it('should return false if silent login is not attempted', async () => { |
| 54 | + mockCrossAppIntegration.initialize.and.returnValue(Promise.resolve()); |
| 55 | + mockCrossAppIntegration.processCrossAppAuthRequest.and.returnValue(Promise.resolve(false)); |
| 56 | + |
| 57 | + const result = await service.initializeCrossAppLogin(); |
| 58 | + |
| 59 | + expect(result).toBe(false); |
| 60 | + expect(mockCrossAppIntegration.initialize).toHaveBeenCalled(); |
| 61 | + expect(mockCrossAppIntegration.processCrossAppAuthRequest).toHaveBeenCalled(); |
| 62 | + }); |
| 63 | + |
| 64 | + it('should handle initialization timeout', async () => { |
| 65 | + mockCrossAppIntegration.initialize.and.returnValue( |
| 66 | + new Promise(() => {}) // Never resolves - simulates hanging |
| 67 | + ); |
| 68 | + spyOn(console, 'warn'); |
| 69 | + |
| 70 | + const result = await service.initializeCrossAppLogin(); |
| 71 | + |
| 72 | + expect(result).toBe(false); |
| 73 | + expect(console.warn).toHaveBeenCalledWith('Cross-app auth initialization failed:', 'Cross-app auth initialization timed out'); |
| 74 | + }); |
| 75 | + |
| 76 | + it('should handle initialization errors', async () => { |
| 77 | + const error = new Error('Network error'); |
| 78 | + mockCrossAppIntegration.initialize.and.returnValue(Promise.reject(error)); |
| 79 | + spyOn(console, 'warn'); |
| 80 | + |
| 81 | + const result = await service.initializeCrossAppLogin(); |
| 82 | + |
| 83 | + expect(result).toBe(false); |
| 84 | + expect(console.warn).toHaveBeenCalledWith('Cross-app auth initialization failed:', 'Network error'); |
| 85 | + }); |
| 86 | + |
| 87 | + it('should handle various error types', async () => { |
| 88 | + const customError = new Error('Custom initialization error'); |
| 89 | + mockCrossAppIntegration.initialize.and.returnValue(Promise.reject(customError)); |
| 90 | + spyOn(console, 'warn'); |
| 91 | + |
| 92 | + const result = await service.initializeCrossAppLogin(); |
| 93 | + |
| 94 | + expect(result).toBe(false); |
| 95 | + expect(console.warn).toHaveBeenCalledWith('Cross-app auth initialization failed:', 'Custom initialization error'); |
| 96 | + }); |
| 97 | + |
| 98 | + it('should handle silent login errors', async () => { |
| 99 | + mockCrossAppIntegration.initialize.and.returnValue(Promise.resolve()); |
| 100 | + mockCrossAppIntegration.processCrossAppAuthRequest.and.returnValue(Promise.reject(new Error('Silent login failed'))); |
| 101 | + spyOn(console, 'warn'); |
| 102 | + |
| 103 | + const result = await service.initializeCrossAppLogin(); |
| 104 | + |
| 105 | + expect(result).toBe(false); |
| 106 | + expect(console.warn).toHaveBeenCalledWith('Cross-app auth initialization failed:', 'Silent login failed'); |
| 107 | + }); |
| 108 | + }); |
| 109 | +}); |
0 commit comments