bridge-node-server/node_server/utils/device/specs/device.spec.js
Martin Donnelly 57bd6c8e6a init
2018-06-24 21:15:03 +01:00

90 lines
2.7 KiB
JavaScript

/**
* Unit testing file for device utils
*/
'use strict';
/* eslint max-nested-callbacks: ["error", 7] */
// eslint-disable-next-line no-unused-vars
const testGlobals = require('../../../tools/test/testGlobals.js');
const chai = require('chai');
const sinon = require('sinon');
const sinonChai = require('sinon-chai');
const chaiAsPromised = require('chai-as-promised');
const rewire = require('rewire');
/**
* Use `rewire` instead of require so that we can access private functions for test
*/
const device = rewire('../device.js');
const mainDBStub = device.__get__('mainDB');
const mainDBPStub = device.__get__('mainDBP');
const expect = chai.expect;
const sandbox = sinon.createSandbox();
chai.use(sinonChai);
chai.use(chaiAsPromised);
/**
* Define a sample Client and Device object to return
*/
describe('utils/device', () => {
describe('archive', () => {
const DEVICE_MONGO_ID = '01234567890abcdef';
const DEVICE_FAKE = {
_id: DEVICE_MONGO_ID,
DeviceAuthorisation: 'SOME HASHED PASSWORD',
DeviceSalt: 'SOME SALT',
CurrentHMAC: 'SOME EXISTING HMAC',
PendingHMAC: 'SOME PENDING HMAC'
};
const EXPECTED_ARCHIVE_DEVICE = {
DeviceIndex: DEVICE_MONGO_ID,
DeviceAuthorisation: '',
DeviceSalt: '',
CurrentHMAC: '',
PendingHMAC: '',
LastUpdate: sinon.match.date
};
/**
* Stub the functions that will be used for the "happy path"
* The responses are specifically overriden below for testing the error cases
*/
beforeEach(() => {
sandbox.stub(mainDBPStub, 'addObject').resolves();
mainDBStub.collectionDeviceArchive = 'Device Archive Collection';
});
afterEach(() => {
sandbox.restore();
});
it('archives a sanitised version of the device', () => {
const archiveP = device.archiveDevice(DEVICE_FAKE);
return archiveP.then(() =>
expect(mainDBPStub.addObject).to.have.been
.calledOnce
.calledWith(
mainDBStub.collectionDeviceArchive,
sinon.match(EXPECTED_ARCHIVE_DEVICE)
)
);
});
it('resolves when it is successfully archived', () => {
const archiveP = device.archiveDevice(DEVICE_FAKE);
return expect(archiveP).to.eventually.be.fulfilled;
});
it('rejects if there is an error in the database', () => {
mainDBPStub.addObject.rejects();
const archiveP = device.archiveDevice(DEVICE_FAKE);
return expect(archiveP).to.eventually.be.rejected;
});
});
});