Skip to content

Commit

Permalink
Merge pull request #14 from MatheusSanchez/SFD-43-e2e-tests-get-user-…
Browse files Browse the repository at this point in the history
…routes

E2E tests get user routes
  • Loading branch information
MatheusSanchez authored Jan 26, 2024
2 parents f30daa9 + 18e692f commit a20e0a1
Show file tree
Hide file tree
Showing 7 changed files with 293 additions and 14 deletions.
2 changes: 2 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -20,10 +20,12 @@
"devDependencies": {
"@rocketseat/eslint-config": "^2.1.0",
"@types/node": "^20.11.6",
"@types/supertest": "^6.0.2",
"@vitest/coverage-v8": "^1.2.1",
"eslint": "8.52.0",
"npm-run-all": "^4.1.5",
"prisma": "5.8.1",
"supertest": "^6.3.4",
"tsup": "^8.0.1",
"tsx": "^4.7.0",
"typescript": "^5.3.3",
Expand Down
149 changes: 149 additions & 0 deletions pnpm-lock.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

62 changes: 60 additions & 2 deletions src/controller/user/getUserByEmail.spec.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,61 @@
import { it } from 'vitest'
import { afterAll, beforeAll, describe, expect, test } from 'vitest'
import request from 'supertest'
import { app } from '../../app'
import { compare } from 'bcryptjs'

it('ok', () => {})
describe('Get User By email E2E', () => {
beforeAll(async () => {
await app.ready()
})

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

test('should be able to get an user by e-mail', async () => {
const email = '[email protected]'
const name = 'John'
const surname = 'Doe'
const password = 'password'

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

const getUserByEmailResponse = await request(app.server)
.get(`/user`)
.query({ email })

expect(getUserByEmailResponse.statusCode).toEqual(200)
expect(getUserByEmailResponse.body.user).toEqual(
expect.objectContaining({
email,
name,
surname,
password_hash: expect.any(String),
}),
)

const passwordMatches = await compare(
password,
getUserByEmailResponse.body.user.password_hash,
)
expect(passwordMatches).toEqual(true)
})

test('should not be able to get an user by e-mail that does not exist', async () => {
const email = '[email protected]'

const getUserByEmailResponse = await request(app.server)
.get(`/user`)
.query({ email })

expect(getUserByEmailResponse.statusCode).toEqual(200)
expect(getUserByEmailResponse.body.user).toEqual(
expect.objectContaining({}),
)
})
})
5 changes: 2 additions & 3 deletions src/controller/user/getUserByEmail.ts
Original file line number Diff line number Diff line change
@@ -1,14 +1,13 @@
import { FastifyReply, FastifyRequest } from 'fastify'
import { InMemoryUserRepository } from '../../repositories/in-memory-db/inMemoryUserRepository'
import { z } from 'zod'
import { GetUserByEmailUseCase } from '../../use-cases/getUserByEmailUseCase'
import { PrismaUsersRepository } from '../../repositories/prisma/prisma-users-repository'

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

const userRepository = new InMemoryUserRepository()
const userRepository = new PrismaUsersRepository()
const getUserByEmailUseCase = new GetUserByEmailUseCase(userRepository)

const getUserByEmailBodySchema = z.object({
Expand Down
Loading

0 comments on commit a20e0a1

Please sign in to comment.