Skip to content

Commit 1dba10c

Browse files
jeromedalbertdhh
andauthored
Generate session controller tests for auth generator (rails#53726)
* Generate session controller tests for auth generator * Use paths for everything --------- Co-authored-by: David Heinemeier Hansson <[email protected]>
1 parent 049d1af commit 1dba10c

File tree

4 files changed

+39
-0
lines changed

4 files changed

+39
-0
lines changed

railties/CHANGELOG.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,7 @@
1+
* Generate session controller tests when running the authentication generator.
2+
3+
*Jerome Dalbert*
4+
15
* Add bin/bundler-audit and config/bundler-audit.yml for discovering and managing known security problems with app gems.
26

37
*DHH*

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

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ def create_user_test_files
1111
end
1212

1313
def create_controller_test_files
14+
template "test/controllers/sessions_controller_test.rb"
1415
template "test/controllers/passwords_controller_test.rb"
1516
end
1617
end
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
require "test_helper"
2+
3+
class SessionsControllerTest < ActionDispatch::IntegrationTest
4+
setup { @user = User.take }
5+
6+
test "new" do
7+
get new_session_path
8+
assert_response :success
9+
end
10+
11+
test "create with valid credentials" do
12+
post session_path, params: { email_address: @user.email_address, password: "password" }
13+
14+
assert_redirected_to root_path
15+
assert cookies[:session_id]
16+
end
17+
18+
test "create with invalid credentials" do
19+
post session_path, params: { email_address: @user.email_address, password: "wrong" }
20+
21+
assert_redirected_to new_session_path
22+
assert_nil cookies[:session_id]
23+
end
24+
25+
test "destroy" do
26+
sign_in_as(User.take)
27+
28+
delete session_path
29+
30+
assert_redirected_to new_session_path
31+
assert_empty cookies[:session_id]
32+
end
33+
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/sessions_controller_test.rb"
6364
assert_file "test/controllers/passwords_controller_test.rb"
6465

6566
assert_file "test/test_helper.rb" do |content|

0 commit comments

Comments
 (0)