Skip to content

Commit

Permalink
Correcao admin.guard.spec.ts
Browse files Browse the repository at this point in the history
  • Loading branch information
viniman27 authored Jul 28, 2024
1 parent 9d300c0 commit a3e7f29
Showing 1 changed file with 30 additions and 11 deletions.
41 changes: 30 additions & 11 deletions src/app/guard/admin.guard.spec.ts
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 } });
});
});

0 comments on commit a3e7f29

Please sign in to comment.