Skip to content

Commit

Permalink
test: 100% coverage of verified org auth guard (#2430)
Browse files Browse the repository at this point in the history
* test: 100% coverage of verified org auth guard

* minor
  • Loading branch information
suyashpatil78 authored Sep 21, 2023
1 parent 3fa65aa commit 9ee8b9a
Show file tree
Hide file tree
Showing 3 changed files with 75 additions and 2 deletions.
68 changes: 66 additions & 2 deletions src/app/core/guards/verified-org-auth.guard.spec.ts
Original file line number Diff line number Diff line change
@@ -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<AuthService>;
let router: jasmine.SpyObj<Router>;
let activatedRoute: jasmine.SpyObj<ActivatedRoute>;

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<AuthService>;
activatedRoute = TestBed.inject(ActivatedRoute) as jasmine.SpyObj<ActivatedRoute>;
router = TestBed.inject(Router) as jasmine.SpyObj<Router>;
});

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<boolean>;
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<boolean>;
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<boolean>;
canActivate.then((res) => {
expect(authService.getEou).toHaveBeenCalledTimes(1);
expect(res).toBeTrue();
expect(router.navigate).toHaveBeenCalledWith(['/', 'auth', 'switch_org']);
done();
});
});
});
});
8 changes: 8 additions & 0 deletions src/app/core/mock-data/extended-org-user.data.ts
Original file line number Diff line number Diff line change
Expand Up @@ -407,3 +407,11 @@ export const eouWithNoAttempts: ExtendedOrgUser = {
mobile_verification_attempts_left: 0,
},
};

export const eouWithPendingDetails: ExtendedOrgUser = {
...apiEouRes,
ou: {
...apiEouRes.ou,
status: 'PENDING_DETAILS',
},
};
1 change: 1 addition & 0 deletions src/app/core/mock-data/router-state-snapshot.data.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export const routerStateSnapshotData = { url: '/test', root: null };

0 comments on commit 9ee8b9a

Please sign in to comment.