diff --git a/package-lock.json b/package-lock.json index 305e6f1..50c6c3e 100644 --- a/package-lock.json +++ b/package-lock.json @@ -687,6 +687,11 @@ "integrity": "sha1-FziZ0//Rx9k4PkR5Ul2+J4yrXys=", "dev": true }, + "array-uniq": { + "version": "1.0.2", + "resolved": "https://registry.npmjs.org/array-uniq/-/array-uniq-1.0.2.tgz", + "integrity": "sha1-X8w3OSB3VyPP1k1lxkvvU7+eum0=" + }, "array-unique": { "version": "0.2.1", "resolved": "https://registry.npmjs.org/array-unique/-/array-unique-0.2.1.tgz", @@ -3425,6 +3430,14 @@ "safe-buffer": "5.1.1" } }, + "randomstring": { + "version": "1.1.5", + "resolved": "https://registry.npmjs.org/randomstring/-/randomstring-1.1.5.tgz", + "integrity": "sha1-bfBij3XL1ZMpMNn+OrTpVqGFGMM=", + "requires": { + "array-uniq": "1.0.2" + } + }, "read-only-stream": { "version": "2.0.0", "resolved": "https://registry.npmjs.org/read-only-stream/-/read-only-stream-2.0.0.tgz", diff --git a/package.json b/package.json index 686702e..b55b983 100644 --- a/package.json +++ b/package.json @@ -19,6 +19,7 @@ "isomorphic-fetch": "^2.2.1", "jwt-decode": "^2.2.0", "query-string": "^5.1.0", + "randomstring": "^1.1.5", "string-template": "^1.0.0", "universal-websocket-client": "^1.0.1" }, diff --git a/src/ApiStrategy.js b/src/ApiStrategy.js index 73a0296..85d2b55 100644 --- a/src/ApiStrategy.js +++ b/src/ApiStrategy.js @@ -50,7 +50,13 @@ class ApiStrategy extends EventEmitter { me.strategy = new (ApiStrategy.getType(mainServiceURL))({ mainServiceURL, authServiceURL, pluginServiceURL }); - me.strategy.on(`message`, (message) => { me.emit(`message`, message) }); + me.strategy.on(`message`, (message) => { + if (message.subscriptionId && message.action) { + me.emit(`message`, message[message.action.split(`/`)[0]]); + } else { + me.emit(`message`, message); + } + }); } @@ -96,6 +102,15 @@ class ApiStrategy extends EventEmitter { } }); } + + /** + * Disconnects transport + */ + disconnect() { + const me = this; + + me.strategy.disconnect(); + } } diff --git a/src/DeviceHive.js b/src/DeviceHive.js index e606e23..82e3c5a 100644 --- a/src/DeviceHive.js +++ b/src/DeviceHive.js @@ -181,6 +181,16 @@ class DeviceHive extends EventEmitter { return me; } + + /** + * Disconnects from DeviceHive server + * @returns {*|void} + */ + disconnect() { + const me = this; + + return me.strategy.disconnect(); + } } diff --git a/src/models/query/NotificationListQuery.js b/src/models/query/NotificationListQuery.js index 8fdc3ba..01ac544 100644 --- a/src/models/query/NotificationListQuery.js +++ b/src/models/query/NotificationListQuery.js @@ -13,20 +13,18 @@ class NotificationListQuery extends BaseModel { * @param {string} options.start - Start timestamp * @param {string} options.end - End timestamp * @param {string} options.notification - Notification name - * @param {string} options.status - Command status * @param {string} options.sortField - Sort field * @param {string} options.sortOrder - Sort order * @param {number} options.take - Limit param * @param {number} options.skip - Skip param */ - constructor({ deviceId, start, end, notification, status, sortField, sortOrder, take, skip } = {}) { + constructor({ deviceId, start, end, notification, sortField, sortOrder, take, skip } = {}) { super(); this.deviceId = deviceId; this.start = start; this.end = end; this.notification = notification; - this.status = status; this.sortField = sortField; this.sortOrder = sortOrder; this.take = take; @@ -65,14 +63,6 @@ class NotificationListQuery extends BaseModel { this._notification = value; } - get status() { - return this._status; - } - - set status(value) { - this._status = value; - } - get sortField() { return this._sortField; } @@ -115,7 +105,6 @@ class NotificationListQuery extends BaseModel { start: this.start, end: this.end, notification: this.notification, - status: this.status, sortField: this.sortField, sortOrder: this.sortOrder, take: this.take, diff --git a/src/transports/HTTP.js b/src/transports/HTTP.js index d5ab869..cba6109 100644 --- a/src/transports/HTTP.js +++ b/src/transports/HTTP.js @@ -123,6 +123,15 @@ class HTTP extends Transport { return headers; } + + /** + * Disconnects HTTP transport + */ + disconnect() { + const me = this; + + me.token= ``; + } } diff --git a/src/transports/WS.js b/src/transports/WS.js index 2e88f24..4c6b792 100644 --- a/src/transports/WS.js +++ b/src/transports/WS.js @@ -98,6 +98,15 @@ class WS extends Transport { }); }); } + + /** + * Disconnects WS transport + */ + disconnect() { + const me = this; + + me.socket.close(); + } } diff --git a/src/transports/base/Transport.js b/src/transports/base/Transport.js index 0d832af..1e06051 100644 --- a/src/transports/base/Transport.js +++ b/src/transports/base/Transport.js @@ -20,6 +20,13 @@ class Transport extends EventEmitter { send() { console.warn(`Method "send" should be implemented in nested classes`); } + + /** + * Disconnects transport + */ + disconnect() { + console.warn(`Method "disconnect" should be implemented in nested classes`); + } } diff --git a/test/integration/config.json b/test/integration/config.json index dab5212..5711a14 100644 --- a/test/integration/config.json +++ b/test/integration/config.json @@ -1,4 +1,7 @@ { + "TEST_USER_ID": 1, + "TEST_USER_LOGIN": "dhadmin", + "TEST_USER_PASSWORD": "dhadmin_#911", "server": { "http": { "login": "dhadmin", diff --git a/test/integration/controllers/ConfigurationAPI.spec.js b/test/integration/controllers/ConfigurationAPI.spec.js index 508479e..2d9d7dc 100644 --- a/test/integration/controllers/ConfigurationAPI.spec.js +++ b/test/integration/controllers/ConfigurationAPI.spec.js @@ -1,66 +1,97 @@ const chai = require(`chai`); const assert = chai.assert; const config = require('../config'); - -const EventEmitter = require('events'); -const events = new EventEmitter(); - const DeviceHive = require('../../../index'); +const Configuration = DeviceHive.models.Configuration; const httpDeviceHive = new DeviceHive(config.server.http); const wsDeviceHive = new DeviceHive(config.server.ws); -const testConfigurations = [ - { - name: 'myTestName', - value: 'string' +const TEST_CONFIGURATIONS = { + HTTP: { + name: `myTestConfigurationName-HTTP}`, + value: `myTestConfigurationValue-HTTP}` }, - { - name: 'myTestName2', - value: 'string' + WS: { + name: `myTestConfigurationName-WS}`, + value: `myTestConfigurationValue-WS}` } -]; +}; describe('ConfigurationAPI', () => { before(done => { - // Configaratuion DeviceHive Promise.all([httpDeviceHive.connect(), wsDeviceHive.connect()]) .then(() => done()); }); + it(`should add new configuration with name ${TEST_CONFIGURATIONS.HTTP.name} and value ${TEST_CONFIGURATIONS.HTTP.value} via HTTP`, (done) => { + const configurationModel = new Configuration(TEST_CONFIGURATIONS.HTTP); - it('ConfigurationAPI.put()', done => { - - // Configurating Configaration model - const configuration = new DeviceHive.models.Configuration(testConfigurations[0]); - const configuration2 = new DeviceHive.models.Configuration(testConfigurations[1]); - - Promise.all([httpDeviceHive.configuration.put(configuration), wsDeviceHive.configuration.put(configuration2)]) + httpDeviceHive.configuration.put(configurationModel) .then(() => done()) .catch(done); }); + it(`should add new configuration with name ${TEST_CONFIGURATIONS.WS.name} and value ${TEST_CONFIGURATIONS.WS.value} via WS`, (done) => { + const configurationModel = new Configuration(TEST_CONFIGURATIONS.WS); - it('ConfigurationAPI.get()', done => { + wsDeviceHive.configuration.put(configurationModel) + .then(() => done()) + .catch(done); + }); - Promise.all([httpDeviceHive.configuration.get(testConfigurations[0].name), wsDeviceHive.configuration.get(testConfigurations[1].name)]) - .then(dataAll => { - for (const key in dataAll) { - assert.isObject(dataAll[key]) - assert.include(dataAll[key], testConfigurations[key]); - } + it(`should get configuration with name ${TEST_CONFIGURATIONS.HTTP.name} and value ${TEST_CONFIGURATIONS.HTTP.value} via HTTP`, done => { + httpDeviceHive.configuration.get(TEST_CONFIGURATIONS.HTTP.name) + .then(configuration => { + assert.isObject(configuration); + assert.equal(configuration.name, TEST_CONFIGURATIONS.HTTP.name); + assert.equal(configuration.value, TEST_CONFIGURATIONS.HTTP.value); }) - .then(done) + .then(() => done()) .catch(done); }); + it(`should get configuration with name ${TEST_CONFIGURATIONS.WS.name} and value ${TEST_CONFIGURATIONS.WS.value} via WS`, done => { + wsDeviceHive.configuration.get(TEST_CONFIGURATIONS.WS.name) + .then(configuration => { + assert.isObject(configuration); + assert.equal(configuration.name, TEST_CONFIGURATIONS.WS.name); + assert.equal(configuration.value, TEST_CONFIGURATIONS.WS.value); + }) + .then(() => done()) + .catch(done); + }); - it('ConfigurationAPI.delete()', done => { + it(`should delete configuration with name ${TEST_CONFIGURATIONS.HTTP.name} and value ${TEST_CONFIGURATIONS.HTTP.value} via HTTP`, done => { + httpDeviceHive.configuration.delete(TEST_CONFIGURATIONS.HTTP.name) + .then(() => done()) + .catch(done); + }); - Promise.all([httpDeviceHive.configuration.delete(testConfigurations[0].name), wsDeviceHive.configuration.delete(testConfigurations[1].name)]) + it(`should delete configuration with name ${TEST_CONFIGURATIONS.WS.name} and value ${TEST_CONFIGURATIONS.WS.value} via WS`, done => { + wsDeviceHive.configuration.delete(TEST_CONFIGURATIONS.WS.name) .then(() => done()) .catch(done); }); + + it(`should not get configuration with name ${TEST_CONFIGURATIONS.HTTP.name} and value ${TEST_CONFIGURATIONS.HTTP.value} via HTTP`, done => { + httpDeviceHive.configuration.get(TEST_CONFIGURATIONS.HTTP.name) + .then(() => done(new Error(`Configuration exists after deletion`))) + .catch(() => done()); + }); + + it(`should not get configuration with name ${TEST_CONFIGURATIONS.WS.name} and value ${TEST_CONFIGURATIONS.WS.value} via WS`, done => { + wsDeviceHive.configuration.get(TEST_CONFIGURATIONS.WS.name) + .then(() => done(new Error(`Configuration exists after deletion`))) + .catch(() => done()); + }); + + after(done => { + httpDeviceHive.disconnect(); + wsDeviceHive.disconnect(); + + done(); + }); }); \ No newline at end of file diff --git a/test/integration/controllers/Device.spec.js b/test/integration/controllers/Device.spec.js index e0329d7..7f349f3 100644 --- a/test/integration/controllers/Device.spec.js +++ b/test/integration/controllers/Device.spec.js @@ -1,118 +1,142 @@ const chai = require(`chai`); const assert = chai.assert; const config = require('../config'); - -const EventEmitter = require('events'); -const events = new EventEmitter(); - const DeviceHive = require('../../../index'); +const Device = DeviceHive.models.Device; +const DeviceListQuery = DeviceHive.models.query.DeviceListQuery; const httpDeviceHive = new DeviceHive(config.server.http); const wsDeviceHive = new DeviceHive(config.server.ws); -const testDevices = [ - { - id: 'myTestId', - name: 'myTestName', +const TEST_DEVICE_ID_PREFIX = `DH-JS-LIB-DEVICE-ID-`; +const TEST_DEVICE_NAME_PREFIX = `DH-JS-LIB-DEVICE-NAME-`; +const TEST_DEVICES = { + HTTP: { + id: `${TEST_DEVICE_ID_PREFIX}HTTP`, + name: `${TEST_DEVICE_NAME_PREFIX}HTTP`, networkId: 1, deviceTypeId: 1, - isBlocked: false - }, { - id: 'myTestId2', - name: 'myTestName', + isBlocked: false, + data: {} + }, + WS: { + id: `${TEST_DEVICE_ID_PREFIX}WS`, + name: `${TEST_DEVICE_NAME_PREFIX}WS`, networkId: 1, deviceTypeId: 1, - isBlocked: false + isBlocked: false, + data: {} } -]; +}; describe('DeviceAPI', () => { before(done => { - // Configaratuion DeviceHive - Promise.all([httpDeviceHive.connect(), wsDeviceHive.connect()]) .then(() => done()); }); + it(`should add new device with next configuration: ${JSON.stringify(TEST_DEVICES.HTTP)} via HTTP`, done => { + const deviceModel = new Device(TEST_DEVICES.HTTP); - it('DeviceAPI.add()', done => { - - // Configurating Device model - const Device = DeviceHive.models.Device; + httpDeviceHive.device.add(deviceModel) + .then(() => done()) + .catch(done); + }); - const device = new Device(testDevices[0]); - const device2 = new Device(testDevices[1]); + it(`should add new device with next configuration: ${JSON.stringify(TEST_DEVICES.WS)} via WS`, done => { + const deviceModel = new Device(TEST_DEVICES.WS); - Promise.all([httpDeviceHive.device.add(device), wsDeviceHive.device.add(device2)]) + wsDeviceHive.device.add(deviceModel) .then(() => done()) .catch(done); }); + it(`should get device with next configuration: ${JSON.stringify(TEST_DEVICES.HTTP)} via HTTP`, done => { + httpDeviceHive.device.get(TEST_DEVICES.HTTP.id) + .then(device => { + assert.isObject(device); + assert.deepEqual(device, TEST_DEVICES.HTTP); + }) + .then(() => done()) + .catch(done); + }); - it('DeviceAPI.get()', done => { + it(`should get device with next configuration: ${JSON.stringify(TEST_DEVICES.WS)} via WS`, done => { + wsDeviceHive.device.get(TEST_DEVICES.WS.id) + .then(device => { + assert.isObject(device); + assert.deepEqual(device, TEST_DEVICES.WS); + }) + .then(() => done()) + .catch(done); + }); - const expected = { - deviceId: testDevices[0].id - }; + it(`should list all devices with the next name pattern "${TEST_DEVICE_NAME_PREFIX}%" via HTTP`, done => { + const deviceListQuery = new DeviceListQuery({ namePattern: `${TEST_DEVICE_NAME_PREFIX}%` }); - Promise.all([httpDeviceHive.device.get(expected.deviceId), wsDeviceHive.device.get(expected.deviceId)]) - .then(dataAll => { - for (const data of dataAll) { - assert.isObject(data) - assert.include(data, testDevices[0]); - } + httpDeviceHive.device.list(deviceListQuery) + .then(devices => { + assert.equal(devices.length, Object.keys(TEST_DEVICES).length); }) - .then(done) + .then(() => done()) .catch(done); }); + it(`should list all devices with the next name pattern "${TEST_DEVICE_NAME_PREFIX}%" via WS`, done => { + const deviceListQuery = new DeviceListQuery({ namePattern: `${TEST_DEVICE_NAME_PREFIX}%` }); - it('DeviceAPI.list()', done => { - - // Configurating Device List query - const deviceListQuery = new DeviceHive.models.query.DeviceListQuery({ - networkId: 1 - }); + wsDeviceHive.device.list(deviceListQuery) + .then(devices => { + assert.equal(devices.length, Object.keys(TEST_DEVICES).length); + }) + .then(() => done()) + .catch(done); + }); + it(`should count devices with the next name pattern "${TEST_DEVICE_NAME_PREFIX}%" via HTTP`, done => { + const deviceListQuery = new DeviceListQuery({ namePattern: `${TEST_DEVICE_NAME_PREFIX}%` }); - Promise.all([httpDeviceHive.device.list(deviceListQuery), wsDeviceHive.device.list(deviceListQuery)]) - .then(dataAll => { - for (const data of dataAll) { - for (const device of data) { - assert.containsAllKeys(device, Object.keys(testDevices[0])); - } - } + httpDeviceHive.device.count(deviceListQuery) + .then(response => { + assert.equal(response.count, Object.keys(TEST_DEVICES).length); }) .then(done) .catch(done); - }); + it(`should count devices with the next name pattern "${TEST_DEVICE_NAME_PREFIX}%" via WS`, done => { + const deviceListQuery = new DeviceListQuery({ namePattern: `${TEST_DEVICE_NAME_PREFIX}%` }); - it('DeviceAPI.count()', done => { - - // Configurating Device List query - const deviceListQuery = new DeviceHive.models.query.DeviceCountQuery({ - networkId: '1' - }); - - Promise.all([httpDeviceHive.device.count(deviceListQuery), wsDeviceHive.device.count(deviceListQuery)]) - .then(dataAll => { - for (const data of dataAll) { - assert.property(data, 'count'); - } + wsDeviceHive.device.count(deviceListQuery) + .then(response => { + assert.equal(response.count, Object.keys(TEST_DEVICES).length); }) .then(done) .catch(done); }); + it(`should delete device with next configuration: ${JSON.stringify(TEST_DEVICES.HTTP)} via HTTP`, done => { + const deviceModel = new Device(TEST_DEVICES.HTTP); + + httpDeviceHive.device.delete(deviceModel.id) + .then(() => done()) + .catch(done); + }); - it('DeviceAPI.delete()', done => { + it(`should delete device with next configuration: ${JSON.stringify(TEST_DEVICES.WS)} via WS`, done => { + const deviceModel = new Device(TEST_DEVICES.WS); - Promise.all([httpDeviceHive.device.delete(testDevices[0].id), wsDeviceHive.device.delete(testDevices[1].id)]) + wsDeviceHive.device.delete(deviceModel.id) .then(() => done()) .catch(done); }); + + after(done => { + httpDeviceHive.disconnect(); + wsDeviceHive.disconnect(); + + done(); + }); }); \ No newline at end of file diff --git a/test/integration/controllers/DeviceCommandAPI.spec.js b/test/integration/controllers/DeviceCommandAPI.spec.js index eba276c..d1e4e52 100644 --- a/test/integration/controllers/DeviceCommandAPI.spec.js +++ b/test/integration/controllers/DeviceCommandAPI.spec.js @@ -1,25 +1,36 @@ +const randomString = require(`randomstring`); const chai = require(`chai`); const assert = chai.assert; const config = require('../config'); - -const EventEmitter = require('events'); -const events = new EventEmitter(); - const DeviceHive = require('../../../index'); +const Device = DeviceHive.models.Device; +const Command = DeviceHive.models.Command; +const CommandListQuery = DeviceHive.models.query.CommandListQuery; +const CommandPollQuery = DeviceHive.models.query.CommandPollQuery; +const CommandPollManyQuery = DeviceHive.models.query.CommandPollManyQuery; +const CommandWaitQuery = DeviceHive.models.query.CommandWaitQuery; const httpDeviceHive = new DeviceHive(config.server.http); const wsDeviceHive = new DeviceHive(config.server.ws); -const deviceId = 'e50d6085-2aba-48e9-b1c3-73c673e414be'; -const testDeviceCommands = [ - { - deviceId, - command: 'command', - timestamp: new Date().toISOString(), - lastUpdated: new Date().toISOString(), +const TIMESTAMP = new Date().toISOString(); +const DH_COMMANDS_TEST_DEVICE = { + id: randomString.generate(), + name: `DH_COMMANDS_TEST_DEVICE`, + networkId: 1, + deviceTypeId: 1, + isBlocked: false, + data: {} +}; +const TEST_DEVICE_COMMANDS = { + HTTP: { + deviceId: DH_COMMANDS_TEST_DEVICE.id, + command: `command-${randomString.generate()}`, + timestamp: TIMESTAMP, userId: 1, networkId: 1, + deviceTypeId: 1, parameters: { jsonString: 'jsonString' }, @@ -29,13 +40,13 @@ const testDeviceCommands = [ jsonString: 'jsonString' } }, - { - deviceId, - command: 'command2', - timestamp: new Date().toISOString(), - lastUpdated: new Date().toISOString(), + WS: { + deviceId: DH_COMMANDS_TEST_DEVICE.id, + command: `command-${randomString.generate()}`, + timestamp: TIMESTAMP, userId: 1, networkId: 1, + deviceTypeId: 1, parameters: { jsonString: 'jsonString' }, @@ -45,141 +56,262 @@ const testDeviceCommands = [ jsonString: 'jsonString' } } -]; +}; describe('DeviceCommandAPI', () => { before(done => { - // Configaratuion DeviceHive Promise.all([httpDeviceHive.connect(), wsDeviceHive.connect()]) - .then(() => done()); + .then(() => httpDeviceHive.device.add(new Device(DH_COMMANDS_TEST_DEVICE))) + .then(() => done()) + .catch(done); }); + it(`should insert new command with name ${TEST_DEVICE_COMMANDS.HTTP.command} via HTTP`, done => { + const commandModel = new Command(TEST_DEVICE_COMMANDS.HTTP); - it('DeviceCommandAPI.insert()', done => { - const command = new DeviceHive.models.Command(testDeviceCommands[0]); - const command2 = new DeviceHive.models.Command(testDeviceCommands[1]); + httpDeviceHive.command.insert(DH_COMMANDS_TEST_DEVICE.id, commandModel) + .then((commandResponse) => { + TEST_DEVICE_COMMANDS.HTTP.id = commandResponse.id; - Promise.all([httpDeviceHive.command.insert(deviceId, command), wsDeviceHive.command.insert(deviceId, command2)]) - .then(() => done()) + done(); + }) .catch(done); }); + it(`should insert new command with name ${TEST_DEVICE_COMMANDS.WS.command} via WS`, done => { + const commandModel = new Command(TEST_DEVICE_COMMANDS.WS); - it('DeviceCommandAPI.list()', done => { + wsDeviceHive.command.insert(DH_COMMANDS_TEST_DEVICE.id, commandModel) + .then((commandResponse) => { + TEST_DEVICE_COMMANDS.WS.id = commandResponse.id; - // Configurating Device List query - const commandListQuery = new DeviceHive.models.query.CommandListQuery({ - deviceId, - command: 'command', - status: 'status', - sortField: 'id', - sortOrder: 'id', - take: 2, - skip: 0 + done(); + }) + .catch(done); + }); + + it(`should list all commands for device with id ${DH_COMMANDS_TEST_DEVICE.id} via HTTP`, done => { + const commandListQuery = new CommandListQuery({ + deviceId: DH_COMMANDS_TEST_DEVICE.id }); - Promise.all([httpDeviceHive.command.list(commandListQuery), wsDeviceHive.command.list(commandListQuery)]) - .then(dataAll => { - for (const data of dataAll) { - for (const deviceCommandKey in data) { - testDeviceCommands[deviceCommandKey].id = data[deviceCommandKey].id; - testDeviceCommands[deviceCommandKey].timestamp = data[deviceCommandKey].timestamp; - testDeviceCommands[deviceCommandKey].lastUpdated = data[deviceCommandKey].lastUpdated; - assert.containsAllKeys(data[deviceCommandKey], Object.keys(testDeviceCommands[0])); - } - } + httpDeviceHive.command.list(commandListQuery) + .then(commands => { + assert.equal(commands.length, Object.keys(TEST_DEVICE_COMMANDS).length); }) - .then(done) + .then(() => done()) .catch(done); }); + it(`should list all commands for device with id ${DH_COMMANDS_TEST_DEVICE.id} via WS`, done => { + const commandListQuery = new CommandListQuery({ + deviceId: DH_COMMANDS_TEST_DEVICE.id + }); - it('DeviceCommand API.get()', done => { + wsDeviceHive.command.list(commandListQuery) + .then(commands => { + assert.equal(commands.length, Object.keys(TEST_DEVICE_COMMANDS).length); + }) + .then(() => done()) + .catch(done); + }); + it(`should get command with id ${TEST_DEVICE_COMMANDS.HTTP.id} for device with id ${DH_COMMANDS_TEST_DEVICE.id} via HTTP`, done => { + httpDeviceHive.command.get(DH_COMMANDS_TEST_DEVICE.id, TEST_DEVICE_COMMANDS.HTTP.id) + .then(command => { + assert.equal(command.id, TEST_DEVICE_COMMANDS.HTTP.id); + }) + .then(done) + .catch(done); + }); - Promise.all([httpDeviceHive.command.get(deviceId, testDeviceCommands[0].id), wsDeviceHive.command.get(deviceId, testDeviceCommands[0].id)]) - .then(dataAll => { - const expected = testDeviceCommands[0]; - for (const data of dataAll) { - assert.isObject(data); - assert.deepInclude(data, expected); - } + it(`should get command with id ${TEST_DEVICE_COMMANDS.WS.id} for device with id ${DH_COMMANDS_TEST_DEVICE.id} via WS`, done => { + wsDeviceHive.command.get(DH_COMMANDS_TEST_DEVICE.id, TEST_DEVICE_COMMANDS.WS.id) + .then(command => { + assert.equal(command.id, TEST_DEVICE_COMMANDS.WS.id); }) .then(done) .catch(done); }); + it(`should update command with id ${TEST_DEVICE_COMMANDS.HTTP.id} for device with id ${DH_COMMANDS_TEST_DEVICE.id} via HTTP`, done => { + TEST_DEVICE_COMMANDS.HTTP.status = `status-${randomString.generate()}`; - it('DeviceCommandAPI.update()', done => { + const commandModel = new Command(TEST_DEVICE_COMMANDS.HTTP); - const command = new DeviceHive.models.Command(testDeviceCommands[0], testDeviceCommands[0]); - const command2 = new DeviceHive.models.Command(testDeviceCommands[0], testDeviceCommands[1]); + httpDeviceHive.command.update(commandModel) + .then(() => httpDeviceHive.command.get(DH_COMMANDS_TEST_DEVICE.id, TEST_DEVICE_COMMANDS.HTTP.id)) + .then((command) => { + assert.equal(command.status, TEST_DEVICE_COMMANDS.HTTP.status); - Promise.all([httpDeviceHive.command.update(command), wsDeviceHive.command.update(command2)]) - .then(() => done()) + done(); + }) .catch(done); }); + it(`should update command with id ${TEST_DEVICE_COMMANDS.HTTP.id} for device with id ${DH_COMMANDS_TEST_DEVICE.id} via WS`, done => { + TEST_DEVICE_COMMANDS.WS.status = `status-${randomString.generate()}`; - it('DeviceCommandAPI.poll()', done => { + const commandModel = new Command(TEST_DEVICE_COMMANDS.WS); - // Configurating Command List query - const commandPollQuery = new DeviceHive.models.query.CommandPollQuery({ - deviceId, - returnUpdatedCommands: true, - limit: 1, + wsDeviceHive.command.update(commandModel) + .then(() => wsDeviceHive.command.get(DH_COMMANDS_TEST_DEVICE.id, TEST_DEVICE_COMMANDS.WS.id)) + .then((command) => { + assert.equal(command.status, TEST_DEVICE_COMMANDS.WS.status); + + done(); + }) + .catch(done); + }); + + it(`should poll new command for device with id ${DH_COMMANDS_TEST_DEVICE.id} via HTTP`, done => { + const commandPollQuery = new CommandPollQuery({ + deviceId: DH_COMMANDS_TEST_DEVICE.id, waitTimeout: 1 }); httpDeviceHive.command.poll(commandPollQuery) - .then(() => done()) + .then((commands) => { + assert.equal(commands.length, 1); + assert.equal(commands[0].command, TEST_DEVICE_COMMANDS.HTTP.command); + + TEST_DEVICE_COMMANDS.HTTP.id = commands[0].id; + done(); + }) .catch(done); - // emit command setTimeout(() => { - const command = new DeviceHive.models.Command(testDeviceCommands[0]); - httpDeviceHive.command.insert(deviceId, command); - }, 50); + TEST_DEVICE_COMMANDS.HTTP.command = `command-${randomString.generate()}`; + httpDeviceHive.command.insert(DH_COMMANDS_TEST_DEVICE.id, new Command(TEST_DEVICE_COMMANDS.HTTP)); + }, 100); }); + it(`should poll command update for device with id ${DH_COMMANDS_TEST_DEVICE.id} via HTTP`, done => { + const commandPollQuery = new CommandPollQuery({ + deviceId: DH_COMMANDS_TEST_DEVICE.id, + returnUpdatedCommands: true, + waitTimeout: 1 + }); - it('DeviceCommandAPI.pollMany()', done => { + httpDeviceHive.command.poll(commandPollQuery) + .then((commands) => { + assert.equal(commands.length, 1); + assert.equal(commands[0].command, TEST_DEVICE_COMMANDS.HTTP.command); + assert.equal(commands[0].status, TEST_DEVICE_COMMANDS.HTTP.status); - const commandPollManyQuery = new DeviceHive.models.query.CommandPollManyQuery({ - deviceIds: deviceId + TEST_DEVICE_COMMANDS.HTTP.id = commands[0].id; + done(); + }) + .catch(done); + + setTimeout(() => { + TEST_DEVICE_COMMANDS.HTTP.status = `status-${randomString.generate()}`; + httpDeviceHive.command.update(new Command(TEST_DEVICE_COMMANDS.HTTP)); + }, 100); + }); + + it(`should poll new command for network with id ${DH_COMMANDS_TEST_DEVICE.networkId} via HTTP`, done => { + const commandPollManyQuery = new CommandPollManyQuery({ + networkIds: [ DH_COMMANDS_TEST_DEVICE.networkId ], + waitTimeout: 1 }); httpDeviceHive.command.pollMany(commandPollManyQuery) - .then(() => done()) + .then((commands) => { + assert.equal(commands.length, 1); + assert.equal(commands[0].command, TEST_DEVICE_COMMANDS.HTTP.command); + + done(); + }) .catch(done); - // emit command setTimeout(() => { - const command = new DeviceHive.models.Command(testDeviceCommands[0]); - httpDeviceHive.command.insert(deviceId, command); - }, 50); + TEST_DEVICE_COMMANDS.HTTP.command = `command-${randomString.generate()}`; + httpDeviceHive.command.insert(DH_COMMANDS_TEST_DEVICE.id, new Command(TEST_DEVICE_COMMANDS.HTTP)); + }, 100); }); + it(`should wait while command will be processed by device with id ${DH_COMMANDS_TEST_DEVICE.id}`, done => { + const commandWaitQuery = new CommandWaitQuery({ waitTimeout: 1 }); + + httpDeviceHive.command.wait(DH_COMMANDS_TEST_DEVICE.id, TEST_DEVICE_COMMANDS.HTTP.id, commandWaitQuery) + .then((command) => { + assert.equal(command.status, TEST_DEVICE_COMMANDS.HTTP.status); + + done() + }) + .catch(done); + + setTimeout(() => { + TEST_DEVICE_COMMANDS.HTTP.command = `command-${randomString.generate()}`; - it('DeviceCommandAPI.wait()', done => { + httpDeviceHive.command.insert(DH_COMMANDS_TEST_DEVICE.id, new Command(TEST_DEVICE_COMMANDS.HTTP)) + .then(() => { + TEST_DEVICE_COMMANDS.HTTP.status = `status-${randomString.generate()}`; - // TODO - done(); + httpDeviceHive.command.update(new Command(TEST_DEVICE_COMMANDS.HTTP)); + }); + }, 100); + }); - // Configurating Command List query - // const commandWaitQuery = new DeviceHive.models.query.CommandWaitQuery({ - // deviceId, - // commandId: testDeviceCommands[0].id, - // waitTimeout: 1 - // }); + it(`should subscribe for command insertion notifications on device with id ${DH_COMMANDS_TEST_DEVICE.id} via HTTP`, done => { + const commandPollQuery = new CommandPollQuery({ deviceId: DH_COMMANDS_TEST_DEVICE.id }); + let subscriptionId; + + httpDeviceHive.command.subscribe(commandPollQuery) + .then((response) => { + subscriptionId = response.subscriptionId; + + setTimeout(() => { + httpDeviceHive.on(`message`, (command) => { + assert.equal(command.command, TEST_DEVICE_COMMANDS.HTTP.command); + httpDeviceHive.command.unsubscribe(subscriptionId) + .then(() => done()) + .catch(done); + }); + }, 200); + + setTimeout(() => { + TEST_DEVICE_COMMANDS.HTTP.command = `command-${randomString.generate()}`; + httpDeviceHive.command.insert(DH_COMMANDS_TEST_DEVICE.id, new Command(TEST_DEVICE_COMMANDS.HTTP)); + }, 300); + }) + .catch(done); + }); + + it(`should subscribe for command insertion notifications on device with id ${DH_COMMANDS_TEST_DEVICE.id} via WS`, done => { + const commandPollQuery = new CommandPollQuery({ deviceId: DH_COMMANDS_TEST_DEVICE.id }); + let subscriptionId; + + wsDeviceHive.command.subscribe(commandPollQuery) + .then((response) => { + subscriptionId = response.subscriptionId; + + setTimeout(() => { + wsDeviceHive.on(`message`, (command) => { + assert.equal(command.command, TEST_DEVICE_COMMANDS.WS.command); + wsDeviceHive.command.unsubscribe(subscriptionId) + .then(() => done()) + .catch(done); + }); + }, 200); + + setTimeout(() => { + TEST_DEVICE_COMMANDS.WS.command = `command-${randomString.generate()}`; + wsDeviceHive.command.insert(DH_COMMANDS_TEST_DEVICE.id, new Command(TEST_DEVICE_COMMANDS.WS)); + }, 300); + }) + .catch(done); + }); - // httpDeviceHive.command.wait(commandWaitQuery.deviceId, commandWaitQuery.commandId, commandWaitQuery) - // .then(() => done()) - // .catch(done); + after(done => { + httpDeviceHive.device.delete(DH_COMMANDS_TEST_DEVICE.id) + .then(() => { + httpDeviceHive.disconnect(); + wsDeviceHive.disconnect(); - // // emit command - // const command = new DeviceHive.models.Command(testDeviceCommands[0]); - // httpDeviceHive.command.insert(deviceId, command); + done(); + }); }); }); \ No newline at end of file diff --git a/test/integration/controllers/DeviceNotificationAPI.spec.js b/test/integration/controllers/DeviceNotificationAPI.spec.js index c4d42a8..f58124c 100644 --- a/test/integration/controllers/DeviceNotificationAPI.spec.js +++ b/test/integration/controllers/DeviceNotificationAPI.spec.js @@ -1,135 +1,227 @@ +const randomString = require(`randomstring`); const chai = require(`chai`); const assert = chai.assert; const config = require('../config'); - -const EventEmitter = require('events'); -const events = new EventEmitter(); - const DeviceHive = require('../../../index'); +const Device = DeviceHive.models.Device; +const Notification = DeviceHive.models.Notification; +const NotificationListQuery = DeviceHive.models.query.NotificationListQuery; +const NotificationPollQuery = DeviceHive.models.query.NotificationPollQuery; +const NotificationPollManyQuery = DeviceHive.models.query.NotificationPollManyQuery; const httpDeviceHive = new DeviceHive(config.server.http); const wsDeviceHive = new DeviceHive(config.server.ws); -const deviceId = 'e50d6085-2aba-48e9-b1c3-73c673e414be'; -const testDeviceNotifications = [ - { - deviceId, - notification: 'notification', - timestamp: new Date().toISOString(), +const TIMESTAMP = new Date().toISOString(); +const DH_NOTIFICATIONS_TEST_DEVICE = { + id: randomString.generate(), + name: `DH_COMMANDS_TEST_DEVICE`, + networkId: 1, + deviceTypeId: 1, + isBlocked: false, + data: {} +}; +const TEST_DEVICE_NOTIFICATIONS = { + HTTP: { + deviceId: DH_NOTIFICATIONS_TEST_DEVICE.id, + notification: `notification-${randomString.generate()}`, + timestamp: TIMESTAMP, parameters: { jsonString: 'jsonString' } }, - { - deviceId, - notification: 'notification', - timestamp: new Date().toISOString(), + WS: { + deviceId: DH_NOTIFICATIONS_TEST_DEVICE.id, + notification: `notification-${randomString.generate()}`, + timestamp: TIMESTAMP, parameters: { jsonString: 'jsonString' } } -]; +}; describe('NotificationAPI', () => { before(done => { - // Configaratuion DeviceHive Promise.all([httpDeviceHive.connect(), wsDeviceHive.connect()]) - .then(() => done()); + .then(() => httpDeviceHive.device.add(new Device(DH_NOTIFICATIONS_TEST_DEVICE))) + .then(() => done()) + .catch(done); }); + it(`should insert new notification with name ${TEST_DEVICE_NOTIFICATIONS.HTTP.notification} via HTTP`, done => { + const notificationModel = new Notification(TEST_DEVICE_NOTIFICATIONS.HTTP); - it('NotificationAPI.insert()', done => { - - const notification = new DeviceHive.models.Notification(testDeviceNotifications[0]); - const notification2 = new DeviceHive.models.Notification(testDeviceNotifications[1]); + httpDeviceHive.notification.insert(DH_NOTIFICATIONS_TEST_DEVICE.id, notificationModel) + .then((notification) => { + TEST_DEVICE_NOTIFICATIONS.HTTP.id = notification.id; - Promise.all([httpDeviceHive.notification.insert(deviceId, notification), wsDeviceHive.notification.insert(deviceId, notification2)]) - .then(() => done()) + done(); + }) .catch(done); }); + it(`should insert new notification with name ${TEST_DEVICE_NOTIFICATIONS.WS.notification} via WS`, done => { + const notificationModel = new Notification(TEST_DEVICE_NOTIFICATIONS.WS); - it('NotificationAPI.list()', done => { - - // Configurating Device List query - const notificationListQuery = new DeviceHive.models.query.NotificationListQuery({ - deviceId, - notification: 'notification', - status: 'status', - sortField: 'id', - sortOrder: 'id', - take: 2, - skip: 0 - }); + wsDeviceHive.notification.insert(DH_NOTIFICATIONS_TEST_DEVICE.id, notificationModel) + .then((notification) => { + TEST_DEVICE_NOTIFICATIONS.WS.id = notification.id; - Promise.all([httpDeviceHive.notification.list(notificationListQuery), wsDeviceHive.notification.list(notificationListQuery)]) - .then(dataAll => { - for (const data of dataAll) { - for (const deviceNotificationKey in data) { - testDeviceNotifications[deviceNotificationKey].id = data[deviceNotificationKey].id; - testDeviceNotifications[deviceNotificationKey].timestamp = data[deviceNotificationKey].timestamp; - assert.containsAllKeys(data[deviceNotificationKey], Object.keys(testDeviceNotifications[0])); - } - } + done(); }) - .then(done) .catch(done); }); + it(`should list all notifications for device with id ${DH_NOTIFICATIONS_TEST_DEVICE.id} via HTTP`, done => { + const notificationListQuery = new NotificationListQuery({ + deviceId: DH_NOTIFICATIONS_TEST_DEVICE.id, + notification: TEST_DEVICE_NOTIFICATIONS.HTTP.notification + }); + + setTimeout(() => { + httpDeviceHive.notification.list(notificationListQuery) + .then(notifications => { + assert.equal(notifications[0].notification, TEST_DEVICE_NOTIFICATIONS.HTTP.notification); + }) + .then(() => done()) + .catch(done); + }, 200); + }); + + it(`should list all notifications for device with id ${DH_NOTIFICATIONS_TEST_DEVICE.id} via WS`, done => { + const notificationListQuery = new NotificationListQuery({ + deviceId: DH_NOTIFICATIONS_TEST_DEVICE.id, + notification: TEST_DEVICE_NOTIFICATIONS.WS.notification + }); - it('NotificationAPI.get()', done => { + setTimeout(() => { + wsDeviceHive.notification.list(notificationListQuery) + .then(notifications => { + assert.equal(notifications[0].notification, TEST_DEVICE_NOTIFICATIONS.WS.notification); + }) + .then(() => done()) + .catch(done); + }, 200); + }); - Promise.all([httpDeviceHive.notification.get(deviceId, testDeviceNotifications[0].id), wsDeviceHive.notification.get(deviceId, testDeviceNotifications[0].id)]) - .then(dataAll => { - const expected = testDeviceNotifications[0]; - for (const data of dataAll) { - assert.isObject(data); - assert.deepInclude(data, expected); - } + it(`should get notification with id ${TEST_DEVICE_NOTIFICATIONS.HTTP.id} for device with id ${DH_NOTIFICATIONS_TEST_DEVICE.id} via HTTP`, done => { + httpDeviceHive.notification.get(DH_NOTIFICATIONS_TEST_DEVICE.id, TEST_DEVICE_NOTIFICATIONS.HTTP.id) + .then(notification => { + assert.equal(notification.id, TEST_DEVICE_NOTIFICATIONS.HTTP.id); }) - .then(done) + .then(() => done()) .catch(done); }); + it(`should get notification with id ${TEST_DEVICE_NOTIFICATIONS.WS.id} for device with id ${DH_NOTIFICATIONS_TEST_DEVICE.id} via WS`, done => { + wsDeviceHive.notification.get(DH_NOTIFICATIONS_TEST_DEVICE.id, TEST_DEVICE_NOTIFICATIONS.WS.id) + .then(command => { + assert.equal(command.id, TEST_DEVICE_NOTIFICATIONS.WS.id); + }) + .then(() => done()) + .catch(done); + }); - it('NotificationAPI.poll()', done => { - - // Configurating Notification List query - const notificationPollQuery = new DeviceHive.models.query.NotificationPollQuery({ - deviceId, - returnUpdatedNotifications: true, - limit: 1, + it(`should poll new notification for device with id ${DH_NOTIFICATIONS_TEST_DEVICE.id} via HTTP`, done => { + const notificationPollQuery = new NotificationPollQuery({ + deviceId: DH_NOTIFICATIONS_TEST_DEVICE.id, waitTimeout: 1 }); httpDeviceHive.notification.poll(notificationPollQuery) - .then(() => done()) + .then((notifications) => { + assert.equal(notifications.length, 1); + assert.equal(notifications[0].notification, TEST_DEVICE_NOTIFICATIONS.HTTP.notification); + + TEST_DEVICE_NOTIFICATIONS.HTTP.id = notifications[0].id; + done(); + }) .catch(done); - // emit notification setTimeout(() => { - const notification = new DeviceHive.models.Notification(testDeviceNotifications[0]); - httpDeviceHive.notification.insert(deviceId, notification); - }, 50); + TEST_DEVICE_NOTIFICATIONS.HTTP.notification = `notification-${randomString.generate()}`; + httpDeviceHive.notification.insert(DH_NOTIFICATIONS_TEST_DEVICE.id, new Notification(TEST_DEVICE_NOTIFICATIONS.HTTP)); + }, 100); }); - - it('NotificationAPI.pollMany()', done => { - - const notificationPollManyQuery = new DeviceHive.models.query.NotificationPollManyQuery({ - deviceIds: deviceId + it(`should poll new notification for network with id ${DH_NOTIFICATIONS_TEST_DEVICE.networkId} via HTTP`, done => { + const notificationPollManyQuery = new NotificationPollManyQuery({ + networkIds: [ DH_NOTIFICATIONS_TEST_DEVICE.networkId ], + waitTimeout: 1 }); httpDeviceHive.notification.pollMany(notificationPollManyQuery) - .then(() => done()) + .then((notifications) => { + assert.equal(notifications.length, 1); + assert.equal(notifications[0].notification, TEST_DEVICE_NOTIFICATIONS.HTTP.notification); + + done(); + }) .catch(done); - // emit notification setTimeout(() => { - const notification = new DeviceHive.models.Notification(testDeviceNotifications[0]); - httpDeviceHive.notification.insert(deviceId, notification); - }, 50); + TEST_DEVICE_NOTIFICATIONS.HTTP.notification = `notification-${randomString.generate()}`; + httpDeviceHive.notification.insert(DH_NOTIFICATIONS_TEST_DEVICE.id, new Notification(TEST_DEVICE_NOTIFICATIONS.HTTP)); + }, 100); + }); + + it(`should subscribe for notification insertion notifications on device with id ${DH_NOTIFICATIONS_TEST_DEVICE.id} via HTTP`, done => { + const notificationPollQuery = new NotificationPollQuery({ deviceId: DH_NOTIFICATIONS_TEST_DEVICE.id }); + let subscriptionId; + + httpDeviceHive.notification.subscribe(notificationPollQuery) + .then((response) => { + subscriptionId = response.subscriptionId; + + httpDeviceHive.on(`message`, (command) => { + assert.equal(command.notification, TEST_DEVICE_NOTIFICATIONS.HTTP.notification); + httpDeviceHive.notification.unsubscribe(subscriptionId) + .then(() => done()) + .catch(done); + }); + + setTimeout(() => { + TEST_DEVICE_NOTIFICATIONS.HTTP.notification = `command-${randomString.generate()}`; + httpDeviceHive.notification.insert(DH_NOTIFICATIONS_TEST_DEVICE.id, new Notification(TEST_DEVICE_NOTIFICATIONS.HTTP)); + }, 100); + }) + .catch(done); + }); + + it(`should subscribe for notification insertion notifications on device with id ${DH_NOTIFICATIONS_TEST_DEVICE.id} via WS`, done => { + const notificationPollQuery = new NotificationPollQuery({ deviceId: DH_NOTIFICATIONS_TEST_DEVICE.id }); + let subscriptionId; + + wsDeviceHive.notification.subscribe(notificationPollQuery) + .then((response) => { + subscriptionId = response.subscriptionId; + + setTimeout(() => { + wsDeviceHive.on(`message`, (command) => { + assert.equal(command.notification, TEST_DEVICE_NOTIFICATIONS.WS.notification); + wsDeviceHive.notification.unsubscribe(subscriptionId) + .then(() => done()) + .catch(done); + }); + }, 200); + + setTimeout(() => { + TEST_DEVICE_NOTIFICATIONS.WS.notification = `command-${randomString.generate()}`; + wsDeviceHive.notification.insert(DH_NOTIFICATIONS_TEST_DEVICE.id, new Notification(TEST_DEVICE_NOTIFICATIONS.WS)); + }, 300); + }) + .catch(done); + }); + + after(done => { + httpDeviceHive.device.delete(DH_NOTIFICATIONS_TEST_DEVICE.id) + .then(() => { + httpDeviceHive.disconnect(); + wsDeviceHive.disconnect(); + + done(); + }); }); }); \ No newline at end of file diff --git a/test/integration/controllers/DeviceTypeAPI.spec.js b/test/integration/controllers/DeviceTypeAPI.spec.js index cb6412d..e41b088 100644 --- a/test/integration/controllers/DeviceTypeAPI.spec.js +++ b/test/integration/controllers/DeviceTypeAPI.spec.js @@ -1,130 +1,167 @@ +const randomString = require(`randomstring`); const chai = require(`chai`); const assert = chai.assert; const config = require('../config'); - -const EventEmitter = require('events'); -const events = new EventEmitter(); - const DeviceHive = require('../../../index'); +const DeviceType = DeviceHive.models.DeviceType; +const DeviceTypeListQuery = DeviceHive.models.query.DeviceTypeListQuery; +const DeviceTypeCountQuery = DeviceHive.models.query.DeviceTypeCountQuery; const httpDeviceHive = new DeviceHive(config.server.http); const wsDeviceHive = new DeviceHive(config.server.ws); -const testDeviceTypes = [ - { - name: 'name', - description: 'description' - }, { - name: 'name2', - description: 'description2' +const TEST_DEVICE_TYPE_NAME_PREFIX = `DH-JS-LIB-DEVICE-TYPE-NAME-`; +const TEST_DEVICE_TYPE_DESCRIPTION_PREFIX = `DH-JS-LIB-DEVICE-TYPE-NAME-`; +const TEST_DEVICE_TYPES = { + HTTP: { + name: `${TEST_DEVICE_TYPE_NAME_PREFIX}-${randomString.generate()}`, + description: `${TEST_DEVICE_TYPE_DESCRIPTION_PREFIX}-${randomString.generate()}` + }, + WS: { + name: `${TEST_DEVICE_TYPE_NAME_PREFIX}-${randomString.generate()}`, + description: `${TEST_DEVICE_TYPE_DESCRIPTION_PREFIX}-${randomString.generate()}` } -]; +}; describe('DeviceTypeAPI', () => { before(done => { - // Configaratuion DeviceHive - Promise.all([httpDeviceHive.connect(), wsDeviceHive.connect()]) .then(() => done()); }); + it(`should insert new device type with next configuration: ${JSON.stringify(TEST_DEVICE_TYPES.HTTP)} via HTTP`, done => { + const deviceTypeModel = new DeviceType(TEST_DEVICE_TYPES.HTTP); - it('DeviceTypeAPI.insert()', done => { - - // Configurating DeviceType model - const DeviceType = DeviceHive.models.DeviceType; + httpDeviceHive.deviceType.insert(deviceTypeModel) + .then(({ id }) => { + TEST_DEVICE_TYPES.HTTP.id = id; + done(); + }) + .catch(done); + }); - const deviceType = new DeviceType(testDeviceTypes[0]); - const deviceType2 = new DeviceType(testDeviceTypes[1]); + it(`should insert new device type with next configuration: ${JSON.stringify(TEST_DEVICE_TYPES.WS)} via WS`, done => { + const deviceTypeModel = new DeviceType(TEST_DEVICE_TYPES.WS); - Promise.all([httpDeviceHive.deviceType.insert(deviceType), wsDeviceHive.deviceType.insert(deviceType2)]) - .then(() => done()) + wsDeviceHive.deviceType.insert(deviceTypeModel) + .then(({ id }) => { + TEST_DEVICE_TYPES.WS.id = id; + done(); + }) .catch(done); }); + it(`should list all device types with the next name pattern: ${TEST_DEVICE_TYPE_NAME_PREFIX}% via HTTP`, done => { + const deviceTypeListQuery = new DeviceTypeListQuery({ namePattern: `${TEST_DEVICE_TYPE_NAME_PREFIX}%` }); - it('DeviceTypeAPI.list()', done => { + httpDeviceHive.deviceType.list(deviceTypeListQuery) + .then(deviceTypes => { + assert(deviceTypes.length, Object.keys(TEST_DEVICE_TYPES).length); + }) + .then(() => done()) + .catch(done); + }); + it(`should list all device types with the next name pattern: ${TEST_DEVICE_TYPE_NAME_PREFIX}% via WS`, done => { + const deviceTypeListQuery = new DeviceTypeListQuery({ namePattern: `${TEST_DEVICE_TYPE_NAME_PREFIX}%` }); - // Configurating Device List query - const deviceTypeListQuery = new DeviceHive.models.query.DeviceTypeListQuery({ - namePattern: 'name%', - sortField: 'name', - sortOrder: 'asc', - take: 2, - skip: 0 - }); + wsDeviceHive.deviceType.list(deviceTypeListQuery) + .then(deviceTypes => { + assert(deviceTypes.length, Object.keys(TEST_DEVICE_TYPES).length); + }) + .then(() => done()) + .catch(done); + }); + it(`should get device type with name: ${TEST_DEVICE_TYPES.HTTP.name} via HTTP`, done => { + httpDeviceHive.deviceType.get(TEST_DEVICE_TYPES.HTTP.id) + .then(deviceType => { + assert.equal(deviceType.id, TEST_DEVICE_TYPES.HTTP.id); + assert.equal(deviceType.name, TEST_DEVICE_TYPES.HTTP.name); + }) + .then(() => done()) + .catch(done); + }); - Promise.all([httpDeviceHive.deviceType.list(deviceTypeListQuery), wsDeviceHive.deviceType.list(deviceTypeListQuery)]) - .then(dataAll => { - for (const data of dataAll) { - for (const deviceTypeKey in data) { - testDeviceTypes[deviceTypeKey].id = data[deviceTypeKey].id; - testDeviceTypes[deviceTypeKey].name = data[deviceTypeKey].name; - testDeviceTypes[deviceTypeKey].description = data[deviceTypeKey].description; - assert.containsAllKeys(data[deviceTypeKey], Object.keys(testDeviceTypes[0])); - } - } + it(`should get device type with name: ${TEST_DEVICE_TYPES.WS.name} via WS`, done => { + wsDeviceHive.deviceType.get(TEST_DEVICE_TYPES.WS.id) + .then(deviceType => { + assert.equal(deviceType.id, TEST_DEVICE_TYPES.WS.id); + assert.equal(deviceType.name, TEST_DEVICE_TYPES.WS.name); }) - .then(done) + .then(() => done()) .catch(done); }); + it(`should update device type with name: ${TEST_DEVICE_TYPES.HTTP.name} via HTTP`, done => { + TEST_DEVICE_TYPES.HTTP.description = `${TEST_DEVICE_TYPE_DESCRIPTION_PREFIX}-${randomString.generate()}`; - it('DeviceTypeAPI.get()', done => { + const deviceTypeModel = new DeviceType(TEST_DEVICE_TYPES.HTTP); - Promise.all([httpDeviceHive.deviceType.get(testDeviceTypes[0].id), wsDeviceHive.deviceType.get(testDeviceTypes[0].id)]) - .then(dataAll => { - const expected = testDeviceTypes[0]; - for (const data of dataAll) { - assert.isObject(data); - assert.deepInclude(data, expected); - } + httpDeviceHive.deviceType.update(deviceTypeModel) + .then(() => httpDeviceHive.deviceType.get(TEST_DEVICE_TYPES.HTTP.id)) + .then((deviceType) => { + assert.equal(deviceType.description, TEST_DEVICE_TYPES.HTTP.description); }) - .then(done) + .then(() => done()) .catch(done); + }); + it(`should update device type with name: ${TEST_DEVICE_TYPES.WS.name} via WS`, done => { + TEST_DEVICE_TYPES.WS.description = `${TEST_DEVICE_TYPE_DESCRIPTION_PREFIX}-${randomString.generate()}`; - it('DeviceTypeAPI.update()', done => { + const deviceTypeModel = new DeviceType(TEST_DEVICE_TYPES.WS); - // Configurating DeviceType model - const DeviceType = DeviceHive.models.DeviceType; + wsDeviceHive.deviceType.update(deviceTypeModel) + .then(() => wsDeviceHive.deviceType.get(TEST_DEVICE_TYPES.WS.id)) + .then((deviceType) => { + assert.equal(deviceType.description, TEST_DEVICE_TYPES.WS.description); + }) + .then(() => done()) + .catch(done); + }); - const deviceType = new DeviceType(testDeviceTypes[0]); - const deviceType2 = new DeviceType(testDeviceTypes[1]); + it(`should count device types with the next name pattern: ${TEST_DEVICE_TYPE_NAME_PREFIX}% via HTTP`, done => { + const deviceTypeCountQuery = new DeviceTypeCountQuery({ namePattern: `${TEST_DEVICE_TYPE_NAME_PREFIX}%` }); - Promise.all([httpDeviceHive.deviceType.update(deviceType), wsDeviceHive.deviceType.update(deviceType2)]) + httpDeviceHive.deviceType.count(deviceTypeCountQuery) + .then(({ count }) => { + assert.equal(count, Object.keys(TEST_DEVICE_TYPES).length); + }) .then(() => done()) .catch(done); - }); - it('DeviceTypeAPI.count()', done => { + it(`should count device types with the next name pattern: ${TEST_DEVICE_TYPE_NAME_PREFIX}% via WS`, done => { + const deviceTypeCountQuery = new DeviceTypeCountQuery({ namePattern: `${TEST_DEVICE_TYPE_NAME_PREFIX}%` }); - // Configurating DeviceType List query - const deviceTypeListQuery = new DeviceHive.models.query.DeviceTypeCountQuery({ - name: 'name', - namePattern: 'namePattern' - }); - - Promise.all([httpDeviceHive.deviceType.count(deviceTypeListQuery), wsDeviceHive.deviceType.count(deviceTypeListQuery)]) - .then(dataAll => { - for (const data of dataAll) { - assert.property(data, 'count'); - } + wsDeviceHive.deviceType.count(deviceTypeCountQuery) + .then(({ count }) => { + assert.equal(count, Object.keys(TEST_DEVICE_TYPES).length); }) - .then(done) + .then(() => done()) .catch(done); }); - it('DeviceTypeAPI.delete()', done => { - - Promise.all([httpDeviceHive.deviceType.delete(testDeviceTypes[0].id), wsDeviceHive.deviceType.delete(testDeviceTypes[1].id)]) + it(`should delete device type with name: ${TEST_DEVICE_TYPES.HTTP.name} via HTTP`, done => { + httpDeviceHive.deviceType.delete(TEST_DEVICE_TYPES.HTTP.id) .then(() => done()) .catch(done); }); + + it(`should delete device type with name: ${TEST_DEVICE_TYPES.WS.name} via WS`, done => { + wsDeviceHive.deviceType.delete(TEST_DEVICE_TYPES.WS.id) + .then(() => done()) + .catch(done); + }); + + after(done => { + httpDeviceHive.disconnect(); + wsDeviceHive.disconnect(); + + done(); + }); }); \ No newline at end of file diff --git a/test/integration/controllers/NetworkAPI.spec.js b/test/integration/controllers/NetworkAPI.spec.js index 2ce51e1..54378c7 100644 --- a/test/integration/controllers/NetworkAPI.spec.js +++ b/test/integration/controllers/NetworkAPI.spec.js @@ -1,129 +1,167 @@ +const randomString = require(`randomstring`); const chai = require(`chai`); const assert = chai.assert; const config = require('../config'); - -const EventEmitter = require('events'); -const events = new EventEmitter(); - const DeviceHive = require('../../../index'); +const Network = DeviceHive.models.Network; +const NetworkListQuery = DeviceHive.models.query.NetworkListQuery; +const NetworkCountQuery = DeviceHive.models.query.NetworkCountQuery; const httpDeviceHive = new DeviceHive(config.server.http); const wsDeviceHive = new DeviceHive(config.server.ws); -const testNetworks = [ - { - name: 'name', - description: 'description' - }, { - name: 'name2', - description: 'description2' +const TEST_NETWORK_NAME_PREFIX = `DH-JS-LIB-NETWORK-NAME-`; +const TEST_NETWORK_DESCRIPTION_PREFIX = `DH-JS-LIB-NETWORK-NAME-`; +const TEST_NETWORKS = { + HTTP: { + name: `${TEST_NETWORK_NAME_PREFIX}-${randomString.generate()}`, + description: `${TEST_NETWORK_DESCRIPTION_PREFIX}-${randomString.generate()}` + }, + WS: { + name: `${TEST_NETWORK_NAME_PREFIX}-${randomString.generate()}`, + description: `${TEST_NETWORK_DESCRIPTION_PREFIX}-${randomString.generate()}` } -]; +}; describe('NetworkAPI', () => { before(done => { - // Configaratuion DeviceHive - Promise.all([httpDeviceHive.connect(), wsDeviceHive.connect()]) .then(() => done()); }); + it(`should insert new network with next configuration: ${JSON.stringify(TEST_NETWORKS.HTTP)} via HTTP`, done => { + const networkModel = new Network(TEST_NETWORKS.HTTP); + + httpDeviceHive.network.insert(networkModel) + .then(({ id }) => { + TEST_NETWORKS.HTTP.id = id; + done(); + }) + .catch(done); + }); - it('NetworkAPI.insert()', done => { + it(`should insert new network with next configuration: ${JSON.stringify(TEST_NETWORKS.WS)} via WS`, done => { + const networkModel = new Network(TEST_NETWORKS.WS); - // Configurating Network model - const Network = DeviceHive.models.Network; + wsDeviceHive.network.insert(networkModel) + .then(({ id }) => { + TEST_NETWORKS.WS.id = id; + done(); + }) + .catch(done); + }); - const network = new Network(testNetworks[0]); - const network2 = new Network(testNetworks[1]); + it(`should list all device types with the next name pattern: ${TEST_NETWORK_NAME_PREFIX}% via HTTP`, done => { + const networkListQuery = new NetworkListQuery({ namePattern: `${TEST_NETWORK_NAME_PREFIX}%` }); - Promise.all([httpDeviceHive.network.insert(network), wsDeviceHive.network.insert(network2)]) + httpDeviceHive.network.list(networkListQuery) + .then(networks => { + assert(networks.length, Object.keys(TEST_NETWORKS).length); + }) .then(() => done()) .catch(done); }); + it(`should list all device types with the next name pattern: ${TEST_NETWORK_NAME_PREFIX}% via WS`, done => { + const networkListQuery = new NetworkListQuery({ namePattern: `${TEST_NETWORK_NAME_PREFIX}%` }); - it('NetworkAPI.list()', done => { - - // Configurating Device List query - const networkListQuery = new DeviceHive.models.query.NetworkListQuery({ - namePattern: 'name%', - sortField: 'name', - sortOrder: 'asc', - take: 2, - skip: 0 - }); - - - Promise.all([httpDeviceHive.network.list(networkListQuery), wsDeviceHive.network.list(networkListQuery)]) - .then(dataAll => { - for (const data of dataAll) { - for (const networkKey in data) { - testNetworks[networkKey].id = data[networkKey].id; - testNetworks[networkKey].name = data[networkKey].name; - testNetworks[networkKey].description = data[networkKey].description; - assert.containsAllKeys(data[networkKey], Object.keys(testNetworks[0])); - } - } + wsDeviceHive.network.list(networkListQuery) + .then(networks => { + assert(networks.length, Object.keys(TEST_NETWORKS).length); }) - .then(done) + .then(() => done()) .catch(done); }); + it(`should get network with name: ${TEST_NETWORKS.HTTP.name} via HTTP`, done => { + httpDeviceHive.network.get(TEST_NETWORKS.HTTP.id) + .then(network => { + assert.equal(network.id, TEST_NETWORKS.HTTP.id); + assert.equal(network.name, TEST_NETWORKS.HTTP.name); + }) + .then(() => done()) + .catch(done); + }); - it('NetworkAPI.get()', done => { - - Promise.all([httpDeviceHive.network.get(testNetworks[0].id), wsDeviceHive.network.get(testNetworks[0].id)]) - .then(dataAll => { - const expected = testNetworks[0]; - for (const data of dataAll) { - assert.isObject(data); - assert.deepInclude(data, expected); - } + it(`should get network with name: ${TEST_NETWORKS.WS.name} via WS`, done => { + wsDeviceHive.network.get(TEST_NETWORKS.WS.id) + .then(network => { + assert.equal(network.id, TEST_NETWORKS.WS.id); + assert.equal(network.name, TEST_NETWORKS.WS.name); }) - .then(done) + .then(() => done()) .catch(done); }); + it(`should update network with name: ${TEST_NETWORKS.HTTP.name} via HTTP`, done => { + TEST_NETWORKS.HTTP.description = `${TEST_NETWORK_DESCRIPTION_PREFIX}-${randomString.generate()}`; - it('NetworkAPI.update()', done => { + const networkModel = new Network(TEST_NETWORKS.HTTP); - // Configurating Network model - const Network = DeviceHive.models.Network; + httpDeviceHive.network.update(networkModel) + .then(() => httpDeviceHive.network.get(TEST_NETWORKS.HTTP.id)) + .then((network) => { + assert.equal(network.description, TEST_NETWORKS.HTTP.description); + }) + .then(() => done()) + .catch(done); + + }); - const network = new Network(testNetworks[0]); - const network2 = new Network(testNetworks[1]); + it(`should update network with name: ${TEST_NETWORKS.WS.name} via WS`, done => { + TEST_NETWORKS.WS.description = `${TEST_NETWORK_DESCRIPTION_PREFIX}-${randomString.generate()}`; - Promise.all([httpDeviceHive.network.update(network), wsDeviceHive.network.update(network2)]) + const networkModel = new Network(TEST_NETWORKS.WS); + + wsDeviceHive.network.update(networkModel) + .then(() => wsDeviceHive.network.get(TEST_NETWORKS.WS.id)) + .then((network) => { + assert.equal(network.description, TEST_NETWORKS.WS.description); + }) .then(() => done()) .catch(done); - }); - it('NetworkAPI.count()', done => { + it(`should count device types with the next name pattern: ${TEST_NETWORK_NAME_PREFIX}% via HTTP`, done => { + const networkCountQuery = new NetworkCountQuery({ namePattern: `${TEST_NETWORK_NAME_PREFIX}%` }); - // Configurating Network List query - const networkListQuery = new DeviceHive.models.query.NetworkCountQuery({ - name: 'name', - namePattern: 'namePattern' - }); + httpDeviceHive.network.count(networkCountQuery) + .then(({ count }) => { + assert.equal(count, Object.keys(TEST_NETWORKS).length); + }) + .then(() => done()) + .catch(done); + }); - Promise.all([httpDeviceHive.network.count(networkListQuery), wsDeviceHive.network.count(networkListQuery)]) - .then(dataAll => { - for (const data of dataAll) { - assert.property(data, 'count'); - } + it(`should count device types with the next name pattern: ${TEST_NETWORK_NAME_PREFIX}% via WS`, done => { + const networkCountQuery = new NetworkCountQuery({ namePattern: `${TEST_NETWORK_NAME_PREFIX}%` }); + + wsDeviceHive.network.count(networkCountQuery) + .then(({ count }) => { + assert.equal(count, Object.keys(TEST_NETWORKS).length); }) - .then(done) + .then(() => done()) .catch(done); }); - it('NetworkAPI.delete()', done => { - - Promise.all([httpDeviceHive.network.delete(testNetworks[0].id), wsDeviceHive.network.delete(testNetworks[1].id)]) + it(`should delete network with name: ${TEST_NETWORKS.HTTP.name} via HTTP`, done => { + httpDeviceHive.network.delete(TEST_NETWORKS.HTTP.id) .then(() => done()) .catch(done); }); + + it(`should delete network with name: ${TEST_NETWORKS.WS.name} via WS`, done => { + wsDeviceHive.network.delete(TEST_NETWORKS.WS.id) + .then(() => done()) + .catch(done); + }); + + after(done => { + httpDeviceHive.disconnect(); + wsDeviceHive.disconnect(); + + done(); + }); }); \ No newline at end of file diff --git a/test/integration/controllers/PluginAPI.spec.js b/test/integration/controllers/PluginAPI.spec.js index e53b5a8..088654d 100644 --- a/test/integration/controllers/PluginAPI.spec.js +++ b/test/integration/controllers/PluginAPI.spec.js @@ -1,143 +1,110 @@ +const randomString = require(`randomstring`); const chai = require(`chai`); const assert = chai.assert; -const config = require('../config'); - -const EventEmitter = require('events'); -const events = new EventEmitter(); - -const DeviceHive = require('../../../index'); +const config = require(`../config`); +const DeviceHive = require(`../../../index`); +const Plugin = DeviceHive.models.Plugin; +const PluginRegisterQuery = DeviceHive.models.query.PluginRegisterQuery; const httpDeviceHive = new DeviceHive(config.server.http); const wsDeviceHive = new DeviceHive(config.server.ws); -const testPlugins = [ - { - name: `testName${new Date().getMilliseconds()}`, - description: 'description', +const TEST_PLUGIN = { + HTTP: { + name: `plugin-${randomString.generate()}`, + description: `description`, parameters: { - jsonString: 'string' + jsonString: `string` } } -]; +}; -describe('PluginAPI', () => { +describe(`PluginAPI`, () => { before(done => { Promise.all([httpDeviceHive.connect(), wsDeviceHive.connect()]) .then(() => done()); }); - - it('PluginAPI.register()', done => { - const plugin = new DeviceHive.models.Plugin(testPlugins[0]); - const pluginQuery = new DeviceHive.models.query.PluginRegisterQuery({ - returnCommands: 'true', - returnUpdatedCommands: 'false', - returnNotifications: 'false' + it(`should register plugin with name: ${TEST_PLUGIN.HTTP.name} via HTTP`, done => { + const plugin = new Plugin(TEST_PLUGIN.HTTP); + const pluginQuery = new PluginRegisterQuery({ + returnCommands: true, + returnUpdatedCommands: false, + returnNotifications: false }); - Promise.all([httpDeviceHive.plugin.register(plugin, pluginQuery)]) - .then(dataAll => { - for (const data of dataAll) { - testPlugins[0] = Object.assign({}, testPlugins[0], data); - } + httpDeviceHive.plugin.register(plugin, pluginQuery) + .then(({ accessToken, refreshToken, topicName }) => { + assert.exists(accessToken); + assert.exists(refreshToken); + assert.exists(topicName); + + TEST_PLUGIN.HTTP.accessToken = accessToken; + TEST_PLUGIN.HTTP.refreshToken = refreshToken; + TEST_PLUGIN.HTTP.topicName = topicName; }) .then(() => done()) .catch(done); }); - it('PluginAPI.list()', done => { + it(`should get plugin with name: ${TEST_PLUGIN.HTTP.name} via HTTP`, done => { const pluginListQuery = new DeviceHive.models.query.PluginListQuery({ - take: 1, - skip: 0 + name: TEST_PLUGIN.HTTP.name }); - Promise.all([httpDeviceHive.plugin.list(pluginListQuery)]) - .then(dataAll => { - for (const data of dataAll) { - assert.isArray(data); - } + httpDeviceHive.plugin.list(pluginListQuery) + .then(plugins => { + assert.equal(plugins[0].name, TEST_PLUGIN.HTTP.name); }) .then(() => done()) .catch(done); }); + it(`should update plugin with name: ${TEST_PLUGIN.HTTP.name} via HTTP`, done => { + TEST_PLUGIN.HTTP.name = `plugin-${randomString.generate()}`; - it('PluginAPI.update()', done => { - const updatedName = `${testPlugins[0].name}-updated`; const pluginUpdateQuery = new DeviceHive.models.query.PluginUpdateQuery({ - topicName: testPlugins[0].topicName, - name: updatedName + topicName: TEST_PLUGIN.HTTP.topicName, + name: TEST_PLUGIN.HTTP.name }); const pluginListQuery = new DeviceHive.models.query.PluginListQuery({ - name: updatedName, - take: 1, - skip: 0 + name: TEST_PLUGIN.HTTP.name }); - 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); - } + httpDeviceHive.plugin.update(pluginUpdateQuery) + .then(() => httpDeviceHive.plugin.list(pluginListQuery)) + .then(plugins => { + assert.equal(plugins[0].name, TEST_PLUGIN.HTTP.name); }) .then(() => done()) .catch(done); }); - - it('PluginAPI.count()', done => { + it(`should count plugin with name: ${TEST_PLUGIN.HTTP.name} via HTTP`, done => { const pluginCountQuery = new DeviceHive.models.query.PluginCountQuery({ - status: '1' + name: TEST_PLUGIN.HTTP.name }); - Promise.all([httpDeviceHive.plugin.count(pluginCountQuery)]) - .then(dataAll => { - for (const data of dataAll) { - assert.property(data, 'count'); - } + httpDeviceHive.plugin.count(pluginCountQuery) + .then(({ count }) => { + assert.equal(count, 1); }) .then(() => done()) .catch(done); }); - it('TokenAPI.createPluginToken()', done => { - const token = new DeviceHive.models.PluginToken({ - actions: [0], - expiration: '2018-02-09T10:09:03.033Z', - type: 0, - topicName: testPlugins[0].topicName - }); - - Promise.all([httpDeviceHive.token.createPluginToken(token)]) - .then(dataAll => { - for (const data of dataAll) { - const expectedKeys = ['accessToken', 'refreshToken'] - assert.containsAllKeys(data, expectedKeys); - } - }) - .then(done) - .catch(done); - }); - - it('TokenAPI.authPlugin()', done => { - Promise.all([httpDeviceHive.token.authPlugin(testPlugins[0].accessToken)]) - .then(dataAll => { - for (const data of dataAll) { - const expectedKeys = ['tpc', 'a', 'e', 't']; - assert.containsAllKeys(data, expectedKeys) - } - }) - .then(done) + it(`should delete plugin with name: ${TEST_PLUGIN.HTTP.name}`, done => { + httpDeviceHive.plugin.delete(TEST_PLUGIN.HTTP.topicName) + .then(() => done()) .catch(done); }); - it('PluginAPI.delete()', done => { + after(done => { + httpDeviceHive.disconnect(); + wsDeviceHive.disconnect(); - Promise.all([httpDeviceHive.plugin.delete(testPlugins[0].topicName)]) - .then(() => done()) - .catch(done); + done(); }); }); \ No newline at end of file diff --git a/test/integration/controllers/ServerInfoAPI.spec.js b/test/integration/controllers/ServerInfoAPI.spec.js index 0bc96df..ecc5138 100644 --- a/test/integration/controllers/ServerInfoAPI.spec.js +++ b/test/integration/controllers/ServerInfoAPI.spec.js @@ -1,10 +1,6 @@ const chai = require(`chai`); const assert = chai.assert; const config = require('../config'); - -const EventEmitter = require('events'); -const events = new EventEmitter(); - const DeviceHive = require('../../../index'); const httpDeviceHive = new DeviceHive(config.server.http); @@ -14,48 +10,59 @@ const wsDeviceHive = new DeviceHive(config.server.ws); describe('ServerInfoAPI', () => { before(done => { - // Configaratuion DeviceHive Promise.all([httpDeviceHive.connect(), wsDeviceHive.connect()]) .then(() => done()); }); + it('should get server info via HTTP', done => { + httpDeviceHive.info.getServerInfo() + .then(serverInfo => { + assert.exists(serverInfo); + }) + .then(() => done()) + .catch(done); + }); - it('InfoAPI.getServerInfo()', done => { - - Promise.all([httpDeviceHive.info.getServerInfo(), wsDeviceHive.info.getServerInfo()]) - .then(dataAll => { - for (const data of dataAll) { - const expectedKeys = ['apiVersion', 'serverTimestamp'] - assert.isObject(data); - assert.containsAllKeys(data, expectedKeys); - } + it('should get server info via WS', done => { + wsDeviceHive.info.getServerInfo() + .then(serverInfo => { + assert.exists(serverInfo); }) - .then(done) + .then(() => done()) .catch(done); }); - it('InfoAPI.getCacheInfo()', done => { + it('should get cache info via HTTP', done => { + httpDeviceHive.info.getCacheInfo() + .then(cacheInfo => { + assert.exists(cacheInfo); + }) + .then(() => done()) + .catch(done); + }); - Promise.all([httpDeviceHive.info.getCacheInfo()]) - .then(dataAll => { - for (const data of dataAll) { - const expectedKeys = ['serverTimestamp', 'cacheStats'] - assert.isObject(data); - assert.containsAllKeys(data, expectedKeys); - } + it('should get cluster info via HTTP', done => { + httpDeviceHive.info.getClusterInfo() + .then(clusterInfo => { + assert.exists(clusterInfo); }) - .then(done) + .then(() => done()) .catch(done); }); - it('InfoAPI.getClusterInfo()', done => { - Promise.all([httpDeviceHive.info.getClusterInfo(), wsDeviceHive.info.getClusterInfo()]) - .then(dataAll => { - for (const data of dataAll) { - assert.isObject(data); - } + it('should get cluster info via WS', done => { + wsDeviceHive.info.getClusterInfo() + .then(clusterInfo => { + assert.exists(clusterInfo); }) - .then(done) + .then(() => done()) .catch(done); }); + + after(done => { + httpDeviceHive.disconnect(); + wsDeviceHive.disconnect(); + + done(); + }); }); \ No newline at end of file diff --git a/test/integration/controllers/TokenAPI.spec.js b/test/integration/controllers/TokenAPI.spec.js index 60f2fc3..afd799f 100644 --- a/test/integration/controllers/TokenAPI.spec.js +++ b/test/integration/controllers/TokenAPI.spec.js @@ -1,96 +1,169 @@ +const randomString = require(`randomstring`); const chai = require(`chai`); const assert = chai.assert; -const config = require('../config'); - -const EventEmitter = require('events'); -const events = new EventEmitter(); - -const DeviceHive = require('../../../index'); +const config = require(`../config`); +const DeviceHive = require(`../../../index`); +const UserToken = DeviceHive.models.UserToken; +const Plugin = DeviceHive.models.Plugin; +const PluginRegisterQuery = DeviceHive.models.query.PluginRegisterQuery; const httpDeviceHive = new DeviceHive(config.server.http); const wsDeviceHive = new DeviceHive(config.server.ws); -const testToken = { - login: config.server.http.login, - password: config.server.http.password +const TEST_TOKENS = { + HTTP: {}, + WS: {} +}; +const TEST_PLUGIN = { + HTTP: { + name: `plugin-${randomString.generate()}`, + description: `description`, + parameters: { + jsonString: `string` + } + } }; -describe('TokenAPI', () => { +describe(`TokenAPI`, () => { before(done => { - // Configaratuion DeviceHive + const plugin = new Plugin(TEST_PLUGIN.HTTP); + const pluginQuery = new PluginRegisterQuery({ + returnCommands: true, + returnUpdatedCommands: false, + returnNotifications: false + }); Promise.all([httpDeviceHive.connect(), wsDeviceHive.connect()]) + .then(() => httpDeviceHive.plugin.register(plugin, pluginQuery)) + .then(({ accessToken, refreshToken, topicName }) => { + TEST_PLUGIN.HTTP.accessToken = accessToken; + TEST_PLUGIN.HTTP.refreshToken = refreshToken; + TEST_PLUGIN.HTTP.topicName = topicName; + }) .then(() => done()); }); - it('TokenAPI.createUserToken()', done => { + it(`should create token for user via HTTP`, done => { + const token = new UserToken({ + userId: config.TEST_USER_ID, + actions: [`*`], + networkIds: [`*`], + deviceTypeIds: [`*`], + expiration: `2050-02-09T10:09:03.033Z` + }); + + httpDeviceHive.token.createUserToken(token) + .then(({ accessToken, refreshToken }) => { + assert.exists(accessToken); + assert.exists(refreshToken); + + TEST_TOKENS.HTTP.accessToken = accessToken; + TEST_TOKENS.HTTP.refreshToken = refreshToken; + }) + .then(() => done()) + .catch(done); + }); - // Configurating Token model - const token = new DeviceHive.models.UserToken({ - userId: 1, - actions: ['string'], - networkIds: ['string'], - deviceTypeIds: ['string'], - expiration: '2030-02-09T10:09:03.033Z' + it(`should create token for user via WS`, done => { + const token = new UserToken({ + userId: config.TEST_USER_ID, + actions: [`*`], + networkIds: [`*`], + deviceTypeIds: [`*`], + expiration: `2050-02-09T10:09:03.033Z` }); - Promise.all([httpDeviceHive.token.createUserToken(token), wsDeviceHive.token.createUserToken(token)]) - .then(dataAll => { - for (const data of dataAll) { - const expectedkeys = ['accessToken', 'refreshToken']; - assert.isObject(data); - assert.containsAllKeys(data, expectedkeys); - testToken.accessToken = data.accessToken; - testToken.refreshToken = data.refreshToken; - } + wsDeviceHive.token.createUserToken(token) + .then(({ accessToken, refreshToken }) => { + assert.exists(accessToken); + assert.exists(refreshToken); + + TEST_TOKENS.WS.accessToken = accessToken; + TEST_TOKENS.WS.refreshToken = refreshToken; }) - .then(done) + .then(() => done()) .catch(done); }); - it('TokenAPI.refresh()', done => { - - // Configurating Token model - - Promise.all([httpDeviceHive.token.refresh(testToken.refreshToken)]) - .then(dataAll => { - for (const data of dataAll) { - const expectedkeys = ['accessToken']; - assert.isObject(data); - assert.containsAllKeys(data, expectedkeys); - testToken.accessToken = data.accessToken; - testToken.refreshToken = data.refreshToken; - } + it(`should refresh user access token via HTTP`, done => { + httpDeviceHive.token.refresh(TEST_TOKENS.HTTP.refreshToken) + .then(({ accessToken }) => { + assert.exists(accessToken); + + TEST_TOKENS.WS.accessToken = accessToken; }) - .then(done) + .then(() => done()) .catch(done); + }); - // sent data - events.once('request', data => { - assert.equal(data.method, 'POST', 'Not correct method'); - assert.equal(data.url.pathname, `/token/refresh`, 'Not correct URL'); - assert.deepEqual(data.body, expectedBody, 'Not correct body'); + it(`should refresh user access token via WS`, done => { + wsDeviceHive.token.refresh(TEST_TOKENS.WS.refreshToken) + .then(({ accessToken }) => { + assert.exists(accessToken); - done(); - }); + TEST_TOKENS.WS.accessToken = accessToken; + }) + .then(() => done()) + .catch(done); + }); + + it(`should log in user with given credentials: login ${config.TEST_USER_LOGIN}, password ${config.TEST_USER_PASSWORD} via HTTP`, done => { + httpDeviceHive.token.login(config.TEST_USER_LOGIN, config.TEST_USER_PASSWORD) + .then(({ accessToken, refreshToken }) => { + assert.exists(accessToken); + assert.exists(refreshToken); + }) + .then(() => done()) + .catch(done) }); + it(`should log in user with given credentials: login ${config.TEST_USER_LOGIN}, password ${config.TEST_USER_PASSWORD} via WS`, done => { + wsDeviceHive.token.login(config.TEST_USER_LOGIN, config.TEST_USER_PASSWORD) + .then(({ accessToken, refreshToken }) => { + assert.exists(accessToken); + assert.exists(refreshToken); + }) + .then(() => done()) + .catch(done) + }); - it('TokenAPI.login()', done => { + it(`should create plugin token for plugin with name: ${TEST_PLUGIN.HTTP.name} via HTTP`, done => { + const token = new DeviceHive.models.PluginToken({ + actions: [], + expiration: `2030-02-09T10:09:03.033Z`, + topicName: TEST_PLUGIN.HTTP.topicName + }); - Promise.all([httpDeviceHive.token.login(testToken.login, testToken.password), wsDeviceHive.token.login(testToken.login, testToken.password)]) - .then(dataAll => { - for (const data of dataAll) { - const expectedkeys = ['accessToken', 'refreshToken']; - assert.isObject(data); - assert.containsAllKeys(data, expectedkeys); - testToken.accessToken = data.accessToken; - testToken.refreshToken = data.refreshToken; - } + httpDeviceHive.token.createPluginToken(token) + .then(({ accessToken, refreshToken }) => { + assert.exists(accessToken); + assert.exists(refreshToken); + }) + .then(() => done()) + .catch(done); + }); + + it(`should authenticate plugin with name: ${TEST_PLUGIN.HTTP.name} via HTTP`, done => { + httpDeviceHive.token.authPlugin(TEST_PLUGIN.HTTP.accessToken) + .then(({ tpc, a, e, t }) => { + assert.exists(tpc); + assert.exists(a); + assert.exists(e); + assert.exists(t); }) .then(done) - .catch(done) + .catch(done); + }); + + after(done => { + httpDeviceHive.plugin.delete(TEST_PLUGIN.HTTP.topicName) + .then(() => { + httpDeviceHive.disconnect(); + wsDeviceHive.disconnect(); + + done(); + }); }); }); \ No newline at end of file diff --git a/test/integration/controllers/UserAPI.spec.js b/test/integration/controllers/UserAPI.spec.js index 5d81fc7..d5ca2ee 100644 --- a/test/integration/controllers/UserAPI.spec.js +++ b/test/integration/controllers/UserAPI.spec.js @@ -1,179 +1,204 @@ +const randomString = require(`randomstring`); const chai = require(`chai`); const assert = chai.assert; -const config = require('../config'); - -const EventEmitter = require('events'); -const events = new EventEmitter(); - -const DeviceHive = require('../../../index'); +const config = require(`../config`); +const DeviceHive = require(`../../../index`); +const User = DeviceHive.models.User; +const UserListQuery = DeviceHive.models.query.UserListQuery; +const UserCountQuery = DeviceHive.models.query.UserCountQuery; const httpDeviceHive = new DeviceHive(config.server.http); const wsDeviceHive = new DeviceHive(config.server.ws); -const testUsers = [ - { - login: 'testLogin', +const TEST_USER_LOGIN_PREFIX = `DH-JS-LIB-USER-LOGIN-`; +const TEST_USERS = { + HTTP: { + login: `${TEST_USER_LOGIN_PREFIX}-${randomString.generate()}`, role: 1, status: 1, - password: 'password', - lastLogin: '2018-02-09T10:09:03.033Z', + password: `password`, data: { - jsonString: 'jsonString' - }, - introReviewed: false, - allDeviceTypesAvailable: false - }, { - login: 'testLogin2', + jsonString: `jsonString` + } + }, + WS: { + login: `${TEST_USER_LOGIN_PREFIX}-${randomString.generate()}`, role: 1, status: 1, - password: 'password', - lastLogin: '2018-02-09T10:09:03.033Z', + password: `password`, data: { - jsonString: 'jsonString' - }, - introReviewed: false, - allDeviceTypesAvailable: false + jsonString: `jsonString` + } } -]; +}; -describe('UserAPI', () => { +describe(`UserAPI`, () => { before(done => { - // Configaratuion DeviceHive - Promise.all([httpDeviceHive.connect(), wsDeviceHive.connect()]) .then(() => done()); }); - it('UserAPI.insert()', done => { - - // Configurating User model - const User = DeviceHive.models.User; + it(`should add new user with login ${TEST_USERS.HTTP.login} via HTTP`, done => { + const userModel = new User(TEST_USERS.HTTP); - const user = new User(testUsers[0]); - const user2 = new User(testUsers[1]); + httpDeviceHive.user.insert(userModel) + .then(({ id }) => { + assert.exists(id); - Promise.all([httpDeviceHive.user.insert(user), wsDeviceHive.user.insert(user2)]) + TEST_USERS.HTTP.id = id; + }) .then(() => done()) .catch(done); }); + it(`should add new user with login ${TEST_USERS.WS.login} via WS`, done => { + const userModel = new User(TEST_USERS.WS); - it('UserAPI.list()', done => { - - // Configurating User List query - const userListQuery = new DeviceHive.models.query.UserListQuery({ - loginPattern: 'testLogin%', - role: 1, - status: 1, - sortField: 'login', - sortOrder: 'asc', - take: 2, - skip: 0, - }); + wsDeviceHive.user.insert(userModel) + .then(({ id }) => { + assert.exists(id); - Promise.all([httpDeviceHive.user.list(userListQuery), wsDeviceHive.user.list(userListQuery)]) - .then(dataAll => { - for (const data of dataAll) { - for (const userKey in data) { - testUsers[userKey].id = data[userKey].id; - testUsers[userKey].lastLogin = data[userKey].lastLogin; - - const expectedKeys = Object.keys(testUsers[0]); - expectedKeys.splice(expectedKeys.indexOf('password'), 1); - assert.containsAllKeys(data[userKey], expectedKeys); - } - } + TEST_USERS.WS.id = id; }) - .then(done) + .then(() => done()) .catch(done); }); - it('UserAPI.get()', done => { + it(`should list users with the next login pattern: ${TEST_USER_LOGIN_PREFIX}% via HTTP`, done => { + const userListQuery = new UserListQuery({ loginPattern: `${TEST_USER_LOGIN_PREFIX}%` }); - Promise.all([httpDeviceHive.user.get(testUsers[0].id), wsDeviceHive.user.get(testUsers[0].id)]) - .then(dataAll => { - const expected = Object.assign({}, testUsers[0]); - delete expected.password; - for (const data of dataAll) { - assert.isObject(data); - assert.deepInclude(data, expected); - } + httpDeviceHive.user.list(userListQuery) + .then(users => { + assert.equal(users.length, Object.keys(TEST_USERS).length); }) - .then(done) + .then(() => done()) .catch(done); }); - it('UserAPI.getCurrent()', done => { + it(`should list users with the next login pattern: ${TEST_USER_LOGIN_PREFIX}% via HTTP`, done => { + const userListQuery = new UserListQuery({ loginPattern: `${TEST_USER_LOGIN_PREFIX}%` }); - Promise.all([httpDeviceHive.user.getCurrent(), wsDeviceHive.user.getCurrent()]) - .then(dataAll => { - const expected = Object.assign({}, testUsers[0]); - delete expected.password; - for (const data of dataAll) { - const expectedKeys = Object.keys(testUsers[0]); - expectedKeys.splice(expectedKeys.indexOf('password'), 1); + wsDeviceHive.user.list(userListQuery) + .then(users => { + assert.equal(users.length, Object.keys(TEST_USERS).length); + }) + .then(() => done()) + .catch(done); + }); - assert.isObject(data); - assert.containsAllKeys(data, expectedKeys); - } + it(`should get user with name: ${TEST_USERS.HTTP.name} via HTTP`, done => { + httpDeviceHive.user.get(TEST_USERS.HTTP.id) + .then(user => { + assert.equal(user.name, TEST_USERS.HTTP.name); }) - .then(done) + .then(() => done()) + .catch(done); + }); + + it(`should get user with name: ${TEST_USERS.WS.name} via WS`, done => { + wsDeviceHive.user.get(TEST_USERS.WS.id) + .then(user => { + assert.equal(user.name, TEST_USERS.WS.name); + }) + .then(() => done()) .catch(done); }); + it(`should get current user: login ${config.TEST_USER_LOGIN}, password ${config.TEST_USER_PASSWORD} via HTTP`, done => { + httpDeviceHive.user.getCurrent() + .then(user => { + assert.equal(user.login, config.TEST_USER_LOGIN); + }) + .then(() => done()) + .catch(done); + }); - it('UserAPI.update()', done => { + it(`should get current user: login ${config.TEST_USER_LOGIN}, password ${config.TEST_USER_PASSWORD} via WS`, done => { + wsDeviceHive.user.getCurrent() + .then(user => { + assert.equal(user.login, config.TEST_USER_LOGIN); + }) + .then(() => done()) + .catch(done); + }); - // Configurating User model - const User = DeviceHive.models.User; + it(`should update user with login: ${TEST_USERS.HTTP.login} via HTTP`, done => { + TEST_USERS.HTTP.data = { update: true }; - const user = new User(testUsers[0]); - const user2 = new User(testUsers[1]); + const userModel = new User(TEST_USERS.HTTP); - Promise.all([httpDeviceHive.user.update(user), wsDeviceHive.user.update(user2)]) + httpDeviceHive.user.update(userModel) + .then(() => httpDeviceHive.user.get(TEST_USERS.HTTP.id)) + .then((user) => { + assert.deepEqual(user.data, TEST_USERS.HTTP.data); + }) .then(() => done()) .catch(done); - }); + it(`should update user with login: ${TEST_USERS.WS.login} via WS`, done => { + TEST_USERS.WS.data = { update: true }; - it('UserAPI.updateCurrent()', done => { + const userModel = new User(TEST_USERS.WS); + + wsDeviceHive.user.update(userModel) + .then(() => wsDeviceHive.user.get(TEST_USERS.WS.id)) + .then((user) => { + assert.deepEqual(user.data, TEST_USERS.WS.data); + }) + .then(() => done()) + .catch(done); + }); - // Configurating User model - const user = new DeviceHive.models.User({ - status: 0 - }); + it(`should update current user with login ${config.TEST_USER_LOGIN} via HTTP`, done => { + const userModel = new User({ status: 0 }); - Promise.all([httpDeviceHive.user.updateCurrent(user), wsDeviceHive.user.updateCurrent(user)]) + httpDeviceHive.user.updateCurrent(userModel) + .then(() => httpDeviceHive.user.getCurrent()) + .then(user => { + assert.equal(user.status, userModel.status); + }) .then(() => done()) .catch(done); }); + it(`should update current user with login ${config.TEST_USER_LOGIN} via WS`, done => { + const userModel = new User({ status: 1 }); - it('UserAPI.count()', done => { + wsDeviceHive.user.updateCurrent(userModel) + .then(() => wsDeviceHive.user.getCurrent()) + .then(user => { + assert.equal(user.status, userModel.status); + }) + .then(() => done()) + .catch(done); + }); - // Configurating User List query - const userListQuery = new DeviceHive.models.query.UserCountQuery({ - login: 'login', - loginPattern: 'loginPattern', - role: '1', - status: '1' - }); + it(`should count users with the next login pattern ${TEST_USER_LOGIN_PREFIX}% via HTTP`, done => { + const userListQuery = new UserCountQuery({ loginPattern: `${TEST_USER_LOGIN_PREFIX}%` }); - Promise.all([httpDeviceHive.user.count(userListQuery), wsDeviceHive.user.count(userListQuery)]) - .then(dataAll => { - for (const data of dataAll) { - assert.property(data, 'count'); - } + httpDeviceHive.user.count(userListQuery) + .then(({ count }) => { + assert.equal(count, Object.keys(TEST_USERS).length); }) - .then(done) + .then(() => done()) .catch(done); }); + it(`should count users with the next login pattern ${TEST_USER_LOGIN_PREFIX}% via WS`, done => { + const userListQuery = new UserCountQuery({ loginPattern: `${TEST_USER_LOGIN_PREFIX}%` }); + + wsDeviceHive.user.count(userListQuery) + .then(({ count }) => { + assert.equal(count, Object.keys(TEST_USERS).length); + }) + .then(() => done()) + .catch(done); + }); - it('UserAPI.getDeviceTypes()', done => { + it(`UserAPI.getDeviceTypes()`, done => { Promise.all([httpDeviceHive.user.getDeviceTypes(testUsers[0].id)]) .then(() => done()) @@ -181,7 +206,7 @@ describe('UserAPI', () => { }); - it('UserAPI.assignAllDeviceTypes()', done => { + it(`UserAPI.assignAllDeviceTypes()`, done => { Promise.all([httpDeviceHive.user.assignAllDeviceTypes(testUsers[0].id)]) .then(() => done()) @@ -189,7 +214,7 @@ describe('UserAPI', () => { }); - it('UserAPI.unassignAllDeviceTypes()', done => { + it(`UserAPI.unassignAllDeviceTypes()`, done => { Promise.all([httpDeviceHive.user.unassignAllDeviceTypes(testUsers[0].id)]) .then(() => done()) @@ -197,7 +222,7 @@ describe('UserAPI', () => { }); - it('UserAPI.assignDeviceType()', done => { + it(`UserAPI.assignDeviceType()`, done => { Promise.all([httpDeviceHive.user.assignDeviceType(testUsers[0].id, 1)]) .then(() => done()) @@ -205,13 +230,13 @@ describe('UserAPI', () => { }); - it('UserAPI.getDeviceType()', done => { + it(`UserAPI.getDeviceType()`, done => { Promise.all([httpDeviceHive.user.getDeviceType(testUsers[0].id, 1)]) .then(dataAll => { for (const data of dataAll) { assert.isObject(data); - assert.property(data, 'deviceType'); + assert.property(data, `deviceType`); } }) .then(done) @@ -219,7 +244,7 @@ describe('UserAPI', () => { }); - it('UserAPI.unassignDeviceType()', done => { + it(`UserAPI.unassignDeviceType()`, done => { Promise.all([httpDeviceHive.user.unassignDeviceType(testUsers[0].id, 1)]) .then(() => done()) @@ -227,7 +252,7 @@ describe('UserAPI', () => { }); - it('UserAPI.assignNetwork()', done => { + it(`UserAPI.assignNetwork()`, done => { httpDeviceHive.user.assignNetwork(testUsers[0].id, 1) .then(() => wsDeviceHive.user.assignNetwork(testUsers[1].id, 1)) @@ -236,13 +261,13 @@ describe('UserAPI', () => { }); - it('UserAPI.getNetwork()', done => { + it(`UserAPI.getNetwork()`, done => { Promise.all([httpDeviceHive.user.getNetwork(testUsers[0].id, 1), wsDeviceHive.user.getNetwork(testUsers[1].id, 1)]) .then(dataAll => { for (const data of dataAll) { assert.isObject(data); - assert.property(data, 'network'); + assert.property(data, `network`); } }) .then(done) @@ -250,7 +275,7 @@ describe('UserAPI', () => { }); - it('UserAPI.unassignNetwork()', done => { + it(`UserAPI.unassignNetwork()`, done => { httpDeviceHive.user.unassignNetwork(testUsers[0].id, 1) .then(() => wsDeviceHive.user.unassignNetwork(testUsers[1].id, 1)) @@ -259,11 +284,17 @@ describe('UserAPI', () => { }); - it('UserAPI.delete()', done => { + it(`UserAPI.delete()`, done => { Promise.all([httpDeviceHive.user.delete(testUsers[0].id), wsDeviceHive.user.delete(testUsers[1].id)]) .then(() => done()) .catch(done); }); + after(done => { + httpDeviceHive.disconnect(); + wsDeviceHive.disconnect(); + + done(); + }); }); \ No newline at end of file