|
1 | 1 | "use strict"
|
2 | 2 | const Parse = require("parse/node");
|
3 | 3 | const request = require('request');
|
| 4 | +const rp = require('request-promise'); |
4 | 5 | const InMemoryCacheAdapter = require('../src/Adapters/Cache/InMemoryCacheAdapter').InMemoryCacheAdapter;
|
5 | 6 |
|
6 | 7 | describe('Cloud Code', () => {
|
7 | 8 | it('can load absolute cloud code file', done => {
|
8 |
| - setServerConfiguration({ |
9 |
| - serverURL: 'http://localhost:8378/1', |
10 |
| - appId: 'test', |
11 |
| - masterKey: 'test', |
12 |
| - cloud: __dirname + '/cloud/cloudCodeRelativeFile.js' |
13 |
| - }); |
14 |
| - Parse.Cloud.run('cloudCodeInFile', {}, result => { |
15 |
| - expect(result).toEqual('It is possible to define cloud code in a file.'); |
16 |
| - done(); |
17 |
| - }); |
| 9 | + reconfigureServer({ cloud: __dirname + '/cloud/cloudCodeRelativeFile.js' }) |
| 10 | + .then(() => { |
| 11 | + Parse.Cloud.run('cloudCodeInFile', {}, result => { |
| 12 | + expect(result).toEqual('It is possible to define cloud code in a file.'); |
| 13 | + done(); |
| 14 | + }); |
| 15 | + }) |
18 | 16 | });
|
19 | 17 |
|
20 | 18 | it('can load relative cloud code file', done => {
|
21 |
| - setServerConfiguration({ |
22 |
| - serverURL: 'http://localhost:8378/1', |
23 |
| - appId: 'test', |
24 |
| - masterKey: 'test', |
25 |
| - cloud: './spec/cloud/cloudCodeAbsoluteFile.js' |
26 |
| - }); |
27 |
| - Parse.Cloud.run('cloudCodeInFile', {}, result => { |
28 |
| - expect(result).toEqual('It is possible to define cloud code in a file.'); |
29 |
| - done(); |
30 |
| - }); |
| 19 | + reconfigureServer({ cloud: './spec/cloud/cloudCodeAbsoluteFile.js' }) |
| 20 | + .then(() => { |
| 21 | + Parse.Cloud.run('cloudCodeInFile', {}, result => { |
| 22 | + expect(result).toEqual('It is possible to define cloud code in a file.'); |
| 23 | + done(); |
| 24 | + }); |
| 25 | + }) |
31 | 26 | });
|
32 | 27 |
|
33 | 28 | it('can create functions', done => {
|
@@ -568,67 +563,75 @@ describe('Cloud Code', () => {
|
568 | 563 | });
|
569 | 564 |
|
570 | 565 | it('clears out the user cache for all sessions when the user is changed', done => {
|
| 566 | + let session1; |
| 567 | + let session2; |
| 568 | + let user; |
571 | 569 | const cacheAdapter = new InMemoryCacheAdapter({ ttl: 100000000 });
|
572 |
| - setServerConfiguration(Object.assign({}, defaultConfiguration, { cacheAdapter: cacheAdapter })); |
573 |
| - Parse.Cloud.define('checkStaleUser', (request, response) => { |
574 |
| - response.success(request.user.get('data')); |
575 |
| - }); |
| 570 | + reconfigureServer({ cacheAdapter }) |
| 571 | + .then(() => { |
| 572 | + Parse.Cloud.define('checkStaleUser', (request, response) => { |
| 573 | + response.success(request.user.get('data')); |
| 574 | + }); |
576 | 575 |
|
577 |
| - let user = new Parse.User(); |
578 |
| - user.set('username', 'test'); |
579 |
| - user.set('password', 'moon-y'); |
580 |
| - user.set('data', 'first data'); |
581 |
| - user.signUp() |
| 576 | + user = new Parse.User(); |
| 577 | + user.set('username', 'test'); |
| 578 | + user.set('password', 'moon-y'); |
| 579 | + user.set('data', 'first data'); |
| 580 | + return user.signUp(); |
| 581 | + }) |
582 | 582 | .then(user => {
|
583 |
| - let session1 = user.getSessionToken(); |
584 |
| - request.get({ |
585 |
| - url: 'http://localhost:8378/1/login?username=test&password=moon-y', |
| 583 | + session1 = user.getSessionToken(); |
| 584 | + return rp({ |
| 585 | + uri: 'http://localhost:8378/1/login?username=test&password=moon-y', |
586 | 586 | json: true,
|
587 | 587 | headers: {
|
588 | 588 | 'X-Parse-Application-Id': 'test',
|
589 | 589 | 'X-Parse-REST-API-Key': 'rest',
|
590 | 590 | },
|
591 |
| - }, (error, response, body) => { |
592 |
| - let session2 = body.sessionToken; |
593 |
| - |
594 |
| - //Ensure both session tokens are in the cache |
595 |
| - Parse.Cloud.run('checkStaleUser') |
596 |
| - .then(() => { |
597 |
| - request.post({ |
598 |
| - url: 'http://localhost:8378/1/functions/checkStaleUser', |
599 |
| - json: true, |
600 |
| - headers: { |
601 |
| - 'X-Parse-Application-Id': 'test', |
602 |
| - 'X-Parse-REST-API-Key': 'rest', |
603 |
| - 'X-Parse-Session-Token': session2, |
604 |
| - } |
605 |
| - }, (error, response, body) => { |
606 |
| - Parse.Promise.all([cacheAdapter.get('test:user:' + session1), cacheAdapter.get('test:user:' + session2)]) |
607 |
| - .then(cachedVals => { |
608 |
| - expect(cachedVals[0].objectId).toEqual(user.id); |
609 |
| - expect(cachedVals[1].objectId).toEqual(user.id); |
610 |
| - |
611 |
| - //Change with session 1 and then read with session 2. |
612 |
| - user.set('data', 'second data'); |
613 |
| - user.save() |
614 |
| - .then(() => { |
615 |
| - request.post({ |
616 |
| - url: 'http://localhost:8378/1/functions/checkStaleUser', |
617 |
| - json: true, |
618 |
| - headers: { |
619 |
| - 'X-Parse-Application-Id': 'test', |
620 |
| - 'X-Parse-REST-API-Key': 'rest', |
621 |
| - 'X-Parse-Session-Token': session2, |
622 |
| - } |
623 |
| - }, (error, response, body) => { |
624 |
| - expect(body.result).toEqual('second data'); |
625 |
| - done(); |
626 |
| - }) |
627 |
| - }); |
628 |
| - }); |
629 |
| - }); |
630 |
| - }); |
631 |
| - }); |
| 591 | + }) |
| 592 | + }) |
| 593 | + .then(body => { |
| 594 | + session2 = body.sessionToken; |
| 595 | + |
| 596 | + //Ensure both session tokens are in the cache |
| 597 | + return Parse.Cloud.run('checkStaleUser') |
| 598 | + }) |
| 599 | + .then(() => rp({ |
| 600 | + method: 'POST', |
| 601 | + uri: 'http://localhost:8378/1/functions/checkStaleUser', |
| 602 | + json: true, |
| 603 | + headers: { |
| 604 | + 'X-Parse-Application-Id': 'test', |
| 605 | + 'X-Parse-REST-API-Key': 'rest', |
| 606 | + 'X-Parse-Session-Token': session2, |
| 607 | + } |
| 608 | + })) |
| 609 | + .then(() => Parse.Promise.all([cacheAdapter.get('test:user:' + session1), cacheAdapter.get('test:user:' + session2)])) |
| 610 | + .then(cachedVals => { |
| 611 | + expect(cachedVals[0].objectId).toEqual(user.id); |
| 612 | + expect(cachedVals[1].objectId).toEqual(user.id); |
| 613 | + |
| 614 | + //Change with session 1 and then read with session 2. |
| 615 | + user.set('data', 'second data'); |
| 616 | + return user.save() |
| 617 | + }) |
| 618 | + .then(() => rp({ |
| 619 | + method: 'POST', |
| 620 | + uri: 'http://localhost:8378/1/functions/checkStaleUser', |
| 621 | + json: true, |
| 622 | + headers: { |
| 623 | + 'X-Parse-Application-Id': 'test', |
| 624 | + 'X-Parse-REST-API-Key': 'rest', |
| 625 | + 'X-Parse-Session-Token': session2, |
| 626 | + } |
| 627 | + })) |
| 628 | + .then(body => { |
| 629 | + expect(body.result).toEqual('second data'); |
| 630 | + done(); |
| 631 | + }) |
| 632 | + .catch(error => { |
| 633 | + fail(JSON.stringify(error)); |
| 634 | + done(); |
632 | 635 | });
|
633 | 636 | });
|
634 | 637 |
|
|
0 commit comments