-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #45 from MatheusSanchez/edit-user
Edit feature user and tests
- Loading branch information
Showing
8 changed files
with
255 additions
and
4 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,71 @@ | ||
import { afterAll, beforeAll, describe, expect, it } from 'vitest' | ||
import request from 'supertest' | ||
import { app } from '../../app' | ||
import { randomUUID } from 'crypto' | ||
import { PrismaUsersRepository } from '../../repositories/prisma/prisma-users-repository' | ||
import { UserRepository } from '../../repositories/user-repository' | ||
|
||
let userRepository: UserRepository | ||
|
||
describe('edit User E2E', () => { | ||
beforeAll(async () => { | ||
userRepository = new PrismaUsersRepository() | ||
await app.ready() | ||
}) | ||
|
||
afterAll(async () => { | ||
await app.close() | ||
}) | ||
|
||
it('should be able to edit a user', async () => { | ||
const email = '[email protected]' | ||
const name = 'John' | ||
const surname = 'Doe' | ||
const password_hash = 'password_hash' | ||
|
||
const newUser = await userRepository.create({ | ||
email, | ||
name, | ||
surname, | ||
password_hash, | ||
}) | ||
|
||
const editUserResponse = await request(app.server) | ||
.put(`/user/${newUser.id}/edit`) | ||
.send({ | ||
name: 'newName', | ||
surname: 'surname', | ||
country: 'country', | ||
}) | ||
|
||
expect(editUserResponse.statusCode).toEqual(200) | ||
expect(editUserResponse.body.user).toEqual( | ||
expect.objectContaining({ | ||
name: 'newName', | ||
surname: 'surname', | ||
country: 'country', | ||
id: newUser.id, | ||
email, | ||
password_hash, | ||
}), | ||
) | ||
}) | ||
|
||
it('should not be able to edit a user that does not exist', async () => { | ||
const editUserResponse = await request(app.server) | ||
.put(`/user/${randomUUID()}/edit`) | ||
.send({ | ||
name: 'newName', | ||
surname: 'surname', | ||
country: 'country', | ||
}) | ||
|
||
expect(editUserResponse.statusCode).toEqual(404) | ||
|
||
expect(editUserResponse.body).toEqual( | ||
expect.objectContaining({ | ||
error: 'User was not Found !', | ||
}), | ||
) | ||
}) | ||
}) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,41 @@ | ||
import { FastifyReply, FastifyRequest } from 'fastify' | ||
import { z } from 'zod' | ||
import { ResourceNotFoundError } from '../../use-cases/errors/ResourceNotFoundError' | ||
import { PrismaUsersRepository } from '../../repositories/prisma/prisma-users-repository' | ||
import { EditUserUseCase } from '../../use-cases/user/editUserUseCase' | ||
|
||
export async function editUserById( | ||
request: FastifyRequest, | ||
response: FastifyReply, | ||
) { | ||
const editUserBodySchema = z.object({ | ||
name: z.string(), | ||
surname: z.string(), | ||
country: z.string(), | ||
}) | ||
|
||
const editUserParamsSchema = z.object({ | ||
userId: z.string().uuid(), | ||
}) | ||
|
||
const { name, surname, country } = editUserBodySchema.parse(request.body) | ||
const { userId } = editUserParamsSchema.parse(request.params) | ||
|
||
const userRepository = new PrismaUsersRepository() | ||
const editUserUseCase = new EditUserUseCase(userRepository) | ||
|
||
try { | ||
const { user } = await editUserUseCase.execute({ | ||
name, | ||
surname, | ||
country, | ||
userId, | ||
}) | ||
|
||
return response.status(200).send({ user }) | ||
} catch (error) { | ||
if (error instanceof ResourceNotFoundError) { | ||
return response.status(404).send({ error: 'User was not Found !' }) | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,8 +1,16 @@ | ||
import { Prisma, User } from '@prisma/client' | ||
|
||
export interface editUserRequestPrisma { | ||
name: string | ||
surname: string | ||
country: string | ||
userId: string | ||
} | ||
|
||
export interface UserRepository { | ||
create(data: Prisma.UserCreateInput): Promise<User> | ||
findByEmail(email: string): Promise<User | null> | ||
findById(id: string): Promise<User | null> | ||
edit({ name, surname, country, userId }: editUserRequestPrisma): Promise<User> | ||
addPhotoUrl(userId: string, photoUrl: string): Promise<User> | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,54 @@ | ||
import { expect, describe, it, beforeEach } from 'vitest' | ||
|
||
import { ResourceNotFoundError } from '../errors/ResourceNotFoundError' | ||
import { InMemoryUserRepository } from '../../repositories/in-memory-db/inMemoryUserRepository' | ||
import { EditUserUseCase } from './editUserUseCase' | ||
|
||
let userRepository: InMemoryUserRepository | ||
|
||
let editUserUseCase: EditUserUseCase | ||
|
||
describe('Edit Project By Id Use Case', () => { | ||
beforeEach(async () => { | ||
userRepository = new InMemoryUserRepository() | ||
editUserUseCase = new EditUserUseCase(userRepository) | ||
}) | ||
|
||
it('should be able edit one user by ID', async () => { | ||
const userToBeEdited = await userRepository.create({ | ||
name: 'John', | ||
surname: 'Doe', | ||
email: '[email protected]', | ||
password_hash: '123456', | ||
}) | ||
|
||
const { user } = await editUserUseCase.execute({ | ||
name: 'newCoolName', | ||
surname: 'newSurCoolName', | ||
country: 'differentCountry', | ||
userId: userToBeEdited.id, | ||
}) | ||
|
||
expect(user).toEqual( | ||
expect.objectContaining({ | ||
name: 'newCoolName', | ||
surname: 'newSurCoolName', | ||
country: 'differentCountry', | ||
email: '[email protected]', | ||
password_hash: '123456', | ||
id: userToBeEdited.id, | ||
}), | ||
) | ||
}) | ||
|
||
it('should not be able to edit a user that does not exist', async () => { | ||
await expect(() => | ||
editUserUseCase.execute({ | ||
name: 'newCoolName', | ||
surname: 'newSurCoolName', | ||
country: 'differentCountry', | ||
userId: 'not-exist-id', | ||
}), | ||
).rejects.toBeInstanceOf(ResourceNotFoundError) | ||
}) | ||
}) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,43 @@ | ||
import { User } from '@prisma/client' | ||
|
||
import { ResourceNotFoundError } from '../errors/ResourceNotFoundError' | ||
import { UserRepository } from '../../repositories/user-repository' | ||
|
||
interface EditUserUseCaseRequest { | ||
name: string | ||
surname: string | ||
country: string | ||
userId: string | ||
} | ||
|
||
interface EditUserUseCaseResponse { | ||
user: User | ||
} | ||
|
||
export class EditUserUseCase { | ||
constructor(private userRepository: UserRepository) {} | ||
|
||
async execute({ | ||
name, | ||
surname, | ||
country, | ||
userId, | ||
}: EditUserUseCaseRequest): Promise<EditUserUseCaseResponse> { | ||
const userToBeUpdated = await this.userRepository.findById(userId) | ||
|
||
if (!userToBeUpdated) { | ||
throw new ResourceNotFoundError() | ||
} | ||
|
||
const user = await this.userRepository.edit({ | ||
name, | ||
surname, | ||
country, | ||
userId, | ||
}) | ||
|
||
return { | ||
user, | ||
} | ||
} | ||
} |