import { afterEach, beforeAll, describe, expect, it, mock } from "bun:test"; import request, { Response } from "supertest"; import app, { startApp } from "../src/app"; import imageService from "../src/services/ImageService"; import populateDatabase from "./populateDatabase"; import { Image } from "../src/models/ImageModel"; const imageServiceOriginal = imageService; let token: string; beforeAll(async () => { await startApp(); await populateDatabase(); const tok = await request(app) .post("/login") .send({ app: "tester", secret: "test" }); token = tok.body.token; }); afterEach(() => { mock.restore(); mock.module("../src/services/ImageService", () => ({ default: imageServiceOriginal, })); }); describe("/login works as instended", async () => { let correctRespose: Response; beforeAll(async () => { correctRespose = await request(app) .post("/login") .send({ app: "tester", secret: "test" }); }); it("should return 200 for correct login", async () => { expect(correctRespose.status).toBe(200); }); it("should contain a token", () => { expect(correctRespose.body).toHaveProperty("token"); }); it("should return 403 for invalid credentials", async () => { const res = await request(app).post("/login").send({}); expect(res.status).toBe(403); }); }); describe("GET / shows all of the endpoints", async () => { const res = await request(app).get("/"); it("should be", async () => { expect(res.body).toHaveProperty("endpoints"); }); it("should be an array", () => { expect(Array.isArray(res.body.endpoints)).toBeTrue(); }); }); describe("GET /images works properly", async () => { const res = await request(app).get("/images"); it("should be an array", () => { expect(Array.isArray(res.body.images)).toBeTrue(); }); it("should return a 200", async () => { expect(res.statusCode).toBe(200); }); 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"); }, }, })); const res = await request(app).get("/images"); expect(res.status).toBe(500); }); }); describe("POST /images works properly", () => { it("should return 401 for unauthenticated requests", async () => { const res = await request(app) .post("/images") .send({ url: "https://test.url.com/0", status: "available", tags: ["2girls", "touhou"], }); expect(res.status).toBe(401); }); it("should return 403 for invalid tokens", async () => { const res = await request(app) .post("/images") .set("authorization", `Bearer token`) .send({ url: "https://test.url.com/0", status: "available", tags: ["2girls", "touhou"], }); expect(res.status).toBe(403); }); it("should return 201 for new image", async () => { const res = await request(app) .post("/images") .set("authorization", `Bearer ${token}`) .send({ url: "https://test.url.com/1", status: "available", tags: ["2girls", "touhou"], }); expect(res.status).toBe(201); }); it("should return 409 for a repeated images", async () => { await request(app) .post("/images") .set("authorization", `Bearer ${token}`) .send({ url: "https://test.url.com/2", status: "available", tags: ["2girls", "touhou"], }); const res = await request(app) .post("/images") .set("authorization", `Bearer ${token}`) .send({ url: "https://test.url.com/2", status: "available", tags: ["2girls", "touhou"], }); expect(res.status).toBe(409); }); 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"); }, }, })); const res = await request(app) .post("/images") .set("authorization", `Bearer ${token}`) .send({ url: "https://test.url.com/3", status: "available", tags: ["2girls", "touhou"], }); expect(res.status).toBe(500); }); it("should return 400 for malformed requests", async () => { const res = await request(app) .post("/images") .set("authorization", `Bearer ${token}`) .send({ url: "https://test.url.com/4", status: "wrong", tags: ["2girls", "touhou"], }); expect(res.status).toBe(400); }); }); describe("GET /images/ works properly", () => { let id: string; let list: Image[]; beforeAll(async () => { const res = await request(app).get("/images"); list = res.body.images; id = list[0]._id; }); it("should return 200 for existing ids", async () => { const res = await request(app).get(`/images/${id}`); expect(res.status).toBe(200); }); it("should return 404 for non-existing ids", async () => { const id = "000000000000000000000000"; // this was the least posible to exist ID if (!(id in list)) { const res = await request(app).get(`/images/${id}`); expect(res.status).toBe(404); } }); it("should return 400 for malformed ids", async () => { const res = await request(app).get("/images/98439384"); expect(res.status).toBe(400); }); }); describe("PUT /images/ works properly", () => { let id: string; let list: Image[]; beforeAll(async () => { const res = await request(app).get("/images"); list = res.body.images; id = list[0]._id; }); it("should return 204 for valid modifications", async () => { const res = await request(app) .put(`/images/${id}`) .set("authorization", `Bearer ${token}`) .send({ status: "available" }); expect(res.status).toBe(204); }); it("should return 404 for non-existing ids", async () => { const id = "000000000000000000000000"; // this was the least posible to exist ID if (!(id in list)) { const res = await request(app) .put(`/images/${id}`) .set("authorization", `Bearer ${token}`) .send({ status: "available" }); expect(res.status).toBe(404); } }); it("should return 400 for malformed ids", async () => { const res = await request(app) .put("/images/98439384") .set("authorization", `Bearer ${token}`) .send({ status: "available" }); expect(res.status).toBe(400); expect(res.body).toEqual({ error: "Invalid Id" }); }); it("should return 400 for malformed requests", async () => { const res = await request(app) .put(`/images/${id}`) .set("authorization", `Bearer ${token}`) .send({ status: "wrong" }); expect(res.status).toBe(400); }); it("should return 409 if we try to modify the url into an existing one", async () => { const res = await request(app) .put(`/images/${id}`) .set("authorization", `Bearer ${token}`) .send({ url: "https://test.url.com/2" }); expect(res.status).toBe(409); }); it("should return 500 for an error on the service", async () => { mock.module("../src/services/ImageService", () => ({ default: { replaceOne: () => { throw new Error("This is an expected testing error"); }, }, })); const res = await request(app) .put(`/images/${id}`) .set("authorization", `Bearer ${token}`) .send({ status: "available" }); expect(res.status).toBe(500); }); it("should return 401 for unauthenticated requests", async () => { const res = await request(app) .put(`/images/${id}`) .send({ status: "available" }); expect(res.status).toBe(401); }); it("should return 403 for invalid tokens", async () => { const res = await request(app) .put(`/images/${id}`) .set("authorization", `Bearer token`) .send({ status: "available" }); expect(res.status).toBe(403); }); it("should have its changes be reflected onto the DB", async () => { const image = await request(app) .post("/images") .set("authorization", `Bearer ${token}`) .send({ url: "https://test.url.com/5", status: "available", tags: ["2girls", "touhou"], }); const id = image.body.image._id; await request(app) .put(`/images/${id}`) .set("authorization", `Bearer ${token}`) .send({ status: "consumed" }); const res = await request(app).get(`/images/${id}`); expect(res.body.image).toHaveProperty("status", "consumed"); }) });