diff --git a/spec/PushController.spec.js b/spec/PushController.spec.js index aa607389..252e3423 100644 --- a/spec/PushController.spec.js +++ b/spec/PushController.spec.js @@ -503,6 +503,62 @@ describe('PushController', () => { .then(done).catch(done.fail); }); + it('properly creates _PushStatus without serverURL', (done) => { + const pushStatusAfterSave = { + handler: function() {} + }; + Parse.Cloud.afterSave('_PushStatus', pushStatusAfterSave.handler); + const installation = new Parse.Object("_Installation"); + installation.set("installationId", "installation"); + installation.set("deviceToken","device_token") + installation.set("badge", 0); + installation.set("originalBadge", 0); + installation.set("deviceType", "ios"); + + var payload = {data: { + alert: "Hello World!", + badge: 1, + }} + + var pushAdapter = { + send: function(body, installations) { + return successfulIOS(body, installations); + }, + getValidPushTypes: function() { + return ["ios"]; + } + } + + var config = new Config(Parse.applicationId); + var auth = { + isMaster: true + } + var pushController = new PushController(); + return installation.save().then(() => { + return reconfigureServer({ + serverURL: 'http://localhost:8378/', // server with borked URL + push: { adapter: pushAdapter } + }) + }) + .then(() => { + return pushController.sendPush(payload, {}, config, auth); + }).then(() => { + // it is enqueued so it can take time + return new Promise((resolve) => { + setTimeout(() => { + resolve(); + }, 1000); + }); + }).then(() => { + Parse.serverURL = 'http://localhost:8378/1'; // GOOD url + const query = new Parse.Query('_PushStatus'); + return query.find({useMasterKey: true}); + }).then((results) => { + expect(results.length).toBe(1); + }) + .then(done).catch(done.fail); + }); + it('should properly report failures in _PushStatus', (done) => { var pushAdapter = { send: function(body, installations) { diff --git a/spec/PushWorker.spec.js b/spec/PushWorker.spec.js index 8848ddca..5499ee4c 100644 --- a/spec/PushWorker.spec.js +++ b/spec/PushWorker.spec.js @@ -2,6 +2,7 @@ var PushWorker = require('../src').PushWorker; var PushUtils = require('../src/Push/utils'); var Config = require('../src/Config'); var { pushStatusHandler } = require('../src/StatusHandler'); +var rest = require('../src/rest'); describe('PushWorker', () => { it('should run with small batch', (done) => { @@ -245,7 +246,7 @@ describe('PushWorker', () => { it('tracks push status per UTC offsets', (done) => { const config = new Config('test'); const handler = pushStatusHandler(config); - const spy = spyOn(Parse, "_request").and.callThrough(); + const spy = spyOn(rest, "update").and.callThrough(); const UTCOffset = 1; handler.setInitial().then(() => { return handler.trackSent([ @@ -267,9 +268,8 @@ describe('PushWorker', () => { }).then(() => { expect(spy).toHaveBeenCalled(); const lastCall = spy.calls.mostRecent(); - expect(lastCall.args[0]).toBe('PUT'); - expect(lastCall.args[1]).toBe(`classes/_PushStatus/${handler.objectId}`); - expect(lastCall.args[2]).toEqual({ + expect(lastCall.args[2]).toBe(`_PushStatus`); + expect(lastCall.args[4]).toEqual({ numSent: { __op: 'Increment', amount: 1 }, numFailed: { __op: 'Increment', amount: 1 }, 'sentPerType.ios': { __op: 'Increment', amount: 1 }, @@ -322,35 +322,32 @@ describe('PushWorker', () => { it('tracks push status per UTC offsets with negative offsets', (done) => { const config = new Config('test'); const handler = pushStatusHandler(config); - spyOn(config.database, "create").and.callFake(() => { - return Promise.resolve(); - }); - const spy = spyOn(Parse, "_request").and.callFake(() => { - return Promise.resolve(); - }); + const spy = spyOn(rest, "update").and.callThrough(); const UTCOffset = -6; - handler.trackSent([ - { - transmitted: false, - device: { - deviceToken: 1, - deviceType: 'ios', + handler.setInitial().then(() => { + return handler.trackSent([ + { + transmitted: false, + device: { + deviceToken: 1, + deviceType: 'ios', + }, + response: { error: 'Unregistered' } }, - response: { error: 'Unregistered' } - }, - { - transmitted: true, - device: { - deviceToken: 1, - deviceType: 'ios', + { + transmitted: true, + device: { + deviceToken: 1, + deviceType: 'ios', + }, + response: { error: 'Unregistered' } }, - response: { error: 'Unregistered' } - }, - ], UTCOffset).then(() => { + ], UTCOffset); + }).then(() => { expect(spy).toHaveBeenCalled(); const lastCall = spy.calls.mostRecent(); - expect(lastCall.args[1]).toBe(`classes/_PushStatus/${handler.objectId}`); - expect(lastCall.args[2]).toEqual({ + expect(lastCall.args[2]).toBe('_PushStatus'); + expect(lastCall.args[4]).toEqual({ numSent: { __op: 'Increment', amount: 1 }, numFailed: { __op: 'Increment', amount: 1 }, 'sentPerType.ios': { __op: 'Increment', amount: 1 }, diff --git a/src/StatusHandler.js b/src/StatusHandler.js index b29e580d..d1ab5dfb 100644 --- a/src/StatusHandler.js +++ b/src/StatusHandler.js @@ -1,6 +1,7 @@ import { md5Hash, newObjectId } from './cryptoUtils'; import { logger } from './logger'; -import Parse from 'parse/node'; +import rest from './rest'; +import Auth from './Auth'; const PUSH_STATUS_COLLECTION = '_PushStatus'; const JOB_STATUS_COLLECTION = '_JobStatus'; @@ -51,21 +52,16 @@ function statusHandler(className, database) { }) } -function restStatusHandler(className) { +function restStatusHandler(className, config) { let lastPromise = Promise.resolve(); - + const auth = Auth.master(config); function create(object) { lastPromise = lastPromise.then(() => { - return Parse._request( - 'POST', - `classes/${className}`, - object, - { useMasterKey: true } - ).then((result) => { - // merge the objects - const response = Object.assign({}, object, result); - return Promise.resolve(response); - }); + return rest.create(config, auth, className, object) + .then(({ response }) => { + // merge the objects + return Promise.resolve(Object.assign({}, object, response)); + }); }); return lastPromise; } @@ -73,15 +69,11 @@ function restStatusHandler(className) { function update(where, object) { // TODO: when we have updateWhere, use that for proper interfacing lastPromise = lastPromise.then(() => { - return Parse._request( - 'PUT', - `classes/${className}/${where.objectId}`, - object, - { useMasterKey: true }).then((result) => { - // merge the objects - const response = Object.assign({}, object, result); - return Promise.resolve(response); - }); + return rest.update(config, auth, className, { objectId: where.objectId }, object) + .then(({ response }) => { + // merge the objects + return Promise.resolve(Object.assign({}, object, response)); + }); }); return lastPromise; } @@ -149,7 +141,7 @@ export function pushStatusHandler(config, existingObjectId) { let pushStatus; const database = config.database; - const handler = restStatusHandler(PUSH_STATUS_COLLECTION); + const handler = restStatusHandler(PUSH_STATUS_COLLECTION, config); let objectId = existingObjectId; const setInitial = function(body = {}, where, options = {source: 'rest'}) { const now = new Date(); @@ -188,7 +180,6 @@ export function pushStatusHandler(config, existingObjectId) { // lockdown! ACL: {} } - return handler.create(object).then((result) => { objectId = result.objectId; pushStatus = {