From faa24bbf327a03616e423b26a7f80fb66cd8ea28 Mon Sep 17 00:00:00 2001 From: Zach Leach Date: Wed, 22 May 2019 19:57:32 -0600 Subject: [PATCH 1/4] Added test that fields cannot be blank for user registration test. --- spec/features/users/new_spec.rb | 28 ++++++++++++++++++++++++++++ 1 file changed, 28 insertions(+) diff --git a/spec/features/users/new_spec.rb b/spec/features/users/new_spec.rb index f72a2d65..9c0f2b55 100644 --- a/spec/features/users/new_spec.rb +++ b/spec/features/users/new_spec.rb @@ -102,5 +102,33 @@ expect(new_user.name).to eq('User_1') end end + + it 'Will not let me register if fields are missing' do + visit root_path + + within '.navbar' do + click_link('Register') + end + + fill_in 'Name', with: '' + fill_in 'Address', with: '' + fill_in 'City', with: '' + fill_in 'State', with: '' + fill_in 'Zip', with: '' + fill_in 'Email', with: 'user_1@gmail.com' + fill_in 'Password', with: 'password' + fill_in 'Confirm password', with: 'password' + + click_button 'Create User' + + expect(current_path).to eq(register_path) + expect(page).to have_content("Some required fields are missing.") + end end end +# +# As a visitor +# When I visit the user registration page +# And I do not fill in this form completely, +# I am returned to the registration page +# And I see a flash message indicating that I am missing required fields From 394cba2be4b4b6d53d970d0a0ff3b25540e3abdf Mon Sep 17 00:00:00 2001 From: Zach Leach Date: Wed, 22 May 2019 20:46:06 -0600 Subject: [PATCH 2/4] Added test to verify that form won't save if fields are blank. --- app/controllers/users_controller.rb | 2 +- app/views/users/new.html.erb | 4 ++++ spec/features/users/new_spec.rb | 14 ++++++++------ 3 files changed, 13 insertions(+), 7 deletions(-) diff --git a/app/controllers/users_controller.rb b/app/controllers/users_controller.rb index 6735fcfd..88340a9d 100644 --- a/app/controllers/users_controller.rb +++ b/app/controllers/users_controller.rb @@ -14,7 +14,7 @@ def create elsif email_confirmation == true flash.now[:notice] = "That email address is already taken." render :new - elsif @user.save! + elsif @user.save session[:user_id] = @user.id flash[:notice] = "You are now registered and logged in." redirect_to profile_path diff --git a/app/views/users/new.html.erb b/app/views/users/new.html.erb index c873c248..9e991278 100644 --- a/app/views/users/new.html.erb +++ b/app/views/users/new.html.erb @@ -1,3 +1,7 @@ +<% @user.errors.full_messages.each do |msg| %> +<%= msg %> +<% end %> + <%= form_for @user, url: register_path do |f| %>

<%= f.label :name %>

diff --git a/spec/features/users/new_spec.rb b/spec/features/users/new_spec.rb index 9c0f2b55..13f45672 100644 --- a/spec/features/users/new_spec.rb +++ b/spec/features/users/new_spec.rb @@ -111,18 +111,20 @@ end fill_in 'Name', with: '' - fill_in 'Address', with: '' - fill_in 'City', with: '' + fill_in 'Address', with: '1111 South One St.' + fill_in 'City', with: 'Denver' fill_in 'State', with: '' - fill_in 'Zip', with: '' + fill_in 'Zip', with: '80000' fill_in 'Email', with: 'user_1@gmail.com' - fill_in 'Password', with: 'password' - fill_in 'Confirm password', with: 'password' + fill_in 'Password', with: '' + fill_in 'Confirm password', with: '' click_button 'Create User' expect(current_path).to eq(register_path) - expect(page).to have_content("Some required fields are missing.") + expect(page).to have_content("Name can't be blank") + expect(page).to have_content("State can't be blank") + expect(page).to have_content("Password can't be blank") end end end From 239a340cb3d67ade1a19bacc48687abd8fd9630c Mon Sep 17 00:00:00 2001 From: Zach Leach Date: Wed, 22 May 2019 21:02:51 -0600 Subject: [PATCH 3/4] Added test to verify output of error message if credentials are bad. --- spec/features/users/login_spec.rb | 28 ++++++++++++++++++++-------- 1 file changed, 20 insertions(+), 8 deletions(-) diff --git a/spec/features/users/login_spec.rb b/spec/features/users/login_spec.rb index f1a259bf..603e3b53 100644 --- a/spec/features/users/login_spec.rb +++ b/spec/features/users/login_spec.rb @@ -51,15 +51,27 @@ expect(current_path).to eq(root_path) expect(page).to have_content('You are now logged in.') end + + it 'shows error message if credentials are bad' do + visit login_path + + fill_in 'Email', with: "reg_@gmail.com" + fill_in 'Password', with: "password" + + click_button('Login') + + save_and_open_page + + expect(current_path).to eq(login_path) + expect(page).to have_content('Those are the wrong credentials.') + end end end -# + # # As a visitor -# When I visit the login path -# I see a field to enter my email address and password -# When I submit valid information -# If I am a regular user, I am redirected to my profile page -# If I am a merchant user, I am redirected to my merchant dashboard page -# If I am an admin user, I am redirected to the home page of the site -# And I see a flash message that I am logged in +# When I visit the login page ("/login") +# And I submit valid information +# Then I am redirected to the login page +# And I see a flash message that tells me that my credentials were incorrect +# I am NOT told whether it was my email or password that was incorrect From 94eee20532be486fe43a0616f73ce226203c9b13 Mon Sep 17 00:00:00 2001 From: Zach Leach Date: Wed, 22 May 2019 21:05:54 -0600 Subject: [PATCH 4/4] Added test for login spec to show error message and verified it's working. --- app/controllers/sessions_controller.rb | 1 + 1 file changed, 1 insertion(+) diff --git a/app/controllers/sessions_controller.rb b/app/controllers/sessions_controller.rb index e70c5cc1..c38c5cbf 100644 --- a/app/controllers/sessions_controller.rb +++ b/app/controllers/sessions_controller.rb @@ -10,6 +10,7 @@ def create flash[:notice] = "You are now logged in." redirects else + flash[:notice] = "Those are the wrong credentials." render :new end end