|
| 1 | +import { TestBed } from '@angular/core/testing'; |
| 2 | +import { |
| 3 | + Store, |
| 4 | + StoreModule, |
| 5 | +} from '@ngrx/store'; |
| 6 | +import { |
| 7 | + MockStore, |
| 8 | + provideMockStore, |
| 9 | +} from '@ngrx/store/testing'; |
| 10 | + |
| 11 | +import { storeModuleConfig } from '../../app.reducer'; |
| 12 | +import { AuthMethodTypeComponent } from '../../shared/log-in/methods/auth-methods.type'; |
| 13 | +import { authReducer } from './auth.reducer'; |
| 14 | +import { AuthMethodsService } from './auth-methods.service'; |
| 15 | +import { AuthMethod } from './models/auth.method'; |
| 16 | +import { AuthMethodType } from './models/auth.method-type'; |
| 17 | + |
| 18 | +describe('AuthMethodsService', () => { |
| 19 | + let service: AuthMethodsService; |
| 20 | + let store: MockStore; |
| 21 | + let mockAuthMethods: Map<AuthMethodType, AuthMethodTypeComponent>; |
| 22 | + let mockAuthMethodsArray: AuthMethod[] = [ |
| 23 | + { id: 'password', authMethodType: AuthMethodType.Password, position: 2 } as AuthMethod, |
| 24 | + { id: 'shibboleth', authMethodType: AuthMethodType.Shibboleth, position: 1 } as AuthMethod, |
| 25 | + { id: 'oidc', authMethodType: AuthMethodType.Oidc, position: 3 } as AuthMethod, |
| 26 | + { id: 'ip', authMethodType: AuthMethodType.Ip, position: 4 } as AuthMethod, |
| 27 | + ]; |
| 28 | + |
| 29 | + const initialState = { |
| 30 | + core: { |
| 31 | + auth: { |
| 32 | + authMethods: mockAuthMethodsArray, |
| 33 | + }, |
| 34 | + }, |
| 35 | + }; |
| 36 | + |
| 37 | + beforeEach(() => { |
| 38 | + TestBed.configureTestingModule({ |
| 39 | + imports: [ |
| 40 | + StoreModule.forRoot(authReducer, storeModuleConfig), |
| 41 | + ], |
| 42 | + providers: [ |
| 43 | + AuthMethodsService, |
| 44 | + provideMockStore({ initialState }), |
| 45 | + ], |
| 46 | + }); |
| 47 | + |
| 48 | + service = TestBed.inject(AuthMethodsService); |
| 49 | + store = TestBed.inject(Store) as MockStore; |
| 50 | + |
| 51 | + // Setup mock auth methods map |
| 52 | + mockAuthMethods = new Map<AuthMethodType, AuthMethodTypeComponent>(); |
| 53 | + mockAuthMethods.set(AuthMethodType.Password, {} as AuthMethodTypeComponent); |
| 54 | + mockAuthMethods.set(AuthMethodType.Shibboleth, {} as AuthMethodTypeComponent); |
| 55 | + mockAuthMethods.set(AuthMethodType.Oidc, {} as AuthMethodTypeComponent); |
| 56 | + mockAuthMethods.set(AuthMethodType.Ip, {} as AuthMethodTypeComponent); |
| 57 | + |
| 58 | + }); |
| 59 | + |
| 60 | + it('should be created', () => { |
| 61 | + expect(service).toBeTruthy(); |
| 62 | + }); |
| 63 | + |
| 64 | + describe('getAuthMethods', () => { |
| 65 | + it('should return auth methods sorted by position', () => { |
| 66 | + |
| 67 | + // Expected result after sorting and filtering IP auth |
| 68 | + const expected = [ |
| 69 | + { id: 'shibboleth', authMethodType: AuthMethodType.Shibboleth, position: 1 }, |
| 70 | + { id: 'password', authMethodType: AuthMethodType.Password, position: 2 }, |
| 71 | + { id: 'oidc', authMethodType: AuthMethodType.Oidc, position: 3 }, |
| 72 | + ]; |
| 73 | + |
| 74 | + service.getAuthMethods(mockAuthMethods).subscribe(result => { |
| 75 | + expect(result.length).toBe(3); |
| 76 | + expect(result).toEqual(expected); |
| 77 | + }); |
| 78 | + }); |
| 79 | + |
| 80 | + it('should exclude specified auth method type', () => { |
| 81 | + |
| 82 | + // Expected result after excluding Password auth and filtering IP auth |
| 83 | + const expected = [ |
| 84 | + { id: 'shibboleth', authMethodType: AuthMethodType.Shibboleth, position: 1 }, |
| 85 | + { id: 'oidc', authMethodType: AuthMethodType.Oidc, position: 3 }, |
| 86 | + ]; |
| 87 | + |
| 88 | + |
| 89 | + service.getAuthMethods(mockAuthMethods, AuthMethodType.Password).subscribe(result => { |
| 90 | + expect(result.length).toBe(2); |
| 91 | + expect(result).toEqual(expected); |
| 92 | + }); |
| 93 | + }); |
| 94 | + |
| 95 | + it('should always filter out IP authentication method', () => { |
| 96 | + |
| 97 | + // Add IP auth to the mock methods map |
| 98 | + mockAuthMethods.set(AuthMethodType.Ip, {} as AuthMethodTypeComponent); |
| 99 | + |
| 100 | + |
| 101 | + service.getAuthMethods(mockAuthMethods).subscribe(result => { |
| 102 | + expect(result.length).toBe(3); |
| 103 | + expect(result.find(method => method.authMethodType === AuthMethodType.Ip)).toBeUndefined(); |
| 104 | + }); |
| 105 | + }); |
| 106 | + |
| 107 | + it('should handle empty auth methods array', () => { |
| 108 | + const authMethods = new Map<AuthMethodType, AuthMethodTypeComponent>(); |
| 109 | + |
| 110 | + |
| 111 | + service.getAuthMethods(authMethods).subscribe(result => { |
| 112 | + expect(result.length).toBe(0); |
| 113 | + expect(result).toEqual([]); |
| 114 | + }); |
| 115 | + }); |
| 116 | + |
| 117 | + it('should handle duplicate auth method types and keep only unique ones', () => { |
| 118 | + // Arrange |
| 119 | + const duplicateMethodsArray = [ |
| 120 | + ...mockAuthMethodsArray, |
| 121 | + { id: 'password2', authMethodType: AuthMethodType.Password, position: 5 } as AuthMethod, |
| 122 | + ]; |
| 123 | + |
| 124 | + |
| 125 | + service.getAuthMethods(mockAuthMethods).subscribe(result => { |
| 126 | + expect(result.length).toBe(3); |
| 127 | + // Check that we only have one Password auth method |
| 128 | + const passwordMethods = result.filter(method => method.authMethodType === AuthMethodType.Password); |
| 129 | + expect(passwordMethods.length).toBe(1); |
| 130 | + }); |
| 131 | + }); |
| 132 | + }); |
| 133 | +}); |
0 commit comments