diff --git a/src/app/core/guards/verified-org-auth.guard.spec.ts b/src/app/core/guards/verified-org-auth.guard.spec.ts index 4167ae8b3e..1347180416 100644 --- a/src/app/core/guards/verified-org-auth.guard.spec.ts +++ b/src/app/core/guards/verified-org-auth.guard.spec.ts @@ -1,16 +1,80 @@ import { TestBed } from '@angular/core/testing'; import { VerifiedOrgAuthGuard } from './verified-org-auth.guard'; +import { AuthService } from '../services/auth.service'; +import { ActivatedRoute, Router } from '@angular/router'; +import { NO_ERRORS_SCHEMA } from '@angular/core'; +import { apiEouRes, eouWithPendingDetails } from '../mock-data/extended-org-user.data'; +import { routerStateSnapshotData } from '../mock-data/router-state-snapshot.data'; -xdescribe('AuthGuard', () => { +describe('AuthGuard', () => { let guard: VerifiedOrgAuthGuard; + let authService: jasmine.SpyObj; + let router: jasmine.SpyObj; + let activatedRoute: jasmine.SpyObj; beforeEach(() => { - TestBed.configureTestingModule({}); + let routerSpy = jasmine.createSpyObj('Router', ['navigate']); + let authServiceSpy = jasmine.createSpyObj('AuthService', ['getEou']); + TestBed.configureTestingModule({ + schemas: [NO_ERRORS_SCHEMA], + providers: [ + { provide: Router, useValue: routerSpy }, + { provide: AuthService, useValue: authServiceSpy }, + { + provide: ActivatedRoute, + useValue: { + snapshot: { + data: { + url: '/enterprise/dashboard', + root: null, + }, + }, + }, + }, + ], + }).compileComponents(); guard = TestBed.inject(VerifiedOrgAuthGuard); + + authService = TestBed.inject(AuthService) as jasmine.SpyObj; + activatedRoute = TestBed.inject(ActivatedRoute) as jasmine.SpyObj; + router = TestBed.inject(Router) as jasmine.SpyObj; }); it('should be created', () => { expect(guard).toBeTruthy(); }); + + describe('canActivate(): ', () => { + it('should return true if eou is present', (done) => { + authService.getEou.and.resolveTo(apiEouRes); + const canActivate = guard.canActivate(activatedRoute.snapshot, routerStateSnapshotData) as Promise; + canActivate.then((res) => { + expect(authService.getEou).toHaveBeenCalledTimes(1); + expect(res).toBeTrue(); + done(); + }); + }); + + it('should return false if eou is not present', (done) => { + authService.getEou.and.resolveTo(null); + const canActivate = guard.canActivate(activatedRoute.snapshot, routerStateSnapshotData) as Promise; + canActivate.then((res) => { + expect(authService.getEou).toHaveBeenCalledTimes(1); + expect(res).toBeFalse(); + done(); + }); + }); + + it('should navigate to switch org if status is PENDING_DETAILS', (done) => { + authService.getEou.and.resolveTo(eouWithPendingDetails); + const canActivate = guard.canActivate(activatedRoute.snapshot, routerStateSnapshotData) as Promise; + canActivate.then((res) => { + expect(authService.getEou).toHaveBeenCalledTimes(1); + expect(res).toBeTrue(); + expect(router.navigate).toHaveBeenCalledWith(['/', 'auth', 'switch_org']); + done(); + }); + }); + }); }); diff --git a/src/app/core/mock-data/extended-org-user.data.ts b/src/app/core/mock-data/extended-org-user.data.ts index 2a907711d8..de92d5cd36 100644 --- a/src/app/core/mock-data/extended-org-user.data.ts +++ b/src/app/core/mock-data/extended-org-user.data.ts @@ -407,3 +407,11 @@ export const eouWithNoAttempts: ExtendedOrgUser = { mobile_verification_attempts_left: 0, }, }; + +export const eouWithPendingDetails: ExtendedOrgUser = { + ...apiEouRes, + ou: { + ...apiEouRes.ou, + status: 'PENDING_DETAILS', + }, +}; diff --git a/src/app/core/mock-data/router-state-snapshot.data.ts b/src/app/core/mock-data/router-state-snapshot.data.ts new file mode 100644 index 0000000000..ec90f79b8e --- /dev/null +++ b/src/app/core/mock-data/router-state-snapshot.data.ts @@ -0,0 +1 @@ +export const routerStateSnapshotData = { url: '/test', root: null };