From bd7bcbab450576ae996039b28655a140860394e3 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Gabriel=20Baz=C3=A1n?= Date: Fri, 15 Jan 2016 17:16:10 -0300 Subject: [PATCH] push notifications unsubscribe method refactor --- lib/expressapp.js | 13 ++----------- lib/server.js | 15 +++++---------- test/integration/server.js | 30 +++++++++--------------------- 3 files changed, 16 insertions(+), 42 deletions(-) diff --git a/lib/expressapp.js b/lib/expressapp.js index e627ee0..af850fb 100644 --- a/lib/expressapp.js +++ b/lib/expressapp.js @@ -527,18 +527,9 @@ ExpressApp.prototype.start = function(opts, cb) { }); }); - router.delete('/v1/pushnotifications/subscriptions/', function(req, res) { + router.delete('/v1/pushnotifications/subscriptions/:token/', function(req, res) { getServerWithAuth(req, res, function(server) { - server.pushNotificationsUnsubscribe(null, function(err, response) { - if (err) return returnError(err, res, req); - res.json(response); - }); - }); - }); - - router.delete('/v1/pushnotifications/subscriptions/:opts/', function(req, res) { - getServerWithAuth(req, res, function(server) { - server.pushNotificationsUnsubscribe(req.params['opts'], function(err, response) { + server.pushNotificationsUnsubscribe(req.params['token'], function(err, response) { if (err) return returnError(err, res, req); res.json(response); }); diff --git a/lib/server.js b/lib/server.js index a60589a..07e5338 100644 --- a/lib/server.js +++ b/lib/server.js @@ -2430,7 +2430,7 @@ WalletService.prototype.getFiatRate = function(opts, cb) { WalletService.prototype.pushNotificationsSubscribe = function(opts, cb) { var self = this; - opts.user = self.walletId + '$' + self.copayerId; + opts.user = self.walletId + '$' + self.copayerId + '$' + opts.token; request({ url: config.pushNotificationsOpts.pushServerUrl + '/subscribe', @@ -2442,21 +2442,16 @@ WalletService.prototype.pushNotificationsSubscribe = function(opts, cb) { }); }; -WalletService.prototype.pushNotificationsUnsubscribe = function(opts, cb) { +WalletService.prototype.pushNotificationsUnsubscribe = function(token, cb) { var self = this; - if (opts) opts = { - token: opts - }; - else opts = { - user: self.walletId + '$' + self.copayerId - }; - request({ url: config.pushNotificationsOpts.pushServerUrl + '/unsubscribe', method: 'POST', json: true, - body: opts + body: { + user: self.walletId + '$' + self.copayerId + '$' + token + } }, function(err, response) { return cb(err, response); }); diff --git a/test/integration/server.js b/test/integration/server.js index 242478b..75dddd7 100644 --- a/test/integration/server.js +++ b/test/integration/server.js @@ -5453,7 +5453,7 @@ describe('Wallet service', function() { }); }); - describe('Subscribe/unsubscribe', function() { + describe.only('Subscribe/unsubscribe', function() { var server, wallet; beforeEach(function(done) { helpers.createAndJoinWallet(2, 3, function(s, w) { @@ -5467,7 +5467,9 @@ describe('Wallet service', function() { request.yields(); helpers.getAuthServer(wallet.copayers[0].id, function(server) { should.exist(server); - server.pushNotificationsSubscribe({}, function(err, response) { + server.pushNotificationsSubscribe({ + token: 'DEVICE_TOKEN' + }, function(err, response) { should.not.exist(err); var calls = request.getCalls(); calls.length.should.equal(1); @@ -5475,6 +5477,8 @@ describe('Wallet service', function() { return c.args[0]; }); args[0].body.user.should.contain(wallet.copayers[0].id); + args[0].body.user.should.contain(wallet.id); + args[0].body.user.should.contain('DEVICE_TOKEN'); done(); }); }); @@ -5484,7 +5488,7 @@ describe('Wallet service', function() { request.yields(); helpers.getAuthServer(wallet.copayers[0].id, function(server) { should.exist(server); - server.pushNotificationsUnsubscribe(null, function(err, response) { + server.pushNotificationsUnsubscribe('DEVICE_TOKEN', function(err, response) { should.not.exist(err); var calls = request.getCalls(); calls.length.should.equal(1); @@ -5493,24 +5497,8 @@ describe('Wallet service', function() { }); args[0].body.user.should.contain(wallet.copayers[0].id); - done(); - }); - }); - }); - - it('should unsubscribe all wallets from device to push notifications service', function(done) { - request.yields(); - helpers.getAuthServer(wallet.copayers[0].id, function(server) { - should.exist(server); - server.pushNotificationsUnsubscribe('TOKEN_DEVICE', function(err, response) { - should.not.exist(err); - var calls = request.getCalls(); - calls.length.should.equal(1); - var args = _.map(calls, function(c) { - return c.args[0]; - }); - - args[0].body.token.should.contain('TOKEN_DEVICE'); + args[0].body.user.should.contain(wallet.id); + args[0].body.user.should.contain('DEVICE_TOKEN'); done(); }); });