diff --git a/src/controller/user/registerUser.spec.ts b/src/controller/user/registerUser.spec.ts index 511d20b..5c36c29 100644 --- a/src/controller/user/registerUser.spec.ts +++ b/src/controller/user/registerUser.spec.ts @@ -22,12 +22,44 @@ describe('Register User E2E', () => { const name = 'John' const surname = 'Doe' const password = 'password' + const avatar_url = 'avatar_url' + + const is_google = true const registerUserResponse = await request(app.server).post('/user').send({ email, name, surname, password, + avatar_url, + is_google, + }) + + const userFoundedDb = await userRepository.findByEmail(email) + + expect(registerUserResponse.statusCode).toEqual(201) + expect(userFoundedDb).not.toBeNull() + expect(userFoundedDb).toEqual( + expect.objectContaining({ + email, + avatar_url, + is_google, + }), + ) + + const passwordMatches = await compare(password, userFoundedDb.password_hash) + expect(passwordMatches).toEqual(true) + }) + + it('should be able to register a new user without avatar_url and without surname', async () => { + const email = 'john_doe2@email.com' + const name = 'John' + const password = 'password' + + const registerUserResponse = await request(app.server).post('/user').send({ + email, + name, + password, }) const userFoundedDb = await userRepository.findByEmail(email) @@ -37,6 +69,9 @@ describe('Register User E2E', () => { expect(userFoundedDb).toEqual( expect.objectContaining({ email, + is_google: false, + avatar_url: + 'https://orangeapp-contents-prod.s3.amazonaws.com/avatar1.png', }), ) diff --git a/src/controller/user/registerUser.ts b/src/controller/user/registerUser.ts index c622188..2bf3a56 100644 --- a/src/controller/user/registerUser.ts +++ b/src/controller/user/registerUser.ts @@ -14,12 +14,11 @@ export async function registerUser( email: z.string().email(), password: z.string().min(6), avatar_url: z.string().optional(), - is_google: z.boolean().optional() + is_google: z.boolean().optional(), }) - const { name, surname, email, password, avatar_url, is_google } = registerBodySchema.parse( - request.body, - ) + const { name, surname, email, password, avatar_url, is_google } = + registerBodySchema.parse(request.body) const usersRepository = new PrismaUsersRepository() const createUserUseCase = new CreateUserUseCase(usersRepository) diff --git a/src/repositories/in-memory-db/inMemoryUserRepository.ts b/src/repositories/in-memory-db/inMemoryUserRepository.ts index 2b8ce67..cc84c22 100644 --- a/src/repositories/in-memory-db/inMemoryUserRepository.ts +++ b/src/repositories/in-memory-db/inMemoryUserRepository.ts @@ -33,6 +33,8 @@ export class InMemoryUserRepository implements UserRepository { email, password_hash, country, + avatar_url, + is_google, }: Prisma.UserCreateInput) { const user: User = { id: id === undefined ? randomUUID() : id, @@ -44,7 +46,10 @@ export class InMemoryUserRepository implements UserRepository { created_at: new Date(), updated_at: new Date(), - avatar_url: null, + avatar_url: + avatar_url || + 'https://orangeapp-contents-prod.s3.amazonaws.com/avatar1.png', + is_google: is_google || false, country: country || 'brasil', } @@ -52,7 +57,6 @@ export class InMemoryUserRepository implements UserRepository { return user } - async edit({ name, surname, @@ -70,6 +74,7 @@ export class InMemoryUserRepository implements UserRepository { return this.db[indexToUpdate] } + async addPhotoUrl(projectId: string, photoUrl: string): Promise { throw new Error('Method not implemented.') } diff --git a/src/use-cases/user/createUserUseCase.spec.ts b/src/use-cases/user/createUserUseCase.spec.ts index 446565f..136f6ea 100644 --- a/src/use-cases/user/createUserUseCase.spec.ts +++ b/src/use-cases/user/createUserUseCase.spec.ts @@ -1,34 +1,48 @@ -import { expect, describe, it, beforeEach } from 'vitest'; -import { CreateUserUseCase } from '../user/createUserUseCase'; -import { compare } from 'bcryptjs'; -import { InMemoryUserRepository } from '../../repositories/in-memory-db/inMemoryUserRepository'; -import { UserAlreadyExistsError } from '../errors/user-already-exists-error'; -import { UserRepository } from '../../repositories/user-repository'; +import { expect, describe, it, beforeEach } from 'vitest' +import { CreateUserUseCase } from '../user/createUserUseCase' +import { compare } from 'bcryptjs' +import { InMemoryUserRepository } from '../../repositories/in-memory-db/inMemoryUserRepository' +import { UserAlreadyExistsError } from '../errors/user-already-exists-error' +import { UserRepository } from '../../repositories/user-repository' -let usersRepository: UserRepository; -let createUserUseCase: CreateUserUseCase; +let usersRepository: UserRepository +let createUserUseCase: CreateUserUseCase describe('Register Use Case', () => { - beforeEach(() => { usersRepository = new InMemoryUserRepository() createUserUseCase = new CreateUserUseCase(usersRepository) }) - - it('should be able to register', async () => { + it('should be able to register', async () => { const { user } = await createUserUseCase.execute({ name: 'John', surname: 'Doe', email: 'johndoe@email.com', password: '123456', + avatar_url: 'avatar_url', + is_google: true, }) expect(user.email).toEqual('johndoe@email.com') + expect(user.is_google).toEqual(true) + }) + + it('should be able to register a user without avatar_url and without surname ', async () => { + const { user } = await createUserUseCase.execute({ + name: 'John', + email: 'johndoe@email.com', + password: '123456', + }) + + expect(user.email).toEqual('johndoe@email.com') + expect(user.is_google).toEqual(false) + expect(user.avatar_url).toEqual( + 'https://orangeapp-contents-prod.s3.amazonaws.com/avatar1.png', + ) }) it('should hash user password upon registration', async () => { - const { user } = await createUserUseCase.execute({ name: 'John', surname: 'Doe', @@ -45,7 +59,6 @@ describe('Register Use Case', () => { }) it('should not be able to register with same email twice', async () => { - const email = 'johndoe@email.com' await createUserUseCase.execute({ @@ -63,7 +76,5 @@ describe('Register Use Case', () => { password: '123456', }), ).rejects.toBeInstanceOf(UserAlreadyExistsError) - }) - }) diff --git a/src/use-cases/user/createUserUseCase.ts b/src/use-cases/user/createUserUseCase.ts index 8bde357..73ce73a 100644 --- a/src/use-cases/user/createUserUseCase.ts +++ b/src/use-cases/user/createUserUseCase.ts @@ -19,17 +19,16 @@ interface CreateUserUseCaseResponse { export class CreateUserUseCase { constructor(private usersRepository: UserRepository) {} - async execute({ - name, - surname, - email, - password, + async execute({ + name, + surname, + email, + password, avatar_url, - is_google + is_google, }: CreateUserUseCaseRequest): Promise { - const userWithSameEmail = await this.usersRepository.findByEmail(email) - + if (userWithSameEmail) { throw new UserAlreadyExistsError() } @@ -41,15 +40,11 @@ export class CreateUserUseCase { email, password_hash, avatar_url, - is_google + is_google, }) - + return { user, } - } } - - -