Backend: code formatted

This commit is contained in:
2024-05-12 21:25:52 +02:00
parent fa25015472
commit bf791db47f
26 changed files with 574 additions and 469 deletions

View File

@@ -1,4 +1,4 @@
import { connectDB, dropDB, dropCollections } from '../src/utils/test_mongodb';
import { connectDB, dropDB, dropCollections } from "../src/utils/test_mongodb";
beforeAll(async () => {
await connectDB();

View File

@@ -1,22 +1,22 @@
import supertest from 'supertest';
import { app } from '../src/app';
import { connectDB, dropDB, dropCollections } from '../src/utils/test_mongodb';
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 {
const resReg: any = await request.post('/api/v1/auth/signup').send({
email: 'test@example.local',
password: 'admin1234',
username: 'Test Test'
const resReg: any = await request.post("/api/v1/auth/signup").send({
email: "test@example.local",
password: "admin1234",
username: "Test Test",
});
const resLog: any = await request.post('/api/auth/login').send({
email: 'test@example.local',
password: 'admin1234'
const resLog: any = await request.post("/api/auth/login").send({
email: "test@example.local",
password: "admin1234",
});
if (resLog.statusCode != 200) throw 'error while logging in';
if (resLog.statusCode != 200) throw "error while logging in";
const body = JSON.parse(resLog.text);
return Promise.resolve(body.data.jwt);
@@ -31,194 +31,194 @@ export const getJWT = async () => {
* @returns JWT cookie
*/
export async function login(): Promise<string> {
const res = await request.post('/api/v1/auth/signin').send({
email: 'thisistest@host.local',
password: 'Admin1234'
const res = await request.post("/api/v1/auth/signin").send({
email: "thisistest@host.local",
password: "Admin1234",
});
return res.headers['set-cookie'];
return res.headers["set-cookie"];
}
export async function signup(): Promise<boolean> {
const res = await request.post('/api/v1/auth/signup').send({
email: 'thisistest@host.local',
password: 'Admin1234',
username: 'Test Test'
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;
}
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({});
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);
});
it('should drop 400 (email))', async () => {
const res: any = await request.post('/api/v1/auth/signup').send({
email: '',
username: 'User Admin',
password: 'Admin1234'
it("should drop 400 (email))", async () => {
const res: any = await request.post("/api/v1/auth/signup").send({
email: "",
username: "User Admin",
password: "Admin1234",
});
console.log(res)
console.log(res);
const body = JSON.parse(res.text);
expect(res.statusCode).toBe(400);
expect(body.data.path).toBe('email');
expect(body.data.path).toBe("email");
});
it('should drop 400 (username))', async () => {
const res: any = await request.post('/api/v1/auth/signup').send({
email: 'admin@localhost.local',
username: '',
password: 'Admin1234'
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);
expect(body.data.path).toBe('username');
expect(body.data.path).toBe("username");
});
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: ''
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);
expect(body.data.path).toBe('password');
expect(body.data.path).toBe("password");
});
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'
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);
expect(body.data.path).toBe('password');
expect(body.data.path).toBe("password");
});
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'
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);
expect(body.data.path).toBe('password');
expect(body.data.path).toBe("password");
});
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'
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);
expect(body.data.path).toBe('password');
expect(body.data.path).toBe("password");
});
});
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'
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);
});
});
describe('POST /api/v1/auth/signin', () => {
const url = '/api/v1/auth/signin';
describe("POST /api/v1/auth/signin", () => {
const url = "/api/v1/auth/signin";
describe('should drop an validation error', () => {
it('should drop 400 (empty)', async () => {
describe("should drop an validation error", () => {
it("should drop 400 (empty)", async () => {
const res = await request.post(url).send();
expect(res.statusCode).toBe(400);
});
it('should drop 400 (email)', async () => {
it("should drop 400 (email)", async () => {
const res = await request.post(url).send({
password: 'Admin1234'
password: "Admin1234",
});
const body = JSON.parse(res.text);
expect(res.statusCode).toBe(400);
expect(body.data.path).toBe('email');
expect(body.data.path).toBe("email");
});
it('should drop 400 (password)', async () => {
it("should drop 400 (password)", async () => {
const res = await request.post(url).send({
email: 'thisistest@host.local'
email: "thisistest@host.local",
});
const body = JSON.parse(res.text);
expect(res.statusCode).toBe(400);
expect(body.data.path).toBe('password');
expect(body.data.path).toBe("password");
});
});
test('should drop 401', async () => {
test("should drop 401", async () => {
const res = await request.post(url).send({
email: 'thisistest@host.local',
password: 'Test12365465132'
email: "thisistest@host.local",
password: "Test12365465132",
});
expect(res.statusCode).toBe(401);
expect(res.header['set-cookie'][0]).toContain("jwt=; Max-Age=0")
expect(res.header['set-cookie'][1]).toContain("auth=false")
expect(res.header["set-cookie"][0]).toContain("jwt=; Max-Age=0");
expect(res.header["set-cookie"][1]).toContain("auth=false");
});
test('should login an user', async () => {
test("should login an user", async () => {
const res: any = await request.post(url).send({
email: 'thisistest@host.local',
password: 'Admin1234'
email: "thisistest@host.local",
password: "Admin1234",
});
expect(res.statusCode).toBe(200);
expect(res.header['set-cookie'][0]).toContain("jwt=")
expect(res.header['set-cookie'][1]).toContain("auth=true")
expect(res.header["set-cookie"][0]).toContain("jwt=");
expect(res.header["set-cookie"][1]).toContain("auth=true");
});
});
describe('POST /api/v1/auth/logout', () => {
const url = '/api/v1/auth/logout';
test('should drop 401 error', async () => {
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);
});
test('should logout an user', async () => {
test("should logout an user", async () => {
const jwt = await login();
const res = await request.post(url).set('Cookie', jwt).send();
const res = await request.post(url).set("Cookie", jwt).send();
expect(res.statusCode).toBe(200);
expect(res.header['set-cookie'][0]).toContain("jwt=; Max-Age=0")
expect(res.header['set-cookie'][1]).toContain("auth=false")
expect(res.header["set-cookie"][0]).toContain("jwt=; Max-Age=0");
expect(res.header["set-cookie"][1]).toContain("auth=false");
});
});
describe('GET /api/v1/auth/status', () => {
const url = '/api/v1/auth/status';
test('should return login status 401', async () => {
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);
});
test('should return login status 200', async () => {
test("should return login status 200", async () => {
const jwt = await login();
const res = await request.get(url).set('Cookie', jwt).send();
const res = await request.get(url).set("Cookie", jwt).send();
expect(res.statusCode).toBe(200);
expect(res.body.data.username).toBe("Test Test")
expect(res.body.data.email).toBe("thisistest@host.local")
expect(res.body.data.password).toBeUndefined()
expect(res.body.data.username).toBe("Test Test");
expect(res.body.data.email).toBe("thisistest@host.local");
expect(res.body.data.password).toBeUndefined();
});
});
});

View File

@@ -1,166 +1,166 @@
import supertest from 'supertest';
import { app } from '../src/app';
import { login } from './auth.test';
import { addExam, delExam, editExam } from '../src/validators/beerValidator';
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);
describe('POST /api/v1/beer/add', () => {
const url = '/api/v1/beer/add';
test('should drop 401 error', async () => {
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);
});
test('should drop 400 ()', async () => {
test("should drop 400 ()", async () => {
const jwt = await login();
const res = await request.post(url).set('Cookie', jwt).send({});
const res = await request.post(url).set("Cookie", jwt).send({});
expect(res.statusCode).toBe(400);
});
test('should drop 400 (brand)', async () => {
test("should drop 400 (brand)", async () => {
const jwt = await login();
const body: any = { ...addExam };
delete body.brand;
const res = await request.post(url).set('Cookie', jwt).send(body);
const res = await request.post(url).set("Cookie", jwt).send(body);
expect(res.statusCode).toBe(400);
});
test('should drop 201', async () => {
test("should drop 201", async () => {
const jwt = await login();
const res = await request.post(url).set('Cookie', jwt).send(addExam);
const res = await request.post(url).set("Cookie", jwt).send(addExam);
expect(res.statusCode).toBe(201);
});
});
describe('GET /api/v1/beer/get', () => {
const url = '/api/v1/beer/get';
describe("GET /api/v1/beer/get", () => {
const url = "/api/v1/beer/get";
test('should drop 401', async () => {
test("should drop 401", async () => {
const res = await request.get(url).send();
expect(res.statusCode).toBe(401);
});
test('should drop 200', async () => {
test("should drop 200", async () => {
const jwt = await login();
const res = await request.get(url).set('Cookie', jwt).send();
const res = await request.get(url).set("Cookie", jwt).send();
expect(res.statusCode).toBe(200);
});
});
describe('POST /api/v1/beer/del', () => {
const url = '/api/v1/beer/del';
describe("POST /api/v1/beer/del", () => {
const url = "/api/v1/beer/del";
test('should drop 401', async () => {
test("should drop 401", async () => {
const res = await request.post(url).send();
expect(res.statusCode).toBe(401);
});
test('should drop 400', async () => {
test("should drop 400", async () => {
const jwt = await login();
const res = await request.post(url).set('Cookie', jwt).send(delExam);
const res = await request.post(url).set("Cookie", jwt).send(delExam);
expect(res.statusCode).toBe(400);
});
test('should drop 400', async () => {
test("should drop 400", async () => {
const jwt = await login();
const res = await request.post(url).set('Cookie', jwt).send({
_id: 'thisWillNotWork'
const res = await request.post(url).set("Cookie", jwt).send({
_id: "thisWillNotWork",
});
expect(res.statusCode).toBe(400);
});
test('should drop 200', async () => {
test("should drop 200", async () => {
const jwt = await login();
const req = await request.post('/api/v1/beer/add').set('Cookie', jwt).send(addExam);
const req = await request.post("/api/v1/beer/add").set("Cookie", jwt).send(addExam);
const id = req.body.data._id;
const res = await request.post(url).set('Cookie', jwt).send({
_id: id
const res = await request.post(url).set("Cookie", jwt).send({
_id: id,
});
expect(res.statusCode).toBe(200);
});
});
describe('POST /api/v1/beer/edit', () => {
const url = '/api/v1/beer/edit';
describe("POST /api/v1/beer/edit", () => {
const url = "/api/v1/beer/edit";
test('should drop 401', async () => {
test("should drop 401", async () => {
const res = await request.post(url).send();
expect(res.statusCode).toBe(401);
});
test('should drop 400', async () => {
test("should drop 400", async () => {
const jwt = await login();
const payload: any = { ...editExam };
delete payload._id;
const res = await request.post(url).set('Cookie', jwt).send(payload);
const res = await request.post(url).set("Cookie", jwt).send(payload);
expect(res.statusCode).toBe(400);
});
test('should drop 200', async () => {
test("should drop 200", async () => {
const jwt = await login();
const payload: any = { ...editExam };
delete payload.name;
const res = await request.post(url).set('Cookie', jwt).send(payload);
const res = await request.post(url).set("Cookie", jwt).send(payload);
expect(res.statusCode).toBe(200);
});
test('should drop 200', async () => {
test("should drop 200", async () => {
const jwt = await login();
const payload: any = { ...editExam };
delete payload.degree;
const res = await request.post(url).set('Cookie', jwt).send(payload);
const res = await request.post(url).set("Cookie", jwt).send(payload);
expect(res.statusCode).toBe(200);
});
test('should drop 200', async () => {
test("should drop 200", async () => {
const jwt = await login();
const payload: any = { ...editExam };
delete payload.packaging;
const res = await request.post(url).set('Cookie', jwt).send(payload);
const res = await request.post(url).set("Cookie", jwt).send(payload);
expect(res.statusCode).toBe(200);
});
test('should drop 200', async () => {
test("should drop 200", async () => {
const jwt = await login();
const payload: any = { ...editExam };
delete payload.brand;
const res = await request.post(url).set('Cookie', jwt).send(payload);
const res = await request.post(url).set("Cookie", jwt).send(payload);
expect(res.statusCode).toBe(200);
});
test('should drop 200', async () => {
test("should drop 200", async () => {
const jwt = await login();
const req = await request.post('/api/v1/beer/add').set('Cookie', jwt).send(addExam);
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 };
let res = await request.post(url).set('Cookie', jwt).send(payload);
let res = await request.post(url).set("Cookie", jwt).send(payload);
delete res.body.data._id;
delete res.body.data.__v;
@@ -172,4 +172,4 @@ describe('POST /api/v1/beer/edit', () => {
expect(res.statusCode).toBe(200);
expect(eq).toBe(true);
});
});
});

View File

@@ -1,12 +1,12 @@
import request from 'supertest';
import { app } from '../src/app';
import request from "supertest";
import { app } from "../src/app";
describe('GET /api/v1', () => {
describe('should return json with docs', () => {
test('should respond with a 200 status code', async () => {
const response = await request(app).get('/api/v1').send({});
expect(response.headers['content-type']).toMatch(/json/);
describe("GET /api/v1", () => {
describe("should return json with docs", () => {
test("should respond with a 200 status code", async () => {
const response = await request(app).get("/api/v1").send({});
expect(response.headers["content-type"]).toMatch(/json/);
expect(response.statusCode).toBe(200);
});
});
});
});

View File

@@ -1,32 +1,32 @@
import supertest from 'supertest';
import { app } from '../src/app';
import { login } from './auth.test';
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);
describe('POST /api/v1/review/add', () => {
const url = '/api/v1/review/add';
test('should drop 401 error', async () => {
describe("POST /api/v1/review/add", () => {
const url = "/api/v1/review/add";
test("should drop 401 error", async () => {
const res = await request.post(url).send({});
expect(res.statusCode).toBe(401);
});
test('should drop 400 ()', async () => {
test("should drop 400 ()", async () => {
const jwt = await login();
const res = await request.post(url).set('Cookie', jwt).send({});
const res = await request.post(url).set("Cookie", jwt).send({});
console.log("TEST", await res.body)
console.log("TEST", await res.body);
expect(res.statusCode).toBe(400);
});
// test('should drop 201', async () => {
// const jwt = await login();
// const res = await request.post(url).set('Cookie', jwt).send(addExam);
//
// expect(res.statusCode).toBe(201);
// });
// test('should drop 201', async () => {
// const jwt = await login();
// const res = await request.post(url).set('Cookie', jwt).send(addExam);
//
// expect(res.statusCode).toBe(201);
// });
});
//describe('GET /api/v1/beer/get', () => {
@@ -45,5 +45,3 @@ describe('POST /api/v1/review/add', () => {
// expect(res.statusCode).toBe(200);
// });
//});