Skip to content

Commit

Permalink
VIH-10632 UI test fixes (#2133
Browse files Browse the repository at this point in the history
  • Loading branch information
Shaed Parkar committed May 2, 2024
1 parent c9e8b7b commit 5cc9de7
Show file tree
Hide file tree
Showing 12 changed files with 373 additions and 84 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -349,3 +349,4 @@ TESTS.xml
Coverage/
**/SpecFlow/userid
.angular/
.nx/
4 changes: 2 additions & 2 deletions VideoWeb/VideoWeb/ClientApp/src/app/app-routing.module.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import { AdminGuard } from './security/admin.guard';
import { AuthGuard } from './security/auth.guard';
import { NavigatorComponent } from './home/navigator/navigator.component';
import { QuickLinksComponent } from './on-the-day/quick-links/quick-links.component';
import { AlreadyAuthenticatedGuard } from './security/guards/already-authenticated.guard';
import { AlreadyAuthenticatedQuickLinkGuard } from './security/guards/already-authenticated-quick-link.guard';

export const routes: Routes = [
{ path: '', redirectTo: `${pageUrls.Navigator}`, pathMatch: 'full' },
Expand All @@ -20,7 +20,7 @@ export const routes: Routes = [
{
path: `${pageUrls.QuickLinks}`,
component: QuickLinksComponent,
canActivate: [AlreadyAuthenticatedGuard],
canActivate: [AlreadyAuthenticatedQuickLinkGuard],
data: { title: 'Quick join' }
},
{ path: `${pageUrls.Navigator}`, component: NavigatorComponent, canActivate: [AuthGuard] },
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
import { TestBed, fakeAsync, tick } from '@angular/core/testing';
import { AlreadyAuthenticatedDom1Guard } from './already-authenticated-dom1.guard';
import { SecurityServiceProvider } from '../authentication/security-provider.service';
import { Subject, of } from 'rxjs';
import { Router } from '@angular/router';
import { ISecurityService } from '../authentication/security-service.interface';
import { pageUrls } from 'src/app/shared/page-url.constants';

describe('AlreadyAuthenticatedDom1Guard', () => {
let guard: AlreadyAuthenticatedDom1Guard;
let isAuthenticatedSubject: Subject<boolean>;
let securityServiceSpy: jasmine.SpyObj<ISecurityService>;
let securityServiceProviderSpy: jasmine.SpyObj<SecurityServiceProvider>;
let routerSpy: jasmine.SpyObj<Router>;

beforeEach(() => {
isAuthenticatedSubject = new Subject<boolean>();
securityServiceSpy = jasmine.createSpyObj<ISecurityService>('ISecurityService', ['isAuthenticated']);
securityServiceSpy.isAuthenticated.and.returnValue(isAuthenticatedSubject.asObservable());

securityServiceProviderSpy = jasmine.createSpyObj<SecurityServiceProvider>('SecurityServiceProvider', ['getSecurityService']);
securityServiceProviderSpy.getSecurityService.and.returnValue(securityServiceSpy);

routerSpy = jasmine.createSpyObj<Router>('Router', ['navigate']);

TestBed.configureTestingModule({
providers: [
{ provide: SecurityServiceProvider, useValue: securityServiceProviderSpy },
{ provide: Router, useValue: routerSpy }
]
});
guard = TestBed.inject(AlreadyAuthenticatedDom1Guard);
});

it('should return true if not authenticated', fakeAsync(() => {
// arrange
securityServiceSpy.isAuthenticated.and.returnValue(of(false));

// act
let result: boolean;
guard.canActivate().subscribe(res => (result = res));
tick();

// assert
expect(result).toBeTrue();
}));

it('should navigate to home page if already authenticated', fakeAsync(() => {
// arrange
securityServiceSpy.isAuthenticated.and.returnValue(of(true));

// act
guard.canActivate().subscribe();
tick();

// assert
expect(routerSpy.navigate).toHaveBeenCalledWith([pageUrls.Home]);
}));
});
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
import { Injectable } from '@angular/core';
import { Router } from '@angular/router';
import { Observable } from 'rxjs';
import { take, tap, map, timeout } from 'rxjs/operators';
import { pageUrls } from 'src/app/shared/page-url.constants';
import { SecurityServiceProvider } from '../authentication/security-provider.service';
import { IdpProviders } from '../idp-providers';

@Injectable({ providedIn: 'root' })
export class AlreadyAuthenticatedDom1Guard {
currentIdp: IdpProviders = IdpProviders.dom1;
constructor(
private securityServiceProvider: SecurityServiceProvider,
private router: Router
) {}

canActivate(): Observable<boolean> {
return this.securityServiceProvider
.getSecurityService(this.currentIdp)
.isAuthenticated(this.currentIdp)
.pipe(
timeout(30000),
take(1),
tap(authenticated => {
if (authenticated) {
this.router.navigate([pageUrls.Home]);
}
}),
map(authenticated => !authenticated)
);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
import { TestBed, fakeAsync, tick } from '@angular/core/testing';
import { SecurityServiceProvider } from '../authentication/security-provider.service';
import { Subject, of } from 'rxjs';
import { Router } from '@angular/router';
import { ISecurityService } from '../authentication/security-service.interface';
import { pageUrls } from 'src/app/shared/page-url.constants';
import { AlreadyAuthenticatedEjudGuard } from './already-authenticated-ejud.guard';

describe('AlreadyAuthenticatedDom1Guard', () => {
let guard: AlreadyAuthenticatedEjudGuard;
let isAuthenticatedSubject: Subject<boolean>;
let securityServiceSpy: jasmine.SpyObj<ISecurityService>;
let securityServiceProviderSpy: jasmine.SpyObj<SecurityServiceProvider>;
let routerSpy: jasmine.SpyObj<Router>;

beforeEach(() => {
isAuthenticatedSubject = new Subject<boolean>();
securityServiceSpy = jasmine.createSpyObj<ISecurityService>('ISecurityService', ['isAuthenticated']);
securityServiceSpy.isAuthenticated.and.returnValue(isAuthenticatedSubject.asObservable());

securityServiceProviderSpy = jasmine.createSpyObj<SecurityServiceProvider>('SecurityServiceProvider', ['getSecurityService']);
securityServiceProviderSpy.getSecurityService.and.returnValue(securityServiceSpy);

routerSpy = jasmine.createSpyObj<Router>('Router', ['navigate']);

TestBed.configureTestingModule({
providers: [
{ provide: SecurityServiceProvider, useValue: securityServiceProviderSpy },
{ provide: Router, useValue: routerSpy }
]
});
guard = TestBed.inject(AlreadyAuthenticatedEjudGuard);
});

it('should return true if not authenticated', fakeAsync(() => {
// arrange
securityServiceSpy.isAuthenticated.and.returnValue(of(false));

// act
let result: boolean;
guard.canActivate().subscribe(res => (result = res));
tick();

// assert
expect(result).toBeTrue();
}));

it('should navigate to home page if already authenticated', fakeAsync(() => {
// arrange
securityServiceSpy.isAuthenticated.and.returnValue(of(true));

// act
guard.canActivate().subscribe();
tick();

// assert
expect(routerSpy.navigate).toHaveBeenCalledWith([pageUrls.Home]);
}));
});
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
import { Injectable } from '@angular/core';
import { Router } from '@angular/router';
import { Observable } from 'rxjs';
import { take, tap, map, timeout } from 'rxjs/operators';
import { pageUrls } from 'src/app/shared/page-url.constants';
import { SecurityServiceProvider } from '../authentication/security-provider.service';
import { IdpProviders } from '../idp-providers';

@Injectable({ providedIn: 'root' })
export class AlreadyAuthenticatedEjudGuard {
currentIdp: IdpProviders = IdpProviders.ejud;
constructor(
private securityServiceProvider: SecurityServiceProvider,
private router: Router
) {}

canActivate(): Observable<boolean> {
return this.securityServiceProvider
.getSecurityService(this.currentIdp)
.isAuthenticated(this.currentIdp)
.pipe(
timeout(30000),
take(1),
tap(authenticated => {
if (authenticated) {
this.router.navigate([pageUrls.Home]);
}
}),
map(authenticated => !authenticated)
);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
import { TestBed, fakeAsync, tick } from '@angular/core/testing';
import { SecurityServiceProvider } from '../authentication/security-provider.service';
import { Subject, of } from 'rxjs';
import { Router } from '@angular/router';
import { ISecurityService } from '../authentication/security-service.interface';
import { pageUrls } from 'src/app/shared/page-url.constants';
import { AlreadyAuthenticatedQuickLinkGuard } from './already-authenticated-quick-link.guard';

describe('AlreadyAuthenticatedDom1Guard', () => {
let guard: AlreadyAuthenticatedQuickLinkGuard;
let isAuthenticatedSubject: Subject<boolean>;
let securityServiceSpy: jasmine.SpyObj<ISecurityService>;
let securityServiceProviderSpy: jasmine.SpyObj<SecurityServiceProvider>;
let routerSpy: jasmine.SpyObj<Router>;

beforeEach(() => {
isAuthenticatedSubject = new Subject<boolean>();
securityServiceSpy = jasmine.createSpyObj<ISecurityService>('ISecurityService', ['isAuthenticated']);
securityServiceSpy.isAuthenticated.and.returnValue(isAuthenticatedSubject.asObservable());

securityServiceProviderSpy = jasmine.createSpyObj<SecurityServiceProvider>('SecurityServiceProvider', ['getSecurityService']);
securityServiceProviderSpy.getSecurityService.and.returnValue(securityServiceSpy);

routerSpy = jasmine.createSpyObj<Router>('Router', ['navigate']);

TestBed.configureTestingModule({
providers: [
{ provide: SecurityServiceProvider, useValue: securityServiceProviderSpy },
{ provide: Router, useValue: routerSpy }
]
});
guard = TestBed.inject(AlreadyAuthenticatedQuickLinkGuard);
});

it('should return true if not authenticated', fakeAsync(() => {
// arrange
securityServiceSpy.isAuthenticated.and.returnValue(of(false));

// act
let result: boolean;
guard.canActivate().subscribe(res => (result = res));
tick();

// assert
expect(result).toBeTrue();
}));

it('should navigate to home page if already authenticated', fakeAsync(() => {
// arrange
securityServiceSpy.isAuthenticated.and.returnValue(of(true));

// act
guard.canActivate().subscribe();
tick();

// assert
expect(routerSpy.navigate).toHaveBeenCalledWith([pageUrls.Home]);
}));
});
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
import { Injectable } from '@angular/core';
import { Router } from '@angular/router';
import { Observable } from 'rxjs';
import { take, tap, map, timeout } from 'rxjs/operators';
import { pageUrls } from 'src/app/shared/page-url.constants';
import { SecurityServiceProvider } from '../authentication/security-provider.service';
import { IdpProviders } from '../idp-providers';

@Injectable({ providedIn: 'root' })
export class AlreadyAuthenticatedQuickLinkGuard {
currentIdp: IdpProviders = IdpProviders.quickLink;
constructor(
private securityServiceProvider: SecurityServiceProvider,
private router: Router
) {}

canActivate(): Observable<boolean> {
return this.securityServiceProvider
.getSecurityService(this.currentIdp)
.isAuthenticated(this.currentIdp)
.pipe(
timeout(30000),
take(1),
tap(authenticated => {
if (authenticated) {
this.router.navigate([pageUrls.Home]);
}
}),
map(authenticated => !authenticated)
);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
import { TestBed, fakeAsync, tick } from '@angular/core/testing';
import { SecurityServiceProvider } from '../authentication/security-provider.service';
import { Subject, of } from 'rxjs';
import { Router } from '@angular/router';
import { ISecurityService } from '../authentication/security-service.interface';
import { pageUrls } from 'src/app/shared/page-url.constants';
import { AlreadyAuthenticatedVhGuard } from './already-authenticated-vh.guard';

describe('AlreadyAuthenticatedDom1Guard', () => {
let guard: AlreadyAuthenticatedVhGuard;
let isAuthenticatedSubject: Subject<boolean>;
let securityServiceSpy: jasmine.SpyObj<ISecurityService>;
let securityServiceProviderSpy: jasmine.SpyObj<SecurityServiceProvider>;
let routerSpy: jasmine.SpyObj<Router>;

beforeEach(() => {
isAuthenticatedSubject = new Subject<boolean>();
securityServiceSpy = jasmine.createSpyObj<ISecurityService>('ISecurityService', ['isAuthenticated']);
securityServiceSpy.isAuthenticated.and.returnValue(isAuthenticatedSubject.asObservable());

securityServiceProviderSpy = jasmine.createSpyObj<SecurityServiceProvider>('SecurityServiceProvider', ['getSecurityService']);
securityServiceProviderSpy.getSecurityService.and.returnValue(securityServiceSpy);

routerSpy = jasmine.createSpyObj<Router>('Router', ['navigate']);

TestBed.configureTestingModule({
providers: [
{ provide: SecurityServiceProvider, useValue: securityServiceProviderSpy },
{ provide: Router, useValue: routerSpy }
]
});
guard = TestBed.inject(AlreadyAuthenticatedVhGuard);
});

it('should return true if not authenticated', fakeAsync(() => {
// arrange
securityServiceSpy.isAuthenticated.and.returnValue(of(false));

// act
let result: boolean;
guard.canActivate().subscribe(res => (result = res));
tick();

// assert
expect(result).toBeTrue();
}));

it('should navigate to home page if already authenticated', fakeAsync(() => {
// arrange
securityServiceSpy.isAuthenticated.and.returnValue(of(true));

// act
guard.canActivate().subscribe();
tick();

// assert
expect(routerSpy.navigate).toHaveBeenCalledWith([pageUrls.Home]);
}));
});
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
import { Injectable } from '@angular/core';
import { Router } from '@angular/router';
import { Observable } from 'rxjs';
import { take, tap, map, timeout } from 'rxjs/operators';
import { pageUrls } from 'src/app/shared/page-url.constants';
import { SecurityServiceProvider } from '../authentication/security-provider.service';
import { IdpProviders } from '../idp-providers';

@Injectable({ providedIn: 'root' })
export class AlreadyAuthenticatedVhGuard {
currentIdp: IdpProviders = IdpProviders.vhaad;
constructor(
private securityServiceProvider: SecurityServiceProvider,
private router: Router
) {}

canActivate(): Observable<boolean> {
return this.securityServiceProvider
.getSecurityService(this.currentIdp)
.isAuthenticated(this.currentIdp)
.pipe(
timeout(30000),
take(1),
tap(authenticated => {
if (authenticated) {
this.router.navigate([pageUrls.Home]);
}
}),
map(authenticated => !authenticated)
);
}
}
Loading

0 comments on commit 5cc9de7

Please sign in to comment.