refactor PayPro tests

This commit is contained in:
Matias Alejo Garcia 2014-11-23 00:29:50 -03:00
commit ae0dd40903
7 changed files with 349 additions and 1135 deletions

View file

@ -168,7 +168,7 @@ angular.module('copayApp.controllers').controller('SendController',
toAddress: address,
amountSat: amount,
comment: commentText,
url: (payInfo && payInfo.merchant) ? url : null,
url: (payInfo && payInfo.merchant) ? payInfo.merchant : null,
}, $scope._afterSend);
};

View file

@ -77,7 +77,7 @@ TxProposal.prototype._checkPayPro = function() {
if (ppOut.address !== txOut.address)
throw new Error('PayPro: Wrong out address in Tx');
if (ppOut.amountSatStr !== txOut.amountSatStr)
if (ppOut.amountSatStr !== txOut.amountSatStr + '')
throw new Error('PayPro: Wrong amount in Tx');
};
@ -112,8 +112,13 @@ TxProposal.prototype.trimForStorage = function() {
};
TxProposal.prototype.addMerchantData = function(merchantData) {
preconditions.checkArgument(merchantData.pr);
preconditions.checkArgument(merchantData.request_url);
var m = _.clone(merchantData);
if (!this.paymentProtocolURL)
this.paymentProtocolURL = m.request_url;
// remove unneeded data
m.raw = m.pr.pki_data = m.pr.signature = undefined;
this.merchant = m;

View file

@ -1535,12 +1535,9 @@ Wallet.prototype.fetchPaymentRequest = function(options, cb) {
.success(function(rawData) {
log.info('PayPro Request done successfully. Parsing response')
var data = PayPro.PaymentRequest.decode(rawData);
var paypro = new PayPro();
var pr = paypro.makePaymentRequest(data);
var merchantData, err;
try {
merchantData = self.parsePaymentRequest(options, pr);
merchantData = self.parsePaymentRequest(options, rawData);
} catch (e) {
err = e
};
@ -1550,7 +1547,6 @@ Wallet.prototype.fetchPaymentRequest = function(options, cb) {
})
.error(function(data, status) {
log.debug('Server did not return PaymentRequest.\nXHR status: ' + status);
preconditions.checkState(options.fetch);
return cb(new Error('Status: ' + status));
});
};
@ -1635,18 +1631,16 @@ Wallet.prototype._addOutputsToMerchantData = function(merchantData) {
/**
* @desc Analyzes a payment request and generate merchantData
* @param {Object} options
* @param {PayProRequest} pr
* @param {string} pr.payment_details_version
* @param {string} pr.pki_type
* @param {Object} pr.data
* @param {string} pr.serialized_payment_details
* @param {string} pr.signature
* @param {string} options.memo
* @param {string} options.comment
* @param {string} options.url url where the pay request was acquired
* @param {string} options.amount Only used if pay requesst allow user to set the amount
* @param {PayProRequest} rawData
*/
Wallet.prototype.parsePaymentRequest = function(options, pr) {
Wallet.prototype.parsePaymentRequest = function(options, rawData) {
var self = this;
var data = PayPro.PaymentRequest.decode(rawData);
var paypro = new PayPro();
var pr = paypro.makePaymentRequest(data);
var ver = pr.get('payment_details_version');
var pki_type = pr.get('pki_type');
var pki_data = pr.get('pki_data');
@ -2132,8 +2126,7 @@ Wallet.prototype.removeTxWithSpentInputs = function(cb) {
*/
Wallet.prototype.createTx = function(opts, cb) {
preconditions.checkArgument(_.isObject(opts));
preconditions.checkArgument(opts.amountSat);
log.debug(opts);
log.debug('create Options', opts);
var self = this;
var toAddress = opts.toAddress;
@ -2142,23 +2135,26 @@ Wallet.prototype.createTx = function(opts, cb) {
var url = opts.url;
if (url && !opts.merchantData) {
w.fetchPaymentRequest({
return self.fetchPaymentRequest({
url: url,
memo: comment,
amount: amountSat,
}, function(err, merchantData) {
if (err) return cb(err);
opts.merchantData = merchantData;
opts.amountSat = merchantData.outs[0].address;
opts.toAddress = merchantData.outs[0].amount;
self.createTx(opts, cb);
opts.toAddress = merchantData.outs[0].address;
opts.amountSat = parseInt(merchantData.outs[0].amountSatStr);
return self.createTx(opts, cb);
});
};
preconditions.checkArgument(amountSat);
preconditions.checkArgument(toAddress);
preconditions.checkArgument(amountSat, 'no amount');
preconditions.checkArgument(toAddress, 'no address');
this.getUnspent(function(err, safeUnspent) {
if (err) return cb(new Error('Could not get list of UTXOs'));
if (err) {
log.info(err);
return cb(new Error('CreateTx: Could not get list of UTXOs'));
}
var ntxid, txp;
try {
@ -2168,8 +2164,9 @@ Wallet.prototype.createTx = function(opts, cb) {
return cb(e);
}
if (opts.merchantData)
if (opts.merchantData) {
txp.addMerchantData(opts.merchantData);
}
var ntxid = self.txProposals.add(txp);
log.debug('TXP Added: ', ntxid);
@ -2207,9 +2204,10 @@ Wallet.prototype.createTxProposal = function(toAddress, amountSat, comment, utxo
var pkr = this.publicKeyRing;
var priv = this.privateKey;
toAddress = sanitize(toAddress);
var addr = sanitize(toAddress);
preconditions.checkState(addr && addr.data && addr.isValid(), 'Bad address:' + addr.toString());
preconditions.checkArgument(toAddress.network().name === this.getNetworkName(), 'networkname mismatch');
preconditions.checkArgument(addr.network().name === this.getNetworkName(), 'networkname mismatch');
preconditions.checkState(pkr.isComplete(), 'pubkey ring incomplete');
preconditions.checkState(priv, 'no private key');
@ -2229,7 +2227,7 @@ Wallet.prototype.createTxProposal = function(toAddress, amountSat, comment, utxo
var b = new Builder(builderOpts)
.setUnspent(utxos)
.setOutputs([{
address: toAddress.data,
address: addr.data,
amountSatStr: amountSat,
}]);

75
js/util/HTTP.js Normal file
View file

@ -0,0 +1,75 @@
module.exports = {
request: function(options, callback) {
preconditions.checkArgument(_.isObject(options));
options.method = options.method || 'GET';
options.headers = options.headers || {};
var ret = {
success: function(cb) {
this._success = cb;
return this;
},
error: function(cb) {
this._error = cb;
return this;
},
_success: function() {;
},
_error: function(_, err) {
throw err;
}
};
var method = (options.method || 'GET').toUpperCase();
var url = options.url;
var req = options;
req.headers = req.headers || {};
req.body = req.body || req.data || {};
var xhr = new XMLHttpRequest();
xhr.open(method, url, true);
Object.keys(req.headers).forEach(function(key) {
var val = req.headers[key];
if (key === 'Content-Length') return;
if (key === 'Content-Transfer-Encoding') return;
xhr.setRequestHeader(key, val);
});
if (req.responseType) {
xhr.responseType = req.responseType;
}
xhr.onload = function(event) {
var response = xhr.response;
var buf = new Uint8Array(response);
var headers = {};
(xhr.getAllResponseHeaders() || '').replace(
/(?:\r?\n|^)([^:\r\n]+): *([^\r\n]+)/g,
function($0, $1, $2) {
headers[$1.toLowerCase()] = $2;
}
);
return ret._success(buf, xhr.status, headers, options);
};
xhr.onerror = function(event) {
var status;
if (xhr.status === 0 || !xhr.statusText) {
status = 'HTTP Request Error: This endpoint likely does not support cross-origin requests.';
} else {
status = xhr.statusText;
}
return ret._error(null, status, null, options);
};
if (req.body) {
xhr.send(req.body);
} else {
xhr.send(null);
}
return ret;
},
};