unit testing #7
|
@ -7,13 +7,14 @@ const authTokenSecret = process.env.JWTSECRET || "badsecret";
|
||||||
class AuthControler {
|
class AuthControler {
|
||||||
async login(req: Request, res: Response) {
|
async login(req: Request, res: Response) {
|
||||||
// Read app and secret from request body
|
// Read app and secret from request body
|
||||||
|
console.log(req.body)
|
||||||
const { app, secret } = req.body;
|
const { app, secret } = req.body;
|
||||||
|
|
||||||
// Filter app from the apps by app and secret
|
// Filter app from the apps by app and secret
|
||||||
const authenticated = await AuthService.find(app, secret);
|
const authenticated = await AuthService.find(app, secret);
|
||||||
|
|
||||||
if (authenticated) {
|
if (authenticated) {
|
||||||
console.log("Authenticated app ", authenticated.app);
|
console.log("Authenticated app", authenticated.app);
|
||||||
// Generate an access token
|
// Generate an access token
|
||||||
const accessToken = jwt.sign(
|
const accessToken = jwt.sign(
|
||||||
{ app: authenticated.app },
|
{ app: authenticated.app },
|
||||||
|
|
11
src/index.ts
11
src/index.ts
|
@ -1,3 +1,12 @@
|
||||||
|
import populateDatabase from "../tests/populateDatabase";
|
||||||
import { startApp } from "./app";
|
import { startApp } from "./app";
|
||||||
|
|
||||||
startApp();
|
await startApp();
|
||||||
|
|
||||||
|
|
||||||
|
try{
|
||||||
|
await populateDatabase();
|
||||||
|
}
|
||||||
|
catch {
|
||||||
|
|
||||||
|
}
|
|
@ -1,8 +1,10 @@
|
||||||
import AuthModel, { Auth } from "../models/AuthModel";
|
import AuthModel, { Auth } from "../models/AuthModel";
|
||||||
|
|
||||||
class AuthService {
|
class AuthService {
|
||||||
async find(app: string, secret: string): Promise<Auth | null> {
|
async find(app: String, secret: String): Promise<Auth | null> {
|
||||||
|
console.log(app, secret)
|
||||||
const auth = await AuthModel.findOne({ app: app, secret: secret });
|
const auth = await AuthModel.findOne({ app: app, secret: secret });
|
||||||
|
console.log(auth)
|
||||||
return auth;
|
return auth;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -1,28 +1,31 @@
|
||||||
import { afterAll, beforeAll, describe, expect, it } from "bun:test";
|
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 app, { startApp } from "../src/app";
|
||||||
import memoryServer from "./memoryServer";
|
import memoryServer from "./memoryServer";
|
||||||
import populateDatabase from "./populateDatabase";
|
import populateDatabase from "./populateDatabase";
|
||||||
|
|
||||||
beforeAll(async () => {
|
beforeAll(async () => {
|
||||||
if (!process.env.DEDICATED_MONGODB_SERVER)
|
if (!process.env.DEDICATED_MONGODB_SERVER) await memoryServer.start();
|
||||||
await memoryServer.start();
|
|
||||||
await startApp();
|
await startApp();
|
||||||
await populateDatabase();
|
await populateDatabase();
|
||||||
});
|
});
|
||||||
|
|
||||||
afterAll(async () => {
|
afterAll(async () => {
|
||||||
if (!process.env.DEDICATED_MONGODB_SERVER)
|
if (!process.env.DEDICATED_MONGODB_SERVER) await memoryServer.stop();
|
||||||
await memoryServer.stop();
|
|
||||||
});
|
});
|
||||||
|
|
||||||
describe("/login", async () => {
|
describe("/login", async () => {
|
||||||
const correctRespose = await request(app)
|
let correctRespose: Response;
|
||||||
|
beforeAll(async () => {
|
||||||
|
correctRespose = await request(app)
|
||||||
.post("/login")
|
.post("/login")
|
||||||
.send({ app: "tester", secret: "test" });
|
.send({ app: "tester", secret: "test" });
|
||||||
it("should return 200 for correct login", () => {
|
});
|
||||||
|
|
||||||
|
it("should return 200 for correct login", async () => {
|
||||||
expect(correctRespose.status).toBe(200);
|
expect(correctRespose.status).toBe(200);
|
||||||
});
|
});
|
||||||
|
|
||||||
it("should contain a token", () => {
|
it("should contain a token", () => {
|
||||||
expect(correctRespose.body).toHaveProperty("token");
|
expect(correctRespose.body).toHaveProperty("token");
|
||||||
});
|
});
|
||||||
|
|
Loading…
Reference in New Issue