Skip to content

Commit

Permalink
test create user without some datas
Browse files Browse the repository at this point in the history
  • Loading branch information
MatheusSanchez committed Feb 4, 2024
1 parent d72bb07 commit 2fa7f01
Show file tree
Hide file tree
Showing 5 changed files with 80 additions and 35 deletions.
35 changes: 35 additions & 0 deletions src/controller/user/registerUser.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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 = '[email protected]'
const name = 'John'
const password = 'password'

const registerUserResponse = await request(app.server).post('/user').send({
email,
name,
password,
})

const userFoundedDb = await userRepository.findByEmail(email)
Expand All @@ -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',
}),
)

Expand Down
7 changes: 3 additions & 4 deletions src/controller/user/registerUser.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
9 changes: 7 additions & 2 deletions src/repositories/in-memory-db/inMemoryUserRepository.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand All @@ -44,15 +46,17 @@ 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',
}

this.db.push(user)
return user
}


async edit({
name,
surname,
Expand All @@ -70,6 +74,7 @@ export class InMemoryUserRepository implements UserRepository {

return this.db[indexToUpdate]
}

async addPhotoUrl(projectId: string, photoUrl: string): Promise<Project> {
throw new Error('Method not implemented.')
}
Expand Down
41 changes: 26 additions & 15 deletions src/use-cases/user/createUserUseCase.spec.ts
Original file line number Diff line number Diff line change
@@ -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: '[email protected]',
password: '123456',
avatar_url: 'avatar_url',
is_google: true,
})

expect(user.email).toEqual('[email protected]')
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: '[email protected]',
password: '123456',
})

expect(user.email).toEqual('[email protected]')
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',
Expand All @@ -45,7 +59,6 @@ describe('Register Use Case', () => {
})

it('should not be able to register with same email twice', async () => {

const email = '[email protected]'

await createUserUseCase.execute({
Expand All @@ -63,7 +76,5 @@ describe('Register Use Case', () => {
password: '123456',
}),
).rejects.toBeInstanceOf(UserAlreadyExistsError)

})

})
23 changes: 9 additions & 14 deletions src/use-cases/user/createUserUseCase.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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<CreateUserUseCaseResponse> {

const userWithSameEmail = await this.usersRepository.findByEmail(email)

if (userWithSameEmail) {
throw new UserAlreadyExistsError()
}
Expand All @@ -41,15 +40,11 @@ export class CreateUserUseCase {
email,
password_hash,
avatar_url,
is_google
is_google,
})

return {
user,
}

}
}



0 comments on commit 2fa7f01

Please sign in to comment.