Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: login twitter test #187

Draft
wants to merge 11 commits into
base: main
Choose a base branch
from
Draft
28 changes: 23 additions & 5 deletions src/features/twitter/controllers/twitter-controller.test.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import type { NextFunction, Request, Response } from 'express';
import jwt from 'jsonwebtoken';
import { mock, mockDeep } from 'vitest-mock-extended';

import type { Logger } from '@/shared/infra/logger/logger';
Expand All @@ -18,6 +19,7 @@ describe('[Controller] Twitter', () => {
let req: Request;
let res: Response;
let next: NextFunction;

beforeEach(() => {
mockLogger = mock<Logger>(loggerMock);

Expand All @@ -38,18 +40,15 @@ describe('[Controller] Twitter', () => {
authController = new TwitterController(authorizeTwitterService);

req = mockDeep<Request>();

res = {
json: vi.fn(),
send: vi.fn(),
status: vi.fn().mockReturnThis(),
} as unknown as Response;

next = vi.fn() as unknown as NextFunction;
});

describe('callback', () => {
it('should be return code', async () => {
it('should return code', async () => {
const spyAuthorizeTwitter = vi
.spyOn(authorizeTwitterService, 'execute')
.mockReturnThis();
Expand All @@ -66,7 +65,26 @@ describe('[Controller] Twitter', () => {
});

describe('login', () => {
it('should be return 401', () => {
it('should return URLs when the token is valid', async () => {
const mockPayload = {
name: 'John Doe',
userId: '12345',
username: 'johndoe',
};
const expectedUrl = `https://twitter.com/i/oauth2/authorize?client_id=mockClientId123&code_challenge=-a4-ROPIVaUBVj1qqB2O6eN_qSC0WvET0EdUEhSFqrI&code_challenge_method=S256&redirect_uri=http%3A%2F%2Fwww.localhost%3A3000%2Fapi%2Ftwitter%2Fcallback&response_type=code&state=${mockPayload.userId}&scope=tweet.write%20tweet.read%20users.read`;

process.env.TWITTER_CLIENT_ID = 'mockClientId123';
req.headers.authorization =
'Bearer eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJuYW1lIjoiSm9obiBEb2UiLCJ1c2VySWQiOiIxMjM0NSIsInVzZXJuYW1lIjoiam9obmRvZSIsImlhdCI6MTY2NTMzMjM0NCwiZXhwIjoxNjY1MzM1OTQ0fQ.S5ReMBrQqVAD6UCD6Q9Vj9fK9J-Q9r_a9f3zRmpDsxM';

vi.spyOn(jwt, 'verify').mockReturnValue(mockPayload as any);

authController.login(req, res, next);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

pq ta sem await?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

pq nao precisa

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

mas nao sei se é melhor com ou sem btw


expect(res.json).toHaveBeenCalledWith(expectedUrl);
});

it('should returns 401 when the token is valid', () => {
req.headers.authorization = undefined;

authController.login(req, res, next);
Expand Down
1 change: 0 additions & 1 deletion src/middlewares/auth/auth-jwt.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,6 @@ export class AuthenticationJWT {
const { userId } = this.jwtHelper.parseToken(token);

const user = await this.userRepository.findById(userId);
console.log('asdasdasd');
if (!user) {
return res.status(401).json({ error: 'Invalid user' });
}
Expand Down
Loading