329 lines
7.7 KiB
JavaScript
329 lines
7.7 KiB
JavaScript
/* globals describe, beforeEach, afterEach, it */
|
||
/**
|
||
* Unit testing file for the worldpay_acquirer
|
||
*/
|
||
'use strict';
|
||
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 recoveryController = rewire('../api_recovery_controller.js');
|
||
|
||
const expect = chai.expect;
|
||
|
||
chai.use(sinonChai);
|
||
chai.use(chaiAsPromised);
|
||
|
||
//
|
||
// Array of test cases for testing comparison of answers to expected values.
|
||
// The anwers are in [type, expected, actual] order
|
||
//
|
||
const kbaTestCases = [
|
||
{
|
||
name: '1 of each type (exact match)',
|
||
valid: true,
|
||
data: [
|
||
['postcode', 'KA9 2HD', 'KA9 2HD'],
|
||
['card', '000', '000'],
|
||
['transactions', 0, '0'],
|
||
['device', '+4477777777', '+4477777777'],
|
||
['dob', '1970-01-01', '1970-01-01']
|
||
]
|
||
},
|
||
{
|
||
name: 'postcode with space differences 1',
|
||
valid: true,
|
||
data: [
|
||
['postcode', 'KA9 2HD', 'KA92HD']
|
||
]
|
||
},
|
||
{
|
||
name: 'postcode with space differences 2',
|
||
valid: true,
|
||
data: [
|
||
['postcode', 'KA92HD', 'KA9 2HD']
|
||
]
|
||
},
|
||
{
|
||
name: 'postcode with case differences 1',
|
||
valid: true,
|
||
data: [
|
||
['postcode', 'KA9 2HD', 'ka9 2hd']
|
||
]
|
||
},
|
||
{
|
||
name: 'postcode with case differences 2',
|
||
valid: true,
|
||
data: [
|
||
['postcode', 'ka9 2hd', 'KA9 2HD']
|
||
]
|
||
},
|
||
{
|
||
name: 'postcode with many case and space differences',
|
||
valid: true,
|
||
data: [
|
||
['postcode', 'k A 9 2 h D', 'Ka92Hd']
|
||
]
|
||
},
|
||
{
|
||
name: 'postcode with missing last char',
|
||
valid: false,
|
||
data: [
|
||
['postcode', 'KA9 2HD', 'KA9 2H']
|
||
]
|
||
},
|
||
{
|
||
name: 'postcode with homograph chars (the 2nd K is 0x039A GREEK CAPIAL LETTER KAPPA!)',
|
||
valid: false,
|
||
data: [
|
||
['postcode', 'KA9 2HD', 'ΚA9 2HD']
|
||
]
|
||
},
|
||
{
|
||
name: 'card with a different value',
|
||
valid: false,
|
||
data: [
|
||
['card', '123', '124']
|
||
]
|
||
},
|
||
{
|
||
name: 'card with a Number rather than a string',
|
||
valid: false,
|
||
data: [
|
||
['card', '123', 123]
|
||
]
|
||
},
|
||
{
|
||
name: 'card with missing leading 0 ("012" != "12")',
|
||
valid: false,
|
||
data: [
|
||
['card', '012', '12']
|
||
]
|
||
},
|
||
{
|
||
name: '1 expected transaction',
|
||
valid: true,
|
||
data: [
|
||
['transactions', 1, '1']
|
||
]
|
||
},
|
||
{
|
||
name: '2 expected transactions (still bucket 1-2)',
|
||
valid: true,
|
||
data: [
|
||
['transactions', 2, '1']
|
||
]
|
||
},
|
||
{
|
||
name: '3 expected transactions',
|
||
valid: true,
|
||
data: [
|
||
['transactions', 3, '3']
|
||
]
|
||
},
|
||
{
|
||
name: '4 expected transactions (still bucket 3-4)',
|
||
valid: true,
|
||
data: [
|
||
['transactions', 4, '3']
|
||
]
|
||
},
|
||
{
|
||
name: '5 expected transactions',
|
||
valid: true,
|
||
data: [
|
||
['transactions', 5, '5']
|
||
]
|
||
},
|
||
{
|
||
name: '6 expected transactions (still bucket 5+)',
|
||
valid: true,
|
||
data: [
|
||
['transactions', 6, '5']
|
||
]
|
||
},
|
||
{
|
||
name: '100 expected transactions (still bucket 5+)',
|
||
valid: true,
|
||
data: [
|
||
['transactions', 100, '5']
|
||
]
|
||
},
|
||
{
|
||
name: 'actual transactions not a valid bucket (2)',
|
||
valid: false,
|
||
data: [
|
||
['transactions', 2, '2']
|
||
]
|
||
},
|
||
{
|
||
name: 'actual transactions not a valid bucket (4)',
|
||
valid: false,
|
||
data: [
|
||
['transactions', 4, '4']
|
||
]
|
||
},
|
||
{
|
||
name: 'actual transactions not a valid bucket (6)',
|
||
valid: false,
|
||
data: [
|
||
['transactions', 6, '6']
|
||
]
|
||
},
|
||
{
|
||
name: 'actual transactions not a valid bucket (99)',
|
||
valid: false,
|
||
data: [
|
||
['transactions', 99, '99']
|
||
]
|
||
},
|
||
{
|
||
name: 'actual transactions don\'t match',
|
||
valid: false,
|
||
data: [
|
||
['transactions', 1, '0']
|
||
]
|
||
},
|
||
{
|
||
name: 'device number doesn\'t match',
|
||
valid: false,
|
||
data: [
|
||
['device', '+447700900123', '+447700900124']
|
||
]
|
||
},
|
||
{
|
||
name: 'non-international format number',
|
||
valid: false,
|
||
data: [
|
||
['device', '+447700900123', '07700900123']
|
||
]
|
||
},
|
||
{
|
||
name: 'date of birth in ISO 8601 with a time',
|
||
valid: true,
|
||
data: [
|
||
['dob', '1970-01-02', '1970-01-02T01:23:45']
|
||
]
|
||
},
|
||
{
|
||
name: 'date of birth in ISO 8601 without a time',
|
||
valid: true,
|
||
data: [
|
||
['dob', '1970-02-03', '1970-02-03']
|
||
]
|
||
},
|
||
{
|
||
name: 'wrong day only',
|
||
valid: false,
|
||
data: [
|
||
['dob', '1970-01-01', '1970-01-02']
|
||
]
|
||
},
|
||
{
|
||
name: 'wrong month only',
|
||
valid: false,
|
||
data: [
|
||
['dob', '1970-01-01', '1970-02-01']
|
||
]
|
||
},
|
||
{
|
||
name: 'wrong year only',
|
||
valid: false,
|
||
data: [
|
||
['dob', '1970-01-01', '1971-01-01']
|
||
]
|
||
},
|
||
{
|
||
name: 'date of birth in a non-ISO 8601 format (long)',
|
||
valid: false,
|
||
data: [
|
||
['dob', '1970-01-01', '1st January 1970']
|
||
]
|
||
},
|
||
{
|
||
name: 'date of birth in a non-ISO 8601 format (short)',
|
||
valid: false,
|
||
data: [
|
||
['dob', '1970-01-01', '01/01/1970']
|
||
]
|
||
}
|
||
];
|
||
|
||
/**
|
||
* Converts the above test cases to the formats expected by the verifyAnswers().
|
||
*
|
||
* @param {Object[]} cases - List of test cases.
|
||
*
|
||
* @returns {Object} - Object with list of expected results and answers to test with.
|
||
*/
|
||
function testCaseToVerifyAnswers(cases) {
|
||
const result = {
|
||
expected: [],
|
||
actual: []
|
||
};
|
||
|
||
for (let i = 0; i < cases.length; ++i) {
|
||
const tc = cases[i];
|
||
|
||
result.expected.push({
|
||
questionID: i,
|
||
questionType: tc[0],
|
||
answer: tc[1]
|
||
});
|
||
|
||
result.actual.push({
|
||
questionID: i,
|
||
questionType: tc[0],
|
||
answer: tc[2]
|
||
});
|
||
}
|
||
|
||
return result;
|
||
}
|
||
|
||
/**
|
||
* Unit test definitions
|
||
*/
|
||
describe('api_recovery_controller', () => {
|
||
describe('verifyAnswers', () => {
|
||
/**
|
||
* Get the private function using the `rewire`-ed controller object.
|
||
*/
|
||
const verifyAnswers = recoveryController.__get__('verifyAnswers');
|
||
|
||
for (let i = 0; i < kbaTestCases.length; ++i) {
|
||
/**
|
||
* Get the testcase, then the test data in the correct format.
|
||
*/
|
||
const tc = kbaTestCases[i];
|
||
const tcConverted = testCaseToVerifyAnswers(tc.data);
|
||
|
||
/**
|
||
* Build a meaningful name for the test.
|
||
*/
|
||
let name = tc.valid ? 'should accept ' : 'should NOT accept ';
|
||
|
||
name += tc.name;
|
||
|
||
/**
|
||
* Run a test for this case.
|
||
*/
|
||
it(name, () => {
|
||
const expected = expect(verifyAnswers(tcConverted.actual, tcConverted.expected));
|
||
|
||
if (tc.valid) {
|
||
return expected.to.eventually.be.fulfilled;
|
||
} else {
|
||
return expected.to.eventually.be.rejected;
|
||
}
|
||
});
|
||
}
|
||
});
|
||
});
|