Compare commits
5 Commits
aa1dc44eae
...
532de04592
Author | SHA1 | Date |
---|---|---|
Alie | 532de04592 | |
Alie | b2933ba5af | |
Alie | c363947870 | |
Suguivy | 98086640e5 | |
Sugui | d749743e56 |
|
@ -16,7 +16,7 @@ db.createUser({
|
||||||
db = new Mongo().getDB("bot");
|
db = new Mongo().getDB("bot");
|
||||||
|
|
||||||
db.images.createIndex({ "status": 1 });
|
db.images.createIndex({ "status": 1 });
|
||||||
db.images.createIndex({ "image": 1 }, { "unique": true });
|
db.images.createIndex({ "url": 1 }, { "unique": true });
|
||||||
db.images.insert({
|
db.images.insert({
|
||||||
url: "https://example.com",
|
url: "https://example.com",
|
||||||
status: "consumed",
|
status: "consumed",
|
||||||
|
|
13
package.json
13
package.json
|
@ -3,13 +3,20 @@
|
||||||
"module": "index.ts",
|
"module": "index.ts",
|
||||||
"type": "module",
|
"type": "module",
|
||||||
"devDependencies": {
|
"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": {
|
"peerDependencies": {
|
||||||
"typescript": "^5.0.0"
|
"typescript": "^5.0.0"
|
||||||
},"scripts": {
|
},
|
||||||
|
"scripts": {
|
||||||
"start": "bun run src/index.ts",
|
"start": "bun run src/index.ts",
|
||||||
"dev": "bun --hot run src/index.ts"
|
"dev": "bun --hot run src/index.ts",
|
||||||
|
"test": "bun test"
|
||||||
},
|
},
|
||||||
"dependencies": {
|
"dependencies": {
|
||||||
"@types/express": "^4.17.21",
|
"@types/express": "^4.17.21",
|
||||||
|
|
17
src/index.ts
17
src/index.ts
|
@ -2,7 +2,7 @@ import express from "express";
|
||||||
import mongoose from "mongoose";
|
import mongoose from "mongoose";
|
||||||
import ImageModel from "./ImageModel";
|
import ImageModel from "./ImageModel";
|
||||||
|
|
||||||
const app = express();
|
export const app = express();
|
||||||
|
|
||||||
app.use(express.json());
|
app.use(express.json());
|
||||||
|
|
||||||
|
@ -23,13 +23,16 @@ app.post("/images", async (req, res) => {
|
||||||
try {
|
try {
|
||||||
// Should add auth here before doing stuff
|
// Should add auth here before doing stuff
|
||||||
// Thowing a 401 if not auth provided
|
// 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);
|
const image = await ImageModel.create(req.body);
|
||||||
res.json(image);
|
res.status(201).json(image);
|
||||||
} catch (error) {
|
} catch (error: any) {
|
||||||
// Should return 409 Conflict for existing urls
|
if (error.code == 11000){
|
||||||
// Should return 500 For other server errors
|
// Should return 409 Conflict for existing urls
|
||||||
res.status(500).json({ message: error });
|
res.status(409).json({ message: "Existing URL" });
|
||||||
|
}
|
||||||
|
// Should return 400 Bad request for invalid requests
|
||||||
|
res.status(400).json({ message: error });
|
||||||
}
|
}
|
||||||
})
|
})
|
||||||
|
|
||||||
|
|
|
@ -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));
|
||||||
|
});
|
||||||
|
});
|
Loading…
Reference in New Issue