diff --git a/test/api/pushNotifications.coffee b/test/api/pushNotifications.coffee index f8ab2c7083..657feead64 100644 --- a/test/api/pushNotifications.coffee +++ b/test/api/pushNotifications.coffee @@ -24,6 +24,11 @@ describe "Push-Notifications", -> done() describe "Events that send push notifications", -> + pushSpy = { sendNotify: sinon.spy() } + + afterEach (done) -> + pushSpy.sendNotify.reset() + done() context "Challenges", -> @@ -73,8 +78,7 @@ describe "Push-Notifications", -> members.sendGift req, res - setTimeout -> - # Allow sendGift to finish + setTimeout -> # Allow sendGift to finish expect(pushSpy.sendNotify).to.have.been.calledOnce expect(pushSpy.sendNotify).to.have.been.calledWith( recipient, @@ -84,37 +88,55 @@ describe "Push-Notifications", -> done() , 100 - context "sending gems as a purchased gift", -> - membersMock = { sendMessage: -> true } - pushSpy = { sendNotify: sinon.spy() } + describe "Purchases", -> payments = rewire("../../website/src/controllers/payments") + payments.__set__('pushNotify', pushSpy) + membersMock = { sendMessage: -> true } payments.__set__('members', membersMock) - it "sends a push notification", (done) -> - data = { - user: user, - gift: { - member: recipient, - gems: { amount: 1 } + context "buying gems as a purchased gift", -> + + it "sends a push notification", (done) -> + data = { + user: user, + gift: { + member: recipient, + gems: { amount: 1 } + } } - } - payments.buyGems data + payments.buyGems data - setTimeout -> - # Allow buyGems to finish - expect(pushSpy.sendNotify).to.have.been.calledOnce - expect(pushSpy.sendNotify).to.have.been.calledWith( - recipient, - 'Gifted Gems', - '1 Gems - by ' + user.profile.name - ) + setTimeout -> # Allow buyGems to finish + expect(pushSpy.sendNotify).to.have.been.calledOnce + expect(pushSpy.sendNotify).to.have.been.calledWith( + recipient, + 'Gifted Gems', + '1 Gems - by ' + user.profile.name + ) - done() - , 100 + done() + , 100 - context "sending a subscription as a purchased gift", -> + it "does not send a push notification if buying gems for self", (done) -> + data = { + user: user, + gift: { + member: user + gems: { amount: 1 } + } + } - it "sends a push notification" + payments.buyGems data + + setTimeout -> # Allow buyGems to finish + expect(pushSpy.sendNotify).to.not.have.been.called + + done() + , 100 + + context "sending a subscription as a purchased gift", -> + + it "sends a push notification" diff --git a/website/src/controllers/payments/index.js b/website/src/controllers/payments/index.js index 073fff39b9..3d47055759 100644 --- a/website/src/controllers/payments/index.js +++ b/website/src/controllers/payments/index.js @@ -136,7 +136,9 @@ exports.buyGems = function(data, cb) { ]); } - pushNotify.sendNotify(data.gift.member, shared.i18n.t('giftedGems'), gemAmount + ' Gems - by '+byUsername); + if (data.gift.member._id != data.user._id) { // Only send push notifications if sending to a user other than yourself + pushNotify.sendNotify(data.gift.member, shared.i18n.t('giftedGems'), gemAmount + ' Gems - by '+byUsername); + } } async.parallel([ function(cb2){data.user.save(cb2)}, @@ -164,4 +166,4 @@ exports.paypalCheckoutSuccess = paypal.executePayment; exports.paypalIPN = paypal.ipn; exports.iapAndroidVerify = iap.androidVerify; -exports.iapIosVerify = iap.iosVerify; \ No newline at end of file +exports.iapIosVerify = iap.iosVerify;