From 3d2f77f75d59fc228cc0f15f8830bad5dbb35e36 Mon Sep 17 00:00:00 2001 From: Richard Hansen Date: Tue, 16 Feb 2021 18:25:15 -0500 Subject: [PATCH] lint: Fix ESLint errors in backend tests --- .../backend/specs/api/characterEncoding.js | 7 +- src/tests/backend/specs/api/chat.js | 19 +++-- src/tests/backend/specs/api/importexport.js | 8 +- src/tests/backend/specs/api/instance.js | 26 +++--- src/tests/backend/specs/api/pad.js | 83 +++++++++---------- .../backend/specs/api/sessionsAndGroups.js | 9 +- src/tests/backend/specs/caching_middleware.js | 5 +- 7 files changed, 79 insertions(+), 78 deletions(-) diff --git a/src/tests/backend/specs/api/characterEncoding.js b/src/tests/backend/specs/api/characterEncoding.js index 00183e12a..b1348af3b 100644 --- a/src/tests/backend/specs/api/characterEncoding.js +++ b/src/tests/backend/specs/api/characterEncoding.js @@ -16,6 +16,8 @@ const apiKey = common.apiKey; let apiVersion = 1; const testPadId = makeid(); +const endPoint = (point, version) => `/api/${version || apiVersion}/${point}?apikey=${apiKey}`; + describe(__filename, function () { describe('Connectivity For Character Encoding', function () { it('can connect', function (done) { @@ -101,11 +103,6 @@ describe(__filename, function () { */ -var endPoint = function (point, version) { - version = version || apiVersion; - return `/api/${version}/${point}?apikey=${apiKey}`; -}; - function makeid() { let text = ''; const possible = 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789'; diff --git a/src/tests/backend/specs/api/chat.js b/src/tests/backend/specs/api/chat.js index 570a8aaaa..7af81f1ed 100644 --- a/src/tests/backend/specs/api/chat.js +++ b/src/tests/backend/specs/api/chat.js @@ -1,3 +1,5 @@ +'use strict'; + const common = require('../../common'); const settings = require('../../../../node/utils/Settings'); const supertest = require('supertest'); @@ -9,6 +11,8 @@ let authorID = ''; const padID = makeid(); const timestamp = Date.now(); +const endPoint = (point) => `/api/${apiVersion}/${point}?apikey=${apiKey}`; + describe(__filename, function () { describe('API Versioning', function () { it('errors if can not connect', function (done) { @@ -51,7 +55,9 @@ describe(__filename, function () { it('Creates an author with a name set', function (done) { api.get(endPoint('createAuthor')) .expect((res) => { - if (res.body.code !== 0 || !res.body.data.authorID) throw new Error('Unable to create author'); + if (res.body.code !== 0 || !res.body.data.authorID) { + throw new Error('Unable to create author'); + } authorID = res.body.data.authorID; // we will be this author for the rest of the tests }) .expect('Content-Type', /json/) @@ -62,7 +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}`) + api.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'); }) @@ -91,7 +98,9 @@ describe(__filename, function () { it('Gets Chat History of a Pad', function (done) { api.get(`${endPoint('getChatHistory')}&padID=${padID}`) .expect((res) => { - if (res.body.data.messages.length !== 1) throw new Error('Chat History Length is wrong'); + if (res.body.data.messages.length !== 1) { + throw new Error('Chat History Length is wrong'); + } if (res.body.code !== 0) throw new Error('Unable to get chat history'); }) .expect('Content-Type', /json/) @@ -100,10 +109,6 @@ describe(__filename, function () { }); }); -var endPoint = function (point) { - return `/api/${apiVersion}/${point}?apikey=${apiKey}`; -}; - function makeid() { let text = ''; const possible = 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789'; diff --git a/src/tests/backend/specs/api/importexport.js b/src/tests/backend/specs/api/importexport.js index 960f019bd..dd07e6e3a 100644 --- a/src/tests/backend/specs/api/importexport.js +++ b/src/tests/backend/specs/api/importexport.js @@ -14,6 +14,8 @@ const api = supertest(`http://${settings.ip}:${settings.port}`); const apiKey = common.apiKey; const apiVersion = 1; +const endPoint = (point, version) => `/api/${version || apiVersion}/${point}?apikey=${apiKey}`; + const testImports = { 'malformed': { input: '
  • wtf', @@ -306,12 +308,6 @@ describe(__filename, function () { }); }); - -function endPoint(point, version) { - version = version || apiVersion; - return `/api/${version}/${point}?apikey=${apiKey}`; -} - function makeid() { let text = ''; const possible = 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789'; diff --git a/src/tests/backend/specs/api/instance.js b/src/tests/backend/specs/api/instance.js index ab465033e..c45f5acd2 100644 --- a/src/tests/backend/specs/api/instance.js +++ b/src/tests/backend/specs/api/instance.js @@ -1,3 +1,5 @@ +'use strict'; + /* * Tests for the instance-level APIs * @@ -12,6 +14,8 @@ const api = supertest(`http://${settings.ip}:${settings.port}`); const apiKey = common.apiKey; const apiVersion = '1.2.14'; +const endPoint = (point, version) => `/api/${version || apiVersion}/${point}?apikey=${apiKey}`; + describe(__filename, function () { describe('Connectivity for instance-level API tests', function () { it('can connect', function (done) { @@ -29,16 +33,21 @@ describe(__filename, function () { .expect((res) => { if (res.body.code !== 0) throw new Error('getStats() failed'); - if (!(('totalPads' in res.body.data) && (typeof res.body.data.totalPads === 'number'))) { - throw new Error(`Response to getStats() does not contain field totalPads, or it's not a number: ${JSON.stringify(res.body.data)}`); + if (!('totalPads' in res.body.data && typeof res.body.data.totalPads === 'number')) { + throw new Error('Response to getStats() does not contain field totalPads, or ' + + `it's not a number: ${JSON.stringify(res.body.data)}`); } - if (!(('totalSessions' in res.body.data) && (typeof res.body.data.totalSessions === 'number'))) { - throw new Error(`Response to getStats() does not contain field totalSessions, or it's not a number: ${JSON.stringify(res.body.data)}`); + if (!('totalSessions' in res.body.data && + typeof res.body.data.totalSessions === 'number')) { + throw new Error('Response to getStats() does not contain field totalSessions, or ' + + `it's not a number: ${JSON.stringify(res.body.data)}`); } - if (!(('totalActivePads' in res.body.data) && (typeof res.body.data.totalActivePads === 'number'))) { - throw new Error(`Response to getStats() does not contain field totalActivePads, or it's not a number: ${JSON.stringify(res.body.data)}`); + if (!('totalActivePads' in res.body.data && + typeof res.body.data.totalActivePads === 'number')) { + throw new Error('Response to getStats() does not contain field totalActivePads, or ' + + `it's not a number: ${JSON.stringify(res.body.data)}`); } }) .expect('Content-Type', /json/) @@ -46,8 +55,3 @@ describe(__filename, function () { }); }); }); - -var endPoint = function (point, version) { - version = version || apiVersion; - return `/api/${version}/${point}?apikey=${apiKey}`; -}; diff --git a/src/tests/backend/specs/api/pad.js b/src/tests/backend/specs/api/pad.js index c94ad810d..2b06973f4 100644 --- a/src/tests/backend/specs/api/pad.js +++ b/src/tests/backend/specs/api/pad.js @@ -1,3 +1,5 @@ +'use strict'; + /* * ACHTUNG: there is a copied & modified version of this file in * /src/tests/container/specs/api/pad.js @@ -5,6 +7,7 @@ * TODO: unify those two files, and merge in a single one. */ +const assert = require('assert').strict; const async = require('async'); const common = require('../../common'); const settings = require('../../../../node/utils/Settings'); @@ -18,6 +21,8 @@ const testPadId = makeid(); let lastEdited = ''; const text = generateLongText(); +const endPoint = (point, version) => `/api/${version || apiVersion}/${point}?apikey=${apiKey}`; + /* * Html document with nested lists of different types, to test its import and * verify it is exported back correctly @@ -160,7 +165,9 @@ describe(__filename, function () { api.get(`${endPoint('getSavedRevisionsCount')}&padID=${testPadId}`) .expect((res) => { if (res.body.code !== 0) throw new Error('Unable to get Saved Revisions Count'); - if (res.body.data.savedRevisions !== 0) throw new Error('Incorrect Saved Revisions Count'); + if (res.body.data.savedRevisions !== 0) { + throw new Error('Incorrect Saved Revisions Count'); + } }) .expect('Content-Type', /json/) .expect(200, done); @@ -173,7 +180,7 @@ describe(__filename, function () { api.get(`${endPoint('listSavedRevisions')}&padID=${testPadId}`) .expect((res) => { if (res.body.code !== 0) throw new Error('Unable to get Saved Revisions List'); - if (!res.body.data.savedRevisions.equals([])) throw new Error('Incorrect Saved Revisions List'); + assert.deepEqual(res.body.data.savedRevisions, []); }) .expect('Content-Type', /json/) .expect(200, done); @@ -326,7 +333,9 @@ describe(__filename, function () { api.get(`${endPoint('getSavedRevisionsCount')}&padID=${testPadId}`) .expect((res) => { if (res.body.code !== 0) throw new Error('Unable to get Saved Revisions Count'); - if (res.body.data.savedRevisions !== 1) throw new Error('Incorrect Saved Revisions Count'); + if (res.body.data.savedRevisions !== 1) { + throw new Error('Incorrect Saved Revisions Count'); + } }) .expect('Content-Type', /json/) .expect(200, done); @@ -339,7 +348,7 @@ describe(__filename, function () { api.get(`${endPoint('listSavedRevisions')}&padID=${testPadId}`) .expect((res) => { if (res.body.code !== 0) throw new Error('Unable to get Saved Revisions List'); - if (!res.body.data.savedRevisions.equals([1])) throw new Error('Incorrect Saved Revisions List'); + assert.deepEqual(res.body.data.savedRevisions, [1]); }) .expect('Content-Type', /json/) .expect(200, done); @@ -374,7 +383,9 @@ describe(__filename, function () { this.timeout(150); api.get(`${endPoint('listAuthorsOfPad')}&padID=${testPadId}`) .expect((res) => { - if (res.body.data.authorIDs.length !== 0) throw new Error('# of Authors of pad is not 0'); + if (res.body.data.authorIDs.length !== 0) { + throw new Error('# of Authors of pad is not 0'); + } }) .expect('Content-Type', /json/) .expect(200, done); @@ -451,7 +462,6 @@ describe(__filename, function () { }); }); - const originalPadId = testPadId; const newPadId = makeid(); const copiedPadId = makeid(); @@ -609,7 +619,9 @@ describe(__filename, function () { api.get(`${endPoint('getText')}&padID=${testPadId}`) .expect((res) => { if (res.body.code !== 0) throw new Error('Pad Get Text failed'); - if (res.body.data.text !== `${text}hello\n`) throw new Error('Pad Text not set properly'); + if (res.body.data.text !== `${text}hello\n`) { + throw new Error('Pad Text not set properly'); + } }) .expect('Content-Type', /json/) .expect(200, done); @@ -627,7 +639,9 @@ describe(__filename, function () { html, }) .expect((res) => { - if (res.body.code !== 0) throw new Error("Crappy HTML Can't be Imported[we weren't able to sanitize it']"); + if (res.body.code !== 0) { + throw new Error("Crappy HTML Can't be Imported[we weren't able to sanitize it']"); + } }) .expect('Content-Type', /json/) .expect(200, done); @@ -730,7 +744,8 @@ describe(__filename, function () { describe('copyPad', function () { it('copies the content of a existent pad', function (done) { this.timeout(200); - api.get(`${endPoint('copyPad')}&sourceID=${testPadId}&destinationID=${copiedPadId}&force=true`) + api.get(`${endPoint('copyPad')}&sourceID=${testPadId}&destinationID=${copiedPadId}` + + '&force=true') .expect((res) => { if (res.body.code !== 0) throw new Error('Copy Pad Failed'); }) @@ -747,13 +762,14 @@ describe(__filename, function () { createNewPadWithHtml(sourcePadId, ulHtml, done); }); - beforeEach(function () { + beforeEach(async function () { newPad = makeid(); }); it('returns a successful response', function (done) { this.timeout(200); - api.get(`${endPoint('copyPadWithoutHistory')}&sourceID=${sourcePadId}&destinationID=${newPad}&force=false`) + api.get(`${endPoint('copyPadWithoutHistory')}&sourceID=${sourcePadId}` + + `&destinationID=${newPad}&force=false`) .expect((res) => { if (res.body.code !== 0) throw new Error('Copy Pad Without History Failed'); }) @@ -764,14 +780,16 @@ describe(__filename, function () { // this test validates if the source pad's text and attributes are kept it('creates a new pad with the same content as the source pad', function (done) { this.timeout(200); - api.get(`${endPoint('copyPadWithoutHistory')}&sourceID=${sourcePadId}&destinationID=${newPad}&force=false`) + api.get(`${endPoint('copyPadWithoutHistory')}&sourceID=${sourcePadId}` + + `&destinationID=${newPad}&force=false`) .expect((res) => { if (res.body.code !== 0) throw new Error('Copy Pad Without History Failed'); }) .end(() => { api.get(`${endPoint('getHTML')}&padID=${newPad}`) .expect((res) => { - const receivedHtml = res.body.data.html.replace('

    ', '').toLowerCase(); + const receivedHtml = + res.body.data.html.replace('

    ', '').toLowerCase(); if (receivedHtml !== expectedHtml) { throw new Error(`HTML received from export is not the one we were expecting. @@ -794,7 +812,8 @@ describe(__filename, function () { const padWithNonExistentGroup = `notExistentGroup$${padId}`; it('throws an error', function (done) { this.timeout(150); - api.get(`${endPoint('copyPadWithoutHistory')}&sourceID=${sourcePadId}&destinationID=${padWithNonExistentGroup}&force=true`) + api.get(`${endPoint('copyPadWithoutHistory')}&sourceID=${sourcePadId}&` + + `destinationID=${padWithNonExistentGroup}&force=true`) .expect((res) => { // code 1, it means an error has happened if (res.body.code !== 1) throw new Error('It should report an error'); @@ -813,7 +832,8 @@ describe(__filename, function () { context('and force is false', function () { it('throws an error', function (done) { this.timeout(150); - api.get(`${endPoint('copyPadWithoutHistory')}&sourceID=${sourcePadId}&destinationID=${padIdExistent}&force=false`) + api.get(`${endPoint('copyPadWithoutHistory')}&sourceID=${sourcePadId}` + + `&destinationID=${padIdExistent}&force=false`) .expect((res) => { // code 1, it means an error has happened if (res.body.code !== 1) throw new Error('It should report an error'); @@ -825,10 +845,13 @@ describe(__filename, function () { context('and force is true', function () { it('returns a successful response', function (done) { this.timeout(200); - api.get(`${endPoint('copyPadWithoutHistory')}&sourceID=${sourcePadId}&destinationID=${padIdExistent}&force=true`) + api.get(`${endPoint('copyPadWithoutHistory')}&sourceID=${sourcePadId}` + + `&destinationID=${padIdExistent}&force=true`) .expect((res) => { // code 1, it means an error has happened - if (res.body.code !== 0) throw new Error('Copy pad without history with force true failed'); + if (res.body.code !== 0) { + throw new Error('Copy pad without history with force true failed'); + } }) .expect(200, done); }); @@ -842,7 +865,7 @@ describe(__filename, function () { */ -var createNewPadWithHtml = function (padId, html, cb) { +const createNewPadWithHtml = (padId, html, cb) => { api.get(`${endPoint('createPad')}&padID=${padId}`) .end(() => { api.post(endPoint('setHTML')) @@ -854,11 +877,6 @@ var createNewPadWithHtml = function (padId, html, cb) { }); }; -var endPoint = function (point, version) { - version = version || apiVersion; - return `/api/${version}/${point}?apikey=${apiKey}`; -}; - function makeid() { let text = ''; const possible = 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789'; @@ -878,22 +896,3 @@ function generateLongText() { } return text; } - -// Need this to compare arrays (listSavedRevisions test) -Array.prototype.equals = function (array) { - // if the other array is a falsy value, return - if (!array) return false; - // compare lengths - can save a lot of time - if (this.length != array.length) return false; - for (let i = 0, l = this.length; i < l; i++) { - // Check if we have nested arrays - if (this[i] instanceof Array && array[i] instanceof Array) { - // recurse into the nested arrays - if (!this[i].equals(array[i])) return false; - } else if (this[i] != array[i]) { - // Warning - two different object instances will never be equal: {x:20} != {x:20} - return false; - } - } - return true; -}; diff --git a/src/tests/backend/specs/api/sessionsAndGroups.js b/src/tests/backend/specs/api/sessionsAndGroups.js index c3cc38752..66877b918 100644 --- a/src/tests/backend/specs/api/sessionsAndGroups.js +++ b/src/tests/backend/specs/api/sessionsAndGroups.js @@ -1,3 +1,5 @@ +'use strict'; + const assert = require('assert').strict; const common = require('../../common'); const settings = require('../../../../node/utils/Settings'); @@ -12,6 +14,8 @@ let authorID = ''; let sessionID = ''; let padID = makeid(); +const endPoint = (point) => `/api/${apiVersion}/${point}?apikey=${apiKey}`; + describe(__filename, function () { describe('API Versioning', function () { it('errors if can not connect', async function () { @@ -389,11 +393,6 @@ describe(__filename, function () { }); }); - -const endPoint = function (point) { - return `/api/${apiVersion}/${point}?apikey=${apiKey}`; -}; - function makeid() { let text = ''; const possible = 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789'; diff --git a/src/tests/backend/specs/caching_middleware.js b/src/tests/backend/specs/caching_middleware.js index 151bb6282..a40ada7d8 100644 --- a/src/tests/backend/specs/caching_middleware.js +++ b/src/tests/backend/specs/caching_middleware.js @@ -1,3 +1,5 @@ +'use strict'; + /** * caching_middleware is responsible for serving everything under path `/javascripts/` * That includes packages as defined in `src/node/utils/tar.json` and probably also plugin code @@ -6,7 +8,6 @@ const common = require('../common'); const assert = require('assert').strict; -const url = require('url'); const queryString = require('querystring'); const settings = require('../../../node/utils/Settings'); @@ -23,7 +24,7 @@ let agent; */ function isPlaintextResponse(fileContent, resource) { // callback=require.define&v=1234 - const query = url.parse(resource).query; + const query = (new URL(resource, 'http://localhost')).search.slice(1); // require.define const jsonp = queryString.parse(query).callback;