Skip to content

Commit

Permalink
get user by id e2e
Browse files Browse the repository at this point in the history
  • Loading branch information
MatheusSanchez committed Jan 26, 2024
1 parent 6535bbe commit 18e692f
Show file tree
Hide file tree
Showing 3 changed files with 80 additions and 9 deletions.
60 changes: 60 additions & 0 deletions src/controller/user/getUserById.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
import { afterAll, beforeAll, describe, expect, it } from 'vitest'
import request from 'supertest'
import { app } from '../../app'
import { PrismaUsersRepository } from '../../repositories/prisma/prisma-users-repository'
import { UserRepository } from '../../repositories/user-repository'

let userRepository: UserRepository

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

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

it('should be able to get an user by ID', async () => {
const id = '9600de4f-8d18-4e69-ba7a-ed7fa210618d'
const email = '[email protected]'
const name = 'John'
const surname = 'Doe'
const password_hash = '9600de4f-8d18-4e69-ba7a-ed7fa210618d'

await userRepository.create({ email, id, name, surname, password_hash })

const getUserByIdResponse = await request(app.server).get(`/user/${id}`)

expect(getUserByIdResponse.statusCode).toEqual(200)
expect(getUserByIdResponse.body.user).toEqual(
expect.objectContaining({
id,
}),
)
})

it('should not be able to get an user by ID that does exists', async () => {
const id = '9999de4f-8d18-4e69-ba7a-ed7fa210618d'

const getUserByIdResponse = await request(app.server).get(`/user/${id}`)

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}`)
expect(getUserByIdResponse.statusCode).toEqual(400)

expect(getUserByIdResponse.body).toEqual(
expect.objectContaining({
message: expect.any(String),
issues: expect.any(Object),
}),
)
})
})
21 changes: 13 additions & 8 deletions src/controller/user/getUserById.ts
Original file line number Diff line number Diff line change
@@ -1,14 +1,14 @@
import { FastifyReply, FastifyRequest } from 'fastify'
import { InMemoryUserRepository } from '../../repositories/in-memory-db/inMemoryUserRepository'
import { z } from 'zod'
import { GetUserByIdUseCase } from '../../use-cases/getUserByIdUseCase'
import { PrismaUsersRepository } from '../../repositories/prisma/prisma-users-repository'
import { ResourceNotFoundError } from '../../use-cases/errors/ResourceNotFoundError'

export async function getUserById(
request: FastifyRequest,
response: FastifyReply,
) {

const userRepository = new InMemoryUserRepository()
const userRepository = new PrismaUsersRepository()
const getUserByIdUseCase = new GetUserByIdUseCase(userRepository)

const getUserByIdBodySchema = z.object({
Expand All @@ -17,9 +17,14 @@ export async function getUserById(

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

const { user } = await getUserByIdUseCase.execute({
id,
})

return response.status(200).send({ user })
try {
const { user } = await getUserByIdUseCase.execute({
id,
})
return response.status(200).send({ user })
} catch (error) {
if (error instanceof ResourceNotFoundError) {
return response.status(404).send()
}
}
}
8 changes: 7 additions & 1 deletion src/repositories/prisma/prisma-users-repository.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,13 @@ export class PrismaUsersRepository implements UserRepository {
}

async findById(id: string): Promise<User | null> {
throw new Error('Method not implemented.')
const user = await prisma.user.findUnique({
where: {
id,
},
})

return user
}

async create(data: Prisma.UserCreateInput): Promise<User> {
Expand Down

0 comments on commit 18e692f

Please sign in to comment.