diff --git a/src/features/twitter/controllers/twitter-controller.test.ts b/src/features/twitter/controllers/twitter-controller.test.ts index 06dae39..ba4e0c1 100644 --- a/src/features/twitter/controllers/twitter-controller.test.ts +++ b/src/features/twitter/controllers/twitter-controller.test.ts @@ -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'; @@ -18,6 +19,7 @@ describe('[Controller] Twitter', () => { let req: Request; let res: Response; let next: NextFunction; + beforeEach(() => { mockLogger = mock(loggerMock); @@ -38,18 +40,15 @@ describe('[Controller] Twitter', () => { authController = new TwitterController(authorizeTwitterService); req = mockDeep(); - 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(); @@ -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); + + expect(res.json).toHaveBeenCalledWith(expectedUrl); + }); + + it('should returns 401 when the token is valid', () => { req.headers.authorization = undefined; authController.login(req, res, next); diff --git a/src/middlewares/auth/auth-jwt.ts b/src/middlewares/auth/auth-jwt.ts index 3818f7f..02385d3 100644 --- a/src/middlewares/auth/auth-jwt.ts +++ b/src/middlewares/auth/auth-jwt.ts @@ -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' }); }