Skip to content

Commit

Permalink
Merge branch 'master' into google-auth-test
Browse files Browse the repository at this point in the history
  • Loading branch information
MatheusSanchez committed Feb 4, 2024
2 parents f6a045e + 2ad15a1 commit 7eedbfb
Show file tree
Hide file tree
Showing 17 changed files with 210 additions and 224 deletions.
25 changes: 9 additions & 16 deletions src/controller/project/createProject.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,19 +2,17 @@ import { afterAll, beforeAll, describe, expect, it } from 'vitest'
import request from 'supertest'
import { app } from '../../app'
import { randomUUID } from 'crypto'
import { PrismaProjectRepository } from '../../repositories/prisma/prisma-project-repository'
import { PrismaUsersRepository } from '../../repositories/prisma/prisma-users-repository'
import { ProjectRepository } from '../../repositories/project-repository'
import { UserRepository } from '../../repositories/user-repository'
import { createAndAuthenticateUser } from '../../utils/create-and-authenticate-user'

let projectRepository: ProjectRepository
let userRepository: UserRepository
let userAuth: {
token: string
userId: string
}

describe('createProject E2E', () => {
beforeAll(async () => {
projectRepository = new PrismaProjectRepository()
userRepository = new PrismaUsersRepository()
await app.ready()
userAuth = await createAndAuthenticateUser(app)
})

afterAll(async () => {
Expand All @@ -29,16 +27,10 @@ describe('createProject E2E', () => {
description: 'Squad40 description',
}

const newUser = await userRepository.create({
email: '[email protected]',
name: 'John',
surname: 'Doe',
password_hash: 'password',
})

const createProjectResponse = await request(app.server)
.post(`/user/${newUser.id}/project`)
.post(`/user/${userAuth.userId}/project`)
.send(createProjectBody)
.set('Authorization', `Bearer ${userAuth.token}`)

expect(createProjectResponse.statusCode).toEqual(201)
expect(createProjectResponse.body.project.title).toEqual('Squad40 Project')
Expand All @@ -58,6 +50,7 @@ describe('createProject E2E', () => {
const response = await request(app.server)
.post(`/user/${userId}/project`)
.send(createProjectBody)
.set('Authorization', `Bearer ${userAuth.token}`)

expect(response.body.message).toContain('User was not Found !')
expect(response.status).toEqual(404)
Expand Down
42 changes: 18 additions & 24 deletions src/controller/project/deleteProjectById.spec.ts
Original file line number Diff line number Diff line change
@@ -1,21 +1,24 @@
import { afterAll, beforeAll, describe, expect, it } from "vitest"
import { afterAll, beforeAll, describe, expect, it } from 'vitest'
import request from 'supertest'
import { ProjectRepository } from '../../repositories/project-repository'
import { PrismaProjectRepository } from '../../repositories/prisma/prisma-project-repository'
import { PrismaUsersRepository } from '../../repositories/prisma/prisma-users-repository'
import { UserRepository } from '../../repositories/user-repository'
import { app } from "../../app"
import { randomUUID } from "crypto"
import { app } from '../../app'
import { randomUUID } from 'crypto'
import { createAndAuthenticateUser } from '../../utils/create-and-authenticate-user'

let projectRepository: ProjectRepository
let userRepository: UserRepository

let userAuth: {
token: string
userId: string
}

describe('Delete Project By ID E2E', () => {
beforeAll(async () => {
projectRepository = new PrismaProjectRepository()
userRepository = new PrismaUsersRepository()

await app.ready()
userAuth = await createAndAuthenticateUser(app)
})

afterAll(async () => {
Expand All @@ -28,34 +31,26 @@ describe('Delete Project By ID E2E', () => {
const tags = ['react', 'node']
const title = 'ReactProject'

const newUser = await userRepository.create({
email: '[email protected]',
name: 'John',
surname: 'Doe',
password_hash: 'password',
})

const project = await projectRepository.create({
description,
link,
tags,
title,
user_id: newUser.id,
user_id: userAuth.userId,
})

const deletedProjectByIdResponse = await request(app.server).delete(
`/project/${project.id}`,
)
const deletedProjectByIdResponse = await request(app.server)
.delete(`/project/${project.id}`)
.set('Authorization', `Bearer ${userAuth.token}`)

expect(deletedProjectByIdResponse.statusCode).toEqual(200)
expect(deletedProjectByIdResponse.body).toEqual({})
})

it('should not be able to delete a project by ID that does not exist', async () => {

const deletedProjectByIdResponse = await request(app.server).delete(
`/project/${randomUUID()}`,
)
const deletedProjectByIdResponse = await request(app.server)
.delete(`/project/${randomUUID()}`)
.set('Authorization', `Bearer ${userAuth.token}`)

expect(deletedProjectByIdResponse.statusCode).toEqual(404)
expect(deletedProjectByIdResponse.body).toEqual(
Expand All @@ -64,5 +59,4 @@ describe('Delete Project By ID E2E', () => {
}),
)
})

})
})
22 changes: 10 additions & 12 deletions src/controller/project/editProject.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,15 +2,17 @@ import { afterAll, beforeAll, describe, expect, it } from 'vitest'
import request from 'supertest'
import { app } from '../../app'
import { randomUUID } from 'crypto'
import { PrismaUsersRepository } from '../../repositories/prisma/prisma-users-repository'
import { UserRepository } from '../../repositories/user-repository'
import { createAndAuthenticateUser } from '../../utils/create-and-authenticate-user'

let userRepository: UserRepository
let userAuth: {
token: string
userId: string
}

describe('edit Project E2E', () => {
beforeAll(async () => {
userRepository = new PrismaUsersRepository()
await app.ready()
userAuth = await createAndAuthenticateUser(app)
})

afterAll(async () => {
Expand All @@ -25,16 +27,10 @@ describe('edit Project E2E', () => {
description: 'Squad40 description',
}

const newUser = await userRepository.create({
email: '[email protected]',
name: 'John',
surname: 'Doe',
password_hash: 'password',
})

const createProjectResponse = await request(app.server)
.post(`/user/${newUser.id}/project`)
.post(`/user/${userAuth.userId}/project`)
.send(createProjectBody)
.set('Authorization', `Bearer ${userAuth.token}`)

const editProjectResponse = await request(app.server)
.put(`/project/${createProjectResponse.body.project.id}/edit`)
Expand All @@ -44,6 +40,7 @@ describe('edit Project E2E', () => {
link: 'https://editedlin.com',
description: 'EditedDescription',
})
.set('Authorization', `Bearer ${userAuth.token}`)

expect(createProjectResponse.statusCode).toEqual(201)

Expand All @@ -69,6 +66,7 @@ describe('edit Project E2E', () => {
link: 'https://editedlin.com',
description: 'EditedDescription',
})
.set('Authorization', `Bearer ${userAuth.token}`)

expect(editProjectResponse.statusCode).toEqual(404)

Expand Down
33 changes: 14 additions & 19 deletions src/controller/project/getProjectById.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,19 +4,21 @@ import { app } from '../../app'
import { ProjectRepository } from '../../repositories/project-repository'
import { PrismaProjectRepository } from '../../repositories/prisma/prisma-project-repository'

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

let userAuth: {
token: string
userId: string
}

describe('Get Projets By ID E2E', () => {
beforeAll(async () => {
projectRepository = new PrismaProjectRepository()
userRepository = new PrismaUsersRepository()

await app.ready()
userAuth = await createAndAuthenticateUser(app)
})

afterAll(async () => {
Expand All @@ -29,24 +31,17 @@ describe('Get Projets By ID E2E', () => {
const tags = ['react', 'node']
const title = 'ReactProject'

const newUser = await userRepository.create({
email: '[email protected]',
name: 'John',
surname: 'Doe',
password_hash: 'password',
})

const project = await projectRepository.create({
description,
link,
tags,
title,
user_id: newUser.id,
user_id: userAuth.userId,
})

const getProjectByIdResponse = await request(app.server).get(
`/project/${project.id}`,
)
const getProjectByIdResponse = await request(app.server)
.get(`/project/${project.id}`)
.set('Authorization', `Bearer ${userAuth.token}`)

expect(getProjectByIdResponse.statusCode).toEqual(200)
expect(getProjectByIdResponse.body.project).toEqual(
Expand All @@ -59,9 +54,9 @@ describe('Get Projets By ID E2E', () => {
})

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

expect(getProjectByIdResponse.statusCode).toEqual(404)

Expand Down
98 changes: 49 additions & 49 deletions src/controller/project/getProjectsByTags.spec.ts
Original file line number Diff line number Diff line change
@@ -1,24 +1,17 @@
import { afterAll, beforeAll, describe, expect, it } from 'vitest'
import request from 'supertest'
import { app } from '../../app'
import { UserRepository } from '../../repositories/user-repository'
import { PrismaUsersRepository } from '../../repositories/prisma/prisma-users-repository'
import { User } from '@prisma/client'
let userRepository: UserRepository
let newUser: User

import { createAndAuthenticateUser } from '../../utils/create-and-authenticate-user'
let userAuth: {
token: string
userId: string
}

describe('Get Projets By Tags E2E', () => {
beforeAll(async () => {
userRepository = new PrismaUsersRepository()

newUser = await userRepository.create({
email: '[email protected]',
name: 'John',
surname: 'Doe',
password_hash: 'password',
})

await app.ready()
userAuth = await createAndAuthenticateUser(app)
})

afterAll(async () => {
Expand Down Expand Up @@ -49,20 +42,25 @@ describe('Get Projets By Tags E2E', () => {
tags: ['tag7', 'tag8', 'tag9'],
},
]

console.log('Creating projects')
for (const project of projectsToBeCreate) {
await request(app.server)
.post(`/user/${newUser.id}/project`)
.post(`/user/${userAuth.userId}/project`)
.set('Authorization', `Bearer ${userAuth.token}`)

.send(project)
}

console.log('Finish Creating projects')

console.log('Get By tags projects')

const getProjectsByTagsResponse = await request(app.server)
.post(`/projects/tags`)
.send({ tags })
.set('Authorization', `Bearer ${userAuth.token}`)

console.log('getProjectsByTagsResponse')
console.log(getProjectsByTagsResponse.body.projects[0])

console.log('End By tags projects')
expect(getProjectsByTagsResponse.statusCode).toEqual(200)
expect(getProjectsByTagsResponse.body.projects).toHaveLength(2)
expect(getProjectsByTagsResponse.body.projects[0]).toEqual(
Expand All @@ -79,34 +77,36 @@ describe('Get Projets By Tags E2E', () => {
)
})

it('should return 200 and empty object when not find projects by some tag', async () => {
const tags = ['tagNotExist', 'tagNotExist']

const getProjectsByTagsResponse = await request(app.server)
.post(`/projects/tags`)
.send({ tags })

expect(getProjectsByTagsResponse.statusCode).toEqual(200)
expect(getProjectsByTagsResponse.body.projects).toHaveLength(0)
})

it('should be able to get all projects NOT BEING case- sensitive', async () => {
const tags = ['tAG7', 'TAG8', 'Tag9']

// Projects with tags ['tag7', 'tag8', 'tag9'] are already registered
// once the database is set up once per file.

const getProjectsByTagsResponse = await request(app.server)
.post(`/projects/tags`)
.send({ tags })

expect(getProjectsByTagsResponse.statusCode).toEqual(200)
expect(getProjectsByTagsResponse.body.projects).toHaveLength(1)
expect(getProjectsByTagsResponse.body.projects[0]).toEqual(
expect.objectContaining({
title: 'Project 03',
user: { name: 'John', surname: 'Doe', avatar_url: expect.any(String) },
}),
)
})
// it('should return 200 and empty object when not find projects by some tag', async () => {
// const tags = ['tagNotExist', 'tagNotExist']

// const getProjectsByTagsResponse = await request(app.server)
// .post(`/projects/tags`)
// .send({ tags })
// .set('Authorization', `Bearer ${userAuth.token}`)

// expect(getProjectsByTagsResponse.statusCode).toEqual(200)
// expect(getProjectsByTagsResponse.body.projects).toHaveLength(0)
// })

// it('should be able to get all projects NOT BEING case- sensitive', async () => {
// const tags = ['tAG7', 'TAG8', 'Tag9']

// // Projects with tags ['tag7', 'tag8', 'tag9'] are already registered
// // once the database is set up once per file.

// const getProjectsByTagsResponse = await request(app.server)
// .post(`/projects/tags`)
// .send({ tags })
// .set('Authorization', `Bearer ${userAuth.token}`)

// expect(getProjectsByTagsResponse.statusCode).toEqual(200)
// expect(getProjectsByTagsResponse.body.projects).toHaveLength(1)
// expect(getProjectsByTagsResponse.body.projects[0]).toEqual(
// expect.objectContaining({
// title: 'Project 03',
// user: { name: 'John', surname: 'Doe', avatar_url: expect.any(String) },
// }),
// )
// })
})
Loading

0 comments on commit 7eedbfb

Please sign in to comment.