Skip to content

Commit

Permalink
Added tests and allow nested params in Redis URI
Browse files Browse the repository at this point in the history
  • Loading branch information
jhsware committed Jan 18, 2017
1 parent 89eafbb commit 8e008c2
Show file tree
Hide file tree
Showing 15 changed files with 158 additions and 121 deletions.
14 changes: 13 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -122,4 +122,16 @@ När vi skapar nya settings-objektet
- include config in head, should look like this

{{prefixScript '/static/js/vendor.js' 'head-scripts'}}
{{prefixScript '/static/browserConfig' 'head-scripts'}}
{{prefixScript '/static/browserConfig' 'head-scripts'}}

## TODO ##
TODO - add test for decodeUri.js
TODO - add test for utils.js
TODO - add test for unpackLDAPConfig.js
TODO - add test for generateConfig.js
TODO - add test for getHandler.js

## DONE ##
DONE - add test for unpackNodeApiConfig.js
DONE - add test for unpackMongodbConfig.js
DONE - add test for unpackRedisConfig.js
2 changes: 1 addition & 1 deletion lib/unpackLDAPConfig.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ module.exports = function (envVarName, password, defaultUri, options) {
Object.assign(outp, options)
}

if (typeof envObj.query === 'object') {
if (envObj.queryString) {
var tmpQuery = envObj.query()
Object.keys(tmpQuery).forEach((key) => {
outp[key] = typeConversion(tmpQuery[key])
Expand Down
4 changes: 2 additions & 2 deletions lib/unpackMongodbConfig.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,14 +13,14 @@ module.exports = function (envVarName, defaultUri, options) {
password: envObj.password(),
uri: uri,
authDatabase: '',
ssl: safeGet(() => envObj.query['ssl'] === 'true')
ssl: false
}

if (typeof options === 'object') {
Object.assign(outp, options)
}

if (typeof envObj.query === 'object') {
if (envObj.queryString) {
var tmpQuery = envObj.query()
Object.keys(tmpQuery).forEach((key) => {
outp[key] = typeConversion(tmpQuery[key])
Expand Down
2 changes: 1 addition & 1 deletion lib/unpackNodeApiConfig.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ module.exports = function (envVarName, defaultUri, options) {
Object.assign(outp, options)
}

if (typeof envObj.query === 'object') {
if (envObj.queryString) {
var tmpQuery = envObj.query()
Object.keys(tmpQuery).forEach((key) => {
outp[key] = typeConversion(tmpQuery[key])
Expand Down
5 changes: 3 additions & 2 deletions lib/unpackRedisConfig.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

const { getEnv, typeConversion } = require('./utils')
const urlgrey = require('urlgrey')
const qs = require('qs')

module.exports = function (envVarName, defaultUri, options) {
const envObj = urlgrey(getEnv(envVarName, defaultUri))
Expand All @@ -15,8 +16,8 @@ module.exports = function (envVarName, defaultUri, options) {
Object.assign(outp, options)
}

if (typeof envObj.query === 'object') {
var tmpQuery = envObj.query()
if (envObj.queryString) {
const tmpQuery = qs.parse(envObj.queryString())
Object.keys(tmpQuery).forEach((key) => {
outp[key] = typeConversion(tmpQuery[key])
})
Expand Down
6 changes: 6 additions & 0 deletions lib/utils.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
'use strict'



module.exports.getEnv = function (name, defaultVal) {
return process.env[name] || defaultVal
}
Expand All @@ -17,3 +19,7 @@ module.exports.typeConversion = function (inp) {
return inp
}
}

module.exports.deflattenObject = function (inp) {

}
15 changes: 8 additions & 7 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "kth-node-configuration",
"version": "1.2.2",
"version": "1.2.3",
"description": "Configuration module for Node.js projects",
"main": "lib/index.js",
"repository": {
Expand All @@ -9,22 +9,23 @@
},
"dependencies": {
"deep-assign": "^2.0.0",
"urlgrey": "^0.4.4",
"safe-utils": "^0.1.1"
"qs": "^6.3.0",
"safe-utils": "^0.1.1",
"url": "^0.4.4"
},
"keywords": [
"node",
"configuration"
],
"private": true,
"devDependencies": {
"chai": "^3.5.0",
"dotenv": "2.0.0",
"standard": "7.1.0",
"tap-spec": "4.1.1",
"tape": "4.5.1"
"mocha": "^3.2.0",
"standard": "7.1.0"
},
"scripts": {
"test": "tape test/**/*Test.js | tap-spec",
"test": "node_modules/mocha/bin/mocha test/**/test-*.js",
"codecheck": "standard",
"preversion": "npm run codecheck && npm run test",
"postversion": "git push && git push --tags"
Expand Down
39 changes: 39 additions & 0 deletions test/test-unpackMongodbConfig.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
/* eslint-env mocha */
'use strict'
const expect = require('chai').expect
const unpackMongodbConfig = require('../lib/unpackMongodbConfig')

const testURI = 'mongodb://[email protected]:password@mongohost:27017/innovation?ssl=false'

describe('unpackMongodbConfig', function () {
it('can decode a Mongodb URI from fallback URI', function () {
const obj = unpackMongodbConfig('no-env-exists', testURI)
expect(obj.username).to.equal('[email protected]')
expect(obj.password).to.equal('password')
expect(obj.uri).to.equal(testURI)
expect(obj.ssl).to.equal(false)
})

it('can decode a Mongodb URI from env var', function () {
process.env['TEST_ENV_NOW_HERE'] = testURI
const obj = unpackMongodbConfig('TEST_ENV_NOW_HERE')
expect(obj.username).to.equal('[email protected]')
expect(obj.password).to.equal('password')
expect(obj.uri).to.equal(testURI)
expect(obj.ssl).to.equal(false)
})

it('can decode a Mongodb URI from fallback URI and merge with options', function () {
const obj = unpackMongodbConfig('no-env-exists', testURI, { extraOption: true })
expect(obj.username).to.equal('[email protected]')
expect(obj.password).to.equal('password')
expect(obj.uri).to.equal(testURI)
expect(obj.ssl).to.equal(false)
expect(obj.extraOption).to.equal(true)
})

it('should not expose protocol property', function () {
const obj = unpackMongodbConfig('no-env-exists', testURI)
expect(obj.protocol).to.equal(undefined)
})
})
46 changes: 46 additions & 0 deletions test/test-unpackNodeApiConfig.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
/* eslint-env mocha */
'use strict'
const expect = require('chai').expect
const unpackNodeApiConfig = require('../lib/unpackNodeApiConfig')

const testURI = 'http://node-api:3001/api/node'
const testURIWithSSL = 'https://node-api:3001/api/node'

describe('unpackNodeApiConfig', function () {
it('can decode a Mongodb URI from fallback URI', function () {
const obj = unpackNodeApiConfig('no-env-exists', testURI)
expect(obj.https).to.equal(false)
expect(obj.host).to.equal('node-api')
expect(obj.port).to.equal(3001)
expect(obj.proxyBasePath).to.equal('/api/node')
})

it('can decode a Mongodb URI from env var', function () {
process.env['TEST_ENV_NOW_HERE'] = testURI
const obj = unpackNodeApiConfig('TEST_ENV_NOW_HERE')
expect(obj.https).to.equal(false)
expect(obj.host).to.equal('node-api')
expect(obj.port).to.equal(3001)
expect(obj.proxyBasePath).to.equal('/api/node')
})

it('can decode a Mongodb URI from fallback URI and merge with options', function () {
const obj = unpackNodeApiConfig('no-env-exists', testURI, { extraOption: true })
expect(obj.https).to.equal(false)
expect(obj.host).to.equal('node-api')
expect(obj.port).to.equal(3001)
expect(obj.proxyBasePath).to.equal('/api/node')
expect(obj.extraOption).to.equal(true)
})

it('should not expose protocol property', function () {
const obj = unpackNodeApiConfig('no-env-exists', testURI)
expect(obj.protocol).to.equal(undefined)
})

it('should not expose port if https', function () {
const obj = unpackNodeApiConfig('no-env-exists', testURIWithSSL)
expect(obj.port).to.equal(undefined)
expect(obj.https).to.equal(true)
})
})
39 changes: 39 additions & 0 deletions test/test-unpackRedisConfig.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
/* eslint-env mocha */
'use strict'
const expect = require('chai').expect
const unpackRedisConfig = require('../lib/unpackRedisConfig')

const testURI = 'redis://localhost:6379/?auth=server.se&tsl[server]=tsl.server.se'

describe('unpackRedisConfig', function () {
it('can decode a Redis URI from fallback URI', function () {
const obj = unpackRedisConfig('no-env-exists', testURI)
expect(obj.host).to.equal('localhost')
expect(obj.port).to.equal(6379)
expect(obj.auth).to.equal('server.se')
expect(obj.tsl.server).to.equal('tsl.server.se')
})

it('can decode a Redis URI from env var', function () {
process.env['TEST_ENV_NOW_HERE'] = testURI
const obj = unpackRedisConfig('TEST_ENV_NOW_HERE')
expect(obj.host).to.equal('localhost')
expect(obj.port).to.equal(6379)
expect(obj.auth).to.equal('server.se')
expect(obj.tsl.server).to.equal('tsl.server.se')
})

it('can decode a Redis URI from fallback URI and merge with options', function () {
const obj = unpackRedisConfig('no-env-exists', testURI, { extraOption: true })
expect(obj.host).to.equal('localhost')
expect(obj.port).to.equal(6379)
expect(obj.auth).to.equal('server.se')
expect(obj.tsl.server).to.equal('tsl.server.se')
expect(obj.extraOption).to.equal(true)
})

it('should not expose protocol property', function () {
const obj = unpackRedisConfig('no-env-exists', testURI)
expect(obj.protocol).to.equal(undefined)
})
})
3 changes: 0 additions & 3 deletions test/unit/config/.env

This file was deleted.

15 changes: 0 additions & 15 deletions test/unit/config/defaults.js

This file was deleted.

9 changes: 0 additions & 9 deletions test/unit/config/local.js

This file was deleted.

14 changes: 0 additions & 14 deletions test/unit/config/ref.js

This file was deleted.

66 changes: 0 additions & 66 deletions test/unit/configuratorTest.js

This file was deleted.

0 comments on commit 8e008c2

Please sign in to comment.