forked from UnBTV/UnB-TV-Frontend
-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
1 changed file
with
30 additions
and
11 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,42 +1,61 @@ | ||
import { TestBed } from '@angular/core/testing'; | ||
import { Router } from '@angular/router'; | ||
import { WithTokenGuard } from './with-token.guard'; | ||
import { TokenAdminGuard } from './admin.guard'; | ||
import { AuthService } from '../services/auth.service'; | ||
import { UserService } from '../services/user.service'; | ||
import { RouterTestingModule } from '@angular/router/testing'; | ||
import { ActivatedRouteSnapshot, RouterStateSnapshot } from '@angular/router'; | ||
|
||
describe('WithTokenGuard', () => { | ||
let guard: WithTokenGuard; | ||
describe('TokenAdminGuard', () => { | ||
let guard: TokenAdminGuard; | ||
let authService: jasmine.SpyObj<AuthService>; | ||
let userService: jasmine.SpyObj<UserService>; | ||
let router: Router; | ||
let route: ActivatedRouteSnapshot; | ||
let state: RouterStateSnapshot; | ||
|
||
beforeEach(() => { | ||
const authServiceSpy = jasmine.createSpyObj('AuthService', ['isAuthenticated']); | ||
const userServiceSpy = jasmine.createSpyObj('UserService', ['getRoles']); | ||
TestBed.configureTestingModule({ | ||
imports: [RouterTestingModule], | ||
providers: [ | ||
WithTokenGuard, | ||
TokenAdminGuard, | ||
{ provide: AuthService, useValue: authServiceSpy }, | ||
{ provide: UserService, useValue: userServiceSpy }, | ||
], | ||
}); | ||
guard = TestBed.inject(WithTokenGuard); | ||
guard = TestBed.inject(TokenAdminGuard); | ||
authService = TestBed.inject(AuthService) as jasmine.SpyObj<AuthService>; | ||
userService = TestBed.inject(UserService) as jasmine.SpyObj<UserService>; | ||
router = TestBed.inject(Router); | ||
route = new ActivatedRouteSnapshot(); | ||
state = { url: '/someUrl' } as RouterStateSnapshot; | ||
}); | ||
|
||
it('should be created', () => { | ||
expect(guard).toBeTruthy(); | ||
}); | ||
|
||
it('should allow activation for unauthenticated user', () => { | ||
authService.isAuthenticated.and.returnValue(false); | ||
expect(guard.canActivate()).toBe(true); | ||
it('should allow activation for authenticated admin user', () => { | ||
authService.isAuthenticated.and.returnValue(true); | ||
userService.getRoles.and.returnValue('ADMIN'); | ||
expect(guard.canActivate(route, state)).toBe(true); | ||
}); | ||
|
||
it('should navigate to /catalog for authenticated user', () => { | ||
it('should navigate to /loginsocial for authenticated non-admin user', () => { | ||
authService.isAuthenticated.and.returnValue(true); | ||
userService.getRoles.and.returnValue('USER'); | ||
spyOn(router, 'navigate'); | ||
expect(guard.canActivate(route, state)).toBe(false); | ||
expect(router.navigate).toHaveBeenCalledWith(['/loginsocial']); | ||
}); | ||
|
||
it('should navigate to /loginsocial with returnUrl for unauthenticated user', () => { | ||
authService.isAuthenticated.and.returnValue(false); | ||
spyOn(router, 'navigate'); | ||
expect(guard.canActivate()).toBe(false); | ||
expect(router.navigate).toHaveBeenCalledWith(['/catalog']); | ||
expect(guard.canActivate(route, state)).toBe(false); | ||
expect(router.navigate).toHaveBeenCalledWith(['/loginsocial'], { queryParams: { returnUrl: state.url } }); | ||
}); | ||
}); | ||
|