BVB Source Codes

parse-server Show ValidationAndPasswordsReset.spec.js Source code

Return Download parse-server: download ValidationAndPasswordsReset.spec.js Source code - Download parse-server Source code - Type:.js
  1. "use strict";
  2.  
  3. const MockEmailAdapterWithOptions = require('./MockEmailAdapterWithOptions');
  4. const request = require('request');
  5. const Config = require("../src/Config");
  6.  
  7. describe("Custom Pages, Email Verification, Password Reset", () => {
  8.   it("should set the custom pages", (done) => {
  9.     reconfigureServer({
  10.       appName: 'unused',
  11.       customPages: {
  12.         invalidLink: "myInvalidLink",
  13.         verifyEmailSuccess: "myVerifyEmailSuccess",
  14.         choosePassword: "myChoosePassword",
  15.         passwordResetSuccess: "myPasswordResetSuccess",
  16.         parseFrameURL: "http://example.com/handle-parse-iframe"
  17.       },
  18.       publicServerURL: "https://my.public.server.com/1"
  19.     })
  20.     .then(() => {
  21.       var config = new Config("test");
  22.       expect(config.invalidLinkURL).toEqual("myInvalidLink");
  23.       expect(config.verifyEmailSuccessURL).toEqual("myVerifyEmailSuccess");
  24.       expect(config.choosePasswordURL).toEqual("myChoosePassword");
  25.       expect(config.passwordResetSuccessURL).toEqual("myPasswordResetSuccess");
  26.       expect(config.parseFrameURL).toEqual("http://example.com/handle-parse-iframe");
  27.       expect(config.verifyEmailURL).toEqual("https://my.public.server.com/1/apps/test/verify_email");
  28.       expect(config.requestResetPasswordURL).toEqual("https://my.public.server.com/1/apps/test/request_password_reset");
  29.       done();
  30.     });
  31.   });
  32.  
  33.   it('sends verification email if email verification is enabled', done => {
  34.     var emailAdapter = {
  35.       sendVerificationEmail: () => Promise.resolve(),
  36.       sendPasswordResetEmail: () => Promise.resolve(),
  37.       sendMail: () => Promise.resolve()
  38.     }
  39.     reconfigureServer({
  40.       appName: 'unused',
  41.       verifyUserEmails: true,
  42.       emailAdapter: emailAdapter,
  43.       publicServerURL: "http://localhost:8378/1"
  44.     })
  45.     .then(() => {
  46.       spyOn(emailAdapter, 'sendVerificationEmail');
  47.       var user = new Parse.User();
  48.       user.setPassword("asdf");
  49.       user.setUsername("zxcv");
  50.       user.setEmail('testIfEnabled@parse.com');
  51.       user.signUp(null, {
  52.         success: function(user) {
  53.           expect(emailAdapter.sendVerificationEmail).toHaveBeenCalled();
  54.           user.fetch()
  55.           .then(() => {
  56.             expect(user.get('emailVerified')).toEqual(false);
  57.             done();
  58.           });
  59.         },
  60.         error: function() {
  61.           fail('Failed to save user');
  62.           done();
  63.         }
  64.       });
  65.     });
  66.   });
  67.  
  68.   it('does not send verification email when verification is enabled and email is not set', done => {
  69.     var emailAdapter = {
  70.       sendVerificationEmail: () => Promise.resolve(),
  71.       sendPasswordResetEmail: () => Promise.resolve(),
  72.       sendMail: () => Promise.resolve()
  73.     }
  74.     reconfigureServer({
  75.       appName: 'unused',
  76.       verifyUserEmails: true,
  77.       emailAdapter: emailAdapter,
  78.       publicServerURL: "http://localhost:8378/1"
  79.     })
  80.     .then(() => {
  81.       spyOn(emailAdapter, 'sendVerificationEmail');
  82.       var user = new Parse.User();
  83.       user.setPassword("asdf");
  84.       user.setUsername("zxcv");
  85.       user.signUp(null, {
  86.         success: function(user) {
  87.           expect(emailAdapter.sendVerificationEmail).not.toHaveBeenCalled();
  88.           user.fetch()
  89.           .then(() => {
  90.             expect(user.get('emailVerified')).toEqual(undefined);
  91.             done();
  92.           });
  93.         },
  94.         error: function() {
  95.           fail('Failed to save user');
  96.           done();
  97.         }
  98.       });
  99.     });
  100.   });
  101.  
  102.   it('does send a validation email when updating the email', done => {
  103.     var emailAdapter = {
  104.       sendVerificationEmail: () => Promise.resolve(),
  105.       sendPasswordResetEmail: () => Promise.resolve(),
  106.       sendMail: () => Promise.resolve()
  107.     }
  108.     reconfigureServer({
  109.       appName: 'unused',
  110.       verifyUserEmails: true,
  111.       emailAdapter: emailAdapter,
  112.       publicServerURL: "http://localhost:8378/1"
  113.     })
  114.     .then(() => {
  115.       spyOn(emailAdapter, 'sendVerificationEmail');
  116.       var user = new Parse.User();
  117.       user.setPassword("asdf");
  118.       user.setUsername("zxcv");
  119.       user.signUp(null, {
  120.         success: function(user) {
  121.           expect(emailAdapter.sendVerificationEmail).not.toHaveBeenCalled();
  122.           user.fetch()
  123.           .then((user) => {
  124.             user.set("email", "testWhenUpdating@parse.com");
  125.             return user.save();
  126.           }).then((user) => {
  127.             return user.fetch();
  128.           }).then(() => {
  129.             expect(user.get('emailVerified')).toEqual(false);
  130.             // Wait as on update email, we need to fetch the username
  131.             setTimeout(function(){
  132.               expect(emailAdapter.sendVerificationEmail).toHaveBeenCalled();
  133.               done();
  134.             }, 200);
  135.           });
  136.         },
  137.         error: function() {
  138.           fail('Failed to save user');
  139.           done();
  140.         }
  141.       });
  142.     });
  143.   });
  144.  
  145.   it('does send a validation email with valid verification link when updating the email', done => {
  146.     var emailAdapter = {
  147.       sendVerificationEmail: () => Promise.resolve(),
  148.       sendPasswordResetEmail: () => Promise.resolve(),
  149.       sendMail: () => Promise.resolve()
  150.     }
  151.     reconfigureServer({
  152.       appName: 'unused',
  153.       verifyUserEmails: true,
  154.       emailAdapter: emailAdapter,
  155.       publicServerURL: "http://localhost:8378/1"
  156.     })
  157.     .then(() => {
  158.       spyOn(emailAdapter, 'sendVerificationEmail').and.callFake((options) => {
  159.         expect(options.link).not.toBeNull();
  160.         expect(options.link).not.toMatch(/token=undefined/);
  161.         Promise.resolve();
  162.       });
  163.       var user = new Parse.User();
  164.       user.setPassword("asdf");
  165.       user.setUsername("zxcv");
  166.       user.signUp(null, {
  167.         success: function(user) {
  168.           expect(emailAdapter.sendVerificationEmail).not.toHaveBeenCalled();
  169.           user.fetch()
  170.           .then((user) => {
  171.             user.set("email", "testValidLinkWhenUpdating@parse.com");
  172.             return user.save();
  173.           }).then((user) => {
  174.             return user.fetch();
  175.           }).then(() => {
  176.             expect(user.get('emailVerified')).toEqual(false);
  177.             // Wait as on update email, we need to fetch the username
  178.             setTimeout(function(){
  179.               expect(emailAdapter.sendVerificationEmail).toHaveBeenCalled();
  180.               done();
  181.             }, 200);
  182.           });
  183.         },
  184.         error: function() {
  185.           fail('Failed to save user');
  186.           done();
  187.         }
  188.       });
  189.     });
  190.   });
  191.  
  192.   it('does send with a simple adapter', done => {
  193.     var calls = 0;
  194.     var emailAdapter = {
  195.       sendMail: function(options){
  196.         expect(options.to).toBe('testSendSimpleAdapter@parse.com');
  197.         if (calls == 0) {
  198.           expect(options.subject).toEqual('Please verify your e-mail for My Cool App');
  199.           expect(options.text.match(/verify_email/)).not.toBe(null);
  200.         } else if (calls == 1) {
  201.           expect(options.subject).toEqual('Password Reset for My Cool App');
  202.           expect(options.text.match(/request_password_reset/)).not.toBe(null);
  203.         }
  204.         calls++;
  205.         return Promise.resolve();
  206.       }
  207.     }
  208.     reconfigureServer({
  209.       appName: 'My Cool App',
  210.       verifyUserEmails: true,
  211.       emailAdapter: emailAdapter,
  212.       publicServerURL: "http://localhost:8378/1"
  213.     })
  214.     .then(() => {
  215.       var user = new Parse.User();
  216.       user.setPassword("asdf");
  217.       user.setUsername("zxcv");
  218.       user.set("email", "testSendSimpleAdapter@parse.com");
  219.       user.signUp(null, {
  220.         success: function(user) {
  221.           expect(calls).toBe(1);
  222.           user.fetch()
  223.           .then((user) => {
  224.             return user.save();
  225.           }).then(() => {
  226.             return Parse.User.requestPasswordReset("testSendSimpleAdapter@parse.com").catch(() => {
  227.               fail('Should not fail requesting a password');
  228.               done();
  229.             })
  230.           }).then(() => {
  231.             expect(calls).toBe(2);
  232.             done();
  233.           });
  234.         },
  235.         error: function() {
  236.           fail('Failed to save user');
  237.           done();
  238.         }
  239.       });
  240.     });
  241.   });
  242.  
  243.   it('prevents user from login if email is not verified but preventLoginWithUnverifiedEmail is set to true', done => {
  244.     reconfigureServer({
  245.       appName: 'test',
  246.       publicServerURL: 'http://localhost:1337/1',
  247.       verifyUserEmails: true,
  248.       preventLoginWithUnverifiedEmail: true,
  249.       emailAdapter: MockEmailAdapterWithOptions({
  250.         fromAddress: 'parse@example.com',
  251.         apiKey: 'k',
  252.         domain: 'd',
  253.       }),
  254.     })
  255.     .then(() => {
  256.       const user = new Parse.User();
  257.       user.setPassword("asdf");
  258.       user.setUsername("zxcv");
  259.       user.set("email", "testInvalidConfig@parse.com");
  260.       user.signUp(null)
  261.       .then(() => Parse.User.logIn("zxcv", "asdf"))
  262.       .then(() => {
  263.         fail('login should have failed');
  264.         done();
  265.       }, error => {
  266.         expect(error.message).toEqual('User email is not verified.')
  267.         done();
  268.       });
  269.     })
  270.     .catch(error => {
  271.       fail(JSON.stringify(error));
  272.       done();
  273.     });
  274.   });
  275.  
  276.   it('allows user to login only after user clicks on the link to confirm email address if preventLoginWithUnverifiedEmail is set to true', done => {
  277.     var user = new Parse.User();
  278.     var sendEmailOptions;
  279.     var emailAdapter = {
  280.       sendVerificationEmail: options => {
  281.         sendEmailOptions = options;
  282.       },
  283.       sendPasswordResetEmail: () => Promise.resolve(),
  284.       sendMail: () => {}
  285.     }
  286.     reconfigureServer({
  287.       appName: 'emailing app',
  288.       verifyUserEmails: true,
  289.       preventLoginWithUnverifiedEmail: true,
  290.       emailAdapter: emailAdapter,
  291.       publicServerURL: "http://localhost:8378/1"
  292.     })
  293.     .then(() => {
  294.       user.setPassword("other-password");
  295.       user.setUsername("user");
  296.       user.set('email', 'user@parse.com');
  297.       return user.signUp();
  298.     }).then(() => {
  299.       expect(sendEmailOptions).not.toBeUndefined();
  300.       request.get(sendEmailOptions.link, {
  301.         followRedirect: false,
  302.       }, (error, response) => {
  303.         expect(response.statusCode).toEqual(302);
  304.         expect(response.body).toEqual('Found. Redirecting to http://localhost:8378/1/apps/verify_email_success.html?username=user');
  305.         user.fetch()
  306.         .then(() => {
  307.           expect(user.get('emailVerified')).toEqual(true);
  308.  
  309.           Parse.User.logIn("user", "other-password")
  310.           .then(user => {
  311.             expect(typeof user).toBe('object');
  312.             expect(user.get('emailVerified')).toBe(true);
  313.             done();
  314.           }, () => {
  315.             fail('login should have succeeded');
  316.             done();
  317.           });
  318.         }, (err) => {
  319.           jfail(err);
  320.           fail("this should not fail");
  321.           done();
  322.         }).catch((err) => {
  323.           jfail(err);
  324.           done();
  325.         })
  326.       });
  327.     });
  328.   });
  329.  
  330.   it('allows user to login if email is not verified but preventLoginWithUnverifiedEmail is set to false', done => {
  331.     reconfigureServer({
  332.       appName: 'test',
  333.       publicServerURL: 'http://localhost:1337/1',
  334.       verifyUserEmails: true,
  335.       preventLoginWithUnverifiedEmail: false,
  336.       emailAdapter: MockEmailAdapterWithOptions({
  337.         fromAddress: 'parse@example.com',
  338.         apiKey: 'k',
  339.         domain: 'd',
  340.       }),
  341.     })
  342.     .then(() => {
  343.       const user = new Parse.User();
  344.       user.setPassword("asdf");
  345.       user.setUsername("zxcv");
  346.       user.set("email", "testInvalidConfig@parse.com");
  347.       user.signUp(null)
  348.       .then(() => Parse.User.logIn("zxcv", "asdf"))
  349.       .then(user => {
  350.         expect(typeof user).toBe('object');
  351.         expect(user.get('emailVerified')).toBe(false);
  352.         done();
  353.       }, () => {
  354.         fail('login should have succeeded');
  355.         done();
  356.       });
  357.     })
  358.     .catch(error => {
  359.       fail(JSON.stringify(error));
  360.       done();
  361.     });
  362.   });
  363.  
  364.   it('fails if you include an emailAdapter, set a publicServerURL, but have no appName and send a password reset email', done => {
  365.     reconfigureServer({
  366.       appName: undefined,
  367.       publicServerURL: 'http://localhost:1337/1',
  368.       emailAdapter: MockEmailAdapterWithOptions({
  369.         fromAddress: 'parse@example.com',
  370.         apiKey: 'k',
  371.         domain: 'd',
  372.       }),
  373.     })
  374.     .then(() => {
  375.       const user = new Parse.User();
  376.       user.setPassword("asdf");
  377.       user.setUsername("zxcv");
  378.       user.set("email", "testInvalidConfig@parse.com");
  379.       user.signUp(null)
  380.       .then(() => Parse.User.requestPasswordReset("testInvalidConfig@parse.com"))
  381.       .then(result => {
  382.         console.log(result);
  383.         fail('sending password reset email should not have succeeded');
  384.         done();
  385.       }, error => {
  386.         expect(error.message).toEqual('An appName, publicServerURL, and emailAdapter are required for password reset and email verification functionality.')
  387.         done();
  388.       });
  389.     })
  390.     .catch(error => {
  391.       fail(JSON.stringify(error));
  392.       done();
  393.     });
  394.   });
  395.  
  396.   it('fails if you include an emailAdapter, have an appName, but have no publicServerURL and send a password reset email', done => {
  397.     reconfigureServer({
  398.       appName: undefined,
  399.       emailAdapter: MockEmailAdapterWithOptions({
  400.         fromAddress: 'parse@example.com',
  401.         apiKey: 'k',
  402.         domain: 'd',
  403.       }),
  404.     })
  405.     .then(() => {
  406.       const user = new Parse.User();
  407.       user.setPassword("asdf");
  408.       user.setUsername("zxcv");
  409.       user.set("email", "testInvalidConfig@parse.com");
  410.       user.signUp(null)
  411.       .then(() => Parse.User.requestPasswordReset("testInvalidConfig@parse.com"))
  412.       .then(result => {
  413.         console.log(result);
  414.         fail('sending password reset email should not have succeeded');
  415.         done();
  416.       }, error => {
  417.         expect(error.message).toEqual('An appName, publicServerURL, and emailAdapter are required for password reset and email verification functionality.')
  418.         done();
  419.       });
  420.     })
  421.     .catch(error => {
  422.       fail(JSON.stringify(error));
  423.       done();
  424.     });
  425.   });
  426.  
  427.   it('fails if you set a publicServerURL, have an appName, but no emailAdapter and send a password reset email', done => {
  428.     reconfigureServer({
  429.       appName: 'unused',
  430.       publicServerURL: 'http://localhost:1337/1',
  431.       emailAdapter: undefined,
  432.     })
  433.     .then(() => {
  434.       const user = new Parse.User();
  435.       user.setPassword("asdf");
  436.       user.setUsername("zxcv");
  437.       user.set("email", "testInvalidConfig@parse.com");
  438.       user.signUp(null)
  439.       .then(() => Parse.User.requestPasswordReset("testInvalidConfig@parse.com"))
  440.       .then(result => {
  441.         console.log(result);
  442.         fail('sending password reset email should not have succeeded');
  443.         done();
  444.       }, error => {
  445.         expect(error.message).toEqual('An appName, publicServerURL, and emailAdapter are required for password reset and email verification functionality.')
  446.         done();
  447.       });
  448.     })
  449.     .catch(error => {
  450.       fail(JSON.stringify(error));
  451.       done();
  452.     });
  453.   });
  454.  
  455.   it('succeeds sending a password reset email if appName, publicServerURL, and email adapter are prodvided', done => {
  456.     reconfigureServer({
  457.       appName: 'coolapp',
  458.       publicServerURL: 'http://localhost:1337/1',
  459.       emailAdapter: MockEmailAdapterWithOptions({
  460.         fromAddress: 'parse@example.com',
  461.         apiKey: 'k',
  462.         domain: 'd',
  463.       }),
  464.     })
  465.     .then(() => {
  466.       const user = new Parse.User();
  467.       user.setPassword("asdf");
  468.       user.setUsername("zxcv");
  469.       user.set("email", "testInvalidConfig@parse.com");
  470.       user.signUp(null)
  471.       .then(() => Parse.User.requestPasswordReset("testInvalidConfig@parse.com"))
  472.       .then(() => {
  473.         done();
  474.       }, error => {
  475.         done(error);
  476.       });
  477.     })
  478.     .catch(error => {
  479.       fail(JSON.stringify(error));
  480.       done();
  481.     });
  482.   });
  483.  
  484.   it('succeeds sending a password reset username if appName, publicServerURL, and email adapter are prodvided', done => {
  485.     const adapter = MockEmailAdapterWithOptions({
  486.       fromAddress: 'parse@example.com',
  487.       apiKey: 'k',
  488.       domain: 'd',
  489.       sendMail: function(options) {
  490.         expect(options.to).toEqual('testValidConfig@parse.com');
  491.         return Promise.resolve();
  492.       }
  493.     });
  494.  
  495.     // delete that handler to force using the default
  496.     delete adapter.sendPasswordResetEmail;
  497.  
  498.     spyOn(adapter, 'sendMail').and.callThrough();
  499.     reconfigureServer({
  500.       appName: 'coolapp',
  501.       publicServerURL: 'http://localhost:1337/1',
  502.       emailAdapter: adapter
  503.     })
  504.     .then(() => {
  505.       const user = new Parse.User();
  506.       user.setPassword("asdf");
  507.       user.setUsername("testValidConfig@parse.com");
  508.       user.signUp(null)
  509.       .then(() => Parse.User.requestPasswordReset("testValidConfig@parse.com"))
  510.       .then(() => {
  511.         expect(adapter.sendMail).toHaveBeenCalled();
  512.         done();
  513.       }, error => {
  514.         done(error);
  515.       });
  516.     })
  517.     .catch(error => {
  518.       fail(JSON.stringify(error));
  519.       done();
  520.     });
  521.   });
  522.  
  523.   it('does not send verification email if email verification is disabled', done => {
  524.     var emailAdapter = {
  525.       sendVerificationEmail: () => Promise.resolve(),
  526.       sendPasswordResetEmail: () => Promise.resolve(),
  527.       sendMail: () => Promise.resolve()
  528.     }
  529.     reconfigureServer({
  530.       appName: 'unused',
  531.       publicServerURL: 'http://localhost:1337/1',
  532.       verifyUserEmails: false,
  533.       emailAdapter: emailAdapter,
  534.     })
  535.     .then(() => {
  536.       spyOn(emailAdapter, 'sendVerificationEmail');
  537.       var user = new Parse.User();
  538.       user.setPassword("asdf");
  539.       user.setUsername("zxcv");
  540.       user.signUp(null, {
  541.         success: function(user) {
  542.           user.fetch()
  543.           .then(() => {
  544.             expect(emailAdapter.sendVerificationEmail.calls.count()).toEqual(0);
  545.             expect(user.get('emailVerified')).toEqual(undefined);
  546.             done();
  547.           });
  548.         },
  549.         error: function() {
  550.           fail('Failed to save user');
  551.           done();
  552.         }
  553.       });
  554.     });
  555.   });
  556.  
  557.   it('receives the app name and user in the adapter', done => {
  558.     var emailSent = false;
  559.     var emailAdapter = {
  560.       sendVerificationEmail: options => {
  561.         expect(options.appName).toEqual('emailing app');
  562.         expect(options.user.get('email')).toEqual('user@parse.com');
  563.         emailSent = true;
  564.       },
  565.       sendPasswordResetEmail: () => Promise.resolve(),
  566.       sendMail: () => {}
  567.     }
  568.     reconfigureServer({
  569.       appName: 'emailing app',
  570.       verifyUserEmails: true,
  571.       emailAdapter: emailAdapter,
  572.       publicServerURL: "http://localhost:8378/1"
  573.     })
  574.     .then(() => {
  575.       var user = new Parse.User();
  576.       user.setPassword("asdf");
  577.       user.setUsername("zxcv");
  578.       user.set('email', 'user@parse.com');
  579.       user.signUp(null, {
  580.         success: () => {
  581.           expect(emailSent).toBe(true);
  582.           done();
  583.         },
  584.         error: function() {
  585.           fail('Failed to save user');
  586.           done();
  587.         }
  588.       });
  589.     });
  590.   })
  591.  
  592.   it('when you click the link in the email it sets emailVerified to true and redirects you', done => {
  593.     var user = new Parse.User();
  594.     var sendEmailOptions;
  595.     var emailAdapter = {
  596.       sendVerificationEmail: options => {
  597.         sendEmailOptions = options;
  598.       },
  599.       sendPasswordResetEmail: () => Promise.resolve(),
  600.       sendMail: () => {}
  601.     }
  602.     reconfigureServer({
  603.       appName: 'emailing app',
  604.       verifyUserEmails: true,
  605.       emailAdapter: emailAdapter,
  606.       publicServerURL: "http://localhost:8378/1"
  607.     })
  608.     .then(() => {
  609.       user.setPassword("other-password");
  610.       user.setUsername("user");
  611.       user.set('email', 'user@parse.com');
  612.       return user.signUp();
  613.     }).then(() => {
  614.       expect(sendEmailOptions).not.toBeUndefined();
  615.       request.get(sendEmailOptions.link, {
  616.         followRedirect: false,
  617.       }, (error, response) => {
  618.         expect(response.statusCode).toEqual(302);
  619.         expect(response.body).toEqual('Found. Redirecting to http://localhost:8378/1/apps/verify_email_success.html?username=user');
  620.         user.fetch()
  621.         .then(() => {
  622.           expect(user.get('emailVerified')).toEqual(true);
  623.           done();
  624.         }, (err) => {
  625.           jfail(err);
  626.           fail("this should not fail");
  627.           done();
  628.         }).catch((err) => {
  629.           jfail(err);
  630.           done();
  631.         })
  632.       });
  633.     });
  634.   });
  635.  
  636.   it('redirects you to invalid link if you try to verify email incorrecly', done => {
  637.     reconfigureServer({
  638.       appName: 'emailing app',
  639.       verifyUserEmails: true,
  640.       emailAdapter: {
  641.         sendVerificationEmail: () => Promise.resolve(),
  642.         sendPasswordResetEmail: () => Promise.resolve(),
  643.         sendMail: () => {}
  644.       },
  645.       publicServerURL: "http://localhost:8378/1"
  646.     })
  647.     .then(() => {
  648.       request.get('http://localhost:8378/1/apps/test/verify_email', {
  649.         followRedirect: false,
  650.       }, (error, response) => {
  651.         expect(response.statusCode).toEqual(302);
  652.         expect(response.body).toEqual('Found. Redirecting to http://localhost:8378/1/apps/invalid_link.html');
  653.         done()
  654.       });
  655.     });
  656.   });
  657.  
  658.   it('redirects you to invalid verification link page if you try to validate a nonexistant users email', done => {
  659.     reconfigureServer({
  660.       appName: 'emailing app',
  661.       verifyUserEmails: true,
  662.       emailAdapter: {
  663.         sendVerificationEmail: () => Promise.resolve(),
  664.         sendPasswordResetEmail: () => Promise.resolve(),
  665.         sendMail: () => {}
  666.       },
  667.       publicServerURL: "http://localhost:8378/1"
  668.     })
  669.     .then(() => {
  670.       request.get('http://localhost:8378/1/apps/test/verify_email?token=asdfasdf&username=sadfasga', {
  671.         followRedirect: false,
  672.       }, (error, response) => {
  673.         expect(response.statusCode).toEqual(302);
  674.         expect(response.body).toEqual('Found. Redirecting to http://localhost:8378/1/apps/invalid_verification_link.html?username=sadfasga&appId=test');
  675.         done();
  676.       });
  677.     });
  678.   });
  679.  
  680.   it('redirects you to link send fail page if you try to resend a link for a nonexistant user', done => {
  681.     reconfigureServer({
  682.       appName: 'emailing app',
  683.       verifyUserEmails: true,
  684.       emailAdapter: {
  685.         sendVerificationEmail: () => Promise.resolve(),
  686.         sendPasswordResetEmail: () => Promise.resolve(),
  687.         sendMail: () => {}
  688.       },
  689.       publicServerURL: "http://localhost:8378/1"
  690.     })
  691.     .then(() => {
  692.       request.post('http://localhost:8378/1/apps/test/resend_verification_email', {
  693.         followRedirect: false,
  694.         form: {
  695.           username: "sadfasga"
  696.         }
  697.       }, (error, response) => {
  698.         expect(response.statusCode).toEqual(302);
  699.         expect(response.body).toEqual('Found. Redirecting to http://localhost:8378/1/apps/link_send_fail.html');
  700.         done();
  701.       });
  702.     });
  703.   });
  704.  
  705.   it('does not update email verified if you use an invalid token', done => {
  706.     var user = new Parse.User();
  707.     var emailAdapter = {
  708.       sendVerificationEmail: () => {
  709.         request.get('http://localhost:8378/1/apps/test/verify_email?token=invalid&username=zxcv', {
  710.           followRedirect: false,
  711.         }, (error, response) => {
  712.           expect(response.statusCode).toEqual(302);
  713.           expect(response.body).toEqual('Found. Redirecting to http://localhost:8378/1/apps/invalid_verification_link.html?username=zxcv&appId=test');
  714.           user.fetch()
  715.           .then(() => {
  716.             expect(user.get('emailVerified')).toEqual(false);
  717.             done();
  718.           });
  719.         });
  720.       },
  721.       sendPasswordResetEmail: () => Promise.resolve(),
  722.       sendMail: () => {}
  723.     }
  724.     reconfigureServer({
  725.       appName: 'emailing app',
  726.       verifyUserEmails: true,
  727.       emailAdapter: emailAdapter,
  728.       publicServerURL: "http://localhost:8378/1"
  729.     })
  730.     .then(() => {
  731.       user.setPassword("asdf");
  732.       user.setUsername("zxcv");
  733.       user.set('email', 'user@parse.com');
  734.       user.signUp(null, {
  735.         success: () => {},
  736.         error: function() {
  737.           fail('Failed to save user');
  738.           done();
  739.         }
  740.       });
  741.     });
  742.   });
  743.  
  744.   it('should send a password reset link', done => {
  745.     var user = new Parse.User();
  746.     var emailAdapter = {
  747.       sendVerificationEmail: () => Promise.resolve(),
  748.       sendPasswordResetEmail: options => {
  749.         request.get(options.link, {
  750.           followRedirect: false,
  751.         }, (error, response) => {
  752.           if (error) {
  753.             jfail(error);
  754.             fail("Failed to get the reset link");
  755.             return;
  756.           }
  757.           expect(response.statusCode).toEqual(302);
  758.           var re = /http:\/\/localhost:8378\/1\/apps\/choose_password\?token=[a-zA-Z0-9]+\&id=test\&username=zxcv%2Bzxcv/;
  759.           expect(response.body.match(re)).not.toBe(null);
  760.           done();
  761.         });
  762.       },
  763.       sendMail: () => {}
  764.     }
  765.     reconfigureServer({
  766.       appName: 'emailing app',
  767.       verifyUserEmails: true,
  768.       emailAdapter: emailAdapter,
  769.       publicServerURL: "http://localhost:8378/1"
  770.     })
  771.     .then(() => {
  772.       user.setPassword("asdf");
  773.       user.setUsername("zxcv+zxcv");
  774.       user.set('email', 'user@parse.com');
  775.       user.signUp().then(() => {
  776.         Parse.User.requestPasswordReset('user@parse.com', {
  777.           error: (err) => {
  778.             jfail(err);
  779.             fail("Should not fail requesting a password");
  780.             done();
  781.           }
  782.         });
  783.       });
  784.     });
  785.   });
  786.  
  787.   it('redirects you to invalid link if you try to request password for a nonexistant users email', done => {
  788.     reconfigureServer({
  789.       appName: 'emailing app',
  790.       verifyUserEmails: true,
  791.       emailAdapter: {
  792.         sendVerificationEmail: () => Promise.resolve(),
  793.         sendPasswordResetEmail: () => Promise.resolve(),
  794.         sendMail: () => {}
  795.       },
  796.       publicServerURL: "http://localhost:8378/1"
  797.     })
  798.     .then(() => {
  799.       request.get('http://localhost:8378/1/apps/test/request_password_reset?token=asdfasdf&username=sadfasga', {
  800.         followRedirect: false,
  801.       }, (error, response) => {
  802.         expect(response.statusCode).toEqual(302);
  803.         expect(response.body).toEqual('Found. Redirecting to http://localhost:8378/1/apps/invalid_link.html');
  804.         done();
  805.       });
  806.     });
  807.   });
  808.  
  809.   it('should programatically reset password', done => {
  810.     var user = new Parse.User();
  811.     var emailAdapter = {
  812.       sendVerificationEmail: () => Promise.resolve(),
  813.       sendPasswordResetEmail: options => {
  814.         request.get(options.link, {
  815.           followRedirect: false,
  816.         }, (error, response) => {
  817.           if (error) {
  818.             jfail(error);
  819.             fail("Failed to get the reset link");
  820.             return;
  821.           }
  822.           expect(response.statusCode).toEqual(302);
  823.           var re = /http:\/\/localhost:8378\/1\/apps\/choose_password\?token=([a-zA-Z0-9]+)\&id=test\&username=zxcv/;
  824.           var match = response.body.match(re);
  825.           if (!match) {
  826.             fail("should have a token");
  827.             done();
  828.             return;
  829.           }
  830.           var token = match[1];
  831.  
  832.           request.post({
  833.             url: "http://localhost:8378/1/apps/test/request_password_reset" ,
  834.             body: `new_password=hello&token=${token}&username=zxcv`,
  835.             headers: {
  836.               'Content-Type': 'application/x-www-form-urlencoded'
  837.             },
  838.             followRedirect: false,
  839.           }, (error, response) => {
  840.             if (error) {
  841.               jfail(error);
  842.               fail("Failed to POST request password reset");
  843.               return;
  844.             }
  845.             expect(response.statusCode).toEqual(302);
  846.             expect(response.body).toEqual('Found. Redirecting to http://localhost:8378/1/apps/password_reset_success.html?username=zxcv');
  847.  
  848.             Parse.User.logIn("zxcv", "hello").then(function(){
  849.               const config = new Config('test');
  850.               config.database.adapter.find('_User', { fields: {} }, { 'username': 'zxcv' }, { limit: 1 })
  851.               .then(results => {
  852.                 // _perishable_token should be unset after reset password
  853.                 expect(results.length).toEqual(1);
  854.                 expect(results[0]['_perishable_token']).toEqual(undefined);
  855.                 done();
  856.               });
  857.             }, (err) => {
  858.               jfail(err);
  859.               fail("should login with new password");
  860.               done();
  861.             });
  862.  
  863.           });
  864.         });
  865.       },
  866.       sendMail: () => {}
  867.     }
  868.     reconfigureServer({
  869.       appName: 'emailing app',
  870.       verifyUserEmails: true,
  871.       emailAdapter: emailAdapter,
  872.       publicServerURL: "http://localhost:8378/1"
  873.     })
  874.     .then(() => {
  875.       user.setPassword("asdf");
  876.       user.setUsername("zxcv");
  877.       user.set('email', 'user@parse.com');
  878.       user.signUp().then(() => {
  879.         Parse.User.requestPasswordReset('user@parse.com', {
  880.           error: (err) => {
  881.             jfail(err);
  882.             fail("Should not fail");
  883.             done();
  884.           }
  885.         });
  886.       });
  887.     });
  888.   });
  889. })
  890.  
