Compare commits
No commits in common. "ebf2ec17d134707b35891b47dec4926b9cfa556b" and "d9e2cb3917ffac229b0853ade0153891cd3e0fbe" have entirely different histories.
ebf2ec17d1
...
d9e2cb3917
|
@ -15,7 +15,6 @@ app.get("/", (_, res) => {
|
|||
|
||||
app.get("/images", imageController.getAllImages);
|
||||
app.get("/images/:id", imageController.getImageById);
|
||||
app.put("/images/:id", authControler.authorize, imageController.editImage);
|
||||
app.post("/images", authControler.authorize, imageController.addImage);
|
||||
app.post("/login", authControler.login);
|
||||
|
||||
|
@ -31,7 +30,6 @@ export const startApp = async () => {
|
|||
user: mongo_user,
|
||||
pass: mongo_pass,
|
||||
});
|
||||
mongoose.set("runValidators", true);
|
||||
app.listen(port, () =>
|
||||
console.log(`Express server listening on port ${port}`)
|
||||
);
|
||||
|
|
|
@ -1,37 +1,8 @@
|
|||
import { Request, Response } from "express";
|
||||
import imageService from "../services/ImageService";
|
||||
import mongoose, { mongo } from "mongoose";
|
||||
import { Image } from "../models/ImageModel";
|
||||
|
||||
class ImageController {
|
||||
async editImage(req: Request, res: Response) {
|
||||
try {
|
||||
const change: Image = req.body;
|
||||
const result = await imageService.replaceOne(req.params.id, change);
|
||||
if (result.matchedCount > 0) {
|
||||
res.status(204).json();
|
||||
} else {
|
||||
res.status(404).json({ error: "Image not found" });
|
||||
}
|
||||
} catch (error: any) {
|
||||
if (error instanceof mongoose.Error.CastError) {
|
||||
res.status(400).json({ error: "Invalid Id" });
|
||||
} else if (error instanceof mongoose.Error.ValidationError) {
|
||||
// Should return 400 Bad request for invalid requests
|
||||
res.status(400).json({ error: error.message });
|
||||
} else if (
|
||||
error instanceof mongo.MongoServerError &&
|
||||
error.code === 11000
|
||||
) {
|
||||
// Should return 409 Conflict for existing urls
|
||||
res.status(409).json({
|
||||
error: `the image with URL ${error.keyValue.url} already exists`,
|
||||
});
|
||||
} else {
|
||||
res.status(500).json({ error: "Internal Server Error" });
|
||||
}
|
||||
}
|
||||
}
|
||||
async getImageById(req: Request, res: Response) {
|
||||
try {
|
||||
const image = await imageService.findById(req.params.id);
|
||||
|
|
|
@ -1,10 +1,6 @@
|
|||
import imageModel, { Image } from "../models/ImageModel";
|
||||
|
||||
class ImageService {
|
||||
async replaceOne(id: string, newimage: Image) {
|
||||
const result = await imageModel.updateOne({ _id: id }, newimage);
|
||||
return result;
|
||||
}
|
||||
async findById(id: string) {
|
||||
const image = await imageModel.findOne({ _id: id });
|
||||
return image;
|
||||
|
|
|
@ -3,7 +3,6 @@ import request, { Response } from "supertest";
|
|||
import app, { startApp } from "../src/app";
|
||||
import imageService from "../src/services/ImageService";
|
||||
import populateDatabase from "./populateDatabase";
|
||||
import { Image } from "../src/models/ImageModel";
|
||||
|
||||
const imageServiceOriginal = imageService;
|
||||
|
||||
|
@ -179,7 +178,7 @@ describe("POST /images works properly", () => {
|
|||
});
|
||||
});
|
||||
|
||||
describe("GET /images/<id> works properly", () => {
|
||||
describe("/images/:id works properly", () => {
|
||||
it("should return 200 for existing ids", async () => {
|
||||
const list = await request(app).get("/images");
|
||||
const id = list.body.images[0]._id;
|
||||
|
@ -201,89 +200,3 @@ describe("GET /images/<id> works properly", () => {
|
|||
expect(res.status).toBe(400);
|
||||
});
|
||||
});
|
||||
|
||||
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);
|
||||
});
|
||||
});
|
||||
|
|
Loading…
Reference in New Issue