Skip to content

Commit

Permalink
Before Connect + Before Subscribe #1
Browse files Browse the repository at this point in the history
  • Loading branch information
dblythy committed Oct 20, 2020
1 parent 5479427 commit 1fb1ca3
Show file tree
Hide file tree
Showing 2 changed files with 72 additions and 26 deletions.
70 changes: 58 additions & 12 deletions spec/ParseLiveQueryServer.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -232,7 +232,7 @@ describe('ParseLiveQueryServer', function () {
classNames: ['Yolo'],
},
})
.then(parseServer => {
.then((parseServer) => {
saveSpy = spyOn(parseServer.config.liveQueryController, 'onAfterSave');
deleteSpy = spyOn(
parseServer.config.liveQueryController,
Expand All @@ -247,7 +247,7 @@ describe('ParseLiveQueryServer', function () {
const obj = new Parse.Object('Yolo');
return obj.save();
})
.then(obj => {
.then((obj) => {
return obj.destroy();
})
.then(() => {
Expand Down Expand Up @@ -1546,7 +1546,7 @@ describe('ParseLiveQueryServer', function () {
});

describe('class level permissions', () => {
it('matches CLP when find is closed', done => {
it('matches CLP when find is closed', (done) => {
const parseLiveQueryServer = new ParseLiveQueryServer({});
const acl = new Parse.ACL();
acl.setReadAccess(testUserId, true);
Expand All @@ -1571,13 +1571,13 @@ describe('ParseLiveQueryServer', function () {
requestId,
'find'
)
.then(isMatched => {
.then((isMatched) => {
expect(isMatched).toBe(false);
done();
});
});

it('matches CLP when find is open', done => {
it('matches CLP when find is open', (done) => {
const parseLiveQueryServer = new ParseLiveQueryServer({});
const acl = new Parse.ACL();
acl.setReadAccess(testUserId, true);
Expand All @@ -1602,13 +1602,13 @@ describe('ParseLiveQueryServer', function () {
requestId,
'find'
)
.then(isMatched => {
.then((isMatched) => {
expect(isMatched).toBe(true);
done();
});
});

it('matches CLP when find is restricted to userIds', done => {
it('matches CLP when find is restricted to userIds', (done) => {
const parseLiveQueryServer = new ParseLiveQueryServer({});
const acl = new Parse.ACL();
acl.setReadAccess(testUserId, true);
Expand All @@ -1633,13 +1633,13 @@ describe('ParseLiveQueryServer', function () {
requestId,
'find'
)
.then(isMatched => {
.then((isMatched) => {
expect(isMatched).toBe(true);
done();
});
});

it('matches CLP when find is restricted to userIds', done => {
it('matches CLP when find is restricted to userIds', (done) => {
const parseLiveQueryServer = new ParseLiveQueryServer({});
const acl = new Parse.ACL();
acl.setReadAccess(testUserId, true);
Expand All @@ -1664,7 +1664,7 @@ describe('ParseLiveQueryServer', function () {
requestId,
'find'
)
.then(isMatched => {
.then((isMatched) => {
expect(isMatched).toBe(false);
done();
});
Expand Down Expand Up @@ -2001,7 +2001,7 @@ describe('LiveQueryController', () => {
classNames: ['Yolo'],
},
})
.then(parseServer => {
.then((parseServer) => {
saveSpy = spyOn(
parseServer.config.liveQueryController,
'onAfterSave'
Expand All @@ -2019,7 +2019,7 @@ describe('LiveQueryController', () => {
const obj = new Parse.Object('Yolo');
return obj.save();
})
.then(obj => {
.then((obj) => {
return obj.destroy();
})
.then(() => {
Expand Down Expand Up @@ -2099,3 +2099,49 @@ describe('LiveQueryController', () => {
});
});
});

it('basic beforeConnect rejection', async () => {
Parse.Cloud.beforeConnect(function () {
throw new Error('You shall not pass!');
});
const parseLiveQueryServer = new ParseLiveQueryServer({});
const parseWebSocket = {
clientId: -1,
};
await parseLiveQueryServer._handleConnect(parseWebSocket, {
sessionToken: 'token',
});
expect(parseLiveQueryServer.clients.size).toBe(0);
const Client = require('../lib/LiveQuery/Client').Client;
expect(Client.pushError).toHaveBeenCalled();
});

it('basic beforeSubscribe rejection', async () => {
Parse.Cloud.beforeSubscribe('test', function () {
throw new Error('You shall not pass!');
});
const parseLiveQueryServer = new ParseLiveQueryServer({});
const parseWebSocket = {
clientId: -1,
};
await parseLiveQueryServer._handleConnect(parseWebSocket, {
sessionToken: 'token',
});
const query = {
className: 'test',
where: {
key: 'value',
},
fields: ['test'],
};
const requestId = 2;
const request = {
query: query,
requestId: requestId,
sessionToken: 'sessionToken',
};
await parseLiveQueryServer._handleSubscribe(parseWebSocket, request);
expect(parseLiveQueryServer.clients.size).toBe(0);
const Client = require('../lib/LiveQuery/Client').Client;
expect(Client.pushError).toHaveBeenCalled();
});
28 changes: 14 additions & 14 deletions src/triggers.js
Original file line number Diff line number Diff line change
Expand Up @@ -155,7 +155,7 @@ export function removeTrigger(type, className, applicationId) {
}

export function _unregisterAll() {
Object.keys(_triggerStore).forEach(appId => delete _triggerStore[appId]);
Object.keys(_triggerStore).forEach((appId) => delete _triggerStore[appId]);
}

export function getTrigger(className, triggerType, applicationId) {
Expand Down Expand Up @@ -188,7 +188,7 @@ export function getFunctionNames(applicationId) {
{};
const functionNames = [];
const extractFunctionNames = (namespace, store) => {
Object.keys(store).forEach(name => {
Object.keys(store).forEach((name) => {
const value = store[name];
if (namespace) {
name = `${namespace}.${name}`;
Expand Down Expand Up @@ -315,7 +315,7 @@ export function getResponseObject(request, resolve, reject) {
if (!response) {
response = request.objects;
}
response = response.map(object => {
response = response.map((object) => {
return object.toJSON();
});
return resolve(response);
Expand Down Expand Up @@ -426,10 +426,10 @@ export function maybeRunAfterFindTrigger(
const request = getRequestObject(triggerType, auth, null, null, config);
const { success, error } = getResponseObject(
request,
object => {
(object) => {
resolve(object);
},
error => {
(error) => {
reject(error);
}
);
Expand All @@ -440,7 +440,7 @@ export function maybeRunAfterFindTrigger(
JSON.stringify(objects),
auth
);
request.objects = objects.map(object => {
request.objects = objects.map((object) => {
//setting the class name to transform into parse object
object.className = className;
return Parse.Object.fromJSON(object);
Expand All @@ -449,7 +449,7 @@ export function maybeRunAfterFindTrigger(
.then(() => {
const response = trigger(request);
if (response && typeof response.then === 'function') {
return response.then(results => {
return response.then((results) => {
if (!results) {
throw new Parse.Error(
Parse.Error.SCRIPT_FAILED,
Expand All @@ -462,7 +462,7 @@ export function maybeRunAfterFindTrigger(
return response;
})
.then(success, error);
}).then(results => {
}).then((results) => {
logTriggerAfterHook(triggerType, className, JSON.stringify(results), auth);
return results;
});
Expand Down Expand Up @@ -509,7 +509,7 @@ export function maybeRunQueryTrigger(
return trigger(requestObject);
})
.then(
result => {
(result) => {
let queryResult = parseQuery;
if (result && result instanceof Parse.Query) {
queryResult = result;
Expand Down Expand Up @@ -569,7 +569,7 @@ export function maybeRunQueryTrigger(
restOptions,
};
},
err => {
(err) => {
if (typeof err === 'string') {
throw new Parse.Error(1, err);
} else {
Expand Down Expand Up @@ -612,7 +612,7 @@ export function maybeRunTrigger(
);
var { success, error } = getResponseObject(
request,
object => {
(object) => {
logTriggerSuccessBeforeHook(
triggerType,
parseObject.className,
Expand All @@ -630,7 +630,7 @@ export function maybeRunTrigger(
}
resolve(object);
},
error => {
(error) => {
logTriggerErrorBeforeHook(
triggerType,
parseObject.className,
Expand Down Expand Up @@ -665,7 +665,7 @@ export function maybeRunTrigger(
// beforeSave is expected to return null (nothing)
if (triggerType === Types.beforeSave) {
if (promise && typeof promise.then === 'function') {
return promise.then(response => {
return promise.then((response) => {
// response.object may come from express routing before hook
if (response && response.object) {
return response;
Expand Down Expand Up @@ -703,7 +703,7 @@ export function runLiveQueryEventHandlers(
) {
return;
}
_triggerStore[applicationId].LiveQuery.forEach(handler => handler(data));
_triggerStore[applicationId].LiveQuery.forEach((handler) => handler(data));
}

export function getRequestFileObject(triggerType, auth, fileObject, config) {
Expand Down

0 comments on commit 1fb1ca3

Please sign in to comment.