Skip to content

Commit c6ef819

Browse files
committed
fix: e2e tests and update tests util function to retrieve token
1 parent 30d3f12 commit c6ef819

7 files changed

Lines changed: 19 additions & 11 deletions

File tree

src/http/controllers/check-ins/create.spec.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ describe('Create Check-In (e2e)', () => {
1414
})
1515

1616
it('should be able to create a check-in', async () => {
17-
const { token } = await createAndAuthenticateUser(app)
17+
const { token } = await createAndAuthenticateUser(app, true)
1818

1919
const gym = await prisma.gym.create({
2020
data: {

src/http/controllers/check-ins/validate.spec.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ describe('Validate Check-In (e2e)', () => {
1414
})
1515

1616
it('should be able to validate a check-in', async () => {
17-
const { token } = await createAndAuthenticateUser(app)
17+
const { token } = await createAndAuthenticateUser(app, true)
1818

1919
const user = await prisma.user.findFirstOrThrow()
2020

src/http/controllers/gyms/create.spec.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ describe('Create Gym (e2e)', () => {
1313
})
1414

1515
it('should be able to create a gym', async () => {
16-
const { token } = await createAndAuthenticateUser(app)
16+
const { token } = await createAndAuthenticateUser(app, true)
1717

1818
const response = await request(app.server)
1919
.post('/gyms')

src/http/controllers/gyms/nearby.spec.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ describe('Nearby Gyms (e2e)', () => {
1313
})
1414

1515
it('should be able to list nearby gyms', async () => {
16-
const { token } = await createAndAuthenticateUser(app)
16+
const { token } = await createAndAuthenticateUser(app, true)
1717

1818
await request(app.server)
1919
.post('/gyms')

src/http/controllers/gyms/search.spec.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ describe('Search Gyms (e2e)', () => {
1313
})
1414

1515
it('should be able to search gyms by title', async () => {
16-
const { token } = await createAndAuthenticateUser(app)
16+
const { token } = await createAndAuthenticateUser(app, true)
1717

1818
await request(app.server)
1919
.post('/gyms')

src/http/controllers/users/profile.spec.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ describe('Profile (e2e)', () => {
1313
})
1414

1515
it('should be able to get user profile', async () => {
16-
const { token } = await createAndAuthenticateUser(app)
16+
const { token } = await createAndAuthenticateUser(app, true)
1717

1818
const profileResponse = await request(app.server)
1919
.get('/me')

src/utils/test/create-and-authenticate-user.ts

Lines changed: 13 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,19 @@
1+
import { prisma } from '@/lib/prisma'
2+
import { hash } from 'bcryptjs'
13
import { FastifyInstance } from 'fastify'
24
import request from 'supertest'
35

4-
export async function createAndAuthenticateUser(app: FastifyInstance) {
5-
await request(app.server).post('/users').send({
6-
name: 'John Doe',
7-
email: 'johndoe@example.com',
8-
password: '123456',
6+
export async function createAndAuthenticateUser(
7+
app: FastifyInstance,
8+
isAdmin = false,
9+
) {
10+
await prisma.user.create({
11+
data: {
12+
name: 'John Doe',
13+
email: 'johndoe@example.com',
14+
password_hash: await hash('123456', 6),
15+
role: isAdmin ? 'ADMIN' : 'MEMBER',
16+
},
917
})
1018

1119
const authResponse = await request(app.server).post('/sessions').send({

0 commit comments

Comments
 (0)