|
1 | 1 | "use strict";
|
2 | 2 |
|
3 | 3 | const request = require('request');
|
| 4 | +const requestp = require('request-promise'); |
4 | 5 | const Config = require('../src/Config');
|
5 | 6 |
|
6 | 7 | describe("Email Verification Token Expiration: ", () => {
|
@@ -482,6 +483,257 @@ describe("Email Verification Token Expiration: ", () => {
|
482 | 483 | });
|
483 | 484 | });
|
484 | 485 |
|
| 486 | + it('should send a new verification email when a resend is requested and the user is UNVERIFIED', done => { |
| 487 | + var user = new Parse.User(); |
| 488 | + var sendEmailOptions; |
| 489 | + var sendVerificationEmailCallCount = 0; |
| 490 | + var emailAdapter = { |
| 491 | + sendVerificationEmail: options => { |
| 492 | + sendEmailOptions = options; |
| 493 | + sendVerificationEmailCallCount++; |
| 494 | + }, |
| 495 | + sendPasswordResetEmail: () => Promise.resolve(), |
| 496 | + sendMail: () => {} |
| 497 | + } |
| 498 | + reconfigureServer({ |
| 499 | + appName: 'emailVerifyToken', |
| 500 | + verifyUserEmails: true, |
| 501 | + emailAdapter: emailAdapter, |
| 502 | + emailVerifyTokenValidityDuration: 5, // 5 seconds |
| 503 | + publicServerURL: 'http://localhost:8378/1' |
| 504 | + }) |
| 505 | + .then(() => { |
| 506 | + user.setUsername('resends_verification_token'); |
| 507 | + user.setPassword('expiringToken'); |
| 508 | + user.set('email', '[email protected]'); |
| 509 | + return user.signUp(); |
| 510 | + }) |
| 511 | + .then(() => { |
| 512 | + expect(sendVerificationEmailCallCount).toBe(1); |
| 513 | + |
| 514 | + return requestp.post({ |
| 515 | + uri: 'http://localhost:8378/1/verificationEmailRequest', |
| 516 | + body: { |
| 517 | + |
| 518 | + }, |
| 519 | + headers: { |
| 520 | + 'X-Parse-Application-Id': Parse.applicationId, |
| 521 | + 'X-Parse-REST-API-Key': 'rest', |
| 522 | + }, |
| 523 | + json: true, |
| 524 | + resolveWithFullResponse: true, |
| 525 | + simple: false // this promise is only rejected if the call itself failed |
| 526 | + }) |
| 527 | + .then((response) => { |
| 528 | + expect(response.statusCode).toBe(200); |
| 529 | + expect(sendVerificationEmailCallCount).toBe(2); |
| 530 | + expect(sendEmailOptions).toBeDefined(); |
| 531 | + done(); |
| 532 | + }); |
| 533 | + }) |
| 534 | + .catch(error => { |
| 535 | + jfail(error); |
| 536 | + done(); |
| 537 | + }); |
| 538 | + }); |
| 539 | + |
| 540 | + it('should not send a new verification email when a resend is requested and the user is VERIFIED', done => { |
| 541 | + var user = new Parse.User(); |
| 542 | + var sendEmailOptions; |
| 543 | + var sendVerificationEmailCallCount = 0; |
| 544 | + var emailAdapter = { |
| 545 | + sendVerificationEmail: options => { |
| 546 | + sendEmailOptions = options; |
| 547 | + sendVerificationEmailCallCount++; |
| 548 | + }, |
| 549 | + sendPasswordResetEmail: () => Promise.resolve(), |
| 550 | + sendMail: () => {} |
| 551 | + } |
| 552 | + reconfigureServer({ |
| 553 | + appName: 'emailVerifyToken', |
| 554 | + verifyUserEmails: true, |
| 555 | + emailAdapter: emailAdapter, |
| 556 | + emailVerifyTokenValidityDuration: 5, // 5 seconds |
| 557 | + publicServerURL: 'http://localhost:8378/1' |
| 558 | + }) |
| 559 | + .then(() => { |
| 560 | + user.setUsername('no_new_verification_token_once_verified'); |
| 561 | + user.setPassword('expiringToken'); |
| 562 | + user.set('email', '[email protected]'); |
| 563 | + return user.signUp(); |
| 564 | + }) |
| 565 | + .then(() => { |
| 566 | + return requestp.get({ |
| 567 | + url: sendEmailOptions.link, |
| 568 | + followRedirect: false, |
| 569 | + resolveWithFullResponse: true, |
| 570 | + simple: false |
| 571 | + }) |
| 572 | + .then((response) => { |
| 573 | + expect(response.statusCode).toEqual(302); |
| 574 | + }); |
| 575 | + }) |
| 576 | + .then(() => { |
| 577 | + expect(sendVerificationEmailCallCount).toBe(1); |
| 578 | + |
| 579 | + return requestp.post({ |
| 580 | + uri: 'http://localhost:8378/1/verificationEmailRequest', |
| 581 | + body: { |
| 582 | + |
| 583 | + }, |
| 584 | + headers: { |
| 585 | + 'X-Parse-Application-Id': Parse.applicationId, |
| 586 | + 'X-Parse-REST-API-Key': 'rest', |
| 587 | + }, |
| 588 | + json: true, |
| 589 | + resolveWithFullResponse: true, |
| 590 | + simple: false // this promise is only rejected if the call itself failed |
| 591 | + }) |
| 592 | + .then((response) => { |
| 593 | + expect(response.statusCode).toBe(400); |
| 594 | + expect(sendVerificationEmailCallCount).toBe(1); |
| 595 | + done(); |
| 596 | + }); |
| 597 | + }) |
| 598 | + .catch(error => { |
| 599 | + jfail(error); |
| 600 | + done(); |
| 601 | + }); |
| 602 | + }); |
| 603 | + |
| 604 | + it('should not send a new verification email if this user does not exist', done => { |
| 605 | + var sendEmailOptions; |
| 606 | + var sendVerificationEmailCallCount = 0; |
| 607 | + var emailAdapter = { |
| 608 | + sendVerificationEmail: options => { |
| 609 | + sendEmailOptions = options; |
| 610 | + sendVerificationEmailCallCount++; |
| 611 | + }, |
| 612 | + sendPasswordResetEmail: () => Promise.resolve(), |
| 613 | + sendMail: () => {} |
| 614 | + } |
| 615 | + reconfigureServer({ |
| 616 | + appName: 'emailVerifyToken', |
| 617 | + verifyUserEmails: true, |
| 618 | + emailAdapter: emailAdapter, |
| 619 | + emailVerifyTokenValidityDuration: 5, // 5 seconds |
| 620 | + publicServerURL: 'http://localhost:8378/1' |
| 621 | + }) |
| 622 | + .then(() => { |
| 623 | + return requestp.post({ |
| 624 | + uri: 'http://localhost:8378/1/verificationEmailRequest', |
| 625 | + body: { |
| 626 | + |
| 627 | + }, |
| 628 | + headers: { |
| 629 | + 'X-Parse-Application-Id': Parse.applicationId, |
| 630 | + 'X-Parse-REST-API-Key': 'rest', |
| 631 | + }, |
| 632 | + json: true, |
| 633 | + resolveWithFullResponse: true, |
| 634 | + simple: false |
| 635 | + }) |
| 636 | + .then(response => { |
| 637 | + expect(response.statusCode).toBe(400); |
| 638 | + expect(sendVerificationEmailCallCount).toBe(0); |
| 639 | + expect(sendEmailOptions).not.toBeDefined(); |
| 640 | + done(); |
| 641 | + }); |
| 642 | + }) |
| 643 | + .catch(error => { |
| 644 | + jfail(error); |
| 645 | + done(); |
| 646 | + }); |
| 647 | + }); |
| 648 | + |
| 649 | + it('should fail if no email is supplied', done => { |
| 650 | + var sendEmailOptions; |
| 651 | + var sendVerificationEmailCallCount = 0; |
| 652 | + var emailAdapter = { |
| 653 | + sendVerificationEmail: options => { |
| 654 | + sendEmailOptions = options; |
| 655 | + sendVerificationEmailCallCount++; |
| 656 | + }, |
| 657 | + sendPasswordResetEmail: () => Promise.resolve(), |
| 658 | + sendMail: () => {} |
| 659 | + } |
| 660 | + reconfigureServer({ |
| 661 | + appName: 'emailVerifyToken', |
| 662 | + verifyUserEmails: true, |
| 663 | + emailAdapter: emailAdapter, |
| 664 | + emailVerifyTokenValidityDuration: 5, // 5 seconds |
| 665 | + publicServerURL: 'http://localhost:8378/1' |
| 666 | + }) |
| 667 | + .then(() => { |
| 668 | + request.post({ |
| 669 | + uri: 'http://localhost:8378/1/verificationEmailRequest', |
| 670 | + body: {}, |
| 671 | + headers: { |
| 672 | + 'X-Parse-Application-Id': Parse.applicationId, |
| 673 | + 'X-Parse-REST-API-Key': 'rest', |
| 674 | + }, |
| 675 | + json: true, |
| 676 | + resolveWithFullResponse: true, |
| 677 | + simple: false |
| 678 | + }, (err, response) => { |
| 679 | + expect(response.statusCode).toBe(400); |
| 680 | + expect(response.body.code).toBe(Parse.Error.EMAIL_MISSING); |
| 681 | + expect(response.body.error).toBe('you must provide an email'); |
| 682 | + expect(sendVerificationEmailCallCount).toBe(0); |
| 683 | + expect(sendEmailOptions).not.toBeDefined(); |
| 684 | + done(); |
| 685 | + }); |
| 686 | + }) |
| 687 | + .catch(error => { |
| 688 | + jfail(error); |
| 689 | + done(); |
| 690 | + }); |
| 691 | + }); |
| 692 | + |
| 693 | + it('should fail if email is not a string', done => { |
| 694 | + var sendEmailOptions; |
| 695 | + var sendVerificationEmailCallCount = 0; |
| 696 | + var emailAdapter = { |
| 697 | + sendVerificationEmail: options => { |
| 698 | + sendEmailOptions = options; |
| 699 | + sendVerificationEmailCallCount++; |
| 700 | + }, |
| 701 | + sendPasswordResetEmail: () => Promise.resolve(), |
| 702 | + sendMail: () => {} |
| 703 | + } |
| 704 | + reconfigureServer({ |
| 705 | + appName: 'emailVerifyToken', |
| 706 | + verifyUserEmails: true, |
| 707 | + emailAdapter: emailAdapter, |
| 708 | + emailVerifyTokenValidityDuration: 5, // 5 seconds |
| 709 | + publicServerURL: 'http://localhost:8378/1' |
| 710 | + }) |
| 711 | + .then(() => { |
| 712 | + request.post({ |
| 713 | + uri: 'http://localhost:8378/1/verificationEmailRequest', |
| 714 | + body: {email: 3}, |
| 715 | + headers: { |
| 716 | + 'X-Parse-Application-Id': Parse.applicationId, |
| 717 | + 'X-Parse-REST-API-Key': 'rest', |
| 718 | + }, |
| 719 | + json: true, |
| 720 | + resolveWithFullResponse: true, |
| 721 | + simple: false |
| 722 | + }, (err, response) => { |
| 723 | + expect(response.statusCode).toBe(400); |
| 724 | + expect(response.body.code).toBe(Parse.Error.INVALID_EMAIL_ADDRESS); |
| 725 | + expect(response.body.error).toBe('you must provide a valid email string'); |
| 726 | + expect(sendVerificationEmailCallCount).toBe(0); |
| 727 | + expect(sendEmailOptions).not.toBeDefined(); |
| 728 | + done(); |
| 729 | + }); |
| 730 | + }) |
| 731 | + .catch(error => { |
| 732 | + jfail(error); |
| 733 | + done(); |
| 734 | + }); |
| 735 | + }); |
| 736 | + |
485 | 737 | it('client should not see the _email_verify_token_expires_at field', done => {
|
486 | 738 | var user = new Parse.User();
|
487 | 739 | var sendEmailOptions;
|
|
0 commit comments