mirror of
https://github.com/Discreetly/server.git
synced 2026-01-09 12:37:58 -05:00
init message testing
This commit is contained in:
@@ -7,7 +7,7 @@ import { faker } from '@faker-js/faker';
|
||||
import { MessageI } from 'discreetly-interfaces';
|
||||
import { Server as SocketIOServer } from 'socket.io';
|
||||
|
||||
export default function Mock(io: SocketIOServer) {
|
||||
export default function Mock(io: SocketIOServer): NodeJS.Timer {
|
||||
class randomMessagePicker {
|
||||
values: any[];
|
||||
weightSums: number[];
|
||||
@@ -38,7 +38,7 @@ export default function Mock(io: SocketIOServer) {
|
||||
const weights = [1, 3, 2, 8];
|
||||
const picker = new randomMessagePicker(values, weights);
|
||||
|
||||
setInterval(() => {
|
||||
return setInterval(() => {
|
||||
const message: MessageI = {
|
||||
id: faker.number.bigInt().toString(),
|
||||
roomId: BigInt(
|
||||
|
||||
@@ -191,6 +191,7 @@ export function initEndpoints(app: Express, adminAuth: RequestHandler) {
|
||||
bandadaAPIKey?: string;
|
||||
bandadaGroupId?: string;
|
||||
membershipType?: string;
|
||||
roomId?: string;
|
||||
}
|
||||
|
||||
/* ~~~~ ADMIN ENDPOINTS ~~~~ */
|
||||
@@ -232,6 +233,7 @@ export function initEndpoints(app: Express, adminAuth: RequestHandler) {
|
||||
const bandadaGroupId = roomMetadata.bandadaGroupId;
|
||||
const bandadaAPIKey = roomMetadata.bandadaAPIKey;
|
||||
const membershipType = roomMetadata.membershipType;
|
||||
const roomId = roomMetadata.roomId;
|
||||
createRoom(
|
||||
roomName,
|
||||
rateLimit,
|
||||
@@ -242,7 +244,8 @@ export function initEndpoints(app: Express, adminAuth: RequestHandler) {
|
||||
bandadaAddress,
|
||||
bandadaGroupId,
|
||||
bandadaAPIKey,
|
||||
membershipType
|
||||
membershipType,
|
||||
roomId
|
||||
)
|
||||
.then((result) => {
|
||||
if (result) {
|
||||
|
||||
@@ -13,8 +13,8 @@ import { websocketSetup as initWebsockets } from './websockets/index';
|
||||
import { initEndpoints } from './endpoints/index';
|
||||
import { generateRandomClaimCode } from 'discreetly-claimcodes';
|
||||
import { listEndpoints } from './endpoints/utils';
|
||||
import { instrument } from '@socket.io/admin-ui'
|
||||
import bcrypt from 'bcryptjs'
|
||||
import { instrument } from '@socket.io/admin-ui';
|
||||
import bcrypt from 'bcryptjs';
|
||||
|
||||
// TODO https://www.npmjs.com/package/winston
|
||||
|
||||
@@ -41,6 +41,8 @@ const adminAuth = basicAuth({
|
||||
}
|
||||
});
|
||||
|
||||
const intervalIds: NodeJS.Timer[] = [];
|
||||
|
||||
function initAppListeners(PORT) {
|
||||
const httpServer = http.createServer(app).listen(PORT, () => {
|
||||
pp(`Server is running at port ${PORT}`);
|
||||
@@ -78,12 +80,12 @@ if (!process.env.NODE_ENV || process.env.NODE_ENV === 'development') {
|
||||
credentials: true
|
||||
}
|
||||
});
|
||||
initWebsockets(io);
|
||||
intervalIds.push(initWebsockets(io));
|
||||
instrument(io, {
|
||||
auth: false,
|
||||
mode: 'development'
|
||||
});
|
||||
mock(io);
|
||||
intervalIds.push(mock(io));
|
||||
} else {
|
||||
const PORT = process.env.PORT;
|
||||
serverConfigStartup.port = PORT;
|
||||
@@ -95,7 +97,7 @@ if (!process.env.NODE_ENV || process.env.NODE_ENV === 'development') {
|
||||
credentials: true
|
||||
}
|
||||
});
|
||||
initWebsockets(io);
|
||||
intervalIds.push(initWebsockets(io));
|
||||
instrument(io, {
|
||||
auth: {
|
||||
type: 'basic',
|
||||
@@ -114,3 +116,4 @@ if (!process.env.NODE_ENV || process.env.NODE_ENV === 'development') {
|
||||
pp(serverConfigStartup, 'table');
|
||||
|
||||
export default _app;
|
||||
export { io, intervalIds };
|
||||
|
||||
@@ -7,7 +7,7 @@ import type { validateMessageResult } from '../data/messages';
|
||||
|
||||
const userCount: Record<string, number> = {};
|
||||
|
||||
export function websocketSetup(io: SocketIOServer) {
|
||||
export function websocketSetup(io: SocketIOServer): NodeJS.Timer {
|
||||
io.on('connection', (socket: Socket) => {
|
||||
pp('SocketIO: a user connected', 'debug');
|
||||
|
||||
@@ -47,7 +47,7 @@ export function websocketSetup(io: SocketIOServer) {
|
||||
io.to(roomID).emit('Members', userCount[roomID] ? userCount[roomID] : 0);
|
||||
});
|
||||
});
|
||||
setInterval(() => {
|
||||
return setInterval(() => {
|
||||
io.emit('TotalMembers', io.engine.clientsCount);
|
||||
}, 30000);
|
||||
}
|
||||
|
||||
@@ -1,5 +1,5 @@
|
||||
const request = require('supertest');
|
||||
import _app from '../src/server';
|
||||
import _app, { intervalIds } from '../src/server';
|
||||
import { genId } from 'discreetly-interfaces';
|
||||
import { serverConfig } from '../src/config/serverConfig';
|
||||
import { PrismaClient } from '@prisma/client';
|
||||
@@ -10,17 +10,6 @@ import { randBigint, randomRoomName } from './utils';
|
||||
process.env.DATABASE_URL = process.env.DATABASE_URL_TEST;
|
||||
process.env.PORT = '3001';
|
||||
|
||||
beforeAll(async () => {
|
||||
const prismaTest = new PrismaClient();
|
||||
await prismaTest.messages.deleteMany();
|
||||
await prismaTest.rooms.deleteMany();
|
||||
await prismaTest.claimCodes.deleteMany();
|
||||
});
|
||||
|
||||
afterAll(async () => {
|
||||
_app.close();
|
||||
});
|
||||
|
||||
const room = {
|
||||
roomName: randomRoomName(),
|
||||
rateLimit: 1000,
|
||||
@@ -30,13 +19,34 @@ const room = {
|
||||
type: 'PUBLIC_CHAT'
|
||||
};
|
||||
|
||||
const messageTestRoom = {
|
||||
roomName: randomRoomName(),
|
||||
rateLimit: 100,
|
||||
userMessageLimit: 2,
|
||||
numClaimCodes: 1,
|
||||
approxNumMockUsers: 10,
|
||||
type: 'PUBLIC_CHAT',
|
||||
id: '444'
|
||||
};
|
||||
|
||||
let roomByIdTest: string;
|
||||
let testCode = '';
|
||||
const testIdentity = randBigint();
|
||||
const username = 'admin';
|
||||
const password = process.env.PASSWORD;
|
||||
|
||||
describe('Endpoints should all work', () => {
|
||||
describe('Endpoints', () => {
|
||||
beforeAll(async () => {
|
||||
const prismaTest = new PrismaClient();
|
||||
await prismaTest.messages.deleteMany();
|
||||
await prismaTest.rooms.deleteMany();
|
||||
await prismaTest.claimCodes.deleteMany();
|
||||
});
|
||||
|
||||
afterAll(async () => {
|
||||
intervalIds.forEach((id) => clearInterval(id));
|
||||
_app.close();
|
||||
});
|
||||
test('It should respond with server info', async () => {
|
||||
await request(_app)
|
||||
.get('/')
|
||||
@@ -49,8 +59,6 @@ describe('Endpoints should all work', () => {
|
||||
});
|
||||
|
||||
test('It should add a new room to the database', async () => {
|
||||
const username = 'admin';
|
||||
const password = process.env.PASSWORD;
|
||||
const base64Credentials = Buffer.from(`${username}:${password}`).toString('base64');
|
||||
await request(_app)
|
||||
.post('/room/add')
|
||||
@@ -71,6 +79,26 @@ describe('Endpoints should all work', () => {
|
||||
.catch((error) => console.warn('POST /room/add - ' + error));
|
||||
});
|
||||
|
||||
test('It should add a new room with a custom id to the database', async () => {
|
||||
const base64Credentials = Buffer.from(`${username}:${password}`).toString('base64');
|
||||
await request(_app)
|
||||
.post('/room/add')
|
||||
.set('Authorization', `Basic ${base64Credentials}`)
|
||||
.send(messageTestRoom)
|
||||
|
||||
.then((res) => {
|
||||
try {
|
||||
expect(res.status).toEqual(200);
|
||||
const result = res.body;
|
||||
expect(res.body.message).toEqual('Room created successfully');
|
||||
expect(result.roomId).toBeDefined();
|
||||
} 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}`)
|
||||
@@ -79,15 +107,13 @@ describe('Endpoints should all work', () => {
|
||||
expect(res.status).toEqual(200);
|
||||
expect(res.body.name).toEqual(room.roomName);
|
||||
} catch (error) {
|
||||
pp('GET /api/room/:roomId - ' + error, 'error');
|
||||
console.error(`GET /api/room/:roomId - + ${error}`);
|
||||
}
|
||||
})
|
||||
.catch((error) => pp('GET /api/room/:roomId - ' + error, 'error'));
|
||||
});
|
||||
|
||||
test('It should return all rooms', async () => {
|
||||
const username = 'admin';
|
||||
const password = process.env.PASSWORD;
|
||||
const base64Credentials = Buffer.from(`${username}:${password}`).toString('base64');
|
||||
await request(_app)
|
||||
.get('/api/rooms')
|
||||
@@ -106,8 +132,6 @@ describe('Endpoints should all work', () => {
|
||||
});
|
||||
|
||||
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');
|
||||
await request(_app)
|
||||
.get('/logclaimcodes')
|
||||
@@ -187,3 +211,20 @@ describe('Endpoints should all work', () => {
|
||||
.catch((error) => pp('GET /api/messages/:roomId - ' + error, 'error'));
|
||||
});
|
||||
});
|
||||
|
||||
describe('Messages', () => {
|
||||
test('it should send and receive a message', async () => {
|
||||
const room = await request(_app)
|
||||
.get(`/api/room/444`)
|
||||
.then((res) => {
|
||||
try {
|
||||
return res.body;
|
||||
} catch (error) {
|
||||
console.error(`GET /api/room/:roomId - + ${error}`);
|
||||
}
|
||||
})
|
||||
.catch((error) => pp('GET /api/room/:roomId - ' + error, 'error'));
|
||||
console.log(room);
|
||||
expect(1).toBe(1);
|
||||
});
|
||||
});
|
||||
Reference in New Issue
Block a user