deguapp/api/tests/beer.test.ts

176 lines
4.3 KiB
TypeScript
Raw Normal View History

2024-05-12 21:25:52 +02:00
import supertest from "supertest";
import { app } from "../src/app";
import { login } from "./auth.test";
import { addExam, delExam, editExam } from "../src/validators/beerValidator";
const request = supertest(app);
2024-05-12 21:25:52 +02:00
describe("POST /api/v1/beer/add", () => {
const url = "/api/v1/beer/add";
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 drop 400 ()", 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(400);
});
2024-05-12 21:25:52 +02:00
test("should drop 400 (brand)", async () => {
const jwt = await login();
const body: any = { ...addExam };
delete body.brand;
2024-05-12 21:25:52 +02:00
const res = await request.post(url).set("Cookie", jwt).send(body);
expect(res.statusCode).toBe(400);
});
2024-05-12 21:25:52 +02:00
test("should drop 201", async () => {
const jwt = await login();
2024-05-12 21:25:52 +02:00
const res = await request.post(url).set("Cookie", jwt).send(addExam);
expect(res.statusCode).toBe(201);
});
});
2024-05-12 21:25:52 +02:00
describe("GET /api/v1/beer/get", () => {
const url = "/api/v1/beer/get";
2024-05-12 21:25:52 +02:00
test("should drop 401", async () => {
const res = await request.get(url).send();
expect(res.statusCode).toBe(401);
});
2024-05-12 21:25:52 +02:00
test("should drop 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
describe("POST /api/v1/beer/del", () => {
const url = "/api/v1/beer/del";
2024-05-12 21:25:52 +02:00
test("should drop 401", async () => {
const res = await request.post(url).send();
expect(res.statusCode).toBe(401);
});
2024-05-12 21:25:52 +02:00
test("should drop 400", async () => {
const jwt = await login();
2024-05-12 21:25:52 +02:00
const res = await request.post(url).set("Cookie", jwt).send(delExam);
expect(res.statusCode).toBe(400);
});
2024-05-12 21:25:52 +02:00
test("should drop 400", async () => {
const jwt = await login();
2024-05-12 21:25:52 +02:00
const res = await request.post(url).set("Cookie", jwt).send({
_id: "thisWillNotWork",
});
expect(res.statusCode).toBe(400);
});
2024-05-12 21:25:52 +02:00
test("should drop 200", async () => {
const jwt = await login();
2024-05-12 21:25:52 +02:00
const req = await request.post("/api/v1/beer/add").set("Cookie", jwt).send(addExam);
const id = req.body.data._id;
2024-05-12 21:25:52 +02:00
const res = await request.post(url).set("Cookie", jwt).send({
_id: id,
});
expect(res.statusCode).toBe(200);
});
});
2024-05-12 21:25:52 +02:00
describe("POST /api/v1/beer/edit", () => {
const url = "/api/v1/beer/edit";
2024-05-12 21:25:52 +02:00
test("should drop 401", async () => {
const res = await request.post(url).send();
expect(res.statusCode).toBe(401);
});
2024-05-12 21:25:52 +02:00
test("should drop 400", async () => {
const jwt = await login();
const payload: any = { ...editExam };
delete payload._id;
2024-05-12 21:25:52 +02:00
const res = await request.post(url).set("Cookie", jwt).send(payload);
expect(res.statusCode).toBe(400);
});
2024-05-12 21:25:52 +02:00
test("should drop 200", async () => {
const jwt = await login();
const payload: any = { ...editExam };
delete payload.name;
2024-05-12 21:25:52 +02:00
const res = await request.post(url).set("Cookie", jwt).send(payload);
expect(res.statusCode).toBe(200);
});
2024-05-12 21:25:52 +02:00
test("should drop 200", async () => {
const jwt = await login();
const payload: any = { ...editExam };
delete payload.degree;
2024-05-12 21:25:52 +02:00
const res = await request.post(url).set("Cookie", jwt).send(payload);
expect(res.statusCode).toBe(200);
});
2024-05-12 21:25:52 +02:00
test("should drop 200", async () => {
const jwt = await login();
const payload: any = { ...editExam };
delete payload.packaging;
2024-05-12 21:25:52 +02:00
const res = await request.post(url).set("Cookie", jwt).send(payload);
expect(res.statusCode).toBe(200);
});
2024-05-12 21:25:52 +02:00
test("should drop 200", async () => {
const jwt = await login();
const payload: any = { ...editExam };
delete payload.brand;
2024-05-12 21:25:52 +02:00
const res = await request.post(url).set("Cookie", jwt).send(payload);
expect(res.statusCode).toBe(200);
});
2024-05-12 21:25:52 +02:00
test("should drop 200", async () => {
const jwt = await login();
2024-05-12 21:25:52 +02:00
const req = await request.post("/api/v1/beer/add").set("Cookie", jwt).send(addExam);
const _id = req.body.data._id;
const payload = { ...editExam, _id: _id };
2024-05-12 21:25:52 +02:00
let res = await request.post(url).set("Cookie", jwt).send(payload);
delete res.body.data._id;
delete res.body.data.__v;
delete res.body.data.createdAt;
delete res.body.data.updatedAt;
delete payload._id;
const eq = JSON.stringify(Object.keys(res.body.data).sort()) === JSON.stringify(Object.keys(payload).sort());
expect(res.statusCode).toBe(200);
expect(eq).toBe(true);
});
2024-05-12 21:25:52 +02:00
});