added a functional test and optimized a test
Unit Tests with docker compose / unit-test (pull_request) Successful in 6m7s Details

This commit is contained in:
Alie 2024-01-06 13:10:45 +01:00
parent ebf2ec17d1
commit fb2d947913
1 changed files with 28 additions and 5 deletions

View File

@ -166,7 +166,6 @@ describe("POST /images works properly", () => {
});
it("should return 400 for malformed requests", async () => {
mock.restore();
const res = await request(app)
.post("/images")
.set("authorization", `Bearer ${token}`)
@ -180,17 +179,22 @@ describe("POST /images works properly", () => {
});
describe("GET /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 200 for existing ids", async () => {
const list = await request(app).get("/images");
const id = list.body.images[0]._id;
const res = await request(app).get(`/images/${id}`);
expect(res.status).toBe(200);
});
it("should return 404 for non-existing ids", async () => {
const list = await request(app).get("/images");
const id = "000000000000000000000000"; // this was the least posible to exist ID
if (!(id in list.body.images)) {
if (!(id in list)) {
const res = await request(app).get(`/images/${id}`);
expect(res.status).toBe(404);
}
@ -286,4 +290,23 @@ describe("PUT /images/<id> works properly", () => {
.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");
})
});