From 1b45481d279172f3be5c1ad0767b43a1b115706a Mon Sep 17 00:00:00 2001 From: Sugui Date: Fri, 29 Dec 2023 15:24:29 +0100 Subject: [PATCH 01/64] commit all changes to tests --- compose.yaml | 3 +- mongo-init.js | 30 ----- package.json | 2 + src/app.ts | 41 +++++++ src/index.ts | 43 +------ src/models/AuthModel.ts | 3 +- src/models/ImageModel.ts | 5 +- src/services/AuthService.ts | 4 +- tests/app.test.ts | 227 +++++++++++++++++++----------------- tests/auth.test.ts | 25 +++- tests/memoryServer.ts | 19 +++ tests/populateDatabase.ts | 15 +++ 12 files changed, 230 insertions(+), 187 deletions(-) delete mode 100644 mongo-init.js create mode 100644 src/app.ts create mode 100644 tests/memoryServer.ts create mode 100644 tests/populateDatabase.ts diff --git a/compose.yaml b/compose.yaml index 964794b..2ca513c 100644 --- a/compose.yaml +++ b/compose.yaml @@ -12,7 +12,6 @@ services: MONGO_INITDB_DATABASE: bot volumes: - mongodb_data:/data/db - - ./mongo-init.js:/docker-entrypoint-initdb.d/mongo-init.js:ro bot-api: image: oven/bun:1 @@ -28,6 +27,8 @@ services: MONGODB_USER: "root" MONGODB_PASS: "password" JWTSECRET: "cooljwtsecret" + # DEDICATED_MONGODB_SERVER is for integration testing, for disabling memory server in tests + DEDICATED_MONGODB_SERVER: "true" volumes: - ./:/usr/src/app:ro diff --git a/mongo-init.js b/mongo-init.js deleted file mode 100644 index 67b53c9..0000000 --- a/mongo-init.js +++ /dev/null @@ -1,30 +0,0 @@ -db.createUser({ - user: "root", - pwd: "password", - roles: [ - { - role: "readWrite", - db: "admin", - }, - { - role: "readWrite", - db: "bot", - }, - ], -}); - -db = new Mongo().getDB("bot"); - -db.images.createIndex({ status: 1 }); -db.images.createIndex({ url: 1 }, { unique: true }); -db.images.insert({ - url: "https://example.com", - status: "consumed", - tags: ["2girls", "sleeping"], -}); - -db.authorizations.createIndex({ app: 1 }); -db.authorizations.insert({ - app: "tester", - secret: "test", -}); diff --git a/package.json b/package.json index 2080221..6d9c33c 100644 --- a/package.json +++ b/package.json @@ -4,9 +4,11 @@ "type": "module", "devDependencies": { "@types/jest": "^29.5.11", + "@types/mongodb-memory-server": "^2.3.0", "@types/supertest": "^6.0.1", "bun-types": "latest", "jest": "^29.7.0", + "mongodb-memory-server": "^9.1.3", "supertest": "^6.3.3", "ts-jest": "^29.1.1" }, diff --git a/src/app.ts b/src/app.ts new file mode 100644 index 0000000..e132aa4 --- /dev/null +++ b/src/app.ts @@ -0,0 +1,41 @@ +import express from "express"; +import listEndpoints from "express-list-endpoints"; +import imageController from "./controllers/ImageController"; +import authControler from "./controllers/AuthControler"; +import mongoose from "mongoose"; + +export const app = express(); + +app.use(express.json()); + +app.get("/", (_, res) => { + const endpoints = listEndpoints(app); + res.json({ endpoints }); +}); + +app.get("/images", imageController.getAllImages); +app.post("/images", authControler.authorize, imageController.addImage); +app.post("/login", authControler.login); + +export const startApp = async () => { + const port = process.env.PORT || 8080; + const mongo_uri: string = process.env.MONGODB_URI || ""; + const mongo_user = process.env.MONGODB_USER; + const mongo_pass = process.env.MONGODB_PASS; + + try { + await mongoose.connect(mongo_uri, { + authSource: "admin", + user: mongo_user, + pass: mongo_pass, + }); + app.listen(port, () => + console.log(`Express server listening on port ${port}`) + ); + } catch (error) { + console.error(error); + process.exit(1); + } + }; + +export default app; \ No newline at end of file diff --git a/src/index.ts b/src/index.ts index d74231b..cdff0b9 100644 --- a/src/index.ts +++ b/src/index.ts @@ -1,42 +1,3 @@ -import express from "express"; -import mongoose from "mongoose"; -import listEndpoints from "express-list-endpoints"; -import imageController from "./controllers/ImageController"; -import authControler from "./controllers/AuthControler"; +import { startApp } from "./app"; -export const app = express(); - -app.use(express.json()); - -app.get("/", (_, res) => { - const endpoints = listEndpoints(app); - res.json({ endpoints }); -}); - -app.get("/images", imageController.getAllImages); -app.post("/images", authControler.authorize, imageController.addImage); -app.post("/login", authControler.login); - -const start = async () => { - // Set the default port to 8080, or use the PORT environment variable - const port = process.env.PORT || 8080; - const mongo_uri: string = process.env.MONGODB_URI || ""; - const mongo_user = process.env.MONGODB_USER; - const mongo_pass = process.env.MONGODB_PASS; - - try { - await mongoose.connect(mongo_uri, { - authSource: "admin", - user: mongo_user, - pass: mongo_pass, - }); - app.listen(port, () => - console.log(`Express server listening on port ${port}`) - ); - } catch (error) { - console.error(error); - process.exit(1); - } -}; - -start(); +startApp(); \ No newline at end of file diff --git a/src/models/AuthModel.ts b/src/models/AuthModel.ts index df71afe..96ce5e5 100644 --- a/src/models/AuthModel.ts +++ b/src/models/AuthModel.ts @@ -9,11 +9,12 @@ const AuthSchema = new mongoose.Schema({ app: { type: String, required: true, + index: true, }, secret: { type: String, required: true, }, -}); +}, { collection: "authorizations" }); export default mongoose.model("authorizations", AuthSchema); diff --git a/src/models/ImageModel.ts b/src/models/ImageModel.ts index d91ab18..b3bff2c 100644 --- a/src/models/ImageModel.ts +++ b/src/models/ImageModel.ts @@ -10,6 +10,8 @@ const ImageSchema = new mongoose.Schema({ url: { type: String, required: true, + index: true, + unique: true }, status: { type: String, @@ -17,10 +19,11 @@ const ImageSchema = new mongoose.Schema({ values: ["consumed", "unavailable", "available"], }, required: true, + index: true, }, tags: { type: [String], }, -}); +}, { collection: "images" }); export default mongoose.model("images", ImageSchema); diff --git a/src/services/AuthService.ts b/src/services/AuthService.ts index aed8d12..d44e7eb 100644 --- a/src/services/AuthService.ts +++ b/src/services/AuthService.ts @@ -1,8 +1,8 @@ import AuthModel, { Auth } from "../models/AuthModel"; class AuthService { - async find(app: String, secret: String): Promise { - const auth = await AuthModel.findOne({ app: app, secret: secret }).exec(); + async find(app: string, secret: string): Promise { + const auth = await AuthModel.findOne({ app: app, secret: secret }); return auth; } } diff --git a/tests/app.test.ts b/tests/app.test.ts index ac40ace..72a6ebc 100644 --- a/tests/app.test.ts +++ b/tests/app.test.ts @@ -1,134 +1,151 @@ -import { afterEach, beforeAll, describe, expect, it, mock } from "bun:test"; +import { afterAll, afterEach, beforeAll, describe, expect, it, mock } from "bun:test"; import request from "supertest"; -import { app } from "../src"; +import app, { startApp } from "../src/app"; import imageService from "../src/services/ImageService"; +import memoryServer from "./memoryServer"; +import populateDatabase from "./populateDatabase"; const imageServiceOriginal = imageService; -const tok = await request(app) - .post("/login") - .send({ app: "tester", secret: "test" }); -const token = tok.body.token; + +let token: string; + +beforeAll(async () => { + if (!process.env.DEDICATED_MONGODB_SERVER) + await memoryServer.start(); + await startApp(); + await populateDatabase(); + + const tok = await request(app) + .post("/login") + .send({ app: "tester", secret: "test" }); + token = tok.body.token; +}); + +afterAll(async () => { + if (!process.env.DEDICATED_MONGODB_SERVER) + await memoryServer.stop(); +}) afterEach(() => { - mock.restore(); - mock.module("../src/services/ImageService", () => ({ - default: imageServiceOriginal, - })); + mock.restore(); + mock.module("../src/services/ImageService", () => ({ + default: imageServiceOriginal, + })); }); describe("GET / shows all of the endpoints", async () => { - const res = await request(app).get("/"); + const res = await request(app).get("/"); - it("should be", async () => { - expect(res.body).toHaveProperty("endpoints"); - }); + it("should be", async () => { + expect(res.body).toHaveProperty("endpoints"); + }); - it("should be an array", () => { - expect(Array.isArray(res.body.endpoints)).toBeTrue(); - }); + it("should be an array", () => { + expect(Array.isArray(res.body.endpoints)).toBeTrue(); + }); }); describe("GET /images works properly", async () => { - const res = await request(app).get("/images"); + const res = await request(app).get("/images"); - it("should be an array", () => { - expect(Array.isArray(res.body.images)).toBeTrue(); - }); + it("should be an array", () => { + expect(Array.isArray(res.body.images)).toBeTrue(); + }); - it("should return a 200", async () => { - expect(res.statusCode).toBe(200); - }); + it("should return a 200", async () => { + expect(res.statusCode).toBe(200); + }); }); describe("POST /images works properly", () => { - it("should return 401 for unauthenticated requests", async () => { - const res = await request(app) - .post("/images") - .send({ - url: "https://test.url.com/0", - status: "available", - tags: ["2girls", "touhou"], - }); - expect(res.status).toBe(401); - }); + it("should return 401 for unauthenticated requests", async () => { + const res = await request(app) + .post("/images") + .send({ + url: "https://test.url.com/0", + status: "available", + tags: ["2girls", "touhou"], + }); + expect(res.status).toBe(401); + }); - it("should return 403 for invalid tokens", async () => { - const res = await request(app) - .post("/images") - .set("authorization", `Bearer token`) - .send({ - url: "https://test.url.com/0", - status: "available", - tags: ["2girls", "touhou"], - }); - expect(res.status).toBe(403); - }); + it("should return 403 for invalid tokens", async () => { + const res = await request(app) + .post("/images") + .set("authorization", `Bearer token`) + .send({ + url: "https://test.url.com/0", + status: "available", + tags: ["2girls", "touhou"], + }); + expect(res.status).toBe(403); + }); - it("should return 201 for new image", async () => { - const res = await request(app) - .post("/images") - .set("authorization", `Bearer ${token}`) - .send({ - url: "https://test.url.com/1", - status: "available", - tags: ["2girls", "touhou"], - }); - expect(res.status).toBe(201); - }); + it("should return 201 for new image", async () => { + const res = await request(app) + .post("/images") + .set("authorization", `Bearer ${token}`) + .send({ + url: "https://test.url.com/1", + status: "available", + tags: ["2girls", "touhou"], + }); + expect(res.status).toBe(201); + }); - it("should return 409 for a repeated images", async () => { - await request(app) - .post("/images") - .set("authorization", `Bearer ${token}`) - .send({ - url: "https://test.url.com/2", - status: "available", - tags: ["2girls", "touhou"], - }); + it("should return 409 for a repeated images", async () => { + await request(app) + .post("/images") + .set("authorization", `Bearer ${token}`) + .send({ + url: "https://test.url.com/2", + status: "available", + tags: ["2girls", "touhou"], + }); - const res = await request(app) - .post("/images") - .set("authorization", `Bearer ${token}`) - .send({ - url: "https://test.url.com/2", - status: "available", - tags: ["2girls", "touhou"], - }); + const res = await request(app) + .post("/images") + .set("authorization", `Bearer ${token}`) + .send({ + url: "https://test.url.com/2", + status: "available", + tags: ["2girls", "touhou"], + }); - expect(res.status).toBe(409); - }); + expect(res.status).toBe(409); + }); - it("should return 500 for an error on the service", async () => { - mock.module("../src/services/ImageService", () => ({ - default: { - add: () => { - throw new Error("This is an expected testing error"); - }, - }, - })); + it("should return 500 for an error on the service", async () => { + mock.module("../src/services/ImageService", () => ({ + default: { + add: () => { + throw new Error("This is an expected testing error"); + }, + }, + })); - const res = await request(app) - .post("/images") - .set("authorization", `Bearer ${token}`) - .send({ - url: "https://test.url.com/3", - status: "available", - tags: ["2girls", "touhou"], - }); + const res = await request(app) + .post("/images") + .set("authorization", `Bearer ${token}`) + .send({ + url: "https://test.url.com/3", + status: "available", + tags: ["2girls", "touhou"], + }); - expect(res.status).toBe(500); - }); + expect(res.status).toBe(500); + }); - it("should return 400 for malformed requests", async () => { - mock.restore(); - const res = await request(app) - .post("/images") - .set("authorization", `Bearer ${token}`) - .send({ - url: "https://test.url.com/4", - status: "wrong", - tags: ["2girls", "touhou"], - }); - expect(res.status).toBe(400); - }); + it("should return 400 for malformed requests", async () => { + mock.restore(); + const res = await request(app) + .post("/images") + .set("authorization", `Bearer ${token}`) + .send({ + url: "https://test.url.com/4", + status: "wrong", + tags: ["2girls", "touhou"], + }); + expect(res.status).toBe(400); + }); }); diff --git a/tests/auth.test.ts b/tests/auth.test.ts index bd20cd2..18c3789 100644 --- a/tests/auth.test.ts +++ b/tests/auth.test.ts @@ -1,12 +1,25 @@ -import { describe, expect, it, mock } from "bun:test"; +import { afterAll, beforeAll, describe, expect, it } from "bun:test"; import request from "supertest"; -import { app } from "../src"; +import app, { startApp } from "../src/app"; +import memoryServer from "./memoryServer"; +import populateDatabase from "./populateDatabase"; + +beforeAll(async () => { + if (!process.env.DEDICATED_MONGODB_SERVER) + await memoryServer.start(); + await startApp(); + await populateDatabase(); +}); + +afterAll(async () => { + if (!process.env.DEDICATED_MONGODB_SERVER) + await memoryServer.stop(); +}); describe("/login", async () => { - const correctRespose = await request(app).post("/login").send({ - app: "tester", - secret: "test", - }); + const correctRespose = await request(app) + .post("/login") + .send({ app: "tester", secret: "test" }); it("should return 200 for correct login", () => { expect(correctRespose.status).toBe(200); }); diff --git a/tests/memoryServer.ts b/tests/memoryServer.ts new file mode 100644 index 0000000..8dd7513 --- /dev/null +++ b/tests/memoryServer.ts @@ -0,0 +1,19 @@ +import { MongoMemoryServer } from "mongodb-memory-server"; + +class MemoryServer { + mongod: MongoMemoryServer | undefined; + + async start() { + this.mongod = await MongoMemoryServer.create(); + const uri = this.mongod.getUri("bot"); + process.env.MONGODB_URI = uri; + } + + async stop() { + if (this.mongod) { + await this.mongod.stop(); + } + } +} + +export default new MemoryServer(); \ No newline at end of file diff --git a/tests/populateDatabase.ts b/tests/populateDatabase.ts new file mode 100644 index 0000000..93f63c1 --- /dev/null +++ b/tests/populateDatabase.ts @@ -0,0 +1,15 @@ +import authModel from "../src/models/AuthModel"; +import imageModel from "../src/models/ImageModel"; + +export default async function () { + await imageModel.create({ + url: "https://example.com", + status: "consumed", + tags: ["2girls", "sleeping"], + }); + + await authModel.create({ + app: "tester", + secret: "test", + }); +} -- 2.39.2 From 7c68174ca40d5be567ed4f38d4c3c5a171cb7e19 Mon Sep 17 00:00:00 2001 From: Alie Date: Fri, 29 Dec 2023 16:05:51 +0100 Subject: [PATCH 02/64] fixed the test issue, before all and things --- src/controllers/AuthControler.ts | 3 ++- src/index.ts | 11 ++++++++++- src/services/AuthService.ts | 4 +++- tests/auth.test.ts | 21 ++++++++++++--------- 4 files changed, 27 insertions(+), 12 deletions(-) diff --git a/src/controllers/AuthControler.ts b/src/controllers/AuthControler.ts index 4f0649c..68c69ac 100644 --- a/src/controllers/AuthControler.ts +++ b/src/controllers/AuthControler.ts @@ -7,13 +7,14 @@ const authTokenSecret = process.env.JWTSECRET || "badsecret"; class AuthControler { async login(req: Request, res: Response) { // Read app and secret from request body + console.log(req.body) const { app, secret } = req.body; // Filter app from the apps by app and secret const authenticated = await AuthService.find(app, secret); if (authenticated) { - console.log("Authenticated app ", authenticated.app); + console.log("Authenticated app", authenticated.app); // Generate an access token const accessToken = jwt.sign( { app: authenticated.app }, diff --git a/src/index.ts b/src/index.ts index cdff0b9..7155635 100644 --- a/src/index.ts +++ b/src/index.ts @@ -1,3 +1,12 @@ +import populateDatabase from "../tests/populateDatabase"; import { startApp } from "./app"; -startApp(); \ No newline at end of file +await startApp(); + + +try{ + await populateDatabase(); +} +catch { + +} \ No newline at end of file diff --git a/src/services/AuthService.ts b/src/services/AuthService.ts index d44e7eb..9dcdabe 100644 --- a/src/services/AuthService.ts +++ b/src/services/AuthService.ts @@ -1,8 +1,10 @@ import AuthModel, { Auth } from "../models/AuthModel"; class AuthService { - async find(app: string, secret: string): Promise { + async find(app: String, secret: String): Promise { + console.log(app, secret) const auth = await AuthModel.findOne({ app: app, secret: secret }); + console.log(auth) return auth; } } diff --git a/tests/auth.test.ts b/tests/auth.test.ts index 18c3789..affe74f 100644 --- a/tests/auth.test.ts +++ b/tests/auth.test.ts @@ -1,28 +1,31 @@ import { afterAll, beforeAll, describe, expect, it } from "bun:test"; -import request from "supertest"; +import request, { Response } from "supertest"; import app, { startApp } from "../src/app"; import memoryServer from "./memoryServer"; import populateDatabase from "./populateDatabase"; beforeAll(async () => { - if (!process.env.DEDICATED_MONGODB_SERVER) - await memoryServer.start(); + if (!process.env.DEDICATED_MONGODB_SERVER) await memoryServer.start(); await startApp(); await populateDatabase(); }); afterAll(async () => { - if (!process.env.DEDICATED_MONGODB_SERVER) - await memoryServer.stop(); + if (!process.env.DEDICATED_MONGODB_SERVER) await memoryServer.stop(); }); describe("/login", async () => { - const correctRespose = await request(app) - .post("/login") - .send({ app: "tester", secret: "test" }); - it("should return 200 for correct login", () => { + let correctRespose: Response; + beforeAll(async () => { + correctRespose = await request(app) + .post("/login") + .send({ app: "tester", secret: "test" }); + }); + + it("should return 200 for correct login", async () => { expect(correctRespose.status).toBe(200); }); + it("should contain a token", () => { expect(correctRespose.body).toHaveProperty("token"); }); -- 2.39.2 From 6acb9ee3d97d962cb1b989405308b569c0bc4ae6 Mon Sep 17 00:00:00 2001 From: Alie Date: Fri, 29 Dec 2023 16:08:09 +0100 Subject: [PATCH 03/64] changes to the action --- .gitea/workflows/test.yaml | 16 ++++++---------- 1 file changed, 6 insertions(+), 10 deletions(-) diff --git a/.gitea/workflows/test.yaml b/.gitea/workflows/test.yaml index 0cca8ee..c4db36b 100644 --- a/.gitea/workflows/test.yaml +++ b/.gitea/workflows/test.yaml @@ -1,19 +1,15 @@ -name: Gitea Actions Demo +name: Unit Tests run-name: ${{ gitea.actor }} is testing out Gitea Actions 🚀 on: [pull_request] jobs: - Explore-Gitea-Actions: + unit-test: container: image: oven/bun:1 steps: - - run: echo "🎉 The job was automatically triggered by a ${{ gitea.event_name }} event." - - run: echo "🐧 This job is now running on a ${{ runner.os }} server hosted by Gitea!" - - run: echo "🔎 The name of your branch is ${{ gitea.ref }} and your repository is ${{ gitea.repository }}." - name: Check out repository code uses: actions/checkout@v3 - - run: echo "💡 The ${{ gitea.repository }} repository has been cloned to the runner." - - run: echo "🖥️ The workflow is now ready to test your code on the runner." - - name: List files in the repository - run: | - ls ${{ gitea.workspace }} \ No newline at end of file + - name: Bun install + run: bun install + - name: Run the test + run: bun test \ No newline at end of file -- 2.39.2 From 1c05d114a4665da52f705ac5c568badca91fc8f7 Mon Sep 17 00:00:00 2001 From: Alie Date: Fri, 29 Dec 2023 16:11:49 +0100 Subject: [PATCH 04/64] changed the image --- .gitea/workflows/test.yaml | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/.gitea/workflows/test.yaml b/.gitea/workflows/test.yaml index c4db36b..80a39fb 100644 --- a/.gitea/workflows/test.yaml +++ b/.gitea/workflows/test.yaml @@ -4,11 +4,11 @@ on: [pull_request] jobs: unit-test: - container: - image: oven/bun:1 steps: - name: Check out repository code uses: actions/checkout@v3 + - name: Install Bun + run: curl -fsSL https://bun.sh/install - name: Bun install run: bun install - name: Run the test -- 2.39.2 From e5d191dd58900523ce70b2d26cabb64115a8bdcb Mon Sep 17 00:00:00 2001 From: Alie Date: Fri, 29 Dec 2023 16:14:37 +0100 Subject: [PATCH 05/64] forgor to pipe --- .gitea/workflows/test.yaml | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/.gitea/workflows/test.yaml b/.gitea/workflows/test.yaml index 80a39fb..2b9102c 100644 --- a/.gitea/workflows/test.yaml +++ b/.gitea/workflows/test.yaml @@ -7,9 +7,9 @@ jobs: steps: - name: Check out repository code uses: actions/checkout@v3 - - name: Install Bun - run: curl -fsSL https://bun.sh/install - - name: Bun install + - name: Install Bun runtime + run: curl -fsSL https://bun.sh/install | sh + - name: Install dependencies run: bun install - name: Run the test run: bun test \ No newline at end of file -- 2.39.2 From d9814a28eb74b431603919f208136718c6c2c0c0 Mon Sep 17 00:00:00 2001 From: Alie Date: Fri, 29 Dec 2023 16:16:57 +0100 Subject: [PATCH 06/64] container --- .gitea/workflows/test.yaml | 2 ++ 1 file changed, 2 insertions(+) diff --git a/.gitea/workflows/test.yaml b/.gitea/workflows/test.yaml index 2b9102c..df77dfe 100644 --- a/.gitea/workflows/test.yaml +++ b/.gitea/workflows/test.yaml @@ -4,6 +4,8 @@ on: [pull_request] jobs: unit-test: + container: + image: node:16-bullseye steps: - name: Check out repository code uses: actions/checkout@v3 -- 2.39.2 From 90b617f86ca46aaa9076e8f263e1577a1a1acb8a Mon Sep 17 00:00:00 2001 From: Alie Date: Fri, 29 Dec 2023 16:17:50 +0100 Subject: [PATCH 07/64] bash --- .gitea/workflows/test.yaml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/.gitea/workflows/test.yaml b/.gitea/workflows/test.yaml index df77dfe..f58295e 100644 --- a/.gitea/workflows/test.yaml +++ b/.gitea/workflows/test.yaml @@ -10,7 +10,7 @@ jobs: - name: Check out repository code uses: actions/checkout@v3 - name: Install Bun runtime - run: curl -fsSL https://bun.sh/install | sh + run: curl -fsSL https://bun.sh/install | bash - name: Install dependencies run: bun install - name: Run the test -- 2.39.2 From 1aa75a21fd1b58ba6303716a757bd235eaccb94b Mon Sep 17 00:00:00 2001 From: Alie Date: Fri, 29 Dec 2023 16:18:53 +0100 Subject: [PATCH 08/64] path --- .gitea/workflows/test.yaml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/.gitea/workflows/test.yaml b/.gitea/workflows/test.yaml index f58295e..9b8a134 100644 --- a/.gitea/workflows/test.yaml +++ b/.gitea/workflows/test.yaml @@ -10,7 +10,7 @@ jobs: - name: Check out repository code uses: actions/checkout@v3 - name: Install Bun runtime - run: curl -fsSL https://bun.sh/install | bash + run: curl -fsSL https://bun.sh/install | bash && source /root/.bashrc - name: Install dependencies run: bun install - name: Run the test -- 2.39.2 From 1da18fba4e3b10d0f9ad080029cba0ad9fa772dd Mon Sep 17 00:00:00 2001 From: Alie Date: Fri, 29 Dec 2023 16:20:18 +0100 Subject: [PATCH 09/64] why does it not let you source??? --- .gitea/workflows/test.yaml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/.gitea/workflows/test.yaml b/.gitea/workflows/test.yaml index 9b8a134..783ca77 100644 --- a/.gitea/workflows/test.yaml +++ b/.gitea/workflows/test.yaml @@ -10,7 +10,7 @@ jobs: - name: Check out repository code uses: actions/checkout@v3 - name: Install Bun runtime - run: curl -fsSL https://bun.sh/install | bash && source /root/.bashrc + run: curl -fsSL https://bun.sh/install | bash && bash - name: Install dependencies run: bun install - name: Run the test -- 2.39.2 From c8fa9cc2ddf6b328ec198d8f1fd99313b1b8ac94 Mon Sep 17 00:00:00 2001 From: Alie Date: Fri, 29 Dec 2023 16:22:03 +0100 Subject: [PATCH 10/64] ok, then directly execute --- .gitea/workflows/test.yaml | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/.gitea/workflows/test.yaml b/.gitea/workflows/test.yaml index 783ca77..f4b14f5 100644 --- a/.gitea/workflows/test.yaml +++ b/.gitea/workflows/test.yaml @@ -10,8 +10,8 @@ jobs: - name: Check out repository code uses: actions/checkout@v3 - name: Install Bun runtime - run: curl -fsSL https://bun.sh/install | bash && bash + run: curl -fsSL https://bun.sh/install | bash - name: Install dependencies - run: bun install + run: ~/.bun/bin/bun install - name: Run the test - run: bun test \ No newline at end of file + run: ~/.bun/bin/bun test \ No newline at end of file -- 2.39.2 From 86f20e6b45ed80541117bc53fcd2af179248ccd4 Mon Sep 17 00:00:00 2001 From: Alie Date: Fri, 29 Dec 2023 16:25:42 +0100 Subject: [PATCH 11/64] there is no mongo dor debian11 aarch64 :) --- .gitea/workflows/test.yaml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/.gitea/workflows/test.yaml b/.gitea/workflows/test.yaml index f4b14f5..c1746f2 100644 --- a/.gitea/workflows/test.yaml +++ b/.gitea/workflows/test.yaml @@ -5,7 +5,7 @@ on: [pull_request] jobs: unit-test: container: - image: node:16-bullseye + image: catthehacker/ubuntu:act-latest steps: - name: Check out repository code uses: actions/checkout@v3 -- 2.39.2 From e64474a4f472f60d0dcee4f59bcdb064182112f9 Mon Sep 17 00:00:00 2001 From: Sugui Date: Fri, 29 Dec 2023 16:37:24 +0100 Subject: [PATCH 12/64] Testing action with docker compose --- .gitea/workflows/test-compose.yaml | 17 +++++++++++++++++ 1 file changed, 17 insertions(+) create mode 100644 .gitea/workflows/test-compose.yaml diff --git a/.gitea/workflows/test-compose.yaml b/.gitea/workflows/test-compose.yaml new file mode 100644 index 0000000..3b3b6d2 --- /dev/null +++ b/.gitea/workflows/test-compose.yaml @@ -0,0 +1,17 @@ +name: Unit Tests with docker compose +run-name: ${{ gitea.actor }} is testing out Gitea Actions 🚀 +on: [pull_request] + +jobs: + unit-test: + container: + image: catthehacker/ubuntu:act-latest + steps: + - name: Check out repository code + uses: actions/checkout@v3 + - name: Install Bun runtime + run: curl -fsSL https://bun.sh/install | bash + - name: Install docker-compose + run: apt install -y docker.io + - name: Run docker-compose + run: docker compose down -v && docker compose run bot-api bun test \ No newline at end of file -- 2.39.2 From 2684b2e1d51b5c8a52577890e496b756e2fef837 Mon Sep 17 00:00:00 2001 From: Sugui Date: Fri, 29 Dec 2023 16:39:54 +0100 Subject: [PATCH 13/64] update --- .gitea/workflows/test-compose.yaml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/.gitea/workflows/test-compose.yaml b/.gitea/workflows/test-compose.yaml index 3b3b6d2..7b2ece1 100644 --- a/.gitea/workflows/test-compose.yaml +++ b/.gitea/workflows/test-compose.yaml @@ -12,6 +12,6 @@ jobs: - name: Install Bun runtime run: curl -fsSL https://bun.sh/install | bash - name: Install docker-compose - run: apt install -y docker.io + run: apt update && apt install -y docker.io - name: Run docker-compose run: docker compose down -v && docker compose run bot-api bun test \ No newline at end of file -- 2.39.2 From 4dd4d18a92724b580eda18b5c921087395efaa71 Mon Sep 17 00:00:00 2001 From: Sugui Date: Fri, 29 Dec 2023 16:48:26 +0100 Subject: [PATCH 14/64] testing moby --- .gitea/workflows/test-compose.yaml | 4 +--- 1 file changed, 1 insertion(+), 3 deletions(-) diff --git a/.gitea/workflows/test-compose.yaml b/.gitea/workflows/test-compose.yaml index 7b2ece1..fd2ea66 100644 --- a/.gitea/workflows/test-compose.yaml +++ b/.gitea/workflows/test-compose.yaml @@ -11,7 +11,5 @@ jobs: uses: actions/checkout@v3 - name: Install Bun runtime run: curl -fsSL https://bun.sh/install | bash - - name: Install docker-compose - run: apt update && apt install -y docker.io - name: Run docker-compose - run: docker compose down -v && docker compose run bot-api bun test \ No newline at end of file + run: moby compose down -v && moby compose run bot-api bun test \ No newline at end of file -- 2.39.2 From 01318e7579a58718980b4eea3ca72cebf019acb7 Mon Sep 17 00:00:00 2001 From: Sugui Date: Fri, 29 Dec 2023 16:53:56 +0100 Subject: [PATCH 15/64] using docker image --- .gitea/workflows/test-compose.yaml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/.gitea/workflows/test-compose.yaml b/.gitea/workflows/test-compose.yaml index fd2ea66..73bb422 100644 --- a/.gitea/workflows/test-compose.yaml +++ b/.gitea/workflows/test-compose.yaml @@ -5,7 +5,7 @@ on: [pull_request] jobs: unit-test: container: - image: catthehacker/ubuntu:act-latest + image: docker:latest steps: - name: Check out repository code uses: actions/checkout@v3 -- 2.39.2 From 54a4025672234d85719498168cbfb77d91ec6fd3 Mon Sep 17 00:00:00 2001 From: Sugui Date: Fri, 29 Dec 2023 16:58:34 +0100 Subject: [PATCH 16/64] adding nodejs --- .gitea/workflows/test-compose.yaml | 2 ++ 1 file changed, 2 insertions(+) diff --git a/.gitea/workflows/test-compose.yaml b/.gitea/workflows/test-compose.yaml index 73bb422..58d6da1 100644 --- a/.gitea/workflows/test-compose.yaml +++ b/.gitea/workflows/test-compose.yaml @@ -7,6 +7,8 @@ jobs: container: image: docker:latest steps: + - name: Installing node + run: apk add nodejs - name: Check out repository code uses: actions/checkout@v3 - name: Install Bun runtime -- 2.39.2 From 36e91e89f6662da5cb6399d2634a492537bb2150 Mon Sep 17 00:00:00 2001 From: Sugui Date: Fri, 29 Dec 2023 17:00:09 +0100 Subject: [PATCH 17/64] adding git --- .gitea/workflows/test-compose.yaml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/.gitea/workflows/test-compose.yaml b/.gitea/workflows/test-compose.yaml index 58d6da1..ca6d1dd 100644 --- a/.gitea/workflows/test-compose.yaml +++ b/.gitea/workflows/test-compose.yaml @@ -8,7 +8,7 @@ jobs: image: docker:latest steps: - name: Installing node - run: apk add nodejs + run: apk add nodejs git - name: Check out repository code uses: actions/checkout@v3 - name: Install Bun runtime -- 2.39.2 From 3c723a810936acdb0a8228d6c925614335059c7b Mon Sep 17 00:00:00 2001 From: Sugui Date: Fri, 29 Dec 2023 17:02:28 +0100 Subject: [PATCH 18/64] adding curl and bash --- .gitea/workflows/test-compose.yaml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/.gitea/workflows/test-compose.yaml b/.gitea/workflows/test-compose.yaml index ca6d1dd..4ba542f 100644 --- a/.gitea/workflows/test-compose.yaml +++ b/.gitea/workflows/test-compose.yaml @@ -8,7 +8,7 @@ jobs: image: docker:latest steps: - name: Installing node - run: apk add nodejs git + run: apk add nodejs git curl bash - name: Check out repository code uses: actions/checkout@v3 - name: Install Bun runtime -- 2.39.2 From a09119ea4525b2e5326927efca17b98e2978bfd5 Mon Sep 17 00:00:00 2001 From: Sugui Date: Fri, 29 Dec 2023 17:04:19 +0100 Subject: [PATCH 19/64] changed moby to docker --- .gitea/workflows/test-compose.yaml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/.gitea/workflows/test-compose.yaml b/.gitea/workflows/test-compose.yaml index 4ba542f..753aa8e 100644 --- a/.gitea/workflows/test-compose.yaml +++ b/.gitea/workflows/test-compose.yaml @@ -14,4 +14,4 @@ jobs: - name: Install Bun runtime run: curl -fsSL https://bun.sh/install | bash - name: Run docker-compose - run: moby compose down -v && moby compose run bot-api bun test \ No newline at end of file + run: ducker compose down -v && docker compose run bot-api bun test \ No newline at end of file -- 2.39.2 From a4f4ebbbd2405c4748a5f4e9e13198452a9600c0 Mon Sep 17 00:00:00 2001 From: Sugui Date: Fri, 29 Dec 2023 17:05:17 +0100 Subject: [PATCH 20/64] typo --- .gitea/workflows/test-compose.yaml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/.gitea/workflows/test-compose.yaml b/.gitea/workflows/test-compose.yaml index 753aa8e..5040055 100644 --- a/.gitea/workflows/test-compose.yaml +++ b/.gitea/workflows/test-compose.yaml @@ -14,4 +14,4 @@ jobs: - name: Install Bun runtime run: curl -fsSL https://bun.sh/install | bash - name: Run docker-compose - run: ducker compose down -v && docker compose run bot-api bun test \ No newline at end of file + run: docker compose down -v && docker compose run bot-api bun test \ No newline at end of file -- 2.39.2 From 7230a08cc9ef407a7b38498aa59a502885bd516a Mon Sep 17 00:00:00 2001 From: Sugui Date: Fri, 29 Dec 2023 17:06:49 +0100 Subject: [PATCH 21/64] dind --- .gitea/workflows/test-compose.yaml | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/.gitea/workflows/test-compose.yaml b/.gitea/workflows/test-compose.yaml index 5040055..c094113 100644 --- a/.gitea/workflows/test-compose.yaml +++ b/.gitea/workflows/test-compose.yaml @@ -5,7 +5,7 @@ on: [pull_request] jobs: unit-test: container: - image: docker:latest + image: docker:dind-latest steps: - name: Installing node run: apk add nodejs git curl bash @@ -14,4 +14,4 @@ jobs: - name: Install Bun runtime run: curl -fsSL https://bun.sh/install | bash - name: Run docker-compose - run: docker compose down -v && docker compose run bot-api bun test \ No newline at end of file + run: dind compose down -v && dind compose run bot-api bun test \ No newline at end of file -- 2.39.2 From 42111a9e3034510ee744da966f443fcdd434dcfc Mon Sep 17 00:00:00 2001 From: Sugui Date: Fri, 29 Dec 2023 17:08:06 +0100 Subject: [PATCH 22/64] fixed dind --- .gitea/workflows/test-compose.yaml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/.gitea/workflows/test-compose.yaml b/.gitea/workflows/test-compose.yaml index c094113..06b6d5b 100644 --- a/.gitea/workflows/test-compose.yaml +++ b/.gitea/workflows/test-compose.yaml @@ -5,7 +5,7 @@ on: [pull_request] jobs: unit-test: container: - image: docker:dind-latest + image: docker:dind steps: - name: Installing node run: apk add nodejs git curl bash -- 2.39.2 From 6850989f06469c849274d47dfb3ad09ee709f6c2 Mon Sep 17 00:00:00 2001 From: Alie Date: Fri, 29 Dec 2023 17:10:48 +0100 Subject: [PATCH 23/64] unified the tests in one file for port binding reasons --- src/controllers/AuthControler.ts | 1 - src/services/AuthService.ts | 2 -- tests/app.test.ts | 24 ++++++++++++++++++++- tests/auth.test.ts | 37 -------------------------------- 4 files changed, 23 insertions(+), 41 deletions(-) delete mode 100644 tests/auth.test.ts diff --git a/src/controllers/AuthControler.ts b/src/controllers/AuthControler.ts index 68c69ac..335e11a 100644 --- a/src/controllers/AuthControler.ts +++ b/src/controllers/AuthControler.ts @@ -7,7 +7,6 @@ const authTokenSecret = process.env.JWTSECRET || "badsecret"; class AuthControler { async login(req: Request, res: Response) { // Read app and secret from request body - console.log(req.body) const { app, secret } = req.body; // Filter app from the apps by app and secret diff --git a/src/services/AuthService.ts b/src/services/AuthService.ts index 9dcdabe..ac32127 100644 --- a/src/services/AuthService.ts +++ b/src/services/AuthService.ts @@ -2,9 +2,7 @@ import AuthModel, { Auth } from "../models/AuthModel"; class AuthService { async find(app: String, secret: String): Promise { - console.log(app, secret) const auth = await AuthModel.findOne({ app: app, secret: secret }); - console.log(auth) return auth; } } diff --git a/tests/app.test.ts b/tests/app.test.ts index 72a6ebc..3a116c8 100644 --- a/tests/app.test.ts +++ b/tests/app.test.ts @@ -1,5 +1,5 @@ import { afterAll, afterEach, beforeAll, describe, expect, it, mock } from "bun:test"; -import request from "supertest"; +import request, { Response } from "supertest"; import app, { startApp } from "../src/app"; import imageService from "../src/services/ImageService"; import memoryServer from "./memoryServer"; @@ -33,6 +33,28 @@ afterEach(() => { })); }); +describe("/login works as instended", async () => { + let correctRespose: Response; + beforeAll(async () => { + correctRespose = await request(app) + .post("/login") + .send({ app: "tester", secret: "test" }); + }); + + it("should return 200 for correct login", async () => { + expect(correctRespose.status).toBe(200); + }); + + it("should contain a token", () => { + expect(correctRespose.body).toHaveProperty("token"); + }); + + it("should return 403 for invalid credentials", async () => { + const res = await request(app).post("/login").send({}); + expect(res.status).toBe(403); + }); + }); + describe("GET / shows all of the endpoints", async () => { const res = await request(app).get("/"); diff --git a/tests/auth.test.ts b/tests/auth.test.ts deleted file mode 100644 index affe74f..0000000 --- a/tests/auth.test.ts +++ /dev/null @@ -1,37 +0,0 @@ -import { afterAll, beforeAll, describe, expect, it } from "bun:test"; -import request, { Response } from "supertest"; -import app, { startApp } from "../src/app"; -import memoryServer from "./memoryServer"; -import populateDatabase from "./populateDatabase"; - -beforeAll(async () => { - if (!process.env.DEDICATED_MONGODB_SERVER) await memoryServer.start(); - await startApp(); - await populateDatabase(); -}); - -afterAll(async () => { - if (!process.env.DEDICATED_MONGODB_SERVER) await memoryServer.stop(); -}); - -describe("/login", async () => { - let correctRespose: Response; - beforeAll(async () => { - correctRespose = await request(app) - .post("/login") - .send({ app: "tester", secret: "test" }); - }); - - it("should return 200 for correct login", async () => { - expect(correctRespose.status).toBe(200); - }); - - it("should contain a token", () => { - expect(correctRespose.body).toHaveProperty("token"); - }); - - it("should return 403 for invalid credentials", async () => { - const res = await request(app).post("/login").send({}); - expect(res.status).toBe(403); - }); -}); -- 2.39.2 From aca87e205b547364c27634a33f1124e81fad97c5 Mon Sep 17 00:00:00 2001 From: Sugui Date: Fri, 29 Dec 2023 17:12:04 +0100 Subject: [PATCH 24/64] privileged --- .gitea/workflows/test-compose.yaml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/.gitea/workflows/test-compose.yaml b/.gitea/workflows/test-compose.yaml index 06b6d5b..c65a8f6 100644 --- a/.gitea/workflows/test-compose.yaml +++ b/.gitea/workflows/test-compose.yaml @@ -14,4 +14,4 @@ jobs: - name: Install Bun runtime run: curl -fsSL https://bun.sh/install | bash - name: Run docker-compose - run: dind compose down -v && dind compose run bot-api bun test \ No newline at end of file + run: dind --privileged compose down -v && dind --privileged compose run bot-api bun test \ No newline at end of file -- 2.39.2 From c27ce7273a82c424876be37e6c54021a7de646ee Mon Sep 17 00:00:00 2001 From: Alie Date: Fri, 29 Dec 2023 17:20:35 +0100 Subject: [PATCH 25/64] enable debug --- package.json | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/package.json b/package.json index 6d9c33c..7d6fe8f 100644 --- a/package.json +++ b/package.json @@ -30,5 +30,10 @@ "express-list-endpoints": "^6.0.0", "jsonwebtoken": "^9.0.2", "mongoose": "^8.0.3" + }, + "config": { + "mongodbMemoryServer": { + "debug": "1" + } } } -- 2.39.2 From 5b2196fbd907e5a8ddf79b7f47fe1158c8ec47cc Mon Sep 17 00:00:00 2001 From: Sugui Date: Fri, 29 Dec 2023 17:21:44 +0100 Subject: [PATCH 26/64] being root --- .gitea/workflows/test-compose.yaml | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/.gitea/workflows/test-compose.yaml b/.gitea/workflows/test-compose.yaml index c65a8f6..0e4b740 100644 --- a/.gitea/workflows/test-compose.yaml +++ b/.gitea/workflows/test-compose.yaml @@ -13,5 +13,7 @@ jobs: uses: actions/checkout@v3 - name: Install Bun runtime run: curl -fsSL https://bun.sh/install | bash + - name: Be root + run: su - name: Run docker-compose - run: dind --privileged compose down -v && dind --privileged compose run bot-api bun test \ No newline at end of file + run: dind compose down -v && dind compose run bot-api bun test \ No newline at end of file -- 2.39.2 From a71640dd9f3ab0e20e86b67c0fac303188df8b0a Mon Sep 17 00:00:00 2001 From: Sugui Date: Fri, 29 Dec 2023 17:23:02 +0100 Subject: [PATCH 27/64] rootless --- .gitea/workflows/test-compose.yaml | 4 +--- 1 file changed, 1 insertion(+), 3 deletions(-) diff --git a/.gitea/workflows/test-compose.yaml b/.gitea/workflows/test-compose.yaml index 0e4b740..f83f482 100644 --- a/.gitea/workflows/test-compose.yaml +++ b/.gitea/workflows/test-compose.yaml @@ -5,7 +5,7 @@ on: [pull_request] jobs: unit-test: container: - image: docker:dind + image: docker:dind-rootless steps: - name: Installing node run: apk add nodejs git curl bash @@ -13,7 +13,5 @@ jobs: uses: actions/checkout@v3 - name: Install Bun runtime run: curl -fsSL https://bun.sh/install | bash - - name: Be root - run: su - name: Run docker-compose run: dind compose down -v && dind compose run bot-api bun test \ No newline at end of file -- 2.39.2 From 04bf90cee6f37879c66a798dd6c9544ad3c336df Mon Sep 17 00:00:00 2001 From: Sugui Date: Fri, 29 Dec 2023 17:29:01 +0100 Subject: [PATCH 28/64] echo --- .gitea/workflows/test-compose.yaml | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/.gitea/workflows/test-compose.yaml b/.gitea/workflows/test-compose.yaml index f83f482..fb70337 100644 --- a/.gitea/workflows/test-compose.yaml +++ b/.gitea/workflows/test-compose.yaml @@ -5,8 +5,10 @@ on: [pull_request] jobs: unit-test: container: - image: docker:dind-rootless + image: docker:dind steps: + - name: echo + run: echo $DOCKER_HOST - name: Installing node run: apk add nodejs git curl bash - name: Check out repository code -- 2.39.2 From c801bc448d4e18dcd0812fc7d0feefcca5460b87 Mon Sep 17 00:00:00 2001 From: Sugui Date: Fri, 29 Dec 2023 17:31:49 +0100 Subject: [PATCH 29/64] Docker instead of dind --- .gitea/workflows/test-compose.yaml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/.gitea/workflows/test-compose.yaml b/.gitea/workflows/test-compose.yaml index fb70337..9c7a939 100644 --- a/.gitea/workflows/test-compose.yaml +++ b/.gitea/workflows/test-compose.yaml @@ -16,4 +16,4 @@ jobs: - name: Install Bun runtime run: curl -fsSL https://bun.sh/install | bash - name: Run docker-compose - run: dind compose down -v && dind compose run bot-api bun test \ No newline at end of file + run: docker compose down -v && docker compose run bot-api bun test \ No newline at end of file -- 2.39.2 From 471c84b18c93f016f6d5870c930aa34f49eeeb9b Mon Sep 17 00:00:00 2001 From: Sugui Date: Fri, 29 Dec 2023 17:53:29 +0100 Subject: [PATCH 30/64] Deleted not docker workflow --- .gitea/workflows/test-compose.yaml | 19 ------------------- .gitea/workflows/test.yaml | 16 +++++++++------- 2 files changed, 9 insertions(+), 26 deletions(-) delete mode 100644 .gitea/workflows/test-compose.yaml diff --git a/.gitea/workflows/test-compose.yaml b/.gitea/workflows/test-compose.yaml deleted file mode 100644 index 9c7a939..0000000 --- a/.gitea/workflows/test-compose.yaml +++ /dev/null @@ -1,19 +0,0 @@ -name: Unit Tests with docker compose -run-name: ${{ gitea.actor }} is testing out Gitea Actions 🚀 -on: [pull_request] - -jobs: - unit-test: - container: - image: docker:dind - steps: - - name: echo - run: echo $DOCKER_HOST - - name: Installing node - run: apk add nodejs git curl bash - - name: Check out repository code - uses: actions/checkout@v3 - - name: Install Bun runtime - run: curl -fsSL https://bun.sh/install | bash - - 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/.gitea/workflows/test.yaml b/.gitea/workflows/test.yaml index c1746f2..9c7a939 100644 --- a/.gitea/workflows/test.yaml +++ b/.gitea/workflows/test.yaml @@ -1,17 +1,19 @@ -name: Unit Tests +name: Unit Tests with docker compose run-name: ${{ gitea.actor }} is testing out Gitea Actions 🚀 on: [pull_request] jobs: unit-test: container: - image: catthehacker/ubuntu:act-latest + image: docker:dind steps: + - name: echo + run: echo $DOCKER_HOST + - name: Installing node + run: apk add nodejs git curl bash - name: Check out repository code uses: actions/checkout@v3 - name: Install Bun runtime - run: curl -fsSL https://bun.sh/install | bash - - name: Install dependencies - run: ~/.bun/bin/bun install - - name: Run the test - run: ~/.bun/bin/bun test \ No newline at end of file + run: curl -fsSL https://bun.sh/install | bash + - name: Run docker-compose + run: docker compose down -v && docker compose run bot-api bun test \ No newline at end of file -- 2.39.2 From 0b3081907625da038eff2bc8b71e907b022bca5a Mon Sep 17 00:00:00 2001 From: Sugui Date: Fri, 29 Dec 2023 17:55:36 +0100 Subject: [PATCH 31/64] dind --- .gitea/workflows/test.yaml | 6 ++---- 1 file changed, 2 insertions(+), 4 deletions(-) diff --git a/.gitea/workflows/test.yaml b/.gitea/workflows/test.yaml index 9c7a939..c0253ab 100644 --- a/.gitea/workflows/test.yaml +++ b/.gitea/workflows/test.yaml @@ -7,13 +7,11 @@ jobs: container: image: docker:dind steps: - - name: echo - run: echo $DOCKER_HOST - - name: Installing node + - name: Installing necessary packages run: apk add nodejs git curl bash - name: Check out repository code uses: actions/checkout@v3 - name: Install Bun runtime run: curl -fsSL https://bun.sh/install | bash - name: Run docker-compose - run: docker compose down -v && docker compose run bot-api bun test \ No newline at end of file + run: dind compose down -v && dind compose run bot-api bun test \ No newline at end of file -- 2.39.2 From 6efb03efbfb873c9e4ab02360722f7d9ad5f616b Mon Sep 17 00:00:00 2001 From: Sugui Date: Fri, 29 Dec 2023 17:59:11 +0100 Subject: [PATCH 32/64] dind-compose --- .gitea/workflows/test.yaml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/.gitea/workflows/test.yaml b/.gitea/workflows/test.yaml index c0253ab..c68111b 100644 --- a/.gitea/workflows/test.yaml +++ b/.gitea/workflows/test.yaml @@ -5,7 +5,7 @@ on: [pull_request] jobs: unit-test: container: - image: docker:dind + image: guglio/dind-compose:latest steps: - name: Installing necessary packages run: apk add nodejs git curl bash -- 2.39.2 From 3526729166bcef960c2820ef5a0d4b6d0e9356b6 Mon Sep 17 00:00:00 2001 From: Sugui Date: Fri, 29 Dec 2023 18:07:55 +0100 Subject: [PATCH 33/64] probamos --- .gitea/workflows/test.yaml | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/.gitea/workflows/test.yaml b/.gitea/workflows/test.yaml index c68111b..c910075 100644 --- a/.gitea/workflows/test.yaml +++ b/.gitea/workflows/test.yaml @@ -5,7 +5,7 @@ on: [pull_request] jobs: unit-test: container: - image: guglio/dind-compose:latest + image: docker:latest steps: - name: Installing necessary packages run: apk add nodejs git curl bash @@ -14,4 +14,4 @@ jobs: - name: Install Bun runtime run: curl -fsSL https://bun.sh/install | bash - name: Run docker-compose - run: dind compose down -v && dind compose run bot-api bun test \ No newline at end of file + run: docker compose down -v && docker compose run bot-api bun test \ No newline at end of file -- 2.39.2 From 511eaba5385248e347b2e3fa04c5231ea0f559b3 Mon Sep 17 00:00:00 2001 From: Sugui Date: Fri, 29 Dec 2023 18:13:25 +0100 Subject: [PATCH 34/64] docker --- .gitea/workflows/test.yaml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/.gitea/workflows/test.yaml b/.gitea/workflows/test.yaml index c910075..258f841 100644 --- a/.gitea/workflows/test.yaml +++ b/.gitea/workflows/test.yaml @@ -5,7 +5,7 @@ on: [pull_request] jobs: unit-test: container: - image: docker:latest + image: docker:dind steps: - name: Installing necessary packages run: apk add nodejs git curl bash -- 2.39.2 From e5122f59e4801329aada07ea4639536263a2575d Mon Sep 17 00:00:00 2001 From: Sugui Date: Fri, 29 Dec 2023 18:15:02 +0100 Subject: [PATCH 35/64] dind dind --- .gitea/workflows/test.yaml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/.gitea/workflows/test.yaml b/.gitea/workflows/test.yaml index 258f841..fa42027 100644 --- a/.gitea/workflows/test.yaml +++ b/.gitea/workflows/test.yaml @@ -14,4 +14,4 @@ jobs: - name: Install Bun runtime run: curl -fsSL https://bun.sh/install | bash - name: Run docker-compose - run: docker compose down -v && docker compose run bot-api bun test \ No newline at end of file + run: dind docker compose down -v && dind docker compose run bot-api bun test \ No newline at end of file -- 2.39.2 From 1c204590df8596cb7254ba3d8fecf6219dab1338 Mon Sep 17 00:00:00 2001 From: Sugui Date: Fri, 29 Dec 2023 18:19:00 +0100 Subject: [PATCH 36/64] dind --- .gitea/workflows/test.yaml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/.gitea/workflows/test.yaml b/.gitea/workflows/test.yaml index fa42027..c0253ab 100644 --- a/.gitea/workflows/test.yaml +++ b/.gitea/workflows/test.yaml @@ -14,4 +14,4 @@ jobs: - name: Install Bun runtime run: curl -fsSL https://bun.sh/install | bash - name: Run docker-compose - run: dind docker compose down -v && dind docker compose run bot-api bun test \ No newline at end of file + run: dind compose down -v && dind compose run bot-api bun test \ No newline at end of file -- 2.39.2 From 108e1793bf9a0e4909c870bd0ce0e9398007425a Mon Sep 17 00:00:00 2001 From: Sugui Date: Fri, 29 Dec 2023 18:26:02 +0100 Subject: [PATCH 37/64] ps --- .gitea/workflows/test.yaml | 2 ++ 1 file changed, 2 insertions(+) diff --git a/.gitea/workflows/test.yaml b/.gitea/workflows/test.yaml index c0253ab..7ba87dc 100644 --- a/.gitea/workflows/test.yaml +++ b/.gitea/workflows/test.yaml @@ -7,6 +7,8 @@ jobs: container: image: docker:dind steps: + - name: ps + run: ps - name: Installing necessary packages run: apk add nodejs git curl bash - name: Check out repository code -- 2.39.2 From b76865dadf44a43c4ace809b3216b7f4ff277421 Mon Sep 17 00:00:00 2001 From: Sugui Date: Fri, 29 Dec 2023 18:33:20 +0100 Subject: [PATCH 38/64] docker init --- .gitea/workflows/test.yaml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/.gitea/workflows/test.yaml b/.gitea/workflows/test.yaml index 7ba87dc..fe2615c 100644 --- a/.gitea/workflows/test.yaml +++ b/.gitea/workflows/test.yaml @@ -8,7 +8,7 @@ jobs: image: docker:dind steps: - name: ps - run: ps + run: docker-init -- dockerd --host=unix:///var/run/docker.sock - name: Installing necessary packages run: apk add nodejs git curl bash - name: Check out repository code -- 2.39.2 From d62ed9ca18b4347b2be208740df867a3045d6922 Mon Sep 17 00:00:00 2001 From: Sugui Date: Fri, 29 Dec 2023 18:34:15 +0100 Subject: [PATCH 39/64] & --- .gitea/workflows/test.yaml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/.gitea/workflows/test.yaml b/.gitea/workflows/test.yaml index fe2615c..eeefc17 100644 --- a/.gitea/workflows/test.yaml +++ b/.gitea/workflows/test.yaml @@ -8,7 +8,7 @@ jobs: image: docker:dind steps: - name: ps - run: docker-init -- dockerd --host=unix:///var/run/docker.sock + run: docker-init -- dockerd --host=unix:///var/run/docker.sock & - name: Installing necessary packages run: apk add nodejs git curl bash - name: Check out repository code -- 2.39.2 From 18f456c83e904c42f7bab4209dcb7c9a9739fc0c Mon Sep 17 00:00:00 2001 From: Sugui Date: Fri, 29 Dec 2023 18:35:08 +0100 Subject: [PATCH 40/64] docker --- .gitea/workflows/test.yaml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/.gitea/workflows/test.yaml b/.gitea/workflows/test.yaml index eeefc17..6003096 100644 --- a/.gitea/workflows/test.yaml +++ b/.gitea/workflows/test.yaml @@ -16,4 +16,4 @@ jobs: - name: Install Bun runtime run: curl -fsSL https://bun.sh/install | bash - name: Run docker-compose - run: dind compose down -v && dind compose run bot-api bun test \ No newline at end of file + run: docker compose down -v && docker compose run bot-api bun test \ No newline at end of file -- 2.39.2 From 006ea44a6a4386b841ff75d652d0b3d3e63de292 Mon Sep 17 00:00:00 2001 From: Sugui Date: Fri, 29 Dec 2023 18:38:25 +0100 Subject: [PATCH 41/64] buni --- .gitea/workflows/test.yaml | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/.gitea/workflows/test.yaml b/.gitea/workflows/test.yaml index 6003096..d4fbf7b 100644 --- a/.gitea/workflows/test.yaml +++ b/.gitea/workflows/test.yaml @@ -13,7 +13,9 @@ jobs: run: apk add nodejs git curl bash - name: Check out repository code uses: actions/checkout@v3 - - name: Install Bun runtime + - name: Install bun runtime run: curl -fsSL https://bun.sh/install | bash + - name: Install project dependencies + run: bun install - name: Run docker-compose run: docker compose down -v && docker compose run bot-api bun test \ No newline at end of file -- 2.39.2 From 15f3af264b3a20d032b60f47d296660ad80647b6 Mon Sep 17 00:00:00 2001 From: Sugui Date: Fri, 29 Dec 2023 18:39:43 +0100 Subject: [PATCH 42/64] bun bin bun --- .gitea/workflows/test.yaml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/.gitea/workflows/test.yaml b/.gitea/workflows/test.yaml index d4fbf7b..3451eb2 100644 --- a/.gitea/workflows/test.yaml +++ b/.gitea/workflows/test.yaml @@ -16,6 +16,6 @@ jobs: - name: Install bun runtime run: curl -fsSL https://bun.sh/install | bash - name: Install project dependencies - run: bun install + run: ~/.bun/bin/bun install - name: Run docker-compose run: docker compose down -v && docker compose run bot-api bun test \ No newline at end of file -- 2.39.2 From ac98daa71e9990aafac8317d6313546e35cfaa86 Mon Sep 17 00:00:00 2001 From: Sugui Date: Fri, 29 Dec 2023 18:48:40 +0100 Subject: [PATCH 43/64] bash --- .gitea/workflows/test.yaml | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/.gitea/workflows/test.yaml b/.gitea/workflows/test.yaml index 3451eb2..390770f 100644 --- a/.gitea/workflows/test.yaml +++ b/.gitea/workflows/test.yaml @@ -7,7 +7,7 @@ jobs: container: image: docker:dind steps: - - name: ps + - name: Starting docker daemon run: docker-init -- dockerd --host=unix:///var/run/docker.sock & - name: Installing necessary packages run: apk add nodejs git curl bash @@ -15,7 +15,9 @@ jobs: uses: actions/checkout@v3 - name: Install bun runtime run: curl -fsSL https://bun.sh/install | bash + - name: Open a new shell + run: bash - name: Install project dependencies - run: ~/.bun/bin/bun install + run: bun install - name: Run docker-compose run: docker compose down -v && docker compose run bot-api bun test \ No newline at end of file -- 2.39.2 From 0dba3d2f6fae234d627392d535ab3a3375bcc742 Mon Sep 17 00:00:00 2001 From: Sugui Date: Fri, 29 Dec 2023 19:02:42 +0100 Subject: [PATCH 44/64] over bun --- .gitea/workflows/test.yaml | 6 +----- 1 file changed, 1 insertion(+), 5 deletions(-) diff --git a/.gitea/workflows/test.yaml b/.gitea/workflows/test.yaml index 390770f..4bb4530 100644 --- a/.gitea/workflows/test.yaml +++ b/.gitea/workflows/test.yaml @@ -13,11 +13,7 @@ jobs: run: apk add nodejs git curl bash - name: Check out repository code uses: actions/checkout@v3 - - name: Install bun runtime - run: curl -fsSL https://bun.sh/install | bash - - name: Open a new shell - run: bash - name: Install project dependencies - run: bun install + run: docker run over/bun:1 bun install - name: Run docker-compose run: docker compose down -v && docker compose run bot-api bun test \ No newline at end of file -- 2.39.2 From 1bcf3d4590c8a2397be907536486227cbe3e4224 Mon Sep 17 00:00:00 2001 From: Sugui Date: Fri, 29 Dec 2023 19:03:48 +0100 Subject: [PATCH 45/64] oven --- .gitea/workflows/test.yaml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/.gitea/workflows/test.yaml b/.gitea/workflows/test.yaml index 4bb4530..3efdecb 100644 --- a/.gitea/workflows/test.yaml +++ b/.gitea/workflows/test.yaml @@ -14,6 +14,6 @@ jobs: - name: Check out repository code uses: actions/checkout@v3 - name: Install project dependencies - run: docker run over/bun:1 bun install + run: docker run oven/bun:1 bun install - name: Run docker-compose run: docker compose down -v && docker compose run bot-api bun test \ No newline at end of file -- 2.39.2 From 6e546cc753e984a1d952c681d9fc5da16a02fbc3 Mon Sep 17 00:00:00 2001 From: Sugui Date: Fri, 29 Dec 2023 19:07:26 +0100 Subject: [PATCH 46/64] yay --- .gitea/workflows/test.yaml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/.gitea/workflows/test.yaml b/.gitea/workflows/test.yaml index 3efdecb..4c41e18 100644 --- a/.gitea/workflows/test.yaml +++ b/.gitea/workflows/test.yaml @@ -14,6 +14,6 @@ jobs: - name: Check out repository code uses: actions/checkout@v3 - name: Install project dependencies - run: docker run oven/bun:1 bun install + run: docker run -w /usr/src/app -v ./:/usr/src/app oven/bun i - name: Run docker-compose run: docker compose down -v && docker compose run bot-api bun test \ No newline at end of file -- 2.39.2 From a59f288d22455251b525afb5894ba1e2094c48df Mon Sep 17 00:00:00 2001 From: Sugui Date: Fri, 29 Dec 2023 19:14:57 +0100 Subject: [PATCH 47/64] verbose --- .gitea/workflows/test.yaml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/.gitea/workflows/test.yaml b/.gitea/workflows/test.yaml index 4c41e18..428e767 100644 --- a/.gitea/workflows/test.yaml +++ b/.gitea/workflows/test.yaml @@ -14,6 +14,6 @@ jobs: - name: Check out repository code uses: actions/checkout@v3 - name: Install project dependencies - run: docker run -w /usr/src/app -v ./:/usr/src/app oven/bun i + run: docker run -w /usr/src/app -v ./:/usr/src/app oven/bun install --verbose - name: Run docker-compose run: docker compose down -v && docker compose run bot-api bun test \ No newline at end of file -- 2.39.2 From 5d2c9cd325d85752a3d977a189d57b4b37c59873 Mon Sep 17 00:00:00 2001 From: Sugui Date: Fri, 29 Dec 2023 19:18:46 +0100 Subject: [PATCH 48/64] fixinggg --- .gitea/workflows/test.yaml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/.gitea/workflows/test.yaml b/.gitea/workflows/test.yaml index 428e767..ac511a2 100644 --- a/.gitea/workflows/test.yaml +++ b/.gitea/workflows/test.yaml @@ -14,6 +14,6 @@ jobs: - name: Check out repository code uses: actions/checkout@v3 - name: Install project dependencies - run: docker run -w /usr/src/app -v ./:/usr/src/app oven/bun install --verbose + run: docker run -w /usr/src/app -v ./:/usr/src/app oven/bun:1 bun i --verbose - name: Run docker-compose run: docker compose down -v && docker compose run bot-api bun test \ No newline at end of file -- 2.39.2 From 2081c2fea8230b24abe6193a748fa8b0fd5682de Mon Sep 17 00:00:00 2001 From: Sugui Date: Fri, 29 Dec 2023 19:24:43 +0100 Subject: [PATCH 49/64] testing with npm, also moved types to dev --- .gitea/workflows/test.yaml | 4 ++-- package.json | 8 ++++---- 2 files changed, 6 insertions(+), 6 deletions(-) diff --git a/.gitea/workflows/test.yaml b/.gitea/workflows/test.yaml index ac511a2..bdaab3f 100644 --- a/.gitea/workflows/test.yaml +++ b/.gitea/workflows/test.yaml @@ -10,10 +10,10 @@ jobs: - name: Starting docker daemon run: docker-init -- dockerd --host=unix:///var/run/docker.sock & - name: Installing necessary packages - run: apk add nodejs git curl bash + run: apk add nodejs git curl bash nodejs - name: Check out repository code uses: actions/checkout@v3 - name: Install project dependencies - run: docker run -w /usr/src/app -v ./:/usr/src/app oven/bun:1 bun i --verbose + run: npm install #docker run -w /usr/src/app -v ./:/usr/src/app oven/bun:1 bun i --verbose - 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/package.json b/package.json index 7d6fe8f..20be423 100644 --- a/package.json +++ b/package.json @@ -6,6 +6,10 @@ "@types/jest": "^29.5.11", "@types/mongodb-memory-server": "^2.3.0", "@types/supertest": "^6.0.1", + "@types/express": "^4.17.21", + "@types/express-list-endpoints": "^6.0.3", + "@types/jsonwebtoken": "^9.0.5", + "@types/mongoose": "^5.11.97", "bun-types": "latest", "jest": "^29.7.0", "mongodb-memory-server": "^9.1.3", @@ -22,10 +26,6 @@ "test": "docker compose down -v && docker compose run bot-api bun test" }, "dependencies": { - "@types/express": "^4.17.21", - "@types/express-list-endpoints": "^6.0.3", - "@types/jsonwebtoken": "^9.0.5", - "@types/mongoose": "^5.11.97", "express": "^4.18.2", "express-list-endpoints": "^6.0.0", "jsonwebtoken": "^9.0.2", -- 2.39.2 From 8b27e2577f14d3e7f471a364b22cd299fafcfa17 Mon Sep 17 00:00:00 2001 From: Sugui Date: Fri, 29 Dec 2023 19:25:48 +0100 Subject: [PATCH 50/64] npm --- .gitea/workflows/test.yaml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/.gitea/workflows/test.yaml b/.gitea/workflows/test.yaml index bdaab3f..8f326b5 100644 --- a/.gitea/workflows/test.yaml +++ b/.gitea/workflows/test.yaml @@ -10,7 +10,7 @@ jobs: - name: Starting docker daemon run: docker-init -- dockerd --host=unix:///var/run/docker.sock & - name: Installing necessary packages - run: apk add nodejs git curl bash nodejs + run: apk add nodejs git curl bash nodejs-npm - name: Check out repository code uses: actions/checkout@v3 - name: Install project dependencies -- 2.39.2 From 49b159f9dc3748d0ef492c29d7624c839a156e57 Mon Sep 17 00:00:00 2001 From: Sugui Date: Fri, 29 Dec 2023 19:28:38 +0100 Subject: [PATCH 51/64] node 20 --- .gitea/workflows/test.yaml | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/.gitea/workflows/test.yaml b/.gitea/workflows/test.yaml index 8f326b5..14460b3 100644 --- a/.gitea/workflows/test.yaml +++ b/.gitea/workflows/test.yaml @@ -10,10 +10,10 @@ jobs: - name: Starting docker daemon run: docker-init -- dockerd --host=unix:///var/run/docker.sock & - name: Installing necessary packages - run: apk add nodejs git curl bash nodejs-npm + run: apk add nodejs git curl bash - name: Check out repository code uses: actions/checkout@v3 - name: Install project dependencies - run: npm install #docker run -w /usr/src/app -v ./:/usr/src/app oven/bun:1 bun i --verbose + run: docker run -w /usr/src/app -v ./:/usr/src/app node:20 npm install - name: Run docker-compose run: docker compose down -v && docker compose run bot-api bun test \ No newline at end of file -- 2.39.2 From 9cd61c101c305d4c7747e4c301c6a392fde2cb5b Mon Sep 17 00:00:00 2001 From: Alie Date: Fri, 29 Dec 2023 19:37:35 +0100 Subject: [PATCH 52/64] fmt, purged useless deps and removed mongoMS --- package.json | 7 - src/app.ts | 40 ++--- src/controllers/ImageController.ts | 8 +- src/index.ts | 10 +- src/models/AuthModel.ts | 23 +-- src/models/ImageModel.ts | 37 ++-- tests/app.test.ts | 272 +++++++++++++++-------------- tests/memoryServer.ts | 2 +- tests/populateDatabase.ts | 18 +- 9 files changed, 208 insertions(+), 209 deletions(-) diff --git a/package.json b/package.json index 20be423..0432e6b 100644 --- a/package.json +++ b/package.json @@ -4,12 +4,10 @@ "type": "module", "devDependencies": { "@types/jest": "^29.5.11", - "@types/mongodb-memory-server": "^2.3.0", "@types/supertest": "^6.0.1", "@types/express": "^4.17.21", "@types/express-list-endpoints": "^6.0.3", "@types/jsonwebtoken": "^9.0.5", - "@types/mongoose": "^5.11.97", "bun-types": "latest", "jest": "^29.7.0", "mongodb-memory-server": "^9.1.3", @@ -30,10 +28,5 @@ "express-list-endpoints": "^6.0.0", "jsonwebtoken": "^9.0.2", "mongoose": "^8.0.3" - }, - "config": { - "mongodbMemoryServer": { - "debug": "1" - } } } diff --git a/src/app.ts b/src/app.ts index e132aa4..43a3f6f 100644 --- a/src/app.ts +++ b/src/app.ts @@ -18,24 +18,24 @@ app.post("/images", authControler.authorize, imageController.addImage); app.post("/login", authControler.login); export const startApp = async () => { - const port = process.env.PORT || 8080; - const mongo_uri: string = process.env.MONGODB_URI || ""; - const mongo_user = process.env.MONGODB_USER; - const mongo_pass = process.env.MONGODB_PASS; - - try { - await mongoose.connect(mongo_uri, { - authSource: "admin", - user: mongo_user, - pass: mongo_pass, - }); - app.listen(port, () => - console.log(`Express server listening on port ${port}`) - ); - } catch (error) { - console.error(error); - process.exit(1); - } - }; + const port = process.env.PORT || 8080; + const mongo_uri: string = process.env.MONGODB_URI || ""; + const mongo_user = process.env.MONGODB_USER; + const mongo_pass = process.env.MONGODB_PASS; -export default app; \ No newline at end of file + try { + await mongoose.connect(mongo_uri, { + authSource: "admin", + user: mongo_user, + pass: mongo_pass, + }); + app.listen(port, () => + console.log(`Express server listening on port ${port}`) + ); + } catch (error) { + console.error(error); + process.exit(1); + } +}; + +export default app; diff --git a/src/controllers/ImageController.ts b/src/controllers/ImageController.ts index 1b86aea..e60bfea 100644 --- a/src/controllers/ImageController.ts +++ b/src/controllers/ImageController.ts @@ -23,11 +23,9 @@ class ImageController { } catch (error: any) { 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`, - }); + res.status(409).json({ + error: `the image with URL ${error.keyValue.url} already exists`, + }); } else if (error instanceof mongoose.Error.ValidationError) { // Should return 400 Bad request for invalid requests res.status(400).json({ error: error.message }); diff --git a/src/index.ts b/src/index.ts index 7155635..4f8ee9f 100644 --- a/src/index.ts +++ b/src/index.ts @@ -3,10 +3,6 @@ import { startApp } from "./app"; await startApp(); - -try{ - await populateDatabase(); -} -catch { - -} \ No newline at end of file +try { + await populateDatabase(); +} catch {} diff --git a/src/models/AuthModel.ts b/src/models/AuthModel.ts index 96ce5e5..8c4a94f 100644 --- a/src/models/AuthModel.ts +++ b/src/models/AuthModel.ts @@ -5,16 +5,19 @@ export interface Auth extends Document { secret: String; } -const AuthSchema = new mongoose.Schema({ - app: { - type: String, - required: true, - index: true, +const AuthSchema = new mongoose.Schema( + { + app: { + type: String, + required: true, + index: true, + }, + secret: { + type: String, + required: true, + }, }, - secret: { - type: String, - required: true, - }, -}, { collection: "authorizations" }); + { collection: "authorizations" } +); export default mongoose.model("authorizations", AuthSchema); diff --git a/src/models/ImageModel.ts b/src/models/ImageModel.ts index b3bff2c..f2baa49 100644 --- a/src/models/ImageModel.ts +++ b/src/models/ImageModel.ts @@ -6,24 +6,27 @@ export interface Image extends Document { tags?: String[]; } -const ImageSchema = new mongoose.Schema({ - url: { - type: String, - required: true, - index: true, - unique: true - }, - status: { - type: String, - enum: { - values: ["consumed", "unavailable", "available"], +const ImageSchema = new mongoose.Schema( + { + url: { + type: String, + required: true, + index: true, + unique: true, + }, + status: { + type: String, + enum: { + values: ["consumed", "unavailable", "available"], + }, + required: true, + index: true, + }, + tags: { + type: [String], }, - required: true, - index: true, }, - tags: { - type: [String], - }, -}, { collection: "images" }); + { collection: "images" } +); export default mongoose.model("images", ImageSchema); diff --git a/tests/app.test.ts b/tests/app.test.ts index 3a116c8..0e8fbda 100644 --- a/tests/app.test.ts +++ b/tests/app.test.ts @@ -1,4 +1,12 @@ -import { afterAll, afterEach, beforeAll, describe, expect, it, mock } from "bun:test"; +import { + afterAll, + afterEach, + beforeAll, + describe, + expect, + it, + mock, +} from "bun:test"; import request, { Response } from "supertest"; import app, { startApp } from "../src/app"; import imageService from "../src/services/ImageService"; @@ -10,164 +18,162 @@ const imageServiceOriginal = imageService; let token: string; beforeAll(async () => { - if (!process.env.DEDICATED_MONGODB_SERVER) - await memoryServer.start(); - await startApp(); - await populateDatabase(); + //if (!process.env.DEDICATED_MONGODB_SERVER) await memoryServer.start(); + await startApp(); + await populateDatabase(); - const tok = await request(app) - .post("/login") - .send({ app: "tester", secret: "test" }); - token = tok.body.token; + const tok = await request(app) + .post("/login") + .send({ app: "tester", secret: "test" }); + token = tok.body.token; }); -afterAll(async () => { - if (!process.env.DEDICATED_MONGODB_SERVER) - await memoryServer.stop(); -}) - +/* afterAll(async () => { + if (!process.env.DEDICATED_MONGODB_SERVER) await memoryServer.stop(); +}); + */ afterEach(() => { - mock.restore(); - mock.module("../src/services/ImageService", () => ({ - default: imageServiceOriginal, - })); + mock.restore(); + mock.module("../src/services/ImageService", () => ({ + default: imageServiceOriginal, + })); }); describe("/login works as instended", async () => { - let correctRespose: Response; - beforeAll(async () => { - correctRespose = await request(app) - .post("/login") - .send({ app: "tester", secret: "test" }); - }); - - it("should return 200 for correct login", async () => { - expect(correctRespose.status).toBe(200); - }); - - it("should contain a token", () => { - expect(correctRespose.body).toHaveProperty("token"); - }); - - it("should return 403 for invalid credentials", async () => { - const res = await request(app).post("/login").send({}); - expect(res.status).toBe(403); - }); - }); + let correctRespose: Response; + beforeAll(async () => { + correctRespose = await request(app) + .post("/login") + .send({ app: "tester", secret: "test" }); + }); + + it("should return 200 for correct login", async () => { + expect(correctRespose.status).toBe(200); + }); + + it("should contain a token", () => { + expect(correctRespose.body).toHaveProperty("token"); + }); + + it("should return 403 for invalid credentials", async () => { + const res = await request(app).post("/login").send({}); + expect(res.status).toBe(403); + }); +}); describe("GET / shows all of the endpoints", async () => { - const res = await request(app).get("/"); + const res = await request(app).get("/"); - it("should be", async () => { - expect(res.body).toHaveProperty("endpoints"); - }); + it("should be", async () => { + expect(res.body).toHaveProperty("endpoints"); + }); - it("should be an array", () => { - expect(Array.isArray(res.body.endpoints)).toBeTrue(); - }); + it("should be an array", () => { + expect(Array.isArray(res.body.endpoints)).toBeTrue(); + }); }); describe("GET /images works properly", async () => { - const res = await request(app).get("/images"); + const res = await request(app).get("/images"); - it("should be an array", () => { - expect(Array.isArray(res.body.images)).toBeTrue(); - }); + it("should be an array", () => { + expect(Array.isArray(res.body.images)).toBeTrue(); + }); - it("should return a 200", async () => { - expect(res.statusCode).toBe(200); - }); + it("should return a 200", async () => { + expect(res.statusCode).toBe(200); + }); }); describe("POST /images works properly", () => { - it("should return 401 for unauthenticated requests", async () => { - const res = await request(app) - .post("/images") - .send({ - url: "https://test.url.com/0", - status: "available", - tags: ["2girls", "touhou"], - }); - expect(res.status).toBe(401); - }); + it("should return 401 for unauthenticated requests", async () => { + const res = await request(app) + .post("/images") + .send({ + url: "https://test.url.com/0", + status: "available", + tags: ["2girls", "touhou"], + }); + expect(res.status).toBe(401); + }); - it("should return 403 for invalid tokens", async () => { - const res = await request(app) - .post("/images") - .set("authorization", `Bearer token`) - .send({ - url: "https://test.url.com/0", - status: "available", - tags: ["2girls", "touhou"], - }); - expect(res.status).toBe(403); - }); + it("should return 403 for invalid tokens", async () => { + const res = await request(app) + .post("/images") + .set("authorization", `Bearer token`) + .send({ + url: "https://test.url.com/0", + status: "available", + tags: ["2girls", "touhou"], + }); + expect(res.status).toBe(403); + }); - it("should return 201 for new image", async () => { - const res = await request(app) - .post("/images") - .set("authorization", `Bearer ${token}`) - .send({ - url: "https://test.url.com/1", - status: "available", - tags: ["2girls", "touhou"], - }); - expect(res.status).toBe(201); - }); + it("should return 201 for new image", async () => { + const res = await request(app) + .post("/images") + .set("authorization", `Bearer ${token}`) + .send({ + url: "https://test.url.com/1", + status: "available", + tags: ["2girls", "touhou"], + }); + expect(res.status).toBe(201); + }); - it("should return 409 for a repeated images", async () => { - await request(app) - .post("/images") - .set("authorization", `Bearer ${token}`) - .send({ - url: "https://test.url.com/2", - status: "available", - tags: ["2girls", "touhou"], - }); + it("should return 409 for a repeated images", async () => { + await request(app) + .post("/images") + .set("authorization", `Bearer ${token}`) + .send({ + url: "https://test.url.com/2", + status: "available", + tags: ["2girls", "touhou"], + }); - const res = await request(app) - .post("/images") - .set("authorization", `Bearer ${token}`) - .send({ - url: "https://test.url.com/2", - status: "available", - tags: ["2girls", "touhou"], - }); + const res = await request(app) + .post("/images") + .set("authorization", `Bearer ${token}`) + .send({ + url: "https://test.url.com/2", + status: "available", + tags: ["2girls", "touhou"], + }); - expect(res.status).toBe(409); - }); + expect(res.status).toBe(409); + }); - it("should return 500 for an error on the service", async () => { - mock.module("../src/services/ImageService", () => ({ - default: { - add: () => { - throw new Error("This is an expected testing error"); - }, - }, - })); + it("should return 500 for an error on the service", async () => { + mock.module("../src/services/ImageService", () => ({ + default: { + add: () => { + throw new Error("This is an expected testing error"); + }, + }, + })); - const res = await request(app) - .post("/images") - .set("authorization", `Bearer ${token}`) - .send({ - url: "https://test.url.com/3", - status: "available", - tags: ["2girls", "touhou"], - }); + const res = await request(app) + .post("/images") + .set("authorization", `Bearer ${token}`) + .send({ + url: "https://test.url.com/3", + status: "available", + tags: ["2girls", "touhou"], + }); - expect(res.status).toBe(500); - }); + expect(res.status).toBe(500); + }); - it("should return 400 for malformed requests", async () => { - mock.restore(); - const res = await request(app) - .post("/images") - .set("authorization", `Bearer ${token}`) - .send({ - url: "https://test.url.com/4", - status: "wrong", - tags: ["2girls", "touhou"], - }); - expect(res.status).toBe(400); - }); + it("should return 400 for malformed requests", async () => { + mock.restore(); + const res = await request(app) + .post("/images") + .set("authorization", `Bearer ${token}`) + .send({ + url: "https://test.url.com/4", + status: "wrong", + tags: ["2girls", "touhou"], + }); + expect(res.status).toBe(400); + }); }); diff --git a/tests/memoryServer.ts b/tests/memoryServer.ts index 8dd7513..a7e6e5c 100644 --- a/tests/memoryServer.ts +++ b/tests/memoryServer.ts @@ -16,4 +16,4 @@ class MemoryServer { } } -export default new MemoryServer(); \ No newline at end of file +export default new MemoryServer(); diff --git a/tests/populateDatabase.ts b/tests/populateDatabase.ts index 93f63c1..f60c9c4 100644 --- a/tests/populateDatabase.ts +++ b/tests/populateDatabase.ts @@ -2,14 +2,14 @@ import authModel from "../src/models/AuthModel"; import imageModel from "../src/models/ImageModel"; export default async function () { - await imageModel.create({ - url: "https://example.com", - status: "consumed", - tags: ["2girls", "sleeping"], - }); + await imageModel.create({ + url: "https://example.com", + status: "consumed", + tags: ["2girls", "sleeping"], + }); - await authModel.create({ - app: "tester", - secret: "test", - }); + await authModel.create({ + app: "tester", + secret: "test", + }); } -- 2.39.2 From 76d576461624d18c233b4fa5a9d1914f26339aa9 Mon Sep 17 00:00:00 2001 From: Alie Date: Fri, 29 Dec 2023 19:47:35 +0100 Subject: [PATCH 53/64] added test coverage to image controler --- src/controllers/ImageController.ts | 2 +- tests/app.test.ts | 15 +++++++++++++++ 2 files changed, 16 insertions(+), 1 deletion(-) diff --git a/src/controllers/ImageController.ts b/src/controllers/ImageController.ts index e60bfea..8c78e99 100644 --- a/src/controllers/ImageController.ts +++ b/src/controllers/ImageController.ts @@ -3,7 +3,7 @@ import imageService from "../services/ImageService"; import mongoose, { mongo } from "mongoose"; class ImageController { - async getAllImages(req: Request, res: Response): Promise { + async getAllImages(_: Request, res: Response): Promise { try { const images = await imageService.findAll(); res.json({ images }); diff --git a/tests/app.test.ts b/tests/app.test.ts index 0e8fbda..7d8fbb9 100644 --- a/tests/app.test.ts +++ b/tests/app.test.ts @@ -83,6 +83,21 @@ describe("GET /images works properly", async () => { it("should return a 200", async () => { expect(res.statusCode).toBe(200); }); + + it("should return 500 for an error on the service", async () => { + mock.module("../src/services/ImageService", () => ({ + default: { + add: () => { + throw new Error("This is an expected testing error"); + }, + }, + })); + + const res = await request(app) + .get("/images"); + + expect(res.status).toBe(500); + }); }); describe("POST /images works properly", () => { -- 2.39.2 From 6561c710ff3951d31b667b671884b7b1490bcb45 Mon Sep 17 00:00:00 2001 From: Sugui Date: Fri, 29 Dec 2023 19:54:56 +0100 Subject: [PATCH 54/64] Deleted DEDICATED_MONGO_SERVER env variable --- compose.yaml | 2 -- 1 file changed, 2 deletions(-) diff --git a/compose.yaml b/compose.yaml index 2ca513c..9ffce94 100644 --- a/compose.yaml +++ b/compose.yaml @@ -27,8 +27,6 @@ services: MONGODB_USER: "root" MONGODB_PASS: "password" JWTSECRET: "cooljwtsecret" - # DEDICATED_MONGODB_SERVER is for integration testing, for disabling memory server in tests - DEDICATED_MONGODB_SERVER: "true" volumes: - ./:/usr/src/app:ro -- 2.39.2 From cabe730ab8fcf179d4df852ae6735b30ea48b894 Mon Sep 17 00:00:00 2001 From: Sugui Date: Fri, 29 Dec 2023 20:10:36 +0100 Subject: [PATCH 55/64] Testing adding MONGO_URL as env var --- compose.yaml | 1 + 1 file changed, 1 insertion(+) diff --git a/compose.yaml b/compose.yaml index 9ffce94..b45561f 100644 --- a/compose.yaml +++ b/compose.yaml @@ -24,6 +24,7 @@ services: - mongodb environment: MONGODB_URI: "mongodb://mongodb:27017/bot" + MONGO_URL: "mongodb://mongodb:27017/bot" MONGODB_USER: "root" MONGODB_PASS: "password" JWTSECRET: "cooljwtsecret" -- 2.39.2 From 6bb187305ad8eeb8568818ed74a47dae2eb2ccdf Mon Sep 17 00:00:00 2001 From: Sugui Date: Fri, 29 Dec 2023 20:22:27 +0100 Subject: [PATCH 56/64] undid last commit --- compose.yaml | 1 - 1 file changed, 1 deletion(-) diff --git a/compose.yaml b/compose.yaml index b45561f..9ffce94 100644 --- a/compose.yaml +++ b/compose.yaml @@ -24,7 +24,6 @@ services: - mongodb environment: MONGODB_URI: "mongodb://mongodb:27017/bot" - MONGO_URL: "mongodb://mongodb:27017/bot" MONGODB_USER: "root" MONGODB_PASS: "password" JWTSECRET: "cooljwtsecret" -- 2.39.2 From 640514af6dc4b199f8325ee69f7fac041c487e6a Mon Sep 17 00:00:00 2001 From: Sugui Date: Fri, 29 Dec 2023 22:01:23 +0100 Subject: [PATCH 57/64] ping --- .gitea/workflows/test.yaml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/.gitea/workflows/test.yaml b/.gitea/workflows/test.yaml index 14460b3..1a4ddc5 100644 --- a/.gitea/workflows/test.yaml +++ b/.gitea/workflows/test.yaml @@ -16,4 +16,4 @@ jobs: - name: Install project dependencies run: docker run -w /usr/src/app -v ./:/usr/src/app node:20 npm install - name: Run docker-compose - run: docker compose down -v && docker compose run bot-api bun test \ No newline at end of file + run: docker compose down -v && docker compose run bot-api ping mongodb \ No newline at end of file -- 2.39.2 From d368c6bed0136085b17ee81e09caea06de80b89b Mon Sep 17 00:00:00 2001 From: Sugui Date: Sun, 31 Dec 2023 11:31:28 +0100 Subject: [PATCH 58/64] change to mongo:bionic to support the runner on pi --- compose.yaml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/compose.yaml b/compose.yaml index 9ffce94..30088de 100644 --- a/compose.yaml +++ b/compose.yaml @@ -2,7 +2,7 @@ version: "3" services: mongodb: - image: mongo + image: mongo:bionic container_name: mongodb ports: - "27017:27017" -- 2.39.2 From 7fc78c0a036909d3a358712e79f1096bef8f00a7 Mon Sep 17 00:00:00 2001 From: Sugui Date: Sun, 31 Dec 2023 11:31:46 +0100 Subject: [PATCH 59/64] Added bun again to install dependencies in action --- .gitea/workflows/test.yaml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/.gitea/workflows/test.yaml b/.gitea/workflows/test.yaml index 1a4ddc5..227b55b 100644 --- a/.gitea/workflows/test.yaml +++ b/.gitea/workflows/test.yaml @@ -14,6 +14,6 @@ jobs: - name: Check out repository code uses: actions/checkout@v3 - name: Install project dependencies - run: docker run -w /usr/src/app -v ./:/usr/src/app node:20 npm install + run: docker run -w /usr/src/app -v ./:/usr/src/app oven/bun:1 bun install - name: Run docker-compose run: docker compose down -v && docker compose run bot-api ping mongodb \ No newline at end of file -- 2.39.2 From e1ced40c065014599a8b8cb523f08f366eacc236 Mon Sep 17 00:00:00 2001 From: Sugui Date: Sun, 31 Dec 2023 11:36:38 +0100 Subject: [PATCH 60/64] monothread and verbose on bun install --- .gitea/workflows/test.yaml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/.gitea/workflows/test.yaml b/.gitea/workflows/test.yaml index 227b55b..b237d6a 100644 --- a/.gitea/workflows/test.yaml +++ b/.gitea/workflows/test.yaml @@ -14,6 +14,6 @@ jobs: - name: Check out repository code uses: actions/checkout@v3 - name: Install project dependencies - run: docker run -w /usr/src/app -v ./:/usr/src/app oven/bun:1 bun install + run: docker run -w /usr/src/app -v ./:/usr/src/app oven/bun:1 bun install --verbose --concurrent-scripts 1 - name: Run docker-compose run: docker compose down -v && docker compose run bot-api ping mongodb \ No newline at end of file -- 2.39.2 From ec1b6935387267d29c8a4ba4b99f8900fa0ed856 Mon Sep 17 00:00:00 2001 From: Sugui Date: Sun, 31 Dec 2023 11:42:19 +0100 Subject: [PATCH 61/64] Removed mongodb-memory-server --- package.json | 5 ++--- tests/app.test.ts | 7 ------- tests/memoryServer.ts | 19 ------------------- 3 files changed, 2 insertions(+), 29 deletions(-) delete mode 100644 tests/memoryServer.ts diff --git a/package.json b/package.json index 0432e6b..e11f4b4 100644 --- a/package.json +++ b/package.json @@ -3,14 +3,13 @@ "module": "index.ts", "type": "module", "devDependencies": { - "@types/jest": "^29.5.11", - "@types/supertest": "^6.0.1", "@types/express": "^4.17.21", "@types/express-list-endpoints": "^6.0.3", + "@types/jest": "^29.5.11", "@types/jsonwebtoken": "^9.0.5", + "@types/supertest": "^6.0.1", "bun-types": "latest", "jest": "^29.7.0", - "mongodb-memory-server": "^9.1.3", "supertest": "^6.3.3", "ts-jest": "^29.1.1" }, diff --git a/tests/app.test.ts b/tests/app.test.ts index 7d8fbb9..f8f7652 100644 --- a/tests/app.test.ts +++ b/tests/app.test.ts @@ -1,5 +1,4 @@ import { - afterAll, afterEach, beforeAll, describe, @@ -10,7 +9,6 @@ import { import request, { Response } from "supertest"; import app, { startApp } from "../src/app"; import imageService from "../src/services/ImageService"; -import memoryServer from "./memoryServer"; import populateDatabase from "./populateDatabase"; const imageServiceOriginal = imageService; @@ -18,7 +16,6 @@ const imageServiceOriginal = imageService; let token: string; beforeAll(async () => { - //if (!process.env.DEDICATED_MONGODB_SERVER) await memoryServer.start(); await startApp(); await populateDatabase(); @@ -28,10 +25,6 @@ beforeAll(async () => { token = tok.body.token; }); -/* afterAll(async () => { - if (!process.env.DEDICATED_MONGODB_SERVER) await memoryServer.stop(); -}); - */ afterEach(() => { mock.restore(); mock.module("../src/services/ImageService", () => ({ diff --git a/tests/memoryServer.ts b/tests/memoryServer.ts deleted file mode 100644 index a7e6e5c..0000000 --- a/tests/memoryServer.ts +++ /dev/null @@ -1,19 +0,0 @@ -import { MongoMemoryServer } from "mongodb-memory-server"; - -class MemoryServer { - mongod: MongoMemoryServer | undefined; - - async start() { - this.mongod = await MongoMemoryServer.create(); - const uri = this.mongod.getUri("bot"); - process.env.MONGODB_URI = uri; - } - - async stop() { - if (this.mongod) { - await this.mongod.stop(); - } - } -} - -export default new MemoryServer(); -- 2.39.2 From 5941fcc1f2c56f7c7faa45f770f1937089223d50 Mon Sep 17 00:00:00 2001 From: Sugui Date: Sun, 31 Dec 2023 11:44:25 +0100 Subject: [PATCH 62/64] added node again --- .gitea/workflows/test.yaml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/.gitea/workflows/test.yaml b/.gitea/workflows/test.yaml index b237d6a..1a4ddc5 100644 --- a/.gitea/workflows/test.yaml +++ b/.gitea/workflows/test.yaml @@ -14,6 +14,6 @@ jobs: - name: Check out repository code uses: actions/checkout@v3 - name: Install project dependencies - run: docker run -w /usr/src/app -v ./:/usr/src/app oven/bun:1 bun install --verbose --concurrent-scripts 1 + run: docker run -w /usr/src/app -v ./:/usr/src/app node:20 npm install - name: Run docker-compose run: docker compose down -v && docker compose run bot-api ping mongodb \ No newline at end of file -- 2.39.2 From 199f06db433305cbb87283959018a8cc72d97be2 Mon Sep 17 00:00:00 2001 From: Sugui Date: Sun, 31 Dec 2023 11:54:46 +0100 Subject: [PATCH 63/64] Now install packages without using a container, and fixed the ping thing --- .gitea/workflows/test.yaml | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/.gitea/workflows/test.yaml b/.gitea/workflows/test.yaml index 1a4ddc5..7c64ca0 100644 --- a/.gitea/workflows/test.yaml +++ b/.gitea/workflows/test.yaml @@ -10,10 +10,10 @@ jobs: - name: Starting docker daemon run: docker-init -- dockerd --host=unix:///var/run/docker.sock & - name: Installing necessary packages - run: apk add nodejs git curl bash + run: apk add npm git curl bash - name: Check out repository code uses: actions/checkout@v3 - name: Install project dependencies - run: docker run -w /usr/src/app -v ./:/usr/src/app node:20 npm install + run: npm install - name: Run docker-compose - run: docker compose down -v && docker compose run bot-api ping mongodb \ No newline at end of file + run: docker compose down -v && docker compose run bot-api bun test \ No newline at end of file -- 2.39.2 From 98f9c5bf8e12a144cb66143c6a2e22665086aceb Mon Sep 17 00:00:00 2001 From: Alie Date: Sun, 31 Dec 2023 12:01:21 +0100 Subject: [PATCH 64/64] deleted a console log --- src/controllers/ImageController.ts | 1 - 1 file changed, 1 deletion(-) diff --git a/src/controllers/ImageController.ts b/src/controllers/ImageController.ts index 8c78e99..75fca37 100644 --- a/src/controllers/ImageController.ts +++ b/src/controllers/ImageController.ts @@ -8,7 +8,6 @@ class ImageController { const images = await imageService.findAll(); res.json({ images }); } catch (error) { - console.error(error); res.status(500).json({ error: "Internal Server Error" }); } } -- 2.39.2