import { User } from "../../../src/models" ; import { isMobile } from "../../support/utils" ; describe ( "User Sign-up and Login" , function ( ) { beforeEach ( function ( ) { cy . task ( "db:seed" ) ; cy . server ( ) ; cy . route ( "POST" , "/users" ) . as ( "signup" ) ; cy . route ( "POST" , "/bankAccounts" ) . as ( "createBankAccount" ) ; } ) ; it ( "should redirect unauthenticated user to signin page" , function ( ) { cy . visit ( "/personal" ) ; cy . location ( "pathname" ) . should ( "equal" , "/signin" ) ; cy . percySnapshot ( "Redirect to SignIn" ) ; } ) ; it ( "should remember a user for 30 days after login" , function ( ) { cy . database ( "find" , "users" ) . then ( ( user ) => { cy . login ( user . username , "s3cret" , true ) ; } ) ; cy . getCookie ( "connect.sid" ) . should ( "have.property" , "expiry" ) ; if ( isMobile ( ) ) { cy . getBySel ( "sidenav-toggle" ) . click ( ) ; } cy . getBySel ( "sidenav-signout" ) . click ( ) ; cy . location ( "pathname" ) . should ( "eq" , "/signin" ) ; cy . percySnapshot ( "Redirect to SignIn" ) ; } ) ; it ( "should allow a visitor to sign-up, login, and logout" , function ( ) { const userInfo = { firstName : "Bob" , lastName : "Ross" , username : "PainterJoy90" , password : "s3cret" , } ; cy . visit ( "/" ) ; cy . getBySel ( "signup" ) . click ( ) ; cy . getBySel ( "signup-title" ) . should ( "be.visible" ) . and ( "contain" , "Sign Up" ) ; cy . percySnapshot ( "Sign Up Title" ) ; cy . getBySel ( "signup-first-name" ) . type ( userInfo . firstName ) ; cy . getBySel ( "signup-last-name" ) . type ( userInfo . lastName ) ; cy . getBySel ( "signup-username" ) . type ( userInfo . username ) ; cy . getBySel ( "signup-password" ) . type ( userInfo . password ) ; cy . getBySel ( "signup-confirmPassword" ) . type ( userInfo . password ) ; cy . percySnapshot ( "About to Sign Up" ) ; cy . getBySel ( "signup-submit" ) . click ( ) ; cy . wait ( "@signup" ) ; cy . login ( userInfo . username , userInfo . password ) ; cy . getBySel ( "user-onboarding-dialog" ) . should ( "be.visible" ) ; cy . percySnapshot ( "User Onboarding Dialog" ) ; cy . getBySel ( "user-onboarding-next" ) . click ( ) ; cy . getBySel ( "user-onboarding-dialog-title" ) . should ( "contain" , "Create Bank Account" ) ; cy . getBySelLike ( "bankName-input" ) . type ( "The Best Bank" ) ; cy . getBySelLike ( "accountNumber-input" ) . type ( "123456789" ) ; cy . getBySelLike ( "routingNumber-input" ) . type ( "987654321" ) ; cy . percySnapshot ( "About to complete User Onboarding" ) ; cy . getBySelLike ( "submit" ) . click ( ) ; cy . wait ( "@createBankAccount" ) ; cy . getBySel ( "user-onboarding-dialog-title" ) . should ( "contain" , "Finished" ) ; cy . getBySel ( "user-onboarding-dialog-content" ) . should ( "contain" , "You're all set!" ) ; cy . percySnapshot ( "Finished User Onboarding" ) ; cy . getBySel ( "user-onboarding-next" ) . click ( ) ; cy . getBySel ( "transaction-list" ) . should ( "be.visible" ) ; cy . percySnapshot ( "Transaction List is visible after User Onboarding" ) ; if ( isMobile ( ) ) { cy . getBySel ( "sidenav-toggle" ) . click ( ) ; } cy . getBySel ( "sidenav-signout" ) . click ( ) ; cy . location ( "pathname" ) . should ( "eq" , "/signin" ) ; cy . percySnapshot ( "Redirect to SignIn" ) ; } ) ; it ( "should display login errors" , function ( ) { cy . visit ( "/" ) ; cy . getBySel ( "signin-username" ) . type ( "User" ) . find ( "input" ) . clear ( ) . blur ( ) ; cy . get ( "#username-helper-text" ) . should ( "be.visible" ) . and ( "contain" , "Username is required" ) ; cy . percySnapshot ( "Display Username is Required Error" ) ; cy . getBySel ( "signin-password" ) . type ( "abc" ) . find ( "input" ) . blur ( ) ; cy . get ( "#password-helper-text" ) . should ( "be.visible" ) . and ( "contain" , "Password must contain at least 4 characters" ) ; cy . percySnapshot ( "Display Password Error" ) ; cy . getBySel ( "signin-submit" ) . should ( "be.disabled" ) ; cy . percySnapshot ( "Sign In Submit Disabled" ) ; } ) ; it ( "should display signup errors" , function ( ) { cy . visit ( "/signup" ) ; cy . getBySel ( "signup-first-name" ) . type ( "First" ) . find ( "input" ) . clear ( ) . blur ( ) ; cy . get ( "#firstName-helper-text" ) . should ( "be.visible" ) . and ( "contain" , "First Name is required" ) ; cy . getBySel ( "signup-last-name" ) . type ( "Last" ) . find ( "input" ) . clear ( ) . blur ( ) ; cy . get ( "#lastName-helper-text" ) . should ( "be.visible" ) . and ( "contain" , "Last Name is required" ) ; cy . getBySel ( "signup-username" ) . type ( "User" ) . find ( "input" ) . clear ( ) . blur ( ) ; cy . get ( "#username-helper-text" ) . should ( "be.visible" ) . and ( "contain" , "Username is required" ) ; cy . getBySel ( "signup-password" ) . type ( "password" ) . find ( "input" ) . clear ( ) . blur ( ) ; cy . get ( "#password-helper-text" ) . should ( "be.visible" ) . and ( "contain" , "Enter your password" ) ; cy . getBySel ( "signup-confirmPassword" ) . type ( "DIFFERENT PASSWORD" ) . find ( "input" ) . blur ( ) ; cy . get ( "#confirmPassword-helper-text" ) . should ( "be.visible" ) . and ( "contain" , "Password does not match" ) ; cy . percySnapshot ( "Display Sign Up Required Errors" ) ; cy . getBySel ( "signup-submit" ) . should ( "be.disabled" ) ; cy . percySnapshot ( "Sign Up Submit Disabled" ) ; } ) ; it ( "should error for an invalid user" , function ( ) { cy . login ( "invalidUserName" , "invalidPa$$word" ) ; cy . getBySel ( "signin-error" ) . should ( "be.visible" ) . and ( "have.text" , "Username or password is invalid" ) ; cy . percySnapshot ( "Sign In, Invalid Username and Password, Username or Password is Invalid" ) ; } ) ; it ( "should error for an invalid password for existing user" , function ( ) { cy . database ( "find" , "users" ) . then ( ( user ) => { cy . login ( user . username , "INVALID" ) ; } ) ; cy . getBySel ( "signin-error" ) . should ( "be.visible" ) . and ( "have.text" , "Username or password is invalid" ) ; cy . percySnapshot ( "Sign In, Invalid Username, Username or Password is Invalid" ) ; } ) ; } ) ;