2023-12-27 19:02:26 +00:00
|
|
|
import { afterEach, describe, expect, it, mock } from "bun:test";
|
2023-12-25 11:15:19 +00:00
|
|
|
import request from "supertest";
|
|
|
|
import { app } from "../src";
|
2023-12-27 19:02:26 +00:00
|
|
|
import imageService from "../src/services/ImageService";
|
|
|
|
|
|
|
|
const imageServiceOriginal = imageService;
|
|
|
|
|
|
|
|
afterEach(() => {
|
|
|
|
mock.restore();
|
|
|
|
mock.module("../src/services/ImageService", () => ({ default: imageServiceOriginal }));
|
|
|
|
})
|
2023-12-25 11:15:19 +00:00
|
|
|
|
2023-12-27 16:58:59 +00:00
|
|
|
describe("GET / shows all of the endpoints", async () => {
|
2023-12-27 15:52:15 +00:00
|
|
|
const res = await request(app).get("/");
|
|
|
|
|
|
|
|
it("should be", async () => {
|
2023-12-27 17:16:46 +00:00
|
|
|
expect(res.body).toHaveProperty("endpoints");
|
2023-12-27 15:52:15 +00:00
|
|
|
});
|
2023-12-27 16:58:59 +00:00
|
|
|
|
|
|
|
it("should be an array", () => {
|
2023-12-27 17:16:46 +00:00
|
|
|
expect(Array.isArray(res.body.endpoints)).toBeTrue();
|
2023-12-27 16:58:59 +00:00
|
|
|
})
|
2023-12-27 15:52:15 +00:00
|
|
|
})
|
|
|
|
|
2023-12-25 11:33:40 +00:00
|
|
|
describe("GET /images works properly", async () => {
|
|
|
|
const res = await request(app).get("/images");
|
|
|
|
|
2023-12-27 16:58:59 +00:00
|
|
|
it("should be an array", () => {
|
2023-12-27 17:16:46 +00:00
|
|
|
expect(Array.isArray(res.body.images)).toBeTrue();
|
2023-12-25 11:33:40 +00:00
|
|
|
});
|
|
|
|
|
|
|
|
it("should return a 200", async () => {
|
2023-12-25 11:15:19 +00:00
|
|
|
expect(res.statusCode).toBe(200);
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
describe("POST /images works properly", () => {
|
2023-12-25 11:33:40 +00:00
|
|
|
it("should return 201 for new image", async () => {
|
2023-12-25 11:15:19 +00:00
|
|
|
const res = await request(app).post("/images").send({
|
2023-12-25 11:33:40 +00:00
|
|
|
url: "https://test.url.com/1",
|
|
|
|
status: "available",
|
|
|
|
tags: ["2girls", "touhou"]
|
|
|
|
});
|
|
|
|
expect(res.status).toSatisfy(status => [201].includes(status));
|
|
|
|
});
|
|
|
|
|
|
|
|
it("should return 409 for a repeated images", async () => {
|
|
|
|
await request(app).post("/images").send({
|
|
|
|
url: "https://test.url.com/2",
|
2023-12-25 11:15:19 +00:00
|
|
|
status: "available",
|
|
|
|
tags: ["2girls", "touhou"]
|
|
|
|
});
|
2023-12-25 11:33:40 +00:00
|
|
|
|
|
|
|
const res = await request(app).post("/images").send({
|
|
|
|
url: "https://test.url.com/2",
|
|
|
|
status: "available",
|
|
|
|
tags: ["2girls", "touhou"]
|
|
|
|
});
|
|
|
|
|
2023-12-27 19:02:26 +00:00
|
|
|
expect(res.status).toBe(409);
|
2023-12-25 11:33:40 +00:00
|
|
|
});
|
|
|
|
|
2023-12-27 19:02:26 +00:00
|
|
|
it("should return 500 for an error on the service", async () => {
|
|
|
|
mock.module("../src/services/ImageService", () => ({
|
|
|
|
default: {
|
|
|
|
add: () => { throw new Error("This is an expected testing error"); }
|
|
|
|
}
|
|
|
|
}));
|
|
|
|
|
2023-12-25 11:33:40 +00:00
|
|
|
const res = await request(app).post("/images").send({
|
|
|
|
url: "https://test.url.com/3",
|
2023-12-27 19:02:26 +00:00
|
|
|
status: "available",
|
|
|
|
tags: ["2girls", "touhou"]
|
|
|
|
});
|
|
|
|
|
|
|
|
expect(res.status).toBe(500);
|
|
|
|
});
|
|
|
|
|
|
|
|
it("should return 400 for malformed requests", async () => {
|
|
|
|
mock.restore();
|
|
|
|
const res = await request(app).post("/images").send({
|
|
|
|
url: "https://test.url.com/4",
|
2023-12-25 11:33:40 +00:00
|
|
|
status: "wrong",
|
|
|
|
tags: ["2girls", "touhou"]
|
|
|
|
});
|
2023-12-27 19:02:26 +00:00
|
|
|
expect(res.status).toBe(400);
|
2023-12-25 11:15:19 +00:00
|
|
|
});
|
|
|
|
});
|