From 7105bc195f688949b6827cbbf906b386fdf736e8 Mon Sep 17 00:00:00 2001 From: Negue Date: Fri, 27 Mar 2015 18:35:32 +0100 Subject: [PATCH 01/24] add all Changes of #4565 --- common/script/index.coffee | 14 +++++ config.json.example | 7 +++ website/src/controllers/challenges.js | 5 +- website/src/controllers/groups.js | 9 ++++ website/src/controllers/members.js | 9 +++- website/src/controllers/payments/index.js | 19 +++++-- website/src/controllers/pushNotifications.js | 56 ++++++++++++++++++++ website/src/models/user.js | 7 ++- website/src/routes/apiv2.coffee | 10 ++++ 9 files changed, 129 insertions(+), 7 deletions(-) create mode 100644 website/src/controllers/pushNotifications.js diff --git a/common/script/index.coffee b/common/script/index.coffee index 922c38aa65..5f8e0c23f7 100644 --- a/common/script/index.coffee +++ b/common/script/index.coffee @@ -645,6 +645,20 @@ api.wrap = (user, main=true) -> user.markModified? 'preferences.webhooks' cb? null, user.preferences.webhooks + # ------ + # Push Notifications + # ------ + addPushDevice: (req, cb) -> + user.pushDevices = [] unless user.pushDevices + pd = user.pushDevices + item = {regId:req.body.regId, type:req.body.type}; + i = _.findIndex pd, {regId: item.regId} + + pd.push(item) unless i != -1 + + user.markModified? 'pushDevices' + cb? null, user.pushDevices + # ------ # Inbox # ------ diff --git a/config.json.example b/config.json.example index 6933f92426..6b87388d90 100644 --- a/config.json.example +++ b/config.json.example @@ -51,5 +51,12 @@ "token": "token", "username": "username", "password": "password" + }, + "PUSH_CONFIGS": { + "GCM_SERVER_API_KEY": "", + "APN_PEM_FILES": { + "KEY": "key.pem", + "CERT": "cert.pem" + } } } diff --git a/website/src/controllers/challenges.js b/website/src/controllers/challenges.js index 5fabd0fb1c..c133c1e4ca 100644 --- a/website/src/controllers/challenges.js +++ b/website/src/controllers/challenges.js @@ -11,7 +11,7 @@ var logging = require('./../logging'); var csv = require('express-csv'); var utils = require('../utils'); var api = module.exports; - +var pushNotify = require('./pushNotifications'); /* ------------------------------------------------------------------------ @@ -341,6 +341,9 @@ api.selectWinner = function(req, res, next) { {name: 'CHALLENGE_NAME', content: chal.name} ]); } + + pushNotify.sendNotify(saved, shared.i18n.t('wonChallenge'), chal.name); + closeChal(cid, {broken: 'CHALLENGE_CLOSED', winner: saved.profile.name}, cb); } ], function(err){ diff --git a/website/src/controllers/groups.js b/website/src/controllers/groups.js index 4e2a93d79f..d00dc9578d 100644 --- a/website/src/controllers/groups.js +++ b/website/src/controllers/groups.js @@ -14,6 +14,7 @@ var Group = require('./../models/group').model; var Challenge = require('./../models/challenge').model; var isProd = nconf.get('NODE_ENV') === 'production'; var api = module.exports; +var pushNotify = require('./pushNotifications'); /* ------------------------------------------------------------------------ @@ -535,9 +536,13 @@ var inviteByUUIDs = function(uuids, group, req, res, next){ function sendInvite (){ if(group.type === 'guild'){ invite.invitations.guilds.push({id: group._id, name: group.name, inviter:res.locals.user._id}); + + pushNotify.sendNotify(invite, shared.i18n.t('invitedGuild'), group.name); }else{ //req.body.type in 'guild', 'party' invite.invitations.party = {id: group._id, name: group.name, inviter:res.locals.user._id}; + + pushNotify.sendNotify(invite, shared.i18n.t('invitedParty'), group.name); } group.invites.push(invite._id); @@ -860,6 +865,10 @@ api.questAccept = function(req, res, next) { group.quest.leader = user._id; } else { group.quest.members[m] = undefined; + + User.findById(m, function(err,groupMember){ + pushNotify.sendNotify(groupMember, "HabitRPG", "Invitation for the Quest "+quest.text()); + }); } }); diff --git a/website/src/controllers/members.js b/website/src/controllers/members.js index 9102df985a..cf0061fcc2 100644 --- a/website/src/controllers/members.js +++ b/website/src/controllers/members.js @@ -7,6 +7,7 @@ var _ = require('lodash'); var shared = require('../../../common'); var utils = require('../utils'); var nconf = require('nconf'); +var pushNotify = require('./pushNotifications'); var fetchMember = function(uuid, restrict){ return function(cb){ @@ -96,12 +97,18 @@ api.sendGift = function(req, res, next){ member.balance += amt; user.balance -= amt; api.sendMessage(user, member, req.body); + + var byUsername = utils.getUserInfo(user, ['name']).name; + if(member.preferences.emailNotifications.giftedGems !== false){ utils.txnEmail(member, 'gifted-gems', [ - {name: 'GIFTER', content: utils.getUserInfo(user, ['name']).name}, + {name: 'GIFTER', content: byUsername}, {name: 'X_GEMS_GIFTED', content: req.body.gems.amount} ]); } + + pushNotify.sendNotify(member, shared.i18n.t('giftedGems'), req.body.gems.amount + ' Gems - by '+byUsername); + return async.parallel([ function (cb2) { member.save(cb2) }, function (cb2) { user.save(cb2) } diff --git a/website/src/controllers/payments/index.js b/website/src/controllers/payments/index.js index 639fe18ebf..7b23500900 100644 --- a/website/src/controllers/payments/index.js +++ b/website/src/controllers/payments/index.js @@ -12,6 +12,7 @@ var async = require('async'); var iap = require('./iap'); var mongoose= require('mongoose'); var cc = require('coupon-code'); +var pushNotify = require('./../pushNotifications'); function revealMysteryItems(user) { _.each(shared.content.gear.flat, function(item) { @@ -75,12 +76,17 @@ exports.createSubscription = function(data, cb) { data.user.purchased.txnCount++; if (data.gift){ members.sendMessage(data.user, data.gift.member, data.gift); + + var byUserName = utils.getUserInfo(data.user, ['name']).name; + if(data.gift.member.preferences.emailNotifications.giftedSubscription !== false){ utils.txnEmail(data.gift.member, 'gifted-subscription', [ - {name: 'GIFTER', content: utils.getUserInfo(data.user, ['name']).name}, + {name: 'GIFTER', content: byUserName}, {name: 'X_MONTHS_SUBSCRIPTION', content: months} ]); - } + } + + pushNotify.sendNotify(data.gift.member, shared.i18n.t('gifted-subscription'), months + " months - by "+ byUserName); } async.parallel([ function(cb2){data.user.save(cb2)}, @@ -119,13 +125,18 @@ exports.buyGems = function(data, cb) { utils.ga.transaction(data.user._id, amt).item(amt, 1, data.paymentMethod.toLowerCase() + "-checkout", "Gems > " + data.paymentMethod).send(); } if (data.gift){ + var byUsername = utils.getUserInfo(data.user, ['name']).name; + var gemAmount = data.gift.gems.amount || 20; + members.sendMessage(data.user, data.gift.member, data.gift); if(data.gift.member.preferences.emailNotifications.giftedGems !== false){ utils.txnEmail(data.gift.member, 'gifted-gems', [ - {name: 'GIFTER', content: utils.getUserInfo(data.user, ['name']).name}, - {name: 'X_GEMS_GIFTED', content: data.gift.gems.amount || 20} + {name: 'GIFTER', content: byUsername}, + {name: 'X_GEMS_GIFTED', content: gemAmount} ]); } + + pushNotify.sendNotify(data.gift.member, shared.i18n.t('giftedGems'), gemAmount + ' Gems - by '+byUsername); } async.parallel([ function(cb2){data.user.save(cb2)}, diff --git a/website/src/controllers/pushNotifications.js b/website/src/controllers/pushNotifications.js new file mode 100644 index 0000000000..fc576beefc --- /dev/null +++ b/website/src/controllers/pushNotifications.js @@ -0,0 +1,56 @@ +var api = module.exports; +var _ = require('lodash'); +var nconf = require('nconf'); + +var pushNotify = require('push-notify'); + +var gcmApiKey = nconf.get("PUSH_CONFIGS:GCM_SERVER_API_KEY"); + +var gcm = gcmApiKey ? pushNotify.gcm({ + apiKey: gcmApiKey, + retries: 3 +}) : undefined; + +if(gcm){ + gcm.on('transmitted', function (result, message, registrationId) { + console.info("transmitted", result, message, registrationId); + }); + + gcm.on('transmissionError', function (error, message, registrationId) { + console.info("transmissionError", error, message, registrationId); + }); + gcm.on('updated', function (result, registrationId) { + console.info("updated", result, registrationId); + }); +} + +api.sendNotify = function(user, title, msg, timeToLive){ + timeToLive = timeToLive || 15; + + _.forEach(user.pushDevices, function(pushDevice){ + switch(pushDevice.type){ + case "android": + if(gcm){ + console.info("sending", title, msg); + console.info(pushDevice); + + + gcm.send({ + registrationId: pushDevice.regId, + //collapseKey: 'COLLAPSE_KEY', + delayWhileIdle: true, + timeToLive: timeToLive, + data: { + title: title, + message: msg + } + }); + } + + break; + + case "ios": + break; + } + }); +}; \ No newline at end of file diff --git a/website/src/models/user.js b/website/src/models/user.js index 08f286eb41..9d00ecee71 100644 --- a/website/src/models/user.js +++ b/website/src/models/user.js @@ -387,7 +387,12 @@ var UserSchema = new Schema({ todos: {type:[TaskSchemas.TodoSchema]}, rewards: {type:[TaskSchemas.RewardSchema]}, - extra: Schema.Types.Mixed + extra: Schema.Types.Mixed, + + pushDevices: {type: [{ + regId: {type: String}, + type: {type: String} + }],'default': []} }, { strict: true, diff --git a/website/src/routes/apiv2.coffee b/website/src/routes/apiv2.coffee index 479521d054..45729710c7 100644 --- a/website/src/routes/apiv2.coffee +++ b/website/src/routes/apiv2.coffee @@ -396,6 +396,16 @@ module.exports = (swagger, v2) -> ] action: user.deleteWebhook + # Push Notifications + "/user/pushDevice": + spec: + method: 'POST' + description: 'Add a new push devices registration ID' + parameters: [ + body '','New push registration { regId: "123123", type: "android"}','object' + ] + action: user.addPushDevice + # --------------------------------- # Groups # --------------------------------- From fcec1dc1e6060953c9ff5321422e54c2e51108fb Mon Sep 17 00:00:00 2001 From: Negue Date: Fri, 27 Mar 2015 19:29:29 +0100 Subject: [PATCH 02/24] forgot to add the push-notify package --- package.json | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/package.json b/package.json index 64c576a05d..6cb1aef0eb 100644 --- a/package.json +++ b/package.json @@ -31,11 +31,11 @@ "grunt-nodemon": "~0.3.0", "grunt-spritesmith": "~3.5.0", "icalendar": "git://github.com/lefnire/node-icalendar#master", + "image-size": "~0.3.2", "in-app-purchase": "^0.2.0", "jade": "~1.7.0", "js2xmlparser": "~0.1.2", "lodash": "~2.4.1", - "image-size": "~0.3.2", "loggly": "~1.0.8", "method-override": "~2.2.0", "moment": "~2.8.3", @@ -51,6 +51,7 @@ "paypal-ipn": "2.1.0", "paypal-rest-sdk": "^1.2.1", "pretty-data": "git://github.com/vkiryukhin/pretty-data#master", + "push-notify": "^1.1.1", "qs": "^2.3.2", "request": "~2.44.0", "s3-upload-stream": "^1.0.6", From 02781d1f332a8ab5a09681261df30c2563ee254b Mon Sep 17 00:00:00 2001 From: Negue Date: Tue, 28 Apr 2015 19:55:01 +0200 Subject: [PATCH 03/24] remove markModified in addPushDevice --- common/script/index.coffee | 1 - 1 file changed, 1 deletion(-) diff --git a/common/script/index.coffee b/common/script/index.coffee index ddb839b26d..a10a7815d5 100644 --- a/common/script/index.coffee +++ b/common/script/index.coffee @@ -656,7 +656,6 @@ api.wrap = (user, main=true) -> pd.push(item) unless i != -1 - user.markModified? 'pushDevices' cb? null, user.pushDevices # ------ From 1858a98badaae4749d07b2bc0895f3da82980e7f Mon Sep 17 00:00:00 2001 From: Negue Date: Fri, 8 May 2015 23:46:41 +0200 Subject: [PATCH 04/24] add test for push notification api call --- test/api.mocha.coffee | 14 ++++++++++++++ 1 file changed, 14 insertions(+) diff --git a/test/api.mocha.coffee b/test/api.mocha.coffee index 2a853562cf..db9a4523cd 100644 --- a/test/api.mocha.coffee +++ b/test/api.mocha.coffee @@ -1029,6 +1029,20 @@ describe "API", -> ], cb ], done + describe "Push-Notifications", -> + it "Register DeviceID", (done) -> + request.post(baseURL + "/user/pushDevice").send( + { regId: "123123", type: "android"} + ).end (res) -> + expectCode res, 200 + + User.findOne + _id: _id + , (err, _user) -> + expect(_user.pushDevices.length).to.be 1 + + done() + describe "Subscriptions", -> user = undefined before (done) -> From bf4ecf244cef6a194544bac5555212819640baff Mon Sep 17 00:00:00 2001 From: Blade Barringer Date: Sun, 10 May 2015 16:36:10 -0500 Subject: [PATCH 05/24] organized push notifications api test --- test/api/pushNotifications.coffee | 27 +++++++++++++++------------ 1 file changed, 15 insertions(+), 12 deletions(-) diff --git a/test/api/pushNotifications.coffee b/test/api/pushNotifications.coffee index c727bb6bfe..d2f0e5e839 100644 --- a/test/api/pushNotifications.coffee +++ b/test/api/pushNotifications.coffee @@ -2,18 +2,21 @@ app = require("../../website/src/server") - describe "Push-Notifications", -> - it "Register DeviceID", (done) -> - request.post(baseURL + "/user/pushDevice").send( - { regId: "123123", type: "android"} - ).end (res) -> - expectCode res, 200 + before (done) -> + registerNewUser(done, true) - User.findOne - _id: global.user._id - , (err, _user) -> - expect(_user.pushDevices.length).to.be 1 - expect(_user.pushDevices[0].regId).to.be "123123" + describe "POST /user/pushDevice", -> + it "Registers a DeviceID", (done) -> + request.post(baseURL + "/user/pushDevice").send( + { regId: "123123", type: "android"} + ).end (res) -> + expectCode res, 200 - done() \ No newline at end of file + User.findOne + _id: global.user._id + , (err, _user) -> + expect(_user.pushDevices.length).to.be 1 + expect(_user.pushDevices[0].regId).to.be "123123" + + done() From 6d025330e96a07ba407b6dcc2e5812cace6704c3 Mon Sep 17 00:00:00 2001 From: Blade Barringer Date: Sun, 10 May 2015 16:48:48 -0500 Subject: [PATCH 06/24] Clean up spaces; remove console log --- website/src/controllers/pushNotifications.js | 70 +++++++++----------- 1 file changed, 33 insertions(+), 37 deletions(-) diff --git a/website/src/controllers/pushNotifications.js b/website/src/controllers/pushNotifications.js index fc576beefc..98ec0b5d82 100644 --- a/website/src/controllers/pushNotifications.js +++ b/website/src/controllers/pushNotifications.js @@ -7,50 +7,46 @@ var pushNotify = require('push-notify'); var gcmApiKey = nconf.get("PUSH_CONFIGS:GCM_SERVER_API_KEY"); var gcm = gcmApiKey ? pushNotify.gcm({ - apiKey: gcmApiKey, - retries: 3 + apiKey: gcmApiKey, + retries: 3 }) : undefined; if(gcm){ - gcm.on('transmitted', function (result, message, registrationId) { - console.info("transmitted", result, message, registrationId); - }); + gcm.on('transmitted', function (result, message, registrationId) { + console.info("transmitted", result, message, registrationId); + }); - gcm.on('transmissionError', function (error, message, registrationId) { - console.info("transmissionError", error, message, registrationId); - }); - gcm.on('updated', function (result, registrationId) { - console.info("updated", result, registrationId); - }); + gcm.on('transmissionError', function (error, message, registrationId) { + console.info("transmissionError", error, message, registrationId); + }); + gcm.on('updated', function (result, registrationId) { + console.info("updated", result, registrationId); + }); } api.sendNotify = function(user, title, msg, timeToLive){ - timeToLive = timeToLive || 15; + timeToLive = timeToLive || 15; - _.forEach(user.pushDevices, function(pushDevice){ - switch(pushDevice.type){ - case "android": - if(gcm){ - console.info("sending", title, msg); - console.info(pushDevice); + _.forEach(user.pushDevices, function(pushDevice){ + switch(pushDevice.type){ + case "android": + if(gcm){ + gcm.send({ + registrationId: pushDevice.regId, + //collapseKey: 'COLLAPSE_KEY', + delayWhileIdle: true, + timeToLive: timeToLive, + data: { + title: title, + message: msg + } + }); + } + break; - gcm.send({ - registrationId: pushDevice.regId, - //collapseKey: 'COLLAPSE_KEY', - delayWhileIdle: true, - timeToLive: timeToLive, - data: { - title: title, - message: msg - } - }); - } - - break; - - case "ios": - break; - } - }); -}; \ No newline at end of file + case "ios": + break; + } + }); +}; From c219df2a9dd0f571a97ce61e1299a2485fbcefd8 Mon Sep 17 00:00:00 2001 From: Blade Barringer Date: Sun, 10 May 2015 17:09:40 -0500 Subject: [PATCH 07/24] Set up pended tests for push notifications --- test/api/pushNotifications.coffee | 22 ++++++++++++++++++++++ 1 file changed, 22 insertions(+) diff --git a/test/api/pushNotifications.coffee b/test/api/pushNotifications.coffee index d2f0e5e839..67caa784f7 100644 --- a/test/api/pushNotifications.coffee +++ b/test/api/pushNotifications.coffee @@ -20,3 +20,25 @@ describe "Push-Notifications", -> expect(_user.pushDevices[0].regId).to.be "123123" done() + + describe "Events that send push notifications", -> + + context "Challenges", -> + + it "sends a push notification when you win a challenge" + + it "does not send a push notification when you lose a challenge" + + context "Groups", -> + + it "sends a push notification when invited to a guild" + + it "sends a push notification when invited to a party" + + it "sends a push notification when invited to a quest" + + context "Gifts", -> + + it "sends a push notification when gifted gems" + + it "sends a push notification when gifted a subscription" From d4a296870b837a9d22aa329feda9649a319fb786 Mon Sep 17 00:00:00 2001 From: Blade Barringer Date: Sun, 10 May 2015 20:39:11 -0500 Subject: [PATCH 08/24] Add test for push notification when gems are sent --- package.json | 1 + test/api/pushNotifications.coffee | 33 ++++++++++++++++++++++++++++++- 2 files changed, 33 insertions(+), 1 deletion(-) diff --git a/package.json b/package.json index ad8788efd7..252f2a144a 100644 --- a/package.json +++ b/package.json @@ -102,6 +102,7 @@ "mongoskin": "~0.6.1", "phantomjssmith": "~0.5.4", "protractor": "~2.0.0", + "rewire": "^2.3.3", "rimraf": "^2.2.8", "shelljs": "^0.4.0", "sinon": "^1.12.2", diff --git a/test/api/pushNotifications.coffee b/test/api/pushNotifications.coffee index 67caa784f7..83f7400cbf 100644 --- a/test/api/pushNotifications.coffee +++ b/test/api/pushNotifications.coffee @@ -1,6 +1,8 @@ 'use strict' app = require("../../website/src/server") +rewire = require('rewire') +sinon = require('sinon') describe "Push-Notifications", -> before (done) -> @@ -23,6 +25,8 @@ describe "Push-Notifications", -> describe "Events that send push notifications", -> + pushSpy = sinon.spy() + context "Challenges", -> it "sends a push notification when you win a challenge" @@ -38,7 +42,34 @@ describe "Push-Notifications", -> it "sends a push notification when invited to a quest" context "Gifts", -> + recipient = null + members = rewire("../../website/src/controllers/members") + members.__set__('pushNotify', pushSpy) + members.sendMessage = -> true - it "sends a push notification when gifted gems" + before (done) -> + registerNewUser (err, _user) -> + recipient = _user + user.balance = 4 + members.__set__ 'fetchMember', (id) -> return (cb) -> cb(null, recipient) + done() + , false + + + it "sends a push notification when gifted gems", (done) -> + req = { + params: { uuid: "uuid" }, + body: { + type: 'gems', + gems: { amount: 1 } + } + } + res = { locals: { user: user } } + + members.sendGift req, res + + expect(pushSpy.calledOnce).to.be.ok + + done() it "sends a push notification when gifted a subscription" From a906c750eb980e4e92bac856d690a734ce67a13d Mon Sep 17 00:00:00 2001 From: Blade Barringer Date: Sun, 10 May 2015 22:13:53 -0500 Subject: [PATCH 09/24] Correct spy set up --- test/api/pushNotifications.coffee | 17 ++++++++--------- 1 file changed, 8 insertions(+), 9 deletions(-) diff --git a/test/api/pushNotifications.coffee b/test/api/pushNotifications.coffee index 83f7400cbf..b79187af13 100644 --- a/test/api/pushNotifications.coffee +++ b/test/api/pushNotifications.coffee @@ -25,7 +25,7 @@ describe "Push-Notifications", -> describe "Events that send push notifications", -> - pushSpy = sinon.spy() + pushSpy = { sendNotify: sinon.spy() } context "Challenges", -> @@ -44,32 +44,31 @@ describe "Push-Notifications", -> context "Gifts", -> recipient = null members = rewire("../../website/src/controllers/members") - members.__set__('pushNotify', pushSpy) members.sendMessage = -> true + members.__set__('pushNotify', pushSpy) before (done) -> registerNewUser (err, _user) -> recipient = _user user.balance = 4 + user.save = -> return true + recipient.save = -> return true members.__set__ 'fetchMember', (id) -> return (cb) -> cb(null, recipient) done() , false - it "sends a push notification when gifted gems", (done) -> - req = { + req = { params: { uuid: "uuid" }, - body: { + body: { type: 'gems', gems: { amount: 1 } - } + } } res = { locals: { user: user } } members.sendGift req, res - - expect(pushSpy.calledOnce).to.be.ok - + # @TODO Expectations done() it "sends a push notification when gifted a subscription" From 3cda9ff82044425b07d41c953d8af3b1b8811563 Mon Sep 17 00:00:00 2001 From: Blade Barringer Date: Mon, 11 May 2015 08:19:49 -0500 Subject: [PATCH 10/24] Update push notification test to use chai expect --- test/api/pushNotifications.coffee | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/test/api/pushNotifications.coffee b/test/api/pushNotifications.coffee index b79187af13..a2a84e42d0 100644 --- a/test/api/pushNotifications.coffee +++ b/test/api/pushNotifications.coffee @@ -18,8 +18,8 @@ describe "Push-Notifications", -> User.findOne _id: global.user._id , (err, _user) -> - expect(_user.pushDevices.length).to.be 1 - expect(_user.pushDevices[0].regId).to.be "123123" + expect(_user.pushDevices.length).to.equal 1 + expect(_user.pushDevices[0].regId).to.equal "123123" done() From 60857994c3ec2b83be3ddb05a5af95229eb9b997 Mon Sep 17 00:00:00 2001 From: Blade Barringer Date: Mon, 11 May 2015 17:27:58 -0500 Subject: [PATCH 11/24] Add expectation that push notification gets called when sending gems --- package.json | 1 + test/api/api-helper.coffee | 3 ++- test/api/pushNotifications.coffee | 13 +++++++++++-- 3 files changed, 14 insertions(+), 3 deletions(-) diff --git a/package.json b/package.json index f1632763b6..04655f1101 100644 --- a/package.json +++ b/package.json @@ -107,6 +107,7 @@ "rimraf": "^2.2.8", "shelljs": "^0.4.0", "sinon": "^1.12.2", + "sinon-chai": "^2.7.0", "superagent": "~0.15.7", "superagent-defaults": "~0.1.5", "vinyl-source-stream": "^1.0.0", diff --git a/test/api/api-helper.coffee b/test/api/api-helper.coffee index 31a774007d..0c3ae7b580 100644 --- a/test/api/api-helper.coffee +++ b/test/api/api-helper.coffee @@ -12,7 +12,8 @@ global.shared = require("../../common") global.User = require("../../website/src/models/user").model global.chai = require("chai") -global.expect = require("chai").expect +chai.use(require("sinon-chai")) +global.expect = chai.expect ############################## # Nconf config diff --git a/test/api/pushNotifications.coffee b/test/api/pushNotifications.coffee index a2a84e42d0..b40a271fd0 100644 --- a/test/api/pushNotifications.coffee +++ b/test/api/pushNotifications.coffee @@ -68,7 +68,16 @@ describe "Push-Notifications", -> res = { locals: { user: user } } members.sendGift req, res - # @TODO Expectations - done() + + setTimeout -> + # Allow sendGift 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 it "sends a push notification when gifted a subscription" From b635c62d16ebdbccc542c532d8b4e28f1bd4d0df Mon Sep 17 00:00:00 2001 From: Blade Barringer Date: Tue, 12 May 2015 09:04:07 -0500 Subject: [PATCH 12/24] Add contexts --- test/api/pushNotifications.coffee | 12 +++++++++--- 1 file changed, 9 insertions(+), 3 deletions(-) diff --git a/test/api/pushNotifications.coffee b/test/api/pushNotifications.coffee index b40a271fd0..479a14fe15 100644 --- a/test/api/pushNotifications.coffee +++ b/test/api/pushNotifications.coffee @@ -41,7 +41,7 @@ describe "Push-Notifications", -> it "sends a push notification when invited to a quest" - context "Gifts", -> + context "sending gems from balance", -> recipient = null members = rewire("../../website/src/controllers/members") members.sendMessage = -> true @@ -57,7 +57,7 @@ describe "Push-Notifications", -> done() , false - it "sends a push notification when gifted gems", (done) -> + it "sends a push notification", (done) -> req = { params: { uuid: "uuid" }, body: { @@ -80,4 +80,10 @@ describe "Push-Notifications", -> done() , 100 - it "sends a push notification when gifted a subscription" + context "sending gems as a purchased gift", -> + + it "sends a push notification" + + context "sending a subscription as a purchased gift", -> + + it "sends a push notification" From adef6bef7b5105a243f8d4d1ac82be654d69e85d Mon Sep 17 00:00:00 2001 From: Negue Date: Tue, 12 May 2015 21:36:22 +0200 Subject: [PATCH 13/24] tried to add a test for purchased gem gift --- test/api/pushNotifications.coffee | 88 +++++++++++++++++++++---------- 1 file changed, 61 insertions(+), 27 deletions(-) diff --git a/test/api/pushNotifications.coffee b/test/api/pushNotifications.coffee index 479a14fe15..6889307537 100644 --- a/test/api/pushNotifications.coffee +++ b/test/api/pushNotifications.coffee @@ -4,6 +4,8 @@ app = require("../../website/src/server") rewire = require('rewire') sinon = require('sinon') +pushSpy = null + describe "Push-Notifications", -> before (done) -> registerNewUser(done, true) @@ -41,11 +43,11 @@ describe "Push-Notifications", -> it "sends a push notification when invited to a quest" - context "sending gems from balance", -> - recipient = null + describe "Gifts", -> members = rewire("../../website/src/controllers/members") members.sendMessage = -> true - members.__set__('pushNotify', pushSpy) + + recipient = null before (done) -> registerNewUser (err, _user) -> @@ -53,37 +55,69 @@ describe "Push-Notifications", -> user.balance = 4 user.save = -> return true recipient.save = -> return true - members.__set__ 'fetchMember', (id) -> return (cb) -> cb(null, recipient) + members.__set__ 'fetchMember', (id) -> + return (cb) -> cb(null, recipient) done() , false - it "sends a push notification", (done) -> - req = { - params: { uuid: "uuid" }, - body: { - type: 'gems', - gems: { amount: 1 } + context "sending gems from balance", -> + pushSpy = { sendNotify: sinon.spy() } + members.__set__('pushNotify', pushSpy) + + it "sends a push notification", (done) -> + req = { + params: { uuid: "uuid" }, + body: { + type: 'gems', + gems: { amount: 1 } + } } - } - res = { locals: { user: user } } + res = { locals: { user: user } } - members.sendGift req, res + members.sendGift req, res - setTimeout -> - # Allow sendGift 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 + setTimeout -> + # Allow sendGift 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 - context "sending gems as a purchased gift", -> + context "sending gems as a purchased gift", -> + pushSpy = { sendNotify: sinon.spy() } - it "sends a push notification" + payments = rewire("../../website/src/controllers/payments") + payments.__set__('pushNotify', pushSpy) + payments.__set__('members', members) - context "sending a subscription as a purchased gift", -> + it "sends a push notification", (done) -> + data = { + user: user, + gift: { + member: recipient, + gems: { amount: 1 } + } + } - it "sends a push notification" + payments.buyGems data, (d) -> + d() + + setTimeout -> + # Allow sendGift 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 + + context "sending a subscription as a purchased gift", -> + + it "sends a push notification" From e35b24582bff71e6ab46615283951a24fd31abf6 Mon Sep 17 00:00:00 2001 From: Blade Barringer Date: Tue, 12 May 2015 18:26:18 -0500 Subject: [PATCH 14/24] Fix push notification tests --- test/api/pushNotifications.coffee | 42 ++++++++++++++----------------- 1 file changed, 19 insertions(+), 23 deletions(-) diff --git a/test/api/pushNotifications.coffee b/test/api/pushNotifications.coffee index 6889307537..f04e3c7314 100644 --- a/test/api/pushNotifications.coffee +++ b/test/api/pushNotifications.coffee @@ -4,8 +4,6 @@ app = require("../../website/src/server") rewire = require('rewire') sinon = require('sinon') -pushSpy = null - describe "Push-Notifications", -> before (done) -> registerNewUser(done, true) @@ -27,8 +25,6 @@ describe "Push-Notifications", -> describe "Events that send push notifications", -> - pushSpy = { sendNotify: sinon.spy() } - context "Challenges", -> it "sends a push notification when you win a challenge" @@ -44,25 +40,26 @@ describe "Push-Notifications", -> it "sends a push notification when invited to a quest" describe "Gifts", -> - members = rewire("../../website/src/controllers/members") - members.sendMessage = -> true recipient = null before (done) -> registerNewUser (err, _user) -> recipient = _user + recipient.preferences.emailNotifications.giftedGems = false user.balance = 4 user.save = -> return true recipient.save = -> return true - members.__set__ 'fetchMember', (id) -> - return (cb) -> cb(null, recipient) done() , false context "sending gems from balance", -> + members = rewire("../../website/src/controllers/members") + members.sendMessage = -> true pushSpy = { sendNotify: sinon.spy() } members.__set__('pushNotify', pushSpy) + members.__set__ 'fetchMember', (id) -> + return (cb) -> cb(null, recipient) it "sends a push notification", (done) -> req = { @@ -77,7 +74,6 @@ describe "Push-Notifications", -> members.sendGift req, res setTimeout -> - # Allow sendGift to finish expect(pushSpy.sendNotify).to.have.been.calledOnce expect(pushSpy.sendNotify).to.have.been.calledWith( recipient, @@ -85,14 +81,15 @@ describe "Push-Notifications", -> '1 Gems - by ' + user.profile.name ) done() - , 100 + , 2000 context "sending gems as a purchased gift", -> + membersMock = { sendMessage: -> true } pushSpy = { sendNotify: sinon.spy() } payments = rewire("../../website/src/controllers/payments") payments.__set__('pushNotify', pushSpy) - payments.__set__('members', members) + payments.__set__('members', membersMock) it "sends a push notification", (done) -> data = { @@ -103,20 +100,19 @@ describe "Push-Notifications", -> } } - payments.buyGems data, (d) -> - d() + payments.buyGems data - setTimeout -> - # Allow sendGift 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 sendGift 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", -> From 8f17519cd3b798f0b4fc70ee0beb78f7ebb459f4 Mon Sep 17 00:00:00 2001 From: Blade Barringer Date: Tue, 12 May 2015 18:57:35 -0500 Subject: [PATCH 15/24] Reduce timeout time --- test/api/pushNotifications.coffee | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/test/api/pushNotifications.coffee b/test/api/pushNotifications.coffee index f04e3c7314..b797bc3652 100644 --- a/test/api/pushNotifications.coffee +++ b/test/api/pushNotifications.coffee @@ -81,7 +81,7 @@ describe "Push-Notifications", -> '1 Gems - by ' + user.profile.name ) done() - , 2000 + , 100 context "sending gems as a purchased gift", -> membersMock = { sendMessage: -> true } From 292b8a67417d2ef2c2876a7194af2b57693027e8 Mon Sep 17 00:00:00 2001 From: Blade Barringer Date: Tue, 12 May 2015 18:58:56 -0500 Subject: [PATCH 16/24] Correct comments --- test/api/pushNotifications.coffee | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/test/api/pushNotifications.coffee b/test/api/pushNotifications.coffee index b797bc3652..f8ab2c7083 100644 --- a/test/api/pushNotifications.coffee +++ b/test/api/pushNotifications.coffee @@ -74,6 +74,7 @@ describe "Push-Notifications", -> members.sendGift req, res setTimeout -> + # Allow sendGift to finish expect(pushSpy.sendNotify).to.have.been.calledOnce expect(pushSpy.sendNotify).to.have.been.calledWith( recipient, @@ -103,7 +104,7 @@ describe "Push-Notifications", -> payments.buyGems data setTimeout -> - # Allow sendGift to finish + # Allow buyGems to finish expect(pushSpy.sendNotify).to.have.been.calledOnce expect(pushSpy.sendNotify).to.have.been.calledWith( recipient, From 8a4abc632fce87c8beae14cb23be2e5ec08464c7 Mon Sep 17 00:00:00 2001 From: Blade Barringer Date: Tue, 12 May 2015 20:18:06 -0500 Subject: [PATCH 17/24] Refactor push notifications test; Prevent push notification from being sent when buying gems for yourself as a gift --- test/api/pushNotifications.coffee | 72 +++++++++++++++-------- website/src/controllers/payments/index.js | 6 +- 2 files changed, 51 insertions(+), 27 deletions(-) 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; From 023458b6e550a113e627de5b86a21543412f5aa1 Mon Sep 17 00:00:00 2001 From: Blade Barringer Date: Wed, 13 May 2015 19:31:26 -0500 Subject: [PATCH 18/24] Add test for getting push notification when gifted a subscription --- test/api/pushNotifications.coffee | 39 ++++++++++++++++++++++- website/src/controllers/payments/index.js | 4 ++- 2 files changed, 41 insertions(+), 2 deletions(-) diff --git a/test/api/pushNotifications.coffee b/test/api/pushNotifications.coffee index 657feead64..25de0b13f8 100644 --- a/test/api/pushNotifications.coffee +++ b/test/api/pushNotifications.coffee @@ -139,4 +139,41 @@ describe "Push-Notifications", -> context "sending a subscription as a purchased gift", -> - it "sends a push notification" + it "sends a push notification", (done) -> + data = { + user: user, + gift: { + member: recipient + subscription: { key: 'basic_6mo' } + } + } + + payments.createSubscription data + + setTimeout -> # Allow createSubscription to finish + expect(pushSpy.sendNotify).to.have.been.calledOnce + expect(pushSpy.sendNotify).to.have.been.calledWith( + recipient, + 'Gifted Subscription', + '6 months - by ' + user.profile.name + ) + + done() + , 100 + + it "does not send a push notification if buying subscription for self", (done) -> + data = { + user: user, + gift: { + member: user + subscription: { key: 'basic_6mo' } + } + } + + payments.createSubscription data + + setTimeout -> # Allow buyGems to finish + expect(pushSpy.sendNotify).to.not.have.been.called + + done() + , 100 diff --git a/website/src/controllers/payments/index.js b/website/src/controllers/payments/index.js index 3d47055759..31d691a7fe 100644 --- a/website/src/controllers/payments/index.js +++ b/website/src/controllers/payments/index.js @@ -86,7 +86,9 @@ exports.createSubscription = function(data, cb) { ]); } - pushNotify.sendNotify(data.gift.member, shared.i18n.t('gifted-subscription'), months + " months - 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('giftedSubscription'), months + " months - by "+ byUserName); + } } async.parallel([ function(cb2){data.user.save(cb2)}, From 2fac464160f31b3249cebd050ebe4a131fca4474 Mon Sep 17 00:00:00 2001 From: Blade Barringer Date: Wed, 13 May 2015 19:35:26 -0500 Subject: [PATCH 19/24] Remove extranious pushSpy declaration --- test/api/pushNotifications.coffee | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/test/api/pushNotifications.coffee b/test/api/pushNotifications.coffee index 25de0b13f8..e9e3032849 100644 --- a/test/api/pushNotifications.coffee +++ b/test/api/pushNotifications.coffee @@ -61,7 +61,7 @@ describe "Push-Notifications", -> context "sending gems from balance", -> members = rewire("../../website/src/controllers/members") members.sendMessage = -> true - pushSpy = { sendNotify: sinon.spy() } + members.__set__('pushNotify', pushSpy) members.__set__ 'fetchMember', (id) -> return (cb) -> cb(null, recipient) From 84aaeedfff5587fe6f183d5b2cda90914787afcb Mon Sep 17 00:00:00 2001 From: Blade Barringer Date: Thu, 14 May 2015 07:32:16 -0500 Subject: [PATCH 20/24] Add challenges test --- test/api/pushNotifications.coffee | 41 +++++++++++++++++++++++++++++-- 1 file changed, 39 insertions(+), 2 deletions(-) diff --git a/test/api/pushNotifications.coffee b/test/api/pushNotifications.coffee index e9e3032849..ae551ce4c3 100644 --- a/test/api/pushNotifications.coffee +++ b/test/api/pushNotifications.coffee @@ -31,10 +31,47 @@ describe "Push-Notifications", -> done() context "Challenges", -> + challenges = rewire("../../website/src/controllers/challenges") + challenges.__set__('pushNotify', pushSpy) + challengeMock = { + findById: (arg, cb) -> + cb(null, {leader: user._id, name: 'challenge-name'}) + } + userMock = { + findById: (arg, cb) -> + cb(null, user) + } - it "sends a push notification when you win a challenge" + challenges.__set__('Challenge', challengeMock) + challenges.__set__('User', userMock) + challenges.__set__('closeChal', -> true) - it "does not send a push notification when you lose a challenge" + beforeEach (done) -> + registerNewUser -> + user.preferences.emailNotifications.wonChallenge = false + user.save = (cb) -> cb(null, user) + done() + , true + + it "sends a push notification when you win a challenge", (done) -> + req = { + params: { cid: 'challenge-id' } + query: {uid: 'user-id'} + } + res = { + locals: { user: user } + } + challenges.selectWinner req, res + + setTimeout -> # Allow selectWinner to finish + expect(pushSpy.sendNotify).to.have.been.calledOnce + expect(pushSpy.sendNotify).to.have.been.calledWith( + user, + 'You Won a Challenge', + 'challenge-name' + ) + done() + , 100 context "Groups", -> From 7a2e2fa22728569d88d1519615638c2e73599999 Mon Sep 17 00:00:00 2001 From: Blade Barringer Date: Thu, 14 May 2015 17:29:33 -0500 Subject: [PATCH 21/24] Add tests for invites to guilds or parties --- test/api/pushNotifications.coffee | 67 ++++++++++++++++++++++++++++++- 1 file changed, 65 insertions(+), 2 deletions(-) diff --git a/test/api/pushNotifications.coffee b/test/api/pushNotifications.coffee index ae551ce4c3..2bcb1f4e5f 100644 --- a/test/api/pushNotifications.coffee +++ b/test/api/pushNotifications.coffee @@ -75,9 +75,72 @@ describe "Push-Notifications", -> context "Groups", -> - it "sends a push notification when invited to a guild" + recipient = null - it "sends a push notification when invited to a party" + groups = rewire("../../website/src/controllers/groups") + groups.__set__('pushNotify', pushSpy) + + before (done) -> + registerNewUser (err,_user)-> + recipient = _user + recipient.invitations.guilds = [] + recipient.save = (cb) -> cb(null, recipient) + recipient.preferences.emailNotifications.invitedGuild = false + recipient.preferences.emailNotifications.invitedParty = false + userMock = { + findById: (arg, cb) -> + cb(null, recipient) + } + groups.__set__('User', userMock) + done() + + , false + + it "sends a push notification when invited to a guild", (done) -> + group = { _id: 'guild-id', name: 'guild-name', type: 'guild', members: [user._id], invites: [] } + group.save = (cb) -> cb(null, group) + req = { + body: { uuids: [recipient._id] } + } + res = { + locals: { group: group, user: user } + json: -> return true + } + + groups.invite req, res + + setTimeout -> # Allow invite to finish + expect(pushSpy.sendNotify).to.have.been.calledOnce + expect(pushSpy.sendNotify).to.have.been.calledWith( + recipient, + 'Invited To Guild', + group.name + ) + done() + , 100 + + it "sends a push notification when invited to a party", (done) -> + group = { _id: 'party-id', name: 'party-name', type: 'party', members: [user._id], invites: [] } + group.save = (cb) -> cb(null, group) + req = { + body: { uuids: [recipient._id] } + } + res = { + locals: { group: group, user: user } + json: -> return true + } + + groups.invite req, res + + setTimeout -> # Allow invite to finish + expect(pushSpy.sendNotify).to.have.been.calledOnce + expect(pushSpy.sendNotify).to.have.been.calledWith( + recipient, + 'Invited To Party', + group.name + ) + done() + , 100 it "sends a push notification when invited to a quest" From 547d234b461530d88d97b96c7ef9e8d537a35530 Mon Sep 17 00:00:00 2001 From: Blade Barringer Date: Thu, 14 May 2015 21:32:28 -0500 Subject: [PATCH 22/24] Add final test for push notifications --- test/api/pushNotifications.coffee | 29 ++++++++++++++++++++++++++++- 1 file changed, 28 insertions(+), 1 deletion(-) diff --git a/test/api/pushNotifications.coffee b/test/api/pushNotifications.coffee index 2bcb1f4e5f..26c0d2ec39 100644 --- a/test/api/pushNotifications.coffee +++ b/test/api/pushNotifications.coffee @@ -78,6 +78,7 @@ describe "Push-Notifications", -> recipient = null groups = rewire("../../website/src/controllers/groups") + groups.__set__('questStart', -> true) groups.__set__('pushNotify', pushSpy) before (done) -> @@ -87,9 +88,12 @@ describe "Push-Notifications", -> recipient.save = (cb) -> cb(null, recipient) recipient.preferences.emailNotifications.invitedGuild = false recipient.preferences.emailNotifications.invitedParty = false + recipient.preferences.emailNotifications.invitedQuest = false userMock = { findById: (arg, cb) -> cb(null, recipient) + find: (arg, arg2, cb) -> + cb(null, [recipient]) } groups.__set__('User', userMock) done() @@ -142,7 +146,30 @@ describe "Push-Notifications", -> done() , 100 - it "sends a push notification when invited to a quest" + it "sends a push notification when invited to a quest", (done) -> + group = { _id: 'party-id', name: 'party-name', type: 'party', members: [user._id, recipient._id], invites: [], quest: {}} + user.items.quests.hedgehog = 5 + group.save = (cb) -> cb(null, group) + req = { + body: { uuids: [recipient._id] } + query: { key: 'hedgehog' } + } + res = { + locals: { group: group, user: user } + json: -> return true + } + + groups.questAccept req, res + + setTimeout -> # Allow questAccept to finish + expect(pushSpy.sendNotify).to.have.been.calledOnce + expect(pushSpy.sendNotify).to.have.been.calledWith( + recipient, + 'HabitRPG', + 'Invitation for the Quest The Hedgebeast' + ) + done() + , 100 describe "Gifts", -> From d7ce1515009a61150f4a74867caaa87a138ef750 Mon Sep 17 00:00:00 2001 From: Blade Barringer Date: Thu, 14 May 2015 21:56:16 -0500 Subject: [PATCH 23/24] Add translatable strings --- common/locales/en/quests.json | 2 ++ common/locales/en/settings.json | 1 + website/src/controllers/groups.js | 2 +- website/src/controllers/members.js | 2 +- 4 files changed, 5 insertions(+), 2 deletions(-) diff --git a/common/locales/en/quests.json b/common/locales/en/quests.json index 3f7be31868..e0546fcf8a 100644 --- a/common/locales/en/quests.json +++ b/common/locales/en/quests.json @@ -6,6 +6,8 @@ "questSend": "Clicking \"Invite\" will send an invitation to your party members. When all members have accepted or denied, the quest begins. See status under Options > Social > Party.", "inviteParty": "Invite Party", "questInvitation": "Quest Invitation: ", + "questInvitationTitle": "Quest Invitation", + "questInvitationInfo": "Invitation for the Quest <%= quest %>", "askLater": "Ask Later", "buyQuest": "Buy Quest", "accepted": "Accepted", diff --git a/common/locales/en/settings.json b/common/locales/en/settings.json index eacd8e0ef8..a1eb6ce215 100644 --- a/common/locales/en/settings.json +++ b/common/locales/en/settings.json @@ -93,6 +93,7 @@ "wonChallenge": "You Won a Challenge", "newPM": "Received Private Message", "giftedGems": "Gifted Gems", + "giftedGemsInfo": "<%= amount %> Gems - by <%= name %>", "giftedSubscription": "Gifted Subscription", "invitedParty": "Invited To Party", "invitedGuild": "Invited To Guild", diff --git a/website/src/controllers/groups.js b/website/src/controllers/groups.js index 1de92c87dc..54b6dde83a 100644 --- a/website/src/controllers/groups.js +++ b/website/src/controllers/groups.js @@ -914,7 +914,7 @@ api.questAccept = function(req, res, next) { group.quest.members[m] = undefined; User.findById(m, function(err,groupMember){ - pushNotify.sendNotify(groupMember, "HabitRPG", "Invitation for the Quest "+quest.text()); + pushNotify.sendNotify(groupMember, shared.i18n.t('questInvitationTitle'), shared.i18n.t('questInvitationInfo', { quest: quest.text() })); }); } }); diff --git a/website/src/controllers/members.js b/website/src/controllers/members.js index 2772371b51..c7fe341d3d 100644 --- a/website/src/controllers/members.js +++ b/website/src/controllers/members.js @@ -107,7 +107,7 @@ api.sendGift = function(req, res, next){ ]); } - pushNotify.sendNotify(member, shared.i18n.t('giftedGems'), req.body.gems.amount + ' Gems - by '+byUsername); + pushNotify.sendNotify(member, shared.i18n.t('giftedGems'), shared.i18n.t('giftedGemsInfo', { amount: req.body.gems.amount, name: byUsername })); return async.parallel([ function (cb2) { member.save(cb2) }, From 56b6794c66640fe8997db0d84b57df7663e0e7d6 Mon Sep 17 00:00:00 2001 From: Blade Barringer Date: Thu, 14 May 2015 22:09:36 -0500 Subject: [PATCH 24/24] Correct test string --- test/api/pushNotifications.coffee | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/test/api/pushNotifications.coffee b/test/api/pushNotifications.coffee index 26c0d2ec39..9b43ca08c7 100644 --- a/test/api/pushNotifications.coffee +++ b/test/api/pushNotifications.coffee @@ -165,7 +165,7 @@ describe "Push-Notifications", -> expect(pushSpy.sendNotify).to.have.been.calledOnce expect(pushSpy.sendNotify).to.have.been.calledWith( recipient, - 'HabitRPG', + 'Quest Invitation', 'Invitation for the Quest The Hedgebeast' ) done()