generated from HenriqueAmorim20/GEROcuidadoAPITemplate
-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
(#136) adiciona configuracoes e testes
- Loading branch information
1 parent
98bb4c3
commit c2b7b0a
Showing
4 changed files
with
92 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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!'), | ||
), | ||
); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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(); | ||
}); | ||
}); |