|
| 1 | +import { TestBed, waitForAsync } from '@angular/core/testing'; |
| 2 | +import { Router, RouterStateSnapshot } from '@angular/router'; |
| 3 | +import { RouterTestingModule } from '@angular/router/testing'; |
| 4 | +import { of } from 'rxjs'; |
| 5 | +import { AuthStateService } from '../authState/auth-state.service'; |
| 6 | +import { AuthStateServiceMock } from '../authState/auth-state.service-mock'; |
| 7 | +import { CheckAuthService } from '../check-auth.service'; |
| 8 | +import { CheckAuthServiceMock } from '../check-auth.service-mock'; |
| 9 | +import { LoginService } from '../login/login.service'; |
| 10 | +import { LoginServiceMock } from '../login/login.service-mock'; |
| 11 | +import { AutoLoginService } from './auto-login-service'; |
| 12 | +import { AutoLoginGuard } from './auto-login.guard'; |
| 13 | + |
| 14 | +describe(`AutoLoginGuard`, () => { |
| 15 | + let autoLoginGuard: AutoLoginGuard; |
| 16 | + let checkAuthService: CheckAuthService; |
| 17 | + let loginService: LoginService; |
| 18 | + let authStateService: AuthStateService; |
| 19 | + let router: Router; |
| 20 | + |
| 21 | + beforeEach(() => { |
| 22 | + TestBed.configureTestingModule({ |
| 23 | + imports: [RouterTestingModule.withRoutes([])], |
| 24 | + providers: [ |
| 25 | + AutoLoginService, |
| 26 | + { provide: AuthStateService, useClass: AuthStateServiceMock }, |
| 27 | + { |
| 28 | + provide: LoginService, |
| 29 | + useClass: LoginServiceMock, |
| 30 | + }, |
| 31 | + { |
| 32 | + provide: CheckAuthService, |
| 33 | + useClass: CheckAuthServiceMock, |
| 34 | + }, |
| 35 | + ], |
| 36 | + }); |
| 37 | + }); |
| 38 | + |
| 39 | + beforeEach(() => { |
| 40 | + autoLoginGuard = TestBed.inject(AutoLoginGuard); |
| 41 | + checkAuthService = TestBed.inject(CheckAuthService); |
| 42 | + authStateService = TestBed.inject(AuthStateService); |
| 43 | + router = TestBed.inject(Router); |
| 44 | + loginService = TestBed.inject(LoginService); |
| 45 | + |
| 46 | + localStorage.clear(); |
| 47 | + }); |
| 48 | + |
| 49 | + it('should create', () => { |
| 50 | + expect(autoLoginGuard).toBeTruthy(); |
| 51 | + }); |
| 52 | + |
| 53 | + describe('canActivate', () => { |
| 54 | + it( |
| 55 | + 'should call checkAuth() if not authenticated already', |
| 56 | + waitForAsync(() => { |
| 57 | + const checkAuthServiceSpy = spyOn(checkAuthService, 'checkAuth').and.returnValue(of(null)); |
| 58 | + |
| 59 | + autoLoginGuard.canActivate(null, { url: 'some-url1' } as RouterStateSnapshot).subscribe(() => { |
| 60 | + expect(checkAuthServiceSpy).toHaveBeenCalledTimes(1); |
| 61 | + }); |
| 62 | + }) |
| 63 | + ); |
| 64 | + |
| 65 | + it( |
| 66 | + 'should NOT call checkAuth() if authenticated already', |
| 67 | + waitForAsync(() => { |
| 68 | + const checkAuthServiceSpy = spyOn(checkAuthService, 'checkAuth').and.returnValue(of(null)); |
| 69 | + spyOnProperty(authStateService, 'authorized$', 'get').and.returnValue(of(true)); |
| 70 | + |
| 71 | + autoLoginGuard.canActivate(null, { url: 'some-url2' } as RouterStateSnapshot).subscribe(() => { |
| 72 | + expect(checkAuthServiceSpy).not.toHaveBeenCalled(); |
| 73 | + }); |
| 74 | + }) |
| 75 | + ); |
| 76 | + |
| 77 | + it( |
| 78 | + 'should call loginService.login() when not authorized', |
| 79 | + waitForAsync(() => { |
| 80 | + spyOn(checkAuthService, 'checkAuth').and.returnValue(of(null)); |
| 81 | + const loginSpy = spyOn(loginService, 'login'); |
| 82 | + |
| 83 | + autoLoginGuard.canActivate(null, { url: 'some-url3' } as RouterStateSnapshot).subscribe(() => { |
| 84 | + expect(loginSpy).toHaveBeenCalledTimes(1); |
| 85 | + }); |
| 86 | + }) |
| 87 | + ); |
| 88 | + |
| 89 | + it( |
| 90 | + 'should return false when not authorized', |
| 91 | + waitForAsync(() => { |
| 92 | + spyOn(checkAuthService, 'checkAuth').and.returnValue(of(null)); |
| 93 | + |
| 94 | + autoLoginGuard.canActivate(null, { url: 'some-url4' } as RouterStateSnapshot).subscribe((result) => { |
| 95 | + expect(result).toBe(false); |
| 96 | + }); |
| 97 | + }) |
| 98 | + ); |
| 99 | + |
| 100 | + it( |
| 101 | + 'if no route is stored, setItem on localStorage is called', |
| 102 | + waitForAsync(() => { |
| 103 | + spyOn(checkAuthService, 'checkAuth').and.returnValue(of(null)); |
| 104 | + const localStorageSpy = spyOn(localStorage, 'setItem'); |
| 105 | + |
| 106 | + autoLoginGuard.canActivate(null, { url: 'some-url5' } as RouterStateSnapshot).subscribe((result) => { |
| 107 | + expect(localStorageSpy).toHaveBeenCalledOnceWith('redirect', 'some-url5'); |
| 108 | + }); |
| 109 | + }) |
| 110 | + ); |
| 111 | + |
| 112 | + it( |
| 113 | + 'returns true if authorized', |
| 114 | + waitForAsync(() => { |
| 115 | + spyOn(checkAuthService, 'checkAuth').and.returnValue(of(true)); |
| 116 | + const localStorageSpy = spyOn(localStorage, 'setItem'); |
| 117 | + |
| 118 | + autoLoginGuard.canActivate(null, { url: 'some-url6' } as RouterStateSnapshot).subscribe((result) => { |
| 119 | + expect(result).toBe(true); |
| 120 | + expect(localStorageSpy).not.toHaveBeenCalled(); |
| 121 | + }); |
| 122 | + }) |
| 123 | + ); |
| 124 | + |
| 125 | + it( |
| 126 | + 'if authorized and stored route exists: remove item, navigate to route and return true', |
| 127 | + waitForAsync(() => { |
| 128 | + spyOn(checkAuthService, 'checkAuth').and.returnValue(of(true)); |
| 129 | + spyOn(localStorage, 'getItem').and.returnValue('stored-route'); |
| 130 | + const localStorageSpy = spyOn(localStorage, 'removeItem'); |
| 131 | + const routerSpy = spyOn(router, 'navigate'); |
| 132 | + const loginSpy = spyOn(loginService, 'login'); |
| 133 | + |
| 134 | + autoLoginGuard.canActivate(null, { url: 'some-url7' } as RouterStateSnapshot).subscribe((result) => { |
| 135 | + expect(result).toBe(true); |
| 136 | + expect(localStorageSpy).toHaveBeenCalledOnceWith('redirect'); |
| 137 | + expect(routerSpy).toHaveBeenCalledOnceWith(['stored-route']); |
| 138 | + expect(loginSpy).not.toHaveBeenCalled(); |
| 139 | + }); |
| 140 | + }) |
| 141 | + ); |
| 142 | + }); |
| 143 | + |
| 144 | + describe('canLoad', () => { |
| 145 | + it( |
| 146 | + 'should call checkAuth() if not authenticated already', |
| 147 | + waitForAsync(() => { |
| 148 | + const checkAuthServiceSpy = spyOn(checkAuthService, 'checkAuth').and.returnValue(of(null)); |
| 149 | + |
| 150 | + autoLoginGuard.canLoad({ path: 'some-url8' }, []).subscribe(() => { |
| 151 | + expect(checkAuthServiceSpy).toHaveBeenCalledTimes(1); |
| 152 | + }); |
| 153 | + }) |
| 154 | + ); |
| 155 | + |
| 156 | + it( |
| 157 | + 'should NOT call checkAuth() if authenticated already', |
| 158 | + waitForAsync(() => { |
| 159 | + const checkAuthServiceSpy = spyOn(checkAuthService, 'checkAuth').and.returnValue(of(null)); |
| 160 | + spyOnProperty(authStateService, 'authorized$', 'get').and.returnValue(of(true)); |
| 161 | + |
| 162 | + autoLoginGuard.canLoad({ path: 'some-url9' }, []).subscribe(() => { |
| 163 | + expect(checkAuthServiceSpy).not.toHaveBeenCalled(); |
| 164 | + }); |
| 165 | + }) |
| 166 | + ); |
| 167 | + |
| 168 | + it( |
| 169 | + 'should call loginService.login() when not authorized', |
| 170 | + waitForAsync(() => { |
| 171 | + spyOn(checkAuthService, 'checkAuth').and.returnValue(of(null)); |
| 172 | + const loginSpy = spyOn(loginService, 'login'); |
| 173 | + |
| 174 | + autoLoginGuard.canLoad({ path: 'some-url10' }, []).subscribe(() => { |
| 175 | + expect(loginSpy).toHaveBeenCalledTimes(1); |
| 176 | + }); |
| 177 | + }) |
| 178 | + ); |
| 179 | + |
| 180 | + it( |
| 181 | + 'should return false when not authorized', |
| 182 | + waitForAsync(() => { |
| 183 | + spyOn(checkAuthService, 'checkAuth').and.returnValue(of(null)); |
| 184 | + |
| 185 | + autoLoginGuard.canLoad({ path: 'some-url11' }, []).subscribe((result) => { |
| 186 | + expect(result).toBe(false); |
| 187 | + }); |
| 188 | + }) |
| 189 | + ); |
| 190 | + |
| 191 | + it( |
| 192 | + 'if no route is stored, setItem on localStorage is called', |
| 193 | + waitForAsync(() => { |
| 194 | + spyOn(checkAuthService, 'checkAuth').and.returnValue(of(null)); |
| 195 | + const localStorageSpy = spyOn(localStorage, 'setItem'); |
| 196 | + |
| 197 | + autoLoginGuard.canLoad({ path: 'some-url12' }, []).subscribe((result) => { |
| 198 | + expect(localStorageSpy).toHaveBeenCalledOnceWith('redirect', 'some-url12'); |
| 199 | + }); |
| 200 | + }) |
| 201 | + ); |
| 202 | + |
| 203 | + it( |
| 204 | + 'returns true if authorized', |
| 205 | + waitForAsync(() => { |
| 206 | + spyOn(checkAuthService, 'checkAuth').and.returnValue(of(true)); |
| 207 | + const localStorageSpy = spyOn(localStorage, 'setItem'); |
| 208 | + |
| 209 | + autoLoginGuard.canLoad({ path: 'some-url13' }, []).subscribe((result) => { |
| 210 | + expect(result).toBe(true); |
| 211 | + expect(localStorageSpy).not.toHaveBeenCalled(); |
| 212 | + }); |
| 213 | + }) |
| 214 | + ); |
| 215 | + |
| 216 | + it( |
| 217 | + 'if authorized and stored route exists: remove item, navigate to route and return true', |
| 218 | + waitForAsync(() => { |
| 219 | + spyOn(checkAuthService, 'checkAuth').and.returnValue(of(true)); |
| 220 | + spyOn(localStorage, 'getItem').and.returnValue('stored-route'); |
| 221 | + const localStorageSpy = spyOn(localStorage, 'removeItem'); |
| 222 | + const routerSpy = spyOn(router, 'navigate'); |
| 223 | + const loginSpy = spyOn(loginService, 'login'); |
| 224 | + |
| 225 | + autoLoginGuard.canLoad({ path: 'some-url14' }, []).subscribe((result) => { |
| 226 | + expect(result).toBe(true); |
| 227 | + expect(localStorageSpy).toHaveBeenCalledOnceWith('redirect'); |
| 228 | + expect(routerSpy).toHaveBeenCalledOnceWith(['stored-route']); |
| 229 | + expect(loginSpy).not.toHaveBeenCalled(); |
| 230 | + }); |
| 231 | + }) |
| 232 | + ); |
| 233 | + }); |
| 234 | +}); |
0 commit comments