|
1 | 1 | import { TestBed } from '@angular/core/testing'; |
2 | | -import { CanActivateFn } from '@angular/router'; |
| 2 | +import { TranslateModule } from '@ngx-translate/core'; |
| 3 | +import { RouterTestingModule } from '@angular/router/testing'; |
| 4 | +import { HttpClientTestingModule } from '@angular/common/http/testing'; |
| 5 | +import { ActivatedRouteSnapshot, Router, RouterStateSnapshot } from '@angular/router'; |
| 6 | +import * as moment from 'moment'; |
| 7 | +import { LocalStorageService } from '../services/local-storage.service'; |
| 8 | +import { AuthGuard } from './auth.guard'; |
3 | 9 |
|
4 | | -import { authGuard } from './auth.guard'; |
| 10 | +describe('AuthGuard', () => { |
| 11 | + let guard: AuthGuard; |
| 12 | + let localStorageSpy: jasmine.SpyObj<LocalStorageService>; |
| 13 | + let routerSpy: jasmine.SpyObj<Router>; |
5 | 14 |
|
6 | | -describe('authGuard', () => { |
7 | | - const executeGuard: CanActivateFn = (...guardParameters) => |
8 | | - TestBed.runInInjectionContext(() => authGuard(...guardParameters)); |
| 15 | + const routeWithRoles = (roles: string[]): ActivatedRouteSnapshot => |
| 16 | + ({ data: { roles } } as unknown as ActivatedRouteSnapshot); |
| 17 | + |
| 18 | + const state = {} as RouterStateSnapshot; |
9 | 19 |
|
10 | 20 | beforeEach(() => { |
11 | | - TestBed.configureTestingModule({}); |
| 21 | + localStorageSpy = jasmine.createSpyObj<LocalStorageService>('LocalStorageService', ['getObject']); |
| 22 | + routerSpy = jasmine.createSpyObj<Router>('Router', ['navigate']); |
| 23 | + |
| 24 | + TestBed.configureTestingModule({ |
| 25 | + imports: [HttpClientTestingModule, RouterTestingModule, TranslateModule.forRoot()], |
| 26 | + providers: [ |
| 27 | + AuthGuard, |
| 28 | + { provide: LocalStorageService, useValue: localStorageSpy }, |
| 29 | + { provide: Router, useValue: routerSpy }, |
| 30 | + ], |
| 31 | + }); |
| 32 | + |
| 33 | + guard = TestBed.inject(AuthGuard); |
12 | 34 | }); |
13 | 35 |
|
14 | 36 | it('should be created', () => { |
15 | | - expect(executeGuard).toBeTruthy(); |
| 37 | + expect(guard).toBeTruthy(); |
| 38 | + }); |
| 39 | + |
| 40 | + it('should redirect to dashboard when login info is empty', () => { |
| 41 | + localStorageSpy.getObject.and.returnValue({} as object); |
| 42 | + |
| 43 | + const canActivate = guard.canActivate(routeWithRoles([]), state); |
| 44 | + |
| 45 | + expect(canActivate).toBeFalse(); |
| 46 | + expect(routerSpy.navigate).toHaveBeenCalledWith(['/dashboard']); |
| 47 | + }); |
| 48 | + |
| 49 | + it('should allow access for a valid individual with a required role', () => { |
| 50 | + localStorageSpy.getObject.and.returnValue({ |
| 51 | + expire: moment().unix() + 300, |
| 52 | + id: 'user-1', |
| 53 | + logged_as: 'user-1', |
| 54 | + roles: [{ name: 'Seller' }], |
| 55 | + organizations: [], |
| 56 | + } as object); |
| 57 | + |
| 58 | + const canActivate = guard.canActivate(routeWithRoles(['seller']), state); |
| 59 | + |
| 60 | + expect(canActivate).toBeTrue(); |
| 61 | + expect(routerSpy.navigate).not.toHaveBeenCalled(); |
| 62 | + }); |
| 63 | + |
| 64 | + it('should deny access when required roles are missing', () => { |
| 65 | + localStorageSpy.getObject.and.returnValue({ |
| 66 | + expire: moment().unix() + 300, |
| 67 | + id: 'user-1', |
| 68 | + logged_as: 'user-1', |
| 69 | + roles: [{ name: 'Buyer' }], |
| 70 | + organizations: [], |
| 71 | + } as object); |
| 72 | + |
| 73 | + const canActivate = guard.canActivate(routeWithRoles(['seller']), state); |
| 74 | + |
| 75 | + expect(canActivate).toBeFalse(); |
| 76 | + expect(routerSpy.navigate).toHaveBeenCalledWith(['/dashboard']); |
16 | 77 | }); |
17 | 78 | }); |
0 commit comments