Skip to content

Commit

Permalink
getProjectsByUserId auth
Browse files Browse the repository at this point in the history
  • Loading branch information
MatheusSanchez committed Feb 3, 2024
1 parent 86be9d2 commit 627c23c
Show file tree
Hide file tree
Showing 3 changed files with 32 additions and 7 deletions.
16 changes: 10 additions & 6 deletions src/controller/project/getProjectsByUserId.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import { PrismaProjectRepository } from '../../repositories/prisma/prisma-projec
import { PrismaUsersRepository } from '../../repositories/prisma/prisma-users-repository'
import { UserRepository } from '../../repositories/user-repository'
import { randomUUID } from 'crypto'
import { createAndAuthenticateUser } from '../../utils/create-and-authenticate-user'

let projectRepository: ProjectRepository
let userRepository: UserRepository
Expand All @@ -23,6 +24,8 @@ describe('Get Projets By UserId E2E', () => {
})

it('should be able to get all projects from an user', async () => {
const { token } = await createAndAuthenticateUser(app)

const description = 'ReactProject'
const link = 'www.google.com.br'
const tags = ['react', 'node']
Expand Down Expand Up @@ -53,9 +56,9 @@ describe('Get Projets By UserId E2E', () => {
user_id: newUser.id,
})

const getProjectsByUserIdResponse = await request(app.server).get(
`/projects/${newUser.id}`,
)
const getProjectsByUserIdResponse = await request(app.server)
.get(`/projects/${newUser.id}`)
.set('Authorization', `Bearer ${token}`)

expect(getProjectsByUserIdResponse.statusCode).toEqual(200)
expect(getProjectsByUserIdResponse.body.projects).toHaveLength(2)
Expand All @@ -69,9 +72,10 @@ describe('Get Projets By UserId E2E', () => {
})

it('should not be able to project that user does not exist', async () => {
const getProjectsByUserIdResponse = await request(app.server).get(
`/projects/${randomUUID()}`,
)
const { token } = await createAndAuthenticateUser(app)
const getProjectsByUserIdResponse = await request(app.server)
.get(`/projects/${randomUUID()}`)
.set('Authorization', `Bearer ${token}`)

expect(getProjectsByUserIdResponse.statusCode).toEqual(404)

Expand Down
3 changes: 2 additions & 1 deletion src/controller/project/routes.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import fastifyStatic from '@fastify/static'
import { getProjectsByTags } from './getProjectsByTags'
import { editProject } from './editProjectById'
import { deleteProjectById } from './deleteProjectById'
import { verifyJWT } from '../middlewares/verifyJwt'

export async function projectRoutes(app: FastifyInstance) {
app.register(FastifyMultipart, {
Expand All @@ -24,7 +25,7 @@ export async function projectRoutes(app: FastifyInstance) {
})

app.post('/projects/tags', getProjectsByTags)
app.get('/projects/:userId', getProjectsByUserId)
app.get('/projects/:userId', { onRequest: verifyJWT }, getProjectsByUserId)
app.get('/project/:projectId', getProjectsById)

app.post('/project/:projectId/photo', addImageProject)
Expand Down
20 changes: 20 additions & 0 deletions src/utils/create-and-authenticate-user.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
import { FastifyInstance } from 'fastify/types/instance'
import request from 'supertest'

export async function createAndAuthenticateUser(app: FastifyInstance) {
await request(app.server).post('/user').send({
name: 'John',
surname: 'Doe',
email: '[email protected]',
password: '12345678',
})

const authResponse = await request(app.server).post('/login').send({
email: '[email protected]',
password: '12345678',
})

const { token } = authResponse.body

return { token }
}

0 comments on commit 627c23c

Please sign in to comment.