-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
4e6aa52
commit 707eb6a
Showing
4 changed files
with
138 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,81 @@ | ||
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' | ||
|
||
let userRepository: UserRepository | ||
|
||
describe('edit Project E2E', () => { | ||
beforeAll(async () => { | ||
userRepository = new PrismaUsersRepository() | ||
await app.ready() | ||
}) | ||
|
||
afterAll(async () => { | ||
await app.close() | ||
}) | ||
|
||
it('should be able to edit a project', async () => { | ||
const createProjectBody = { | ||
title: 'Squad40 Project', | ||
tags: ['react', 'node'], | ||
link: 'https://Squad40.com', | ||
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`) | ||
.send(createProjectBody) | ||
|
||
const editProjectResponse = await request(app.server) | ||
.put(`/project/${createProjectResponse.body.project.id}/edit`) | ||
.send({ | ||
title: 'EditedTitle', | ||
tags: ['react', 'node', 'edit'], | ||
link: 'https://editedlin.com', | ||
description: 'EditedDescription', | ||
}) | ||
|
||
expect(createProjectResponse.statusCode).toEqual(201) | ||
|
||
expect(editProjectResponse.statusCode).toEqual(200) | ||
|
||
expect(editProjectResponse.body.project.title).toEqual('EditedTitle') | ||
expect(editProjectResponse.body.project.tags).toEqual([ | ||
'react', | ||
'node', | ||
'edit', | ||
]) | ||
expect(editProjectResponse.body.project.description).toEqual( | ||
'EditedDescription', | ||
) | ||
}) | ||
|
||
it('should not be able to edit a project that does not exist', async () => { | ||
const editProjectResponse = await request(app.server) | ||
.put(`/project/${randomUUID()}/edit`) | ||
.send({ | ||
title: 'EditedTitle', | ||
tags: ['react', 'node', 'edit'], | ||
link: 'https://editedlin.com', | ||
description: 'EditedDescription', | ||
}) | ||
|
||
expect(editProjectResponse.statusCode).toEqual(404) | ||
|
||
expect(editProjectResponse.body).toEqual( | ||
expect.objectContaining({ | ||
error: 'Project was not Found !', | ||
}), | ||
) | ||
}) | ||
}) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,45 @@ | ||
import { FastifyReply, FastifyRequest } from 'fastify' | ||
import { z } from 'zod' | ||
import { PrismaProjectRepository } from '../../repositories/prisma/prisma-project-repository' | ||
import { ResourceNotFoundError } from '../../use-cases/errors/ResourceNotFoundError' | ||
import { EditProjectUseCase } from '../../use-cases/project/editProjectUseCase' | ||
|
||
export async function editProject( | ||
request: FastifyRequest, | ||
response: FastifyReply, | ||
) { | ||
const editProjectBodySchema = z.object({ | ||
title: z.string(), | ||
tags: z.array(z.string()), | ||
link: z.string(), | ||
description: z.string(), | ||
}) | ||
|
||
const editProjectParamsSchema = z.object({ | ||
projectId: z.string().uuid(), | ||
}) | ||
|
||
const { title, tags, link, description } = editProjectBodySchema.parse( | ||
request.body, | ||
) | ||
const { projectId } = editProjectParamsSchema.parse(request.params) | ||
|
||
const projectRepository = new PrismaProjectRepository() | ||
const editProjectUseCase = new EditProjectUseCase(projectRepository) | ||
|
||
try { | ||
const { project } = await editProjectUseCase.execute({ | ||
projectId, | ||
title, | ||
tags, | ||
link, | ||
description, | ||
}) | ||
|
||
return response.status(200).send({ project }) | ||
} catch (error) { | ||
if (error instanceof ResourceNotFoundError) { | ||
return response.status(404).send({ error: 'Project was not Found !' }) | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters