v1.0.0 #28
|
@ -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<void> {
|
||||
|
@ -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 });
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
@ -1,12 +1,12 @@
|
|||
import ImageModel, { Image } from "../models/ImageModel";
|
||||
import imageModel, { Image } from "../models/ImageModel";
|
||||
|
||||
class ImageService {
|
||||
async findAll(): Promise<Image[]> {
|
||||
const allImages = await ImageModel.find();
|
||||
const allImages = await imageModel.find();
|
||||
return allImages;
|
||||
}
|
||||
async add(image: Image): Promise<Image> {
|
||||
const newImage = await ImageModel.create(image);
|
||||
const newImage = await imageModel.create(image);
|
||||
return newImage;
|
||||
}
|
||||
}
|
||||
|
|
|
@ -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);
|
||||
});
|
||||
});
|
Loading…
Reference in New Issue