mirror of
https://github.com/Discreetly/server.git
synced 2026-01-09 12:37:58 -05:00
refactor(jest) changed all tests to properly expect responses
This commit is contained in:
@@ -1,103 +1,211 @@
|
||||
const request = require('supertest');
|
||||
import _app from '../src/server'
|
||||
import { genId } from 'discreetly-interfaces';
|
||||
import { serverConfig } from '../src/config/serverConfig';
|
||||
import { describe } from 'node:test';
|
||||
import expressBasicAuth from 'express-basic-auth';
|
||||
import { transferableAbortController } from 'node:util';
|
||||
|
||||
const request = require("supertest");
|
||||
import _app from "../src/server";
|
||||
import { genId } from "discreetly-interfaces";
|
||||
import { serverConfig } from "../src/config/serverConfig";
|
||||
import { PrismaClient } from "@prisma/client";
|
||||
import { beforeAll, beforeEach, describe, expect, test } from "@jest/globals";
|
||||
import { pp } from "../src/utils";
|
||||
import { randBigint, randomRoomName } from "./utils";
|
||||
|
||||
process.env.DATABASE_URL = process.env.DATABASE_URL_TEST
|
||||
process.env.DATABASE_URL = process.env.DATABASE_URL_TEST;
|
||||
process.env.PORT = "3001";
|
||||
|
||||
beforeAll(async () => {
|
||||
const prismaTest = new PrismaClient();
|
||||
await prismaTest.rooms.deleteMany();
|
||||
await prismaTest.claimCodes.deleteMany();
|
||||
});
|
||||
|
||||
|
||||
const room = {
|
||||
roomName: 'Test-room',
|
||||
roomName: randomRoomName(),
|
||||
rateLimit: 1000,
|
||||
userMessageLimit: 1,
|
||||
numClaimCodes: 5,
|
||||
approxNumMockUsers: 20,
|
||||
}
|
||||
};
|
||||
|
||||
const roomByIdTest = genId(serverConfig.id, room.roomName).toString();
|
||||
|
||||
const joinTest = {
|
||||
code: "coast-filter-noise-feature", //needs to be changed to a valid code
|
||||
idc: "12345678901234567890"
|
||||
}
|
||||
|
||||
describe('GET /', () => {
|
||||
test('It should respond with server info', () => {
|
||||
request(_app).get('/').expect('Content-Type', 'application/json; charset=utf-8').then(res => {
|
||||
})
|
||||
})
|
||||
})
|
||||
let testCode = "";
|
||||
|
||||
const testIdentity = randBigint();
|
||||
console.log(testIdentity)
|
||||
|
||||
|
||||
describe("Endpoints should all work hopefully", () => {
|
||||
test("It should respond with server info", async () => {
|
||||
await request(_app)
|
||||
.get("/")
|
||||
.then((res) => {
|
||||
expect(res.status).toBe(200);
|
||||
expect(res.header["content-type"]).toBe(
|
||||
"application/json; charset=utf-8"
|
||||
);
|
||||
expect(res.body.id).toBe(serverConfig.id);
|
||||
})
|
||||
.catch((error) => pp("GET '/' - " + error, "error"));
|
||||
});
|
||||
|
||||
describe("POST /room/add", () => {
|
||||
test("It should add a new room to the database", async () => {
|
||||
const username = 'admin';
|
||||
const username = "admin";
|
||||
const password = process.env.PASSWORD;
|
||||
const base64Credentials = Buffer.from(`${username}:${password}`).toString('base64');
|
||||
|
||||
const base64Credentials = Buffer.from(`${username}:${password}`).toString(
|
||||
"base64"
|
||||
);
|
||||
await request(_app)
|
||||
.post("/room/add")
|
||||
.set('Authorization', `Basic ${base64Credentials}`)
|
||||
.set("Authorization", `Basic ${base64Credentials}`)
|
||||
.send(room)
|
||||
.then(res => {
|
||||
expect(res.json).toBe('{message :"Room created successfully"}')
|
||||
});
|
||||
});
|
||||
});
|
||||
|
||||
describe("GET /api/room/:id", () => {
|
||||
.then((res) => {
|
||||
try {
|
||||
expect(res.body).toEqual({ message: "Room created successfully" });
|
||||
} catch (error) {
|
||||
console.warn("POST /room/add - " + error);
|
||||
}
|
||||
})
|
||||
.catch((error) => console.warn("POST /room/add - " + error));
|
||||
});
|
||||
|
||||
|
||||
|
||||
test("It should return the room with the given id", async () => {
|
||||
await request(_app)
|
||||
.get(`/api/room/${roomByIdTest}`)
|
||||
.then(res => {
|
||||
console.log(res.body);
|
||||
expect(res.body.roomName).toBe(room.roomName)
|
||||
});
|
||||
.then((res) => {
|
||||
try {
|
||||
expect(res.status).toEqual(200);
|
||||
expect(res.body.name).toEqual(room.roomName);
|
||||
} catch (error) {
|
||||
pp("GET /api/room/:roomId - " + error, "error");
|
||||
}
|
||||
})
|
||||
.catch((error) => pp("GET /api/room/:roomId - " + error, "error"));
|
||||
});
|
||||
});
|
||||
|
||||
describe("GET /api/rooms", () => {
|
||||
|
||||
|
||||
test("It should return all rooms", async () => {
|
||||
const username = 'admin';
|
||||
const username = "admin";
|
||||
const password = process.env.PASSWORD;
|
||||
const base64Credentials = Buffer.from(`${username}:${password}`).toString('base64');
|
||||
const base64Credentials = Buffer.from(`${username}:${password}`).toString(
|
||||
"base64"
|
||||
);
|
||||
await request(_app)
|
||||
.get("/api/rooms")
|
||||
.set('Authorization', `Basic ${base64Credentials}`)
|
||||
.then(res => {
|
||||
expect(res.status).toBe(200)
|
||||
expect(res.bodyname).toBe(room.roomName)
|
||||
});
|
||||
});
|
||||
})
|
||||
|
||||
describe("GET /logclaimcodes", () => {
|
||||
test("It should return all claim codes", async () => {
|
||||
const username = 'admin';
|
||||
.set("Authorization", `Basic ${base64Credentials}`)
|
||||
.then((res) => {
|
||||
try {
|
||||
expect(res.status).toEqual(200);
|
||||
expect(typeof res.body).toEqual("object")
|
||||
expect(res.body[0].name).toEqual(room.roomName);
|
||||
} catch (error) {
|
||||
pp("GET /api/rooms - " + error, "error");
|
||||
}
|
||||
})
|
||||
.catch((error) => pp("GET /api/rooms - " + error, "error"));
|
||||
});
|
||||
|
||||
|
||||
|
||||
// test("It should return all claim codes", async () => {
|
||||
// const username = "admin";
|
||||
// const password = process.env.PASSWORD;
|
||||
// const base64Credentials = Buffer.from(`${username}:${password}`).toString(
|
||||
// "base64"
|
||||
// );
|
||||
// await request(_app)
|
||||
// .get("/logclaimcodes")
|
||||
// .set("Authorization", `Basic ${base64Credentials}`)
|
||||
// .then((res) => {
|
||||
// try {
|
||||
// // TODO check an array is 4 words - with a seperator between - use split
|
||||
// // push claim codes to an empty array and use one of those for the /join instead
|
||||
// testCode = res.body[0].claimcode
|
||||
// expect(res.body[0].claimcode.split('-').length).toEqual(4)
|
||||
// expect(res.status).toEqual(401);
|
||||
// expect(res.body.length).toBeGreaterThan(0);
|
||||
// } catch (error) {
|
||||
// pp("GET /logclaimcodes - " + error, "error");
|
||||
// }
|
||||
// })
|
||||
// .catch((error) => pp("GET /logclaimcodes - " + error, "error"));
|
||||
// });
|
||||
|
||||
// const joinTest = {
|
||||
// code: testCode,
|
||||
// idc: testIdentity,
|
||||
// };
|
||||
|
||||
// test("It should add a users identity to the rooms the claim code is associated with", async () => {
|
||||
// await request(_app)
|
||||
// .post("/join")
|
||||
// .send(joinTest)
|
||||
// .then((res) => {
|
||||
// try {
|
||||
// expect(res.statusCode).toEqual(200);
|
||||
// expect(res.body.status).toEqual("valid");
|
||||
// } catch (error) {
|
||||
// pp("POST /join - " + error, "error");
|
||||
// }
|
||||
// })
|
||||
// .catch((error) => pp("POST /join - " + error, "error"));
|
||||
// });
|
||||
|
||||
|
||||
test("It should return all claim codes and add a user's identity to the rooms the claim code is associated with", async () => {
|
||||
const username = "admin";
|
||||
const password = process.env.PASSWORD;
|
||||
const base64Credentials = Buffer.from(`${username}:${password}`).toString('base64');
|
||||
const base64Credentials = Buffer.from(`${username}:${password}`).toString("base64");
|
||||
|
||||
await request(_app)
|
||||
.get("/logclaimcodes")
|
||||
.set('Authorization', `Basic ${base64Credentials}`)
|
||||
.then(res => {
|
||||
expect(res.status).toBe(401)
|
||||
expect(res.body.length).toBeGreaterThan(0)
|
||||
|
||||
.set("Authorization", `Basic ${base64Credentials}`)
|
||||
.then(async (res) => {
|
||||
try {
|
||||
testCode = res.body[0].claimcode;
|
||||
expect(testCode.split('-').length).toEqual(4);
|
||||
expect(res.status).toEqual(401);
|
||||
expect(res.body.length).toBeGreaterThan(0);
|
||||
|
||||
|
||||
const joinTest = {
|
||||
code: testCode,
|
||||
idc: testIdentity
|
||||
};
|
||||
|
||||
await request(_app)
|
||||
.post("/join")
|
||||
.send(joinTest)
|
||||
.then((res) => {
|
||||
expect(res.statusCode).toEqual(200);
|
||||
expect(res.body.status).toEqual("valid");
|
||||
});
|
||||
} catch (error) {
|
||||
console.error('Error in test: ', error);
|
||||
}
|
||||
})
|
||||
.catch((error) => {
|
||||
console.error('Error in request: ', error);
|
||||
});
|
||||
});
|
||||
});
|
||||
console.log(testIdentity);
|
||||
|
||||
describe("POST /join", () => {
|
||||
test("It should add a users identity to the rooms the claim code is associated with", async () => {
|
||||
test("It should return all rooms associated with the given identity", async () => {
|
||||
await request(_app)
|
||||
.post("/join")
|
||||
.send(joinTest)
|
||||
.then(res => {
|
||||
expect(res.statusCode).toBe(200)
|
||||
expect(res.body.status).toBe('valid')
|
||||
})
|
||||
})
|
||||
})
|
||||
.get(`/api/rooms/${testIdentity}`)
|
||||
.then((res) => {
|
||||
try {
|
||||
console.log(res.body);
|
||||
expect(res.statusCode).toEqual(200);
|
||||
} catch (error) {
|
||||
pp("GET /api/rooms/:idc - " + error, "error");
|
||||
}
|
||||
})
|
||||
.catch((error) => pp("GET /api/rooms/:idc - " + error, "error"));
|
||||
});
|
||||
});
|
||||
|
||||
9
tests/utils.ts
Normal file
9
tests/utils.ts
Normal file
@@ -0,0 +1,9 @@
|
||||
import { faker } from '@faker-js/faker';
|
||||
|
||||
export function randBigint(): bigint {
|
||||
return faker.number.bigInt();
|
||||
}
|
||||
|
||||
export function randomRoomName(min = 5, max = 20) {
|
||||
return faker.string.alphanumeric({ length: { min: min, max: max } });
|
||||
}
|
||||
Reference in New Issue
Block a user