From fd683f0a725c713183f1b7a2ed26061bc8adff5d Mon Sep 17 00:00:00 2001 From: Meisam <39205857+MFTabriz@users.noreply.github.com> Date: Thu, 8 Dec 2022 19:07:54 +0100 Subject: [PATCH] refactor chatuser api tests (#2416) * block and unblock ipv6 explicitly * refactor admin api tests * use sendAdminPayload() for chatuser tests * fix sendAdminRequests * add getAdminResponse() to api test lib/admin.js * some admin apis don't have response body * cleanup test/automated/api/chatusers.test.js * cleanup test/automated/api/chatusers.test.js use getAdminResponse() to access admin apis --- test/automated/api/chatusers.test.js | 91 +++++++-------------- test/automated/api/configmanagement.test.js | 85 ++++++++++--------- test/automated/api/federation.test.js | 14 ++-- test/automated/api/lib/admin.js | 31 +++---- 4 files changed, 89 insertions(+), 132 deletions(-) diff --git a/test/automated/api/chatusers.test.js b/test/automated/api/chatusers.test.js index d37f78f62..29c85007e 100644 --- a/test/automated/api/chatusers.test.js +++ b/test/automated/api/chatusers.test.js @@ -6,8 +6,13 @@ const fs = require('fs'); const registerChat = require('./lib/chat').registerChat; const sendChatMessage = require('./lib/chat').sendChatMessage; +const sendAdminRequest = require('./lib/admin').sendAdminRequest; +const sendAdminPayload = require('./lib/admin').sendAdminPayload; +const getAdminResponse = require('./lib/admin').getAdminResponse; -const localIPAddress = '127.0.0.1'; + +const localIPAddressV4 = '127.0.0.1'; +const localIPAddressV6 = '::1'; const testVisibilityMessage = { body: 'message ' + Math.floor(Math.random() * 100), @@ -16,31 +21,24 @@ const testVisibilityMessage = { var userId; var accessToken; -test('can register a user', async (done) => { +test('register a user', async (done) => { const registration = await registerChat(); userId = registration.id; accessToken = registration.accessToken; done(); }); -test('can send a chat message', async (done) => { +test('send a chat message', async (done) => { sendChatMessage(testVisibilityMessage, accessToken, done); }); -test('can set the user as moderator', async (done) => { - await request - .post('/api/admin/chat/users/setmoderator') - .send({ userId: userId, isModerator: true }) - .auth('admin', 'abc123') - .expect(200); +test('set the user as moderator', async (done) => { + const res = await sendAdminPayload('chat/users/setmoderator', { userId: userId, isModerator: true }); done(); }); test('verify user is a moderator', async (done) => { - const response = await request - .get('/api/admin/chat/users/moderators') - .auth('admin', 'abc123') - .expect(200); + const response = await getAdminResponse('chat/users/moderators'); const tokenCheck = response.body.filter((user) => user.id === userId); expect(tokenCheck).toHaveLength(1); @@ -56,10 +54,7 @@ test('verify user list is populated', async (done) => { ); ws.on('open', async function open() { - const response = await request - .get('/api/admin/chat/clients') - .auth('admin', 'abc123') - .expect(200); + const response = await getAdminResponse('chat/clients'); expect(response.body.length).toBeGreaterThan(0); @@ -81,7 +76,7 @@ test('verify user list is populated', async (done) => { }); }); -test('can disable a user', async (done) => { +test('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)); @@ -93,31 +88,21 @@ test('can disable a user', async (done) => { } ); - await request - .post('/api/admin/chat/users/setenabled') - .send({ userId: userId, enabled: false }) - .auth('admin', 'abc123') - .expect(200); + const res = await sendAdminPayload('chat/users/setenabled', { userId: userId, enabled: false }); await new Promise((r) => setTimeout(r, 1500)); done(); }); test('verify user is disabled', async (done) => { - const response = await request - .get('/api/admin/chat/users/disabled') - .auth('admin', 'abc123') - .expect(200); + const response = await getAdminResponse('chat/users/disabled'); const tokenCheck = response.body.filter((user) => user.id === userId); expect(tokenCheck).toHaveLength(1); done(); }); test('verify messages from user are hidden', async (done) => { - const response = await request - .get('/api/admin/chat/messages') - .auth('admin', 'abc123') - .expect(200); + const response = await getAdminResponse('chat/messages'); const message = response.body.filter((obj) => { return obj.user.id === userId; }); @@ -125,20 +110,13 @@ test('verify messages from user are hidden', async (done) => { done(); }); -test('can re-enable a user', async (done) => { - await request - .post('/api/admin/chat/users/setenabled') - .send({ userId: userId, enabled: true }) - .auth('admin', 'abc123') - .expect(200); +test('re-enable a user', async (done) => { + const res = await sendAdminPayload('chat/users/setenabled', { userId: userId, enabled: true }); done(); }); test('verify user is enabled', async (done) => { - const response = await request - .get('/api/admin/chat/users/disabled') - .auth('admin', 'abc123') - .expect(200); + const response = await getAdminResponse('chat/users/disabled'); const tokenCheck = response.body.filter((user) => user.id === userId); expect(tokenCheck).toHaveLength(0); @@ -146,20 +124,15 @@ test('verify user is enabled', async (done) => { }); test('ban an ip address', async (done) => { - await request - .post('/api/admin/chat/users/ipbans/create') - .send({ value: localIPAddress }) - .auth('admin', 'abc123') - .expect(200); + const resIPv4 = await sendAdminRequest('chat/users/ipbans/create', localIPAddressV4); + const resIPv6 = await sendAdminRequest('chat/users/ipbans/create', localIPAddressV6); done(); }); test('verify IP address is blocked from the ban', async (done) => { - const response = await request - .get(`/api/admin/chat/users/ipbans`) - .auth('admin', 'abc123') - .expect(200); + const response = await getAdminResponse('chat/users/ipbans'); + expect(response.body).toHaveLength(2); expect(onlyLocalIPAddress(response.body)).toBe(true); done(); }); @@ -170,34 +143,28 @@ test('verify access is denied', async (done) => { }); test('remove an ip address ban', async (done) => { - await request - .post('/api/admin/chat/users/ipbans/remove') - .send({ value: localIPAddress }) - .auth('admin', 'abc123') - .expect(200); + const resIPv4 = await sendAdminRequest('chat/users/ipbans/remove', localIPAddressV4); + const resIPv6 = await sendAdminRequest('chat/users/ipbans/remove', localIPAddressV6); done(); }); test('verify IP address is no longer banned', async (done) => { - const response = await request - .get(`/api/admin/chat/users/ipbans`) - .auth('admin', 'abc123') - .expect(200); + const response = await getAdminResponse('chat/users/ipbans'); expect(response.body).toHaveLength(0); done(); }); -test('verify access is again allowed', async (done) => { +test('verify access is allowed after unban', async (done) => { await request.get(`/api/chat?accessToken=${accessToken}`).expect(200); done(); }); -// This function expects the local address to be 127.0.0.1 & ::1 +// This function expects the local address to be localIPAddressV4 & localIPAddressV6 function onlyLocalIPAddress(banInfo) { for (let i = 0; i < banInfo.length; i++) { - if ((banInfo[i].ipAddress != "127.0.0.1") && (banInfo[i].ipAddress != "::1")) { + if ((banInfo[i].ipAddress != localIPAddressV4) && (banInfo[i].ipAddress != localIPAddressV6)) { return false } } diff --git a/test/automated/api/configmanagement.test.js b/test/automated/api/configmanagement.test.js index 0b7607d36..0b604e825 100644 --- a/test/automated/api/configmanagement.test.js +++ b/test/automated/api/configmanagement.test.js @@ -2,9 +2,8 @@ var request = require('supertest'); const Random = require('crypto-random'); -const sendConfigChangeRequest = require('./lib/admin').sendConfigChangeRequest; -const getAdminConfig = require('./lib/admin').getAdminConfig; -const getAdminStatus = require('./lib/admin').getAdminStatus; +const sendAdminRequest = require('./lib/admin').sendAdminRequest; +const getAdminResponse = require('./lib/admin').getAdminResponse; request = request('http://127.0.0.1:8080'); @@ -144,7 +143,7 @@ test('verify default config values', async (done) => { }); test('verify default admin configuration', async (done) => { - const res = await getAdminConfig(); + const res = await getAdminResponse('serverconfig'); expect(res.body.instanceDetails.name).toBe(defaultServerName); expect(res.body.instanceDetails.summary).toBe(defaultServerSummary); @@ -181,165 +180,165 @@ test('verify default admin configuration', async (done) => { }); test('set server name', async (done) => { - const res = await sendConfigChangeRequest('name', newServerName); + const res = await sendAdminRequest('config/name', newServerName); done(); }); test('set stream title', async (done) => { - const res = await sendConfigChangeRequest('streamtitle', newStreamTitle); + const res = await sendAdminRequest('config/streamtitle', newStreamTitle); done(); }); test('set server summary', async (done) => { - const res = await sendConfigChangeRequest('serversummary', newServerSummary); + const res = await sendAdminRequest('config/serversummary', newServerSummary); done(); }); test('set extra page content', async (done) => { - const res = await sendConfigChangeRequest('pagecontent', newPageContent); + const res = await sendAdminRequest('config/pagecontent', newPageContent); done(); }); test('set tags', async (done) => { - const res = await sendConfigChangeRequest('tags', newTags); + const res = await sendAdminRequest('config/tags', newTags); done(); }); test('set stream keys', async (done) => { - const res = await sendConfigChangeRequest('streamkeys', newStreamKeys); + const res = await sendAdminRequest('config/streamkeys', newStreamKeys); done(); }); test('set latency level', async (done) => { - const res = await sendConfigChangeRequest( - 'video/streamlatencylevel', + const res = await sendAdminRequest( + 'config/video/streamlatencylevel', latencyLevel ); done(); }); test('set video stream output variants', async (done) => { - const res = await sendConfigChangeRequest('video/streamoutputvariants', [ + const res = await sendAdminRequest('config/video/streamoutputvariants', [ streamOutputVariants, ]); done(); }); test('set social handles', async (done) => { - const res = await sendConfigChangeRequest('socialhandles', newSocialHandles); + const res = await sendAdminRequest('config/socialhandles', newSocialHandles); done(); }); test('set s3 configuration', async (done) => { - const res = await sendConfigChangeRequest('s3', newS3Config); + const res = await sendAdminRequest('config/s3', newS3Config); done(); }); test('set forbidden usernames', async (done) => { - const res = await sendConfigChangeRequest( - 'chat/forbiddenusernames', + const res = await sendAdminRequest( + 'config/chat/forbiddenusernames', newForbiddenUsernames ); done(); }); test('set server url', async (done) => { - const res = await sendConfigChangeRequest( - 'serverurl', + const res = await sendAdminRequest( + 'config/serverurl', newYPConfig.instanceUrl ); done(); }); test('set federation username', async (done) => { - const res = await sendConfigChangeRequest( - 'federation/username', + const res = await sendAdminRequest( + 'config/federation/username', newFederationConfig.username ); done(); }); test('set federation goLiveMessage', async (done) => { - const res = await sendConfigChangeRequest( - 'federation/livemessage', + const res = await sendAdminRequest( + 'config/federation/livemessage', newFederationConfig.goLiveMessage ); done(); }); test('toggle private federation mode', async (done) => { - const res = await sendConfigChangeRequest( - 'federation/private', + const res = await sendAdminRequest( + 'config/federation/private', newFederationConfig.isPrivate ); done(); }); test('toggle federation engagement', async (done) => { - const res = await sendConfigChangeRequest( - 'federation/showengagement', + const res = await sendAdminRequest( + 'config/federation/showengagement', newFederationConfig.showEngagement ); done(); }); test('set federation blocked domains', async (done) => { - const res = await sendConfigChangeRequest( - 'federation/blockdomains', + const res = await sendAdminRequest( + 'config/federation/blockdomains', newFederationConfig.blockedDomains ); done(); }); test('set offline message', async (done) => { - const res = await sendConfigChangeRequest( - 'offlinemessage', + const res = await sendAdminRequest( + 'config/offlinemessage', newOfflineMessage ); done(); }); test('set hide viewer count', async (done) => { - const res = await sendConfigChangeRequest( - 'hideviewercount', + const res = await sendAdminRequest( + 'config/hideviewercount', newHideViewerCount ); done(); }); test('set custom style values', async (done) => { - const res = await sendConfigChangeRequest('appearance', appearanceValues); + const res = await sendAdminRequest('config/appearance', appearanceValues); done(); }); test('enable directory', async (done) => { - const res = await sendConfigChangeRequest('directoryenabled', true); + const res = await sendAdminRequest('config/directoryenabled', true); done(); }); test('enable federation', async (done) => { - const res = await sendConfigChangeRequest( - 'federation/enable', + const res = await sendAdminRequest( + 'config/federation/enable', newFederationConfig.enabled ); done(); }); test('change admin password', async (done) => { - const res = await sendConfigChangeRequest('adminpass', newAdminPassword); + const res = await sendAdminRequest('config/adminpass', newAdminPassword); done(); }); test('verify admin password change', async (done) => { - const res = await getAdminConfig((adminPassword = newAdminPassword)); + const res = await getAdminResponse('serverconfig', (adminPassword = newAdminPassword)); expect(res.body.adminPassword).toBe(newAdminPassword); done(); }); test('reset admin password', async (done) => { - const res = await sendConfigChangeRequest( - 'adminpass', + const res = await sendAdminRequest( + 'config/adminpass', defaultAdminPassword, (adminPassword = newAdminPassword) ); @@ -360,7 +359,7 @@ test('verify updated config values', async (done) => { // Test that the raw video details being broadcasted are coming through test('verify admin stream details', async (done) => { - const res = await getAdminStatus(); + const res = await getAdminResponse('status'); expect(res.body.broadcaster.streamDetails.width).toBe(320); expect(res.body.broadcaster.streamDetails.height).toBe(180); @@ -373,7 +372,7 @@ test('verify admin stream details', async (done) => { }); test('verify updated admin configuration', async (done) => { - const res = await getAdminConfig(); + const res = await getAdminResponse('serverconfig'); expect(res.body.instanceDetails.name).toBe(newServerName); expect(res.body.instanceDetails.summary).toBe(newServerSummary); diff --git a/test/automated/api/federation.test.js b/test/automated/api/federation.test.js index 89d0fa877..243996562 100644 --- a/test/automated/api/federation.test.js +++ b/test/automated/api/federation.test.js @@ -1,7 +1,7 @@ var request = require('supertest'); const jsonfile = require('jsonfile'); const Ajv = require('ajv-draft-04'); -const sendConfigChangeRequest = require('./lib/admin').sendConfigChangeRequest; +const sendAdminRequest = require('./lib/admin').sendAdminRequest; request = request('http://127.0.0.1:8080'); @@ -12,7 +12,7 @@ const serverURL = 'owncast.server.test' const fediUsername = 'streamer' test('disable federation', async (done) => { - const res = await sendConfigChangeRequest('federation/enable', false); + const res = await sendAdminRequest('config/federation/enable', false); done(); }); @@ -57,15 +57,15 @@ test('verify responses of /federation/ when federation is disabled', async (done }); test('set required parameters and enable federation', async (done) => { - const res1 = await sendConfigChangeRequest( - 'serverurl', + const res1 = await sendAdminRequest( + 'config/serverurl', serverURL ); - const res2 = await sendConfigChangeRequest( - 'federation/username', + const res2 = await sendAdminRequest( + 'config/federation/username', fediUsername ); - const res3 = await sendConfigChangeRequest('federation/enable', true); + const res3 = await sendAdminRequest('config/federation/enable', true); done(); }); diff --git a/test/automated/api/lib/admin.js b/test/automated/api/lib/admin.js index 3e7146dde..c4a275256 100644 --- a/test/automated/api/lib/admin.js +++ b/test/automated/api/lib/admin.js @@ -3,30 +3,22 @@ request = request('http://127.0.0.1:8080'); const defaultAdminPassword = 'abc123'; -async function getAdminConfig(adminPassword = defaultAdminPassword) { +async function getAdminResponse(endpoint, adminPassword = defaultAdminPassword) { + const url = '/api/admin/' + endpoint; const res = request - .get('/api/admin/serverconfig') + .get(url) .auth('admin', adminPassword) .expect(200); return res; } -async function getAdminStatus(adminPassword = defaultAdminPassword) { - const res = request - .get('/api/admin/status') - .auth('admin', adminPassword) - .expect(200); - - return res; -} - -async function sendConfigChangeRequest( +async function sendAdminRequest( endpoint, value, adminPassword = defaultAdminPassword ) { - const url = '/api/admin/config/' + endpoint; + const url = '/api/admin/' + endpoint; const res = await request .post(url) .auth('admin', adminPassword) @@ -37,24 +29,23 @@ async function sendConfigChangeRequest( return res; } -async function sendConfigChangePayload( +async function sendAdminPayload( endpoint, payload, adminPassword = defaultAdminPassword ) { - const url = '/api/admin/config/' + endpoint; + const url = '/api/admin/' + endpoint; const res = await request .post(url) .auth('admin', adminPassword) .send(payload) .expect(200); - expect(res.body.success).toBe(true); + expect(res.body.success).not.toBe(false); return res; } -module.exports.getAdminConfig = getAdminConfig; -module.exports.getAdminStatus = getAdminStatus; -module.exports.sendConfigChangeRequest = sendConfigChangeRequest; -module.exports.sendConfigChangePayload = sendConfigChangePayload; +module.exports.getAdminResponse = getAdminResponse; +module.exports.sendAdminRequest = sendAdminRequest; +module.exports.sendAdminPayload = sendAdminPayload;