Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
describe('Over ' + transportCtor.name, () => {
// tslint:disable:no-invalid-this
this.timeout(60000);
const testDevice = testUtils.createTestDevice();
const scs = ServiceConnectionString.parse(process.env.IOTHUB_CONNECTION_STRING);
const testDeviceCS = DeviceConnectionString.createWithSharedAccessKey(scs.HostName, testDevice.deviceId, testDevice.authentication.symmetricKey.primaryKey);
beforeEach((beforeEachCallback) => {
testUtils.addTestDeviceToRegistry(testDevice, beforeEachCallback);
});
afterEach((afterEachCallback) => {
testUtils.removeTestDeviceFromRegistry(testDevice, afterEachCallback);
});
[null, '', 'foo', { k1: 'v1' }, {}].forEach((testPayload) => {
it('device can receive a method call with a payload of ' + JSON.stringify(testPayload) + ' and send a response', (testCallback) => {
const methodName = 'testMethod';
const requestPayload = testPayload;
const responsePayload = { responseKey: uuid.v4() };
const deviceClient = DeviceClient.fromConnectionString(testDeviceCS, transportCtor);
describe('Over ' + transportCtor.name, () => {
// tslint:disable:no-invalid-this
this.timeout(60000);
const testDevice = testUtils.createTestDevice();
const scs = ServiceConnectionString.parse(process.env.IOTHUB_CONNECTION_STRING);
const testDeviceCS = DeviceConnectionString.createWithSharedAccessKey(scs.HostName, testDevice.deviceId, testDevice.authentication.symmetricKey.primaryKey);
beforeEach((beforeEachCallback) => {
testUtils.addTestDeviceToRegistry(testDevice, beforeEachCallback);
});
afterEach((afterEachCallback) => {
testUtils.removeTestDeviceFromRegistry(testDevice, afterEachCallback);
});
it('device can get its device twin and modify reported properties', (testCallback) => {
const deviceClient = DeviceClient.fromConnectionString(testDeviceCS, transportCtor);
const twinPatch = { twinKey: 'twinValue' };
deviceClient.open((err) => {
if (err) throw err;
describe('C2D', () => {
// tslint:disable:no-invalid-this
this.timeout(60000);
const testDevice2 = testUtils.createTestDevice();
const hostName = ServiceConnectionString.parse(process.env.IOTHUB_CONNECTION_STRING).HostName;
const testDeviceCS2 = DeviceConnectionString.createWithSharedAccessKey(hostName, testDevice2.deviceId, testDevice2.authentication.symmetricKey.primaryKey);
before((beforeCallback) => {
testUtils.addTestDeviceToRegistry(testDevice2, beforeCallback);
});
after((afterCallback) => {
testUtils.removeTestDeviceFromRegistry(testDevice2, afterCallback);
});
[DeviceAmqp, DeviceAmqpWs, DeviceMqtt, DeviceMqttWs, DeviceHttp].forEach((transportCtor: any) => {
describe('Over ' + transportCtor.name, () => {
let deviceClient: DeviceClient;
beforeEach((beforeEachCallback) => {
deviceClient = DeviceClient.fromConnectionString(testDeviceCS2, transportCtor);
registry.create({ deviceId: testDeviceId }, continueWith((result) => {
testDevice = result;
const host = ConnectionString.parse(process.env.IOTHUB_CONNECTION_STRING).HostName;
testDeviceConnectionString = 'HostName=' + host + ';DeviceId=' + testDevice.deviceId + ';SharedAccessKey=' + testDevice.authentication.symmetricKey.primaryKey;
debug('created device with id: ' + testDevice.deviceId);
beforeCallback();
}));
});
timeout(registry.create.bind(registry), MAX_CREATE_TIME)({ deviceId: deviceId }, (err, deviceInfo) => {
if (err) {
debug('error creating device: ' + deviceId + ':' + err.toString());
callback(err);
} else {
debug('device created: ' + deviceId);
const cs = HubConnectionString.parse(hubConnectionString);
callback(null, DeviceConnectionString.createWithSharedAccessKey(cs.HostName, deviceInfo.deviceId, deviceInfo.authentication.symmetricKey.primaryKey));
}
});
};
function(done) {
logger.info('');
logger.info('--- Executing IOT Hub tests ---');
if(process.argv.length <= 2) {
logger.crit('Skipping IotHub tests because no IotHub configuration provided. To verify connectivity to an IotHub provide the connection string on the command line.');
return done();
} else {
try {
ConnectionString.parse(argv._[0])
}
catch(exception) {
logger.crit('Unable to parse the connection string, verify correctness: ' + exception);
return done();
}
require('./iothubtests').run(argv._[0], argv.consumerGroup, argv.device, done);
}
}
], function () {
function getSas(connectionString) {
var sas;
if (connectionString) {
var cn;
try {
cn = ConnectionString.parse(connectionString);
} catch (e) {
if (e instanceof errors.ArgumentError) {
inputError('Could not parse connection string: ' + connectionString);
} else {
throw e;
}
}
var expiry = Math.floor(Date.now() / 1000) + 3600;
sas = SharedAccessSignature.create(cn.HostName, cn.SharedAccessKeyName, cn.SharedAccessKey, expiry).toString();
} else {
sas = loadSasFromUserFile();
}
if (!sas) {
inputError('You must either use the login command or the --login argument for iothub-explorer to authenticate with your IoT Hub instance');
}
const DeviceClient = require('azure-iot-device').Client;
const Mqtt = require('azure-iot-device-mqtt').Mqtt;
const ServiceConnectionString = require('azure-iothub').ConnectionString;
const DeviceSas = require('azure-iot-device').SharedAccessSignature;
const TestInterfaceInstance = require('./test_interfaceInstance').TestInterfaceInstance;
const createModel = require('./model_repository_helper').createModel;
const startEventHubsClient = require('./event_hubs_helper').startEventHubsClient;
const closeClients = require('./event_hubs_helper').closeClients;
const interfaceDocument = require('./dtdl/test_interface');
const capabilityModelDocument = require('./dtdl/test_capability_model');
const hubConnectionString = process.env.IOTHUB_CONNECTION_STRING;
const hubHostName = ServiceConnectionString.parse(hubConnectionString).HostName;
describe('Digital Twin Telemetry', function () {
const deviceDescription = {
deviceId: 'node-e2e-digitaltwin-telemetry-' + uuid.v4()
};
let createdDevice;
before('creating device identity: ' + deviceDescription.deviceId, async function () {
this.timeout(60000);
debug('creating test device: ' + deviceDescription.deviceId);
return Registry.fromConnectionString(hubConnectionString).create(deviceDescription)
.then((device) => {
createdDevice = device.responseBody;
debug('create or update test interface model: ' + interfaceDocument['@id']);
return createModel(interfaceDocument);
}).then(() => {
const DeviceSas = require('azure-iot-device').SharedAccessSignature;
const DeviceClient = require('azure-iot-device').Client;
const IoTHubTokenCredentials = require('azure-iot-digitaltwins-service').IoTHubTokenCredentials;
const DigitalTwinDeviceClient = require('azure-iot-digitaltwins-device').DigitalTwinClient;
const DigitalTwinServiceClient = require('azure-iot-digitaltwins-service').DigitalTwinServiceClient;
const createModel = require('./model_repository_helper').createModel;
const interfaceDocument = require('./dtdl/test_interface');
const capabilityModelDocument = require('./dtdl/test_capability_model');
const startEventHubsClient = require('./event_hubs_helper').startEventHubsClient;
const closeClients = require('./event_hubs_helper').closeClients;
const hubConnectionString = process.env.IOTHUB_CONNECTION_STRING;
const hubHostName = ServiceConnectionString.parse(hubConnectionString).HostName;
const credentials = new IoTHubTokenCredentials(hubConnectionString);
const TestInterfaceInstance = require('./test_interfaceInstance').TestInterfaceInstance;
describe('Digital Twin Invoke Command', function () {
const deviceDescription = {
deviceId: 'node-e2e-digitaltwin-invoke-command-' + uuid.v4()
};
const testInterfaceInstanceName = 'testInterfaceInstance';
const syncCommandName = 'syncCommand';
const asyncCommandName = 'asyncCommand';
const invokeCommandArgument = 'testInvokeCommandArgument';
const invokeCommandResponse = 'testInvokeCommandResponse';
let createdDevice;
before('creating device identity: ' + deviceDescription.deviceId, function () {