Skip to content

Commit

Permalink
Inline email update form into /account
Browse files Browse the repository at this point in the history
Note there are two fields on this page labelled "Current password" so
we have to scope the Capybara selector in the system test more
specifically to avoid errors.
  • Loading branch information
chrislo committed Oct 29, 2024
1 parent 7923cc2 commit f81a88d
Show file tree
Hide file tree
Showing 10 changed files with 76 additions and 70 deletions.
2 changes: 1 addition & 1 deletion app/controllers/identity/email_verifications_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ def set_user
token = EmailVerificationToken.find_signed!(params[:sid])
@user = token.user
rescue StandardError
redirect_to edit_identity_email_path, alert: 'That email verification link is invalid'
redirect_to root_path, alert: 'That email verification link is invalid'
end

def send_email_verification
Expand Down
17 changes: 7 additions & 10 deletions app/controllers/identity/emails_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -4,19 +4,16 @@ module Identity
class EmailsController < ApplicationController
before_action :set_user

def edit
authorize @user
end

def update
authorize @user

if !@user.authenticate(params[:current_password])
redirect_to edit_identity_email_path, alert: 'The password you entered is incorrect'
flash[:emails_update_password_incorrect] = 'The password you entered is incorrect'
redirect_to account_path
elsif @user.update(email: params[:email])
redirect_to_root
resend_verification_email_and_redirect
else
render :edit, status: :unprocessable_entity
render 'users/show', status: :unprocessable_entity
end
end

Expand All @@ -26,12 +23,12 @@ def set_user
@user = Current.user
end

def redirect_to_root
def resend_verification_email_and_redirect
if @user.email_previously_changed?
resend_email_verification
redirect_to root_path, notice: 'Your email has been changed'
redirect_to account_path, notice: 'Your email has been changed'
else
redirect_to root_path
redirect_to account_path
end
end

Expand Down
21 changes: 0 additions & 21 deletions app/views/identity/emails/edit.html.erb

This file was deleted.

22 changes: 20 additions & 2 deletions app/views/users/show.html.erb
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,26 @@
<h2 class="text-lg font-bold">Email address</h2>
</div>
<div class="basis-3/4">
<p>form goes here</p>
<% unless Current.user.verified? %>
<div class="mb-3 border border-slate-300 p-4">
<p>We sent a verification email to the address below. Check that email and follow those instructions to confirm it's your email address.</p>
<p><%= button_to "Re-send verification email", identity_email_verification_path, class: 'py-3 px-5 bg-amber-600 hover:bg-amber-500 text-white font-medium cursor-pointer my-3' %></p>
</div>
<% end %>

<%= form_with(url: identity_email_path, method: :patch, builder: TailwindFormBuilder) do |form| %>
<% if flash[:emails_update_password_incorrect] %>
<%= render ErrorBoxComponent.new(title: 'Incorrect password').with_content(flash[:emails_update_password_incorrect]) %>
<% end %>
<% if @user.errors.include?(:email) %>
<%= render ModelErrorComponent.new(model: @user) %>
<% end %>
<%= form.email_field :email, label: { text: "New email" }, required: true, autofocus: true, class: 'w-full mb-3' %>
<%= form.password_field :current_password, required: true, autocomplete: "current-password", class: 'w-full mb-3' %>
<div class="flex flex-row justify-end">
<%= form.submit "Save changes", class: 'mt-3' %>
</div>
<% end %>
</div>
</section>
<section class="flex flex-row py-6">
Expand All @@ -41,7 +60,6 @@
</div>

<ul>
<li><%= text_link_to "Change email address", edit_identity_email_path %></li>
<% if Current.user.payout_detail %>
<li><%= text_link_to "Edit payout details", edit_payout_detail_path %></li>
<% else %>
Expand Down
2 changes: 1 addition & 1 deletion config/routes.rb
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@
resolve('PayoutDetail') { [:payout_detail] }

namespace :identity do
resource :email, only: %i[edit update]
resource :email, only: %i[update]
resource :email_verification, only: %i[show create]
resource :password_reset, only: %i[new edit create update]
end
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ class EmailVerificationsControllerTest < ActionDispatch::IntegrationTest

get identity_email_verification_url(sid: sid_exp, email: @user.email)

assert_redirected_to edit_identity_email_url
assert_redirected_to root_url
assert_equal 'That email verification link is invalid', flash[:alert]
end
end
Expand Down
11 changes: 3 additions & 8 deletions test/controllers/identity/emails_controller_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -8,21 +8,16 @@ class EmailsControllerTest < ActionDispatch::IntegrationTest
@user = log_in_as(create(:user))
end

