Skip to content

Commit 3fb14ed

Browse files
authored
Support pushStatus on Parse.Push (#1302)
1 parent e0a4da2 commit 3fb14ed

File tree

6 files changed

+131
-7
lines changed

6 files changed

+131
-7
lines changed

integration/test/ParsePushTest.js

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
'use strict';
2+
3+
const Parse = require('../../node');
4+
5+
describe('Parse Push', () => {
6+
it('can get pushStatusId', async () => {
7+
const payload = {
8+
data: { alert: 'We return status!' },
9+
where: { deviceType: { $eq: 'random' } },
10+
};
11+
const pushStatusId = await Parse.Push.send(payload, { useMasterKey: true });
12+
const pushStatus = await Parse.Push.getPushStatus(pushStatusId, { useMasterKey: true });
13+
expect(pushStatus.id).toBe(pushStatusId);
14+
});
15+
});

integration/test/helper.js

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,12 @@ const defaultConfiguration = {
3636
},
3737
verbose: false,
3838
silent: true,
39+
push: {
40+
android: {
41+
senderId: 'yolo',
42+
apiKey: 'yolo',
43+
},
44+
},
3945
idempotencyOptions: {
4046
paths: ['functions/CloudFunctionIdempotency', 'jobs/CloudJob1', 'classes/IdempotentTest'],
4147
ttl: 120,

src/Push.js

Lines changed: 38 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ import CoreManager from './CoreManager';
1313
import ParseQuery from './ParseQuery';
1414

1515
import type { WhereClause } from './ParseQuery';
16+
import type { FullOptions } from './RESTController';
1617

1718
export type PushData = {
1819
where?: WhereClause | ParseQuery,
@@ -37,7 +38,7 @@ export type PushData = {
3738
*
3839
* @function send
3940
* @name Parse.Push.send
40-
* @param {object} data - The data of the push notification. Valid fields
41+
* @param {object} data - The data of the push notification. Valid fields
4142
* are:
4243
* <ol>
4344
* <li>channels - An Array of channels to push to.</li>
@@ -49,10 +50,15 @@ export type PushData = {
4950
* a set of installations to push to.</li>
5051
* <li>data - The data to send as part of the push.</li>
5152
* <ol>
53+
* @param {object} options Valid options
54+
* are:<ul>
55+
* <li>useMasterKey: In Cloud Code and Node only, causes the Master Key to
56+
* be used for this request.
57+
* </ul>
5258
* @returns {Promise} A promise that is fulfilled when the push request
5359
* completes.
5460
*/
55-
export function send(data: PushData): Promise {
61+
export function send(data: PushData, options?: FullOptions = {}): Promise {
5662
if (data.where && data.where instanceof ParseQuery) {
5763
data.where = data.where.toJSON().where;
5864
}
@@ -69,14 +75,39 @@ export function send(data: PushData): Promise {
6975
throw new Error('expiration_time and expiration_interval cannot both be set.');
7076
}
7177

72-
return CoreManager.getPushController().send(data);
78+
const pushOptions = { useMasterKey: true };
79+
if (options.hasOwnProperty('useMasterKey')) {
80+
pushOptions.useMasterKey = options.useMasterKey;
81+
}
82+
83+
return CoreManager.getPushController().send(data, pushOptions);
84+
}
85+
86+
/**
87+
* Gets push status by Id
88+
*
89+
* @function getPushStatus
90+
* @name Parse.Push.getPushStatus
91+
* @param {string} pushStatusId The Id of Push Status.
92+
* @param {object} options Valid options
93+
* are:<ul>
94+
* <li>useMasterKey: In Cloud Code and Node only, causes the Master Key to
95+
* be used for this request.
96+
* </ul>
97+
* @returns {Parse.Object} Status of Push.
98+
*/
99+
export function getPushStatus(pushStatusId: string, options?: FullOptions = {}): Promise<string> {
100+
const pushOptions = { useMasterKey: true };
101+
if (options.hasOwnProperty('useMasterKey')) {
102+
pushOptions.useMasterKey = options.useMasterKey;
103+
}
104+
const query = new ParseQuery('_PushStatus');
105+
return query.get(pushStatusId, pushOptions);
73106
}
74107

75108
const DefaultController = {
76-
send(data: PushData) {
77-
return CoreManager.getRESTController().request('POST', 'push', data, {
78-
useMasterKey: true,
79-
});
109+
send(data: PushData, options?: FullOptions) {
110+
return CoreManager.getRESTController().request('POST', 'push', data, options);
80111
},
81112
};
82113

src/RESTController.js

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -122,6 +122,9 @@ const RESTController = {
122122
if ((xhr.getAllResponseHeaders() || '').includes('x-parse-job-status-id: ')) {
123123
response = xhr.getResponseHeader('x-parse-job-status-id');
124124
}
125+
if ((xhr.getAllResponseHeaders() || '').includes('x-parse-push-status-id: ')) {
126+
response = xhr.getResponseHeader('x-parse-push-status-id');
127+
}
125128
}
126129
} catch (e) {
127130
promise.reject(e.toString());

src/__tests__/Cloud-test.js

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,9 +14,11 @@ jest.dontMock('../encode');
1414
jest.dontMock('../ParseError');
1515
jest.dontMock('../ParseObject');
1616
jest.dontMock('../ParseQuery');
17+
jest.dontMock('../Push');
1718

1819
const Cloud = require('../Cloud');
1920
const CoreManager = require('../CoreManager');
21+
const Push = require('../Push');
2022

2123
const defaultController = CoreManager.getCloudController();
2224

@@ -304,4 +306,48 @@ describe('CloudController', () => {
304306
});
305307
expect(options.useMasterKey).toBe(true);
306308
});
309+
310+
it('can get push status', async () => {
311+
const request = jest.fn();
312+
request.mockReturnValue(
313+
Promise.resolve({
314+
results: [{ className: '_PushStatus', objectId: 'pushId1234' }],
315+
})
316+
);
317+
CoreManager.setRESTController({ request: request, ajax: jest.fn() });
318+
319+
await Push.getPushStatus('pushId1234');
320+
const [method, path, data, options] = request.mock.calls[0];
321+
expect(method).toBe('GET');
322+
expect(path).toBe('classes/_PushStatus');
323+
expect(data).toEqual({
324+
limit: 1,
325+
where: {
326+
objectId: 'pushId1234',
327+
},
328+
});
329+
expect(options.useMasterKey).toBe(true);
330+
});
331+
332+
it('can get push status with masterKey', async () => {
333+
const request = jest.fn();
334+
request.mockReturnValue(
335+
Promise.resolve({
336+
results: [{ className: '_PushStatus', objectId: 'pushId1234' }],
337+
})
338+
);
339+
CoreManager.setRESTController({ request: request, ajax: jest.fn() });
340+
341+
await Push.getPushStatus('pushId1234', { useMasterKey: false });
342+
const [method, path, data, options] = request.mock.calls[0];
343+
expect(method).toBe('GET');
344+
expect(path).toBe('classes/_PushStatus');
345+
expect(data).toEqual({
346+
limit: 1,
347+
where: {
348+
objectId: 'pushId1234',
349+
},
350+
});
351+
expect(options.useMasterKey).toBe(false);
352+
});
307353
});

src/__tests__/RESTController-test.js

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -242,6 +242,29 @@ describe('RESTController', () => {
242242
expect(response).toBe(1234);
243243
});
244244

245+
it('handles x-parse-push-status-id header', async () => {
246+
const XHR = function () {};
247+
XHR.prototype = {
248+
open: function () {},
249+
setRequestHeader: function () {},
250+
getResponseHeader: function () {
251+
return 1234;
252+
},
253+
send: function () {
254+
this.status = 200;
255+
this.responseText = '{}';
256+
this.readyState = 4;
257+
this.onreadystatechange();
258+
},
259+
getAllResponseHeaders: function () {
260+
return 'x-parse-push-status-id: 1234';
261+
},
262+
};
263+
RESTController._setXHR(XHR);
264+
const response = await RESTController.request('POST', 'push', {}, {});
265+
expect(response).toBe(1234);
266+
});
267+
245268
it('handles invalid header', async () => {
246269
const XHR = function () {};
247270
XHR.prototype = {

0 commit comments

Comments
 (0)