From 9cd61c101c305d4c7747e4c301c6a392fde2cb5b Mon Sep 17 00:00:00 2001 From: Alie Date: Fri, 29 Dec 2023 19:37:35 +0100 Subject: [PATCH] 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", + }); }