Compare commits

..

No commits in common. "532de04592d1106c11626c143efc5cc83422bb1f" and "aa1dc44eaed56e87d67ded0041fdb5dc8d5f525c" have entirely different histories.

4 changed files with 11 additions and 72 deletions

View File

@ -16,7 +16,7 @@ db.createUser({
db = new Mongo().getDB("bot");
db.images.createIndex({ "status": 1 });
db.images.createIndex({ "url": 1 }, { "unique": true });
db.images.createIndex({ "image": 1 }, { "unique": true });
db.images.insert({
url: "https://example.com",
status: "consumed",

View File

@ -3,20 +3,13 @@
"module": "index.ts",
"type": "module",
"devDependencies": {
"@types/jest": "^29.5.11",
"@types/supertest": "^6.0.1",
"bun-types": "latest",
"jest": "^29.7.0",
"supertest": "^6.3.3",
"ts-jest": "^29.1.1"
"bun-types": "latest"
},
"peerDependencies": {
"typescript": "^5.0.0"
},
"scripts": {
},"scripts": {
"start": "bun run src/index.ts",
"dev": "bun --hot run src/index.ts",
"test": "bun test"
"dev": "bun --hot run src/index.ts"
},
"dependencies": {
"@types/express": "^4.17.21",

View File

@ -2,7 +2,7 @@ import express from "express";
import mongoose from "mongoose";
import ImageModel from "./ImageModel";
export const app = express();
const app = express();
app.use(express.json());
@ -23,16 +23,13 @@ app.post("/images", async (req, res) => {
try {
// Should add auth here before doing stuff
// Thowing a 401 if not auth provided
// Throwing a 403 for incorrect auth
// throwing a 403 for incorrect auth
const image = await ImageModel.create(req.body);
res.status(201).json(image);
} catch (error: any) {
if (error.code == 11000){
// Should return 409 Conflict for existing urls
res.status(409).json({ message: "Existing URL" });
}
// Should return 400 Bad request for invalid requests
res.status(400).json({ message: error });
res.json(image);
} catch (error) {
// Should return 409 Conflict for existing urls
// Should return 500 For other server errors
res.status(500).json({ message: error });
}
})

View File

@ -1,51 +0,0 @@
import { beforeAll, describe, expect, it } from "bun:test";
import request from "supertest";
import { app } from "../src";
describe("GET /images works properly", async () => {
const res = await request(app).get("/images");
it("should be an array", async () => {
expect(Array.isArray(res.body)).toBeTrue();
});
it("should return a 200", async () => {
expect(res.statusCode).toBe(200);
});
});
describe("POST /images works properly", () => {
it("should return 201 for new image", async () => {
const res = await request(app).post("/images").send({
url: "https://test.url.com/1",
status: "available",
tags: ["2girls", "touhou"]
});
expect(res.status).toSatisfy(status => [201].includes(status));
});
it("should return 409 for a repeated images", async () => {
await request(app).post("/images").send({
url: "https://test.url.com/2",
status: "available",
tags: ["2girls", "touhou"]
});
const res = await request(app).post("/images").send({
url: "https://test.url.com/2",
status: "available",
tags: ["2girls", "touhou"]
});
expect(res.status).toSatisfy(status => [409].includes(status));
});
it("should return 400 for malformed requests", async () => {
const res = await request(app).post("/images").send({
url: "https://test.url.com/3",
status: "wrong",
tags: ["2girls", "touhou"]
});
expect(res.status).toSatisfy(status => [400].includes(status));
});
});