Compare commits

...

5 Commits

4 changed files with 72 additions and 11 deletions

View File

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

View File

@ -3,13 +3,20 @@
"module": "index.ts",
"type": "module",
"devDependencies": {
"bun-types": "latest"
"@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"
},
"peerDependencies": {
"typescript": "^5.0.0"
},"scripts": {
},
"scripts": {
"start": "bun run src/index.ts",
"dev": "bun --hot run src/index.ts"
"dev": "bun --hot run src/index.ts",
"test": "bun test"
},
"dependencies": {
"@types/express": "^4.17.21",

View File

@ -2,7 +2,7 @@ import express from "express";
import mongoose from "mongoose";
import ImageModel from "./ImageModel";
const app = express();
export const app = express();
app.use(express.json());
@ -23,13 +23,16 @@ 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.json(image);
} catch (error) {
// Should return 409 Conflict for existing urls
// Should return 500 For other server errors
res.status(500).json({ message: error });
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 });
}
})

51
tests/app.test.ts Normal file
View File

@ -0,0 +1,51 @@
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));
});
});