2021-07-20 04:22:29 +02:00
|
|
|
const { test } = require('@jest/globals');
|
|
|
|
var request = require('supertest');
|
|
|
|
request = request('http://127.0.0.1:8080');
|
2021-08-13 01:49:40 +02:00
|
|
|
const WebSocket = require('ws');
|
|
|
|
const fs = require('fs');
|
2021-07-20 04:22:29 +02:00
|
|
|
|
|
|
|
const registerChat = require('./lib/chat').registerChat;
|
|
|
|
const sendChatMessage = require('./lib/chat').sendChatMessage;
|
|
|
|
|
|
|
|
const testVisibilityMessage = {
|
2021-08-13 01:49:40 +02:00
|
|
|
body: 'message ' + Math.floor(Math.random() * 100),
|
|
|
|
type: 'CHAT',
|
2021-07-20 04:22:29 +02:00
|
|
|
};
|
|
|
|
|
2021-08-13 01:49:40 +02:00
|
|
|
var userId;
|
|
|
|
var accessToken;
|
2021-07-20 04:22:29 +02:00
|
|
|
test('can register a user', async (done) => {
|
2021-08-13 01:49:40 +02:00
|
|
|
const registration = await registerChat();
|
|
|
|
userId = registration.id;
|
|
|
|
accessToken = registration.accessToken;
|
|
|
|
done();
|
2021-07-20 04:22:29 +02:00
|
|
|
});
|
|
|
|
|
|
|
|
test('can send a chat message', async (done) => {
|
2021-08-13 01:49:40 +02:00
|
|
|
sendChatMessage(testVisibilityMessage, accessToken, done);
|
2021-07-20 04:22:29 +02:00
|
|
|
});
|
|
|
|
|
|
|
|
test('can disable a user', async (done) => {
|
|
|
|
// To allow for visually being able to see the test hiding the
|
|
|
|
// message add a short delay.
|
|
|
|
await new Promise((r) => setTimeout(r, 1500));
|
2021-08-13 01:49:40 +02:00
|
|
|
|
|
|
|
await request
|
|
|
|
.post('/api/admin/chat/users/setenabled')
|
|
|
|
.send({ userId: userId, enabled: false })
|
|
|
|
.auth('admin', 'abc123')
|
|
|
|
.expect(200);
|
2021-07-20 04:22:29 +02:00
|
|
|
done();
|
2021-08-13 01:49:40 +02:00
|
|
|
});
|
2021-07-20 04:22:29 +02:00
|
|
|
|
|
|
|
test('verify user is disabled', async (done) => {
|
2021-08-13 01:49:40 +02:00
|
|
|
const response = await request
|
|
|
|
.get('/api/admin/chat/users/disabled')
|
|
|
|
.auth('admin', 'abc123')
|
|
|
|
.expect(200);
|
|
|
|
const tokenCheck = response.body.filter((user) => user.id === userId);
|
|
|
|
expect(tokenCheck).toHaveLength(1);
|
|
|
|
done();
|
2021-07-20 04:22:29 +02:00
|
|
|
});
|
|
|
|
|
|
|
|
test('verify messages from user are hidden', async (done) => {
|
2021-08-13 01:49:40 +02:00
|
|
|
const response = await request
|
|
|
|
.get('/api/admin/chat/messages')
|
2021-07-20 04:22:29 +02:00
|
|
|
.auth('admin', 'abc123')
|
|
|
|
.expect(200);
|
2021-08-13 01:49:40 +02:00
|
|
|
const message = response.body.filter((obj) => {
|
|
|
|
return obj.user.id === userId;
|
|
|
|
});
|
|
|
|
expect(message[0].hiddenAt).toBeTruthy();
|
|
|
|
done();
|
2021-07-20 04:22:29 +02:00
|
|
|
});
|
|
|
|
|
|
|
|
test('can re-enable a user', async (done) => {
|
2021-08-13 01:49:40 +02:00
|
|
|
await request
|
|
|
|
.post('/api/admin/chat/users/setenabled')
|
|
|
|
.send({ userId: userId, enabled: true })
|
|
|
|
.auth('admin', 'abc123')
|
|
|
|
.expect(200);
|
|
|
|
done();
|
2021-07-20 04:22:29 +02:00
|
|
|
});
|
|
|
|
|
|
|
|
test('verify user is enabled', async (done) => {
|
2021-08-13 01:49:40 +02:00
|
|
|
const response = await request
|
|
|
|
.get('/api/admin/chat/users/disabled')
|
|
|
|
.auth('admin', 'abc123')
|
|
|
|
.expect(200);
|
|
|
|
const tokenCheck = response.body.filter((user) => user.id === userId);
|
|
|
|
expect(tokenCheck).toHaveLength(0);
|
|
|
|
|
|
|
|
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() {
|
|
|
|
const response = await request
|
|
|
|
.get('/api/admin/chat/clients')
|
|
|
|
.auth('admin', 'abc123')
|
|
|
|
.expect(200);
|
2021-07-20 04:22:29 +02:00
|
|
|
|
2021-08-13 01:49:40 +02: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');
|
|
|
|
} else {
|
|
|
|
console.warn('GeoIP Data is not supplied. Skipping test. See https://owncast.online/docs/viewers/');
|
|
|
|
}
|
|
|
|
|
|
|
|
ws.close();
|
|
|
|
});
|
|
|
|
|
|
|
|
ws.on('error', function incoming(data) {
|
|
|
|
console.error(data);
|
|
|
|
ws.close();
|
|
|
|
});
|
|
|
|
|
|
|
|
ws.on('close', function incoming(data) {
|
2021-07-20 04:22:29 +02:00
|
|
|
done();
|
2021-08-13 01:49:40 +02:00
|
|
|
});
|
|
|
|
|
2021-07-20 04:22:29 +02:00
|
|
|
});
|