Skip to content

Commit

Permalink
(#136) adiciona configuracoes e testes
Browse files Browse the repository at this point in the history
  • Loading branch information
HenriqueAmorim20 committed Oct 24, 2023
1 parent 98bb4c3 commit c2b7b0a
Show file tree
Hide file tree
Showing 4 changed files with 92 additions and 2 deletions.
6 changes: 6 additions & 0 deletions docker-compose.prod.yml
Original file line number Diff line number Diff line change
Expand Up @@ -9,12 +9,15 @@ services:
- DB_PASS=postgres
- DB_DATABASE=gerocuidado-forum-db
- DB_PORT=5002
- AUTH_HOST=gerocuidado-usuario-api-prod
- AUTH_PORT=4001
ports:
- '3002:3002'
depends_on:
- gerocuidado-forum-db
networks:
- gerocuidado-forum-net
- gerocuidado-apis-net

gerocuidado-forum-db:
build:
Expand All @@ -36,3 +39,6 @@ services:
networks:
gerocuidado-forum-net:
driver: bridge
gerocuidado-apis-net:
name: gerocuidado-apis-net
external: true
4 changes: 2 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -16,10 +16,10 @@
"lint": "eslint \"{src,apps,libs,test}/**/*.ts\" --fix",
"test": "jest --no-cache --colors --detectOpenHandles",
"test:watch": "jest --watchAll",
"test:cov": "jest --coverage --colors",
"test:cov": "jest --forceExit --detectOpenHandles --runInBand --coverage --colors",
"test:debug": "node --inspect-brk -r tsconfig-paths/register -r ts-node/register node_modules/.bin/jest --runInBand",
"test:e2e": "jest --forceExit --detectOpenHandles --colors --config ./e2e/jest-e2e.json",
"test:e2e:cov": "jest --forceExit --detectOpenHandles --colors --coverage --config ./e2e/jest-e2e.json",
"test:e2e:cov": "jest --forceExit --detectOpenHandles --runInBand --colors --coverage --config ./e2e/jest-e2e.json",
"test:e2e:watch": "jest --detectOpenHandles --config ./e2e/jest-e2e.json --watchAll",
"typeorm": "node --require ts-node/register ./node_modules/typeorm/cli.js",
"typeorm:create": "npm run typeorm migration:create",
Expand Down
76 changes: 76 additions & 0 deletions src/autenticacao.guard.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,76 @@
import { UnauthorizedException } from '@nestjs/common';
import { Reflector } from '@nestjs/core';
import { Test, TestingModule } from '@nestjs/testing';
import { of } from 'rxjs';
import { AutenticacaoGuard } from './autenticacao.guard';

describe('AutenticacaoGuard', () => {
let guard: AutenticacaoGuard;
let reflector: Reflector;

const mockClientProxy = {
send: jest.fn(),
};

const mockContext = {
switchToHttp: jest.fn().mockReturnValue({
getRequest: jest.fn().mockReturnValue({
headers: {
authorization: 'Bearer token',
},
}),
}),
getHandler: jest.fn(),
getClass: jest.fn(),
};

beforeEach(async () => {
const module: TestingModule = await Test.createTestingModule({
providers: [
AutenticacaoGuard,
{
provide: 'AUTH_CLIENT',
useValue: mockClientProxy,
},
Reflector,
],
}).compile();

guard = module.get<AutenticacaoGuard>(AutenticacaoGuard);
reflector = module.get<Reflector>(Reflector);
});

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

it('should pass if route is public', async () => {
jest.spyOn(reflector, 'getAllAndOverride').mockReturnValue(true);

const result = await guard.canActivate(mockContext as any);

expect(result).toBe(true);
});

it('should pass if authentication is successful', async () => {
jest.spyOn(reflector, 'getAllAndOverride').mockReturnValue(false);
mockClientProxy.send.mockReturnValue(of(true));

const result = await guard.canActivate(mockContext as any);

expect(result).toBe(true);
});

it('should not pass if authentication is unsuccessful', async () => {
jest.spyOn(reflector, 'getAllAndOverride').mockReturnValue(false);
mockClientProxy.send.mockReturnValue(of(false));

guard
.canActivate(mockContext as any)
.catch((err) =>
expect(err).toEqual(
new UnauthorizedException('Usuário não autenticado!'),
),
);
});
});
8 changes: 8 additions & 0 deletions src/shared/decorators/public-route.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
import { PublicRoute } from './public-route.decorator';

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

0 comments on commit c2b7b0a

Please sign in to comment.