This commit is contained in:
Alie 2023-12-27 20:41:35 +01:00
parent 3be98d0e20
commit 0d2e8dfe36
10 changed files with 85 additions and 84 deletions

View File

@ -1,4 +1,4 @@
version: '3' version: "3"
services: services:
mongodb: mongodb:

View File

@ -23,7 +23,11 @@ class ImageController {
} catch (error: any) { } catch (error: any) {
if (error instanceof mongo.MongoServerError && error.code === 11000) { if (error instanceof mongo.MongoServerError && error.code === 11000) {
// Should return 409 Conflict for existing urls // Should return 409 Conflict for existing urls
res.status(409).json({ error: `the image with URL ${error.keyValue.url} already exists` }); res
.status(409)
.json({
error: `the image with URL ${error.keyValue.url} already exists`,
});
} else if (error instanceof mongoose.Error.ValidationError) { } else if (error instanceof mongoose.Error.ValidationError) {
// Should return 400 Bad request for invalid requests // Should return 400 Bad request for invalid requests
res.status(400).json({ error: error.message }); res.status(400).json({ error: error.message });

View File

@ -15,8 +15,7 @@ app.get("/", (_, res) => {
app.get("/images", imageController.getAllImages); app.get("/images", imageController.getAllImages);
app.post("/images", authControler.authorize, imageController.addImage); app.post("/images", authControler.authorize, imageController.addImage);
app.post("/login", authControler.login) app.post("/login", authControler.login);
const start = async () => { const start = async () => {
// Set the default port to 8080, or use the PORT environment variable // Set the default port to 8080, or use the PORT environment variable

View File

@ -1,8 +1,8 @@
import mongoose, { Document } from "mongoose"; import mongoose, { Document } from "mongoose";
export interface Auth extends Document { export interface Auth extends Document {
app: String, app: String;
secret: String secret: String;
} }
const AuthSchema = new mongoose.Schema({ const AuthSchema = new mongoose.Schema({

View File

@ -9,18 +9,18 @@ export interface Image extends Document {
const ImageSchema = new mongoose.Schema({ const ImageSchema = new mongoose.Schema({
url: { url: {
type: String, type: String,
required: true required: true,
}, },
status: { status: {
type: String, type: String,
enum: { enum: {
values: ["consumed", "unavailable", "available"], values: ["consumed", "unavailable", "available"],
}, },
required: true required: true,
}, },
tags: { tags: {
type: [String] type: [String],
} },
}); });
export default mongoose.model<Image>('images', ImageSchema); export default mongoose.model<Image>("images", ImageSchema);

View File

@ -41,7 +41,6 @@ describe("GET /images works properly", async () => {
}); });
describe("POST /images works properly", () => { describe("POST /images works properly", () => {
it("should return 401 for unauthenticated requests", async () => { it("should return 401 for unauthenticated requests", async () => {
const res = await request(app) const res = await request(app)
.post("/images") .post("/images")

View File

@ -19,4 +19,3 @@ describe("/login", async () => {
expect(res.status).toBe(403); expect(res.status).toBe(403);
}); });
}); });