136 lines
3.5 KiB
TypeScript
136 lines
3.5 KiB
TypeScript
import { afterEach, beforeAll, describe, expect, it, mock } from "bun:test";
|
|
import request from "supertest";
|
|
import { app } from "../src";
|
|
import imageService from "../src/services/ImageService";
|
|
|
|
const imageServiceOriginal = imageService;
|
|
const tok = await request(app)
|
|
.post("/login")
|
|
.send({ app: "tester", secret: "test" });
|
|
const token = tok.body.token;
|
|
|
|
afterEach(() => {
|
|
mock.restore();
|
|
mock.module("../src/services/ImageService", () => ({
|
|
default: imageServiceOriginal,
|
|
}));
|
|
});
|
|
|
|
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);
|
|
});
|
|
});
|
|
|
|
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 () => {
|
|
mock.restore();
|
|
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);
|
|
});
|
|
});
|