/** * @fileOverview End-to-end testing of the payment instruments add card swagger API */ 'use strict'; // eslint-disable-next-line no-unused-vars const testGlobals = require('../../tools/test/testGlobals.js'); const request = require('supertest'); const express = require('express'); const _ = require('lodash'); const initDevApi = require('../dev_server.js'); function respondsWithValue(thisApp, params, header, value) { return request(thisApp) .post('/dev/v0/payment-instruments/cards') .set('Accept', 'application/json') .set('Authorization', header) .send(params) .expect(value); } /** * Test values */ // Correct auth method (Bearer), correct token const TOKEN_VALID = 'YTM2ZGQ1NzUtOWFmNS01MjMyLTg5MjYtM2NkZjA5ZDU2ZGU1'; const HEADER_VALID = 'Bearer ' + TOKEN_VALID; // Standard errored values const TOO_LONG = '0123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789' + '0123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789' + '012345678901234567890123456789012345678901234567890123456789'; const BAD_DATE_FMT = '12?18'; const BAD_DATE_MON = '34-18'; const BAD_PANA = '123A 1234 1234 1234'; const BAD_PANB = '147100001111222 '; const BAD_POSTCODE = 'AB12 &CD'; const BAD_PHONENUM = '012345G 67890'; const BAD_NAME = '^£&$£%&$'; const BAD_EMAIL = 'a@b@c.com'; // Valid test data const correctParameters = { payer: { email: 'peon@example.com', firstName: 'John', lastName: 'Doe' }, description: 'A random bank card.', card: { nameOnCard: 'John Doe', PAN: '4444 3333 2222 1111', expiryDate: '11-22', startDate: '11-20', issueNumber: 1, CV2: '012', address: { address1: 'First line of address', address2: 'Second line of address', address3: 'Third line of addresst', town: 'Christchurch', county: 'Dorset', postcode: 'BH23 6AA', phoneNumber: '+44 123 1110000' } } }; let badParameters; describe('E2E: save card for future use request', () => { let app; /** * Load the dev API router to handle `/dev/*` routes */ before(() => { app = express(); const devApiRouter = initDevApi.init(); app.use('/dev', devApiRouter); }); describe('tests with missing required parameters', () => { /* * Tests where required top level attributes are missing. * ====================================================== */ /* * No payer */ badParameters = _.cloneDeep(correctParameters); delete badParameters.payer; it('with no payer parameter', () => { return respondsWithValue(app, badParameters, HEADER_VALID, 400); }); /* * No card */ badParameters = _.cloneDeep(correctParameters); delete badParameters.card; it('with no card parameter', () => { return respondsWithValue(app, badParameters, HEADER_VALID, 400); }); /* * Tests where required child attributes are missing. * ====================================================== */ /* * No payer.email */ badParameters = _.cloneDeep(correctParameters); delete badParameters.payer.email; it('with no payer.email parameter', () => { return respondsWithValue(app, badParameters, HEADER_VALID, 400); }); /* * No payer.firstName */ badParameters = _.cloneDeep(correctParameters); delete badParameters.payer.firstName; it('with no payer.firstName parameter', () => { return respondsWithValue(app, badParameters, HEADER_VALID, 400); }); /* * No payer.lastName */ badParameters = _.cloneDeep(correctParameters); delete badParameters.payer.lastName; it('with no payer.lastName parameter', () => { return respondsWithValue(app, badParameters, HEADER_VALID, 400); }); /* * No card.PAN */ badParameters = _.cloneDeep(correctParameters); delete badParameters.card.PAN; it('with no card.PAN parameter', () => { return respondsWithValue(app, badParameters, HEADER_VALID, 400); }); /* * No expiryDate */ badParameters = _.cloneDeep(correctParameters); delete badParameters.card.expiryDate; it('with no card.expiryDate parameter', () => { return respondsWithValue(app, badParameters, HEADER_VALID, 400); }); /* * No CV2 */ badParameters = _.cloneDeep(correctParameters); delete badParameters.card.CV2; it('with no card.CV2 parameter', () => { return respondsWithValue(app, badParameters, HEADER_VALID, 400); }); /* * No name on card field */ badParameters = _.cloneDeep(correctParameters); delete badParameters.card.nameOnCard; it('with no card.nameOnCard parameter', () => { return respondsWithValue(app, badParameters, HEADER_VALID, 400); }); /* * No address */ badParameters = _.cloneDeep(correctParameters); delete badParameters.card.address; it('with no card.address parameter', () => { return respondsWithValue(app, badParameters, HEADER_VALID, 400); }); /* * No card.address.address1 */ badParameters = _.cloneDeep(correctParameters); delete badParameters.card.address.address1; it('with no card.address.address1 parameter', () => { return respondsWithValue(app, badParameters, HEADER_VALID, 400); }); /* * No card.address.town */ badParameters = _.cloneDeep(correctParameters); delete badParameters.card.address.town; it('with no card.address.town parameter', () => { return respondsWithValue(app, badParameters, HEADER_VALID, 400); }); /* * No card.address.postcode */ badParameters = _.cloneDeep(correctParameters); delete badParameters.card.address.postcode; it('with no card.address.postcode parameter', () => { return respondsWithValue(app, badParameters, HEADER_VALID, 400); }); }); describe('bad data format tests', () => { /* * Invalid data format errors * ========================== */ /* * Bad payer first name */ badParameters = _.cloneDeep(correctParameters); badParameters.payer.firstName = BAD_NAME; it('with a badly formatted first name parameter', () => { return respondsWithValue(app, badParameters, HEADER_VALID, 400); }); /* * Bad payer last name */ badParameters = _.cloneDeep(correctParameters); badParameters.payer.lastName = BAD_NAME; it('with a badly formatted last name parameter', () => { return respondsWithValue(app, badParameters, HEADER_VALID, 400); }); /* * Bad payer email */ badParameters = _.cloneDeep(correctParameters); badParameters.payer.email = BAD_EMAIL; it('with a badly formatted email parameter', () => { return respondsWithValue(app, badParameters, HEADER_VALID, 400); }); /* * Bad card description field */ badParameters = _.cloneDeep(correctParameters); badParameters.description = TOO_LONG; it('with a badly formatted card description parameter', () => { return respondsWithValue(app, badParameters, HEADER_VALID, 400); }); /* * Bad PAN */ badParameters = _.cloneDeep(correctParameters); badParameters.card.PAN = BAD_PANA; it('with a bad card PAN parameter containing a letter', () => { return respondsWithValue(app, badParameters, HEADER_VALID, 400); }); badParameters = _.cloneDeep(correctParameters); badParameters.card.PAN = BAD_PANB; it('with a bad card PAN parameter with a trailing space', () => { return respondsWithValue(app, badParameters, HEADER_VALID, 400); }); /* * Bad expiryDate 1 */ badParameters = _.cloneDeep(correctParameters); badParameters.card.expiryDate = BAD_DATE_FMT; it('with a bad character in the card expiry date parameter', () => { return respondsWithValue(app, badParameters, HEADER_VALID, 400); }); /* * Bad expiryDate 2 */ badParameters = _.cloneDeep(correctParameters); badParameters.card.expiryDate = BAD_DATE_MON; it('with a bad month number in the card expiry date parameter', () => { return respondsWithValue(app, badParameters, HEADER_VALID, 400); }); /* * Bad startDate */ badParameters = _.cloneDeep(correctParameters); badParameters.card.startDate = BAD_DATE_FMT; it('with a badly formatted card start date parameter', () => { return respondsWithValue(app, badParameters, HEADER_VALID, 400); }); /* * Bad startDate */ badParameters = _.cloneDeep(correctParameters); badParameters.card.startDate = BAD_DATE_MON; it('with a bad month number in the card start date parameter', () => { return respondsWithValue(app, badParameters, HEADER_VALID, 400); }); /* * Bad issueNumber */ badParameters = _.cloneDeep(correctParameters); badParameters.card.issueNumber = 'Z'; it('with a badly formatted card issue number parameter', () => { return respondsWithValue(app, badParameters, HEADER_VALID, 400); }); /* * Bad card.address.address1: too long */ badParameters = _.cloneDeep(correctParameters); badParameters.card.address.address1 = TOO_LONG; it('with a card address line 1 too long', () => { respondsWithValue(app, badParameters, HEADER_VALID, 400); }); /* * Bad card.address.address1: too short */ badParameters = _.cloneDeep(correctParameters); badParameters.card.address.address1 = ''; it('with a card address line 1 too short', () => { return respondsWithValue(app, badParameters, HEADER_VALID, 400); }); /* * Bad card.address.address2 */ badParameters = _.cloneDeep(correctParameters); badParameters.card.address.address2 = TOO_LONG; it('with a badly formatted card address line 2 parameter', () => { return respondsWithValue(app, badParameters, HEADER_VALID, 400); }); /* * Bad card.address.address3 */ badParameters = _.cloneDeep(correctParameters); badParameters.card.address.address3 = TOO_LONG; it('with a badly formatted card address line 3 parameter', () => { return respondsWithValue(app, badParameters, HEADER_VALID, 400); }); /* * Bad card.address.town */ badParameters = _.cloneDeep(correctParameters); badParameters.card.address.town = TOO_LONG; it('with a badly formatted card address town name parameter', () => { return respondsWithValue(app, badParameters, HEADER_VALID, 400); }); /* * Bad card.address.county */ badParameters = _.cloneDeep(correctParameters); badParameters.card.address.county = TOO_LONG; it('with a badly formatted card address county name parameter', () => { return respondsWithValue(app, badParameters, HEADER_VALID, 400); }); /* * ) Bad card.address.postcode */ badParameters = _.cloneDeep(correctParameters); badParameters.card.address.postcode = BAD_POSTCODE; it('with a badly formatted card address postcode parameter', () => { return respondsWithValue(app, badParameters, HEADER_VALID, 400); }); /* * ) Bad card.address.phoneNumber */ badParameters = _.cloneDeep(correctParameters); badParameters.card.address.phoneNumber = BAD_PHONENUM; it('with a badly formatted card address phone number parameter', () => { return respondsWithValue(app, badParameters, HEADER_VALID, 400); }); }); /* Skip these tests as they will cahnge when the actual function is implemented. */ describe.skip('Good parameter data tests', () => { /* * Verify that the command correctly validates a minimum set of correctly formatted parameters. */ const minimumValidSet = _.cloneDeep(correctParameters); delete minimumValidSet.description; delete minimumValidSet.card.startDate; delete minimumValidSet.card.issueNumber; delete minimumValidSet.card.address.address2; delete minimumValidSet.card.address.address3; delete minimumValidSet.card.address.county; delete minimumValidSet.card.address.phoneNumber; it('with the minimum set of correct parameters', () => { return respondsWithValue(app, minimumValidSet, HEADER_VALID, 500); }); /* * Verify that the command works with correctly formatted parameters. */ it('with a full set of correct parameters', () => { return respondsWithValue(app, correctParameters, HEADER_VALID, 500); }); }); });