From b959f19e228959c9ae4e2b24df310c4764cdd31f Mon Sep 17 00:00:00 2001 From: DaviMatheus Date: Tue, 3 Sep 2024 00:34:55 -0300 Subject: [PATCH] fix(#71):arrumando_teste_comentado --- test/role.guard.spec.ts | 107 +++++++++++++++++++---------------- test/user.controller.spec.ts | 1 - 2 files changed, 58 insertions(+), 50 deletions(-) diff --git a/test/role.guard.spec.ts b/test/role.guard.spec.ts index cef0cb5..93ebc45 100644 --- a/test/role.guard.spec.ts +++ b/test/role.guard.spec.ts @@ -1,17 +1,37 @@ -/* eslint-disable prettier/prettier */ -import { RolesGuard } from 'src/auth/guards/roles.guard'; +import { Test, TestingModule } from '@nestjs/testing'; import { Reflector } from '@nestjs/core'; +import { JwtService } from '@nestjs/jwt'; import { ExecutionContext } from '@nestjs/common'; import { UserRole } from 'src/users/dtos/user-role.enum'; +import { RolesGuard } from 'src/auth/guards/roles.guard'; describe('RolesGuard', () => { let rolesGuard: RolesGuard; + let jwtService: JwtService; let reflector: Reflector; - beforeEach(() => { - reflector = new Reflector(); - const jwtService = {} as any; - rolesGuard = new RolesGuard(reflector, jwtService); + beforeEach(async () => { + const module: TestingModule = await Test.createTestingModule({ + providers: [ + RolesGuard, + { + provide: Reflector, + useValue: { + get: jest.fn(), + }, + }, + { + provide: JwtService, + useValue: { + decode: jest.fn(), + }, + }, + ], + }).compile(); + + rolesGuard = module.get(RolesGuard); + reflector = module.get(Reflector); + jwtService = module.get(JwtService); }); it('should be defined', () => { @@ -19,55 +39,44 @@ describe('RolesGuard', () => { }); describe('canActivate', () => { - it('should return true if no roles are defined', () => { + it('should return true if no roles are required', () => { + const context = createMockExecutionContext({}); jest.spyOn(reflector, 'get').mockReturnValue(undefined); - const context = { - switchToHttp: jest.fn().mockReturnValue({ - getRequest: jest - .fn() - .mockReturnValue({ user: { role: UserRole.ALUNO } }), - }), - getHandler: jest.fn().mockReturnValue(null), - } as unknown as ExecutionContext; - - const result = rolesGuard.canActivate(context); - - expect(result).toBe(true); + expect(rolesGuard.canActivate(context)).toBe(true); }); - // it('should return true if user role is in required roles', () => { - // const requiredRoles = [UserRole.ADMIN]; - // jest.spyOn(reflector, 'get').mockReturnValue(requiredRoles); - - // const context = { - // switchToHttp: jest.fn().mockReturnValue({ - // getRequest: jest - // .fn() - // .mockReturnValue({ user: { role: UserRole.ADMIN } }), - // }), - // getHandler: jest.fn().mockReturnValue(null), - // } as unknown as ExecutionContext; + it('should return false if the user does not have the required role', () => { + const context = createMockExecutionContext({ + headers: { authorization: 'Bearer validToken' }, + }); + jest.spyOn(reflector, 'get').mockReturnValue([UserRole.ADMIN]); + jest + .spyOn(jwtService, 'decode') + .mockReturnValue({ role: [UserRole.ALUNO] }); - // const result = rolesGuard.canActivate(context); - - // expect(result).toBe(true); - // }); - - // it('should return false if user role is not in required roles', () => { - // const requiredRoles = [UserRole.ADMIN]; - // jest.spyOn(reflector, 'get').mockReturnValue(requiredRoles); + expect(rolesGuard.canActivate(context)).toBe(false); + }); - // const context = { - // switchToHttp: jest.fn().mockReturnValue({ - // getRequest: jest - // .fn() - // .mockReturnValue({ user: { role: UserRole.ALUNO } }), - // }), - // getHandler: jest.fn().mockReturnValue(null), - // } as unknown as ExecutionContext; + it('should return true if the user has the required role', () => { + const context = createMockExecutionContext({ + headers: { authorization: 'Bearer validToken' }, + }); + jest.spyOn(reflector, 'get').mockReturnValue([UserRole.ADMIN]); + jest + .spyOn(jwtService, 'decode') + .mockReturnValue({ role: [UserRole.ADMIN] }); - // expect(() => rolesGuard.canActivate(context)).toBe(false); - // }); + expect(rolesGuard.canActivate(context)).toBe(true); + }); }); }); + +function createMockExecutionContext(request: any) { + return { + switchToHttp: () => ({ + getRequest: () => request, + }), + getHandler: () => jest.fn(), + } as unknown as ExecutionContext; +} diff --git a/test/user.controller.spec.ts b/test/user.controller.spec.ts index cb97963..30c2e2b 100644 --- a/test/user.controller.spec.ts +++ b/test/user.controller.spec.ts @@ -150,4 +150,3 @@ describe('UsersController', () => { ).rejects.toThrow(NotFoundException); }); }); -