From 7dae5e3db8b68da09a064f21aa837ccdcdb94b08 Mon Sep 17 00:00:00 2001 From: Richard Hansen Date: Tue, 16 Feb 2021 18:35:50 -0500 Subject: [PATCH] tests: Use the supertest agent from `common.js` for backend tests --- .github/workflows/backend-tests.yml | 6 +- .travis.yml | 4 +- src/bin/plugins/lib/backend-tests.yml | 3 +- .../backend/specs/api/characterEncoding.js | 18 +-- src/tests/backend/specs/api/chat.js | 20 +-- src/tests/backend/specs/api/importexport.js | 15 +- src/tests/backend/specs/api/instance.js | 11 +- src/tests/backend/specs/api/pad.js | 134 +++++++++--------- .../backend/specs/api/sessionsAndGroups.js | 71 +++++----- src/tests/frontend/travis/runnerBackend.sh | 46 ------ 10 files changed, 139 insertions(+), 189 deletions(-) delete mode 100755 src/tests/frontend/travis/runnerBackend.sh diff --git a/.github/workflows/backend-tests.yml b/.github/workflows/backend-tests.yml index 7ab58c410..a9c8c602e 100644 --- a/.github/workflows/backend-tests.yml +++ b/.github/workflows/backend-tests.yml @@ -30,9 +30,8 @@ jobs: - name: Install all dependencies and symlink for ep_etherpad-lite run: src/bin/installDeps.sh - # configures some settings and runs npm run test - name: Run the backend tests - run: src/tests/frontend/travis/runnerBackend.sh + run: cd src && npm test withplugins: # run on pushes to any branch @@ -84,6 +83,5 @@ jobs: - name: Install all dependencies and symlink for ep_etherpad-lite run: src/bin/installDeps.sh - # configures some settings and runs npm run test - name: Run the backend tests - run: src/tests/frontend/travis/runnerBackend.sh + run: cd src && npm test diff --git a/.travis.yml b/.travis.yml index 8517e2a89..99aece0b2 100644 --- a/.travis.yml +++ b/.travis.yml @@ -64,7 +64,7 @@ jobs: - "src/bin/installDeps.sh" - "cd src && npm install && cd -" script: - - "src/tests/frontend/travis/runnerBackend.sh" + - "cd src && npm test" - name: "Test the Dockerfile" install: - "cd src && npm install && cd -" @@ -107,7 +107,7 @@ jobs: - *install_plugins - "cd src && npm install && cd -" script: - - "src/tests/frontend/travis/runnerBackend.sh" + - "cd src && npm test" - name: "Test the Dockerfile" install: - "cd src && npm install && cd -" diff --git a/src/bin/plugins/lib/backend-tests.yml b/src/bin/plugins/lib/backend-tests.yml index f1d3a4af1..48ea969d3 100644 --- a/src/bin/plugins/lib/backend-tests.yml +++ b/src/bin/plugins/lib/backend-tests.yml @@ -43,9 +43,8 @@ jobs: cd node_modules/${{github.event.repository.name}} npm ci - # configures some settings and runs npm run test - name: Run the backend tests - run: src/tests/frontend/travis/runnerBackend.sh + run: cd src && npm test ##ETHERPAD_NPM_V=1 ## NPM configuration automatically created using src/bin/plugins/updateAllPluginsScript.sh diff --git a/src/tests/backend/specs/api/characterEncoding.js b/src/tests/backend/specs/api/characterEncoding.js index b1348af3b..3080425ec 100644 --- a/src/tests/backend/specs/api/characterEncoding.js +++ b/src/tests/backend/specs/api/characterEncoding.js @@ -8,10 +8,8 @@ const common = require('../../common'); const fs = require('fs'); -const settings = require('../../../../node/utils/Settings'); -const supertest = require('supertest'); -const api = supertest(`http://${settings.ip}:${settings.port}`); +let agent; const apiKey = common.apiKey; let apiVersion = 1; const testPadId = makeid(); @@ -19,10 +17,12 @@ const testPadId = makeid(); const endPoint = (point, version) => `/api/${version || apiVersion}/${point}?apikey=${apiKey}`; describe(__filename, function () { + before(async function () { agent = await common.init(); }); + describe('Connectivity For Character Encoding', function () { it('can connect', function (done) { this.timeout(250); - api.get('/api/') + agent.get('/api/') .expect('Content-Type', /json/) .expect(200, done); }); @@ -31,7 +31,7 @@ describe(__filename, function () { describe('API Versioning', function () { this.timeout(150); it('finds the version tag', function (done) { - api.get('/api/') + agent.get('/api/') .expect((res) => { apiVersion = res.body.currentVersion; if (!res.body.currentVersion) throw new Error('No version set in API'); @@ -47,7 +47,7 @@ describe(__filename, function () { // This is broken because Etherpad doesn't handle HTTP codes properly see #2343 // If your APIKey is password you deserve to fail all tests anyway const permErrorURL = `/api/${apiVersion}/createPad?apikey=password&padID=test`; - api.get(permErrorURL) + agent.get(permErrorURL) .expect(401, done); }); }); @@ -55,7 +55,7 @@ describe(__filename, function () { describe('createPad', function () { it('creates a new Pad', function (done) { this.timeout(150); - api.get(`${endPoint('createPad')}&padID=${testPadId}`) + agent.get(`${endPoint('createPad')}&padID=${testPadId}`) .expect((res) => { if (res.body.code !== 0) throw new Error('Unable to create new Pad'); }) @@ -68,7 +68,7 @@ describe(__filename, function () { it('Sets the HTML of a Pad attempting to weird utf8 encoded content', function (done) { this.timeout(1000); fs.readFile('tests/backend/specs/api/emojis.html', 'utf8', (err, html) => { - api.post(endPoint('setHTML')) + agent.post(endPoint('setHTML')) .send({ padID: testPadId, html, @@ -85,7 +85,7 @@ describe(__filename, function () { describe('getHTML', function () { it('get the HTML of Pad with emojis', function (done) { this.timeout(400); - api.get(`${endPoint('getHTML')}&padID=${testPadId}`) + agent.get(`${endPoint('getHTML')}&padID=${testPadId}`) .expect((res) => { if (res.body.data.html.indexOf('🇼') === -1) { throw new Error('Unable to get the HTML'); diff --git a/src/tests/backend/specs/api/chat.js b/src/tests/backend/specs/api/chat.js index 7af81f1ed..17f5bf4ef 100644 --- a/src/tests/backend/specs/api/chat.js +++ b/src/tests/backend/specs/api/chat.js @@ -1,10 +1,8 @@ 'use strict'; const common = require('../../common'); -const settings = require('../../../../node/utils/Settings'); -const supertest = require('supertest'); -const api = supertest(`http://${settings.ip}:${settings.port}`); +let agent; const apiKey = common.apiKey; let apiVersion = 1; let authorID = ''; @@ -14,9 +12,11 @@ const timestamp = Date.now(); const endPoint = (point) => `/api/${apiVersion}/${point}?apikey=${apiKey}`; describe(__filename, function () { + before(async function () { agent = await common.init(); }); + describe('API Versioning', function () { it('errors if can not connect', function (done) { - api.get('/api/') + agent.get('/api/') .expect((res) => { apiVersion = res.body.currentVersion; if (!res.body.currentVersion) throw new Error('No version set in API'); @@ -41,7 +41,7 @@ describe(__filename, function () { describe('createPad', function () { this.timeout(400); it('creates a new Pad', function (done) { - api.get(`${endPoint('createPad')}&padID=${padID}`) + agent.get(`${endPoint('createPad')}&padID=${padID}`) .expect((res) => { if (res.body.code !== 0) throw new Error('Unable to create new Pad'); }) @@ -53,7 +53,7 @@ describe(__filename, function () { describe('createAuthor', function () { this.timeout(100); it('Creates an author with a name set', function (done) { - api.get(endPoint('createAuthor')) + agent.get(endPoint('createAuthor')) .expect((res) => { if (res.body.code !== 0 || !res.body.data.authorID) { throw new Error('Unable to create author'); @@ -68,8 +68,8 @@ describe(__filename, function () { describe('appendChatMessage', function () { this.timeout(100); it('Adds a chat message to the pad', function (done) { - api.get(`${endPoint('appendChatMessage')}&padID=${padID}&text=blalblalbha` + - `&authorID=${authorID}&time=${timestamp}`) + agent.get(`${endPoint('appendChatMessage')}&padID=${padID}&text=blalblalbha` + + `&authorID=${authorID}&time=${timestamp}`) .expect((res) => { if (res.body.code !== 0) throw new Error('Unable to create chat message'); }) @@ -82,7 +82,7 @@ describe(__filename, function () { describe('getChatHead', function () { this.timeout(100); it('Gets the head of chat', function (done) { - api.get(`${endPoint('getChatHead')}&padID=${padID}`) + agent.get(`${endPoint('getChatHead')}&padID=${padID}`) .expect((res) => { if (res.body.data.chatHead !== 0) throw new Error('Chat Head Length is wrong'); @@ -96,7 +96,7 @@ describe(__filename, function () { describe('getChatHistory', function () { this.timeout(40); it('Gets Chat History of a Pad', function (done) { - api.get(`${endPoint('getChatHistory')}&padID=${padID}`) + agent.get(`${endPoint('getChatHistory')}&padID=${padID}`) .expect((res) => { if (res.body.data.messages.length !== 1) { throw new Error('Chat History Length is wrong'); diff --git a/src/tests/backend/specs/api/importexport.js b/src/tests/backend/specs/api/importexport.js index dd07e6e3a..6b2fb3ac9 100644 --- a/src/tests/backend/specs/api/importexport.js +++ b/src/tests/backend/specs/api/importexport.js @@ -7,10 +7,8 @@ */ const common = require('../../common'); -const settings = require('../../../container/loadSettings.js').loadSettings(); -const supertest = require('supertest'); -const api = supertest(`http://${settings.ip}:${settings.port}`); +let agent; const apiKey = common.apiKey; const apiVersion = 1; @@ -228,6 +226,8 @@ const testImports = { }; describe(__filename, function () { + before(async function () { agent = await common.init(); }); + Object.keys(testImports).forEach((testName) => { describe(testName, function () { const testPadId = makeid(); @@ -239,7 +239,7 @@ describe(__filename, function () { } it('createPad', function (done) { this.timeout(200); - api.get(`${endPoint('createPad')}&padID=${testPadId}`) + agent.get(`${endPoint('createPad')}&padID=${testPadId}`) .expect((res) => { if (res.body.code !== 0) throw new Error('Unable to create new Pad'); }) @@ -249,7 +249,8 @@ describe(__filename, function () { it('setHTML', function (done) { this.timeout(150); - api.get(`${endPoint('setHTML')}&padID=${testPadId}&html=${encodeURIComponent(test.input)}`) + agent.get(`${endPoint('setHTML')}&padID=${testPadId}` + + `&html=${encodeURIComponent(test.input)}`) .expect((res) => { if (res.body.code !== 0) throw new Error(`Error:${testName}`); }) @@ -259,7 +260,7 @@ describe(__filename, function () { it('getHTML', function (done) { this.timeout(150); - api.get(`${endPoint('getHTML')}&padID=${testPadId}`) + agent.get(`${endPoint('getHTML')}&padID=${testPadId}`) .expect((res) => { const gotHtml = res.body.data.html; if (gotHtml !== test.wantHTML) { @@ -283,7 +284,7 @@ describe(__filename, function () { it('getText', function (done) { this.timeout(100); - api.get(`${endPoint('getText')}&padID=${testPadId}`) + agent.get(`${endPoint('getText')}&padID=${testPadId}`) .expect((res) => { const gotText = res.body.data.text; if (gotText !== test.wantText) { diff --git a/src/tests/backend/specs/api/instance.js b/src/tests/backend/specs/api/instance.js index c45f5acd2..64a9ea5e0 100644 --- a/src/tests/backend/specs/api/instance.js +++ b/src/tests/backend/specs/api/instance.js @@ -6,21 +6,20 @@ * Section "GLOBAL FUNCTIONS" in src/node/db/API.js */ const common = require('../../common'); -const settings = require('../../../../node/utils/Settings'); -const supertest = require('supertest'); - -const api = supertest(`http://${settings.ip}:${settings.port}`); +let agent; const apiKey = common.apiKey; const apiVersion = '1.2.14'; const endPoint = (point, version) => `/api/${version || apiVersion}/${point}?apikey=${apiKey}`; describe(__filename, function () { + before(async function () { agent = await common.init(); }); + describe('Connectivity for instance-level API tests', function () { it('can connect', function (done) { this.timeout(150); - api.get('/api/') + agent.get('/api/') .expect('Content-Type', /json/) .expect(200, done); }); @@ -29,7 +28,7 @@ describe(__filename, function () { describe('getStats', function () { it('Gets the stats of a running instance', function (done) { this.timeout(100); - api.get(endPoint('getStats')) + agent.get(endPoint('getStats')) .expect((res) => { if (res.body.code !== 0) throw new Error('getStats() failed'); diff --git a/src/tests/backend/specs/api/pad.js b/src/tests/backend/specs/api/pad.js index 2b06973f4..bb0ecdd9a 100644 --- a/src/tests/backend/specs/api/pad.js +++ b/src/tests/backend/specs/api/pad.js @@ -10,11 +10,8 @@ const assert = require('assert').strict; const async = require('async'); const common = require('../../common'); -const settings = require('../../../../node/utils/Settings'); -const supertest = require('supertest'); - -const api = supertest(`http://${settings.ip}:${settings.port}`); +let agent; const apiKey = common.apiKey; let apiVersion = 1; const testPadId = makeid(); @@ -50,10 +47,12 @@ const ulSpaceHtml = '