[PushController] Fixes issue with undefined push_time (#3717) (#3824)

* Fixes #3717

This fixes PR #3717. Sending push with parse-server@2.4.0 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
This commit is contained in:
Felipe Andrade
2017-05-20 13:07:45 -03:00
committed by Florent Vilmart
parent 35d781a160
commit 03b6449fe1
2 changed files with 55 additions and 5 deletions

View File

@@ -131,6 +131,42 @@ describe('PushController', () => {
done();
});
it('can get push time in string format', (done) => {
// Make mock request
var timeStr = '2015-03-19T22:05:08Z';
var body = {
'push_time': timeStr
}
var time = PushController.getPushTime(body);
expect(time).toEqual(new Date(timeStr));
done();
});
it('can get push time in number format', (done) => {
// Make mock request
var timeNumber = 1426802708;
var body = {
'push_time': timeNumber
}
var time = PushController.getPushTime(body).valueOf();
expect(time).toEqual(timeNumber * 1000);
done();
});
it('can throw on getPushTime in invalid format', (done) => {
// Make mock request
var body = {
'push_time': 'abcd'
}
expect(function(){
PushController.getPushTime(body);
}).toThrow();
done();
});
it('properly increment badges', (done) => {
var pushAdapter = {
send: function(body, installations) {
@@ -603,13 +639,24 @@ describe('PushController', () => {
});
});
it('should not schedule push when configured', (done) => {
it('should schedule push when configured', (done) => {
var auth = {
isMaster: true
}
var pushAdapter = {
send: function(body, installations) {
return successfulTransmissions(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"];
@@ -642,7 +689,7 @@ describe('PushController', () => {
var config = new Config(Parse.applicationId);
return Parse.Object.saveAll(installations).then(() => {
return pushController.sendPush(payload, {}, config, auth);
});
}).then(() => new Promise(resolve => setTimeout(resolve, 100)));
}).then(() => {
const query = new Parse.Query('_PushStatus');
return query.find({useMasterKey: true}).then((results) => {

View File

@@ -14,7 +14,10 @@ export class PushController {
}
// Replace the expiration_time and push_time with a valid Unix epoch milliseconds time
body.expiration_time = PushController.getExpirationTime(body);
body.push_time = PushController.getPushTime(body);
const push_time = PushController.getPushTime(body);
if (typeof push_time !== 'undefined') {
body['push_time'] = push_time;
}
// TODO: If the req can pass the checking, we return immediately instead of waiting
// pushes to be sent. We probably change this behaviour in the future.
let badgeUpdate = () => {
@@ -50,7 +53,7 @@ export class PushController {
onPushStatusSaved(pushStatus.objectId);
return badgeUpdate();
}).then(() => {
if (body.push_time && config.hasPushScheduledSupport) {
if (body.hasOwnProperty('push_time') && config.hasPushScheduledSupport) {
return Promise.resolve();
}
return config.pushControllerQueue.enqueue(body, where, config, auth, pushStatus);