Skip to content

Commit

Permalink
A couple of optimisations done
Browse files Browse the repository at this point in the history
- Finished issue #24
- removed unrequired dependencies
- mocked ProcessSuccess and SOAPRequest controllers post requests
- removed option of using default environment provided TEST phonenumber and amount, use POSTMAN or CURL now
- Updated the node required to version to v4.3.2 or higher
  • Loading branch information
kn9ts committed May 28, 2016
1 parent 1dfcbc3 commit 9075795
Show file tree
Hide file tree
Showing 4 changed files with 27 additions and 13 deletions.
3 changes: 1 addition & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,6 @@
"express": "^4.13.4",
"express-session": "^1.13.0",
"jade": "~1.11.0",
"lodash": "^4.12.0",
"moment": "^2.13.0",
"morgan": "~1.6.1",
"node-uuid": "^1.4.7",
Expand All @@ -56,6 +55,6 @@
"sinon": "^1.17.4"
},
"engines": {
"node": ">=4.2.0"
"node": ">=4.3.2",
}
}
11 changes: 5 additions & 6 deletions server/controllers/PaymentRequest.js
Original file line number Diff line number Diff line change
Expand Up @@ -25,13 +25,12 @@ class PaymentRequest {
</soapenv:Header>
<soapenv:Body>
<tns:processCheckOutRequest>
<MERCHANT_TRANSACTION_ID>${data.merchantTransactionID}</MERCHANT_TRANSACTION_ID>
<MERCHANT_TRANSACTION_ID>${String(data.merchantTransactionID).slice(0, 8)}
</MERCHANT_TRANSACTION_ID>
<REFERENCE_ID>${data.referenceID}</REFERENCE_ID>
<AMOUNT>${data.amountInDoubleFloat}</AMOUNT>
<MSISDN>${data.clientPhoneNumber}</MSISDN>
<ENC_PARAMS>
${data.extraPayload ? JSON.stringify(data.extraPayload) : ''}
</ENC_PARAMS>
<ENC_PARAMS>${JSON.stringify(data.extraPayload)}</ENC_PARAMS>
<CALL_BACK_URL>${process.env.CALLBACK_URL}</CALL_BACK_URL>
<CALL_BACK_METHOD>${process.env.CALLBACK_METHOD}</CALL_BACK_METHOD>
<TIMESTAMP>${data.timeStamp}</TIMESTAMP>
Expand All @@ -48,8 +47,8 @@ class PaymentRequest {
referenceID: (req.body.referenceID || uuid.v4()),
// product, service or order ID
merchantTransactionID: (req.body.merchantTransactionID || uuid.v1()),
amountInDoubleFloat: (req.body.totalAmount || process.env.TEST_AMOUNT),
clientPhoneNumber: (req.body.phoneNumber || process.env.TEST_PHONENUMBER),
amountInDoubleFloat: req.body.totalAmount,
clientPhoneNumber: req.body.phoneNumber,
extraPayload: req.body.extraPayload,
timeStamp: req.timeStamp,
encryptedPassword: req.encryptedPassword,
Expand Down
10 changes: 8 additions & 2 deletions test/controllers/PaymentSuccess.js
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,11 @@ req.body = {
const res = {};
res.sendStatus = sinon.stub();

let error = false;
sinon.stub(paymentSuccess, 'request', (params, callback) => {
callback(error);
});

describe('paymentSuccess', () => {
it('Make a request to MERCHANT_ENDPOINT and respond to SAG with OK', (done) => {
process.env.MERCHANT_ENDPOINT = process.env.ENDPOINT;
Expand All @@ -35,16 +40,17 @@ describe('paymentSuccess', () => {
setTimeout(() => {
assert.isTrue(res.sendStatus.calledWithExactly(200));
done();
}, 1500);
}, 20);
});

it('If ENDPOINT is not reachable, an error reponse is sent back', (done) => {
delete process.env.MERCHANT_ENDPOINT;
error = new Error('ENDPOINT not reachable');
paymentSuccess.handler(req, res);

setTimeout(() => {
assert.isTrue(res.sendStatus.calledWithExactly(500));
done();
}, 1000);
}, 20);
});
});
16 changes: 13 additions & 3 deletions test/controllers/SOAPRequest.js
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ const paymentDetails = {
encryptedPassword,
};

const parser = new ParseResponse('processcheckoutresponse');
const parser = new ParseResponse('bodyTagName');
parser.parse = sinon.stub().returns(parser);
parser.toJSON = sinon.stub();
parser.toJSON.onFirstCall().returns({ status_code: 200 });
Expand All @@ -34,8 +34,16 @@ const soapRequest = new SOAPRequest();
paymentRequest.buildSoapBody(paymentDetails);
soapRequest.construct(paymentRequest, parser);

let requestError = undefined;
sinon.stub(soapRequest, 'request', (params, callback) => {
callback(requestError, null, 'a soap dom tree string');
});

describe('SOAPRequest', () => {
afterEach(() => {
requestError = undefined;
});

it('SOAPRequest is contructed', () => {
assert.instanceOf(soapRequest.parser, ParseResponse);
assert.sameMembers(Object.keys(soapRequest.requestOptions), [
Expand All @@ -54,6 +62,7 @@ describe('SOAPRequest', () => {
assert.sameMembers(Object.keys(response), ['status_code']);
assert.isTrue(soapRequest.parser.parse.called);
assert.isTrue(soapRequest.parser.toJSON.called);
assert.isTrue(soapRequest.request.called);
done();
});
});
Expand All @@ -65,21 +74,22 @@ describe('SOAPRequest', () => {
assert.sameMembers(Object.keys(error), ['status_code']);
assert.isTrue(soapRequest.parser.parse.called);
assert.isTrue(soapRequest.parser.toJSON.called);
assert.isTrue(soapRequest.request.called);
done();
});
});


it('Invokes catch method if an error is returned on invalid request', (done) => {
process.env.ENDPOINT = 'undefined';
soapRequest.construct(paymentRequest, parser);
requestError = new Error('invalid URI provided');

const request = soapRequest.post().catch((error) => {
assert.instanceOf(request, Promise);
assert.isObject(error);
assert.sameMembers(Object.keys(error), ['description']);
assert.isTrue(soapRequest.parser.parse.called);
assert.isTrue(soapRequest.parser.toJSON.called);
assert.isTrue(soapRequest.request.called);
done();
});
});
Expand Down

0 comments on commit 9075795

Please sign in to comment.