Skip to content

Commit 03b6449

Browse files
felipeandradebezerraflovilmart
authored andcommitted
[PushController] Fixes issue with undefined push_time (#3717) (#3824)
* Fixes #3717 This fixes PR #3717. Sending push with [email protected] returns error 504 GATEWAY_TIMEOUT. This happens when push_time is not set (default). * Fix lint issues * Fix in PushController and add tests Add a test to check push_time format and if it should schedule push when the parse-server is configured
1 parent 35d781a commit 03b6449

File tree

2 files changed

+55
-5
lines changed

2 files changed

+55
-5
lines changed

spec/PushController.spec.js

+50-3
Original file line numberDiff line numberDiff line change
@@ -131,6 +131,42 @@ describe('PushController', () => {
131131
done();
132132
});
133133

134+
it('can get push time in string format', (done) => {
135+
// Make mock request
136+
var timeStr = '2015-03-19T22:05:08Z';
137+
var body = {
138+
'push_time': timeStr
139+
}
140+
141+
var time = PushController.getPushTime(body);
142+
expect(time).toEqual(new Date(timeStr));
143+
done();
144+
});
145+
146+
it('can get push time in number format', (done) => {
147+
// Make mock request
148+
var timeNumber = 1426802708;
149+
var body = {
150+
'push_time': timeNumber
151+
}
152+
153+
var time = PushController.getPushTime(body).valueOf();
154+
expect(time).toEqual(timeNumber * 1000);
155+
done();
156+
});
157+
158+
it('can throw on getPushTime in invalid format', (done) => {
159+
// Make mock request
160+
var body = {
161+
'push_time': 'abcd'
162+
}
163+
164+
expect(function(){
165+
PushController.getPushTime(body);
166+
}).toThrow();
167+
done();
168+
});
169+
134170
it('properly increment badges', (done) => {
135171
var pushAdapter = {
136172
send: function(body, installations) {
@@ -603,13 +639,24 @@ describe('PushController', () => {
603639
});
604640
});
605641

606-
it('should not schedule push when configured', (done) => {
642+
it('should schedule push when configured', (done) => {
607643
var auth = {
608644
isMaster: true
609645
}
610646
var pushAdapter = {
611647
send: function(body, installations) {
612-
return successfulTransmissions(body, installations);
648+
const promises = installations.map((device) => {
649+
if (!device.deviceToken) {
650+
// Simulate error when device token is not set
651+
return Promise.reject();
652+
}
653+
return Promise.resolve({
654+
transmitted: true,
655+
device: device,
656+
})
657+
});
658+
659+
return Promise.all(promises);
613660
},
614661
getValidPushTypes: function() {
615662
return ["ios"];
@@ -642,7 +689,7 @@ describe('PushController', () => {
642689
var config = new Config(Parse.applicationId);
643690
return Parse.Object.saveAll(installations).then(() => {
644691
return pushController.sendPush(payload, {}, config, auth);
645-
});
692+
}).then(() => new Promise(resolve => setTimeout(resolve, 100)));
646693
}).then(() => {
647694
const query = new Parse.Query('_PushStatus');
648695
return query.find({useMasterKey: true}).then((results) => {

src/Controllers/PushController.js

+5-2
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,10 @@ export class PushController {
1414
}
1515
// Replace the expiration_time and push_time with a valid Unix epoch milliseconds time
1616
body.expiration_time = PushController.getExpirationTime(body);
17-
body.push_time = PushController.getPushTime(body);
17+
const push_time = PushController.getPushTime(body);
18+
if (typeof push_time !== 'undefined') {
19+
body['push_time'] = push_time;
20+
}
1821
// TODO: If the req can pass the checking, we return immediately instead of waiting
1922
// pushes to be sent. We probably change this behaviour in the future.
2023
let badgeUpdate = () => {
@@ -50,7 +53,7 @@ export class PushController {
5053
onPushStatusSaved(pushStatus.objectId);
5154
return badgeUpdate();
5255
}).then(() => {
53-
if (body.push_time && config.hasPushScheduledSupport) {
56+
if (body.hasOwnProperty('push_time') && config.hasPushScheduledSupport) {
5457
return Promise.resolve();
5558
}
5659
return config.pushControllerQueue.enqueue(body, where, config, auth, pushStatus);

0 commit comments

Comments
 (0)