]>
Commit | Line | Data |
---|---|---|
9f95a23c TL |
1 | import { HttpClientTestingModule } from '@angular/common/http/testing'; |
2 | import { Component } from '@angular/core'; | |
3 | import { ComponentFixture, TestBed } from '@angular/core/testing'; | |
4 | import { Routes } from '@angular/router'; | |
5 | import { RouterTestingModule } from '@angular/router/testing'; | |
6 | ||
f67539c2 | 7 | import { NgbAlertModule } from '@ng-bootstrap/ng-bootstrap'; |
9f95a23c TL |
8 | import { of as observableOf } from 'rxjs'; |
9 | ||
f67539c2 TL |
10 | import { SettingsService } from '~/app/shared/api/settings.service'; |
11 | import { AuthStorageService } from '~/app/shared/services/auth-storage.service'; | |
12 | import { configureTestBed } from '~/testing/unit-test-helper'; | |
9f95a23c TL |
13 | import { PwdExpirationNotificationComponent } from './pwd-expiration-notification.component'; |
14 | ||
15 | describe('PwdExpirationNotificationComponent', () => { | |
16 | let component: PwdExpirationNotificationComponent; | |
17 | let fixture: ComponentFixture<PwdExpirationNotificationComponent>; | |
18 | let settingsService: SettingsService; | |
19 | let authStorageService: AuthStorageService; | |
20 | ||
21 | @Component({ selector: 'cd-fake', template: '' }) | |
22 | class FakeComponent {} | |
23 | ||
24 | const routes: Routes = [{ path: 'login', component: FakeComponent }]; | |
25 | ||
f6b5b4d7 TL |
26 | const spyOnDate = (fakeDate: string) => { |
27 | const dateValue = Date; | |
28 | spyOn(global, 'Date').and.callFake((date) => new dateValue(date ? date : fakeDate)); | |
29 | }; | |
30 | ||
9f95a23c TL |
31 | configureTestBed({ |
32 | declarations: [PwdExpirationNotificationComponent, FakeComponent], | |
f67539c2 | 33 | imports: [NgbAlertModule, HttpClientTestingModule, RouterTestingModule.withRoutes(routes)], |
9f95a23c TL |
34 | providers: [SettingsService, AuthStorageService] |
35 | }); | |
36 | ||
37 | describe('password expiration date has been set', () => { | |
38 | beforeEach(() => { | |
f67539c2 TL |
39 | authStorageService = TestBed.inject(AuthStorageService); |
40 | settingsService = TestBed.inject(SettingsService); | |
9f95a23c TL |
41 | spyOn(authStorageService, 'getPwdExpirationDate').and.returnValue(1645488000); |
42 | spyOn(settingsService, 'getStandardSettings').and.returnValue( | |
43 | observableOf({ | |
44 | user_pwd_expiration_warning_1: 10, | |
45 | user_pwd_expiration_warning_2: 5, | |
46 | user_pwd_expiration_span: 90 | |
47 | }) | |
48 | ); | |
49 | fixture = TestBed.createComponent(PwdExpirationNotificationComponent); | |
50 | component = fixture.componentInstance; | |
51 | fixture.detectChanges(); | |
52 | }); | |
53 | ||
54 | it('should create', () => { | |
55 | component.ngOnInit(); | |
56 | expect(component).toBeTruthy(); | |
57 | }); | |
58 | ||
59 | it('should set warning levels', () => { | |
60 | component.ngOnInit(); | |
61 | expect(component.pwdExpirationSettings.pwdExpirationWarning1).toBe(10); | |
62 | expect(component.pwdExpirationSettings.pwdExpirationWarning2).toBe(5); | |
63 | }); | |
64 | ||
65 | it('should calculate password expiration in days', () => { | |
f6b5b4d7 | 66 | spyOnDate('2022-02-18T00:00:00.000Z'); |
9f95a23c TL |
67 | component.ngOnInit(); |
68 | expect(component['expirationDays']).toBe(4); | |
69 | }); | |
70 | ||
71 | it('should set alert type warning correctly', () => { | |
f6b5b4d7 | 72 | spyOnDate('2022-02-14T00:00:00.000Z'); |
9f95a23c TL |
73 | component.ngOnInit(); |
74 | expect(component['alertType']).toBe('warning'); | |
f6b5b4d7 | 75 | expect(component.displayNotification).toBeTruthy(); |
9f95a23c TL |
76 | }); |
77 | ||
78 | it('should set alert type danger correctly', () => { | |
f6b5b4d7 | 79 | spyOnDate('2022-02-18T00:00:00.000Z'); |
9f95a23c TL |
80 | component.ngOnInit(); |
81 | expect(component['alertType']).toBe('danger'); | |
f6b5b4d7 TL |
82 | expect(component.displayNotification).toBeTruthy(); |
83 | }); | |
84 | ||
85 | it('should not display if date is far', () => { | |
86 | spyOnDate('2022-01-01T00:00:00.000Z'); | |
87 | component.ngOnInit(); | |
88 | expect(component.displayNotification).toBeFalsy(); | |
9f95a23c TL |
89 | }); |
90 | }); | |
91 | ||
92 | describe('password expiration date has not been set', () => { | |
93 | beforeEach(() => { | |
f67539c2 | 94 | authStorageService = TestBed.inject(AuthStorageService); |
9f95a23c TL |
95 | spyOn(authStorageService, 'getPwdExpirationDate').and.returnValue(null); |
96 | fixture = TestBed.createComponent(PwdExpirationNotificationComponent); | |
97 | component = fixture.componentInstance; | |
98 | fixture.detectChanges(); | |
99 | }); | |
100 | ||
101 | it('should calculate no expirationDays', () => { | |
102 | component.ngOnInit(); | |
103 | expect(component['expirationDays']).toBeUndefined(); | |
104 | }); | |
105 | }); | |
106 | }); |