bridge-node-server/node_server/dev_api/controllers/instruments/cards/create.spec.js

132 lines
4.1 KiB
JavaScript
Raw Normal View History

2018-06-24 20:15:03 +00:00
/* eslint-disable max-nested-callbacks */
/* eslint-disable mocha/no-hooks-for-single-case */
'use strict';
const sinon = require('sinon');
const chai = require('chai');
const sinonChai = require('sinon-chai');
const chaiAsPromised = require('chai-as-promised');
const rewire = require('rewire');
// eslint-disable-next-line import/no-unassigned-import
require('../../../../tools/test/testGlobals');
const create = rewire('./create');
const encryptStub = create.__get__('encrypt');
const daoAccountStub = create.__get__('daoAccount');
const daoAddressStub = create.__get__('daoAddress');
const dataMapperStub = create.__get__('dataMapper');
const expect = chai.expect;
const sandbox = sinon.createSandbox();
chai.use(sinonChai);
chai.use(chaiAsPromised);
const ADDRESS_UUID = 'df324Qwef3';
const ACCOUNT_UUID = 'afw34fwsfs';
const UNMAPPED_BODY = {
card: {
startDate: '01-00',
expiryDate: '01-99'
}
};
const MAPPED_ACCOUNT = {
Account: {
accountInfo: 'someAccounInfo',
CreditDebitCardInfo: {
someCardInfo: 'someCardInfo'
}
},
Address: {addressInfo: 'someAddressInfo'}
};
const ENCRYPTED_ACCOUNT = {
Account: {
encryptedAccountInfo: 'someEncryptedAccountInfo',
CreditDebitCardInfo: {
someEncryptedCardInfo: 'someEncryptedCardInfo'
}
},
Address: {addressInfo: 'someAddressInfo'}
};
const ACCOUNT_PLUS_UUIDS = {
encryptedAccountInfo: 'someEncryptedAccountInfo',
CreditDebitCardInfo: {
someEncryptedCardInfo: 'someEncryptedCardInfo',
BillingAddress: ADDRESS_UUID
}
};
/**
* Wrapper for mocha's `it` testcase function to wait for the result of the
* function before running the expectations.
*
* @param {Object} body - a parmeter of the function
* @param {string} description - The description for the test
* @param {Function} expectation - The expectation fucntion for this test
*
* @returns {Promise} - Promise for the completion of the test
*/
function itP(body, description, expectation) {
it(description, () => {
return create.create(body, '1')
.then((accountID) => {
return expectation(accountID);
})
.catch((error) => {
return expectation(error);
});
});
}
describe('instruments.cards.create', () => {
let clock;
before(() => {
const now = new Date(2020, 1);
clock = sinon.useFakeTimers(now.getTime());
});
after(() => {
clock.restore();
});
beforeEach(() => {
sandbox.stub(daoAccountStub, 'createOne').resolves(ACCOUNT_UUID);
sandbox.stub(daoAddressStub, 'createOne').resolves(ADDRESS_UUID);
sandbox.stub(encryptStub, 'encrypt').resolves(ENCRYPTED_ACCOUNT);
sandbox.stub(dataMapperStub, 'dataMapper').returns(MAPPED_ACCOUNT);
});
afterEach(() => {
sandbox.restore();
});
describe('creates an account and address object', () => {
itP(UNMAPPED_BODY, 'it maps the data', () => {
return expect(dataMapperStub.dataMapper).to.have.been
.calledOnce
.calledWith(UNMAPPED_BODY, '1');
});
itP(UNMAPPED_BODY, 'it encrypts the data', () => {
return expect(encryptStub.encrypt).to.have.been
.calledOnce
.calledWith(
MAPPED_ACCOUNT,
sinon.match.string,
'1');
});
itP(UNMAPPED_BODY, 'it creates the address object', () => {
return expect(daoAddressStub.createOne).to.have.been
.calledOnce
.calledWith(ENCRYPTED_ACCOUNT.Address);
});
itP(UNMAPPED_BODY, 'it creates the account object', () => {
return expect(daoAccountStub.createOne).to.have.been
.calledOnce
.calledWith(ACCOUNT_PLUS_UUIDS);
});
itP(UNMAPPED_BODY, 'it returns the account ID', (accountID) => {
return expect(accountID).to.include({
cardID: ACCOUNT_UUID});
});
});
});