|
1 | 1 | "use strict" |
2 | 2 | const Parse = require("parse/node"); |
| 3 | +const request = require('request'); |
| 4 | +const InMemoryCacheAdapter = require('../src/Adapters/Cache/InMemoryCacheAdapter').InMemoryCacheAdapter; |
3 | 5 |
|
4 | 6 | describe('Cloud Code', () => { |
5 | 7 | it('can load absolute cloud code file', done => { |
@@ -467,4 +469,92 @@ describe('Cloud Code', () => { |
467 | 469 | done(); |
468 | 470 | }); |
469 | 471 | }); |
| 472 | + |
| 473 | + it('doesnt receive stale user in cloud code functions after user has been updated with master key (regression test for #1836)', done => { |
| 474 | + Parse.Cloud.define('testQuery', function(request, response) { |
| 475 | + response.success(request.user.get('data')); |
| 476 | + }); |
| 477 | + |
| 478 | + Parse.User.signUp('user', 'pass') |
| 479 | + .then(user => { |
| 480 | + user.set('data', 'AAA'); |
| 481 | + return user.save(); |
| 482 | + }) |
| 483 | + .then(() => Parse.Cloud.run('testQuery')) |
| 484 | + .then(result => { |
| 485 | + expect(result).toEqual('AAA'); |
| 486 | + Parse.User.current().set('data', 'BBB'); |
| 487 | + return Parse.User.current().save(null, {useMasterKey: true}); |
| 488 | + }) |
| 489 | + .then(() => Parse.Cloud.run('testQuery')) |
| 490 | + .then(result => { |
| 491 | + expect(result).toEqual('BBB'); |
| 492 | + done(); |
| 493 | + }); |
| 494 | + }); |
| 495 | + |
| 496 | + it('clears out the user cache for all sessions when the user is changed', done => { |
| 497 | + const cacheAdapter = new InMemoryCacheAdapter({ ttl: 100000000 }); |
| 498 | + setServerConfiguration(Object.assign({}, defaultConfiguration, { cacheAdapter: cacheAdapter })); |
| 499 | + Parse.Cloud.define('checkStaleUser', (request, response) => { |
| 500 | + response.success(request.user.get('data')); |
| 501 | + }); |
| 502 | + |
| 503 | + let user = new Parse.User(); |
| 504 | + user.set('username', 'test'); |
| 505 | + user.set('password', 'moon-y'); |
| 506 | + user.set('data', 'first data'); |
| 507 | + user.signUp() |
| 508 | + .then(user => { |
| 509 | + let session1 = user.getSessionToken(); |
| 510 | + request.get({ |
| 511 | + url: 'http://localhost:8378/1/login?username=test&password=moon-y', |
| 512 | + json: true, |
| 513 | + headers: { |
| 514 | + 'X-Parse-Application-Id': 'test', |
| 515 | + 'X-Parse-REST-API-Key': 'rest', |
| 516 | + }, |
| 517 | + }, (error, response, body) => { |
| 518 | + let session2 = body.sessionToken; |
| 519 | + |
| 520 | + //Ensure both session tokens are in the cache |
| 521 | + Parse.Cloud.run('checkStaleUser') |
| 522 | + .then(() => { |
| 523 | + request.post({ |
| 524 | + url: 'http://localhost:8378/1/functions/checkStaleUser', |
| 525 | + json: true, |
| 526 | + headers: { |
| 527 | + 'X-Parse-Application-Id': 'test', |
| 528 | + 'X-Parse-REST-API-Key': 'rest', |
| 529 | + 'X-Parse-Session-Token': session2, |
| 530 | + } |
| 531 | + }, (error, response, body) => { |
| 532 | + Parse.Promise.all([cacheAdapter.get('test:user:' + session1), cacheAdapter.get('test:user:' + session2)]) |
| 533 | + .then(cachedVals => { |
| 534 | + expect(cachedVals[0].objectId).toEqual(user.id); |
| 535 | + expect(cachedVals[1].objectId).toEqual(user.id); |
| 536 | + |
| 537 | + //Change with session 1 and then read with session 2. |
| 538 | + user.set('data', 'second data'); |
| 539 | + user.save() |
| 540 | + .then(() => { |
| 541 | + request.post({ |
| 542 | + url: 'http://localhost:8378/1/functions/checkStaleUser', |
| 543 | + json: true, |
| 544 | + headers: { |
| 545 | + 'X-Parse-Application-Id': 'test', |
| 546 | + 'X-Parse-REST-API-Key': 'rest', |
| 547 | + 'X-Parse-Session-Token': session2, |
| 548 | + } |
| 549 | + }, (error, response, body) => { |
| 550 | + expect(body.result).toEqual('second data'); |
| 551 | + done(); |
| 552 | + }) |
| 553 | + }); |
| 554 | + }); |
| 555 | + }); |
| 556 | + }); |
| 557 | + }); |
| 558 | + }); |
| 559 | + }); |
470 | 560 | }); |
0 commit comments