-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Implementing method adoption and test e2e
- Loading branch information
Showing
14 changed files
with
152 additions
and
36 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
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,55 @@ | ||
import request from 'supertest' | ||
import { app } from '@/app' | ||
import { afterAll, beforeAll, describe, expect, it } from 'vitest' | ||
import { createAndAuthenticateOrgs } from '@/utils/test/create-and-authenticate-orgs' | ||
import { createAndAuthenticateUser } from '@/utils/test/create-and-authenticate-user' | ||
|
||
describe('Adoption Pets (e2e)', () => { | ||
beforeAll(async () => { | ||
await app.ready() | ||
}) | ||
|
||
afterAll(async () => { | ||
await app.close() | ||
}) | ||
|
||
it('should be able to adoption pets and after adopting pets appear as unavailable', async () => { | ||
const { token: tokenUser, userId } = await createAndAuthenticateUser(app, true) // true = isAdmin | ||
const { token: tokenOrg, orgId } = await createAndAuthenticateOrgs(app, true) // true = isAdmin | ||
|
||
const pet = await request(app.server) | ||
.post(`/orgs/${orgId}/pets`) | ||
.set('Authorization', `Bearer ${tokenOrg}`) | ||
.send({ | ||
animalType: 'dog', | ||
name: 'max', | ||
breed: 'pit-bull', | ||
size: 'large', | ||
age: 1, | ||
}) | ||
|
||
const petId = pet.body.registeredPet.pet.id | ||
|
||
const response = await request(app.server) | ||
.post(`/${userId}/${petId}/adoption`) | ||
.set('Authorization', `Bearer ${tokenUser}`) | ||
.send() | ||
|
||
expect(response.statusCode).toEqual(201) | ||
expect(response.body).toEqual( | ||
expect.objectContaining({phone: expect.any(String)}), | ||
) | ||
|
||
const responseFilter = await request(app.server) | ||
.get('/pets/filter') | ||
.query({ | ||
animalType: 'dog', | ||
breed: 'pit-bull', | ||
age: 1, | ||
}) | ||
.set('Authorization', `Bearer ${tokenUser}`) | ||
.send() | ||
|
||
expect(responseFilter.statusCode).toEqual(400) | ||
}) | ||
}) |
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,30 @@ | ||
import { FastifyReply, FastifyRequest } from 'fastify' | ||
import { z } from "zod" | ||
import { makeAdoptionsUseCase } from '@/use-cases/factories/make-adoptions-use-case' | ||
import { PetNotFoundError } from '@/use-cases/errors/pet-not-found' | ||
|
||
export async function Adoption(request: FastifyRequest, reply: FastifyReply) { | ||
const adoptionParamsSchema = z.object({ | ||
userId: z.string().uuid(), | ||
petId: z.string().uuid(), | ||
}) | ||
|
||
try { | ||
const { userId, petId } = adoptionParamsSchema.parse(request.params) | ||
|
||
const adoptionPetUseCase = makeAdoptionsUseCase() | ||
|
||
const adoptionDetails = await adoptionPetUseCase.execute({ | ||
userId, | ||
petId, | ||
}) | ||
|
||
return reply.status(201).send(adoptionDetails) | ||
} catch (err) { | ||
if(err instanceof PetNotFoundError){ | ||
return reply.status(409).send({ message: err.message}) | ||
} | ||
|
||
throw err | ||
} | ||
} |
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,11 @@ | ||
import { FastifyInstance } from 'fastify' | ||
|
||
import { verifyJWT } from '@/http/middlewares/verify-jwt' | ||
|
||
import { Adoption } from './adoption' | ||
|
||
export async function adoptionsRoutes(app: FastifyInstance) { | ||
app.addHook('onRequest', verifyJWT) | ||
|
||
app.post('/:userId/:petId/adoption', Adoption) | ||
} |
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
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
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
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
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
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -7,7 +7,7 @@ import request from 'supertest' | |
app: FastifyInstance, | ||
isAdmin = false, | ||
) { | ||
await prisma.user.create({ | ||
const user = await prisma.user.create({ | ||
data: { | ||
name: 'John Doe', | ||
email: '[email protected]', | ||
|
@@ -22,6 +22,7 @@ import request from 'supertest' | |
}) | ||
|
||
const { token } = authResponse.body | ||
const userId = user.id | ||
|
||
return {token} | ||
return {token, userId} | ||
} |