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

removing user data from user auth request #52

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
56 changes: 15 additions & 41 deletions src/controller/session/authUser.spec.ts
Original file line number Diff line number Diff line change
@@ -1,79 +1,53 @@
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 () => {
await app.close()
})

test('should be able to login', async () => {
const email = '[email protected]'
const name = 'John'
const surname = 'Doe'
const password = 'password'

await request(app.server).post('/user').send({
email,
name,
surname,
password,
})
const email = '[email protected]'
const password = '12345678'

const userData = await request(app.server)
.post('/login')
.send({ email, password })

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 = '[email protected]'
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 = '[email protected]'
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 = '[email protected]'
const wrongEmail = '[email protected]'
const name = 'John'
const surname = 'Doe'
const password = 'password'

await request(app.server).post('/user').send({
email,
name,
surname,
password,
})
const email = '[email protected]'
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({}))
Expand Down
2 changes: 1 addition & 1 deletion src/controller/session/authUser.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand Down
13 changes: 12 additions & 1 deletion src/utils/create-and-authenticate-user.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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: '[email protected]' })
.set('Authorization', `Bearer ${token}`)

const { id: userId } = getUseByEmailResponse.body.user

return { token, userId }
}
Loading