Update and test password recovery forms

This commit is contained in:
cryptogopher 2023-04-19 19:29:12 +02:00
parent 894c21f322
commit 7dabbd3036
7 changed files with 43 additions and 32 deletions

View File

@ -12,6 +12,7 @@ module ApplicationHelper
end
def label_for(method, options = {})
return "" if (options[:label] == false)
text = options.delete(:label)
text ||= @object.class.human_attribute_name(method).capitalize

View File

@ -1,25 +1,13 @@
<h2>Change your password</h2>
<%= form_for(resource, as: resource_name, url: password_path(resource_name), html: { method: :put }) do |f| %>
<%= tabular_form_for resource, url: user_password_path, html: { method: :put } do |f| %>
<%= render "users/shared/error_messages", resource: resource %>
<%= f.hidden_field :reset_password_token %>
<div class="field">
<%= f.label :password, "New password" %><br />
<% if @minimum_password_length %>
<em>(<%= @minimum_password_length %> characters minimum)</em><br />
<% end %>
<%= f.password_field :password, autofocus: true, autocomplete: "new-password" %>
</div>
<%= f.hidden_field :reset_password_token, label: false %>
<div class="field">
<%= f.label :password_confirmation, "Confirm new password" %><br />
<%= f.password_field :password_confirmation, autocomplete: "new-password" %>
</div>
<%= f.password_field :password, label: t('.new_password'),
required: true, size: 30, autofocus: true, autocomplete: "new-password",
hint: ("(#{@minimum_password_length} characters minimum)" if @minimum_password_length) %>
<%= f.password_field :password_confirmation, label: t('.password_confirmation'),
required: true, size: 30, autocomplete: "new-password" %>
<div class="actions">
<%= f.submit "Change my password" %>
</div>
<%= f.submit t('.update_password') %>
<% end %>
<%= render "users/shared/links" %>

View File

@ -1,16 +1,7 @@
<h2>Forgot your password?</h2>
<%= form_for(resource, as: resource_name, url: password_path(resource_name), html: { method: :post }) do |f| %>
<%= tabular_form_for resource, url: user_password_path, html: { method: :post } do |f| %>
<%= render "users/shared/error_messages", resource: resource %>
<div class="field">
<%= f.label :email %><br />
<%= f.email_field :email, autofocus: true, autocomplete: "email" %>
</div>
<%= f.email_field :email, required: true, size: 30, autofocus: true, autocomplete: "email" %>
<div class="actions">
<%= f.submit "Send me reset password instructions" %>
</div>
<%= f.submit t(:recover_password) %>
<% end %>
<%= render "users/shared/links" %>

View File

@ -10,6 +10,7 @@
<%= f.submit t(:register) %>
<% end %>
<%= content_tag :p, t(:or), style: "text-align: center;" %>
<%= image_link_to t(:resend_confirmation), "email-sync-outline", new_user_confirmation_path,
class: "centered" %>

View File

@ -8,5 +8,6 @@
<%= f.submit t(:sign_in) %>
<% end %>
<%= content_tag :p, t(:or), style: "text-align: center;" %>
<%= image_link_to t(:recover_password), "lock-reset", new_user_password_path, class: "centered" %>

View File

@ -5,6 +5,11 @@ en:
email: e-mail
password: password
users:
passwords:
edit:
new_password: New password
password_confirmation: Retype new password
update_password: Update password
registrations:
new:
password_confirmation: Retype password

View File

@ -49,4 +49,28 @@ class UsersTest < ApplicationSystemTestCase
assert_text t('devise.confirmations.confirmed')
assert User.last.confirmed?
end
test "recover password" do
visit new_user_session_url
click_link t(:recover_password)
fill_in User.human_attribute_name(:email).capitalize, with: users.sample.email
assert_emails 1 do
click_on t(:recover_password)
end
assert_current_path new_user_session_path
assert_text t('devise.passwords.send_instructions')
with_last_email do |mail|
visit Capybara.string(mail.body.to_s).find_link("Change my password")[:href]
end
new_password = random_password
fill_in t('users.passwords.edit.new_password'), with: new_password
fill_in t('users.passwords.edit.password_confirmation'), with: new_password
assert_emails 1 do
click_on t('users.passwords.edit.update_password')
end
assert_no_current_path user_password_path
assert_text t('devise.passwords.updated')
end
end