Skip to content

Commit 8704a35

Browse files
committed
Before Connect + Before Subscribe #1
1 parent 8cbb3b0 commit 8704a35

File tree

2 files changed

+72
-26
lines changed

2 files changed

+72
-26
lines changed

spec/ParseLiveQueryServer.spec.js

Lines changed: 58 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -232,7 +232,7 @@ describe('ParseLiveQueryServer', function () {
232232
classNames: ['Yolo'],
233233
},
234234
})
235-
.then(parseServer => {
235+
.then((parseServer) => {
236236
saveSpy = spyOn(parseServer.config.liveQueryController, 'onAfterSave');
237237
deleteSpy = spyOn(
238238
parseServer.config.liveQueryController,
@@ -247,7 +247,7 @@ describe('ParseLiveQueryServer', function () {
247247
const obj = new Parse.Object('Yolo');
248248
return obj.save();
249249
})
250-
.then(obj => {
250+
.then((obj) => {
251251
return obj.destroy();
252252
})
253253
.then(() => {
@@ -1546,7 +1546,7 @@ describe('ParseLiveQueryServer', function () {
15461546
});
15471547

15481548
describe('class level permissions', () => {
1549-
it('matches CLP when find is closed', done => {
1549+
it('matches CLP when find is closed', (done) => {
15501550
const parseLiveQueryServer = new ParseLiveQueryServer({});
15511551
const acl = new Parse.ACL();
15521552
acl.setReadAccess(testUserId, true);
@@ -1571,13 +1571,13 @@ describe('ParseLiveQueryServer', function () {
15711571
requestId,
15721572
'find'
15731573
)
1574-
.then(isMatched => {
1574+
.then((isMatched) => {
15751575
expect(isMatched).toBe(false);
15761576
done();
15771577
});
15781578
});
15791579

1580-
it('matches CLP when find is open', done => {
1580+
it('matches CLP when find is open', (done) => {
15811581
const parseLiveQueryServer = new ParseLiveQueryServer({});
15821582
const acl = new Parse.ACL();
15831583
acl.setReadAccess(testUserId, true);
@@ -1602,13 +1602,13 @@ describe('ParseLiveQueryServer', function () {
16021602
requestId,
16031603
'find'
16041604
)
1605-
.then(isMatched => {
1605+
.then((isMatched) => {
16061606
expect(isMatched).toBe(true);
16071607
done();
16081608
});
16091609
});
16101610

1611-
it('matches CLP when find is restricted to userIds', done => {
1611+
it('matches CLP when find is restricted to userIds', (done) => {
16121612
const parseLiveQueryServer = new ParseLiveQueryServer({});
16131613
const acl = new Parse.ACL();
16141614
acl.setReadAccess(testUserId, true);
@@ -1633,13 +1633,13 @@ describe('ParseLiveQueryServer', function () {
16331633
requestId,
16341634
'find'
16351635
)
1636-
.then(isMatched => {
1636+
.then((isMatched) => {
16371637
expect(isMatched).toBe(true);
16381638
done();
16391639
});
16401640
});
16411641

1642-
it('matches CLP when find is restricted to userIds', done => {
1642+
it('matches CLP when find is restricted to userIds', (done) => {
16431643
const parseLiveQueryServer = new ParseLiveQueryServer({});
16441644
const acl = new Parse.ACL();
16451645
acl.setReadAccess(testUserId, true);
@@ -1664,7 +1664,7 @@ describe('ParseLiveQueryServer', function () {
16641664
requestId,
16651665
'find'
16661666
)
1667-
.then(isMatched => {
1667+
.then((isMatched) => {
16681668
expect(isMatched).toBe(false);
16691669
done();
16701670
});
@@ -2001,7 +2001,7 @@ describe('LiveQueryController', () => {
20012001
classNames: ['Yolo'],
20022002
},
20032003
})
2004-
.then(parseServer => {
2004+
.then((parseServer) => {
20052005
saveSpy = spyOn(
20062006
parseServer.config.liveQueryController,
20072007
'onAfterSave'
@@ -2019,7 +2019,7 @@ describe('LiveQueryController', () => {
20192019
const obj = new Parse.Object('Yolo');
20202020
return obj.save();
20212021
})
2022-
.then(obj => {
2022+
.then((obj) => {
20232023
return obj.destroy();
20242024
})
20252025
.then(() => {
@@ -2099,3 +2099,49 @@ describe('LiveQueryController', () => {
20992099
});
21002100
});
21012101
});
2102+
2103+
it('basic beforeConnect rejection', async () => {
2104+
Parse.Cloud.beforeConnect(function () {
2105+
throw new Error('You shall not pass!');
2106+
});
2107+
const parseLiveQueryServer = new ParseLiveQueryServer({});
2108+
const parseWebSocket = {
2109+
clientId: -1,
2110+
};
2111+
await parseLiveQueryServer._handleConnect(parseWebSocket, {
2112+
sessionToken: 'token',
2113+
});
2114+
expect(parseLiveQueryServer.clients.size).toBe(0);
2115+
const Client = require('../lib/LiveQuery/Client').Client;
2116+
expect(Client.pushError).toHaveBeenCalled();
2117+
});
2118+
2119+
it('basic beforeSubscribe rejection', async () => {
2120+
Parse.Cloud.beforeSubscribe('test', function () {
2121+
throw new Error('You shall not pass!');
2122+
});
2123+
const parseLiveQueryServer = new ParseLiveQueryServer({});
2124+
const parseWebSocket = {
2125+
clientId: -1,
2126+
};
2127+
await parseLiveQueryServer._handleConnect(parseWebSocket, {
2128+
sessionToken: 'token',
2129+
});
2130+
const query = {
2131+
className: 'test',
2132+
where: {
2133+
key: 'value',
2134+
},
2135+
fields: ['test'],
2136+
};
2137+
const requestId = 2;
2138+
const request = {
2139+
query: query,
2140+
requestId: requestId,
2141+
sessionToken: 'sessionToken',
2142+
};
2143+
await parseLiveQueryServer._handleSubscribe(parseWebSocket, request);
2144+
expect(parseLiveQueryServer.clients.size).toBe(0);
2145+
const Client = require('../lib/LiveQuery/Client').Client;
2146+
expect(Client.pushError).toHaveBeenCalled();
2147+
});

src/triggers.js

Lines changed: 14 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -155,7 +155,7 @@ export function removeTrigger(type, className, applicationId) {
155155
}
156156

157157
export function _unregisterAll() {
158-
Object.keys(_triggerStore).forEach(appId => delete _triggerStore[appId]);
158+
Object.keys(_triggerStore).forEach((appId) => delete _triggerStore[appId]);
159159
}
160160

161161
export function getTrigger(className, triggerType, applicationId) {
@@ -188,7 +188,7 @@ export function getFunctionNames(applicationId) {
188188
{};
189189
const functionNames = [];
190190
const extractFunctionNames = (namespace, store) => {
191-
Object.keys(store).forEach(name => {
191+
Object.keys(store).forEach((name) => {
192192
const value = store[name];
193193
if (namespace) {
194194
name = `${namespace}.${name}`;
@@ -315,7 +315,7 @@ export function getResponseObject(request, resolve, reject) {
315315
if (!response) {
316316
response = request.objects;
317317
}
318-
response = response.map(object => {
318+
response = response.map((object) => {
319319
return object.toJSON();
320320
});
321321
return resolve(response);
@@ -426,10 +426,10 @@ export function maybeRunAfterFindTrigger(
426426
const request = getRequestObject(triggerType, auth, null, null, config);
427427
const { success, error } = getResponseObject(
428428
request,
429-
object => {
429+
(object) => {
430430
resolve(object);
431431
},
432-
error => {
432+
(error) => {
433433
reject(error);
434434
}
435435
);
@@ -440,7 +440,7 @@ export function maybeRunAfterFindTrigger(
440440
JSON.stringify(objects),
441441
auth
442442
);
443-
request.objects = objects.map(object => {
443+
request.objects = objects.map((object) => {
444444
//setting the class name to transform into parse object
445445
object.className = className;
446446
return Parse.Object.fromJSON(object);
@@ -449,7 +449,7 @@ export function maybeRunAfterFindTrigger(
449449
.then(() => {
450450
const response = trigger(request);
451451
if (response && typeof response.then === 'function') {
452-
return response.then(results => {
452+
return response.then((results) => {
453453
if (!results) {
454454
throw new Parse.Error(
455455
Parse.Error.SCRIPT_FAILED,
@@ -462,7 +462,7 @@ export function maybeRunAfterFindTrigger(
462462
return response;
463463
})
464464
.then(success, error);
465-
}).then(results => {
465+
}).then((results) => {
466466
logTriggerAfterHook(triggerType, className, JSON.stringify(results), auth);
467467
return results;
468468
});
@@ -509,7 +509,7 @@ export function maybeRunQueryTrigger(
509509
return trigger(requestObject);
510510
})
511511
.then(
512-
result => {
512+
(result) => {
513513
let queryResult = parseQuery;
514514
if (result && result instanceof Parse.Query) {
515515
queryResult = result;
@@ -569,7 +569,7 @@ export function maybeRunQueryTrigger(
569569
restOptions,
570570
};
571571
},
572-
err => {
572+
(err) => {
573573
if (typeof err === 'string') {
574574
throw new Parse.Error(1, err);
575575
} else {
@@ -612,7 +612,7 @@ export function maybeRunTrigger(
612612
);
613613
var { success, error } = getResponseObject(
614614
request,
615-
object => {
615+
(object) => {
616616
logTriggerSuccessBeforeHook(
617617
triggerType,
618618
parseObject.className,
@@ -630,7 +630,7 @@ export function maybeRunTrigger(
630630
}
631631
resolve(object);
632632
},
633-
error => {
633+
(error) => {
634634
logTriggerErrorBeforeHook(
635635
triggerType,
636636
parseObject.className,
@@ -665,7 +665,7 @@ export function maybeRunTrigger(
665665
// beforeSave is expected to return null (nothing)
666666
if (triggerType === Types.beforeSave) {
667667
if (promise && typeof promise.then === 'function') {
668-
return promise.then(response => {
668+
return promise.then((response) => {
669669
// response.object may come from express routing before hook
670670
if (response && response.object) {
671671
return response;
@@ -703,7 +703,7 @@ export function runLiveQueryEventHandlers(
703703
) {
704704
return;
705705
}
706-
_triggerStore[applicationId].LiveQuery.forEach(handler => handler(data));
706+
_triggerStore[applicationId].LiveQuery.forEach((handler) => handler(data));
707707
}
708708

709709
export function getRequestFileObject(triggerType, auth, fileObject, config) {

0 commit comments

Comments
 (0)