unit testing #7

Merged
Suguivy merged 67 commits from testing into develop 2023-12-31 11:11:23 +00:00
4 changed files with 23 additions and 41 deletions
Showing only changes of commit 071994a17c - Show all commits

View File

@ -7,7 +7,6 @@ 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

View File

@ -2,9 +2,7 @@ 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;
} }
} }

View File

@ -1,5 +1,5 @@
import { afterAll, 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 request, { Response } from "supertest";
import app, { startApp } from "../src/app"; import app, { startApp } from "../src/app";
import imageService from "../src/services/ImageService"; import imageService from "../src/services/ImageService";
import memoryServer from "./memoryServer"; 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 () => { describe("GET / shows all of the endpoints", async () => {
const res = await request(app).get("/"); const res = await request(app).get("/");

View File

@ -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);
});
});