|
| 1 | +import { describe, expect, it } from "bun:test"; |
| 2 | +import { app } from "@/app"; |
| 3 | +import db from "@/db"; |
| 4 | +import { speciesTable } from "../specie"; |
| 5 | +import { bearerToken } from "@/test"; |
| 6 | + |
| 7 | +describe("Delete specie e2e", () => { |
| 8 | + it("should delete a specie successfully", async () => { |
| 9 | + const specie = ( |
| 10 | + await db |
| 11 | + .insert(speciesTable) |
| 12 | + .values({ |
| 13 | + name: "Dog", |
| 14 | + }) |
| 15 | + .returning() |
| 16 | + )[0]; |
| 17 | + |
| 18 | + const request = new Request(`http://localhost/pet/species/${specie.id}`, { |
| 19 | + method: "DELETE", |
| 20 | + headers: { |
| 21 | + Authorization: bearerToken, |
| 22 | + }, |
| 23 | + }); |
| 24 | + |
| 25 | + const response = await app.handle(request); |
| 26 | + |
| 27 | + expect(response.status).toBe(204); |
| 28 | + }); |
| 29 | + |
| 30 | + it("should return 404 when trying to delete a specie that doesn't exist", async () => { |
| 31 | + const request = new Request( |
| 32 | + "http://localhost/pet/species/08cb2572-77d4-4de3-b6f7-97e4b5cd6512", |
| 33 | + { |
| 34 | + method: "DELETE", |
| 35 | + headers: { |
| 36 | + Authorization: bearerToken, |
| 37 | + }, |
| 38 | + }, |
| 39 | + ); |
| 40 | + |
| 41 | + const response = await app.handle(request); |
| 42 | + |
| 43 | + const body = await response.json(); |
| 44 | + |
| 45 | + expect(body.name).toBe("SpecieNotFoundError"); |
| 46 | + |
| 47 | + expect(response.status).toBe(404); |
| 48 | + }); |
| 49 | + |
| 50 | + it("should return 422 when delete a specie with invalid data", async () => { |
| 51 | + const request = new Request("http://localhost/pet/species/invalid", { |
| 52 | + method: "DELETE", |
| 53 | + headers: { |
| 54 | + Authorization: bearerToken, |
| 55 | + }, |
| 56 | + }); |
| 57 | + |
| 58 | + const response = await app.handle(request); |
| 59 | + |
| 60 | + const body = await response.json(); |
| 61 | + |
| 62 | + expect(body).toBeTruthy(); |
| 63 | + |
| 64 | + expect(response.status).toBe(422); |
| 65 | + }); |
| 66 | + |
| 67 | + it("should return 401 trying being Unauthorized", async () => { |
| 68 | + const request = new Request( |
| 69 | + "http://localhost/pet/species/b56647a9-9f40-4a31-9a58-42634bfff669", |
| 70 | + { |
| 71 | + method: "DELETE", |
| 72 | + }, |
| 73 | + ); |
| 74 | + |
| 75 | + const response = await app.handle(request); |
| 76 | + |
| 77 | + const body = await response.json(); |
| 78 | + |
| 79 | + expect(body.name).toBe("Unauthorized"); |
| 80 | + |
| 81 | + expect(response.status).toBe(401); |
| 82 | + }); |
| 83 | +}); |
0 commit comments