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 8704e26 commit eedcd35
Show file tree
Hide file tree
Showing 3 changed files with 80 additions and 116 deletions.
132 changes: 66 additions & 66 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 @@ -307,52 +307,6 @@ describe('ParseLiveQueryServer', function () {
expect(client.pushConnect).toHaveBeenCalled();
});

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(1);
const Client = require('../lib/LiveQuery/Client').Client;
expect(Client.pushError).toHaveBeenCalled();
});

it('can handle subscribe command without clientId', async () => {
const parseLiveQueryServer = new ParseLiveQueryServer({});
const incompleteParseConn = {};
Expand Down Expand Up @@ -775,7 +729,7 @@ describe('ParseLiveQueryServer', function () {
expect(client.pushDelete).not.toHaveBeenCalled();
});

it('can handle object delete command which matches some subscriptions', async done => {
it('can handle object delete command which matches some subscriptions', async (done) => {
const parseLiveQueryServer = new ParseLiveQueryServer({});
// Make deletedParseObject
const parseObject = new Parse.Object(testClassName);
Expand Down Expand Up @@ -819,7 +773,7 @@ describe('ParseLiveQueryServer', function () {
parseLiveQueryServer._onAfterSave(message);
});

it('can handle object save command which does not match any subscription', async done => {
it('can handle object save command which does not match any subscription', async (done) => {
const parseLiveQueryServer = new ParseLiveQueryServer({});
// Make mock request message
const message = generateMockMessage();
Expand Down Expand Up @@ -850,7 +804,7 @@ describe('ParseLiveQueryServer', function () {
}, jasmine.ASYNC_TEST_WAIT_TIME);
});

it('can handle object enter command which matches some subscriptions', async done => {
it('can handle object enter command which matches some subscriptions', async (done) => {
const parseLiveQueryServer = new ParseLiveQueryServer({});
// Make mock request message
const message = generateMockMessage(true);
Expand Down Expand Up @@ -887,7 +841,7 @@ describe('ParseLiveQueryServer', function () {
}, jasmine.ASYNC_TEST_WAIT_TIME);
});

it('can handle object update command which matches some subscriptions', async done => {
it('can handle object update command which matches some subscriptions', async (done) => {
const parseLiveQueryServer = new ParseLiveQueryServer({});
// Make mock request message
const message = generateMockMessage(true);
Expand Down Expand Up @@ -920,7 +874,7 @@ describe('ParseLiveQueryServer', function () {
}, jasmine.ASYNC_TEST_WAIT_TIME);
});

it('can handle object leave command which matches some subscriptions', async done => {
it('can handle object leave command which matches some subscriptions', async (done) => {
const parseLiveQueryServer = new ParseLiveQueryServer({});
// Make mock request message
const message = generateMockMessage(true);
Expand Down Expand Up @@ -957,7 +911,7 @@ describe('ParseLiveQueryServer', function () {
}, jasmine.ASYNC_TEST_WAIT_TIME);
});

it('can handle update command with original object', async done => {
it('can handle update command with original object', async (done) => {
jasmine.restoreLibrary('../lib/LiveQuery/Client', 'Client');
const Client = require('../lib/LiveQuery/Client').Client;
const parseLiveQueryServer = new ParseLiveQueryServer({});
Expand Down Expand Up @@ -1007,7 +961,7 @@ describe('ParseLiveQueryServer', function () {
}, jasmine.ASYNC_TEST_WAIT_TIME);
});

it('can handle object create command which matches some subscriptions', async done => {
it('can handle object create command which matches some subscriptions', async (done) => {
const parseLiveQueryServer = new ParseLiveQueryServer({});
// Make mock request message
const message = generateMockMessage();
Expand Down Expand Up @@ -1040,7 +994,7 @@ describe('ParseLiveQueryServer', function () {
}, jasmine.ASYNC_TEST_WAIT_TIME);
});

it('can handle create command with fields', async done => {
it('can handle create command with fields', async (done) => {
jasmine.restoreLibrary('../lib/LiveQuery/Client', 'Client');
const Client = require('../lib/LiveQuery/Client').Client;
const parseLiveQueryServer = new ParseLiveQueryServer({});
Expand Down Expand Up @@ -1546,7 +1500,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 +1525,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 +1556,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 +1587,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 +1618,7 @@ describe('ParseLiveQueryServer', function () {
requestId,
'find'
)
.then(isMatched => {
.then((isMatched) => {
expect(isMatched).toBe(false);
done();
});
Expand Down Expand Up @@ -2001,7 +1955,7 @@ describe('LiveQueryController', () => {
classNames: ['Yolo'],
},
})
.then(parseServer => {
.then((parseServer) => {
saveSpy = spyOn(
parseServer.config.liveQueryController,
'onAfterSave'
Expand All @@ -2019,7 +1973,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 +2053,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();
});
36 changes: 0 additions & 36 deletions src/cloud-code/Parse.Cloud.js
Original file line number Diff line number Diff line change
Expand Up @@ -453,21 +453,6 @@ ParseCloud.afterDeleteFile = function (handler) {
);
};

/**
* Registers a before live query server connect function.
*
* **Available in Cloud Code only.**
*
* ```
* Parse.Cloud.beforeConnect(async (request) => {
* // code here
* })
*```
*
* @method beforeConnect
* @name Parse.Cloud.beforeConnect
* @param {Function} func The function to before connection is made. This function can be async and should take just one parameter, {@link Parse.Cloud.ConnectTriggerRequest}.
*/
ParseCloud.beforeConnect = function (handler) {
triggers.addConnectTrigger(
triggers.Types.beforeConnect,
Expand All @@ -476,27 +461,6 @@ ParseCloud.beforeConnect = function (handler) {
);
};

/**
* Registers a before live query subscription function.
*
* **Available in Cloud Code only.**
*
* If you want to use beforeSubscribe for a predefined class in the Parse JavaScript SDK (e.g. {@link Parse.User}), you should pass the class itself and not the String for arg1.
* ```
* Parse.Cloud.beforeSubscribe('MyCustomClass', (request) => {
* // code here
* })
*
* Parse.Cloud.beforeSubscribe(Parse.User, (request) => {
* // code here
* })
*```
*
* @method beforeSubscribe
* @name Parse.Cloud.beforeSubscribe
* @param {(String|Parse.Object)} arg1 The Parse.Object subclass to register the before subscription function for. This can instead be a String that is the className of the subclass.
* @param {Function} func The function to run before a subscription. This function can be async and should take one parameter, a {@link Parse.Cloud.TriggerRequest}.
*/
ParseCloud.beforeSubscribe = function (parseClass, handler) {
var className = getClassName(parseClass);
triggers.addTrigger(
Expand Down
Loading

0 comments on commit eedcd35

Please sign in to comment.