From f40492df661650834487e091756d1d327eb28f84 Mon Sep 17 00:00:00 2001 From: Peter Murray <681306+peter-murray@users.noreply.github.com> Date: Tue, 31 Dec 2019 09:30:52 +0000 Subject: [PATCH] Adding tests for the HueAPIConfig to eliminate typo issues as identified in #155. --- lib/api/HueApiConfig.js | 8 +-- lib/api/HueApiConfig.test.js | 106 +++++++++++++++++++++++++++++++++++ 2 files changed, 110 insertions(+), 4 deletions(-) create mode 100644 lib/api/HueApiConfig.test.js diff --git a/lib/api/HueApiConfig.js b/lib/api/HueApiConfig.js index feb5bbb..96325dc 100644 --- a/lib/api/HueApiConfig.js +++ b/lib/api/HueApiConfig.js @@ -13,7 +13,7 @@ module.exports = class HueApiConfig { this._remoteApi = remoteApi; this._transport = transport; - this._isRemote = !! config.remote && !!remoteApi; + this._isRemote = !!config.remote && !!remoteApi; } /** @@ -41,7 +41,7 @@ module.exports = class HueApiConfig { if (this.isRemote) { return this._remoteApi; } else { - throw new ApiError('This API has not been set up a remote API'); + throw new ApiError('This API has not been set up as a remote API'); } } @@ -131,13 +131,13 @@ module.exports = class HueApiConfig { _requireRemote() { if (!this.isRemote) { - throw new ApiError('The function in only valid on a remote Hue API instance'); + throw new ApiError('The function is only valid on a remote Hue API instance'); } } _requireLocal() { if (this.isRemote) { - throw new ApiError('The function in only valid on a local Hue API instance'); + throw new ApiError('The function is only valid on a local Hue API instance'); } } }; \ No newline at end of file diff --git a/lib/api/HueApiConfig.test.js b/lib/api/HueApiConfig.test.js new file mode 100644 index 0000000..26cb645 --- /dev/null +++ b/lib/api/HueApiConfig.test.js @@ -0,0 +1,106 @@ +'use strict'; + +const expect = require('chai').expect + , HueApiConfig = require('./HueApiConfig') + , Transport = require('./http/Transport') + , RemoteApi = require('./http/RemoteApi') + , ApiError = require('../ApiError') +; + +const BASE_URL = 'https://localhost:443' + , DUMMY_CLIENT_KEY = 'aaaaaaaaaaaaaaaaaa' + , DUMMY_CLIENT_SECRET = 'abcd1234567890' + , DUMMY_CLIENT_ID = 'abcd1234567890abc' + , USERNAME = 'xxxxxxxxxxxxxxxxxxxx' +; + + +describe('HueApiConfig', () => { + + const dummyTransport = new Transport(null, null, null); + + describe('Remote API', () => { + + const dummyRemoteApi = new RemoteApi(null, null); + + let config; + + beforeEach(() => { + config = new HueApiConfig({ + remote: true, + username: USERNAME, + baseUrl: BASE_URL, + clientSecret: DUMMY_CLIENT_SECRET, + clientId: DUMMY_CLIENT_ID + }, + dummyTransport, + dummyRemoteApi + ); + }); + + it('should get #baseURL', () => { + expect(config.baseUrl).to.equal(BASE_URL); + }); + + it('should get #username', () => { + expect(config.username).to.equal(USERNAME); + }); + + it('should identify a remote API', () => { + expect(config.isRemote).to.be.true; + expect(config.transport).to.equal(dummyTransport); + expect(config.remote).to.equal(dummyRemoteApi); + }); + + + it('should get #clientSecret and #clientId', () => { + expect(config.clientSecret).to.equal(DUMMY_CLIENT_SECRET); + expect(config.clientId).to.equal(DUMMY_CLIENT_ID); + }); + + + it('should fail on #clientKey', () => { + try { + config.clientKey; + } catch (err) { + expect(err.message).to.contain('only valid on a local Hue API instance'); + } + }); + }); + + + describe('Local API', () => { + + let config; + + beforeEach(() => { + config = new HueApiConfig({ + baseUrl: BASE_URL, + username: USERNAME, + clientkey: DUMMY_CLIENT_KEY, + }, + dummyTransport); + }); + + it('should get #baseURL', () => { + expect(config.baseUrl).to.equal(BASE_URL); + }); + + it('should get #username', () => { + expect(config.username).to.equal(USERNAME); + }); + + it('should get #clientKey', () => { + expect(config.clientKey).to.equal(DUMMY_CLIENT_KEY); + }); + + it('should error on #remote', () => { + try { + config.remote; + } catch (err) { + expect(err).to.be.instanceOf(ApiError); + expect(err.message).to.contain('not been set up as a remote API'); + } + }); + }); +});