bridge-node-server/node_server/utils/test/paycodes.spec.js

153 lines
5.7 KiB
JavaScript
Raw Normal View History

2018-06-24 20:15:03 +00:00
/**
* @fileOverview Test the paycode generation libraries
*/
'use strict';
/* eslint max-nested-callbacks: ["error", 7] */
const chai = require('chai');
const utils = require('../../ComServe/utils.js');
const paycodes = require('../paycodes.js');
const expect = chai.expect;
const PAYCODE_LENGTH_DEFAULT = 5;
const PAYCODE_BANK_DEFAULT = paycodes.PAYCODE_METHODS[0];
const SUCCESSFUL_VALIDATION = utils.createError(10000, 'Success');
describe('paycodes', () => {
describe('simplePayCode', () => {
it('generates a 5-character string', () => {
expect(paycodes.simplePayCode())
.to.be.a('string')
.to.have.lengthOf(PAYCODE_LENGTH_DEFAULT);
});
it('generates a string from only the utils.paycodeString characters', () => {
const paycodeStringRegex = new RegExp(
'^[' + utils.paycodeString + ']+$'
);
expect(paycodes.simplePayCode())
.to.match(paycodeStringRegex);
});
it('generates a different string every time', () => {
const code1 = paycodes.simplePayCode();
const code2 = paycodes.simplePayCode();
expect(code1).to.not.equal(code2);
});
it('generates a string that passes paycodeValidate', () => {
const code = paycodes.simplePayCode();
expect(paycodes.payCodeValidate(code))
.to.deep.equal(SUCCESSFUL_VALIDATION);
});
});
describe('payCodeGeneration', () => {
describe('only allows specific lengths to be generated', () => {
it('<5 is INVALID ', () => {
expect(paycodes.payCodeGeneration(utils.paycodeString, 4, PAYCODE_BANK_DEFAULT))
.to.equal(-1);
});
for (let i = 5; i < 9; ++i) {
it(i + ' is VALID ', () => {
expect(paycodes.payCodeGeneration(utils.paycodeString, i, PAYCODE_BANK_DEFAULT))
.to.be.a('string')
.to.have.lengthOf(i);
});
}
it('9 is INVALID ', () => {
expect(paycodes.payCodeGeneration(utils.paycodeString, 9, PAYCODE_BANK_DEFAULT))
.to.equal(-1);
});
it('10 is VALID ', () => {
expect(paycodes.payCodeGeneration(utils.paycodeString, 10, PAYCODE_BANK_DEFAULT))
.to.be.a('string')
.to.have.lengthOf(10);
});
it('>10 is INVALID ', () => {
expect(paycodes.payCodeGeneration(utils.paycodeString, 11, PAYCODE_BANK_DEFAULT))
.to.equal(-1);
});
});
describe('only allows specific methods', () => {
paycodes.PAYCODE_METHODS.forEach((method) => {
it('"' + method + '" is VALID', () => {
expect(paycodes.payCodeGeneration(utils.paycodeString, PAYCODE_LENGTH_DEFAULT, method))
.to.be.a('string')
.to.have.lengthOf(PAYCODE_LENGTH_DEFAULT);
});
});
it('other values are INVALID', () => {
expect(paycodes.payCodeGeneration(utils.paycodeString, PAYCODE_LENGTH_DEFAULT, 'other values'))
.to.equal(-1);
});
});
describe('supports a limited range of character sets', () => {
const validCharacterSets = [
'paycodeString'
];
const invalidCharacterSets = [
'generalText',
'fullAlphaNumeric',
'alpha',
'lowerCaseHex',
'numeric',
'hexadecimal'
];
validCharacterSets.forEach((charset) => {
const list = utils[charset];
describe('utils.' + charset + ' is VALID', () => {
it('generates a string of the required length', () => {
expect(paycodes.payCodeGeneration(list, PAYCODE_LENGTH_DEFAULT, PAYCODE_BANK_DEFAULT))
.to.be.a('string')
.to.have.lengthOf(PAYCODE_LENGTH_DEFAULT);
});
it('generates a string from only the provided characters', () => {
const paycodeStringRegex = new RegExp(
'^[' + list + ']+$'
);
expect(paycodes.payCodeGeneration(list, PAYCODE_LENGTH_DEFAULT, PAYCODE_BANK_DEFAULT))
.to.match(paycodeStringRegex);
});
it('generates a different string every time', () => {
const code1 = paycodes.payCodeGeneration(list, PAYCODE_LENGTH_DEFAULT, PAYCODE_BANK_DEFAULT);
const code2 = paycodes.payCodeGeneration(list, PAYCODE_LENGTH_DEFAULT, PAYCODE_BANK_DEFAULT);
expect(code1).to.not.equal(code2);
});
it('generates a string that passes paycodeValidate', () => {
const code = paycodes.payCodeGeneration(list, PAYCODE_LENGTH_DEFAULT, PAYCODE_BANK_DEFAULT);
expect(paycodes.payCodeValidate(code))
.to.deep.equal(SUCCESSFUL_VALIDATION);
});
});
});
invalidCharacterSets.forEach((charset) => {
const list = utils[charset];
it.skip('utils.' + charset + ' is INVALID', () => {
expect(paycodes.payCodeGeneration(list, PAYCODE_LENGTH_DEFAULT, PAYCODE_BANK_DEFAULT))
.to.equal(-1);
});
});
});
});
});