diff --git a/docker-compose.prod.yml b/docker-compose.prod.yml index bb4c64b..5368ada 100644 --- a/docker-compose.prod.yml +++ b/docker-compose.prod.yml @@ -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: @@ -36,3 +39,6 @@ services: networks: gerocuidado-forum-net: driver: bridge + gerocuidado-apis-net: + name: gerocuidado-apis-net + external: true diff --git a/package.json b/package.json index 30e4313..0c1350c 100644 --- a/package.json +++ b/package.json @@ -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", diff --git a/src/autenticacao.guard.spec.ts b/src/autenticacao.guard.spec.ts new file mode 100644 index 0000000..54570e1 --- /dev/null +++ b/src/autenticacao.guard.spec.ts @@ -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); + reflector = module.get(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!'), + ), + ); + }); +}); diff --git a/src/shared/decorators/public-route.spec.ts b/src/shared/decorators/public-route.spec.ts new file mode 100644 index 0000000..4303b04 --- /dev/null +++ b/src/shared/decorators/public-route.spec.ts @@ -0,0 +1,8 @@ +import { PublicRoute } from './public-route.decorator'; + +describe('Pagination', () => { + it('should be defined', () => { + PublicRoute(); + expect(PublicRoute).toBeDefined(); + }); +});