Skip to content

Commit 3eb5f34

Browse files
codergeek121dhh
andauthored
Add PasswordsController tests for the authentication generator (rails#53255)
* Add PasswordsController tests for the authentication generator * Fix style and presentation * Add assert_notice helper --------- Co-authored-by: David Heinemeier Hansson <[email protected]>
1 parent c7162d6 commit 3eb5f34

File tree

3 files changed

+72
-0
lines changed

3 files changed

+72
-0
lines changed

railties/lib/rails/generators/test_unit/authentication/authentication_generator.rb

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,10 @@ def create_user_test_files
99
template "test/fixtures/users.yml"
1010
template "test/models/user_test.rb"
1111
end
12+
13+
def create_controller_test_files
14+
template "test/controllers/passwords_controller_test.rb"
15+
end
1216
end
1317
end
1418
end
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,67 @@
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

railties/test/generators/authentication_generator_test.rb

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -60,6 +60,7 @@ def test_authentication_generator
6060

6161
assert_file "test/models/user_test.rb"
6262
assert_file "test/fixtures/users.yml"
63+
assert_file "test/controllers/passwords_controller_test.rb"
6364

6465
assert_file "test/test_helper.rb" do |content|
6566
assert_match(/session_test_helper/, content)

0 commit comments

Comments
 (0)