generated from HenriqueAmorim20/GEROcuidadoAPITemplate
-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #4 from fga-eps-mds/feature/#136-autenticacao
Feature/#136 autenticacao
- Loading branch information
Showing
9 changed files
with
170 additions
and
7 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
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
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,43 @@ | ||
import { | ||
CanActivate, | ||
ExecutionContext, | ||
Inject, | ||
Injectable, | ||
UnauthorizedException, | ||
} from '@nestjs/common'; | ||
import { Reflector } from '@nestjs/core'; | ||
import { ClientProxy } from '@nestjs/microservices'; | ||
import { lastValueFrom, timeout } from 'rxjs'; | ||
import { IS_PUBLIC_KEY } from './shared/decorators/public-route.decorator'; | ||
|
||
@Injectable() | ||
export class AutenticacaoGuard implements CanActivate { | ||
constructor( | ||
@Inject('AUTH_CLIENT') | ||
private readonly _client: ClientProxy, | ||
private readonly _reflector: Reflector, | ||
) {} | ||
|
||
async canActivate(context: ExecutionContext) { | ||
const req = context.switchToHttp().getRequest(); | ||
const jwt = req.headers['authorization']?.split(' ')[1]; | ||
|
||
const isPublic = this._reflector.getAllAndOverride<boolean>(IS_PUBLIC_KEY, [ | ||
context.getHandler(), | ||
context.getClass(), | ||
]); | ||
|
||
if (isPublic) return true; | ||
|
||
const request = this._client | ||
.send({ role: 'auth', cmd: 'check' }, { jwt }) | ||
.pipe(timeout(5000)); | ||
const response = await lastValueFrom(request); | ||
|
||
if (!response) { | ||
throw new UnauthorizedException('Usuário não autenticado!'); | ||
} | ||
|
||
return true; | ||
} | ||
} |
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(); | ||
}); | ||
}); |