diff --git a/README.md b/README.md index 7242ee3..8f158b7 100644 --- a/README.md +++ b/README.md @@ -137,10 +137,11 @@ DeviceHive module | [options.accessToken] | string | Access token | | [options.refreshToken] | string | Refresh token | | [options.login] | string | Login | -| [options.password] | string | Paaword | +| [options.password] | string | Password | | options.mainServiceURL | string | Main Service URL | | [options.authServiceURL] | string | Auth Service URL (required only for http) | -| [options.pluginServiceURL] | string | Alug inServi ceURL (required only for http) | +| [options.pluginServiceURL] | string | Plugin Service URL (required only for http) | +| [options.autoUpdateSession] | boolean | Flag to enable/disable autoupdating session. Default: true | @@ -2552,7 +2553,7 @@ Creates Plugin Update Query model | [options.returnCommands] | boolean | Checks if commands should be returned | | [options.returnUpdatedCommands] | boolean | Checks if updated commands should be returned | | [options.returnNotifications] | boolean | Checks if commands should be returned | -| [options.status] | string | Plugin status - active or disabled (ACTIVE | DISABLED | CREATED) | +| [options.status] | string | Plugin status - active or disabled (ACTIVE | INACTIVE | CREATED) | | [options.name] | string | Plugin name | | [options.description] | string | Plugin description | | [options.parameters] | string | Plugin parameters | @@ -2621,12 +2622,3 @@ Creates User List Query ### userListQuery.toObject() ⇒ Object Returns instance as a plain JS object - - - -## License - -[DeviceHive] is developed by [DataArt] Apps and distributed under Open Source -[Apache 2.0 license](https://en.wikipedia.org/wiki/Apache_License). - -© Copyright 2018 [DataArt] Apps © All Rights Reservedstrong text \ No newline at end of file diff --git a/example/node/index.js b/example/node/index.js index 31dc98d..9503faf 100644 --- a/example/node/index.js +++ b/example/node/index.js @@ -1,5 +1,6 @@ const DeviceHive = require(`../../index`); + const httpDeviceHive = new DeviceHive({ login: `dhadmin`, password: `dhadmin_#911`, @@ -11,7 +12,8 @@ const httpDeviceHive = new DeviceHive({ const wsDeviceHive = new DeviceHive({ login: `dhadmin`, password: `dhadmin_#911`, - mainServiceURL: 'ws://localhost:8080/dh/websocket' + mainServiceURL: 'ws://localhost:8080/dh/websocket', + autoUpdateSession: true }); const DeviceListQuery = DeviceHive.models.query.DeviceListQuery; @@ -48,7 +50,10 @@ void async function start () { console.log(await wsDeviceHive.network.list(networkListQuery)); console.log(await wsDeviceHive.token.refresh(refreshToken)); } + } catch (error) { console.warn(error); } + + process.exit(1); }(); \ No newline at end of file diff --git a/package-lock.json b/package-lock.json index 8112900..305e6f1 100644 --- a/package-lock.json +++ b/package-lock.json @@ -851,9 +851,9 @@ } }, "babel-loader": { - "version": "8.0.0-beta.0", - "resolved": "https://registry.npmjs.org/babel-loader/-/babel-loader-8.0.0-beta.0.tgz", - "integrity": "sha512-qVXXyIqTrLBH3Ki2VCJog1fUd6qzKUk9lHS34WJPW93Bh0BUvXTFSD5ZkG3a5+Uxxje+RgCk8Y7RyB6zyK9rWw==", + "version": "8.0.0-beta.2", + "resolved": "https://registry.npmjs.org/babel-loader/-/babel-loader-8.0.0-beta.2.tgz", + "integrity": "sha512-P1zch1DvQy3RGmp/1CH78uPg5gTPQQ01S9r6ipCOWVamO0UIC8gnrx7m7LsUsXa470yB6IOZxhtEEwIUclRLNw==", "dev": true, "requires": { "find-cache-dir": "1.0.0", @@ -2267,7 +2267,7 @@ "dev": true, "requires": { "commondir": "1.0.1", - "make-dir": "1.1.0", + "make-dir": "1.2.0", "pkg-dir": "2.0.0" } }, @@ -2714,6 +2714,11 @@ "integrity": "sha1-P02uSpH6wxX3EGL4UhzCOfE2YoA=", "dev": true }, + "jwt-decode": { + "version": "2.2.0", + "resolved": "https://registry.npmjs.org/jwt-decode/-/jwt-decode-2.2.0.tgz", + "integrity": "sha1-fYa9VmefWM5qhHBKZX3TkruoGnk=" + }, "kind-of": { "version": "3.2.2", "resolved": "https://registry.npmjs.org/kind-of/-/kind-of-3.2.2.tgz", @@ -2813,20 +2818,12 @@ } }, "make-dir": { - "version": "1.1.0", - "resolved": "https://registry.npmjs.org/make-dir/-/make-dir-1.1.0.tgz", - "integrity": "sha512-0Pkui4wLJ7rxvmfUvs87skoEaxmu0hCUApF8nonzpl7q//FWp9zu8W61Scz4sd/kUiqDxvUhtoam2efDyiBzcA==", + "version": "1.2.0", + "resolved": "https://registry.npmjs.org/make-dir/-/make-dir-1.2.0.tgz", + "integrity": "sha512-aNUAa4UMg/UougV25bbrU4ZaaKNjJ/3/xnvg/twpmKROPdKZPZ9wGgI0opdZzO8q/zUFawoUuixuOv33eZ61Iw==", "dev": true, "requires": { "pify": "3.0.0" - }, - "dependencies": { - "pify": { - "version": "3.0.0", - "resolved": "https://registry.npmjs.org/pify/-/pify-3.0.0.tgz", - "integrity": "sha1-5aSs0sEB/fPZpNB/DbxNtJ3SgXY=", - "dev": true - } } }, "md5.js": { @@ -3282,6 +3279,12 @@ "sha.js": "2.4.10" } }, + "pify": { + "version": "3.0.0", + "resolved": "https://registry.npmjs.org/pify/-/pify-3.0.0.tgz", + "integrity": "sha1-5aSs0sEB/fPZpNB/DbxNtJ3SgXY=", + "dev": true + }, "pkg-dir": { "version": "2.0.0", "resolved": "https://registry.npmjs.org/pkg-dir/-/pkg-dir-2.0.0.tgz", @@ -4109,14 +4112,13 @@ "dev": true }, "ws": { - "version": "4.0.0", - "resolved": "https://registry.npmjs.org/ws/-/ws-4.0.0.tgz", - "integrity": "sha512-QYslsH44bH8O7/W2815u5DpnCpXWpEK44FmaHffNwgJI4JMaSZONgPBTOfrxJ29mXKbXak+LsJ2uAkDTYq2ptQ==", + "version": "4.1.0", + "resolved": "https://registry.npmjs.org/ws/-/ws-4.1.0.tgz", + "integrity": "sha512-ZGh/8kF9rrRNffkLFV4AzhvooEclrOH0xaugmqGsIfFgOE/pIz4fMc4Ef+5HSQqTEug2S9JZIWDR47duDSLfaA==", "dev": true, "requires": { "async-limiter": "1.0.0", - "safe-buffer": "5.1.1", - "ultron": "1.1.1" + "safe-buffer": "5.1.1" } }, "xtend": { diff --git a/package.json b/package.json index d99288b..686702e 100644 --- a/package.json +++ b/package.json @@ -17,6 +17,7 @@ "license": "Apache-2.0", "dependencies": { "isomorphic-fetch": "^2.2.1", + "jwt-decode": "^2.2.0", "query-string": "^5.1.0", "string-template": "^1.0.0", "universal-websocket-client": "^1.0.1" @@ -25,13 +26,13 @@ "@babel/core": "^7.0.0-beta.40", "@babel/preset-env": "^7.0.0-beta.40", "babel-core": "^6.26.0", - "babel-loader": "^8.0.0-beta.0", + "babel-loader": "^8.0.0-beta.2", "babelify": "^8.0.0", "browserify": "^15.2.0", "chai": "^4.1.2", "exorcist": "^1.0.1", "mocha": "^5.0.1", "tinyify": "^2.4.0", - "ws": "^4.0.0" + "ws": "^4.1.0" } } diff --git a/src/ApiStrategy.js b/src/ApiStrategy.js index ec8db4e..73a0296 100644 --- a/src/ApiStrategy.js +++ b/src/ApiStrategy.js @@ -20,9 +20,9 @@ class ApiStrategy extends EventEmitter { static getType(url) { let result; - if (url.startsWith('http') || url.startsWith('https')) { + if (url.startsWith(HTTP.TYPE)) { result = HTTP; - } else if (url.startsWith('ws') || url.startsWith('wss')) { + } else if (url.startsWith(WS.TYPE)) { result = WS; } else { throw new UnsupportedTransportError(); @@ -40,6 +40,8 @@ class ApiStrategy extends EventEmitter { const me = this; + me.reconnectionHandler = null; + me.urlsMap = new Map(); me.urlsMap.set(API.MAIN_BASE, mainServiceURL); @@ -83,7 +85,16 @@ class ApiStrategy extends EventEmitter { } return me.strategy.send(sendData) - .then((response) => API.normalizeResponse(me.strategy.type, key, response)); + .then((response) => API.normalizeResponse(me.strategy.type, key, response)) + .catch(error => { + if (error === Utils.TOKEN_EXPIRED_MARK && me.reconnectionHandler) { + return me.reconnectionHandler() + .then(() => me.strategy.send(sendData)) + .then((response) => API.normalizeResponse(me.strategy.type, key, response)); + } else { + throw error; + } + }); } } diff --git a/src/DeviceHive.js b/src/DeviceHive.js index 7cabbf2..e606e23 100644 --- a/src/DeviceHive.js +++ b/src/DeviceHive.js @@ -1,3 +1,4 @@ +const Utils = require(`./utils/Utils`); const EventEmitter = require('events'); const APIStrategy = require('./ApiStrategy'); const InfoAPI = require('./controllers/ServerInfoAPI'); @@ -98,7 +99,7 @@ class DeviceHive extends EventEmitter { * @param {string} [options.authServiceURL] - Auth Service URL (required only for http) * @param {string} [options.pluginServiceURL] - Alug inServi ceURL (required only for http) */ - constructor({ accessToken, refreshToken, login, password, mainServiceURL, authServiceURL, pluginServiceURL }) { + constructor({ mainServiceURL, authServiceURL, pluginServiceURL, accessToken, refreshToken, login, password, autoUpdateSession = true }) { super(); const me = this; @@ -107,6 +108,7 @@ class DeviceHive extends EventEmitter { me.refreshToken = refreshToken; me.login = login; me.password = password; + me.autoUpdateSession = autoUpdateSession; me.strategy = new APIStrategy({ mainServiceURL, authServiceURL, pluginServiceURL }); @@ -130,22 +132,48 @@ class DeviceHive extends EventEmitter { * Connect to the DeviceHive service * @returns {Promise} */ - async connect() { + async connect({ accessToken, refreshToken, login, password } = {}) { const me = this; - if (me.accessToken || me.refreshToken || (me.login && me.password)) { + if (!accessToken && !refreshToken && !(login && password)) { + accessToken = accessToken || me.accessToken; + refreshToken = refreshToken || me.refreshToken; + login = login || me.login; + password = password || me.password; + } + + if (accessToken || refreshToken || (login && password)) { try { - if (me.accessToken) { - await me.strategy.authorize(me.accessToken); - } else if (me.refreshToken) { - const accessToken = await me.token.refresh(me.refreshToken); + if (login && password) { + const { accessToken, refreshToken } = await me.token.login(login, password); + await me.strategy.authorize(accessToken); - } else if (me.login && me.password) { - const { accessToken } = await me.token.login(me.login, me.password); + + me.accessToken = accessToken; + me.refreshToken = refreshToken; + } else if (refreshToken) { + const { accessToken } = await me.token.refresh(refreshToken); + await me.strategy.authorize(accessToken); + + me.accessToken = accessToken; + me.refreshToken = refreshToken; + } else if (accessToken) { + await me.strategy.authorize(accessToken); + + me.accessToken = accessToken; + } + + if (me.autoUpdateSession === true) { + const userTokens = await me.token.createUserToken( + Utils.createUserTokenFromJWT(me.accessToken)); + + me.accessToken = userTokens.accessToken; + me.refreshToken = userTokens.refreshToken; + me.strategy.reconnectionHandler = () => me.connect({ refreshToken: me.refreshToken }); } } catch (error) { - throw new InvalidCredentialsError(); + throw new InvalidCredentialsError(error); } } else { throw new NoAuthCredentialsError(); diff --git a/src/controllers/transportResolvers/ApiMap.js b/src/controllers/transportResolvers/ApiMap.js index 56138d1..25bf7f5 100644 --- a/src/controllers/transportResolvers/ApiMap.js +++ b/src/controllers/transportResolvers/ApiMap.js @@ -156,15 +156,15 @@ class ApiMap { } -apiMap.set(ApiMap.login, { http: { method: 'POST', uri: '/token', base: ApiMap.AUTH_BASE }, ws: { action: 'token', response: [`accessToken`, `refreshToken`] } }); +apiMap.set(ApiMap.login, { http: { method: 'POST', uri: '/token', base: ApiMap.AUTH_BASE, noAuth: true }, ws: { action: 'token', response: [`accessToken`, `refreshToken`] } }); apiMap.set(ApiMap.createUserToken, { http: { method: 'POST', uri: '/token/create', base: ApiMap.AUTH_BASE }, ws: { action: 'token/create', bodyKey: 'payload', response: [`accessToken`, `refreshToken`] } }); apiMap.set(ApiMap.createPluginToken, { http: { method: 'POST', uri: '/token/plugin/create', base: ApiMap.AUTH_BASE } }); -apiMap.set(ApiMap.refreshToken, { http: { method: 'POST', uri: '/token/refresh', base: ApiMap.AUTH_BASE }, ws: { action: 'token/refresh', response: [`accessToken`] } }); +apiMap.set(ApiMap.refreshToken, { http: { method: 'POST', uri: '/token/refresh', base: ApiMap.AUTH_BASE, noAuth: true }, ws: { action: 'token/refresh', response: [`accessToken`] } }); apiMap.set(ApiMap.authenticatePlugin, { http: { method: 'GET', uri: '/token/plugin/authenticate', base: ApiMap.AUTH_BASE } }); -apiMap.set(ApiMap.getServerInfo, { http: { method: 'GET', uri: '/info', base: ApiMap.MAIN_BASE }, ws: { action: 'server/info', response: { bodyKey: `info` } } }); -apiMap.set(ApiMap.getCacheInfo, { http: { method: 'GET', uri: '/info/cache', base: ApiMap.MAIN_BASE } }); -apiMap.set(ApiMap.getClusterInfo, { http: { method: 'GET', uri: '/info/config/cluster', base: ApiMap.MAIN_BASE }, ws: { action: 'cluster/info', response: { bodyKey: `clusterInfo` } } }); +apiMap.set(ApiMap.getServerInfo, { http: { method: 'GET', uri: '/info', base: ApiMap.MAIN_BASE, noAuth: true }, ws: { action: 'server/info', response: { bodyKey: `info` } } }); +apiMap.set(ApiMap.getCacheInfo, { http: { method: 'GET', uri: '/info/cache', base: ApiMap.MAIN_BASE, noAuth: true } }); +apiMap.set(ApiMap.getClusterInfo, { http: { method: 'GET', uri: '/info/config/cluster', base: ApiMap.MAIN_BASE, noAuth: true }, ws: { action: 'cluster/info', response: { bodyKey: `clusterInfo` } } }); apiMap.set(ApiMap.getConfiguration, { http: { method: 'GET', uri: '/configuration/{name}', base: ApiMap.MAIN_BASE }, ws: { action: 'configuration/get', response: { bodyKey: `configuration` } } }); apiMap.set(ApiMap.putConfiguration, { http: { method: 'PUT', uri: '/configuration/{name}', base: ApiMap.MAIN_BASE }, ws: { action: 'configuration/put', response: { bodyKey: `configuration` } } }); diff --git a/src/controllers/transportResolvers/HttpApiResolver.js b/src/controllers/transportResolvers/HttpApiResolver.js index b207d7b..26c1213 100644 --- a/src/controllers/transportResolvers/HttpApiResolver.js +++ b/src/controllers/transportResolvers/HttpApiResolver.js @@ -54,7 +54,7 @@ class HttpApiResolver { * @param {boolean} options.subscription * @param {boolean} options.unsubscription */ - constructor({ method, uri, base, subscription, unsubscription }) { + constructor({ method, uri, base, subscription, unsubscription, noAuth }) { const me = this; me.method = method; @@ -62,6 +62,7 @@ class HttpApiResolver { me.base = base; me.subscription = subscription; me.unsubscription = unsubscription; + me.noAuth = noAuth; } /** @@ -76,6 +77,7 @@ class HttpApiResolver { if (me.unsubscription === true) { result = { + noAuth: me.noAuth, unsubscription: me.unsubscription, body: { subscriptionId: parameters.subscriptionId @@ -83,6 +85,7 @@ class HttpApiResolver { }; } else { result = { + noAuth: me.noAuth, method: me.method, endpoint: HttpApiResolver.buildUrl(me.uri, parameters), base: me.base, diff --git a/src/error/InvalidCredentialsError.js b/src/error/InvalidCredentialsError.js index d0fd1d6..a93f4a6 100644 --- a/src/error/InvalidCredentialsError.js +++ b/src/error/InvalidCredentialsError.js @@ -6,10 +6,10 @@ class InvalidCredentialsError extends Error { /** * Creates new InvalidCredentialsError */ - constructor() { + constructor(message) { super(); - this.message = `Invalid credentials error during attempt to authenticate.`; + this.message = `Invalid credentials error during attempt to authenticate. Error: ${message}`; } } diff --git a/src/transports/HTTP.js b/src/transports/HTTP.js index 058c55d..d5ab869 100644 --- a/src/transports/HTTP.js +++ b/src/transports/HTTP.js @@ -37,7 +37,7 @@ class HTTP extends Transport { /** * Rest API send method */ - send({ endpoint, method, body, subscription, unsubscription }) { + send({ endpoint, method, body, subscription, unsubscription, noAuth }) { const me = this; if (subscription === true) { @@ -62,7 +62,7 @@ class HTTP extends Transport { return Promise.resolve({ status: `No such subscription` }) } } else { - return fetch(endpoint, { headers: me._getHeaders(), method: method, body: JSON.stringify(body) }) + return fetch(endpoint, { headers: me._getHeaders(noAuth), method: method, body: JSON.stringify(body) }) .then(response => response.text()) .then(responseText => { return responseText ? JSON.parse(responseText) : responseText @@ -110,15 +110,15 @@ class HTTP extends Transport { * @returns {Object} * @private */ - _getHeaders() { + _getHeaders(noAuth = false) { const me = this; const headers = { "Content-type": `application/json`, "Accept": `application/json` }; - if (me.token) { - headers[`Authorization`] = `Bearer ${me.token}`; + if (me.token && !noAuth) { + headers.Authorization = `Bearer ${me.token}`; } return headers; diff --git a/src/utils/Utils.js b/src/utils/Utils.js index 7f99d1b..102e21c 100644 --- a/src/utils/Utils.js +++ b/src/utils/Utils.js @@ -1,8 +1,14 @@ +const jwtDecode = require(`jwt-decode`); +const UserToken = require(`../models/UserToken`); + + /** * Utils */ class Utils { + static get TOKEN_EXPIRED_MARK() { return `Token expired`; } + /** * Checks that object is empty * @returns {boolean} - Is object empty @@ -25,6 +31,22 @@ class Utils { return `${firstPart}${secondPart}`; } + + /** + * Creates UserToken from jwt + * @param jwtToken + * @returns {UserToken} + */ + static createUserTokenFromJWT(jwtToken) { + const tokenPayload = jwtDecode(jwtToken).payload; + + return new UserToken({ + userId: tokenPayload.u, + actions: tokenPayload.a, + networkIds: tokenPayload.n, + deviceTypeIds: tokenPayload.dt + }); + } } diff --git a/test/integration/controllers/ConfigurationAPI.spec.js b/test/integration/controllers/ConfigurationAPI.spec.js index d4bf98a..508479e 100644 --- a/test/integration/controllers/ConfigurationAPI.spec.js +++ b/test/integration/controllers/ConfigurationAPI.spec.js @@ -8,7 +8,7 @@ const events = new EventEmitter(); const DeviceHive = require('../../../index'); const httpDeviceHive = new DeviceHive(config.server.http); -const wsDeviceHive = new DeviceHive(config.server.ws) +const wsDeviceHive = new DeviceHive(config.server.ws); const testConfigurations = [ diff --git a/test/integration/controllers/Device.spec.js b/test/integration/controllers/Device.spec.js index 72b4512..e0329d7 100644 --- a/test/integration/controllers/Device.spec.js +++ b/test/integration/controllers/Device.spec.js @@ -8,7 +8,7 @@ const events = new EventEmitter(); const DeviceHive = require('../../../index'); const httpDeviceHive = new DeviceHive(config.server.http); -const wsDeviceHive = new DeviceHive(config.server.ws) +const wsDeviceHive = new DeviceHive(config.server.ws); const testDevices = [ diff --git a/test/integration/controllers/DeviceCommandAPI.spec.js b/test/integration/controllers/DeviceCommandAPI.spec.js index f139351..eba276c 100644 --- a/test/integration/controllers/DeviceCommandAPI.spec.js +++ b/test/integration/controllers/DeviceCommandAPI.spec.js @@ -8,9 +8,9 @@ const events = new EventEmitter(); const DeviceHive = require('../../../index'); const httpDeviceHive = new DeviceHive(config.server.http); -const wsDeviceHive = new DeviceHive(config.server.ws) +const wsDeviceHive = new DeviceHive(config.server.ws); -const deviceId = 'e50d6085-2aba-48e9-b1c3-73c673e414be2'; +const deviceId = 'e50d6085-2aba-48e9-b1c3-73c673e414be'; const testDeviceCommands = [ { diff --git a/test/integration/controllers/DeviceNotificationAPI.spec.js b/test/integration/controllers/DeviceNotificationAPI.spec.js index fb97e36..c4d42a8 100644 --- a/test/integration/controllers/DeviceNotificationAPI.spec.js +++ b/test/integration/controllers/DeviceNotificationAPI.spec.js @@ -8,9 +8,9 @@ const events = new EventEmitter(); const DeviceHive = require('../../../index'); const httpDeviceHive = new DeviceHive(config.server.http); -const wsDeviceHive = new DeviceHive(config.server.ws) +const wsDeviceHive = new DeviceHive(config.server.ws); -const deviceId = 'e50d6085-2aba-48e9-b1c3-73c673e414be2'; +const deviceId = 'e50d6085-2aba-48e9-b1c3-73c673e414be'; const testDeviceNotifications = [ { @@ -28,7 +28,7 @@ const testDeviceNotifications = [ parameters: { jsonString: 'jsonString' } - }, + } ]; describe('NotificationAPI', () => { diff --git a/test/integration/controllers/DeviceTypeAPI.spec.js b/test/integration/controllers/DeviceTypeAPI.spec.js index 82cf932..cb6412d 100644 --- a/test/integration/controllers/DeviceTypeAPI.spec.js +++ b/test/integration/controllers/DeviceTypeAPI.spec.js @@ -8,7 +8,7 @@ const events = new EventEmitter(); const DeviceHive = require('../../../index'); const httpDeviceHive = new DeviceHive(config.server.http); -const wsDeviceHive = new DeviceHive(config.server.ws) +const wsDeviceHive = new DeviceHive(config.server.ws); const testDeviceTypes = [ diff --git a/test/integration/controllers/NetworkAPI.spec.js b/test/integration/controllers/NetworkAPI.spec.js index 12f6a6b..2ce51e1 100644 --- a/test/integration/controllers/NetworkAPI.spec.js +++ b/test/integration/controllers/NetworkAPI.spec.js @@ -8,7 +8,7 @@ const events = new EventEmitter(); const DeviceHive = require('../../../index'); const httpDeviceHive = new DeviceHive(config.server.http); -const wsDeviceHive = new DeviceHive(config.server.ws) +const wsDeviceHive = new DeviceHive(config.server.ws); const testNetworks = [ diff --git a/test/integration/controllers/PluginAPI.spec.js b/test/integration/controllers/PluginAPI.spec.js index fab0122..e53b5a8 100644 --- a/test/integration/controllers/PluginAPI.spec.js +++ b/test/integration/controllers/PluginAPI.spec.js @@ -8,7 +8,7 @@ const events = new EventEmitter(); const DeviceHive = require('../../../index'); const httpDeviceHive = new DeviceHive(config.server.http); -const wsDeviceHive = new DeviceHive(config.server.ws) +const wsDeviceHive = new DeviceHive(config.server.ws); const testPlugins = [ @@ -24,8 +24,6 @@ const testPlugins = [ describe('PluginAPI', () => { before(done => { - // Configaratuion DeviceHive - Promise.all([httpDeviceHive.connect(), wsDeviceHive.connect()]) .then(() => done()); }); @@ -50,8 +48,6 @@ describe('PluginAPI', () => { }); it('PluginAPI.list()', done => { - - // Configurating Plugin List query const pluginListQuery = new DeviceHive.models.query.PluginListQuery({ take: 1, skip: 0 @@ -68,14 +64,31 @@ describe('PluginAPI', () => { }); - // TODO - // it('PluginAPI.update()', done => { - // }); + it('PluginAPI.update()', done => { + const updatedName = `${testPlugins[0].name}-updated`; + const pluginUpdateQuery = new DeviceHive.models.query.PluginUpdateQuery({ + topicName: testPlugins[0].topicName, + name: updatedName + }); + const pluginListQuery = new DeviceHive.models.query.PluginListQuery({ + name: updatedName, + take: 1, + skip: 0 + }); + Promise.all([httpDeviceHive.plugin.update(pluginUpdateQuery)]) + .then(() => Promise.all([httpDeviceHive.plugin.list(pluginListQuery)])) + .then(dataAll => { + for (const data of dataAll) { + assert.strictEqual(data[0].name, updatedName); + } + }) + .then(() => done()) + .catch(done); + }); - it('PluginAPI.count()', done => { - // Configurating Plugin List query + it('PluginAPI.count()', done => { const pluginCountQuery = new DeviceHive.models.query.PluginCountQuery({ status: '1' }); @@ -91,8 +104,6 @@ describe('PluginAPI', () => { }); it('TokenAPI.createPluginToken()', done => { - - // Configurating Token model const token = new DeviceHive.models.PluginToken({ actions: [0], expiration: '2018-02-09T10:09:03.033Z', diff --git a/test/integration/controllers/ServerInfoAPI.spec.js b/test/integration/controllers/ServerInfoAPI.spec.js index 4b21d60..0bc96df 100644 --- a/test/integration/controllers/ServerInfoAPI.spec.js +++ b/test/integration/controllers/ServerInfoAPI.spec.js @@ -8,7 +8,7 @@ const events = new EventEmitter(); const DeviceHive = require('../../../index'); const httpDeviceHive = new DeviceHive(config.server.http); -const wsDeviceHive = new DeviceHive(config.server.ws) +const wsDeviceHive = new DeviceHive(config.server.ws); describe('ServerInfoAPI', () => { diff --git a/test/integration/controllers/TokenAPI.spec.js b/test/integration/controllers/TokenAPI.spec.js index d6e7aab..60f2fc3 100644 --- a/test/integration/controllers/TokenAPI.spec.js +++ b/test/integration/controllers/TokenAPI.spec.js @@ -8,7 +8,7 @@ const events = new EventEmitter(); const DeviceHive = require('../../../index'); const httpDeviceHive = new DeviceHive(config.server.http); -const wsDeviceHive = new DeviceHive(config.server.ws) +const wsDeviceHive = new DeviceHive(config.server.ws); const testToken = { diff --git a/test/integration/controllers/UserAPI.spec.js b/test/integration/controllers/UserAPI.spec.js index 088c911..5d81fc7 100644 --- a/test/integration/controllers/UserAPI.spec.js +++ b/test/integration/controllers/UserAPI.spec.js @@ -8,7 +8,7 @@ const events = new EventEmitter(); const DeviceHive = require('../../../index'); const httpDeviceHive = new DeviceHive(config.server.http); -const wsDeviceHive = new DeviceHive(config.server.ws) +const wsDeviceHive = new DeviceHive(config.server.ws); const testUsers = [ @@ -159,7 +159,7 @@ describe('UserAPI', () => { login: 'login', loginPattern: 'loginPattern', role: '1', - status: '1', + status: '1' }); Promise.all([httpDeviceHive.user.count(userListQuery), wsDeviceHive.user.count(userListQuery)]) diff --git a/test/unit/controllers/ConfigurationAPI.http.spec.js b/test/unit/controllers/ConfigurationAPI.http.spec.js index d26a741..e99c46d 100644 --- a/test/unit/controllers/ConfigurationAPI.http.spec.js +++ b/test/unit/controllers/ConfigurationAPI.http.spec.js @@ -50,7 +50,8 @@ describe('ConfigurationAPI HTTP', () => { login: `dhadmin`, password: `dhadmin_#911`, mainServiceURL: 'http://localhost:3390', - authServiceURL: 'http://localhost:3391' + authServiceURL: 'http://localhost:3391', + autoUpdateSession: false }); deviceHive.connect() diff --git a/test/unit/controllers/ConfigurationAPI.ws.spec.js b/test/unit/controllers/ConfigurationAPI.ws.spec.js index 8cc4734..072afd1 100644 --- a/test/unit/controllers/ConfigurationAPI.ws.spec.js +++ b/test/unit/controllers/ConfigurationAPI.ws.spec.js @@ -40,7 +40,8 @@ describe('ConfigurationAPI WS', () => { deviceHive = new DeviceHive({ login: `dhadmin`, password: `dhadmin_#911`, - mainServiceURL: 'ws://localhost:4390' + mainServiceURL: 'ws://localhost:4390', + autoUpdateSession: false }); deviceHive.connect() diff --git a/test/unit/controllers/DeviceAPI.http.spec.js b/test/unit/controllers/DeviceAPI.http.spec.js index 3705d0e..3ab1849 100644 --- a/test/unit/controllers/DeviceAPI.http.spec.js +++ b/test/unit/controllers/DeviceAPI.http.spec.js @@ -50,7 +50,8 @@ describe('DeviceAPI', () => { login: `dhadmin`, password: `dhadmin_#911`, mainServiceURL: 'http://localhost:3390', - authServiceURL: 'http://localhost:3391' + authServiceURL: 'http://localhost:3391', + autoUpdateSession: false }); deviceHive.connect() diff --git a/test/unit/controllers/DeviceAPI.ws.spec.js b/test/unit/controllers/DeviceAPI.ws.spec.js index 3ae25d4..33cff90 100644 --- a/test/unit/controllers/DeviceAPI.ws.spec.js +++ b/test/unit/controllers/DeviceAPI.ws.spec.js @@ -40,7 +40,8 @@ describe('DeviceAPI WS', () => { deviceHive = new DeviceHive({ login: `dhadmin`, password: `dhadmin_#911`, - mainServiceURL: 'ws://localhost:4390' + mainServiceURL: 'ws://localhost:4390', + autoUpdateSession: false }); deviceHive.connect() diff --git a/test/unit/controllers/DeviceCommandAPI.http.spec.js b/test/unit/controllers/DeviceCommandAPI.http.spec.js index cabedca..4cf17a6 100644 --- a/test/unit/controllers/DeviceCommandAPI.http.spec.js +++ b/test/unit/controllers/DeviceCommandAPI.http.spec.js @@ -50,7 +50,8 @@ describe('DeviceCommandAPI HTTP', () => { login: `dhadmin`, password: `dhadmin_#911`, mainServiceURL: 'http://localhost:3390', - authServiceURL: 'http://localhost:3391' + authServiceURL: 'http://localhost:3391', + autoUpdateSession: false }); deviceHive.connect() diff --git a/test/unit/controllers/DeviceCommandAPI.ws.spec.js b/test/unit/controllers/DeviceCommandAPI.ws.spec.js index 364e317..a690828 100644 --- a/test/unit/controllers/DeviceCommandAPI.ws.spec.js +++ b/test/unit/controllers/DeviceCommandAPI.ws.spec.js @@ -40,7 +40,8 @@ describe('DeviceCommandAPI WS', () => { deviceHive = new DeviceHive({ login: `dhadmin`, password: `dhadmin_#911`, - mainServiceURL: 'ws://localhost:4390' + mainServiceURL: 'ws://localhost:4390', + autoUpdateSession: false }); deviceHive.connect() diff --git a/test/unit/controllers/DeviceNotificationAPI.http.spec.js b/test/unit/controllers/DeviceNotificationAPI.http.spec.js index 9332d71..26fa6fb 100644 --- a/test/unit/controllers/DeviceNotificationAPI.http.spec.js +++ b/test/unit/controllers/DeviceNotificationAPI.http.spec.js @@ -50,7 +50,8 @@ describe('NotificationAPI HTTP', () => { login: `dhadmin`, password: `dhadmin_#911`, mainServiceURL: 'http://localhost:3390', - authServiceURL: 'http://localhost:3391' + authServiceURL: 'http://localhost:3391', + autoUpdateSession: false }); deviceHive.connect() diff --git a/test/unit/controllers/DeviceNotificationAPI.ws.spec.js b/test/unit/controllers/DeviceNotificationAPI.ws.spec.js index 52558b3..0386e3d 100644 --- a/test/unit/controllers/DeviceNotificationAPI.ws.spec.js +++ b/test/unit/controllers/DeviceNotificationAPI.ws.spec.js @@ -40,7 +40,8 @@ describe('NotificationAPI WS', () => { deviceHive = new DeviceHive({ login: `dhadmin`, password: `dhadmin_#911`, - mainServiceURL: 'ws://localhost:4390' + mainServiceURL: 'ws://localhost:4390', + autoUpdateSession: false }); deviceHive.connect() diff --git a/test/unit/controllers/DeviceTypeAPI.http.spec.js b/test/unit/controllers/DeviceTypeAPI.http.spec.js index ca56fa0..89c44da 100644 --- a/test/unit/controllers/DeviceTypeAPI.http.spec.js +++ b/test/unit/controllers/DeviceTypeAPI.http.spec.js @@ -50,7 +50,8 @@ describe('DeviceTypeAPI HTTP', () => { login: `dhadmin`, password: `dhadmin_#911`, mainServiceURL: 'http://localhost:3390', - authServiceURL: 'http://localhost:3391' + authServiceURL: 'http://localhost:3391', + autoUpdateSession: false }); deviceHive.connect() diff --git a/test/unit/controllers/DeviceTypeAPI.ws.spec.js b/test/unit/controllers/DeviceTypeAPI.ws.spec.js index 9d67940..f041ff2 100644 --- a/test/unit/controllers/DeviceTypeAPI.ws.spec.js +++ b/test/unit/controllers/DeviceTypeAPI.ws.spec.js @@ -40,7 +40,8 @@ describe('DeviceTypeAPI WS', () => { deviceHive = new DeviceHive({ login: `dhadmin`, password: `dhadmin_#911`, - mainServiceURL: 'ws://localhost:4390' + mainServiceURL: 'ws://localhost:4390', + autoUpdateSession: false }); deviceHive.connect() diff --git a/test/unit/controllers/NetworkAPI.http.spec.js b/test/unit/controllers/NetworkAPI.http.spec.js index a60633e..b654370 100644 --- a/test/unit/controllers/NetworkAPI.http.spec.js +++ b/test/unit/controllers/NetworkAPI.http.spec.js @@ -50,7 +50,8 @@ describe('NetworkAPI', () => { login: `dhadmin`, password: `dhadmin_#911`, mainServiceURL: 'http://localhost:3390', - authServiceURL: 'http://localhost:3391' + authServiceURL: 'http://localhost:3391', + autoUpdateSession: false }); deviceHive.connect() diff --git a/test/unit/controllers/NetworkAPI.ws.spec.js b/test/unit/controllers/NetworkAPI.ws.spec.js index 73f3e8e..a330a88 100644 --- a/test/unit/controllers/NetworkAPI.ws.spec.js +++ b/test/unit/controllers/NetworkAPI.ws.spec.js @@ -40,7 +40,8 @@ describe('NetworkAPI WS', () => { deviceHive = new DeviceHive({ login: `dhadmin`, password: `dhadmin_#911`, - mainServiceURL: 'ws://localhost:4390' + mainServiceURL: 'ws://localhost:4390', + autoUpdateSession: false }); deviceHive.connect() diff --git a/test/unit/controllers/PluginAPI.http.spec.js b/test/unit/controllers/PluginAPI.http.spec.js index a7f8a39..fda4d71 100644 --- a/test/unit/controllers/PluginAPI.http.spec.js +++ b/test/unit/controllers/PluginAPI.http.spec.js @@ -73,6 +73,7 @@ describe('PluginAPI HTTP', () => { mainServiceURL: 'http://localhost:3390', authServiceURL: 'http://localhost:3391', pluginServiceURL: 'http://localhost:3392', + autoUpdateSession: false }); deviceHive.connect() diff --git a/test/unit/controllers/ServerInfoAPI.http.spec.js b/test/unit/controllers/ServerInfoAPI.http.spec.js index 152ab7b..0f9a048 100644 --- a/test/unit/controllers/ServerInfoAPI.http.spec.js +++ b/test/unit/controllers/ServerInfoAPI.http.spec.js @@ -50,7 +50,8 @@ describe('InfoAPI HTTP', () => { login: `dhadmin`, password: `dhadmin_#911`, mainServiceURL: 'http://localhost:3390', - authServiceURL: 'http://localhost:3391' + authServiceURL: 'http://localhost:3391', + autoUpdateSession: false }); deviceHive.connect() diff --git a/test/unit/controllers/ServerInfoAPI.ws.spec.js b/test/unit/controllers/ServerInfoAPI.ws.spec.js index 5f61a50..d41e76f 100644 --- a/test/unit/controllers/ServerInfoAPI.ws.spec.js +++ b/test/unit/controllers/ServerInfoAPI.ws.spec.js @@ -40,7 +40,8 @@ describe('InfoAPI WS', () => { deviceHive = new DeviceHive({ login: `dhadmin`, password: `dhadmin_#911`, - mainServiceURL: 'ws://localhost:4390' + mainServiceURL: 'ws://localhost:4390', + autoUpdateSession: false }); deviceHive.connect() diff --git a/test/unit/controllers/TokenAPI.http.spec.js b/test/unit/controllers/TokenAPI.http.spec.js index aa58873..e75fb20 100644 --- a/test/unit/controllers/TokenAPI.http.spec.js +++ b/test/unit/controllers/TokenAPI.http.spec.js @@ -51,7 +51,8 @@ describe('TokenAPI HTTP', () => { login: `dhadmin`, password: `dhadmin_#911`, mainServiceURL: 'http://localhost:3390', - authServiceURL: 'http://localhost:3391' + authServiceURL: 'http://localhost:3391', + autoUpdateSession: false }); deviceHive.connect() diff --git a/test/unit/controllers/TokenAPI.ws.spec.js b/test/unit/controllers/TokenAPI.ws.spec.js index de64cca..a1a455a 100644 --- a/test/unit/controllers/TokenAPI.ws.spec.js +++ b/test/unit/controllers/TokenAPI.ws.spec.js @@ -40,7 +40,8 @@ describe('TokenAPI WS', () => { deviceHive = new DeviceHive({ login: `dhadmin`, password: `dhadmin_#911`, - mainServiceURL: 'ws://localhost:4390' + mainServiceURL: 'ws://localhost:4390', + autoUpdateSession: false }); deviceHive.connect() diff --git a/test/unit/controllers/UserAPI.http.spec.js b/test/unit/controllers/UserAPI.http.spec.js index 2f34cc0..a69244a 100644 --- a/test/unit/controllers/UserAPI.http.spec.js +++ b/test/unit/controllers/UserAPI.http.spec.js @@ -50,7 +50,8 @@ describe('UserAPI HTTP', () => { login: `dhadmin`, password: `dhadmin_#911`, mainServiceURL: 'http://localhost:3390', - authServiceURL: 'http://localhost:3391' + authServiceURL: 'http://localhost:3391', + autoUpdateSession: false }); deviceHive.connect() diff --git a/test/unit/controllers/UserAPI.ws.spec.js b/test/unit/controllers/UserAPI.ws.spec.js index ec91fb6..0b33f0b 100644 --- a/test/unit/controllers/UserAPI.ws.spec.js +++ b/test/unit/controllers/UserAPI.ws.spec.js @@ -40,7 +40,8 @@ describe('NetworkAPI WS', () => { deviceHive = new DeviceHive({ login: `dhadmin`, password: `dhadmin_#911`, - mainServiceURL: 'ws://localhost:4390' + mainServiceURL: 'ws://localhost:4390', + autoUpdateSession: false }); deviceHive.connect()