From 82fb2499803e23eabe54050c4fda0e8d716793ee Mon Sep 17 00:00:00 2001 From: Florent Vilmart Date: Fri, 12 May 2017 09:13:09 -0400 Subject: [PATCH 1/4] Adds test for not set device tokens --- spec/PushController.spec.js | 76 +++++++++++++++++++++++++++++++++++++ 1 file changed, 76 insertions(+) diff --git a/spec/PushController.spec.js b/spec/PushController.spec.js index 1eb1607daf..d2399fb234 100644 --- a/spec/PushController.spec.js +++ b/spec/PushController.spec.js @@ -657,4 +657,80 @@ describe('PushController', () => { done(); }); }); + + it('should not enqueue push when device token is not set', (done) => { + var auth = { + isMaster: true + } + var pushAdapter = { + send: function(body, installations) { + const promises = installations.map((device) => { + if (!device.deviceToken) { + // Simulate error when device token is not set + return Promise.reject(); + } + return Promise.resolve({ + transmitted: true, + device: device, + }) + }); + + return Promise.all(promises); + }, + getValidPushTypes: function() { + return ["ios"]; + } + } + + var pushController = new PushController(); + const payload = { + data: { + alert: 'hello', + }, + push_time: new Date().getTime() / 1000 + } + + var installations = []; + while(installations.length != 5) { + const installation = new Parse.Object("_Installation"); + installation.set("installationId", "installation_" + installations.length); + installation.set("deviceToken","device_token_" + installations.length) + installation.set("badge", installations.length); + installation.set("originalBadge", installations.length); + installation.set("deviceType", "ios"); + installations.push(installation); + } + + while(installations.length != 15) { + const installation = new Parse.Object("_Installation"); + installation.set("installationId", "installation_" + installations.length); + installation.set("badge", installations.length); + installation.set("originalBadge", installations.length); + installation.set("deviceType", "ios"); + installations.push(installation); + } + + reconfigureServer({ + push: { adapter: pushAdapter } + }).then(() => { + var config = new Config(Parse.applicationId); + return Parse.Object.saveAll(installations).then(() => { + return pushController.sendPush(payload, {}, config, auth); + }); + }).then(() => { + const query = new Parse.Query('_PushStatus'); + return query.find({useMasterKey: true}).then((results) => { + expect(results.length).toBe(1); + const pushStatus = results[0]; + expect(pushStatus.get('numSent')).toBe(5); + expect(pushStatus.get('status')).toBe('succeeded'); + done(); + }); + }).catch((err) => { + console.error(err); + fail('should not fail'); + done(); + }); + + }); }); From 9b60a3efcbab7b7cc247003f1103410d5e6c94c5 Mon Sep 17 00:00:00 2001 From: Florent Vilmart Date: Fri, 12 May 2017 09:14:04 -0400 Subject: [PATCH 2/4] Properly filter the installations without a deviceToken --- src/Push/PushQueue.js | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/src/Push/PushQueue.js b/src/Push/PushQueue.js index 80b6c2f316..d645c6025b 100644 --- a/src/Push/PushQueue.js +++ b/src/Push/PushQueue.js @@ -1,6 +1,7 @@ import { ParseMessageQueue } from '../ParseMessageQueue'; import rest from '../rest'; import { isPushIncrementing } from './utils'; +import deepcopy from 'deepcopy'; const PUSH_CHANNEL = 'parse-server-push'; const DEFAULT_BATCH_SIZE = 100; @@ -27,7 +28,10 @@ export class PushQueue { // Order by badge (because the payload is badge dependant) // and createdAt to fix the order const order = isPushIncrementing(body) ? 'badge,createdAt' : 'createdAt'; - + where = deepcopy(where); + if (!where.hasOwnProperty('deviceToken')) { + where['deviceToken'] = {'$exists': true}; + } return Promise.resolve().then(() => { return rest.find(config, auth, From 052eb991e7ac78e334a6a756b62a3082882153be Mon Sep 17 00:00:00 2001 From: Florent Vilmart Date: Fri, 12 May 2017 13:08:42 -0400 Subject: [PATCH 3/4] nit for slower PG test --- spec/PushController.spec.js | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/spec/PushController.spec.js b/spec/PushController.spec.js index d2399fb234..cea9e2bfaa 100644 --- a/spec/PushController.spec.js +++ b/spec/PushController.spec.js @@ -716,6 +716,11 @@ describe('PushController', () => { var config = new Config(Parse.applicationId); return Parse.Object.saveAll(installations).then(() => { return pushController.sendPush(payload, {}, config, auth); + }).then(() => { + // Wait a few ms to let the push be sent + return new Promise((resolve) => { + setTimeout(resolve, 100); + }); }); }).then(() => { const query = new Parse.Query('_PushStatus'); From 90cbe421753def441dc3a8a1ccfb934362a9e669 Mon Sep 17 00:00:00 2001 From: Florent Vilmart Date: Fri, 12 May 2017 14:10:24 -0400 Subject: [PATCH 4/4] nit --- spec/PushController.spec.js | 7 +------ 1 file changed, 1 insertion(+), 6 deletions(-) diff --git a/spec/PushController.spec.js b/spec/PushController.spec.js index cea9e2bfaa..12447bcfdc 100644 --- a/spec/PushController.spec.js +++ b/spec/PushController.spec.js @@ -716,12 +716,7 @@ describe('PushController', () => { var config = new Config(Parse.applicationId); return Parse.Object.saveAll(installations).then(() => { return pushController.sendPush(payload, {}, config, auth); - }).then(() => { - // Wait a few ms to let the push be sent - return new Promise((resolve) => { - setTimeout(resolve, 100); - }); - }); + }).then(() => new Promise(resolve => setTimeout(resolve, 100))); }).then(() => { const query = new Parse.Query('_PushStatus'); return query.find({useMasterKey: true}).then((results) => {