|
| 1 | +const Config = require('../lib/Config'); |
1 | 2 | const DatabaseController = require('../lib/Controllers/DatabaseController.js');
|
2 | 3 | const validateQuery = DatabaseController._validateQuery;
|
3 | 4 |
|
@@ -361,6 +362,259 @@ describe('DatabaseController', function () {
|
361 | 362 | done();
|
362 | 363 | });
|
363 | 364 | });
|
| 365 | + |
| 366 | + describe('enableCollationCaseComparison', () => { |
| 367 | + const dummyStorageAdapter = { |
| 368 | + find: () => Promise.resolve([]), |
| 369 | + watch: () => Promise.resolve(), |
| 370 | + getAllClasses: () => Promise.resolve([]), |
| 371 | + }; |
| 372 | + |
| 373 | + beforeEach(() => { |
| 374 | + Config.get(Parse.applicationId).schemaCache.clear(); |
| 375 | + }); |
| 376 | + |
| 377 | + it('should force caseInsensitive to false with enableCollationCaseComparison option', async () => { |
| 378 | + const databaseController = new DatabaseController(dummyStorageAdapter, { |
| 379 | + enableCollationCaseComparison: true, |
| 380 | + }); |
| 381 | + const spy = spyOn(dummyStorageAdapter, 'find'); |
| 382 | + spy.and.callThrough(); |
| 383 | + await databaseController.find('SomeClass', {}, { caseInsensitive: true }); |
| 384 | + expect(spy.calls.all()[0].args[3].caseInsensitive).toEqual(false); |
| 385 | + }); |
| 386 | + |
| 387 | + it('should support caseInsensitive without enableCollationCaseComparison option', async () => { |
| 388 | + const databaseController = new DatabaseController(dummyStorageAdapter, {}); |
| 389 | + const spy = spyOn(dummyStorageAdapter, 'find'); |
| 390 | + spy.and.callThrough(); |
| 391 | + await databaseController.find('_User', {}, { caseInsensitive: true }); |
| 392 | + expect(spy.calls.all()[0].args[3].caseInsensitive).toEqual(true); |
| 393 | + }); |
| 394 | + |
| 395 | + it_only_db('mongo')( |
| 396 | + 'should create insensitive indexes without enableCollationCaseComparison', |
| 397 | + async () => { |
| 398 | + await reconfigureServer({ |
| 399 | + databaseURI: 'mongodb://localhost:27017/enableCollationCaseComparisonFalse', |
| 400 | + databaseAdapter: undefined, |
| 401 | + }); |
| 402 | + const user = new Parse.User(); |
| 403 | + await user.save({ |
| 404 | + username: 'example', |
| 405 | + password: 'password', |
| 406 | + |
| 407 | + }); |
| 408 | + const schemas = await Parse.Schema.all(); |
| 409 | + const UserSchema = schemas.find(({ className }) => className === '_User'); |
| 410 | + expect(UserSchema.indexes).toEqual({ |
| 411 | + _id_: { _id: 1 }, |
| 412 | + username_1: { username: 1 }, |
| 413 | + case_insensitive_username: { username: 1 }, |
| 414 | + case_insensitive_email: { email: 1 }, |
| 415 | + email_1: { email: 1 }, |
| 416 | + }); |
| 417 | + } |
| 418 | + ); |
| 419 | + |
| 420 | + it_only_db('mongo')( |
| 421 | + 'should not create insensitive indexes with enableCollationCaseComparison', |
| 422 | + async () => { |
| 423 | + await reconfigureServer({ |
| 424 | + enableCollationCaseComparison: true, |
| 425 | + databaseURI: 'mongodb://localhost:27017/enableCollationCaseComparisonTrue', |
| 426 | + databaseAdapter: undefined, |
| 427 | + }); |
| 428 | + const user = new Parse.User(); |
| 429 | + await user.save({ |
| 430 | + username: 'example', |
| 431 | + password: 'password', |
| 432 | + |
| 433 | + }); |
| 434 | + const schemas = await Parse.Schema.all(); |
| 435 | + const UserSchema = schemas.find(({ className }) => className === '_User'); |
| 436 | + expect(UserSchema.indexes).toEqual({ |
| 437 | + _id_: { _id: 1 }, |
| 438 | + username_1: { username: 1 }, |
| 439 | + email_1: { email: 1 }, |
| 440 | + }); |
| 441 | + } |
| 442 | + ); |
| 443 | + }); |
| 444 | + |
| 445 | + describe('convertEmailToLowercase', () => { |
| 446 | + const dummyStorageAdapter = { |
| 447 | + createObject: () => Promise.resolve({ ops: [{}] }), |
| 448 | + findOneAndUpdate: () => Promise.resolve({}), |
| 449 | + watch: () => Promise.resolve(), |
| 450 | + getAllClasses: () => |
| 451 | + Promise.resolve([ |
| 452 | + { |
| 453 | + className: '_User', |
| 454 | + fields: { email: 'String' }, |
| 455 | + indexes: {}, |
| 456 | + classLevelPermissions: { protectedFields: {} }, |
| 457 | + }, |
| 458 | + ]), |
| 459 | + }; |
| 460 | + const dates = { |
| 461 | + createdAt: { iso: undefined, __type: 'Date' }, |
| 462 | + updatedAt: { iso: undefined, __type: 'Date' }, |
| 463 | + }; |
| 464 | + |
| 465 | + it('should not transform email to lower case without convertEmailToLowercase option on create', async () => { |
| 466 | + const databaseController = new DatabaseController(dummyStorageAdapter, {}); |
| 467 | + const spy = spyOn(dummyStorageAdapter, 'createObject'); |
| 468 | + spy.and.callThrough(); |
| 469 | + await databaseController.create('_User', { |
| 470 | + |
| 471 | + }); |
| 472 | + expect(spy.calls.all()[0].args[2]).toEqual({ |
| 473 | + |
| 474 | + ...dates, |
| 475 | + }); |
| 476 | + }); |
| 477 | + |
| 478 | + it('should transform email to lower case with convertEmailToLowercase option on create', async () => { |
| 479 | + const databaseController = new DatabaseController(dummyStorageAdapter, { |
| 480 | + convertEmailToLowercase: true, |
| 481 | + }); |
| 482 | + const spy = spyOn(dummyStorageAdapter, 'createObject'); |
| 483 | + spy.and.callThrough(); |
| 484 | + await databaseController.create('_User', { |
| 485 | + |
| 486 | + }); |
| 487 | + expect(spy.calls.all()[0].args[2]).toEqual({ |
| 488 | + |
| 489 | + ...dates, |
| 490 | + }); |
| 491 | + }); |
| 492 | + |
| 493 | + it('should not transform email to lower case without convertEmailToLowercase option on update', async () => { |
| 494 | + const databaseController = new DatabaseController(dummyStorageAdapter, {}); |
| 495 | + const spy = spyOn(dummyStorageAdapter, 'findOneAndUpdate'); |
| 496 | + spy.and.callThrough(); |
| 497 | + await databaseController.update('_User', { id: 'example' }, { email: '[email protected]' }); |
| 498 | + expect(spy.calls.all()[0].args[3]).toEqual({ |
| 499 | + |
| 500 | + }); |
| 501 | + }); |
| 502 | + |
| 503 | + it('should transform email to lower case with convertEmailToLowercase option on update', async () => { |
| 504 | + const databaseController = new DatabaseController(dummyStorageAdapter, { |
| 505 | + convertEmailToLowercase: true, |
| 506 | + }); |
| 507 | + const spy = spyOn(dummyStorageAdapter, 'findOneAndUpdate'); |
| 508 | + spy.and.callThrough(); |
| 509 | + await databaseController.update('_User', { id: 'example' }, { email: '[email protected]' }); |
| 510 | + expect(spy.calls.all()[0].args[3]).toEqual({ |
| 511 | + |
| 512 | + }); |
| 513 | + }); |
| 514 | + |
| 515 | + it('should not find a case insensitive user by email with convertEmailToLowercase', async () => { |
| 516 | + await reconfigureServer({ convertEmailToLowercase: true }); |
| 517 | + const user = new Parse.User(); |
| 518 | + await user.save({ username: 'EXAMPLE', email: '[email protected]', password: 'password' }); |
| 519 | + |
| 520 | + const query = new Parse.Query(Parse.User); |
| 521 | + query.equalTo('email', '[email protected]'); |
| 522 | + const result = await query.find({ useMasterKey: true }); |
| 523 | + expect(result.length).toEqual(0); |
| 524 | + |
| 525 | + const query2 = new Parse.Query(Parse.User); |
| 526 | + query2.equalTo('email', '[email protected]'); |
| 527 | + const result2 = await query2.find({ useMasterKey: true }); |
| 528 | + expect(result2.length).toEqual(1); |
| 529 | + }); |
| 530 | + }); |
| 531 | + |
| 532 | + describe('convertUsernameToLowercase', () => { |
| 533 | + const dummyStorageAdapter = { |
| 534 | + createObject: () => Promise.resolve({ ops: [{}] }), |
| 535 | + findOneAndUpdate: () => Promise.resolve({}), |
| 536 | + watch: () => Promise.resolve(), |
| 537 | + getAllClasses: () => |
| 538 | + Promise.resolve([ |
| 539 | + { |
| 540 | + className: '_User', |
| 541 | + fields: { username: 'String' }, |
| 542 | + indexes: {}, |
| 543 | + classLevelPermissions: { protectedFields: {} }, |
| 544 | + }, |
| 545 | + ]), |
| 546 | + }; |
| 547 | + const dates = { |
| 548 | + createdAt: { iso: undefined, __type: 'Date' }, |
| 549 | + updatedAt: { iso: undefined, __type: 'Date' }, |
| 550 | + }; |
| 551 | + |
| 552 | + it('should not transform username to lower case without convertUsernameToLowercase option on create', async () => { |
| 553 | + const databaseController = new DatabaseController(dummyStorageAdapter, {}); |
| 554 | + const spy = spyOn(dummyStorageAdapter, 'createObject'); |
| 555 | + spy.and.callThrough(); |
| 556 | + await databaseController.create('_User', { |
| 557 | + username: 'EXAMPLE', |
| 558 | + }); |
| 559 | + expect(spy.calls.all()[0].args[2]).toEqual({ |
| 560 | + username: 'EXAMPLE', |
| 561 | + ...dates, |
| 562 | + }); |
| 563 | + }); |
| 564 | + |
| 565 | + it('should transform username to lower case with convertUsernameToLowercase option on create', async () => { |
| 566 | + const databaseController = new DatabaseController(dummyStorageAdapter, { |
| 567 | + convertUsernameToLowercase: true, |
| 568 | + }); |
| 569 | + const spy = spyOn(dummyStorageAdapter, 'createObject'); |
| 570 | + spy.and.callThrough(); |
| 571 | + await databaseController.create('_User', { |
| 572 | + username: 'EXAMPLE', |
| 573 | + }); |
| 574 | + expect(spy.calls.all()[0].args[2]).toEqual({ |
| 575 | + username: 'example', |
| 576 | + ...dates, |
| 577 | + }); |
| 578 | + }); |
| 579 | + |
| 580 | + it('should not transform username to lower case without convertUsernameToLowercase option on update', async () => { |
| 581 | + const databaseController = new DatabaseController(dummyStorageAdapter, {}); |
| 582 | + const spy = spyOn(dummyStorageAdapter, 'findOneAndUpdate'); |
| 583 | + spy.and.callThrough(); |
| 584 | + await databaseController.update('_User', { id: 'example' }, { username: 'EXAMPLE' }); |
| 585 | + expect(spy.calls.all()[0].args[3]).toEqual({ |
| 586 | + username: 'EXAMPLE', |
| 587 | + }); |
| 588 | + }); |
| 589 | + |
| 590 | + it('should transform username to lower case with convertUsernameToLowercase option on update', async () => { |
| 591 | + const databaseController = new DatabaseController(dummyStorageAdapter, { |
| 592 | + convertUsernameToLowercase: true, |
| 593 | + }); |
| 594 | + const spy = spyOn(dummyStorageAdapter, 'findOneAndUpdate'); |
| 595 | + spy.and.callThrough(); |
| 596 | + await databaseController.update('_User', { id: 'example' }, { username: 'EXAMPLE' }); |
| 597 | + expect(spy.calls.all()[0].args[3]).toEqual({ |
| 598 | + username: 'example', |
| 599 | + }); |
| 600 | + }); |
| 601 | + |
| 602 | + it('should not find a case insensitive user by username with convertUsernameToLowercase', async () => { |
| 603 | + await reconfigureServer({ convertUsernameToLowercase: true }); |
| 604 | + const user = new Parse.User(); |
| 605 | + await user.save({ username: 'EXAMPLE', password: 'password' }); |
| 606 | + |
| 607 | + const query = new Parse.Query(Parse.User); |
| 608 | + query.equalTo('username', 'EXAMPLE'); |
| 609 | + const result = await query.find({ useMasterKey: true }); |
| 610 | + expect(result.length).toEqual(0); |
| 611 | + |
| 612 | + const query2 = new Parse.Query(Parse.User); |
| 613 | + query2.equalTo('username', 'example'); |
| 614 | + const result2 = await query2.find({ useMasterKey: true }); |
| 615 | + expect(result2.length).toEqual(1); |
| 616 | + }); |
| 617 | + }); |
364 | 618 | });
|
365 | 619 |
|
366 | 620 | function buildCLP(pointerNames) {
|
|
0 commit comments