downloadValidationAndPasswordsReset.spec.js Source code - Download parse-server Source code
Related Source Codes/Software:
react-boilerplate - 2017-06-07
webtorrent - Streaming torrent client for the web ... 2017-06-06
machine-learning-for-software-engineers - A complete daily plan for studying to become a mac... 2017-06-06
upterm - A terminal emulator for the 21st century. 2017-06-06
lottie-android - Render After Effects animations natively on Androi... 2017-06-07
AsyncDisplayKit - Smooth asynchronous user interfaces for iOS apps. ... 2017-06-07
ionicons - The premium icon font for Ionic ... 2017-06-07
storybook - 2017-06-07
prettier - Prettier is an opinionated JavaScript formatter. ... 2017-06-08
CRYENGINE - CRYENGINE is a powerful real-time game development... 2017-06-11
postal - 2017-06-11
reactide - Reactide is the first dedicated IDE for React web ... 2017-06-11
rkt - rkt is a pod-native container engine for Linux. It... 2017-06-11
uWebSockets - Tiny WebSockets https://for... 2017-06-11
realworld - TodoMVC for the RealWorld - Exemplary fullstack Me... 2017-06-11
goreplay - GoReplay is an open-source tool for capturing and ... 2017-06-10
pyenv - Simple Python version management 2017-06-10
redux-saga - An alternative side effect model for Redux apps ... 2017-06-10
angular-starter - 2017-06-10

 Back to top