2021-07-20 05:22:29 +03:00
|
|
|
const { test } = require('@jest/globals');
|
|
|
|
var request = require('supertest');
|
|
|
|
request = request('http://127.0.0.1:8080');
|
2021-08-13 02:49:40 +03:00
|
|
|
const WebSocket = require('ws');
|
|
|
|
const fs = require('fs');
|
2021-07-20 05:22:29 +03:00
|
|
|
|
|
|
|
const registerChat = require('./lib/chat').registerChat;
|
|
|
|
const sendChatMessage = require('./lib/chat').sendChatMessage;
|
2022-12-08 21:07:54 +03:00
|
|
|
const sendAdminRequest = require('./lib/admin').sendAdminRequest;
|
|
|
|
const sendAdminPayload = require('./lib/admin').sendAdminPayload;
|
|
|
|
const getAdminResponse = require('./lib/admin').getAdminResponse;
|
2023-01-05 01:09:51 +03:00
|
|
|
const randomNumber = require('./lib/rand').randomNumber;
|
2022-12-08 21:07:54 +03:00
|
|
|
|
|
|
|
const localIPAddressV4 = '127.0.0.1';
|
|
|
|
const localIPAddressV6 = '::1';
|
2022-03-07 07:34:49 +03:00
|
|
|
|
2021-07-20 05:22:29 +03:00
|
|
|
const testVisibilityMessage = {
|
2023-01-05 01:09:51 +03:00
|
|
|
body: 'message ' + randomNumber(100),
|
2021-08-13 02:49:40 +03:00
|
|
|
type: 'CHAT',
|
2021-07-20 05:22:29 +03:00
|
|
|
};
|
|
|
|
|
2021-08-13 02:49:40 +03:00
|
|
|
var userId;
|
|
|
|
var accessToken;
|
2022-12-08 21:07:54 +03:00
|
|
|
test('register a user', async (done) => {
|
2021-08-13 02:49:40 +03:00
|
|
|
const registration = await registerChat();
|
|
|
|
userId = registration.id;
|
|
|
|
accessToken = registration.accessToken;
|
|
|
|
done();
|
2021-07-20 05:22:29 +03:00
|
|
|
});
|
|
|
|
|
2022-12-08 21:07:54 +03:00
|
|
|
test('send a chat message', async (done) => {
|
2021-08-13 02:49:40 +03:00
|
|
|
sendChatMessage(testVisibilityMessage, accessToken, done);
|
2021-07-20 05:22:29 +03:00
|
|
|
});
|
|
|
|
|
2022-12-08 21:07:54 +03:00
|
|
|
test('set the user as moderator', async (done) => {
|
|
|
|
const res = await sendAdminPayload('chat/users/setmoderator', { userId: userId, isModerator: true });
|
2022-03-07 07:34:49 +03:00
|
|
|
done();
|
|
|
|
});
|
|
|
|
|
|
|
|
test('verify user is a moderator', async (done) => {
|
2022-12-08 21:07:54 +03:00
|
|
|
const response = await getAdminResponse('chat/users/moderators');
|
2022-03-07 07:34:49 +03:00
|
|
|
const tokenCheck = response.body.filter((user) => user.id === userId);
|
|
|
|
expect(tokenCheck).toHaveLength(1);
|
|
|
|
|
|
|
|
done();
|
|
|
|
});
|
|
|
|
|
|
|
|
test('verify user list is populated', async (done) => {
|
|
|
|
const ws = new WebSocket(
|
|
|
|
`ws://localhost:8080/ws?accessToken=${accessToken}`,
|
|
|
|
{
|
|
|
|
origin: 'http://localhost:8080',
|
|
|
|
}
|
|
|
|
);
|
|
|
|
|
|
|
|
ws.on('open', async function open() {
|
2022-12-08 21:07:54 +03:00
|
|
|
const response = await getAdminResponse('chat/clients');
|
2022-03-07 07:34:49 +03:00
|
|
|
|
|
|
|
expect(response.body.length).toBeGreaterThan(0);
|
|
|
|
|
|
|
|
// Optionally, if GeoIP is configured, check the location property.
|
|
|
|
if (fs.existsSync('../../../data/GeoLite2-City.mmdb')) {
|
|
|
|
expect(response.body[0].geo.regionName).toBe('Localhost');
|
|
|
|
}
|
|
|
|
|
|
|
|
ws.close();
|
|
|
|
});
|
|
|
|
|
|
|
|
ws.on('error', function incoming(data) {
|
|
|
|
console.error(data);
|
|
|
|
ws.close();
|
|
|
|
});
|
|
|
|
|
|
|
|
ws.on('close', function incoming(data) {
|
|
|
|
done();
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
2022-12-11 08:10:10 +03:00
|
|
|
test('disable a user by admin', async (done) => {
|
2021-11-11 23:43:21 +03:00
|
|
|
// To allow for visually being able to see the test hiding the
|
|
|
|
// message add a short delay.
|
|
|
|
await new Promise((r) => setTimeout(r, 1500));
|
|
|
|
|
2022-03-07 07:34:49 +03:00
|
|
|
const ws = new WebSocket(
|
|
|
|
`ws://localhost:8080/ws?accessToken=${accessToken}`,
|
|
|
|
{
|
|
|
|
origin: 'http://localhost:8080',
|
|
|
|
}
|
|
|
|
);
|
|
|
|
|
2022-12-08 21:07:54 +03:00
|
|
|
const res = await sendAdminPayload('chat/users/setenabled', { userId: userId, enabled: false });
|
2022-03-07 07:34:49 +03:00
|
|
|
|
|
|
|
await new Promise((r) => setTimeout(r, 1500));
|
2021-11-11 23:43:21 +03:00
|
|
|
done();
|
|
|
|
});
|
2021-07-20 05:22:29 +03:00
|
|
|
|
|
|
|
test('verify user is disabled', async (done) => {
|
2022-12-08 21:07:54 +03:00
|
|
|
const response = await getAdminResponse('chat/users/disabled');
|
2021-08-13 02:49:40 +03:00
|
|
|
const tokenCheck = response.body.filter((user) => user.id === userId);
|
|
|
|
expect(tokenCheck).toHaveLength(1);
|
|
|
|
done();
|
2021-07-20 05:22:29 +03:00
|
|
|
});
|
|
|
|
|
|
|
|
test('verify messages from user are hidden', async (done) => {
|
2022-12-08 21:07:54 +03:00
|
|
|
const response = await getAdminResponse('chat/messages');
|
2021-08-13 02:49:40 +03:00
|
|
|
const message = response.body.filter((obj) => {
|
|
|
|
return obj.user.id === userId;
|
|
|
|
});
|
2022-01-13 00:53:10 +03:00
|
|
|
expect(message[0].user.disabledAt).toBeTruthy();
|
2021-08-13 02:49:40 +03:00
|
|
|
done();
|
2021-07-20 05:22:29 +03:00
|
|
|
});
|
|
|
|
|
2022-12-11 08:10:10 +03:00
|
|
|
test('re-enable a user by admin', async (done) => {
|
2022-12-08 21:07:54 +03:00
|
|
|
const res = await sendAdminPayload('chat/users/setenabled', { userId: userId, enabled: true });
|
2021-08-13 02:49:40 +03:00
|
|
|
done();
|
2021-07-20 05:22:29 +03:00
|
|
|
});
|
|
|
|
|
|
|
|
test('verify user is enabled', async (done) => {
|
2022-12-08 21:07:54 +03:00
|
|
|
const response = await getAdminResponse('chat/users/disabled');
|
2021-08-13 02:49:40 +03:00
|
|
|
const tokenCheck = response.body.filter((user) => user.id === userId);
|
|
|
|
expect(tokenCheck).toHaveLength(0);
|
|
|
|
|
|
|
|
done();
|
|
|
|
});
|
|
|
|
|
2022-12-11 08:10:10 +03:00
|
|
|
test('ban an ip address by admin', async (done) => {
|
2022-12-08 21:07:54 +03:00
|
|
|
const resIPv4 = await sendAdminRequest('chat/users/ipbans/create', localIPAddressV4);
|
|
|
|
const resIPv6 = await sendAdminRequest('chat/users/ipbans/create', localIPAddressV6);
|
2021-11-03 05:27:41 +03:00
|
|
|
done();
|
|
|
|
});
|
|
|
|
|
2022-03-07 07:34:49 +03:00
|
|
|
test('verify IP address is blocked from the ban', async (done) => {
|
2022-12-08 21:07:54 +03:00
|
|
|
const response = await getAdminResponse('chat/users/ipbans');
|
2021-11-03 05:27:41 +03:00
|
|
|
|
2022-12-08 21:07:54 +03:00
|
|
|
expect(response.body).toHaveLength(2);
|
2022-12-07 01:29:05 +03:00
|
|
|
expect(onlyLocalIPAddress(response.body)).toBe(true);
|
2021-11-03 05:27:41 +03:00
|
|
|
done();
|
|
|
|
});
|
|
|
|
|
2022-03-07 07:34:49 +03:00
|
|
|
test('verify access is denied', async (done) => {
|
|
|
|
await request.get(`/api/chat?accessToken=${accessToken}`).expect(401);
|
|
|
|
done();
|
|
|
|
});
|
2021-08-13 02:49:40 +03:00
|
|
|
|
2022-12-11 08:10:10 +03:00
|
|
|
test('remove an ip address ban by admin', async (done) => {
|
2022-12-08 21:07:54 +03:00
|
|
|
const resIPv4 = await sendAdminRequest('chat/users/ipbans/remove', localIPAddressV4);
|
|
|
|
const resIPv6 = await sendAdminRequest('chat/users/ipbans/remove', localIPAddressV6);
|
2022-03-07 07:34:49 +03:00
|
|
|
done();
|
|
|
|
});
|
2021-08-13 02:49:40 +03:00
|
|
|
|
2022-03-07 07:34:49 +03:00
|
|
|
test('verify IP address is no longer banned', async (done) => {
|
2022-12-08 21:07:54 +03:00
|
|
|
const response = await getAdminResponse('chat/users/ipbans');
|
2021-08-13 02:49:40 +03:00
|
|
|
|
2022-03-07 07:34:49 +03:00
|
|
|
expect(response.body).toHaveLength(0);
|
|
|
|
done();
|
|
|
|
});
|
2021-08-13 02:49:40 +03:00
|
|
|
|
2022-12-08 21:07:54 +03:00
|
|
|
test('verify access is allowed after unban', async (done) => {
|
2022-03-07 07:34:49 +03:00
|
|
|
await request.get(`/api/chat?accessToken=${accessToken}`).expect(200);
|
|
|
|
done();
|
2021-07-20 05:22:29 +03:00
|
|
|
});
|
2022-12-07 01:29:05 +03:00
|
|
|
|
|
|
|
|
2022-12-08 21:07:54 +03:00
|
|
|
// This function expects the local address to be localIPAddressV4 & localIPAddressV6
|
2022-12-07 01:29:05 +03:00
|
|
|
function onlyLocalIPAddress(banInfo) {
|
|
|
|
for (let i = 0; i < banInfo.length; i++) {
|
2022-12-08 21:07:54 +03:00
|
|
|
if ((banInfo[i].ipAddress != localIPAddressV4) && (banInfo[i].ipAddress != localIPAddressV6)) {
|
2022-12-07 01:29:05 +03:00
|
|
|
return false
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return true
|
|
|
|
}
|