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
3884f75
commit cfbc9ee
Showing
5 changed files
with
116 additions
and
5 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
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
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,97 @@ | ||
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(), | ||
}; | ||
|
||
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 () => { | ||
const context = { | ||
switchToHttp: jest.fn().mockReturnValue({ | ||
getRequest: jest.fn().mockReturnValue({ | ||
headers: { | ||
authorization: 'Bearer valid_token', | ||
}, | ||
}), | ||
}), | ||
getHandler: jest.fn(), | ||
getClass: jest.fn(), | ||
}; | ||
jest.spyOn(reflector, 'getAllAndOverride').mockReturnValue(true); | ||
|
||
const result = await guard.canActivate(context as any); | ||
|
||
expect(result).toBe(true); | ||
}); | ||
|
||
it('should pass if authentication is successful', async () => { | ||
const context = { | ||
switchToHttp: jest.fn().mockReturnValue({ | ||
getRequest: jest.fn().mockReturnValue({ | ||
headers: { | ||
authorization: 'Bearer valid_token', | ||
}, | ||
}), | ||
}), | ||
getHandler: jest.fn(), | ||
getClass: jest.fn(), | ||
}; | ||
jest.spyOn(reflector, 'getAllAndOverride').mockReturnValue(false); | ||
mockClientProxy.send.mockReturnValue(of(true)); | ||
|
||
const result = await guard.canActivate(context as any); | ||
|
||
expect(result).toBe(true); | ||
}); | ||
|
||
it('should not pass if authentication is unsuccessful', async () => { | ||
const context = { | ||
switchToHttp: jest.fn().mockReturnValue({ | ||
getRequest: jest.fn().mockReturnValue({ | ||
headers: { | ||
authorization: 'Bearer invalid_token', | ||
}, | ||
}), | ||
}), | ||
getHandler: jest.fn(), | ||
getClass: jest.fn(), | ||
}; | ||
jest.spyOn(reflector, 'getAllAndOverride').mockReturnValue(false); | ||
mockClientProxy.send.mockReturnValue(of(false)); | ||
|
||
guard | ||
.canActivate(context 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(); | ||
}); | ||
}); |