diff --git a/src/controller/session/authUser.spec.ts b/src/controller/session/authUser.spec.ts index ea67497..5dd10ef 100644 --- a/src/controller/session/authUser.spec.ts +++ b/src/controller/session/authUser.spec.ts @@ -1,10 +1,15 @@ import { afterAll, beforeAll, describe, expect, test } from 'vitest' import { app } from '../../app' import request from 'supertest' - +import { createAndAuthenticateUser } from '../../utils/create-and-authenticate-user' +let userAuth: { + token: string + userId: string +} describe('User Login E2E', () => { beforeAll(async () => { await app.ready() + userAuth = await createAndAuthenticateUser(app) }) afterAll(async () => { @@ -12,17 +17,8 @@ describe('User Login E2E', () => { }) test('should be able to login', async () => { - const email = 'john_doe@email.com.br' - const name = 'John' - const surname = 'Doe' - const password = 'password' - - await request(app.server).post('/user').send({ - email, - name, - surname, - password, - }) + const email = 'johndoe@example.com' + const password = '12345678' const userData = await request(app.server) .post('/login') @@ -30,50 +26,28 @@ describe('User Login E2E', () => { expect(userData.statusCode).toEqual(200) expect(userData.body).toEqual({ - user: expect.any(Object), - token: expect.any(String), + token: userAuth.token, }) }) test('should not be able to login because the password is incorrect', async () => { - const email = 'john_doe@email.com.br' - const name = 'John' - const surname = 'Doe' - const password = 'password' - const wrongPassword = 'wrongPassword' - - await request(app.server).post('/user').send({ - email, - name, - surname, - password, - }) - + const email = 'johndoe@example.com' + const password = 'wrongpass' const userData = await request(app.server) .post('/login') - .send({ email, password: wrongPassword }) + .send({ email, password }) expect(userData.statusCode).toEqual(401) expect(userData.body.user).toEqual(expect.objectContaining({})) }) test('should not be able to login because the email is incorrect', async () => { - const email = 'john_doe@email.com.br' - const wrongEmail = 'wrong@email.com.br' - const name = 'John' - const surname = 'Doe' - const password = 'password' - - await request(app.server).post('/user').send({ - email, - name, - surname, - password, - }) + const email = 'wrongemail@example.com' + const password = '12345678' const userData = await request(app.server) .post('/login') - .send({ email: wrongEmail, password }) + .send({ email, password }) expect(userData.statusCode).toEqual(401) expect(userData.body.user).toEqual(expect.objectContaining({})) diff --git a/src/controller/session/authUser.ts b/src/controller/session/authUser.ts index b75c9b7..e077714 100644 --- a/src/controller/session/authUser.ts +++ b/src/controller/session/authUser.ts @@ -30,7 +30,7 @@ export async function authUser( }, ) - return response.status(200).send({ user, token }) + return response.status(200).send({ token }) } catch (e) { if (e instanceof InvalidCredentialsError) { return response.status(401).send() diff --git a/src/utils/create-and-authenticate-user.ts b/src/utils/create-and-authenticate-user.ts index 0aa9aaa..c92072d 100644 --- a/src/utils/create-and-authenticate-user.ts +++ b/src/utils/create-and-authenticate-user.ts @@ -15,7 +15,18 @@ export async function createAndAuthenticateUser(app: FastifyInstance) { }) const { token } = authResponse.body - const { id: userId } = authResponse.body.user + + /* + we won't need that in the future + we need to adress this issue to remove this call to get the user id + https://github.com/MatheusSanchez/orange-back/issues/50 + */ + const getUseByEmailResponse = await request(app.server) + .get('/user') + .query({ email: 'johndoe@example.com' }) + .set('Authorization', `Bearer ${token}`) + + const { id: userId } = getUseByEmailResponse.body.user return { token, userId } }