diff options
Diffstat (limited to 'src/pybind/mgr/dashboard/frontend/src/app/shared/components/pwd-expiration-notification/pwd-expiration-notification.component.spec.ts')
-rw-r--r-- | src/pybind/mgr/dashboard/frontend/src/app/shared/components/pwd-expiration-notification/pwd-expiration-notification.component.spec.ts | 107 |
1 files changed, 107 insertions, 0 deletions
diff --git a/src/pybind/mgr/dashboard/frontend/src/app/shared/components/pwd-expiration-notification/pwd-expiration-notification.component.spec.ts b/src/pybind/mgr/dashboard/frontend/src/app/shared/components/pwd-expiration-notification/pwd-expiration-notification.component.spec.ts new file mode 100644 index 000000000..597f5bab3 --- /dev/null +++ b/src/pybind/mgr/dashboard/frontend/src/app/shared/components/pwd-expiration-notification/pwd-expiration-notification.component.spec.ts @@ -0,0 +1,107 @@ +import { HttpClientTestingModule } from '@angular/common/http/testing'; +import { Component } from '@angular/core'; +import { ComponentFixture, TestBed } from '@angular/core/testing'; +import { Routes } from '@angular/router'; +import { RouterTestingModule } from '@angular/router/testing'; + +import { NgbAlertModule } from '@ng-bootstrap/ng-bootstrap'; +import { of as observableOf } from 'rxjs'; + +import { SettingsService } from '~/app/shared/api/settings.service'; +import { AlertPanelComponent } from '~/app/shared/components/alert-panel/alert-panel.component'; +import { AuthStorageService } from '~/app/shared/services/auth-storage.service'; +import { configureTestBed } from '~/testing/unit-test-helper'; +import { PwdExpirationNotificationComponent } from './pwd-expiration-notification.component'; + +describe('PwdExpirationNotificationComponent', () => { + let component: PwdExpirationNotificationComponent; + let fixture: ComponentFixture<PwdExpirationNotificationComponent>; + let settingsService: SettingsService; + let authStorageService: AuthStorageService; + + @Component({ selector: 'cd-fake', template: '' }) + class FakeComponent {} + + const routes: Routes = [{ path: 'login', component: FakeComponent }]; + + const spyOnDate = (fakeDate: string) => { + const dateValue = Date; + spyOn(global, 'Date').and.callFake((date) => new dateValue(date ? date : fakeDate)); + }; + + configureTestBed({ + declarations: [PwdExpirationNotificationComponent, FakeComponent, AlertPanelComponent], + imports: [NgbAlertModule, HttpClientTestingModule, RouterTestingModule.withRoutes(routes)], + providers: [SettingsService, AuthStorageService] + }); + + describe('password expiration date has been set', () => { + beforeEach(() => { + authStorageService = TestBed.inject(AuthStorageService); + settingsService = TestBed.inject(SettingsService); + spyOn(authStorageService, 'getPwdExpirationDate').and.returnValue(1645488000); + spyOn(settingsService, 'getStandardSettings').and.returnValue( + observableOf({ + user_pwd_expiration_warning_1: 10, + user_pwd_expiration_warning_2: 5, + user_pwd_expiration_span: 90 + }) + ); + fixture = TestBed.createComponent(PwdExpirationNotificationComponent); + component = fixture.componentInstance; + fixture.detectChanges(); + }); + + it('should create', () => { + component.ngOnInit(); + expect(component).toBeTruthy(); + }); + + it('should set warning levels', () => { + component.ngOnInit(); + expect(component.pwdExpirationSettings.pwdExpirationWarning1).toBe(10); + expect(component.pwdExpirationSettings.pwdExpirationWarning2).toBe(5); + }); + + it('should calculate password expiration in days', () => { + spyOnDate('2022-02-18T00:00:00.000Z'); + component.ngOnInit(); + expect(component['expirationDays']).toBe(4); + }); + + it('should set alert type warning correctly', () => { + spyOnDate('2022-02-14T00:00:00.000Z'); + component.ngOnInit(); + expect(component['alertType']).toBe('warning'); + expect(component.displayNotification).toBeTruthy(); + }); + + it('should set alert type danger correctly', () => { + spyOnDate('2022-02-18T00:00:00.000Z'); + component.ngOnInit(); + expect(component['alertType']).toBe('danger'); + expect(component.displayNotification).toBeTruthy(); + }); + + it('should not display if date is far', () => { + spyOnDate('2022-01-01T00:00:00.000Z'); + component.ngOnInit(); + expect(component.displayNotification).toBeFalsy(); + }); + }); + + describe('password expiration date has not been set', () => { + beforeEach(() => { + authStorageService = TestBed.inject(AuthStorageService); + spyOn(authStorageService, 'getPwdExpirationDate').and.returnValue(null); + fixture = TestBed.createComponent(PwdExpirationNotificationComponent); + component = fixture.componentInstance; + fixture.detectChanges(); + }); + + it('should calculate no expirationDays', () => { + component.ngOnInit(); + expect(component['expirationDays']).toBeUndefined(); + }); + }); +}); |