2023-12-29 18:37:35 +00:00
|
|
|
import {
|
|
|
|
afterEach,
|
|
|
|
beforeAll,
|
|
|
|
describe,
|
|
|
|
expect,
|
|
|
|
it,
|
|
|
|
mock,
|
|
|
|
} from "bun:test";
|
2023-12-29 16:10:48 +00:00
|
|
|
import request, { Response } from "supertest";
|
2023-12-29 14:24:29 +00:00
|
|
|
import app, { startApp } from "../src/app";
|
2023-12-27 19:02:26 +00:00
|
|
|
import imageService from "../src/services/ImageService";
|
2023-12-29 14:24:29 +00:00
|
|
|
import populateDatabase from "./populateDatabase";
|
2023-12-27 19:02:26 +00:00
|
|
|
|
|
|
|
const imageServiceOriginal = imageService;
|
2023-12-29 14:24:29 +00:00
|
|
|
|
|
|
|
let token: string;
|
|
|
|
|
|
|
|
beforeAll(async () => {
|
2023-12-29 18:37:35 +00:00
|
|
|
await startApp();
|
|
|
|
await populateDatabase();
|
|
|
|
|
|
|
|
const tok = await request(app)
|
|
|
|
.post("/login")
|
|
|
|
.send({ app: "tester", secret: "test" });
|
|
|
|
token = tok.body.token;
|
2023-12-29 14:24:29 +00:00
|
|
|
});
|
|
|
|
|
2023-12-27 19:02:26 +00:00
|
|
|
afterEach(() => {
|
2023-12-29 18:37:35 +00:00
|
|
|
mock.restore();
|
|
|
|
mock.module("../src/services/ImageService", () => ({
|
|
|
|
default: imageServiceOriginal,
|
|
|
|
}));
|
2023-12-27 19:33:25 +00:00
|
|
|
});
|
2023-12-25 11:15:19 +00:00
|
|
|
|
2023-12-29 16:10:48 +00:00
|
|
|
describe("/login works as instended", async () => {
|
2023-12-29 18:37:35 +00:00
|
|
|
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);
|
|
|
|
});
|
|
|
|
});
|
2023-12-29 16:10:48 +00:00
|
|
|
|
2023-12-27 16:58:59 +00:00
|
|
|
describe("GET / shows all of the endpoints", async () => {
|
2023-12-29 18:37:35 +00:00
|
|
|
const res = await request(app).get("/");
|
2023-12-27 15:52:15 +00:00
|
|
|
|
2023-12-29 18:37:35 +00:00
|
|
|
it("should be", async () => {
|
|
|
|
expect(res.body).toHaveProperty("endpoints");
|
|
|
|
});
|
2023-12-27 16:58:59 +00:00
|
|
|
|
2023-12-29 18:37:35 +00:00
|
|
|
it("should be an array", () => {
|
|
|
|
expect(Array.isArray(res.body.endpoints)).toBeTrue();
|
|
|
|
});
|
2023-12-27 19:33:25 +00:00
|
|
|
});
|
2023-12-27 15:52:15 +00:00
|
|
|
|
2023-12-25 11:33:40 +00:00
|
|
|
describe("GET /images works properly", async () => {
|
2023-12-29 18:37:35 +00:00
|
|
|
const res = await request(app).get("/images");
|
2023-12-25 11:33:40 +00:00
|
|
|
|
2023-12-29 18:37:35 +00:00
|
|
|
it("should be an array", () => {
|
|
|
|
expect(Array.isArray(res.body.images)).toBeTrue();
|
|
|
|
});
|
2023-12-25 11:33:40 +00:00
|
|
|
|
2023-12-29 18:37:35 +00:00
|
|
|
it("should return a 200", async () => {
|
|
|
|
expect(res.statusCode).toBe(200);
|
|
|
|
});
|
2023-12-29 18:47:35 +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");
|
|
|
|
},
|
|
|
|
},
|
|
|
|
}));
|
|
|
|
|
|
|
|
const res = await request(app)
|
|
|
|
.get("/images");
|
|
|
|
|
|
|
|
expect(res.status).toBe(500);
|
|
|
|
});
|
2023-12-25 11:15:19 +00:00
|
|
|
});
|
|
|
|
|
|
|
|
describe("POST /images works properly", () => {
|
2023-12-29 18:37:35 +00:00
|
|
|
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);
|
|
|
|
});
|
2023-12-27 19:33:25 +00:00
|
|
|
});
|