2016-07-19 01:10:36 -05:00
"use strict" ;
const request = require ( 'request' ) ;
2017-03-04 13:30:52 -08:00
const requestp = require ( 'request-promise' ) ;
2016-08-15 16:48:39 -04:00
const Config = require ( '../src/Config' ) ;
2016-07-19 01:10:36 -05:00
describe ( "Email Verification Token Expiration: " , ( ) => {
2017-05-10 14:02:16 +01:00
it ( 'show the invalid verification link page, if the user clicks on the verify email link after the email verify token expires' , done => {
2016-07-19 01:10:36 -05:00
var user = new Parse . User ( ) ;
var sendEmailOptions ;
var emailAdapter = {
sendVerificationEmail : options => {
sendEmailOptions = options ;
} ,
sendPasswordResetEmail : ( ) => Promise . resolve ( ) ,
sendMail : ( ) => { }
}
reconfigureServer ( {
appName : 'emailVerifyToken' ,
verifyUserEmails : true ,
emailAdapter : emailAdapter ,
emailVerifyTokenValidityDuration : 0.5 , // 0.5 second
publicServerURL : "http://localhost:8378/1"
} )
2017-06-20 09:15:26 -07:00
. then ( ( ) => {
user . setUsername ( "testEmailVerifyTokenValidity" ) ;
user . setPassword ( "expiringToken" ) ;
user . set ( 'email' , 'user@parse.com' ) ;
return user . signUp ( ) ;
} ) . then ( ( ) => {
2016-07-19 01:10:36 -05:00
// wait for 1 second - simulate user behavior to some extent
2017-06-20 09:15:26 -07:00
setTimeout ( ( ) => {
expect ( sendEmailOptions ) . not . toBeUndefined ( ) ;
2016-07-19 01:10:36 -05:00
2017-06-20 09:15:26 -07:00
request . get ( sendEmailOptions . link , {
followRedirect : false ,
} , ( error , response ) => {
expect ( response . statusCode ) . toEqual ( 302 ) ;
expect ( response . body ) . toEqual ( 'Found. Redirecting to http://localhost:8378/1/apps/invalid_verification_link.html?username=testEmailVerifyTokenValidity&appId=test' ) ;
done ( ) ;
} ) ;
} , 1000 ) ;
} ) . catch ( ( err ) => {
jfail ( err ) ;
done ( ) ;
} ) ;
2016-07-19 01:10:36 -05:00
} ) ;
2016-08-15 16:48:39 -04:00
it ( 'emailVerified should set to false, if the user does not verify their email before the email verify token expires' , done => {
2016-07-19 01:10:36 -05:00
var user = new Parse . User ( ) ;
var sendEmailOptions ;
var emailAdapter = {
sendVerificationEmail : options => {
sendEmailOptions = options ;
} ,
sendPasswordResetEmail : ( ) => Promise . resolve ( ) ,
sendMail : ( ) => { }
}
reconfigureServer ( {
appName : 'emailVerifyToken' ,
verifyUserEmails : true ,
emailAdapter : emailAdapter ,
emailVerifyTokenValidityDuration : 0.5 , // 0.5 second
publicServerURL : "http://localhost:8378/1"
} )
2017-06-20 09:15:26 -07:00
. then ( ( ) => {
user . setUsername ( "testEmailVerifyTokenValidity" ) ;
user . setPassword ( "expiringToken" ) ;
user . set ( 'email' , 'user@parse.com' ) ;
return user . signUp ( ) ;
} ) . then ( ( ) => {
2016-07-19 01:10:36 -05:00
// wait for 1 second - simulate user behavior to some extent
2017-06-20 09:15:26 -07:00
setTimeout ( ( ) => {
expect ( sendEmailOptions ) . not . toBeUndefined ( ) ;
2016-07-19 01:10:36 -05:00
2017-06-20 09:15:26 -07:00
request . get ( sendEmailOptions . link , {
followRedirect : false ,
} , ( error , response ) => {
expect ( response . statusCode ) . toEqual ( 302 ) ;
user . fetch ( )
. then ( ( ) => {
expect ( user . get ( 'emailVerified' ) ) . toEqual ( false ) ;
done ( ) ;
} )
. catch ( ( ) => {
jfail ( error ) ;
done ( ) ;
} ) ;
2016-07-19 01:10:36 -05:00
} ) ;
2017-06-20 09:15:26 -07:00
} , 1000 ) ;
} ) . catch ( ( error ) => {
jfail ( error ) ;
done ( ) ;
} ) ;
2016-07-19 01:10:36 -05:00
} ) ;
2016-08-15 16:48:39 -04:00
it ( 'if user clicks on the email verify link before email verification token expiration then show the verify email success page' , done => {
2016-07-19 01:10:36 -05:00
var user = new Parse . User ( ) ;
var sendEmailOptions ;
var emailAdapter = {
sendVerificationEmail : options => {
sendEmailOptions = options ;
} ,
sendPasswordResetEmail : ( ) => Promise . resolve ( ) ,
sendMail : ( ) => { }
}
reconfigureServer ( {
appName : 'emailVerifyToken' ,
verifyUserEmails : true ,
emailAdapter : emailAdapter ,
emailVerifyTokenValidityDuration : 5 , // 5 seconds
publicServerURL : "http://localhost:8378/1"
} )
2017-06-20 09:15:26 -07:00
. then ( ( ) => {
user . setUsername ( "testEmailVerifyTokenValidity" ) ;
user . setPassword ( "expiringToken" ) ;
user . set ( 'email' , 'user@parse.com' ) ;
return user . signUp ( ) ;
} ) . then ( ( ) => {
request . get ( sendEmailOptions . link , {
followRedirect : false ,
} , ( error , response ) => {
expect ( response . statusCode ) . toEqual ( 302 ) ;
expect ( response . body ) . toEqual ( 'Found. Redirecting to http://localhost:8378/1/apps/verify_email_success.html?username=testEmailVerifyTokenValidity' ) ;
done ( ) ;
} ) ;
} ) . catch ( ( error ) => {
jfail ( error ) ;
2016-07-19 01:10:36 -05:00
done ( ) ;
} ) ;
} ) ;
2016-08-15 16:48:39 -04:00
it ( 'if user clicks on the email verify link before email verification token expiration then emailVerified should be true' , done => {
2016-07-19 01:10:36 -05:00
var user = new Parse . User ( ) ;
var sendEmailOptions ;
var emailAdapter = {
sendVerificationEmail : options => {
sendEmailOptions = options ;
} ,
sendPasswordResetEmail : ( ) => Promise . resolve ( ) ,
sendMail : ( ) => { }
}
reconfigureServer ( {
appName : 'emailVerifyToken' ,
verifyUserEmails : true ,
emailAdapter : emailAdapter ,
emailVerifyTokenValidityDuration : 5 , // 5 seconds
publicServerURL : "http://localhost:8378/1"
} )
2017-06-20 09:15:26 -07:00
. then ( ( ) => {
user . setUsername ( "testEmailVerifyTokenValidity" ) ;
user . setPassword ( "expiringToken" ) ;
user . set ( 'email' , 'user@parse.com' ) ;
return user . signUp ( ) ;
} ) . then ( ( ) => {
request . get ( sendEmailOptions . link , {
followRedirect : false ,
} , ( error , response ) => {
expect ( response . statusCode ) . toEqual ( 302 ) ;
user . fetch ( )
. then ( ( ) => {
expect ( user . get ( 'emailVerified' ) ) . toEqual ( true ) ;
done ( ) ;
} )
. catch ( ( error ) => {
jfail ( error ) ;
done ( ) ;
} ) ;
2016-07-19 01:10:36 -05:00
} ) ;
2017-06-20 09:15:26 -07:00
} ) . catch ( ( error ) => {
jfail ( error ) ;
done ( ) ;
2016-07-19 01:10:36 -05:00
} ) ;
} ) ;
2016-08-15 16:48:39 -04:00
it ( 'if user clicks on the email verify link before email verification token expiration then user should be able to login' , done => {
2016-07-19 01:10:36 -05:00
var user = new Parse . User ( ) ;
var sendEmailOptions ;
var emailAdapter = {
sendVerificationEmail : options => {
sendEmailOptions = options ;
} ,
sendPasswordResetEmail : ( ) => Promise . resolve ( ) ,
sendMail : ( ) => { }
}
reconfigureServer ( {
appName : 'emailVerifyToken' ,
verifyUserEmails : true ,
emailAdapter : emailAdapter ,
emailVerifyTokenValidityDuration : 5 , // 5 seconds
publicServerURL : "http://localhost:8378/1"
} )
2017-06-20 09:15:26 -07:00
. then ( ( ) => {
user . setUsername ( "testEmailVerifyTokenValidity" ) ;
user . setPassword ( "expiringToken" ) ;
user . set ( 'email' , 'user@parse.com' ) ;
return user . signUp ( ) ;
} ) . then ( ( ) => {
request . get ( sendEmailOptions . link , {
followRedirect : false ,
} , ( error , response ) => {
expect ( response . statusCode ) . toEqual ( 302 ) ;
Parse . User . logIn ( "testEmailVerifyTokenValidity" , "expiringToken" )
. then ( user => {
expect ( typeof user ) . toBe ( 'object' ) ;
expect ( user . get ( 'emailVerified' ) ) . toBe ( true ) ;
done ( ) ;
} )
. catch ( ( error ) => {
jfail ( error ) ;
done ( ) ;
} ) ;
2016-07-19 01:10:36 -05:00
} ) ;
2017-06-20 09:15:26 -07:00
} ) . catch ( ( error ) => {
jfail ( error ) ;
done ( ) ;
2016-07-19 01:10:36 -05:00
} ) ;
} ) ;
2016-08-15 16:48:39 -04:00
it ( 'sets the _email_verify_token_expires_at and _email_verify_token fields after user SignUp' , done => {
2016-07-19 01:10:36 -05:00
var user = new Parse . User ( ) ;
var sendEmailOptions ;
var emailAdapter = {
sendVerificationEmail : options => {
sendEmailOptions = options ;
} ,
sendPasswordResetEmail : ( ) => Promise . resolve ( ) ,
sendMail : ( ) => { }
}
reconfigureServer ( {
appName : 'emailVerifyToken' ,
verifyUserEmails : true ,
emailAdapter : emailAdapter ,
emailVerifyTokenValidityDuration : 5 , // 5 seconds
publicServerURL : 'http://localhost:8378/1'
} )
2017-06-20 09:15:26 -07:00
. then ( ( ) => {
user . setUsername ( 'sets_email_verify_token_expires_at' ) ;
user . setPassword ( 'expiringToken' ) ;
user . set ( 'email' , 'user@parse.com' ) ;
return user . signUp ( ) ;
} )
. then ( ( ) => {
2017-10-23 08:43:05 -04:00
const config = Config . get ( 'test' ) ;
2017-06-20 09:15:26 -07:00
return config . database . find ( '_User' , { username : 'sets_email_verify_token_expires_at' } ) ;
} )
. then ( results => {
expect ( results . length ) . toBe ( 1 ) ;
const user = results [ 0 ] ;
expect ( typeof user ) . toBe ( 'object' ) ;
expect ( user . emailVerified ) . toEqual ( false ) ;
expect ( typeof user . _email _verify _token ) . toBe ( 'string' ) ;
expect ( typeof user . _email _verify _token _expires _at ) . toBe ( 'object' ) ;
expect ( sendEmailOptions ) . toBeDefined ( ) ;
done ( ) ;
} )
. catch ( error => {
jfail ( error ) ;
done ( ) ;
} ) ;
2016-07-19 01:10:36 -05:00
} ) ;
2016-08-15 16:48:39 -04:00
it ( 'unsets the _email_verify_token_expires_at and _email_verify_token fields in the User class if email verification is successful' , done => {
2016-07-19 01:10:36 -05:00
var user = new Parse . User ( ) ;
var sendEmailOptions ;
var emailAdapter = {
sendVerificationEmail : options => {
sendEmailOptions = options ;
} ,
sendPasswordResetEmail : ( ) => Promise . resolve ( ) ,
sendMail : ( ) => { }
}
reconfigureServer ( {
appName : 'emailVerifyToken' ,
verifyUserEmails : true ,
emailAdapter : emailAdapter ,
emailVerifyTokenValidityDuration : 5 , // 5 seconds
publicServerURL : "http://localhost:8378/1"
} )
2017-06-20 09:15:26 -07:00
. then ( ( ) => {
user . setUsername ( "unsets_email_verify_token_expires_at" ) ;
user . setPassword ( "expiringToken" ) ;
user . set ( 'email' , 'user@parse.com' ) ;
return user . signUp ( ) ;
} )
. then ( ( ) => {
request . get ( sendEmailOptions . link , {
followRedirect : false ,
} , ( error , response ) => {
expect ( response . statusCode ) . toEqual ( 302 ) ;
2017-10-23 08:43:05 -04:00
const config = Config . get ( 'test' ) ;
2017-06-20 09:15:26 -07:00
return config . database . find ( '_User' , { username : 'unsets_email_verify_token_expires_at' } ) . then ( ( results ) => {
expect ( results . length ) . toBe ( 1 ) ;
return results [ 0 ] ;
} )
. then ( user => {
expect ( typeof user ) . toBe ( 'object' ) ;
expect ( user . emailVerified ) . toEqual ( true ) ;
expect ( typeof user . _email _verify _token ) . toBe ( 'undefined' ) ;
expect ( typeof user . _email _verify _token _expires _at ) . toBe ( 'undefined' ) ;
done ( ) ;
} )
. catch ( error => {
jfail ( error ) ;
done ( ) ;
} ) ;
2016-07-19 01:10:36 -05:00
} ) ;
2017-06-20 09:15:26 -07:00
} )
. catch ( error => {
jfail ( error ) ;
done ( ) ;
2016-07-19 01:10:36 -05:00
} ) ;
} ) ;
2017-05-10 14:02:16 +01:00
it ( 'clicking on the email verify link by an email VERIFIED user that was setup before enabling the expire email verify token should show email verify email success' , done => {
2016-07-19 01:10:36 -05:00
var user = new Parse . User ( ) ;
var sendEmailOptions ;
var emailAdapter = {
sendVerificationEmail : options => {
sendEmailOptions = options ;
} ,
sendPasswordResetEmail : ( ) => Promise . resolve ( ) ,
sendMail : ( ) => { }
}
var serverConfig = {
appName : 'emailVerifyToken' ,
verifyUserEmails : true ,
emailAdapter : emailAdapter ,
publicServerURL : "http://localhost:8378/1"
} ;
// setup server WITHOUT enabling the expire email verify token flag
reconfigureServer ( serverConfig )
2017-06-20 09:15:26 -07:00
. then ( ( ) => {
user . setUsername ( "testEmailVerifyTokenValidity" ) ;
user . setPassword ( "expiringToken" ) ;
user . set ( 'email' , 'user@parse.com' ) ;
return user . signUp ( ) ;
} )
. then ( ( ) => {
return new Promise ( ( resolve , reject ) => {
request . get ( sendEmailOptions . link , { followRedirect : false , } )
. on ( 'error' , error => reject ( error ) )
. on ( 'response' , ( response ) => {
expect ( response . statusCode ) . toEqual ( 302 ) ;
resolve ( user . fetch ( ) ) ;
} ) ;
} ) ;
} )
. then ( ( ) => {
expect ( user . get ( 'emailVerified' ) ) . toEqual ( true ) ;
// RECONFIGURE the server i.e., ENABLE the expire email verify token flag
serverConfig . emailVerifyTokenValidityDuration = 5 ; // 5 seconds
return reconfigureServer ( serverConfig ) ;
} )
. then ( ( ) => {
request . get ( sendEmailOptions . link , {
followRedirect : false ,
} , ( error , response ) => {
2016-07-19 01:10:36 -05:00
expect ( response . statusCode ) . toEqual ( 302 ) ;
2017-06-20 09:15:26 -07:00
expect ( response . body ) . toEqual ( 'Found. Redirecting to http://localhost:8378/1/apps/verify_email_success.html?username=testEmailVerifyTokenValidity' ) ;
done ( ) ;
2016-07-19 01:10:36 -05:00
} ) ;
2017-06-20 09:15:26 -07:00
} )
. catch ( ( error ) => {
jfail ( error ) ;
2016-07-19 01:10:36 -05:00
done ( ) ;
} ) ;
} ) ;
2017-05-10 14:02:16 +01:00
it ( 'clicking on the email verify link by an email UNVERIFIED user that was setup before enabling the expire email verify token should show invalid verficiation link page' , done => {
2016-07-19 01:10:36 -05:00
var user = new Parse . User ( ) ;
var sendEmailOptions ;
var emailAdapter = {
sendVerificationEmail : options => {
sendEmailOptions = options ;
} ,
sendPasswordResetEmail : ( ) => Promise . resolve ( ) ,
sendMail : ( ) => { }
}
var serverConfig = {
appName : 'emailVerifyToken' ,
verifyUserEmails : true ,
emailAdapter : emailAdapter ,
publicServerURL : "http://localhost:8378/1"
} ;
// setup server WITHOUT enabling the expire email verify token flag
reconfigureServer ( serverConfig )
2017-06-20 09:15:26 -07:00
. then ( ( ) => {
user . setUsername ( "testEmailVerifyTokenValidity" ) ;
user . setPassword ( "expiringToken" ) ;
user . set ( 'email' , 'user@parse.com' ) ;
return user . signUp ( ) ;
} )
. then ( ( ) => {
2016-07-19 01:10:36 -05:00
// just get the user again - DO NOT email verify the user
2017-06-20 09:15:26 -07:00
return user . fetch ( ) ;
} )
. then ( ( ) => {
expect ( user . get ( 'emailVerified' ) ) . toEqual ( false ) ;
// RECONFIGURE the server i.e., ENABLE the expire email verify token flag
serverConfig . emailVerifyTokenValidityDuration = 5 ; // 5 seconds
return reconfigureServer ( serverConfig ) ;
} )
. then ( ( ) => {
request . get ( sendEmailOptions . link , {
followRedirect : false ,
} , ( error , response ) => {
expect ( response . statusCode ) . toEqual ( 302 ) ;
expect ( response . body ) . toEqual ( 'Found. Redirecting to http://localhost:8378/1/apps/invalid_verification_link.html?username=testEmailVerifyTokenValidity&appId=test' ) ;
done ( ) ;
} ) ;
} )
. catch ( ( error ) => {
jfail ( error ) ;
2016-07-19 01:10:36 -05:00
done ( ) ;
} ) ;
} ) ;
2016-08-15 16:48:39 -04:00
it ( 'setting the email on the user should set a new email verification token and new expiration date for the token when expire email verify token flag is set' , done => {
2016-07-19 01:10:36 -05:00
2016-12-07 15:17:05 -08:00
const user = new Parse . User ( ) ;
2016-07-19 01:10:36 -05:00
let userBeforeEmailReset ;
let sendEmailOptions ;
2016-12-07 15:17:05 -08:00
const emailAdapter = {
2016-07-19 01:10:36 -05:00
sendVerificationEmail : options => {
sendEmailOptions = options ;
} ,
sendPasswordResetEmail : ( ) => Promise . resolve ( ) ,
sendMail : ( ) => { }
} ;
2016-12-07 15:17:05 -08:00
const serverConfig = {
2016-07-19 01:10:36 -05:00
appName : 'emailVerifyToken' ,
verifyUserEmails : true ,
emailAdapter : emailAdapter ,
emailVerifyTokenValidityDuration : 5 , // 5 seconds
publicServerURL : "http://localhost:8378/1"
} ;
reconfigureServer ( serverConfig )
2017-06-20 09:15:26 -07:00
. then ( ( ) => {
user . setUsername ( "newEmailVerifyTokenOnEmailReset" ) ;
user . setPassword ( "expiringToken" ) ;
user . set ( 'email' , 'user@parse.com' ) ;
return user . signUp ( ) ;
} )
. then ( ( ) => {
2017-10-23 08:43:05 -04:00
const config = Config . get ( 'test' ) ;
2017-06-20 09:15:26 -07:00
return config . database . find ( '_User' , { username : 'newEmailVerifyTokenOnEmailReset' } ) . then ( ( results ) => {
return results [ 0 ] ;
} ) ;
} )
. then ( userFromDb => {
expect ( typeof userFromDb ) . toBe ( 'object' ) ;
userBeforeEmailReset = userFromDb ;
2016-07-19 01:10:36 -05:00
2017-06-20 09:15:26 -07:00
// trigger another token generation by setting the email
user . set ( 'email' , 'user@parse.com' ) ;
return new Promise ( ( resolve ) => {
2016-07-19 01:10:36 -05:00
// wait for half a sec to get a new expiration time
2017-06-20 09:15:26 -07:00
setTimeout ( ( ) => resolve ( user . save ( ) ) , 500 ) ;
} ) ;
} )
. then ( ( ) => {
2017-10-23 08:43:05 -04:00
const config = Config . get ( 'test' ) ;
2017-06-20 09:15:26 -07:00
return config . database . find ( '_User' , { username : 'newEmailVerifyTokenOnEmailReset' } ) . then ( ( results ) => {
return results [ 0 ] ;
} ) ;
} )
. then ( userAfterEmailReset => {
expect ( typeof userAfterEmailReset ) . toBe ( 'object' ) ;
expect ( userBeforeEmailReset . _email _verify _token ) . not . toEqual ( userAfterEmailReset . _email _verify _token ) ;
expect ( userBeforeEmailReset . _email _verify _token _expires _at ) . not . toEqual ( userAfterEmailReset . _ _email _verify _token _expires _at ) ;
expect ( sendEmailOptions ) . toBeDefined ( ) ;
done ( ) ;
} )
. catch ( ( error ) => {
jfail ( error ) ;
done ( ) ;
2016-08-15 16:48:39 -04:00
} ) ;
2016-07-19 01:10:36 -05:00
} ) ;
2017-03-04 13:30:52 -08:00
it ( 'should send a new verification email when a resend is requested and the user is UNVERIFIED' , done => {
var user = new Parse . User ( ) ;
var sendEmailOptions ;
var sendVerificationEmailCallCount = 0 ;
var emailAdapter = {
sendVerificationEmail : options => {
sendEmailOptions = options ;
sendVerificationEmailCallCount ++ ;
} ,
sendPasswordResetEmail : ( ) => Promise . resolve ( ) ,
sendMail : ( ) => { }
}
reconfigureServer ( {
appName : 'emailVerifyToken' ,
verifyUserEmails : true ,
emailAdapter : emailAdapter ,
emailVerifyTokenValidityDuration : 5 , // 5 seconds
publicServerURL : 'http://localhost:8378/1'
} )
2017-06-20 09:15:26 -07:00
. then ( ( ) => {
user . setUsername ( 'resends_verification_token' ) ;
user . setPassword ( 'expiringToken' ) ;
user . set ( 'email' , 'user@parse.com' ) ;
return user . signUp ( ) ;
} )
. then ( ( ) => {
expect ( sendVerificationEmailCallCount ) . toBe ( 1 ) ;
2017-03-04 13:30:52 -08:00
2017-06-20 09:15:26 -07:00
return requestp . post ( {
uri : 'http://localhost:8378/1/verificationEmailRequest' ,
body : {
email : 'user@parse.com'
} ,
headers : {
'X-Parse-Application-Id' : Parse . applicationId ,
'X-Parse-REST-API-Key' : 'rest' ,
} ,
json : true ,
resolveWithFullResponse : true ,
simple : false // this promise is only rejected if the call itself failed
} )
. then ( ( response ) => {
expect ( response . statusCode ) . toBe ( 200 ) ;
expect ( sendVerificationEmailCallCount ) . toBe ( 2 ) ;
expect ( sendEmailOptions ) . toBeDefined ( ) ;
done ( ) ;
} ) ;
2017-03-04 13:30:52 -08:00
} )
2017-06-20 09:15:26 -07:00
. catch ( error => {
jfail ( error ) ;
2017-03-04 13:30:52 -08:00
done ( ) ;
} ) ;
} ) ;
it ( 'should not send a new verification email when a resend is requested and the user is VERIFIED' , done => {
var user = new Parse . User ( ) ;
var sendEmailOptions ;
var sendVerificationEmailCallCount = 0 ;
var emailAdapter = {
sendVerificationEmail : options => {
sendEmailOptions = options ;
sendVerificationEmailCallCount ++ ;
} ,
sendPasswordResetEmail : ( ) => Promise . resolve ( ) ,
sendMail : ( ) => { }
}
reconfigureServer ( {
appName : 'emailVerifyToken' ,
verifyUserEmails : true ,
emailAdapter : emailAdapter ,
emailVerifyTokenValidityDuration : 5 , // 5 seconds
publicServerURL : 'http://localhost:8378/1'
} )
2017-06-20 09:15:26 -07:00
. then ( ( ) => {
user . setUsername ( 'no_new_verification_token_once_verified' ) ;
user . setPassword ( 'expiringToken' ) ;
user . set ( 'email' , 'user@parse.com' ) ;
return user . signUp ( ) ;
2017-03-04 13:30:52 -08:00
} )
2017-06-20 09:15:26 -07:00
. then ( ( ) => {
return requestp . get ( {
url : sendEmailOptions . link ,
followRedirect : false ,
resolveWithFullResponse : true ,
simple : false
} )
. then ( ( response ) => {
expect ( response . statusCode ) . toEqual ( 302 ) ;
} ) ;
2017-03-04 13:30:52 -08:00
} )
2017-06-20 09:15:26 -07:00
. then ( ( ) => {
2017-03-04 13:30:52 -08:00
expect ( sendVerificationEmailCallCount ) . toBe ( 1 ) ;
2017-06-20 09:15:26 -07:00
return requestp . post ( {
uri : 'http://localhost:8378/1/verificationEmailRequest' ,
body : {
email : 'user@parse.com'
} ,
headers : {
'X-Parse-Application-Id' : Parse . applicationId ,
'X-Parse-REST-API-Key' : 'rest' ,
} ,
json : true ,
resolveWithFullResponse : true ,
simple : false // this promise is only rejected if the call itself failed
} )
. then ( ( response ) => {
expect ( response . statusCode ) . toBe ( 400 ) ;
expect ( sendVerificationEmailCallCount ) . toBe ( 1 ) ;
done ( ) ;
} ) ;
} )
. catch ( error => {
jfail ( error ) ;
2017-03-04 13:30:52 -08:00
done ( ) ;
} ) ;
} ) ;
it ( 'should not send a new verification email if this user does not exist' , done => {
var sendEmailOptions ;
var sendVerificationEmailCallCount = 0 ;
var emailAdapter = {
sendVerificationEmail : options => {
sendEmailOptions = options ;
sendVerificationEmailCallCount ++ ;
} ,
sendPasswordResetEmail : ( ) => Promise . resolve ( ) ,
sendMail : ( ) => { }
}
reconfigureServer ( {
appName : 'emailVerifyToken' ,
verifyUserEmails : true ,
emailAdapter : emailAdapter ,
emailVerifyTokenValidityDuration : 5 , // 5 seconds
publicServerURL : 'http://localhost:8378/1'
} )
2017-06-20 09:15:26 -07:00
. then ( ( ) => {
return requestp . post ( {
uri : 'http://localhost:8378/1/verificationEmailRequest' ,
body : {
email : 'user@parse.com'
} ,
headers : {
'X-Parse-Application-Id' : Parse . applicationId ,
'X-Parse-REST-API-Key' : 'rest' ,
} ,
json : true ,
resolveWithFullResponse : true ,
simple : false
} )
. then ( response => {
expect ( response . statusCode ) . toBe ( 400 ) ;
expect ( sendVerificationEmailCallCount ) . toBe ( 0 ) ;
expect ( sendEmailOptions ) . not . toBeDefined ( ) ;
done ( ) ;
} ) ;
2017-03-04 13:30:52 -08:00
} )
2017-06-20 09:15:26 -07:00
. catch ( error => {
jfail ( error ) ;
2017-03-04 13:30:52 -08:00
done ( ) ;
} ) ;
} ) ;
it ( 'should fail if no email is supplied' , done => {
var sendEmailOptions ;
var sendVerificationEmailCallCount = 0 ;
var emailAdapter = {
sendVerificationEmail : options => {
sendEmailOptions = options ;
sendVerificationEmailCallCount ++ ;
} ,
sendPasswordResetEmail : ( ) => Promise . resolve ( ) ,
sendMail : ( ) => { }
}
reconfigureServer ( {
appName : 'emailVerifyToken' ,
verifyUserEmails : true ,
emailAdapter : emailAdapter ,
emailVerifyTokenValidityDuration : 5 , // 5 seconds
publicServerURL : 'http://localhost:8378/1'
} )
2017-06-20 09:15:26 -07:00
. then ( ( ) => {
request . post ( {
uri : 'http://localhost:8378/1/verificationEmailRequest' ,
body : { } ,
headers : {
'X-Parse-Application-Id' : Parse . applicationId ,
'X-Parse-REST-API-Key' : 'rest' ,
} ,
json : true ,
resolveWithFullResponse : true ,
simple : false
} , ( err , response ) => {
expect ( response . statusCode ) . toBe ( 400 ) ;
expect ( response . body . code ) . toBe ( Parse . Error . EMAIL _MISSING ) ;
expect ( response . body . error ) . toBe ( 'you must provide an email' ) ;
expect ( sendVerificationEmailCallCount ) . toBe ( 0 ) ;
expect ( sendEmailOptions ) . not . toBeDefined ( ) ;
done ( ) ;
} ) ;
} )
. catch ( error => {
jfail ( error ) ;
2017-03-04 13:30:52 -08:00
done ( ) ;
} ) ;
} ) ;
it ( 'should fail if email is not a string' , done => {
var sendEmailOptions ;
var sendVerificationEmailCallCount = 0 ;
var emailAdapter = {
sendVerificationEmail : options => {
sendEmailOptions = options ;
sendVerificationEmailCallCount ++ ;
} ,
sendPasswordResetEmail : ( ) => Promise . resolve ( ) ,
sendMail : ( ) => { }
}
reconfigureServer ( {
appName : 'emailVerifyToken' ,
verifyUserEmails : true ,
emailAdapter : emailAdapter ,
emailVerifyTokenValidityDuration : 5 , // 5 seconds
publicServerURL : 'http://localhost:8378/1'
} )
2017-06-20 09:15:26 -07:00
. then ( ( ) => {
request . post ( {
uri : 'http://localhost:8378/1/verificationEmailRequest' ,
body : { email : 3 } ,
headers : {
'X-Parse-Application-Id' : Parse . applicationId ,
'X-Parse-REST-API-Key' : 'rest' ,
} ,
json : true ,
resolveWithFullResponse : true ,
simple : false
} , ( err , response ) => {
expect ( response . statusCode ) . toBe ( 400 ) ;
expect ( response . body . code ) . toBe ( Parse . Error . INVALID _EMAIL _ADDRESS ) ;
expect ( response . body . error ) . toBe ( 'you must provide a valid email string' ) ;
expect ( sendVerificationEmailCallCount ) . toBe ( 0 ) ;
expect ( sendEmailOptions ) . not . toBeDefined ( ) ;
done ( ) ;
} ) ;
} )
. catch ( error => {
jfail ( error ) ;
2017-03-04 13:30:52 -08:00
done ( ) ;
} ) ;
} ) ;
2016-08-15 16:48:39 -04:00
it ( 'client should not see the _email_verify_token_expires_at field' , done => {
2016-07-19 01:10:36 -05:00
var user = new Parse . User ( ) ;
var sendEmailOptions ;
var emailAdapter = {
sendVerificationEmail : options => {
sendEmailOptions = options ;
} ,
sendPasswordResetEmail : ( ) => Promise . resolve ( ) ,
sendMail : ( ) => { }
}
reconfigureServer ( {
appName : 'emailVerifyToken' ,
verifyUserEmails : true ,
emailAdapter : emailAdapter ,
emailVerifyTokenValidityDuration : 5 , // 5 seconds
publicServerURL : "http://localhost:8378/1"
} )
. then ( ( ) => {
2017-06-20 09:15:26 -07:00
user . setUsername ( "testEmailVerifyTokenValidity" ) ;
user . setPassword ( "expiringToken" ) ;
user . set ( 'email' , 'user@parse.com' ) ;
return user . signUp ( ) ;
2016-07-19 01:10:36 -05:00
} )
2017-06-20 09:15:26 -07:00
. then ( ( ) => {
user . fetch ( )
. then ( ( ) => {
expect ( user . get ( 'emailVerified' ) ) . toEqual ( false ) ;
expect ( typeof user . get ( '_email_verify_token_expires_at' ) ) . toBe ( 'undefined' ) ;
expect ( sendEmailOptions ) . toBeDefined ( ) ;
done ( ) ;
} )
. catch ( error => {
jfail ( error ) ;
done ( ) ;
} ) ;
} ) . catch ( ( error ) => {
2016-08-15 16:48:39 -04:00
jfail ( error ) ;
2016-07-19 01:10:36 -05:00
done ( ) ;
} ) ;
} ) ;
} )