diff --git a/test/automated/api/admin.test.js b/test/automated/api/admin.test.js index bf8ee439d..1a073a7e7 100644 --- a/test/automated/api/admin.test.js +++ b/test/automated/api/admin.test.js @@ -1,10 +1,10 @@ var request = require('supertest'); request = request('http://127.0.0.1:8080'); - +const getAdminResponse = require('./lib/admin').getAdminResponse; test('correct number of log entries exist', (done) => { - request.get('/api/admin/logs').auth('admin', 'abc123').expect(200) + getAdminResponse('logs') .then((res) => { // expect(res.body).toHaveLength(8); done(); diff --git a/test/automated/api/chat.test.js b/test/automated/api/chat.test.js index 9e2283d8f..9830ae060 100644 --- a/test/automated/api/chat.test.js +++ b/test/automated/api/chat.test.js @@ -4,6 +4,7 @@ request = request('http://127.0.0.1:8080'); const registerChat = require('./lib/chat').registerChat; const sendChatMessage = require('./lib/chat').sendChatMessage; +const getAdminResponse = require('./lib/admin').getAdminResponse; var userDisplayName; const message = Math.floor(Math.random() * 100) + ' test 123'; @@ -13,7 +14,7 @@ const testMessage = { type: 'CHAT', }; -test('can send a chat message', async (done) => { +test('send a chat message', async (done) => { const registration = await registerChat(); const accessToken = registration.accessToken; userDisplayName = registration.displayName; @@ -21,11 +22,8 @@ test('can send a chat message', async (done) => { sendChatMessage(testMessage, accessToken, done); }); -test('can fetch chat messages', async (done) => { - const res = await request - .get('/api/admin/chat/messages') - .auth('admin', 'abc123') - .expect(200); +test('fetch chat messages by admin', async (done) => { + const res = await getAdminResponse('chat/messages'); const message = res.body.filter((m) => m.body === testMessage.body)[0]; if (!message) { @@ -41,7 +39,7 @@ test('can fetch chat messages', async (done) => { done(); }); -test('can derive display name from user header', async (done) => { +test('derive display name from user header', async (done) => { const res = await request .post('/api/chat/register') .set('X-Forwarded-User', 'test-user') @@ -51,7 +49,7 @@ test('can derive display name from user header', async (done) => { done(); }); -test('can overwrite user header derived display name with body', async (done) => { +test('overwrite user header derived display name with body', async (done) => { const res = await request .post('/api/chat/register') .send({ displayName: 'TestUserChat' }) diff --git a/test/automated/api/chatmoderation.test.js b/test/automated/api/chatmoderation.test.js index ffeedea86..05b37f525 100644 --- a/test/automated/api/chatmoderation.test.js +++ b/test/automated/api/chatmoderation.test.js @@ -5,6 +5,9 @@ const WebSocket = require('ws'); const registerChat = require('./lib/chat').registerChat; const sendChatMessage = require('./lib/chat').sendChatMessage; +const getAdminResponse = require('./lib/admin').getAdminResponse; +const sendAdminPayload = require('./lib/admin').sendAdminPayload; +const sendAdminRequest = require('./lib/admin').sendAdminRequest; const testVisibilityMessage = { body: 'message ' + Math.floor(Math.random() * 100), @@ -18,14 +21,14 @@ const establishedUserFailedChatMessage = { type: 'CHAT', }; -test('can send a chat message', async (done) => { +test('send a chat message', async (done) => { const registration = await registerChat(); const accessToken = registration.accessToken; sendChatMessage(testVisibilityMessage, accessToken, done); }); -test('verify we can make API call to mark message as hidden', async (done) => { +test('verify admin can make API call to mark message as hidden', async (done) => { const registration = await registerChat(); const accessToken = registration.accessToken; const ws = new WebSocket( @@ -48,27 +51,17 @@ test('verify we can make API call to mark message as hidden', async (done) => { }); }); - const res = await request - .get('/api/admin/chat/messages') - .auth('admin', 'abc123') - .expect(200); + const res = await getAdminResponse('chat/messages'); 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); + await sendAdminPayload('chat/messagevisibility', { idArray: [messageId], visible: false }); }); test('verify message has become hidden', async (done) => { await new Promise((r) => setTimeout(r, 2000)); - const res = await request - .get('/api/admin/chat/messages') - .expect(200) - .auth('admin', 'abc123'); + const res = await getAdminResponse('chat/messages'); const message = res.body.filter((obj) => { return obj.id === messageId; @@ -78,16 +71,12 @@ test('verify message has become hidden', async (done) => { done(); }); -test('can enable established chat user mode', async (done) => { - await request - .post('/api/admin/config/chat/establishedusermode') - .auth('admin', 'abc123') - .send({ value: true }) - .expect(200); +test('enable established chat user mode', async (done) => { + await sendAdminRequest('config/chat/establishedusermode', true); done(); }); -test('can send a message after established user mode is enabled', async (done) => { +test('send a message after established user mode is enabled', async (done) => { const registration = await registerChat(); const accessToken = registration.accessToken; @@ -95,10 +84,7 @@ test('can send a message after established user mode is enabled', async (done) = }); test('verify rejected message is not in the chat feed', async (done) => { - const res = await request - .get('/api/admin/chat/messages') - .expect(200) - .auth('admin', 'abc123'); + const res = await getAdminResponse('chat/messages'); const message = res.body.filter((obj) => { return obj.body === establishedUserFailedChatMessage.body; @@ -108,11 +94,7 @@ test('verify rejected message is not in the chat feed', async (done) => { done(); }); -test('can disable established chat user mode', async (done) => { - await request - .post('/api/admin/config/chat/establishedusermode') - .auth('admin', 'abc123') - .send({ value: false }) - .expect(200); +test('disable established chat user mode', async (done) => { + await sendAdminRequest('config/chat/establishedusermode', false); done(); }); diff --git a/test/automated/api/integrations.test.js b/test/automated/api/integrations.test.js index 8501be001..e285b64de 100644 --- a/test/automated/api/integrations.test.js +++ b/test/automated/api/integrations.test.js @@ -1,6 +1,9 @@ var request = require('supertest'); request = request('http://127.0.0.1:8080'); +const getAdminResponse = require('./lib/admin').getAdminResponse; +const sendAdminPayload = require('./lib/admin').sendAdminPayload; + var accessToken = ''; var webhookID; @@ -8,7 +11,7 @@ const webhook = 'https://super.duper.cool.thing.biz/owncast'; const events = ['CHAT']; test('create webhook', async (done) => { - const res = await sendIntegrationsChangePayload('webhooks/create', { + const res = await sendAdminPayload('webhooks/create', { url: webhook, events: events, }); @@ -20,10 +23,8 @@ test('create webhook', async (done) => { }); test('check webhooks', (done) => { - request - .get('/api/admin/webhooks') - .auth('admin', 'abc123') - .expect(200) + + getAdminResponse('webhooks') .then((res) => { expect(res.body).toHaveLength(1); expect(res.body[0].url).toBe(webhook); @@ -34,7 +35,7 @@ test('check webhooks', (done) => { }); test('delete webhook', async (done) => { - const res = await sendIntegrationsChangePayload('webhooks/delete', { + const res = await sendAdminPayload('webhooks/delete', { id: webhookID, }); expect(res.body.success).toBe(true); @@ -42,10 +43,7 @@ test('delete webhook', async (done) => { }); test('check that webhook was deleted', (done) => { - request - .get('/api/admin/webhooks') - .auth('admin', 'abc123') - .expect(200) + getAdminResponse('webhooks') .then((res) => { expect(res.body).toHaveLength(0); done(); @@ -59,7 +57,7 @@ test('create access token', async (done) => { 'CAN_SEND_MESSAGES', 'HAS_ADMIN_ACCESS', ]; - const res = await sendIntegrationsChangePayload('accesstokens/create', { + const res = await sendAdminPayload('accesstokens/create', { name: name, scopes: scopes, }); @@ -74,10 +72,7 @@ test('create access token', async (done) => { }); test('check access tokens', async (done) => { - const res = await request - .get('/api/admin/accesstokens') - .auth('admin', 'abc123') - .expect(200); + const res = await getAdminResponse('accesstokens'); const tokenCheck = res.body.filter( (token) => token.accessToken === accessToken ); @@ -146,7 +141,7 @@ test('test fetch chat history OPTIONS request', async (done) => { }); test('delete access token', async (done) => { - const res = await sendIntegrationsChangePayload('accesstokens/delete', { + const res = await sendAdminPayload('accesstokens/delete', { token: accessToken, }); expect(res.body.success).toBe(true); @@ -154,10 +149,7 @@ test('delete access token', async (done) => { }); test('check token delete was successful', async (done) => { - const res = await request - .get('/api/admin/accesstokens') - .auth('admin', 'abc123') - .expect(200); + const res = await getAdminResponse('accesstokens'); const tokenCheck = res.body.filter( (token) => token.accessToken === accessToken ); @@ -165,13 +157,3 @@ test('check token delete was successful', async (done) => { done(); }); -async function sendIntegrationsChangePayload(endpoint, payload) { - const url = '/api/admin/' + endpoint; - const res = await request - .post(url) - .auth('admin', 'abc123') - .send(payload) - .expect(200); - - return res; -}