diff --git a/src/controllers/ImageController.ts b/src/controllers/ImageController.ts index 58c2a92..5e1fcac 100644 --- a/src/controllers/ImageController.ts +++ b/src/controllers/ImageController.ts @@ -1,5 +1,6 @@ import { Request, Response } from "express"; import imageService from "../services/ImageService"; +import mongoose, { mongo } from "mongoose"; class ImageController { async getAllImages(req: Request, res: Response): Promise { @@ -20,12 +21,16 @@ class ImageController { const image = await imageService.add(req.body); res.status(201).json({ image }); } catch (error: any) { - if (error.code === 11000) { + if (error instanceof mongo.MongoServerError && error.code === 11000) { // Should return 409 Conflict for existing urls - res.status(409).json({ error: "Existing URL" }); + res.status(409).json({ error: `the image with URL ${error.keyValue.url} already exists` }); + } else if (error instanceof mongoose.Error.ValidationError) { + // Should return 400 Bad request for invalid requests + res.status(400).json({ error: error.message }); + } else { + // Return 500 in other case + res.status(500).json({ error: error }); } - // Should return 400 Bad request for invalid requests - res.status(400).json({ error: error }); } } } diff --git a/src/services/ImageService.ts b/src/services/ImageService.ts index c8a3c16..bfaab2f 100644 --- a/src/services/ImageService.ts +++ b/src/services/ImageService.ts @@ -1,12 +1,12 @@ -import ImageModel, { Image } from "../models/ImageModel"; +import imageModel, { Image } from "../models/ImageModel"; class ImageService { async findAll(): Promise { - const allImages = await ImageModel.find(); + const allImages = await imageModel.find(); return allImages; } async add(image: Image): Promise { - const newImage = await ImageModel.create(image); + const newImage = await imageModel.create(image); return newImage; } } diff --git a/tests/app.test.ts b/tests/app.test.ts index f8ec9f4..455b873 100644 --- a/tests/app.test.ts +++ b/tests/app.test.ts @@ -1,6 +1,14 @@ -import { describe, expect, it, mock } from "bun:test"; +import { afterEach, describe, expect, it, mock } from "bun:test"; import request from "supertest"; import { app } from "../src"; +import imageService from "../src/services/ImageService"; + +const imageServiceOriginal = imageService; + +afterEach(() => { + mock.restore(); + mock.module("../src/services/ImageService", () => ({ default: imageServiceOriginal })); +}) describe("GET / shows all of the endpoints", async () => { const res = await request(app).get("/"); @@ -49,15 +57,32 @@ describe("POST /images works properly", () => { tags: ["2girls", "touhou"] }); - expect(res.status).toSatisfy(status => [409].includes(status)); + 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").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").send({ - url: "https://test.url.com/3", + url: "https://test.url.com/4", status: "wrong", tags: ["2girls", "touhou"] }); - expect(res.status).toSatisfy(status => [400].includes(status)); + expect(res.status).toBe(400); }); }); \ No newline at end of file