diff --git a/.gitea/workflows/test.yaml b/.gitea/workflows/test.yaml index 65dd6e2..2b656bd 100644 --- a/.gitea/workflows/test.yaml +++ b/.gitea/workflows/test.yaml @@ -6,15 +6,17 @@ jobs: container: image: docker:dind volumes: - - /var/lib/docker:/var/lib/docker + - /usr/local/share/.cache/yarn:/usr/local/share/.cache/yarn + - /var/lib/docker/image:/var/lib/docker/image + - /var/lib/docker/overlay2:/var/lib/docker/overlay2 steps: - name: Starting docker daemon run: docker-init -- dockerd --host=unix:///var/run/docker.sock & - name: Installing necessary packages - run: apk add npm git curl bash + run: apk add yarn git curl bash - name: Check out repository code uses: actions/checkout@v3 - name: Install project dependencies - run: npm install + run: yarn install --frozen-lockfile --ignore-scripts - name: Run docker-compose run: docker compose down -v && docker compose run bot-api bun test \ No newline at end of file diff --git a/README.md b/README.md index d4c7f57..62f16e5 100644 --- a/README.md +++ b/README.md @@ -37,13 +37,12 @@ bun install To run: ```bash -docker compose up +bun run dev ``` For testing, remember: ```bash -docker compose down -v -docker compose run bot-api bun run test +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. diff --git a/bunfig.toml b/bunfig.toml new file mode 100644 index 0000000..252e66d --- /dev/null +++ b/bunfig.toml @@ -0,0 +1,4 @@ +[install.lockfile] +# whether to save a non-Bun lockfile alongside bun.lockb +# only "yarn" is supported +print = "yarn" diff --git a/package.json b/package.json index 95c8a32..9e10900 100644 --- a/package.json +++ b/package.json @@ -1,5 +1,5 @@ { - "name": "fib-api", + "name": "bot-api", "module": "index.ts", "type": "module", "devDependencies": { diff --git a/src/app.ts b/src/app.ts index 8ae7edd..e76640e 100644 --- a/src/app.ts +++ b/src/app.ts @@ -15,6 +15,7 @@ app.get("/", (_, res) => { app.get("/images", imageController.getAllImages); app.get("/images/:id", imageController.getImageById); +app.put("/images/:id", authControler.authorize, imageController.editImage); app.post("/images", authControler.authorize, imageController.addImage); app.post("/login", authControler.login); @@ -30,6 +31,7 @@ export const startApp = async () => { user: mongo_user, pass: mongo_pass, }); + mongoose.set("runValidators", true); app.listen(port, () => console.log(`Express server listening on port ${port}`) ); diff --git a/src/controllers/ImageController.ts b/src/controllers/ImageController.ts index 75097fc..d1958c2 100644 --- a/src/controllers/ImageController.ts +++ b/src/controllers/ImageController.ts @@ -4,6 +4,34 @@ import mongoose, { mongo } from "mongoose"; import { Image } from "../models/ImageModel"; class ImageController { + async editImage(req: Request, res: Response) { + try { + const change: Image = req.body; + const result = await imageService.replaceOne(req.params.id, change); + if (result.matchedCount > 0) { + res.status(204).json(); + } else { + res.status(404).json({ error: "Image not found" }); + } + } catch (error: any) { + if (error instanceof mongoose.Error.CastError) { + res.status(400).json({ error: "Invalid Id" }); + } else if (error instanceof mongoose.Error.ValidationError) { + // Should return 400 Bad request for invalid requests + res.status(400).json({ error: error.message }); + } else if ( + error instanceof mongo.MongoServerError && + error.code === 11000 + ) { + // Should return 409 Conflict for existing urls + res.status(409).json({ + error: `the image with URL ${error.keyValue.url} already exists`, + }); + } else { + res.status(500).json({ error: "Internal Server Error" }); + } + } + } async getImageById(req: Request, res: Response) { try { const image = await imageService.findById(req.params.id); diff --git a/src/index.ts b/src/index.ts index 0d016b7..b431ebd 100644 --- a/src/index.ts +++ b/src/index.ts @@ -6,7 +6,7 @@ await startApp(); // This try carch is to prevent hot reload from making the process die due to coliding entries try { // Not insert test data into production - if (process.env.NODE_ENV != "production"){ + if (process.env.NODE_ENV != "production") { await populateDatabase(); } } catch {} diff --git a/src/services/ImageService.ts b/src/services/ImageService.ts index c10c10d..bff9273 100644 --- a/src/services/ImageService.ts +++ b/src/services/ImageService.ts @@ -1,6 +1,10 @@ import imageModel, { Image } from "../models/ImageModel"; class ImageService { + async replaceOne(id: string, newimage: Image) { + const result = await imageModel.updateOne({ _id: id }, newimage); + return result; + } async findById(id: string) { const image = await imageModel.findOne({ _id: id }); return image; diff --git a/tests/app.test.ts b/tests/app.test.ts index 69061a1..1b1c2d0 100644 --- a/tests/app.test.ts +++ b/tests/app.test.ts @@ -229,7 +229,6 @@ describe("POST /images works properly", () => { }); it("should return 400 for malformed requests", async () => { - mock.restore(); const res = await request(app) .post("/images") .set("authorization", `Bearer ${token}`) @@ -242,18 +241,23 @@ describe("POST /images works properly", () => { }); }); -describe("GET /images/:id works properly", () => { +describe("GET /images/ works properly", () => { + let id: string; + let list: Image[]; + beforeAll(async () => { + const res = await request(app).get("/images"); + list = res.body.images; + id = list[0]._id; + }); + it("should return 200 for existing ids", async () => { - const list = await request(app).get("/images"); - const id = list.body.images[0]._id; const res = await request(app).get(`/images/${id}`); expect(res.status).toBe(200); }); it("should return 404 for non-existing ids", async () => { - const list = await request(app).get("/images"); const id = "000000000000000000000000"; // this was the least posible to exist ID - if (!(id in list.body.images)) { + if (!(id in list)) { const res = await request(app).get(`/images/${id}`); expect(res.status).toBe(404); } @@ -264,3 +268,108 @@ describe("GET /images/:id works properly", () => { expect(res.status).toBe(400); }); }); + +describe("PUT /images/ works properly", () => { + let id: string; + let list: Image[]; + beforeAll(async () => { + const res = await request(app).get("/images"); + list = res.body.images; + id = list[0]._id; + }); + + it("should return 204 for valid modifications", async () => { + const res = await request(app) + .put(`/images/${id}`) + .set("authorization", `Bearer ${token}`) + .send({ status: "available" }); + expect(res.status).toBe(204); + }); + + it("should return 404 for non-existing ids", async () => { + const id = "000000000000000000000000"; // this was the least posible to exist ID + if (!(id in list)) { + const res = await request(app) + .put(`/images/${id}`) + .set("authorization", `Bearer ${token}`) + .send({ status: "available" }); + expect(res.status).toBe(404); + } + }); + + it("should return 400 for malformed ids", async () => { + const res = await request(app) + .put("/images/98439384") + .set("authorization", `Bearer ${token}`) + .send({ status: "available" }); + expect(res.status).toBe(400); + expect(res.body).toEqual({ error: "Invalid Id" }); + }); + + it("should return 400 for malformed requests", async () => { + const res = await request(app) + .put(`/images/${id}`) + .set("authorization", `Bearer ${token}`) + .send({ status: "wrong" }); + expect(res.status).toBe(400); + }); + + it("should return 409 if we try to modify the url into an existing one", async () => { + const res = await request(app) + .put(`/images/${id}`) + .set("authorization", `Bearer ${token}`) + .send({ url: "https://test.url.com/2" }); + expect(res.status).toBe(409); + }); + + it("should return 500 for an error on the service", async () => { + mock.module("../src/services/ImageService", () => ({ + default: { + replaceOne: () => { + throw new Error("This is an expected testing error"); + }, + }, + })); + + const res = await request(app) + .put(`/images/${id}`) + .set("authorization", `Bearer ${token}`) + .send({ status: "available" }); + + expect(res.status).toBe(500); + }); + + it("should return 401 for unauthenticated requests", async () => { + const res = await request(app) + .put(`/images/${id}`) + .send({ status: "available" }); + expect(res.status).toBe(401); + }); + + it("should return 403 for invalid tokens", async () => { + const res = await request(app) + .put(`/images/${id}`) + .set("authorization", `Bearer token`) + .send({ status: "available" }); + expect(res.status).toBe(403); + }); + + it("should have its changes be reflected onto the DB", async () => { + const image = await request(app) + .post("/images") + .set("authorization", `Bearer ${token}`) + .send({ + url: "https://test.url.com/5", + status: "available", + tags: ["2girls", "touhou"], + }); + const id = image.body.image._id; + await request(app) + .put(`/images/${id}`) + .set("authorization", `Bearer ${token}`) + .send({ status: "consumed" }); + + const res = await request(app).get(`/images/${id}`); + expect(res.body.image).toHaveProperty("status", "consumed"); + }) +}); diff --git a/yarn.lock b/yarn.lock index 01d8ca0..1cf8075 100644 --- a/yarn.lock +++ b/yarn.lock @@ -1,6 +1,6 @@ # THIS IS AN AUTOGENERATED FILE. DO NOT EDIT THIS FILE DIRECTLY. # yarn lockfile v1 -# bun ./bun.lockb --hash: FB1C344BE4E1C170-a59dde1fb7411c09-538A0DE39E28C689-3912799b87d41761 +# bun ./bun.lockb --hash: 738A3B3B61512E75-26d27a9609e246ac-33059F282FFA6348-27fd5707ca8843fa "@ampproject/remapping@^2.2.0": @@ -25,19 +25,19 @@ integrity sha512-uU27kfDRlhfKl+w1U6vp16IuvSLtjAxdArVXPa9BvLkrr7CYIsxH5adpHObeAGY/41+syctUWOZ140a2Rvkgjw== "@babel/core@>=7.0.0-beta.0 <8", "@babel/core@^7.0.0", "@babel/core@^7.0.0-0", "@babel/core@^7.11.6", "@babel/core@^7.12.3", "@babel/core@^7.8.0": - version "7.23.6" - resolved "https://registry.npmjs.org/@babel/core/-/core-7.23.6.tgz" - integrity sha512-FxpRyGjrMJXh7X3wGLGhNDCRiwpWEF74sKjTLDJSG5Kyvow3QZaG0Adbqzi9ZrVjTWpsX+2cxWXD71NMg93kdw== + version "7.23.7" + resolved "https://registry.npmjs.org/@babel/core/-/core-7.23.7.tgz" + integrity sha512-+UpDgowcmqe36d4NwqvKsyPMlOLNGMsfMmQ5WGCu+siCe3t3dfe9njrzGfdN4qq+bcNUt0+Vw6haRxBOycs4dw== dependencies: "@ampproject/remapping" "^2.2.0" "@babel/code-frame" "^7.23.5" "@babel/generator" "^7.23.6" "@babel/helper-compilation-targets" "^7.23.6" "@babel/helper-module-transforms" "^7.23.3" - "@babel/helpers" "^7.23.6" + "@babel/helpers" "^7.23.7" "@babel/parser" "^7.23.6" "@babel/template" "^7.22.15" - "@babel/traverse" "^7.23.6" + "@babel/traverse" "^7.23.7" "@babel/types" "^7.23.6" convert-source-map "^2.0.0" debug "^4.1.0" @@ -138,13 +138,13 @@ resolved "https://registry.npmjs.org/@babel/helper-validator-option/-/helper-validator-option-7.23.5.tgz" integrity sha512-85ttAOMLsr53VgXkTbkx8oA6YTfT4q7/HzXSLEYmjcSTJPMPQtvq1BD79Byep5xMUYbGRzEpDsjUf3dyp54IKw== -"@babel/helpers@^7.23.6": - version "7.23.6" - resolved "https://registry.npmjs.org/@babel/helpers/-/helpers-7.23.6.tgz" - integrity sha512-wCfsbN4nBidDRhpDhvcKlzHWCTlgJYUUdSJfzXb2NuBssDSIjc3xcb+znA7l+zYsFljAcGM0aFkN40cR3lXiGA== +"@babel/helpers@^7.23.7": + version "7.23.7" + resolved "https://registry.npmjs.org/@babel/helpers/-/helpers-7.23.7.tgz" + integrity sha512-6AMnjCoC8wjqBzDHkuqpa7jAKwvMo4dC+lr/TFBz+ucfulO1XMpDnwWPGBNwClOKZ8h6xn5N81W/R5OrcKtCbQ== dependencies: "@babel/template" "^7.22.15" - "@babel/traverse" "^7.23.6" + "@babel/traverse" "^7.23.7" "@babel/types" "^7.23.6" "@babel/highlight@^7.23.4": @@ -268,10 +268,10 @@ "@babel/parser" "^7.22.15" "@babel/types" "^7.22.15" -"@babel/traverse@^7.23.6": - version "7.23.6" - resolved "https://registry.npmjs.org/@babel/traverse/-/traverse-7.23.6.tgz" - integrity sha512-czastdK1e8YByZqezMPFiZ8ahwVMh/ESl9vPgvgdB9AmFMGP5jfpFax74AQgl5zj4XHzqeYAg2l8PuUeRS1MgQ== +"@babel/traverse@^7.23.7": + version "7.23.7" + resolved "https://registry.npmjs.org/@babel/traverse/-/traverse-7.23.7.tgz" + integrity sha512-tY3mM8rH9jM0YHFGyfC0/xf+SB5eKUu7HPj7/k3fpi9dAlsMc5YbQvDi0Sh2QTPXqMhyaAtzAr807TIyfQrmyg== dependencies: "@babel/code-frame" "^7.23.5" "@babel/generator" "^7.23.6" @@ -596,9 +596,9 @@ "@babel/types" "^7.0.0" "@types/babel__traverse@*", "@types/babel__traverse@^7.0.6": - version "7.20.4" - resolved "https://registry.npmjs.org/@types/babel__traverse/-/babel__traverse-7.20.4.tgz" - integrity sha512-mSM/iKUk5fDDrEV/e83qY+Cr3I1+Q3qqTuEn++HAWYjEa1+NxZr6CNrcJGf2ZTnq4HoFGC3zaTPZTobCzCFukA== + version "7.20.5" + resolved "https://registry.npmjs.org/@types/babel__traverse/-/babel__traverse-7.20.5.tgz" + integrity sha512-WXCyOcRtH37HAUkpXhUduaxdm82b4GSlyTqajXviN4EfiuPgNYR109xMCKvpl6zPIpua0DGlMEDCq+g8EdoheQ== dependencies: "@babel/types" "^7.20.7" @@ -711,16 +711,16 @@ integrity sha512-iJt33IQnVRkqeqC7PzBHPTC6fDlRNRW8vjrgqtScAhrmMwe8c4Eo7+fUGTa+XdWrpEgpyKWMYmi2dIwMAYRzPw== "@types/node@*": - version "20.10.4" - resolved "https://registry.npmjs.org/@types/node/-/node-20.10.4.tgz" - integrity sha512-D08YG6rr8X90YB56tSIuBaddy/UXAA9RKJoFvrsnogAum/0pmjkgi4+2nx96A330FmioegBWmEYQ+syqCFaveg== + version "20.10.6" + resolved "https://registry.npmjs.org/@types/node/-/node-20.10.6.tgz" + integrity sha512-Vac8H+NlRNNlAmDfGUP7b5h/KA+AtWIzuXy0E6OyP8f1tCLYAtPvKRRDJjAPqhpCb0t6U2j7/xqAuLEebW2kiw== dependencies: undici-types "~5.26.4" "@types/qs@*": - version "6.9.10" - resolved "https://registry.npmjs.org/@types/qs/-/qs-6.9.10.tgz" - integrity sha512-3Gnx08Ns1sEoCrWssEgTSJs/rsT2vhGP+Ja9cnnk9k4ALxinORlQneLXFeFKOTJMOeZUFD1s7w+w2AphTpvzZw== + version "6.9.11" + resolved "https://registry.npmjs.org/@types/qs/-/qs-6.9.11.tgz" + integrity sha512-oGk0gmhnEJK4Yyk+oI7EfXsLayXatCWPHary1MtcmbAifkobT9cM9yutG/hZKIseOU0MqbIwQ/u2nn/Gb+ltuQ== "@types/range-parser@*": version "1.2.7" @@ -759,9 +759,9 @@ "@types/methods" "^1.1.4" "@types/supertest@^6.0.1": - version "6.0.1" - resolved "https://registry.npmjs.org/@types/supertest/-/supertest-6.0.1.tgz" - integrity sha512-M1xs8grAWC4RisSEQjyQV0FZzXnL3y796540Q/HCdiPcErwKpcAfvsNQFb4xp+5btSWMOZG1YlDWs2z96pdbcw== + version "6.0.2" + resolved "https://registry.npmjs.org/@types/supertest/-/supertest-6.0.2.tgz" + integrity sha512-137ypx2lk/wTQbW6An6safu9hXmajAifU/s7szAHLN/FeIm5w7yR0Wkl9fdJMRSHwOn4HLAI0DaB2TOORuhPDg== dependencies: "@types/methods" "^1.1.4" "@types/superagent" "^8.1.0" @@ -779,6 +779,13 @@ "@types/node" "*" "@types/webidl-conversions" "*" +"@types/ws@*": + version "8.5.10" + resolved "https://registry.npmjs.org/@types/ws/-/ws-8.5.10.tgz" + integrity sha512-vmQSUcfalpIq0R9q7uTo2lXs6eGIpt9wtnLdMv9LVpIjCA/+ufZRozlVoVelIYixx1ugCBKDhn89vnsEGOCx9A== + dependencies: + "@types/node" "*" + "@types/yargs@^17.0.8": version "17.0.32" resolved "https://registry.npmjs.org/@types/yargs/-/yargs-17.0.32.tgz" @@ -860,7 +867,7 @@ asynckit@^0.4.0: resolved "https://registry.npmjs.org/asynckit/-/asynckit-0.4.0.tgz" integrity sha512-Oei9OH4tRh0YqU3GxhX79dM/mwVgvbZJaSNaRk+bshkj0S5cfHcgYakreBjrHwatXKbz+IoIdYLxrKim2MjW0Q== -babel-jest@^29.0.0, babel-jest@^29.7.0: +babel-jest@^29.7.0: version "29.7.0" resolved "https://registry.npmjs.org/babel-jest/-/babel-jest-29.7.0.tgz" integrity sha512-BrvGY3xZSwEcCzKvKsCi2GgHqDqsYkOP4/by5xCgIwGXQxIEh+8ew3gmrE1y7XRR6LHZIj6yLYnUi/mm2KXKBg== @@ -998,9 +1005,13 @@ buffer-from@^1.0.0: integrity sha512-E+XQCRwSbaaiChtv6k6Dwgc+bx+Bs6vuKJHHl5kox/BaKbhiXzqQOwK4cO22yElGp2OCmjwVhT3HmxgyPGnJfQ== bun-types@latest: - version "1.0.15" - resolved "https://registry.npmjs.org/bun-types/-/bun-types-1.0.15.tgz" - integrity sha512-XkEvWLV1JIhcVIpf2Lu6FXnZUxRUkQVJmgY+VT7os6Tk5X1nkXx11q4Rtu6txsqpDJZfUeZXblnnD59K+6wsVA== + version "1.0.21" + resolved "https://registry.npmjs.org/bun-types/-/bun-types-1.0.21.tgz" + integrity sha512-Ugagjf+XZUXDvxDRa3EnhLeMzm2g8ZYFleBF55Ac3GZSzPqdMLAdK9kvZB6M1H4nAFvrEHdV2PHqkzIoNs+3wQ== + dependencies: + "@types/node" "*" + "@types/ws" "*" + undici-types "^5.26.4" bytes@3.1.2: version "3.1.2" @@ -1032,9 +1043,9 @@ camelcase@^6.2.0: integrity sha512-Gmy6FhYlCY7uOElZUSbxo2UCDH8owEk996gkbrpsgGtrJLM3J7jGxl9Ic7Qwwj4ivOE5AWZWRMecDdF7hqGjFA== caniuse-lite@^1.0.30001565: - version "1.0.30001571" - resolved "https://registry.npmjs.org/caniuse-lite/-/caniuse-lite-1.0.30001571.tgz" - integrity sha512-tYq/6MoXhdezDLFZuCO/TKboTzuQ/xR5cFdgXPfDtM7/kchBO3b4VWghE/OAi/DV7tTdhmLjZiZBZi1fA/GheQ== + version "1.0.30001574" + resolved "https://registry.npmjs.org/caniuse-lite/-/caniuse-lite-1.0.30001574.tgz" + integrity sha512-BtYEK4r/iHt/txm81KBudCUcTy7t+s9emrIaHqjYurQ10x71zJ5VQ9x1dYPcz/b+pKSp4y/v1xSI67A+LzpNyg== chalk@^2.4.2: version "2.4.2" @@ -1261,9 +1272,9 @@ ee-first@1.1.1: integrity sha512-WMwm9LhRUo+WUaRN+vRuETqG89IgZphVSNkdFgeb6sS/E4OrDIN7t48CAewSHXc6C8lefD8KKfr5vY61brQlow== electron-to-chromium@^1.4.601: - version "1.4.616" - resolved "https://registry.npmjs.org/electron-to-chromium/-/electron-to-chromium-1.4.616.tgz" - integrity sha512-1n7zWYh8eS0L9Uy+GskE0lkBUNK83cXTVJI0pU3mGprFsbfSdAc15VTFbo+A+Bq4pwstmL30AVcEU3Fo463lNg== + version "1.4.623" + resolved "https://registry.npmjs.org/electron-to-chromium/-/electron-to-chromium-1.4.623.tgz" + integrity sha512-lKoz10iCYlP1WtRYdh5MvocQPWVRoI7ysp6qf18bmeBgR8abE6+I2CsfyNKztRDZvhdWc+krKT6wS7Neg8sw3A== emittery@^0.13.1: version "0.13.1" @@ -2916,7 +2927,7 @@ type-is@~1.6.18: resolved "https://registry.npmjs.org/typescript/-/typescript-5.3.3.tgz" integrity sha512-pXWcraxM0uxAS+tN0AG/BF2TyqmHO014Z070UsJ+pFvYuRSq8KH8DmWpnbXe0pEPDHXZV3FcAbJkijJ5oNEnWw== -undici-types@~5.26.4: +undici-types@^5.26.4, undici-types@~5.26.4: version "5.26.5" resolved "https://registry.npmjs.org/undici-types/-/undici-types-5.26.5.tgz" integrity sha512-JlCMO+ehdEIKqlFxk6IfVoAUVmgz7cU7zD/h9XZ0qzeosSHmUJVOzSQvvYSYWXkFXC+IfLKSIffhv0sVZup6pA==