Skip to content

Commit

Permalink
(#136) corrige linhas duplicadas
Browse files Browse the repository at this point in the history
  • Loading branch information
HenriqueAmorim20 committed Oct 24, 2023
1 parent cfbc9ee commit 8984bd6
Showing 1 changed file with 15 additions and 36 deletions.
51 changes: 15 additions & 36 deletions src/autenticacao.guard.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,18 @@ describe('AutenticacaoGuard', () => {
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: [
Expand All @@ -33,61 +45,28 @@ describe('AutenticacaoGuard', () => {
});

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);
const result = await guard.canActivate(mockContext 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);
const result = await guard.canActivate(mockContext 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)
.canActivate(mockContext as any)
.catch((err) =>
expect(err).toEqual(
new UnauthorizedException('Usuário não autenticado!'),
Expand Down

0 comments on commit 8984bd6

Please sign in to comment.