From 9a9336f9996621f6830ec9fc8e41112271e53430 Mon Sep 17 00:00:00 2001 From: Alvaro Vega Date: Thu, 23 May 2024 09:35:44 +0200 Subject: [PATCH 1/5] use oldDevice apikey when update device --- lib/bindings/AMQPBinding.js | 2 +- lib/bindings/HTTPBindings.js | 12 +++++++----- lib/bindings/MQTTBinding.js | 2 +- lib/iotagent-ul.js | 9 +++++++-- 4 files changed, 16 insertions(+), 9 deletions(-) diff --git a/lib/bindings/AMQPBinding.js b/lib/bindings/AMQPBinding.js index ea7e2799..628c6219 100644 --- a/lib/bindings/AMQPBinding.js +++ b/lib/bindings/AMQPBinding.js @@ -111,7 +111,7 @@ function deviceProvisioningHandler(device, callback) { * * @param {Object} device Device object containing all the information about the provisioned device. */ -function deviceUpdatingHandler(device, callback) { +function deviceUpdatingHandler(device, oldDevice, callback) { callback(null, device); } diff --git a/lib/bindings/HTTPBindings.js b/lib/bindings/HTTPBindings.js index dbb99537..c1de8fe1 100644 --- a/lib/bindings/HTTPBindings.js +++ b/lib/bindings/HTTPBindings.js @@ -448,11 +448,13 @@ function deviceProvisioningHandler(device, callback) { * * @param {Object} device Device object containing all the information about the updated device. */ -function deviceUpdatingHandler(device, callback) { - context = fillService(context, device); - config.getLogger().debug(context, 'httpbinding.deviceUpdatingHandler %j', device); +function deviceUpdatingHandler(newDevice, oldDevice, callback) { + context = fillService(context, newDevice); + config + .getLogger() + .debug(context, 'httpbinding.deviceUpdatingHandler newDevice %j oldDevice %j', newDevice, oldDevice); let group = {}; - iotAgentLib.getConfigurationSilently(config.getConfig().iota.defaultResource || '', device.apikey, function ( + iotAgentLib.getConfigurationSilently(config.getConfig().iota.defaultResource || '', oldDevice.apikey, function ( error, foundGroup ) { @@ -460,7 +462,7 @@ function deviceUpdatingHandler(device, callback) { group = foundGroup; } config.getLogger().debug(context, 'httpbinding.deviceUpdatingHandler group %j', group); - setPollingAndDefaultTransport(device, group, callback); + setPollingAndDefaultTransport(newDevice, group, callback); }); } diff --git a/lib/bindings/MQTTBinding.js b/lib/bindings/MQTTBinding.js index cf79aada..068d8783 100644 --- a/lib/bindings/MQTTBinding.js +++ b/lib/bindings/MQTTBinding.js @@ -313,7 +313,7 @@ function deviceProvisioningHandler(device, callback) { * * @param {Object} device Device object containing all the information about the provisioned device. */ -function deviceUpdatingHandler(device, callback) { +function deviceUpdatingHandler(device, oldDevice, callback) { callback(null, device); } diff --git a/lib/iotagent-ul.js b/lib/iotagent-ul.js index baa363cd..b7ba8576 100644 --- a/lib/iotagent-ul.js +++ b/lib/iotagent-ul.js @@ -132,6 +132,7 @@ function commandHandler(id, type, service, subservice, attributes, callback) { * @param {Object} device Device provisioning information. */ function deviceProvisioningHandler(device, callback) { + config.getLogger().debug(context, 'deviceProvisioningHandler for device %j', device); transportSelector.applyFunctionFromBinding([device], 'deviceProvisioningHandler', null, function (error, devices) { if (error) { callback(error); @@ -147,8 +148,12 @@ function deviceProvisioningHandler(device, callback) { * * @param {Object} device Device updating information. */ -function deviceUpdatingHandler(device, callback) { - transportSelector.applyFunctionFromBinding([device], 'deviceUpdatingHandler', null, function (error, devices) { +function deviceUpdatingHandler(newDevice, oldDevice, callback) { + config.getLogger().debug(context, 'deviceUpdatingHandler for newDevice %j oldDevice %j', newDevice, oldDevice); + transportSelector.applyFunctionFromBinding([newDevice, oldDevice], 'deviceUpdatingHandler', null, function ( + error, + devices + ) { if (error) { callback(error); } else { From 2da7b1f042f37b65b4ef31a737a6e863b367afed Mon Sep 17 00:00:00 2001 From: Alvaro Vega Date: Tue, 4 Jun 2024 13:45:03 +0200 Subject: [PATCH 2/5] add tests --- test/unit/ngsiv2/HTTP_update_device_test.js | 119 ++++++++++++++++++ .../provisionDeviceHTTP4.json | 47 +++++++ .../updateProvisionDevice4.json | 3 + 3 files changed, 169 insertions(+) create mode 100644 test/unit/ngsiv2/HTTP_update_device_test.js create mode 100644 test/unit/ngsiv2/deviceProvisioning/provisionDeviceHTTP4.json create mode 100644 test/unit/ngsiv2/deviceProvisioning/updateProvisionDevice4.json diff --git a/test/unit/ngsiv2/HTTP_update_device_test.js b/test/unit/ngsiv2/HTTP_update_device_test.js new file mode 100644 index 00000000..bdf74c2f --- /dev/null +++ b/test/unit/ngsiv2/HTTP_update_device_test.js @@ -0,0 +1,119 @@ +/* + * Copyright 2016 Telefonica Investigación y Desarrollo, S.A.U + * + * This file is part of iotagent-json + * + * iotagent-ul is free software: you can redistribute it and/or + * modify it under the terms of the GNU Affero General Public License as + * published by the Free Software Foundation, either version 3 of the License, + * or (at your option) any later version. + * + * iotagent-ul is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. + * See the GNU Affero General Public License for more details. + * + * You should have received a copy of the GNU Affero General Public + * License along with iotagent-json. + * If not, seehttp://www.gnu.org/licenses/. + * + * For those usages not covered by the GNU Affero General Public License + * please contact with::[contacto@tid.es] + * + */ + +/* eslint-disable no-unused-vars */ + +const iotagentMqtt = require('../../../'); +const config = require('./config-test.js'); +const nock = require('nock'); +const should = require('should'); +const iotAgentLib = require('iotagent-node-lib'); +const async = require('async'); +const utils = require('../../utils'); +const request = utils.request; +let mockedClientServer; +let contextBrokerMock; + +describe('HTTP binding - Update command provisioned with a new apikey', function () { + const provisionOptions = { + url: 'http://localhost:' + config.iota.server.port + '/iot/devices', + method: 'POST', + json: utils.readExampleFile('./test/unit/ngsiv2/deviceProvisioning/provisionDeviceHTTP4.json'), + headers: { + 'fiware-service': 'smartgondor', + 'fiware-servicepath': '/gardens' + } + }; + beforeEach(function (done) { + config.iota.logLevel = 'FATAL'; + nock.cleanAll(); + + iotagentMqtt.start(config, function () { + request(provisionOptions, function (error, response, body) { + done(); + }); + }); + }); + + it('should have provisioned with APIKEY1', function (done) { + const options = { + url: 'http://localhost:' + config.iota.server.port + '/iot/devices/MQTT_4', + headers: { + 'fiware-service': 'smartgondor', + 'fiware-servicepath': '/gardens' + }, + method: 'GET' + }; + request(options, function (error, response, body) { + const parsedBody = JSON.parse(body); + parsedBody.apikey.should.equal('APIKEY1'); + done(); + }); + }); + + afterEach(function (done) { + nock.cleanAll(); + async.series([iotAgentLib.clearAll, iotagentMqtt.stop], done); + }); + + describe('When a request to update a provision device arrives', function () { + const optionsUpdate = { + url: 'http://localhost:' + config.iota.server.port + '/iot/devices/MQTT_4', + method: 'PUT', + headers: { + 'fiware-service': 'smartgondor', + 'fiware-servicepath': '/gardens' + }, + json: utils.readExampleFile('./test/unit/ngsiv2/deviceProvisioning/updateProvisionDevice4.json') + }; + + it('should return a 200 OK and no errors', function (done) { + request(optionsUpdate, function (error, response, body) { + should.not.exist(error); + response.statusCode.should.equal(204); + done(); + }); + }); + + it('should have updated device apikey', function (done) { + request(optionsUpdate, function (error, response, body) { + const options = { + url: 'http://localhost:' + config.iota.server.port + '/iot/devices/MQTT_4', + headers: { + 'fiware-service': 'smartgondor', + 'fiware-servicepath': '/gardens' + }, + method: 'GET' + }; + + request(options, function (error, response, body) { + /* jshint camelcase:false */ + const parsedBody = JSON.parse(body); + parsedBody.apikey.should.equal('APIKEY2'); + done(); + }); + }); + }); + }); +}); diff --git a/test/unit/ngsiv2/deviceProvisioning/provisionDeviceHTTP4.json b/test/unit/ngsiv2/deviceProvisioning/provisionDeviceHTTP4.json new file mode 100644 index 00000000..d0ef4750 --- /dev/null +++ b/test/unit/ngsiv2/deviceProvisioning/provisionDeviceHTTP4.json @@ -0,0 +1,47 @@ +{ + "devices": [ + { + "device_id": "MQTT_4", + "protocol": "GENERIC_PROTO", + "apikey": "APIKEY1", + "entity_name": "Second MQTT Device", + "timezone": "America/Santiago", + "entity_type": "AnMQTTDevice", + "attributes": [ + { + "name": "temperature", + "type": "celsius" + }, + { + "name": "humidity", + "type": "degrees" + }, + { + "name": "luminosity", + "type": "Integer" + + }, + { + "name": "pollution", + "type": "Float" + }, + { + "name": "configuration", + "type": "Object" + }, + { + "name": "tags", + "type": "Array" + }, + { + "name": "enabled", + "type": "Boolean" + }, + { + "name": "alive", + "type": "Null" + } + ] + } + ] +} diff --git a/test/unit/ngsiv2/deviceProvisioning/updateProvisionDevice4.json b/test/unit/ngsiv2/deviceProvisioning/updateProvisionDevice4.json new file mode 100644 index 00000000..3b2648cb --- /dev/null +++ b/test/unit/ngsiv2/deviceProvisioning/updateProvisionDevice4.json @@ -0,0 +1,3 @@ +{ + "apikey": "APIKEY2" +} From 67a1b7ab4b44e6c3221328c9990f71bbbf34b980 Mon Sep 17 00:00:00 2001 From: Alvaro Vega Date: Tue, 4 Jun 2024 15:01:49 +0200 Subject: [PATCH 3/5] update CNR --- CHANGES_NEXT_RELEASE | 1 + 1 file changed, 1 insertion(+) diff --git a/CHANGES_NEXT_RELEASE b/CHANGES_NEXT_RELEASE index b05eacdd..ab84fd92 100644 --- a/CHANGES_NEXT_RELEASE +++ b/CHANGES_NEXT_RELEASE @@ -1 +1,2 @@ +- Fix: update device using previous device apikey to avoid error when apikey is updated /(iota-json#833) - Fix: default express limit to 1Mb instead default 100Kb and allow change it throught a conf env var 'IOTA_EXPRESS_LIMIT' (iota-json#827) From 09c70c4a64cd4a7832fc920083d1c4afc199d5a0 Mon Sep 17 00:00:00 2001 From: Alvaro Vega Date: Tue, 4 Jun 2024 15:27:28 +0200 Subject: [PATCH 4/5] update test --- test/unit/ngsiv2/HTTP_update_device_test.js | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/test/unit/ngsiv2/HTTP_update_device_test.js b/test/unit/ngsiv2/HTTP_update_device_test.js index bdf74c2f..43baddbc 100644 --- a/test/unit/ngsiv2/HTTP_update_device_test.js +++ b/test/unit/ngsiv2/HTTP_update_device_test.js @@ -30,12 +30,12 @@ const nock = require('nock'); const should = require('should'); const iotAgentLib = require('iotagent-node-lib'); const async = require('async'); +const request = require('request'); const utils = require('../../utils'); -const request = utils.request; let mockedClientServer; let contextBrokerMock; -describe('HTTP binding - Update command provisioned with a new apikey', function () { +describe('HTTP binding - Update provisioned devices with a new apikey', function () { const provisionOptions = { url: 'http://localhost:' + config.iota.server.port + '/iot/devices', method: 'POST', @@ -66,6 +66,7 @@ describe('HTTP binding - Update command provisioned with a new apikey', function method: 'GET' }; request(options, function (error, response, body) { + /* jshint camelcase:false */ const parsedBody = JSON.parse(body); parsedBody.apikey.should.equal('APIKEY1'); done(); From 9887a9306a50fe56a62997419237a7841f749077 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Ferm=C3=ADn=20Gal=C3=A1n=20M=C3=A1rquez?= Date: Wed, 5 Jun 2024 09:37:36 +0200 Subject: [PATCH 5/5] Update CHANGES_NEXT_RELEASE --- CHANGES_NEXT_RELEASE | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/CHANGES_NEXT_RELEASE b/CHANGES_NEXT_RELEASE index 88e001c5..b6d2696e 100644 --- a/CHANGES_NEXT_RELEASE +++ b/CHANGES_NEXT_RELEASE @@ -1,3 +1,3 @@ -- Fix: update device using previous device apikey to avoid error when apikey is updated /(iota-json#833) +- Fix: update device using previous device apikey to avoid error when apikey is updated (iota-json#833) - Fix: allow send multiple measures to CB in a batch (POST /v2/op/update) and sorted by TimeInstant when possible, instead of using multiples single request (iotagent-json#825, iotagent-node-lib#1612) - Fix: default express limit to 1Mb instead default 100Kb and allow change it throught a conf env var 'IOTA_EXPRESS_LIMIT' (iota-json#827) \ No newline at end of file