2020-12-29 22:35:33 +01:00
|
|
|
const { test } = require('@jest/globals');
|
|
|
|
var request = require('supertest');
|
|
|
|
request = request('http://127.0.0.1:8080');
|
2021-11-11 22:17:00 +01:00
|
|
|
const WebSocket = require('ws');
|
2020-12-29 22:35:33 +01:00
|
|
|
|
2021-07-20 04:22:29 +02:00
|
|
|
const registerChat = require('./lib/chat').registerChat;
|
|
|
|
const sendChatMessage = require('./lib/chat').sendChatMessage;
|
2020-12-29 22:35:33 +01:00
|
|
|
|
|
|
|
const testVisibilityMessage = {
|
2022-06-20 17:46:53 +02:00
|
|
|
body: 'message ' + Math.floor(Math.random() * 100),
|
|
|
|
type: 'CHAT',
|
2020-12-29 22:35:33 +01:00
|
|
|
};
|
|
|
|
|
2022-03-07 08:26:24 +01:00
|
|
|
var messageId;
|
|
|
|
|
|
|
|
const establishedUserFailedChatMessage = {
|
2022-06-20 17:46:53 +02:00
|
|
|
body: 'this message should fail to send ' + Math.floor(Math.random() * 100),
|
|
|
|
type: 'CHAT',
|
2022-03-07 08:26:24 +01:00
|
|
|
};
|
|
|
|
|
2021-07-20 04:22:29 +02:00
|
|
|
test('can send a chat message', async (done) => {
|
2022-06-20 17:46:53 +02:00
|
|
|
const registration = await registerChat();
|
|
|
|
const accessToken = registration.accessToken;
|
2021-11-11 22:17:00 +01:00
|
|
|
|
2022-06-20 17:46:53 +02:00
|
|
|
sendChatMessage(testVisibilityMessage, accessToken, done);
|
2021-11-11 22:17:00 +01:00
|
|
|
});
|
2020-12-29 22:35:33 +01:00
|
|
|
|
|
|
|
test('verify we can make API call to mark message as hidden', async (done) => {
|
2022-06-20 17:46:53 +02:00
|
|
|
const registration = await registerChat();
|
|
|
|
const accessToken = registration.accessToken;
|
|
|
|
const ws = new WebSocket(
|
|
|
|
`ws://localhost:8080/ws?accessToken=${accessToken}`,
|
|
|
|
{
|
|
|
|
origin: 'http://localhost:8080',
|
|
|
|
}
|
|
|
|
);
|
|
|
|
|
|
|
|
// Verify the visibility change comes through the websocket
|
|
|
|
ws.on('message', async function incoming(message) {
|
|
|
|
const messages = message.split('\n');
|
|
|
|
messages.forEach(async function (message) {
|
|
|
|
const event = JSON.parse(message);
|
|
|
|
|
|
|
|
if (event.type === 'VISIBILITY-UPDATE') {
|
|
|
|
ws.close();
|
|
|
|
done();
|
|
|
|
}
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
const res = await request
|
|
|
|
.get('/api/admin/chat/messages')
|
|
|
|
.auth('admin', 'abc123')
|
|
|
|
.expect(200);
|
|
|
|
|
|
|
|
const message = res.body[0];
|
|
|
|
messageId = message.id;
|
|
|
|
await request
|
|
|
|
.post('/api/admin/chat/messagevisibility')
|
|
|
|
.auth('admin', 'abc123')
|
|
|
|
.send({ idArray: [messageId], visible: false })
|
|
|
|
.expect(200);
|
2020-12-29 22:35:33 +01:00
|
|
|
});
|
|
|
|
|
|
|
|
test('verify message has become hidden', async (done) => {
|
2022-06-20 17:46:53 +02:00
|
|
|
await new Promise((r) => setTimeout(r, 2000));
|
|
|
|
|
|
|
|
const res = await request
|
|
|
|
.get('/api/admin/chat/messages')
|
|
|
|
.expect(200)
|
|
|
|
.auth('admin', 'abc123');
|
|
|
|
|
|
|
|
const message = res.body.filter((obj) => {
|
|
|
|
return obj.id === messageId;
|
|
|
|
});
|
|
|
|
expect(message.length).toBe(1);
|
|
|
|
// expect(message[0].hiddenAt).toBeTruthy();
|
|
|
|
done();
|
2020-12-29 22:35:33 +01:00
|
|
|
});
|
2022-03-07 08:26:24 +01:00
|
|
|
|
|
|
|
test('can enable established chat user mode', async (done) => {
|
2022-06-20 17:46:53 +02:00
|
|
|
await request
|
|
|
|
.post('/api/admin/config/chat/establishedusermode')
|
|
|
|
.auth('admin', 'abc123')
|
|
|
|
.send({ value: true })
|
|
|
|
.expect(200);
|
|
|
|
done();
|
2022-03-07 08:26:24 +01:00
|
|
|
});
|
|
|
|
|
|
|
|
test('can send a message after established user mode is enabled', async (done) => {
|
2022-06-20 17:46:53 +02:00
|
|
|
const registration = await registerChat();
|
|
|
|
const accessToken = registration.accessToken;
|
2022-03-07 08:26:24 +01:00
|
|
|
|
2022-06-20 17:46:53 +02:00
|
|
|
sendChatMessage(establishedUserFailedChatMessage, accessToken, done);
|
2022-03-07 08:26:24 +01:00
|
|
|
});
|
|
|
|
|
|
|
|
test('verify rejected message is not in the chat feed', async (done) => {
|
2022-06-20 17:46:53 +02:00
|
|
|
const res = await request
|
|
|
|
.get('/api/admin/chat/messages')
|
|
|
|
.expect(200)
|
|
|
|
.auth('admin', 'abc123');
|
2022-03-07 08:26:24 +01:00
|
|
|
|
2022-06-20 17:46:53 +02:00
|
|
|
const message = res.body.filter((obj) => {
|
|
|
|
return obj.body === establishedUserFailedChatMessage.body;
|
|
|
|
});
|
2022-03-07 08:26:24 +01:00
|
|
|
|
2022-06-20 17:46:53 +02:00
|
|
|
expect(message.length).toBe(0);
|
|
|
|
done();
|
2022-03-07 08:26:24 +01:00
|
|
|
});
|
|
|
|
|
|
|
|
test('can disable established chat user mode', async (done) => {
|
2022-06-20 17:46:53 +02:00
|
|
|
await request
|
|
|
|
.post('/api/admin/config/chat/establishedusermode')
|
|
|
|
.auth('admin', 'abc123')
|
|
|
|
.send({ value: false })
|
|
|
|
.expect(200);
|
|
|
|
done();
|
2022-03-07 08:26:24 +01:00
|
|
|
});
|