From 1bedb0addea49fd4b335782460fa51abe28df3dd Mon Sep 17 00:00:00 2001 From: Hugo Dozois Date: Mon, 20 Feb 2017 01:21:08 -0800 Subject: [PATCH] Added initial tooling to support test for routes --- .eslintrc.js | 2 +- package.json | 6 +++-- test/.eslintrc.js | 5 ++++ test/{bson.js => lib/bsonSpec.js} | 5 ++-- test/{csv.js => lib/csvSpec.js} | 4 +-- test/lib/routers/indexSpec.js | 36 ++++++++++++++++++++++++++ test/mocha.opts | 1 + test/testDefaultConfig.js | 43 +++++++++++++++++++++++++++++++ test/testHttpUtils.js | 14 ++++++++++ test/testMongoConfig.js | 11 ++++++++ test/testMongoUtils.js | 34 ++++++++++++++++++++++++ test/testUtils.js | 10 +++++++ 12 files changed, 162 insertions(+), 9 deletions(-) create mode 100644 test/.eslintrc.js rename test/{bson.js => lib/bsonSpec.js} (99%) rename test/{csv.js => lib/csvSpec.js} (94%) create mode 100644 test/lib/routers/indexSpec.js create mode 100644 test/testDefaultConfig.js create mode 100644 test/testHttpUtils.js create mode 100644 test/testMongoConfig.js create mode 100644 test/testMongoUtils.js create mode 100644 test/testUtils.js diff --git a/.eslintrc.js b/.eslintrc.js index be5db8317..5b140ac45 100644 --- a/.eslintrc.js +++ b/.eslintrc.js @@ -43,7 +43,7 @@ module.exports = { "import/no-extraneous-dependencies": ["error", { "devDependencies": [ - "**/test/*.js", + "**/test/**/*.js", "**/scripts/*.js", "**/webpack.config.js", ] diff --git a/package.json b/package.json index 8a464c1f3..2f8bb1425 100644 --- a/package.json +++ b/package.json @@ -68,7 +68,8 @@ "eslint-plugin-lodash": "^2.3.5", "mocha": "3.0.2", "nodemon": "^1.11.0", - "pre-commit": "1.1.3" + "pre-commit": "1.1.3", + "supertest": "^3.0.0" }, "engines": { "node": ">=6.0.0", @@ -82,7 +83,8 @@ "build-dev": "webpack --watch", "build": "NODE_ENV=production webpack", "test": "npm run mocha && npm run lint", - "mocha": "mocha", + "mocha": "NODE_ENV=test mocha", + "test-watch": "NODE_ENV=test mocha --watch --reporter spec", "postinstall": "npm run build" }, "pre-commit": [ diff --git a/test/.eslintrc.js b/test/.eslintrc.js new file mode 100644 index 000000000..42275b7cd --- /dev/null +++ b/test/.eslintrc.js @@ -0,0 +1,5 @@ +module.exports = { + env: { + mocha: true, + }, +}; diff --git a/test/bson.js b/test/lib/bsonSpec.js similarity index 99% rename from test/bson.js rename to test/lib/bsonSpec.js index d76041367..c0e388676 100644 --- a/test/bson.js +++ b/test/lib/bsonSpec.js @@ -1,11 +1,10 @@ 'use strict'; -/*globals describe, it*/ - -var bson = require('../lib/bson'); var expect = require('chai').expect; var mongodb = require('mongodb'); +var bson = require('../../lib/bson'); + describe('BSON', function () { describe('toBSON', function () { it('should convert documents with native JS types', function () { diff --git a/test/csv.js b/test/lib/csvSpec.js similarity index 94% rename from test/csv.js rename to test/lib/csvSpec.js index 09a3003f5..ac6b424e8 100644 --- a/test/csv.js +++ b/test/lib/csvSpec.js @@ -1,10 +1,8 @@ 'use strict'; -/*globals describe, it*/ - var expect = require('chai').expect; var mongo = require('mongodb'); -var csv = require('../lib/csv'); +var csv = require('../../lib/csv'); var test = function (data, result) { expect(csv(data)).to.eql(result); diff --git a/test/lib/routers/indexSpec.js b/test/lib/routers/indexSpec.js new file mode 100644 index 000000000..c2821be24 --- /dev/null +++ b/test/lib/routers/indexSpec.js @@ -0,0 +1,36 @@ +'use strict'; + +const expect = require('chai').expect; + +const httpUtils = require('../../testHttpUtils'); +const testMongoUtils = require('../../testMongoUtils'); +const asPromise = require('../../testUtils').asPromise; + +describe('Routers index', () => { + let request; + let close; + let db; + before(() => { + const server = httpUtils.createServer(); + request = server.request; + close = server.close; + return testMongoUtils.initializeDb() + .then((newDb) => { + db = newDb; + }); + }); + + it('GET / should return html', () => + asPromise(cb => request.get('/').expect(200).end(cb)) + .then((res) => { + expect(res.text).to.match(/

Databases<\/h4>/); + expect(res.text).to.match(new RegExp('mongo-express-test-db

')); + }) + ); + + after(() => Promise.all([ + testMongoUtils.cleanAndCloseDb(db), + close(), + ])); +}); + diff --git a/test/mocha.opts b/test/mocha.opts index ff593c4d2..d5bd344e6 100644 --- a/test/mocha.opts +++ b/test/mocha.opts @@ -1 +1,2 @@ +--recursive --reporter dot diff --git a/test/testDefaultConfig.js b/test/testDefaultConfig.js new file mode 100644 index 000000000..8f26a1cdb --- /dev/null +++ b/test/testDefaultConfig.js @@ -0,0 +1,43 @@ +'use strict'; + +const mongoConfig = require('./testMongoConfig'); + +module.exports = () => ({ + mongodb: { + server: mongoConfig.host, + port: mongoConfig.port, + + autoReconnect: true, + poolSize: 4, + admin: true, + auth: [], + + adminUsername: '', + adminPassword: '', + whitelist: [mongoConfig.dbName], + blacklist: [], + }, + + site: { + host: 'localhost', + port: 3000, + cookieSecret: 'cookiesecret', + sessionSecret: 'sessionsecret', + cookieKeyName: 'mongo-express', + sslEnabled: false, + sslCert: '', + sslKey: '', + }, + + useBasicAuth: false, + + options: { + documentsPerPage: 10, + editorTheme: 'rubyblue', + + logger: { skip: () => true }, + readOnly: false, + }, + + defaultKeyNames: {}, +}); diff --git a/test/testHttpUtils.js b/test/testHttpUtils.js new file mode 100644 index 000000000..a648383b3 --- /dev/null +++ b/test/testHttpUtils.js @@ -0,0 +1,14 @@ +'use strict'; + +const supertest = require('supertest'); + +const defaultConf = require('./testDefaultConfig'); +const asPromise = require('./testUtils').asPromise; +const middleware = require('../lib/middleware'); + +exports.createServer = () => { + const app = middleware(defaultConf()); + const httpServer = app.listen(); + const request = supertest.agent(httpServer); + return { request, close: () => asPromise(cb => httpServer.close(cb)) }; +}; diff --git a/test/testMongoConfig.js b/test/testMongoConfig.js new file mode 100644 index 000000000..c54203023 --- /dev/null +++ b/test/testMongoConfig.js @@ -0,0 +1,11 @@ +'use strict'; + +module.exports = { + host: 'localhost', + port: 27017, + dbName: 'mongo-express-test-db', + makeConnectionUrl: () => { + const m = module.exports; + return `mongodb://${m.host}:${m.port}/${m.dbName}`; + }, +}; diff --git a/test/testMongoUtils.js b/test/testMongoUtils.js new file mode 100644 index 000000000..483642cd9 --- /dev/null +++ b/test/testMongoUtils.js @@ -0,0 +1,34 @@ +'use strict'; + +const MongoClient = require('mongodb').MongoClient; + +const mongoConfig = require('./testMongoConfig'); +const asPromise = require('./testUtils').asPromise; + +exports.testData = [ + { testItem: 1 }, + { testItem: 2 }, + { testItem: 3 }, + { testItem: 4 }, +]; +exports.testCollectionName = 'test/items'; + +exports.createConnection = () => + asPromise(cb => MongoClient.connect(mongoConfig.makeConnectionUrl(), cb)); + +exports.createTestCollection = db => + asPromise(cb => db.collection(exports.testCollectionName).insertMany(exports.testData, cb)); + +exports.dropTestCollection = db => + asPromise(cb => db.collection(exports.testCollectionName).drop(cb)); + +exports.closeDb = db => + asPromise(cb => db.close(cb)); + +exports.initializeDb = () => + exports.createConnection().then(db => + exports.createTestCollection(db).then(() => db) + ); + +exports.cleanAndCloseDb = db => + exports.dropTestCollection(db).then(() => exports.closeDb(db)); diff --git a/test/testUtils.js b/test/testUtils.js new file mode 100644 index 000000000..ef6ff2224 --- /dev/null +++ b/test/testUtils.js @@ -0,0 +1,10 @@ +'use strict'; + +exports.asPromise = function asPromise(fct) { + return new Promise((resolve, reject) => + fct((err, result) => { + if (err) reject(err); + else resolve(result); + }) + ); +};