Skip to content

Commit

Permalink
Removing getUserByEmail and removing id as query param. We now can us…
Browse files Browse the repository at this point in the history
…e the jwt token request.user.sub
  • Loading branch information
MatheusSanchez committed Feb 4, 2024
1 parent c3f5887 commit 946c7bf
Show file tree
Hide file tree
Showing 3 changed files with 12 additions and 40 deletions.
41 changes: 10 additions & 31 deletions src/controller/user/getUserById.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,60 +2,39 @@ import { afterAll, beforeAll, describe, expect, it } from 'vitest'
import request from 'supertest'
import { app } from '../../app'
import { createAndAuthenticateUser } from '../../utils/create-and-authenticate-user'
import { randomUUID } from 'crypto'

let userAuth: {
token: string
userId: string
}

describe('Get User By Id E2E', () => {
beforeAll(async () => {
await app.ready()
userAuth = await createAndAuthenticateUser(app)
})

afterAll(async () => {
await app.close()
})

it('should be able to get an user by ID', async () => {
const { token } = await createAndAuthenticateUser(app)
const getUserByIdResponse = await request(app.server)
.get(`/user/${userAuth.userId}`)
.set('Authorization', `Bearer ${userAuth.token}`)
.get(`/user`)
.set('Authorization', `Bearer ${token}`)

expect(getUserByIdResponse.statusCode).toEqual(200)
expect(getUserByIdResponse.body.user).toEqual(
expect.objectContaining({
id: userAuth.userId,
country: 'brasil',
name: 'John',
surname: 'Doe',
email: '[email protected]',
}),
)
})

it('should not be able to get an user by ID that does exists', async () => {
const getUserByIdResponse = await request(app.server)
.get(`/user/${randomUUID()}`)
.set('Authorization', `Bearer ${userAuth.token}`)

expect(getUserByIdResponse.statusCode).toEqual(404)
expect(getUserByIdResponse.body.user).toEqual(expect.objectContaining({}))
})

it('should not be able to get an user requesting with id that is not uuid', async () => {
const id = 'id_not_uuid'

const getUserByIdResponse = await request(app.server)
.get(`/user/${id}`)
.set('Authorization', `Bearer ${userAuth.token}`)

expect(getUserByIdResponse.statusCode).toEqual(400)
it('should not be able to get an user without authenticate', async () => {
const getUserByIdResponse = await request(app.server).get(`/user`)

expect(getUserByIdResponse.statusCode).toEqual(401)
expect(getUserByIdResponse.body).toEqual(
expect.objectContaining({
message: expect.any(String),
issues: expect.any(Object),
}),
expect.objectContaining({ message: 'Unauthorized' }),
)
})
})
8 changes: 1 addition & 7 deletions src/controller/user/getUserById.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,15 +11,9 @@ export async function getUserById(
const userRepository = new PrismaUsersRepository()
const getUserByIdUseCase = new GetUserByIdUseCase(userRepository)

const getUserByIdBodySchema = z.object({
id: z.string().uuid(),
})

const { id } = getUserByIdBodySchema.parse(request.params)

try {
const { user } = await getUserByIdUseCase.execute({
id,
id: request.user.sub,
})
return response
.status(200)
Expand Down
3 changes: 1 addition & 2 deletions src/controller/user/routes.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,7 @@ export async function userRoutes(app: FastifyInstance) {
},
})
app.post('/user', registerUser)
app.get('/user/:id', { onRequest: verifyJWT }, getUserById)
app.get('/user', { onRequest: verifyJWT }, getUserByEmail)
app.get('/user', { onRequest: verifyJWT }, getUserById)
app.put('/user/:userId/edit', { onRequest: verifyJWT }, editUserById)
app.post('/user/:userId/photo', { onRequest: verifyJWT }, addImageUser)
}

0 comments on commit 946c7bf

Please sign in to comment.