From 6850989f06469c849274d47dfb3ad09ee709f6c2 Mon Sep 17 00:00:00 2001 From: Alie Date: Fri, 29 Dec 2023 17:10:48 +0100 Subject: [PATCH] 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); - }); -});