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';
7 import { NgbAlertModule } from '@ng-bootstrap/ng-bootstrap';
8 import { of as observableOf } from 'rxjs';
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';
13 import { PwdExpirationNotificationComponent } from './pwd-expiration-notification.component';
15 describe('PwdExpirationNotificationComponent', () => {
16 let component: PwdExpirationNotificationComponent;
17 let fixture: ComponentFixture<PwdExpirationNotificationComponent>;
18 let settingsService: SettingsService;
19 let authStorageService: AuthStorageService;
21 @Component({ selector: 'cd-fake', template: '' })
22 class FakeComponent {}
24 const routes: Routes = [{ path: 'login', component: FakeComponent }];
26 const spyOnDate = (fakeDate: string) => {
27 const dateValue = Date;
28 spyOn(global, 'Date').and.callFake((date) => new dateValue(date ? date : fakeDate));
32 declarations: [PwdExpirationNotificationComponent, FakeComponent],
33 imports: [NgbAlertModule, HttpClientTestingModule, RouterTestingModule.withRoutes(routes)],
34 providers: [SettingsService, AuthStorageService]
37 describe('password expiration date has been set', () => {
39 authStorageService = TestBed.inject(AuthStorageService);
40 settingsService = TestBed.inject(SettingsService);
41 spyOn(authStorageService, 'getPwdExpirationDate').and.returnValue(1645488000);
42 spyOn(settingsService, 'getStandardSettings').and.returnValue(
44 user_pwd_expiration_warning_1: 10,
45 user_pwd_expiration_warning_2: 5,
46 user_pwd_expiration_span: 90
49 fixture = TestBed.createComponent(PwdExpirationNotificationComponent);
50 component = fixture.componentInstance;
51 fixture.detectChanges();
54 it('should create', () => {
56 expect(component).toBeTruthy();
59 it('should set warning levels', () => {
61 expect(component.pwdExpirationSettings.pwdExpirationWarning1).toBe(10);
62 expect(component.pwdExpirationSettings.pwdExpirationWarning2).toBe(5);
65 it('should calculate password expiration in days', () => {
66 spyOnDate('2022-02-18T00:00:00.000Z');
68 expect(component['expirationDays']).toBe(4);
71 it('should set alert type warning correctly', () => {
72 spyOnDate('2022-02-14T00:00:00.000Z');
74 expect(component['alertType']).toBe('warning');
75 expect(component.displayNotification).toBeTruthy();
78 it('should set alert type danger correctly', () => {
79 spyOnDate('2022-02-18T00:00:00.000Z');
81 expect(component['alertType']).toBe('danger');
82 expect(component.displayNotification).toBeTruthy();
85 it('should not display if date is far', () => {
86 spyOnDate('2022-01-01T00:00:00.000Z');
88 expect(component.displayNotification).toBeFalsy();
92 describe('password expiration date has not been set', () => {
94 authStorageService = TestBed.inject(AuthStorageService);
95 spyOn(authStorageService, 'getPwdExpirationDate').and.returnValue(null);
96 fixture = TestBed.createComponent(PwdExpirationNotificationComponent);
97 component = fixture.componentInstance;
98 fixture.detectChanges();
101 it('should calculate no expirationDays', () => {
102 component.ngOnInit();
103 expect(component['expirationDays']).toBeUndefined();