From 7c68174ca40d5be567ed4f38d4c3c5a171cb7e19 Mon Sep 17 00:00:00 2001 From: Alie Date: Fri, 29 Dec 2023 16:05:51 +0100 Subject: [PATCH] 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"); });