From 048bd0f50d6458f2fca6d9ea1737f169c523da03 Mon Sep 17 00:00:00 2001 From: Richard Hansen Date: Thu, 8 Oct 2020 01:37:17 -0400 Subject: [PATCH] tests: Simplify API key reading Also delete unused imports. --- src/node/handler/APIHandler.js | 4 ++++ tests/backend/common.js | 2 ++ tests/backend/fuzzImportTest.js | 8 ++------ tests/backend/specs/api/api.js | 9 ++------- tests/backend/specs/api/characterEncoding.js | 9 ++------- tests/backend/specs/api/chat.js | 15 ++++++--------- tests/backend/specs/api/fuzzImportTest.js | 8 ++------ tests/backend/specs/api/importexport.js | 10 ++-------- tests/backend/specs/api/importexportGetPost.js | 5 +---- tests/backend/specs/api/instance.js | 10 ++-------- tests/backend/specs/api/pad.js | 9 ++------- tests/backend/specs/api/sessionsAndGroups.js | 7 ++----- 12 files changed, 29 insertions(+), 67 deletions(-) diff --git a/src/node/handler/APIHandler.js b/src/node/handler/APIHandler.js index f4e0be38e..000e46c3a 100644 --- a/src/node/handler/APIHandler.js +++ b/src/node/handler/APIHandler.js @@ -196,3 +196,7 @@ exports.handle = async function(apiVersion, functionName, fields, req, res) // call the api function return api[functionName].apply(this, functionParams); } + +exports.exportedForTestingOnly = { + apiKey: apikey, +}; diff --git a/tests/backend/common.js b/tests/backend/common.js index 0b4f85205..6c780219c 100644 --- a/tests/backend/common.js +++ b/tests/backend/common.js @@ -1,5 +1,6 @@ function m(mod) { return __dirname + '/../../src/' + mod; } +const apiHandler = require(m('node/handler/APIHandler')); const log4js = require(m('node_modules/log4js')); const server = require(m('node/server')); const settings = require(m('node/utils/Settings')); @@ -9,6 +10,7 @@ const webaccess = require(m('node/hooks/express/webaccess')); const backups = {}; let inited = false; +exports.apiKey = apiHandler.exportedForTestingOnly.apiKey; exports.agent = null; exports.baseUrl = null; exports.httpServer = null; diff --git a/tests/backend/fuzzImportTest.js b/tests/backend/fuzzImportTest.js index 1c82e6c1c..f8eac55eb 100644 --- a/tests/backend/fuzzImportTest.js +++ b/tests/backend/fuzzImportTest.js @@ -2,17 +2,13 @@ * Fuzz testing the import endpoint * Usage: node fuzzImportTest.js */ -const fs = require('fs'); +const common = require('./common'); const settings = require(__dirname+'/loadSettings').loadSettings(); const host = "http://" + settings.ip + ":" + settings.port; -const path = require('path'); -const async = require(__dirname+'/../../src/node_modules/async'); const request = require('request'); const froth = require('mocha-froth'); -var filePath = path.join(__dirname, '/../../APIKEY.txt'); -var apiKey = fs.readFileSync(filePath, {encoding: 'utf-8'}); -apiKey = apiKey.replace(/\n$/, ""); +const apiKey = common.apiKey; var apiVersion = 1; var testPadId = "TEST_fuzz" + makeid(); diff --git a/tests/backend/specs/api/api.js b/tests/backend/specs/api/api.js index 0b372de85..e5fca6bca 100644 --- a/tests/backend/specs/api/api.js +++ b/tests/backend/specs/api/api.js @@ -6,19 +6,14 @@ * and openapi definitions. */ -const assert = require('assert'); +const common = require('../../common'); const supertest = require(__dirname + '/../../../../src/node_modules/supertest'); -const fs = require('fs'); const settings = require(__dirname + '/../../../../src/node/utils/Settings'); const api = supertest('http://' + settings.ip + ':' + settings.port); -const path = require('path'); var validateOpenAPI = require(__dirname + '/../../../../src/node_modules/openapi-schema-validation').validate; -var filePath = path.join(__dirname, '../../../../APIKEY.txt'); - -var apiKey = fs.readFileSync(filePath, { encoding: 'utf-8' }); -apiKey = apiKey.replace(/\n$/, ''); +const apiKey = common.apiKey; var apiVersion = 1; var testPadId = makeid(); diff --git a/tests/backend/specs/api/characterEncoding.js b/tests/backend/specs/api/characterEncoding.js index 83145089f..ed192d325 100644 --- a/tests/backend/specs/api/characterEncoding.js +++ b/tests/backend/specs/api/characterEncoding.js @@ -4,18 +4,13 @@ * TODO: maybe unify those two files and merge in a single one. */ -const assert = require('assert'); +const common = require('../../common'); const supertest = require(__dirname+'/../../../../src/node_modules/supertest'); const fs = require('fs'); const settings = require(__dirname + '/../../../../src/node/utils/Settings'); const api = supertest('http://'+settings.ip+":"+settings.port); -const path = require('path'); -const async = require(__dirname+'/../../../../src/node_modules/async'); -var filePath = path.join(__dirname, '../../../../APIKEY.txt'); - -var apiKey = fs.readFileSync(filePath, {encoding: 'utf-8'}); -apiKey = apiKey.replace(/\n$/, ""); +const apiKey = common.apiKey; var apiVersion = 1; var testPadId = makeid(); diff --git a/tests/backend/specs/api/chat.js b/tests/backend/specs/api/chat.js index e2ce59b1c..293b4a145 100644 --- a/tests/backend/specs/api/chat.js +++ b/tests/backend/specs/api/chat.js @@ -1,14 +1,11 @@ -var assert = require('assert') - supertest = require(__dirname+'/../../../../src/node_modules/supertest'), - fs = require('fs'), - settings = require(__dirname + '/../../../../src/node/utils/Settings'), - api = supertest('http://'+settings.ip+":"+settings.port), - path = require('path'); +function m(mod) { return __dirname + '/../../../../src/' + mod; } -var filePath = path.join(__dirname, '../../../../APIKEY.txt'); +const common = require('../../common'); +const settings = require(m('node/utils/Settings')); +const supertest = require(m('node_modules/supertest')); -var apiKey = fs.readFileSync(filePath, {encoding: 'utf-8'}); -apiKey = apiKey.replace(/\n$/, ""); +const api = supertest(`http://${settings.ip}:${settings.port}`); +const apiKey = common.apiKey; var apiVersion = 1; var authorID = ""; var padID = makeid(); diff --git a/tests/backend/specs/api/fuzzImportTest.js b/tests/backend/specs/api/fuzzImportTest.js index ca62a1bee..461d25423 100644 --- a/tests/backend/specs/api/fuzzImportTest.js +++ b/tests/backend/specs/api/fuzzImportTest.js @@ -2,17 +2,13 @@ * Fuzz testing the import endpoint */ /* -const fs = require('fs'); +const common = require('../../common'); const settings = require(__dirname+'/../../../../tests/container/loadSettings.js').loadSettings(); const host = "http://" + settings.ip + ":" + settings.port; -const path = require('path'); -const async = require(__dirname+'/../../../../src/node_modules/async'); const request = require(__dirname+'/../../../../src/node_modules/request'); const froth = require(__dirname+'/../../../../src/node_modules/mocha-froth'); -var filePath = path.join(__dirname, '../../../../APIKEY.txt'); -var apiKey = fs.readFileSync(filePath, {encoding: 'utf-8'}); -apiKey = apiKey.replace(/\n$/, ""); +const apiKey = common.apiKey; var apiVersion = 1; var testPadId = "TEST_fuzz" + makeid(); diff --git a/tests/backend/specs/api/importexport.js b/tests/backend/specs/api/importexport.js index 685bb7241..67ced55db 100644 --- a/tests/backend/specs/api/importexport.js +++ b/tests/backend/specs/api/importexport.js @@ -5,18 +5,12 @@ * TODO: unify those two files, and merge in a single one. */ -const assert = require('assert'); +const common = require('../../common'); const supertest = require(__dirname+'/../../../../src/node_modules/supertest'); -const fs = require('fs'); const settings = require(__dirname+'/../../../../tests/container/loadSettings.js').loadSettings(); const api = supertest('http://'+settings.ip+":"+settings.port); -const path = require('path'); -const async = require(__dirname+'/../../../../src/node_modules/async'); -var filePath = path.join(__dirname, '../../../../APIKEY.txt'); - -var apiKey = fs.readFileSync(filePath, {encoding: 'utf-8'}); -apiKey = apiKey.replace(/\n$/, ""); +const apiKey = common.apiKey; var apiVersion = 1; var lastEdited = ""; diff --git a/tests/backend/specs/api/importexportGetPost.js b/tests/backend/specs/api/importexportGetPost.js index 5c716ea0c..edc51f92d 100644 --- a/tests/backend/specs/api/importexportGetPost.js +++ b/tests/backend/specs/api/importexportGetPost.js @@ -8,7 +8,6 @@ const superagent = require(__dirname+'/../../../../src/node_modules/superagent') const fs = require('fs'); const settings = require(__dirname+'/../../../../src/node/utils/Settings'); const padManager = require(__dirname+'/../../../../src/node/db/PadManager'); -const path = require('path'); const plugins = require(__dirname+'/../../../../src/static/js/pluginfw/plugin_defs'); const padText = fs.readFileSync("../tests/backend/specs/api/test.txt"); @@ -17,11 +16,9 @@ const wordDoc = fs.readFileSync("../tests/backend/specs/api/test.doc"); const wordXDoc = fs.readFileSync("../tests/backend/specs/api/test.docx"); const odtDoc = fs.readFileSync("../tests/backend/specs/api/test.odt"); const pdfDoc = fs.readFileSync("../tests/backend/specs/api/test.pdf"); -var filePath = path.join(__dirname, '../../../../APIKEY.txt'); let agent; -var apiKey = fs.readFileSync(filePath, {encoding: 'utf-8'}); -apiKey = apiKey.replace(/\n$/, ""); +const apiKey = common.apiKey; var apiVersion = 1; const testPadId = makeid(); const testPadIdEnc = encodeURIComponent(testPadId); diff --git a/tests/backend/specs/api/instance.js b/tests/backend/specs/api/instance.js index 4849c6507..c7b25e8d5 100644 --- a/tests/backend/specs/api/instance.js +++ b/tests/backend/specs/api/instance.js @@ -3,18 +3,12 @@ * * Section "GLOBAL FUNCTIONS" in src/node/db/API.js */ -const assert = require('assert'); +const common = require('../../common'); const supertest = require(__dirname+'/../../../../src/node_modules/supertest'); -const fs = require('fs'); const settings = require(__dirname+'/../../../../src/node/utils/Settings'); const api = supertest('http://'+settings.ip+":"+settings.port); -const path = require('path'); - -var filePath = path.join(__dirname, '../../../../APIKEY.txt'); - -var apiKey = fs.readFileSync(filePath, {encoding: 'utf-8'}); -apiKey = apiKey.replace(/\n$/, ""); +const apiKey = common.apiKey; var apiVersion = '1.2.14'; describe('Connectivity for instance-level API tests', function() { diff --git a/tests/backend/specs/api/pad.js b/tests/backend/specs/api/pad.js index 6de05c8b5..5440856e9 100644 --- a/tests/backend/specs/api/pad.js +++ b/tests/backend/specs/api/pad.js @@ -5,18 +5,13 @@ * TODO: unify those two files, and merge in a single one. */ -const assert = require('assert'); +const common = require('../../common'); const supertest = require(__dirname+'/../../../../src/node_modules/supertest'); -const fs = require('fs'); const settings = require(__dirname + '/../../../../src/node/utils/Settings'); const api = supertest('http://'+settings.ip+":"+settings.port); -const path = require('path'); const async = require(__dirname+'/../../../../src/node_modules/async'); -var filePath = path.join(__dirname, '../../../../APIKEY.txt'); - -var apiKey = fs.readFileSync(filePath, {encoding: 'utf-8'}); -apiKey = apiKey.replace(/\n$/, ""); +const apiKey = common.apiKey; var apiVersion = 1; var testPadId = makeid(); var lastEdited = ""; diff --git a/tests/backend/specs/api/sessionsAndGroups.js b/tests/backend/specs/api/sessionsAndGroups.js index 61aa3ceb7..597b05d70 100644 --- a/tests/backend/specs/api/sessionsAndGroups.js +++ b/tests/backend/specs/api/sessionsAndGroups.js @@ -1,13 +1,10 @@ const assert = require('assert').strict; +const common = require('../../common'); const supertest = require(__dirname + '/../../../../src/node_modules/supertest'); -const fs = require('fs'); const settings = require(__dirname + '/../../../../src/node/utils/Settings'); const api = supertest(`http://${settings.ip}:${settings.port}`); -const path = require('path'); -const filePath = path.join(__dirname, '../../../../APIKEY.txt'); - -const apiKey = fs.readFileSync(filePath, {encoding: 'utf-8'}).replace(/\n$/, ''); +const apiKey = common.apiKey; let apiVersion = 1; let groupID = ''; let authorID = '';