Browse Source

add integrations test - should exclude outgoing notifications

activeAddress
Gabriel Bazán 9 years ago
parent
commit
ba1c603977
  1. 16
      lib/pushnotificationsservice.js
  2. 95
      test/integration/pushNotifications.js

16
lib/pushnotificationsservice.js

@ -31,7 +31,7 @@ var PUSHNOTIFICATIONS_TYPES = {
}, },
'TxProposalFinallyRejected': { 'TxProposalFinallyRejected': {
filename: 'txp_finally_rejected', filename: 'txp_finally_rejected',
} },
}; };
function PushNotificationsService() {}; function PushNotificationsService() {};
@ -100,6 +100,10 @@ PushNotificationsService.prototype._sendPushNotifications = function(notificatio
// console.log(notification); // console.log(notification);
self._checkShouldSendNotif(notification, function(err, should) {
if (err) return cb(err);
if (!should) return cb();
self._getRecipientsList(notification, function(err, recipientsList) { self._getRecipientsList(notification, function(err, recipientsList) {
if (err) return cb(err); if (err) return cb(err);
@ -145,6 +149,16 @@ PushNotificationsService.prototype._sendPushNotifications = function(notificatio
return cb(err); return cb(err);
}); });
}); });
});
};
PushNotificationsService.prototype._checkShouldSendNotif = function(notification, cb) {
var self = this;
if (notification.type != 'NewTxProposal') return cb(null, true);
self.storage.fetchWallet(notification.walletId, function(err, wallet) {
return cb(err, wallet.m > 1);
});
}; };
PushNotificationsService.prototype._getRecipientsList = function(notification, cb) { PushNotificationsService.prototype._getRecipientsList = function(notification, cb) {

95
test/integration/pushNotifications.js

@ -38,6 +38,7 @@ describe('Push notifications', function() {
helpers.getAuthServer(copayer.id, function(server) { helpers.getAuthServer(copayer.id, function(server) {
server.savePreferences({ server.savePreferences({
email: 'copayer' + (++i) + '@domain.com', email: 'copayer' + (++i) + '@domain.com',
language: 'en',
unit: 'bit', unit: 'bit',
}, next); }, next);
}); });
@ -74,6 +75,10 @@ describe('Push notifications', function() {
}); });
it('should build each notifications using preferences of the copayers', function(done) { it('should build each notifications using preferences of the copayers', function(done) {
server.savePreferences({
language: 'en',
unit: 'bit',
}, function(err) {
server.createAddress({}, function(err, address) { server.createAddress({}, function(err, address) {
should.not.exist(err); should.not.exist(err);
@ -90,17 +95,17 @@ describe('Push notifications', function() {
var args = _.map(calls, function(c) { var args = _.map(calls, function(c) {
return c.args[0]; return c.args[0];
}); });
calls.length.should.equal(1);
args[0].body.android.data.title.should.contain('New payment received'); args[0].body.android.data.title.should.contain('New payment received');
args[0].body.android.data.message.should.contain('123,000'); args[0].body.android.data.message.should.contain('123,000');
done(); done();
}, 100); }, 100);
}); });
}); });
}); });
});
it('number of calls should be 0', function(done) { it('should not notify auto-payments to creator', function(done) {
server.createAddress({}, function(err, address) { server.createAddress({}, function(err, address) {
should.not.exist(err); should.not.exist(err);
@ -115,14 +120,13 @@ describe('Push notifications', function() {
setTimeout(function() { setTimeout(function() {
var calls = requestStub.getCalls(); var calls = requestStub.getCalls();
calls.length.should.equal(0); calls.length.should.equal(0);
done(); done();
}, 100); }, 100);
}); });
}); });
}); });
it('number of calls should be 1', function(done) { it('should notify copayers when payment is received', function(done) {
server.createAddress({}, function(err, address) { server.createAddress({}, function(err, address) {
should.not.exist(err); should.not.exist(err);
@ -137,7 +141,6 @@ describe('Push notifications', function() {
setTimeout(function() { setTimeout(function() {
var calls = requestStub.getCalls(); var calls = requestStub.getCalls();
calls.length.should.equal(1); calls.length.should.equal(1);
done(); done();
}, 100); }, 100);
}); });
@ -193,6 +196,11 @@ describe('Push notifications', function() {
}); });
it('should build each notifications using preferences of the copayers', function(done) { it('should build each notifications using preferences of the copayers', function(done) {
server.savePreferences({
email: 'copayer1@domain.com',
language: 'es',
unit: 'btc',
}, function(err) {
server.createAddress({}, function(err, address) { server.createAddress({}, function(err, address) {
should.not.exist(err); should.not.exist(err);
@ -210,16 +218,24 @@ describe('Push notifications', function() {
return c.args[0]; return c.args[0];
}); });
args[0].body.android.data.title.should.contain('New payment received'); calls.length.should.equal(3);
args[0].body.android.data.message.should.contain('123,000');
args[0].body.android.data.title.should.contain('Nuevo pago recibido');
args[0].body.android.data.message.should.contain('0.123');
args[1].body.android.data.title.should.contain('New payment received');
args[1].body.android.data.message.should.contain('123,000');
args[2].body.android.data.title.should.contain('New payment received');
args[2].body.android.data.message.should.contain('123,000');
done(); done();
}, 100); }, 100);
}); });
}); });
}); });
});
it('number of calls should be 3', function(done) { it('should notify copayers when payment is received', function(done) {
server.createAddress({}, function(err, address) { server.createAddress({}, function(err, address) {
should.not.exist(err); should.not.exist(err);
@ -241,7 +257,7 @@ describe('Push notifications', function() {
}); });
}); });
it('number of calls should be 2', function(done) { it('should not notify auto-payments to creator', function(done) {
server.createAddress({}, function(err, address) { server.createAddress({}, function(err, address) {
should.not.exist(err); should.not.exist(err);
@ -328,6 +344,63 @@ describe('Push notifications', function() {
}); });
}); });
}); });
it('should notify copayers a new outgoing tx has been created', function(done) {
helpers.stubUtxos(server, wallet, 1, function() {
var txOpts = helpers.createSimpleProposalOpts('18PzpUFkFZE8zKWUPvfykkTxmB9oMR8qP7', 0.8, TestData.copayers[0].privKey_1H_0, {
message: 'some message'
});
var txp;
async.waterfall([
function(next) {
server.createTxLegacy(txOpts, next);
},
function(t, next) {
txp = t;
async.eachSeries(_.range(1, 3), function(i, next) {
var copayer = TestData.copayers[i];
helpers.getAuthServer(copayer.id44, function(s) {
server = s;
var signatures = helpers.clientSign(txp, copayer.xPrivKey_44H_0H_0H);
server.signTx({
txProposalId: txp.id,
signatures: signatures,
}, function(err, t) {
txp = t;
next();
});
});
}, next);
},
function(next) {
helpers.stubBroadcast();
server.broadcastTx({
txProposalId: txp.id,
}, next);
},
], function(err) {
should.not.exist(err);
setTimeout(function() {
var calls = requestStub.getCalls();
var args = _.map(_.takeRight(calls, 2), function(c) {
return c.args[0];
});
args[0].body.android.data.title.should.contain('Payment sent');
args[1].body.android.data.title.should.contain('Payment sent');
server.getWallet(null, function(err, w) {
server.copayerId.should.not.equal((args[0].body.users[0]).split('$')[1]);
server.copayerId.should.not.equal((args[1].body.users[0]).split('$')[1]);
done();
});
}, 100);
});
});
});
}); });
describe('joinWallet', function() { describe('joinWallet', function() {
@ -372,7 +445,7 @@ describe('Push notifications', function() {
}); });
}); });
it.only('should notify copayers when a new copayer just joined into your wallet except the one who joined', function(done) { it('should notify copayers when a new copayer just joined into your wallet except the one who joined', function(done) {
async.eachSeries(_.range(3), function(i, next) { async.eachSeries(_.range(3), function(i, next) {
var copayerOpts = helpers.getSignedCopayerOpts({ var copayerOpts = helpers.getSignedCopayerOpts({
walletId: walletId, walletId: walletId,

Loading…
Cancel
Save