2023-12-31 11:29:31 +00:00
|
|
|
import { afterEach, beforeAll, describe, expect, it, mock } from "bun:test";
|
2023-12-31 11:11:22 +00:00
|
|
|
import request, { Response } from "supertest";
|
|
|
|
import app, { startApp } from "../src/app";
|
2023-12-27 19:02:26 +00:00
|
|
|
import imageService from "../src/services/ImageService";
|
2023-12-31 11:11:22 +00:00
|
|
|
import populateDatabase from "./populateDatabase";
|
2024-01-06 11:57:22 +00:00
|
|
|
import { Image } from "../src/models/ImageModel";
|
2023-12-27 19:02:26 +00:00
|
|
|
|
|
|
|
const imageServiceOriginal = imageService;
|
2023-12-31 11:11:22 +00:00
|
|
|
|
|
|
|
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;
|
|
|
|
});
|
2023-12-27 19:02:26 +00:00
|
|
|
|
|
|
|
afterEach(() => {
|
2023-12-27 19:33:25 +00:00
|
|
|
mock.restore();
|
|
|
|
mock.module("../src/services/ImageService", () => ({
|
|
|
|
default: imageServiceOriginal,
|
|
|
|
}));
|
|
|
|
});
|
2023-12-25 11:15:19 +00:00
|
|
|
|
2023-12-31 11:11:22 +00:00
|
|
|
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);
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
2023-12-27 16:58:59 +00:00
|
|
|
describe("GET / shows all of the endpoints", async () => {
|
2023-12-27 19:33:25 +00:00
|
|
|
const res = await request(app).get("/");
|
2023-12-27 15:52:15 +00:00
|
|
|
|
2023-12-27 19:33:25 +00:00
|
|
|
it("should be", async () => {
|
|
|
|
expect(res.body).toHaveProperty("endpoints");
|
|
|
|
});
|
2023-12-27 16:58:59 +00:00
|
|
|
|
2023-12-27 19:33:25 +00:00
|
|
|
it("should be an array", () => {
|
|
|
|
expect(Array.isArray(res.body.endpoints)).toBeTrue();
|
|
|
|
});
|
|
|
|
});
|
2023-12-27 15:52:15 +00:00
|
|
|
|
2023-12-25 11:33:40 +00:00
|
|
|
describe("GET /images works properly", async () => {
|
2023-12-27 19:33:25 +00:00
|
|
|
const res = await request(app).get("/images");
|
2023-12-25 11:33:40 +00:00
|
|
|
|
2023-12-27 19:33:25 +00:00
|
|
|
it("should be an array", () => {
|
|
|
|
expect(Array.isArray(res.body.images)).toBeTrue();
|
|
|
|
});
|
2023-12-25 11:33:40 +00:00
|
|
|
|
2023-12-27 19:33:25 +00:00
|
|
|
it("should return a 200", async () => {
|
|
|
|
expect(res.statusCode).toBe(200);
|
|
|
|
});
|
2023-12-31 11:11:22 +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-31 11:29:31 +00:00
|
|
|
const res = await request(app).get("/images");
|
2023-12-31 11:11:22 +00:00
|
|
|
|
|
|
|
expect(res.status).toBe(500);
|
|
|
|
});
|
2023-12-25 11:15:19 +00:00
|
|
|
});
|
|
|
|
|
|
|
|
describe("POST /images works properly", () => {
|
2023-12-27 19:41: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"],
|
2023-12-27 19:33:25 +00:00
|
|
|
});
|
2023-12-27 19:41:35 +00:00
|
|
|
expect(res.status).toBe(401);
|
|
|
|
});
|
2023-12-25 11:33:40 +00:00
|
|
|
|
2023-12-27 19:41:35 +00:00
|
|
|
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"],
|
2023-12-27 19:33:25 +00:00
|
|
|
});
|
2023-12-27 19:41:35 +00:00
|
|
|
expect(res.status).toBe(403);
|
|
|
|
});
|
2023-12-25 11:33:40 +00:00
|
|
|
|
2023-12-27 19:33:25 +00:00
|
|
|
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);
|
|
|
|
});
|
2023-12-25 11:33:40 +00:00
|
|
|
|
2023-12-27 19:33:25 +00:00
|
|
|
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"],
|
|
|
|
});
|
2023-12-27 19:02:26 +00:00
|
|
|
|
2023-12-27 19:33:25 +00:00
|
|
|
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);
|
|
|
|
});
|
|
|
|
});
|
2024-01-03 12:58:18 +00:00
|
|
|
|
2024-01-06 11:57:22 +00:00
|
|
|
describe("GET /images/<id> works properly", () => {
|
2024-01-06 12:10:45 +00:00
|
|
|
let id: string;
|
|
|
|
let list: Image[];
|
|
|
|
beforeAll(async () => {
|
|
|
|
const res = await request(app).get("/images");
|
|
|
|
list = res.body.images;
|
|
|
|
id = list[0]._id;
|
|
|
|
});
|
|
|
|
|
2024-01-03 12:58:18 +00:00
|
|
|
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
|
2024-01-06 12:10:45 +00:00
|
|
|
if (!(id in list)) {
|
2024-01-03 12:58:18 +00:00
|
|
|
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);
|
|
|
|
});
|
|
|
|
});
|
2024-01-06 11:57:22 +00:00
|
|
|
|
|
|
|
describe("PUT /images/<id> 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);
|
|
|
|
});
|
2024-01-06 12:10:45 +00:00
|
|
|
|
|
|
|
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");
|
|
|
|
})
|
2024-01-06 11:57:22 +00:00
|
|
|
});
|