test 'should get edit' do
get edit_identity_email_url
assert_response :success
end

test 'should update email' do
patch identity_email_url, params: { email: '[email protected]', current_password: 'Secret1*3*5*' }
assert_redirected_to root_url
assert_redirected_to account_path
end

test 'should not update email with wrong current password' do
patch identity_email_url, params: { email: '[email protected]', current_password: 'SecretWrong1*3' }

assert_redirected_to edit_identity_email_url
assert_equal 'The password you entered is incorrect', flash[:alert]
assert_redirected_to account_path
assert_equal 'The password you entered is incorrect', flash[:emails_update_password_incorrect]
end
end
end
5 changes: 0 additions & 5 deletions test/controllers/users_controller_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -12,11 +12,6 @@ class UsersControllerTestSignedIn < ActionDispatch::IntegrationTest
get account_url
assert_response :success
end

test '#show has a link to change email address' do
get account_url
assert_select 'a', href: edit_identity_email_path
end
end

class UserControllerTestSignedOut < ActionDispatch::IntegrationTest
Expand Down
38 changes: 25 additions & 13 deletions test/system/identity/emails_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -9,27 +9,31 @@ class EmailsTest < ApplicationSystemTestCase
end

test 'when I update my email address I should be prompted to verify it' do
visit edit_identity_email_path
visit account_path

fill_in 'New email', with: '[email protected]'
fill_in 'Current password', with: 'Secret1*3*5*'
click_on 'Save changes'
within(email_address_section) do
fill_in 'New email', with: '[email protected]'
fill_in 'Current password', with: 'Secret1*3*5*'
click_on 'Save changes'
end

assert_text 'Your email has been changed'

visit edit_identity_email_path
visit account_path
assert_text 'We sent a verification email to the address below'

click_on 'Re-send verification email'
assert_text 'We sent a verification email to your email address'
end

test 'updating my email address fails if my current password is wrong' do
visit edit_identity_email_path
visit account_path

fill_in 'New email', with: '[email protected]'
fill_in 'Current password', with: 'wrongpassword'
click_on 'Save changes'
within(email_address_section) do
fill_in 'New email', with: '[email protected]'
fill_in 'Current password', with: 'wrongpassword'
click_on 'Save changes'
end

assert_text 'The password you entered is incorrect'
refute_text 'We sent a verification email to the address below'
Expand All @@ -38,14 +42,22 @@ class EmailsTest < ApplicationSystemTestCase
test 'updating my email address fails if I use an existing email' do
existing_user = create(:user)

visit edit_identity_email_path
visit account_path

fill_in 'New email', with: existing_user.email
fill_in 'Current password', with: 'Secret1*3*5*'
click_on 'Save changes'
within(email_address_section) do
fill_in 'New email', with: existing_user.email
fill_in 'Current password', with: 'Secret1*3*5*'
click_on 'Save changes'
end

assert_text 'Email has already been taken'
refute_text 'We sent a verification email to the address below'
end

private

def email_address_section
find('h2', text: 'Email address').ancestor('section')
end
end
end
26 changes: 18 additions & 8 deletions test/system/passwords_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -10,10 +10,12 @@ class PasswordsTest < ApplicationSystemTestCase
test 'updating the password' do
visit account_path

fill_in 'Current password', with: 'Secret1*3*5*'
fill_in 'New password', with: 'Secret6*4*2*'
fill_in 'Confirm new password', with: 'Secret6*4*2*'
click_on 'Save changes'
within(password_section) do
fill_in 'Current password', with: 'Secret1*3*5*'
fill_in 'New password', with: 'Secret6*4*2*'
fill_in 'Confirm new password', with: 'Secret6*4*2*'
click_on 'Save changes'
end

assert_text 'Your password has been changed'
assert_current_path account_path
Expand All @@ -22,12 +24,20 @@ class PasswordsTest < ApplicationSystemTestCase
test 'when password is too short' do
visit account_path

fill_in 'Current password', with: 'Secret1*3*5*'
fill_in 'New password', with: 'short'
fill_in 'Confirm new password', with: 'short'
click_on 'Save changes'
within(password_section) do
fill_in 'Current password', with: 'Secret1*3*5*'
fill_in 'New password', with: 'short'
fill_in 'Confirm new password', with: 'short'
click_on 'Save changes'
end

assert_text 'Password is too short'
assert_current_path account_path
end

private

def password_section
find('h2', text: 'Password').ancestor('section')
end
end

0 comments on commit f81a88d

Please sign in to comment.