Skip to content

Commit

Permalink
fix(#71):arrumando_teste_comentado
Browse files Browse the repository at this point in the history
  • Loading branch information
DaviMatheus committed Sep 3, 2024
1 parent 079ea59 commit b959f19
Show file tree
Hide file tree
Showing 2 changed files with 58 additions and 50 deletions.
107 changes: 58 additions & 49 deletions test/role.guard.spec.ts
Original file line number Diff line number Diff line change
@@ -1,73 +1,82 @@
/* 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>(RolesGuard);
reflector = module.get<Reflector>(Reflector);
jwtService = module.get<JwtService>(JwtService);
});

it('should be defined', () => {
expect(rolesGuard).toBeDefined();
});

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;
}
1 change: 0 additions & 1 deletion test/user.controller.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -150,4 +150,3 @@ describe('UsersController', () => {
).rejects.toThrow(NotFoundException);
});
});

0 comments on commit b959f19

Please sign in to comment.