mirror of
https://github.com/Discreetly/server.git
synced 2026-01-09 12:57:58 -05:00
refactor(tests) refactoring tests for new endpoints
This commit is contained in:
@@ -1,7 +1,7 @@
|
|||||||
module.exports = {
|
module.exports = {
|
||||||
preset: 'ts-jest',
|
preset: 'ts-jest',
|
||||||
testEnvironment: 'node',
|
testEnvironment: 'node',
|
||||||
testTimeout: 50000,
|
testTimeout: 10000,
|
||||||
setupFiles: ["<rootDir>/tests/setEnvVars.ts"],
|
setupFiles: ["<rootDir>/tests/setEnvVars.ts"],
|
||||||
"transform": {
|
"transform": {
|
||||||
"^.+\\.test.ts$": ["ts-jest", { tsconfig: "./tsconfig.tests.json" }]
|
"^.+\\.test.ts$": ["ts-jest", { tsconfig: "./tsconfig.tests.json" }]
|
||||||
|
|||||||
@@ -12,8 +12,8 @@ import { createSystemMessages } from '../../data/db';
|
|||||||
const prisma = new PrismaClient();
|
const prisma = new PrismaClient();
|
||||||
const router = express.Router();
|
const router = express.Router();
|
||||||
|
|
||||||
const adminPassword = process.env.ADMIN_PASSWORD
|
const adminPassword = process.env.PASSWORD
|
||||||
? process.env.ADMIN_PASSWORD
|
? process.env.PASSWORD
|
||||||
: 'password';
|
: 'password';
|
||||||
|
|
||||||
const adminAuth = basicAuth({
|
const adminAuth = basicAuth({
|
||||||
|
|||||||
@@ -89,11 +89,11 @@ router.get('/:id', limiter, (req, res) => {
|
|||||||
* @returns {void}
|
* @returns {void}
|
||||||
*/
|
*/
|
||||||
router.get(
|
router.get(
|
||||||
'/:idc',
|
'/idc/:idc',
|
||||||
limiter,
|
limiter,
|
||||||
asyncHandler(async (req: Request, res: Response) => {
|
asyncHandler(async (req: Request, res: Response) => {
|
||||||
const isValid = await verifyIdentityProof(req.body as IDCProof);
|
const isValid = await verifyIdentityProof(req.body as IDCProof);
|
||||||
|
console.log('VALID', isValid);
|
||||||
if (isValid) {
|
if (isValid) {
|
||||||
try {
|
try {
|
||||||
res.status(200).json(await findRoomsByIdentity(req.params.idc));
|
res.status(200).json(await findRoomsByIdentity(req.params.idc));
|
||||||
@@ -101,6 +101,8 @@ router.get(
|
|||||||
console.error(error);
|
console.error(error);
|
||||||
res.status(500).json({ error: 'Internal Server Error' });
|
res.status(500).json({ error: 'Internal Server Error' });
|
||||||
}
|
}
|
||||||
|
} else {
|
||||||
|
res.status(400).json({ error: 'Invalid Proof' });
|
||||||
}
|
}
|
||||||
})
|
})
|
||||||
);
|
);
|
||||||
|
|||||||
@@ -1,10 +1,10 @@
|
|||||||
const request = require('supertest');
|
const request = require('supertest');
|
||||||
import _app, { intervalIds } from '../src/server';
|
import _app, { intervalIds } from '../src/server';
|
||||||
import { RoomI, genId } from 'discreetly-interfaces';
|
import { RoomI } from 'discreetly-interfaces';
|
||||||
import { serverConfig } from '../src/config/serverConfig';
|
import { serverConfig } from '../src/config/serverConfig';
|
||||||
import { PrismaClient } from '@prisma/client';
|
import { PrismaClient } from '@prisma/client';
|
||||||
import { beforeAll, beforeEach, afterAll, describe, expect, test } from '@jest/globals';
|
import { beforeAll, afterAll, describe, expect, test } from '@jest/globals';
|
||||||
import { randBigint, randomRoomName } from './utils';
|
import { randomRoomName } from './utils';
|
||||||
import { generateIdentityProof } from '../src/crypto/idcVerifier/verifier';
|
import { generateIdentityProof } from '../src/crypto/idcVerifier/verifier';
|
||||||
import { Identity } from '@semaphore-protocol/identity';
|
import { Identity } from '@semaphore-protocol/identity';
|
||||||
|
|
||||||
@@ -14,9 +14,6 @@ process.env.PORT = '3001';
|
|||||||
|
|
||||||
const CUSTOM_ID = '444';
|
const CUSTOM_ID = '444';
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
const room = {
|
const room = {
|
||||||
roomName: randomRoomName(),
|
roomName: randomRoomName(),
|
||||||
rateLimit: 1000,
|
rateLimit: 1000,
|
||||||
@@ -90,7 +87,7 @@ describe('Endpoints', () => {
|
|||||||
test('It should create claimCode for the new room', async () => {
|
test('It should create claimCode for the new room', async () => {
|
||||||
const base64Credentials = Buffer.from(`${username}:${password}`).toString('base64');
|
const base64Credentials = Buffer.from(`${username}:${password}`).toString('base64');
|
||||||
await request(_app)
|
await request(_app)
|
||||||
.post(`/api/addcode`)
|
.post(`/admin/addcode`)
|
||||||
.set('Authorization', `Basic ${base64Credentials}`)
|
.set('Authorization', `Basic ${base64Credentials}`)
|
||||||
.send({ numCodes: 1, rooms: [roomByIdTest], all: false, expiresAt: 0, usesLeft: -1 })
|
.send({ numCodes: 1, rooms: [roomByIdTest], all: false, expiresAt: 0, usesLeft: -1 })
|
||||||
.then((res) => {
|
.then((res) => {
|
||||||
@@ -147,7 +144,7 @@ describe('Endpoints', () => {
|
|||||||
|
|
||||||
test('It should return the room with the given id', async () => {
|
test('It should return the room with the given id', async () => {
|
||||||
await request(_app)
|
await request(_app)
|
||||||
.get(`/api/room/${roomByIdTest}`)
|
.get(`/room/${roomByIdTest}`)
|
||||||
.then((res) => {
|
.then((res) => {
|
||||||
try {
|
try {
|
||||||
expect(res.status).toEqual(200);
|
expect(res.status).toEqual(200);
|
||||||
@@ -161,7 +158,7 @@ describe('Endpoints', () => {
|
|||||||
|
|
||||||
test('It should return the room with the given custom id', async () => {
|
test('It should return the room with the given custom id', async () => {
|
||||||
await request(_app)
|
await request(_app)
|
||||||
.get(`/api/room/${CUSTOM_ID}`)
|
.get(`/room/${CUSTOM_ID}`)
|
||||||
.then((res) => {
|
.then((res) => {
|
||||||
try {
|
try {
|
||||||
expect(res.status).toEqual(200);
|
expect(res.status).toEqual(200);
|
||||||
@@ -176,7 +173,7 @@ describe('Endpoints', () => {
|
|||||||
test('It should return all rooms', async () => {
|
test('It should return all rooms', async () => {
|
||||||
const base64Credentials = Buffer.from(`${username}:${password}`).toString('base64');
|
const base64Credentials = Buffer.from(`${username}:${password}`).toString('base64');
|
||||||
await request(_app)
|
await request(_app)
|
||||||
.get('/api/rooms')
|
.get('/admin/rooms')
|
||||||
|
|
||||||
.set('Authorization', `Basic ${base64Credentials}`)
|
.set('Authorization', `Basic ${base64Credentials}`)
|
||||||
.then((res) => {
|
.then((res) => {
|
||||||
@@ -194,7 +191,7 @@ describe('Endpoints', () => {
|
|||||||
test("It should return all claim codes and add a user's identity to the rooms the claim code is associated with", async () => {
|
test("It should return all claim codes and add a user's identity to the rooms the claim code is associated with", async () => {
|
||||||
const base64Credentials = Buffer.from(`${username}:${password}`).toString('base64');
|
const base64Credentials = Buffer.from(`${username}:${password}`).toString('base64');
|
||||||
await request(_app)
|
await request(_app)
|
||||||
.get('/logclaimcodes')
|
.get('/admin/logclaimcodes')
|
||||||
|
|
||||||
.set('Authorization', `Basic ${base64Credentials}`)
|
.set('Authorization', `Basic ${base64Credentials}`)
|
||||||
.then(async (res) => {
|
.then(async (res) => {
|
||||||
@@ -208,7 +205,7 @@ describe('Endpoints', () => {
|
|||||||
};
|
};
|
||||||
|
|
||||||
await request(_app)
|
await request(_app)
|
||||||
.post('/join')
|
.post('/gateway/code/join')
|
||||||
.send(joinTest)
|
.send(joinTest)
|
||||||
.then((res) => {
|
.then((res) => {
|
||||||
console.log(res.body);
|
console.log(res.body);
|
||||||
@@ -226,9 +223,8 @@ describe('Endpoints', () => {
|
|||||||
|
|
||||||
test('It should return all rooms associated with the given identity', async () => {
|
test('It should return all rooms associated with the given identity', async () => {
|
||||||
let proof = await generateIdentityProof(testIdentity, BigInt(Date.now()))
|
let proof = await generateIdentityProof(testIdentity, BigInt(Date.now()))
|
||||||
console.log("PROOF", proof);
|
|
||||||
await request(_app)
|
await request(_app)
|
||||||
.get(`/api/rooms/${testIdentity.getCommitment().toString()}`)
|
.get(`/room/idc/${testIdentity.getCommitment().toString()}`)
|
||||||
.send(proof)
|
.send(proof)
|
||||||
.then((res) => {
|
.then((res) => {
|
||||||
try {
|
try {
|
||||||
@@ -261,7 +257,7 @@ describe('Endpoints', () => {
|
|||||||
|
|
||||||
test('It should return the messages for a given room', async () => {
|
test('It should return the messages for a given room', async () => {
|
||||||
await request(_app)
|
await request(_app)
|
||||||
.get(`/api/room/${roomByIdTest}/messages`)
|
.get(`/room/${roomByIdTest}/messages`)
|
||||||
.then((res) => {
|
.then((res) => {
|
||||||
try {
|
try {
|
||||||
expect(res.statusCode).toEqual(200);
|
expect(res.statusCode).toEqual(200);
|
||||||
@@ -278,7 +274,7 @@ describe('Endpoints', () => {
|
|||||||
|
|
||||||
test('It should send and receive a message', async () => {
|
test('It should send and receive a message', async () => {
|
||||||
await request(_app)
|
await request(_app)
|
||||||
.get(`/api/room/${CUSTOM_ID}`)
|
.get(`/room/${CUSTOM_ID}`)
|
||||||
.then((res) => {
|
.then((res) => {
|
||||||
try {
|
try {
|
||||||
testRoom = res.body as RoomI;
|
testRoom = res.body as RoomI;
|
||||||
|
|||||||
Reference in New Issue
Block a user