-
Notifications
You must be signed in to change notification settings - Fork 22
feat: login twitter test #187
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
base: main
Are you sure you want to change the base?
Changes from 10 commits
ef304cb
7146fca
5fc911e
8b732f7
972734d
fed53aa
21a58de
f3c30d2
d248f54
c2b4c08
444cec0
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
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'; | ||
|
@@ -18,6 +19,7 @@ describe('[Controller] Twitter', () => { | |
let req: Request; | ||
let res: Response; | ||
let next: NextFunction; | ||
|
||
beforeEach(() => { | ||
mockLogger = mock<Logger>(loggerMock); | ||
|
||
|
@@ -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(); | ||
|
@@ -66,7 +65,25 @@ 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); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. pq ta sem await? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. pq nao precisa There was a problem hiding this comment. Choose a reason for hiding this commentThe 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); | ||
juliaam marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
Uh oh!
There was an error while loading. Please reload this page.