From 29dcfeedefe5046a3e15ba4ca9780061e28d1ba2 Mon Sep 17 00:00:00 2001 From: JonahIBM <51744616+JonahIBM@users.noreply.github.com> Date: Thu, 10 Oct 2019 11:32:48 +0100 Subject: [PATCH 1/2] Code coverage for AppConfig and DeviceConfig - ApplicationConfig.js & DeviceConfig.js coverage increase and corrections to travis build error in DeviceConfig.spec.js --- src/application/ApplicationConfig.js | 2 +- test/ApplicationConfig.spec.js | 36 +++++++++++++++++++++ test/DeviceConfig.spec.js | 15 +++++---- test/DeviceConfigFileLogLevelTest.spec.yaml | 17 ---------- test/incorrectLogLevelTest.spec.yaml | 2 ++ 5 files changed, 48 insertions(+), 24 deletions(-) delete mode 100644 test/DeviceConfigFileLogLevelTest.spec.yaml create mode 100644 test/incorrectLogLevelTest.spec.yaml diff --git a/src/application/ApplicationConfig.js b/src/application/ApplicationConfig.js index 4db4249..5d29f92 100644 --- a/src/application/ApplicationConfig.js +++ b/src/application/ApplicationConfig.js @@ -179,7 +179,7 @@ export default class ApplicationConfig extends BaseConfig{ var validLevels = ["error", "warning", "info", "debug"]; if (!(validLevels.includes(data["options"]["logLevel"]))) { - throw new Error("Optional setting options.logLevel must be one of error, warning, info, debug" + data["options"]["logLevel"]) + throw new Error("Optional setting options.logLevel (Currently: " + data["options"]["logLevel"] + ") must be one of error, warning, info, debug") } } else diff --git a/test/ApplicationConfig.spec.js b/test/ApplicationConfig.spec.js index 2a88e77..187e980 100644 --- a/test/ApplicationConfig.spec.js +++ b/test/ApplicationConfig.spec.js @@ -56,4 +56,40 @@ describe('WIoTP Application Configuration', () => { expect(config.options.http.verify).to.equal(true); }); + it('Missing auth.key throws error', () => { + let identity = null; + let auth = {key: null}; + let options = null; + var applicationConfigTest = function(){new ApplicationConfig(identity, auth, options)}; + expect(applicationConfigTest).to.throw('Missing auth.key from configuration'); + }); + + it('Missing auth.token throws error', () => { + let identity = null; + let auth = {key: "MyKey", token: null}; + let options = null; + var applicationConfigTest = function(){new ApplicationConfig(identity, auth, options)}; + expect(applicationConfigTest).to.throw('Missing auth.token from configuration'); + }); + + it('Initialise without auth to induce quickstart', () => { + let identity = null; + let auth = null; + let options = null; + + let config = new ApplicationConfig(identity, auth, options); + expect(config.getOrgId()).to.equal("quickstart"); + }); + + it('Load port as a string with environment variables', () => { + process.env['WIOTP_OPTIONS_MQTT_PORT'] = '1234'; + let config = ApplicationConfig.parseEnvVars(); + expect(config.options.mqtt.port).to.equal(1234); + }); + + it('Incorrect logLevel in config file throws error', () => { + var applicationConfigTest = function(){new ApplicationConfig.parseConfigFile("./test/incorrectLogLevelTest.spec.yaml")}; + expect(applicationConfigTest).to.throw('Optional setting options.logLevel (Currently: notALogLevel) must be one of error, warning, info, debug'); + }); + }); diff --git a/test/DeviceConfig.spec.js b/test/DeviceConfig.spec.js index 369133d..9aa91b8 100644 --- a/test/DeviceConfig.spec.js +++ b/test/DeviceConfig.spec.js @@ -18,12 +18,12 @@ console.info = () => {}; describe('WIoTP Device Configuration', () => { it('Initialise with the minimum required configuration', () => { - let identity = {orgId: "orgid", typeId: "mytypeid", deviceId: "mydeviceid"}; + let identity = {orgId: "myOrg", typeId: "myType", deviceId: "myDevice"}; let auth = {token: "myToken"}; let options = null; let config = new DeviceConfig(identity, auth, options); - expect(config.getOrgId()).to.equal("orgid"); + expect(config.getOrgId()).to.equal("myOrg"); expect(config.options.logLevel).to.equal("info"); expect(config.options.domain).to.equal("internetofthings.ibmcloud.com"); expect(config.options.mqtt.transport).to.equal("tcp"); @@ -31,8 +31,9 @@ describe('WIoTP Device Configuration', () => { }); it('Load mimimal configuration from environment variables', () => { - process.env['WIOTP_IDENTITY_ORGID'] = 'testOrg'; - process.env['WIOTP_IDENTITY_DEVICEID'] = 'testDevice'; + process.env['WIOTP_IDENTITY_ORGID'] = 'myOrg'; + process.env['WIOTP_IDENTITY_DEVICEID'] = 'MyDevice'; + process.env['WIOTP_IDENTITY_TYPEID'] = 'myType'; let config = DeviceConfig.parseEnvVars(); expect(config.options.logLevel).to.equal("info"); expect(config.options.domain).to.equal("internetofthings.ibmcloud.com"); @@ -40,6 +41,8 @@ describe('WIoTP Device Configuration', () => { expect(config.options.mqtt.port).to.equal(8883); delete process.env['WIOTP_IDENTITY_ORGID']; delete process.env['WIOTP_IDENTITY_DEVICEID']; + delete process.env['WIOTP_IDENTITY_TYPEID']; + }); it('Load configuration from yaml config file', () => { @@ -114,8 +117,8 @@ describe('WIoTP Device Configuration', () => { expect(deviceConfigTest).to.throw('Missing auth.token from configuration'); }); - it('Missing config file throws error', () => { - var deviceConfigTest = function(){new DeviceConfig.parseConfigFile("./test/DeviceConfigFileLogLevelTest.spec.yaml")}; + it('Incorrect logLevel in config file throws error', () => { + var deviceConfigTest = function(){new DeviceConfig.parseConfigFile("./test/incorrectLogLevelTest.spec.yaml")}; expect(deviceConfigTest).to.throw('Optional setting options.logLevel (Currently: notALogLevel) must be one of error, warning, info, debug'); }); diff --git a/test/DeviceConfigFileLogLevelTest.spec.yaml b/test/DeviceConfigFileLogLevelTest.spec.yaml deleted file mode 100644 index cda9bca..0000000 --- a/test/DeviceConfigFileLogLevelTest.spec.yaml +++ /dev/null @@ -1,17 +0,0 @@ - - identity: - orgId: myOrg - typeId: myType - deviceId: myDevice - auth: - token: myToken - options: - domain: internetofthings.ibmcloud.com - logLevel: notALogLevel - mqtt: - port: 8883 - transport: tcp - cleanStart: true - sessionExpiry: 3600 - keepAlive: 60 - caFile: myPath diff --git a/test/incorrectLogLevelTest.spec.yaml b/test/incorrectLogLevelTest.spec.yaml new file mode 100644 index 0000000..35829c6 --- /dev/null +++ b/test/incorrectLogLevelTest.spec.yaml @@ -0,0 +1,2 @@ + options: + logLevel: notALogLevel From dcd62496f20ff6b4077c622e2eb2f4f816f1ed9b Mon Sep 17 00:00:00 2001 From: JonahIBM <51744616+JonahIBM@users.noreply.github.com> Date: Mon, 21 Oct 2019 11:22:26 +0100 Subject: [PATCH 2/2] Application config mqtt port consitencies Changed the env port from 1234 to 8883 --- package.json | 6 +++--- test/ApplicationConfig.spec.js | 4 ++-- 2 files changed, 5 insertions(+), 5 deletions(-) diff --git a/package.json b/package.json index 504986b..d742016 100644 --- a/package.json +++ b/package.json @@ -32,11 +32,11 @@ "@babel/core": "7.4.4", "@babel/preset-env": "7.4.4", "@babel/register": "7.4.4", - "@cloudant/cloudant": "2.1.0", + "@cloudant/cloudant": "^2.1.0", "@istanbuljs/nyc-config-babel": "^2.1.1", "babel-plugin-istanbul": "^5.1.4", - "chai": "4.2.0", - "chai-as-promised": "7.1.1", + "chai": "^4.2.0", + "chai-as-promised": "^7.1.1", "coveralls": "3.0.3", "mocha": "6.1.4", "mocha-steps": "^1.3.0", diff --git a/test/ApplicationConfig.spec.js b/test/ApplicationConfig.spec.js index 187e980..7afab0c 100644 --- a/test/ApplicationConfig.spec.js +++ b/test/ApplicationConfig.spec.js @@ -82,9 +82,9 @@ describe('WIoTP Application Configuration', () => { }); it('Load port as a string with environment variables', () => { - process.env['WIOTP_OPTIONS_MQTT_PORT'] = '1234'; + process.env['WIOTP_OPTIONS_MQTT_PORT'] = '8883'; let config = ApplicationConfig.parseEnvVars(); - expect(config.options.mqtt.port).to.equal(1234); + expect(config.options.mqtt.port).to.equal(8883); }); it('Incorrect logLevel in config file throws error', () => {