|
| 1 | +require "test_helper" |
| 2 | + |
| 3 | +class PasswordsControllerTest < ActionDispatch::IntegrationTest |
| 4 | + setup { @user = User.take } |
| 5 | + |
| 6 | + test "new" do |
| 7 | + get new_password_path |
| 8 | + assert_response :success |
| 9 | + end |
| 10 | + |
| 11 | + test "create" do |
| 12 | + post passwords_path, params: { email_address: @user.email_address } |
| 13 | + assert_enqueued_email_with PasswordsMailer, :reset, args: [ @user ] |
| 14 | + assert_redirected_to new_session_path |
| 15 | + |
| 16 | + follow_redirect! |
| 17 | + assert_notice "reset instructions sent" |
| 18 | + end |
| 19 | + |
| 20 | + test "create for an unknown user redirects but sends no mail" do |
| 21 | + post passwords_path, params: { email_address: " [email protected]" } |
| 22 | + assert_enqueued_emails 0 |
| 23 | + assert_redirected_to new_session_path |
| 24 | + |
| 25 | + follow_redirect! |
| 26 | + assert_notice "reset instructions sent" |
| 27 | + end |
| 28 | + |
| 29 | + test "edit" do |
| 30 | + get edit_password_path(@user.password_reset_token) |
| 31 | + assert_response :success |
| 32 | + end |
| 33 | + |
| 34 | + test "edit with invalid password reset token" do |
| 35 | + get edit_password_path("invalid token") |
| 36 | + assert_redirected_to new_password_path |
| 37 | + |
| 38 | + follow_redirect! |
| 39 | + assert_notice "reset link is invalid" |
| 40 | + end |
| 41 | + |
| 42 | + test "update" do |
| 43 | + assert_changes -> { @user.reload.password_digest } do |
| 44 | + put password_path(@user.password_reset_token), params: { password: "new", password_confirmation: "new" } |
| 45 | + assert_redirected_to new_session_path |
| 46 | + end |
| 47 | + |
| 48 | + follow_redirect! |
| 49 | + assert_notice "Password has been reset" |
| 50 | + end |
| 51 | + |
| 52 | + test "update with non matching passwords" do |
| 53 | + token = @user.password_reset_token |
| 54 | + assert_no_changes -> { @user.reload.password_digest } do |
| 55 | + put password_path(token), params: { password: "no", password_confirmation: "match" } |
| 56 | + assert_redirected_to edit_password_path(token) |
| 57 | + end |
| 58 | + |
| 59 | + follow_redirect! |
| 60 | + assert_notice "Passwords did not match" |
| 61 | + end |
| 62 | + |
| 63 | + private |
| 64 | + def assert_notice(text) |
| 65 | + assert_select "div", /#{text}/ |
| 66 | + end |
| 67 | +end |
0 commit comments