deguapp/api/tests/auth.test.ts

225 lines
6.2 KiB
TypeScript
Raw Normal View History

2024-05-12 21:25:52 +02:00
import supertest from "supertest";
import { app } from "../src/app";
import { connectDB, dropDB, dropCollections } from "../src/utils/test_mongodb";
const request = supertest(app);
export const getJWT = async () => {
try {
2024-05-12 21:25:52 +02:00
const resReg: any = await request.post("/api/v1/auth/signup").send({
email: "test@example.local",
password: "admin1234",
username: "Test Test",
});
2024-05-12 21:25:52 +02:00
const resLog: any = await request.post("/api/auth/login").send({
email: "test@example.local",
password: "admin1234",
});
2024-05-12 21:25:52 +02:00
if (resLog.statusCode != 200) throw "error while logging in";
const body = JSON.parse(resLog.text);
return Promise.resolve(body.data.jwt);
} catch (err: any) {
console.log(err);
return err;
}
};
/**
*
* @returns JWT cookie
*/
export async function login(): Promise<string> {
2024-05-12 21:25:52 +02:00
const res = await request.post("/api/v1/auth/signin").send({
email: "thisistest@host.local",
password: "Admin1234",
});
2024-05-12 21:25:52 +02:00
return res.headers["set-cookie"];
}
export async function signup(): Promise<boolean> {
2024-05-12 21:25:52 +02:00
const res = await request.post("/api/v1/auth/signup").send({
email: "thisistest@host.local",
password: "Admin1234",
username: "Test Test",
});
if (res.statusCode == 201) return true;
return false;
}
2024-05-12 21:25:52 +02:00
describe("POST /api/v1/auth/signup", () => {
describe("should drop validation error", () => {
it("should drop 400 (empty request))", async () => {
const res: any = await request.post("/api/v1/auth/signup").send({});
expect(res.statusCode).toBe(400);
});
2024-05-12 21:25:52 +02:00
it("should drop 400 (email))", async () => {
const res: any = await request.post("/api/v1/auth/signup").send({
email: "",
username: "User Admin",
password: "Admin1234",
});
2024-05-12 21:25:52 +02:00
console.log(res);
const body = JSON.parse(res.text);
expect(res.statusCode).toBe(400);
2024-05-12 21:25:52 +02:00
expect(body.data.path).toBe("email");
});
2024-05-12 21:25:52 +02:00
it("should drop 400 (username))", async () => {
const res: any = await request.post("/api/v1/auth/signup").send({
email: "admin@localhost.local",
username: "",
password: "Admin1234",
});
const body = JSON.parse(res.text);
expect(res.statusCode).toBe(400);
2024-05-12 21:25:52 +02:00
expect(body.data.path).toBe("username");
});
2024-05-12 21:25:52 +02:00
it("should drop 400 (password))", async () => {
const res: any = await request.post("/api/v1/auth/signup").send({
email: "admin@localhost.local",
username: "User Admin",
password: "",
});
const body = JSON.parse(res.text);
expect(res.statusCode).toBe(400);
2024-05-12 21:25:52 +02:00
expect(body.data.path).toBe("password");
});
2024-05-12 21:25:52 +02:00
it("should drop 400 (password - min 8 chars", async () => {
const res = await request.post("/api/v1/auth/signup").send({
email: "admin@localhost.local",
username: "User Admin",
password: "Admin12",
});
const body = JSON.parse(res.text);
expect(res.statusCode).toBe(400);
2024-05-12 21:25:52 +02:00
expect(body.data.path).toBe("password");
});
2024-05-12 21:25:52 +02:00
it("should drop 400 (password - min 1 number", async () => {
const res = await request.post("/api/v1/auth/signup").send({
email: "admin@localhost.local",
username: "User Admin",
password: "Adminadmin",
});
const body = JSON.parse(res.text);
expect(res.statusCode).toBe(400);
2024-05-12 21:25:52 +02:00
expect(body.data.path).toBe("password");
});
2024-05-12 21:25:52 +02:00
it("should drop 400 (password - min 1 uppercase", async () => {
const res = await request.post("/api/v1/auth/signup").send({
email: "admin@localhost.local",
username: "User Admin",
password: "admin1234",
});
const body = JSON.parse(res.text);
expect(res.statusCode).toBe(400);
2024-05-12 21:25:52 +02:00
expect(body.data.path).toBe("password");
});
});
2024-05-12 21:25:52 +02:00
test("should register an user", async () => {
const res: any = await request.post("/api/v1/auth/signup").send({
email: "thisistest@host.local",
password: "Admin1234",
username: "Test Test",
});
expect(res.statusCode).toBe(201);
});
});
2024-05-12 21:25:52 +02:00
describe("POST /api/v1/auth/signin", () => {
const url = "/api/v1/auth/signin";
2024-05-12 21:25:52 +02:00
describe("should drop an validation error", () => {
it("should drop 400 (empty)", async () => {
const res = await request.post(url).send();
expect(res.statusCode).toBe(400);
});
2024-05-12 21:25:52 +02:00
it("should drop 400 (email)", async () => {
const res = await request.post(url).send({
2024-05-12 21:25:52 +02:00
password: "Admin1234",
});
const body = JSON.parse(res.text);
expect(res.statusCode).toBe(400);
2024-05-12 21:25:52 +02:00
expect(body.data.path).toBe("email");
});
2024-05-12 21:25:52 +02:00
it("should drop 400 (password)", async () => {
const res = await request.post(url).send({
2024-05-12 21:25:52 +02:00
email: "thisistest@host.local",
});
const body = JSON.parse(res.text);
expect(res.statusCode).toBe(400);
2024-05-12 21:25:52 +02:00
expect(body.data.path).toBe("password");
});
});
2024-05-12 21:25:52 +02:00
test("should drop 401", async () => {
const res = await request.post(url).send({
2024-05-12 21:25:52 +02:00
email: "thisistest@host.local",
password: "Test12365465132",
});
2024-05-02 00:37:59 +02:00
expect(res.statusCode).toBe(401);
2024-05-12 21:25:52 +02:00
expect(res.header["set-cookie"][0]).toContain("jwt=; Max-Age=0");
expect(res.header["set-cookie"][1]).toContain("auth=false");
});
2024-05-12 21:25:52 +02:00
test("should login an user", async () => {
const res: any = await request.post(url).send({
2024-05-12 21:25:52 +02:00
email: "thisistest@host.local",
password: "Admin1234",
});
expect(res.statusCode).toBe(200);
2024-05-12 21:25:52 +02:00
expect(res.header["set-cookie"][0]).toContain("jwt=");
expect(res.header["set-cookie"][1]).toContain("auth=true");
});
});
2024-05-12 21:25:52 +02:00
describe("POST /api/v1/auth/logout", () => {
const url = "/api/v1/auth/logout";
test("should drop 401 error", async () => {
const res = await request.post(url).send({});
expect(res.statusCode).toBe(401);
});
2024-05-12 21:25:52 +02:00
test("should logout an user", async () => {
const jwt = await login();
2024-05-12 21:25:52 +02:00
const res = await request.post(url).set("Cookie", jwt).send();
expect(res.statusCode).toBe(200);
2024-05-12 21:25:52 +02:00
expect(res.header["set-cookie"][0]).toContain("jwt=; Max-Age=0");
expect(res.header["set-cookie"][1]).toContain("auth=false");
});
});
2024-05-12 21:25:52 +02:00
describe("GET /api/v1/auth/status", () => {
const url = "/api/v1/auth/status";
test("should return login status 401", async () => {
const res = await request.get(url).send();
expect(res.statusCode).toBe(401);
});
2024-05-12 21:25:52 +02:00
test("should return login status 200", async () => {
const jwt = await login();
2024-05-12 21:25:52 +02:00
const res = await request.get(url).set("Cookie", jwt).send();
expect(res.statusCode).toBe(200);
2024-05-12 21:25:52 +02:00
expect(res.body.data.username).toBe("Test Test");
expect(res.body.data.email).toBe("thisistest@host.local");
expect(res.body.data.password).toBeUndefined();
});
2024-05-12 21:25:52 +02:00
});