From 946c7bfa2969574c61e56b412ce8c91c47d0de47 Mon Sep 17 00:00:00 2001 From: Matheus Sanchez Date: Sun, 4 Feb 2024 15:01:07 -0300 Subject: [PATCH] Removing getUserByEmail and removing id as query param. We now can use the jwt token request.user.sub --- src/controller/user/getUserById.spec.ts | 41 ++++++------------------- src/controller/user/getUserById.ts | 8 +---- src/controller/user/routes.ts | 3 +- 3 files changed, 12 insertions(+), 40 deletions(-) diff --git a/src/controller/user/getUserById.spec.ts b/src/controller/user/getUserById.spec.ts index f552486..75c9ee1 100644 --- a/src/controller/user/getUserById.spec.ts +++ b/src/controller/user/getUserById.spec.ts @@ -2,17 +2,10 @@ 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 () => { @@ -20,42 +13,28 @@ describe('Get User By Id E2E', () => { }) 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: 'johndoe@example.com', }), ) }) - 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' }), ) }) }) diff --git a/src/controller/user/getUserById.ts b/src/controller/user/getUserById.ts index 7b4f6f0..3550ebd 100644 --- a/src/controller/user/getUserById.ts +++ b/src/controller/user/getUserById.ts @@ -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) diff --git a/src/controller/user/routes.ts b/src/controller/user/routes.ts index ef5ed66..6d72d85 100644 --- a/src/controller/user/routes.ts +++ b/src/controller/user/routes.ts @@ -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) }