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 { configureTestBed } from '../../../../testing/unit-test-helper';
11 import { SettingsService } from '../../api/settings.service';
12 import { AuthStorageService } from '../../services/auth-storage.service';
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 }];
27 declarations: [PwdExpirationNotificationComponent, FakeComponent],
28 imports: [NgbAlertModule, HttpClientTestingModule, RouterTestingModule.withRoutes(routes)],
29 providers: [SettingsService, AuthStorageService]
32 describe('password expiration date has been set', () => {
34 authStorageService = TestBed.inject(AuthStorageService);
35 settingsService = TestBed.inject(SettingsService);
36 spyOn(authStorageService, 'getPwdExpirationDate').and.returnValue(1645488000);
37 spyOn(settingsService, 'getStandardSettings').and.returnValue(
39 user_pwd_expiration_warning_1: 10,
40 user_pwd_expiration_warning_2: 5,
41 user_pwd_expiration_span: 90
44 fixture = TestBed.createComponent(PwdExpirationNotificationComponent);
45 component = fixture.componentInstance;
46 fixture.detectChanges();
49 it('should create', () => {
51 expect(component).toBeTruthy();
54 it('should set warning levels', () => {
56 expect(component.pwdExpirationSettings.pwdExpirationWarning1).toBe(10);
57 expect(component.pwdExpirationSettings.pwdExpirationWarning2).toBe(5);
60 it('should calculate password expiration in days', () => {
61 const dateValue = Date;
62 spyOn(global, 'Date').and.callFake((date) => {
64 return new dateValue(date);
66 return new Date('2022-02-18T00:00:00.000Z');
70 expect(component['expirationDays']).toBe(4);
73 it('should set alert type warning correctly', () => {
74 const dateValue = Date;
75 spyOn(global, 'Date').and.callFake((date) => {
77 return new dateValue(date);
79 return new Date('2022-02-14T00:00:00.000Z');
83 expect(component['alertType']).toBe('warning');
86 it('should set alert type danger correctly', () => {
87 const dateValue = Date;
88 spyOn(global, 'Date').and.callFake((date) => {
90 return new dateValue(date);
92 return new Date('2022-02-18T00:00:00.000Z');
96 expect(component['alertType']).toBe('danger');
100 describe('password expiration date has not been set', () => {
102 authStorageService = TestBed.inject(AuthStorageService);
103 spyOn(authStorageService, 'getPwdExpirationDate').and.returnValue(null);
104 fixture = TestBed.createComponent(PwdExpirationNotificationComponent);
105 component = fixture.componentInstance;
106 fixture.detectChanges();
109 it('should calculate no expirationDays', () => {
110 component.ngOnInit();
111 expect(component['expirationDays']).toBeUndefined();