From aa1dc44eaed56e87d67ded0041fdb5dc8d5f525c Mon Sep 17 00:00:00 2001 From: Sugui Date: Mon, 25 Dec 2023 11:17:56 +0100 Subject: [PATCH 1/4] images endpoint --- src/index.ts | 15 ++++++++++++++- 1 file changed, 14 insertions(+), 1 deletion(-) diff --git a/src/index.ts b/src/index.ts index 10401fc..cc1c227 100644 --- a/src/index.ts +++ b/src/index.ts @@ -6,7 +6,6 @@ const app = express(); app.use(express.json()); -// Hello World GET endpoint app.get("/", (_, res) => { res.json({ message: "Blazing fast 🚀" }); }); @@ -20,6 +19,20 @@ app.get("/images", async (req, res) => { } }) +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 + 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 }); + } +}) + // Set the default port to 8080, or use the PORT environment variable const start = async () => { -- 2.39.2 From c363947870ffebc9b05d8f681ad522ed8276af54 Mon Sep 17 00:00:00 2001 From: Alie Date: Mon, 25 Dec 2023 12:21:01 +0100 Subject: [PATCH 2/4] added the logic for the endpoint and made a fix in an index that was wrong --- mongo-init.js | 2 +- src/index.ts | 15 +++++++++------ 2 files changed, 10 insertions(+), 7 deletions(-) diff --git a/mongo-init.js b/mongo-init.js index c02de9c..a7b191b 100644 --- a/mongo-init.js +++ b/mongo-init.js @@ -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", diff --git a/src/index.ts b/src/index.ts index cc1c227..3308c10 100644 --- a/src/index.ts +++ b/src/index.ts @@ -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 }); } }) -- 2.39.2 From 532de04592d1106c11626c143efc5cc83422bb1f Mon Sep 17 00:00:00 2001 From: Alie Date: Mon, 25 Dec 2023 12:33:40 +0100 Subject: [PATCH 3/4] added tests for POST, fixed tests for GET --- tests/app.test.ts | 41 +++++++++++++++++++++++++++++++++++------ 1 file changed, 35 insertions(+), 6 deletions(-) diff --git a/tests/app.test.ts b/tests/app.test.ts index feb519f..a375b81 100644 --- a/tests/app.test.ts +++ b/tests/app.test.ts @@ -1,22 +1,51 @@ -import { describe, expect, it } from "bun:test"; +import { beforeAll, describe, expect, it } from "bun:test"; import request from "supertest"; import { app } from "../src"; -describe("GET /images works properly", () => { +describe("GET /images works properly", async () => { + const res = await request(app).get("/images"); + it("should be an array", async () => { - const res = await request(app).get("/images"); 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 or 409", async () => { + it("should return 201 for new image", async () => { const res = await request(app).post("/images").send({ - url: "https://test.url.com/123", + url: "https://test.url.com/1", status: "available", tags: ["2girls", "touhou"] }); - expect(res.status).toSatisfy(status => [201, 409].includes(status)); + 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)); }); }); \ No newline at end of file -- 2.39.2 From e29ef4b95385075c1be27ecf000d6bd61af162a4 Mon Sep 17 00:00:00 2001 From: Alie Date: Mon, 25 Dec 2023 12:35:59 +0100 Subject: [PATCH 4/4] updated readme --- README.md | 8 +++++++- 1 file changed, 7 insertions(+), 1 deletion(-) diff --git a/README.md b/README.md index 5998e95..c3ca5ca 100644 --- a/README.md +++ b/README.md @@ -9,7 +9,13 @@ bun install To run: ```bash -bun run index.ts +docker compose up +``` + +For testing, remember: +```bash +docker compose down -v +docker compose run bot-api bun run test ``` This project was created using `bun init` in bun v1.0.13. [Bun](https://bun.sh) is a fast all-in-one JavaScript runtime. -- 2.39.2