|
| 1 | +import { TestBed } from '@angular/core/testing'; |
| 2 | +import { AuthGuard } from './auth.guard'; |
| 3 | +import { Router, UrlTree } from '@angular/router'; |
| 4 | +import { AccountService } from '@shared/services/account/account.service'; |
| 5 | +import { ActivatedRouteSnapshot, RouterStateSnapshot } from '@angular/router'; |
| 6 | +import { AccountVO } from '@models/account-vo'; |
| 7 | +import { ArchiveVO } from '@models/index'; |
| 8 | + |
| 9 | +describe('AuthGuard', () => { |
| 10 | + let guard: AuthGuard; |
| 11 | + let accountServiceSpy: jasmine.SpyObj<AccountService>; |
| 12 | + let routerSpy: jasmine.SpyObj<Router>; |
| 13 | + |
| 14 | + beforeEach(() => { |
| 15 | + accountServiceSpy = jasmine.createSpyObj('AccountService', [ |
| 16 | + 'getAccount', |
| 17 | + 'hasOwnArchives', |
| 18 | + ]); |
| 19 | + |
| 20 | + routerSpy = jasmine.createSpyObj('Router', ['createUrlTree', 'parseUrl']); |
| 21 | + |
| 22 | + TestBed.configureTestingModule({ |
| 23 | + providers: [ |
| 24 | + AuthGuard, |
| 25 | + { provide: AccountService, useValue: accountServiceSpy }, |
| 26 | + { provide: Router, useValue: routerSpy }, |
| 27 | + ], |
| 28 | + }); |
| 29 | + |
| 30 | + guard = TestBed.inject(AuthGuard); |
| 31 | + }); |
| 32 | + |
| 33 | + function createMockRouteSnapshot( |
| 34 | + query: Record<string, string> = {}, |
| 35 | + ): ActivatedRouteSnapshot { |
| 36 | + return { |
| 37 | + queryParamMap: { |
| 38 | + get: (key: string) => query[key] ?? null, |
| 39 | + }, |
| 40 | + } as any; |
| 41 | + } |
| 42 | + |
| 43 | + it('should allow access if no account is found', () => { |
| 44 | + accountServiceSpy.getAccount.and.returnValue(null); |
| 45 | + const result = guard.canActivate(createMockRouteSnapshot(), { |
| 46 | + url: '/signup', |
| 47 | + } as RouterStateSnapshot); |
| 48 | + expect(result).toBeTrue(); |
| 49 | + }); |
| 50 | + |
| 51 | + it('should redirect to /app/dialog:archives/pending if on /signup with invite params', () => { |
| 52 | + accountServiceSpy.getAccount.and.returnValue( |
| 53 | + new AccountVO({ accountId: 1 }), |
| 54 | + ); |
| 55 | + |
| 56 | + const query = { |
| 57 | + inviteCode: 'xyz', |
| 58 | + fullName: 'Test User', |
| 59 | + primaryEmail: '[email protected]', |
| 60 | + }; |
| 61 | + |
| 62 | + const expectedTree = {} as UrlTree; |
| 63 | + routerSpy.createUrlTree.and.returnValue(expectedTree); |
| 64 | + |
| 65 | + const result = guard.canActivate(createMockRouteSnapshot(query), { |
| 66 | + url: '/signup', |
| 67 | + } as RouterStateSnapshot); |
| 68 | + |
| 69 | + expect(routerSpy.createUrlTree).toHaveBeenCalledWith([ |
| 70 | + '/app', |
| 71 | + { outlets: { primary: 'private', dialog: 'archives/pending' } }, |
| 72 | + ]); |
| 73 | + expect(result).toBe(expectedTree); |
| 74 | + }); |
| 75 | + |
| 76 | + it('should redirect to /app/private if account has own archives', async () => { |
| 77 | + accountServiceSpy.getAccount.and.returnValue( |
| 78 | + new AccountVO({ accountId: 1 }), |
| 79 | + ); |
| 80 | + accountServiceSpy.hasOwnArchives.and.returnValue(Promise.resolve(true)); |
| 81 | + |
| 82 | + const expectedUrl = {} as UrlTree; |
| 83 | + routerSpy.parseUrl.and.returnValue(expectedUrl); |
| 84 | + |
| 85 | + const result = await guard.canActivate(createMockRouteSnapshot(), { |
| 86 | + url: '/app/anything', |
| 87 | + } as RouterStateSnapshot); |
| 88 | + |
| 89 | + expect(accountServiceSpy.hasOwnArchives).toHaveBeenCalled(); |
| 90 | + expect(result).toBe(expectedUrl); |
| 91 | + expect(routerSpy.parseUrl).toHaveBeenCalledWith('/app/private'); |
| 92 | + }); |
| 93 | + |
| 94 | + it('should redirect to /app/onboarding if account has no own archives', async () => { |
| 95 | + accountServiceSpy.getAccount.and.returnValue( |
| 96 | + new AccountVO({ accountId: 1 }), |
| 97 | + ); |
| 98 | + accountServiceSpy.hasOwnArchives.and.returnValue(Promise.resolve(false)); |
| 99 | + |
| 100 | + const expectedUrl = {} as UrlTree; |
| 101 | + routerSpy.parseUrl.and.returnValue(expectedUrl); |
| 102 | + |
| 103 | + const result = await guard.canActivate(createMockRouteSnapshot(), { |
| 104 | + url: '/app/anything', |
| 105 | + } as RouterStateSnapshot); |
| 106 | + |
| 107 | + expect(accountServiceSpy.hasOwnArchives).toHaveBeenCalled(); |
| 108 | + expect(result).toBe(expectedUrl); |
| 109 | + expect(routerSpy.parseUrl).toHaveBeenCalledWith('/app/onboarding'); |
| 110 | + }); |
| 111 | +}); |
0 commit comments