From: Andy Allan Date: Wed, 10 Mar 2021 14:14:36 +0000 (+0000) Subject: Use login_path instead of explicit controller and actions X-Git-Tag: live~2241^2 X-Git-Url: https://git.openstreetmap.org./rails.git/commitdiff_plain/38ad8fbc36f1092b8b0b416e5c2c80ee7825f113?hp=2233edbcfa15d532c2232e5e71a2a2495e3a72eb Use login_path instead of explicit controller and actions This makes future refactoring easier. --- diff --git a/.rubocop_todo.yml b/.rubocop_todo.yml index 219135dff..cac193504 100644 --- a/.rubocop_todo.yml +++ b/.rubocop_todo.yml @@ -52,7 +52,7 @@ Lint/DuplicateBranch: # Offense count: 487 # Configuration parameters: IgnoredMethods, CountRepeatedAttributes. Metrics/AbcSize: - Max: 234 + Max: 235 # Offense count: 62 # Configuration parameters: CountComments, CountAsOne, ExcludedMethods, IgnoredMethods. diff --git a/app/controllers/application_controller.rb b/app/controllers/application_controller.rb index 586b18116..c3eb1ad85 100644 --- a/app/controllers/application_controller.rb +++ b/app/controllers/application_controller.rb @@ -59,7 +59,7 @@ class ApplicationController < ActionController::Base def require_user unless current_user if request.get? - redirect_to :controller => "users", :action => "login", :referer => request.fullpath + redirect_to login_path(:referer => request.fullpath) else head :forbidden end @@ -356,7 +356,7 @@ class ApplicationController < ActionController::Base end elsif request.get? respond_to do |format| - format.html { redirect_to :controller => "users", :action => "login", :referer => request.fullpath } + format.html { redirect_to login_path(:referer => request.fullpath) } format.any { head :forbidden } end else diff --git a/app/controllers/messages_controller.rb b/app/controllers/messages_controller.rb index 2047c4614..dea5c3b07 100644 --- a/app/controllers/messages_controller.rb +++ b/app/controllers/messages_controller.rb @@ -57,7 +57,7 @@ class MessagesController < ApplicationController render :action => "new" else flash[:notice] = t ".wrong_user", :user => current_user.display_name - redirect_to :controller => "users", :action => "login", :referer => request.fullpath + redirect_to login_path(:referer => request.fullpath) end rescue ActiveRecord::RecordNotFound @title = t "messages.no_such_message.title" @@ -74,7 +74,7 @@ class MessagesController < ApplicationController @message.save else flash[:notice] = t ".wrong_user", :user => current_user.display_name - redirect_to :controller => "users", :action => "login", :referer => request.fullpath + redirect_to login_path(:referer => request.fullpath) end rescue ActiveRecord::RecordNotFound @title = t "messages.no_such_message.title" diff --git a/app/controllers/users_controller.rb b/app/controllers/users_controller.rb index 121d050b5..cc8f46d23 100644 --- a/app/controllers/users_controller.rb +++ b/app/controllers/users_controller.rb @@ -164,7 +164,7 @@ class UsersController < ApplicationController token = user.tokens.create UserMailer.lost_password(user, token).deliver_later flash[:notice] = t "users.lost_password.notice email on way" - redirect_to :action => "login" + redirect_to login_path else flash.now[:error] = t "users.lost_password.notice email cannot find" end @@ -306,7 +306,7 @@ class UsersController < ApplicationController token = UserToken.find_by(:token => params[:confirm_string]) if token&.user&.active? flash[:error] = t("users.confirm.already active") - redirect_to :action => "login" + redirect_to login_path elsif !token || token.expired? flash[:error] = t("users.confirm.unknown token") redirect_to :action => "confirm" @@ -328,7 +328,7 @@ class UsersController < ApplicationController if token.nil? || token.user != user flash[:notice] = t("users.confirm.success") - redirect_to :action => :login, :referer => referer + redirect_to login_path(:referer => referer) else token.destroy @@ -356,7 +356,7 @@ class UsersController < ApplicationController flash[:notice] = t "users.confirm_resend.success_html", :email => user.email, :sender => Settings.support_email end - redirect_to :action => "login" + redirect_to login_path end def confirm_email diff --git a/app/views/browse/changeset.html.erb b/app/views/browse/changeset.html.erb index 670c7dc32..cbc457d30 100644 --- a/app/views/browse/changeset.html.erb +++ b/app/views/browse/changeset.html.erb @@ -64,7 +64,7 @@ <% unless current_user %>
- <%= link_to(t(".join_discussion"), :controller => "users", :action => "login", :referer => request.fullpath) %> + <%= link_to(t(".join_discussion"), login_path(:referer => request.fullpath)) %>
<% end %> diff --git a/app/views/diary_entries/show.html.erb b/app/views/diary_entries/show.html.erb index 154a7a195..2b7245356 100644 --- a/app/views/diary_entries/show.html.erb +++ b/app/views/diary_entries/show.html.erb @@ -29,7 +29,7 @@ <% end %> <% end %> <% else %> -

<%= t(".login_to_leave_a_comment_html", :login_link => link_to(t(".login"), :controller => "users", :action => "login", :referer => request.fullpath)) %>

+

<%= t(".login_to_leave_a_comment_html", :login_link => link_to(t(".login"), login_path(:referer => request.fullpath))) %>

<% end %> diff --git a/test/controllers/changesets_controller_test.rb b/test/controllers/changesets_controller_test.rb index fbbb36f3f..31a1e2d0c 100644 --- a/test/controllers/changesets_controller_test.rb +++ b/test/controllers/changesets_controller_test.rb @@ -164,7 +164,7 @@ class ChangesetsControllerTest < ActionDispatch::IntegrationTest get friend_changesets_path assert_response :redirect - assert_redirected_to :controller => :users, :action => :login, :referer => friend_changesets_path + assert_redirected_to login_path(:referer => friend_changesets_path) session_for(private_user) @@ -190,7 +190,7 @@ class ChangesetsControllerTest < ActionDispatch::IntegrationTest get nearby_changesets_path assert_response :redirect - assert_redirected_to :controller => :users, :action => :login, :referer => nearby_changesets_path + assert_redirected_to login_path(:referer => nearby_changesets_path) session_for(private_user) diff --git a/test/controllers/diary_entries_controller_test.rb b/test/controllers/diary_entries_controller_test.rb index 868171752..45fe4add1 100644 --- a/test/controllers/diary_entries_controller_test.rb +++ b/test/controllers/diary_entries_controller_test.rb @@ -113,7 +113,7 @@ class DiaryEntriesControllerTest < ActionDispatch::IntegrationTest # are not logged in get new_diary_entry_path assert_response :redirect - assert_redirected_to :controller => :users, :action => :login, :referer => "/diary/new" + assert_redirected_to login_path(:referer => "/diary/new") end def test_new_form @@ -257,7 +257,7 @@ class DiaryEntriesControllerTest < ActionDispatch::IntegrationTest # not logged in, without and with the id of the entry you want to edit get edit_diary_entry_path(:display_name => entry.user.display_name, :id => entry) assert_response :redirect - assert_redirected_to :controller => :users, :action => :login, :referer => "/user/#{ERB::Util.u(entry.user.display_name)}/diary/#{entry.id}/edit" + assert_redirected_to login_path(:referer => "/user/#{ERB::Util.u(entry.user.display_name)}/diary/#{entry.id}/edit") session_for(other_user) @@ -506,7 +506,7 @@ class DiaryEntriesControllerTest < ActionDispatch::IntegrationTest # Try a list of diary entries for your friends when not logged in get friends_diary_entries_path assert_response :redirect - assert_redirected_to :controller => :users, :action => :login, :referer => "/diary/friends" + assert_redirected_to login_path(:referer => "/diary/friends") # Try a list of diary entries for your friends when logged in session_for(user) @@ -526,7 +526,7 @@ class DiaryEntriesControllerTest < ActionDispatch::IntegrationTest # Try a list of diary entries for nearby users when not logged in get nearby_diary_entries_path assert_response :redirect - assert_redirected_to :controller => :users, :action => :login, :referer => "/diary/nearby" + assert_redirected_to login_path(:referer => "/diary/nearby") # Try a list of diary entries for nearby users when logged in session_for(nearby_user) diff --git a/test/controllers/site_controller_test.rb b/test/controllers/site_controller_test.rb index 4d08a8d0c..6f6e7dcfa 100644 --- a/test/controllers/site_controller_test.rb +++ b/test/controllers/site_controller_test.rb @@ -178,7 +178,7 @@ class SiteControllerTest < ActionDispatch::IntegrationTest get edit_path assert_response :redirect - assert_redirected_to :controller => :users, :action => :login, :referer => "/edit" + assert_redirected_to login_path(:referer => "/edit") end # Test the error when trying to edit without public edits @@ -452,7 +452,7 @@ class SiteControllerTest < ActionDispatch::IntegrationTest def test_welcome get welcome_path assert_response :redirect - assert_redirected_to :controller => :users, :action => :login, :referer => "/welcome" + assert_redirected_to login_path(:referer => "/welcome") session_for(create(:user)) get welcome_path diff --git a/test/controllers/traces_controller_test.rb b/test/controllers/traces_controller_test.rb index 36bee9c95..9ced73414 100644 --- a/test/controllers/traces_controller_test.rb +++ b/test/controllers/traces_controller_test.rb @@ -178,7 +178,7 @@ class TracesControllerTest < ActionDispatch::IntegrationTest # First try to get it when not logged in get traces_mine_path - assert_redirected_to :controller => "users", :action => "login", :referer => "/traces/mine" + assert_redirected_to login_path(:referer => "/traces/mine") session_for(user) @@ -541,7 +541,7 @@ class TracesControllerTest < ActionDispatch::IntegrationTest # First with no auth get new_trace_path assert_response :redirect - assert_redirected_to :controller => :users, :action => :login, :referer => new_trace_path + assert_redirected_to login_path(:referer => new_trace_path) # Now authenticated as a user with gps.trace.visibility set user = create(:user) @@ -627,7 +627,7 @@ class TracesControllerTest < ActionDispatch::IntegrationTest # First with no auth get edit_trace_path(:display_name => public_trace_file.user.display_name, :id => public_trace_file) assert_response :redirect - assert_redirected_to :controller => :users, :action => :login, :referer => edit_trace_path(:display_name => public_trace_file.user.display_name, :id => public_trace_file.id) + assert_redirected_to login_path(:referer => edit_trace_path(:display_name => public_trace_file.user.display_name, :id => public_trace_file.id)) # Now with some other user, which should fail session_for(create(:user)) diff --git a/test/controllers/users_controller_test.rb b/test/controllers/users_controller_test.rb index 54c737b97..ff87c9466 100644 --- a/test/controllers/users_controller_test.rb +++ b/test/controllers/users_controller_test.rb @@ -602,7 +602,7 @@ class UsersControllerTest < ActionDispatch::IntegrationTest confirm_string = User.find_by(:email => user.email).tokens.create(:referer => new_diary_entry_path).token post user_confirm_path, :params => { :display_name => user.display_name, :confirm_string => confirm_string } - assert_redirected_to :action => "login" + assert_redirected_to login_path assert_match(/already been confirmed/, flash[:error]) end @@ -847,7 +847,7 @@ class UsersControllerTest < ActionDispatch::IntegrationTest end end assert_response :redirect - assert_redirected_to :action => :login + assert_redirected_to login_path assert_match(/^Sorry you lost it/, flash[:notice]) email = ActionMailer::Base.deliveries.first assert_equal 1, email.to.count @@ -862,7 +862,7 @@ class UsersControllerTest < ActionDispatch::IntegrationTest end end assert_response :redirect - assert_redirected_to :action => :login + assert_redirected_to login_path assert_match(/^Sorry you lost it/, flash[:notice]) email = ActionMailer::Base.deliveries.first assert_equal 1, email.to.count @@ -889,7 +889,7 @@ class UsersControllerTest < ActionDispatch::IntegrationTest end end assert_response :redirect - assert_redirected_to :action => :login + assert_redirected_to login_path assert_match(/^Sorry you lost it/, flash[:notice]) email = ActionMailer::Base.deliveries.first assert_equal 1, email.to.count @@ -904,7 +904,7 @@ class UsersControllerTest < ActionDispatch::IntegrationTest end end assert_response :redirect - assert_redirected_to :action => :login + assert_redirected_to login_path assert_match(/^Sorry you lost it/, flash[:notice]) email = ActionMailer::Base.deliveries.first assert_equal 1, email.to.count @@ -960,7 +960,7 @@ class UsersControllerTest < ActionDispatch::IntegrationTest # you are not logged in get user_account_path(user) assert_response :redirect - assert_redirected_to :action => "login", :referer => "/user/#{ERB::Util.u(user.display_name)}/account" + assert_redirected_to login_path(:referer => "/user/#{ERB::Util.u(user.display_name)}/account") # Make sure that you are blocked when not logged in as the right user session_for(create(:user)) @@ -1336,7 +1336,7 @@ class UsersControllerTest < ActionDispatch::IntegrationTest # Shouldn't work when not logged in get users_path assert_response :redirect - assert_redirected_to :action => :login, :referer => users_path + assert_redirected_to login_path(:referer => users_path) session_for(user) diff --git a/test/integration/client_applications_test.rb b/test/integration/client_applications_test.rb index 5a00a5631..f8055ebc7 100644 --- a/test/integration/client_applications_test.rb +++ b/test/integration/client_applications_test.rb @@ -9,7 +9,7 @@ class ClientApplicationsTest < ActionDispatch::IntegrationTest get "/login" assert_response :redirect - assert_redirected_to "controller" => "users", "action" => "login", "cookie_test" => "true" + assert_redirected_to login_path(:cookie_test => "true") follow_redirect! assert_response :success post "/login", :params => { "username" => user.email, "password" => "test", :referer => "/user/#{ERB::Util.u(user.display_name)}" } diff --git a/test/integration/user_login_test.rb b/test/integration/user_login_test.rb index 149875def..509d2525c 100644 --- a/test/integration/user_login_test.rb +++ b/test/integration/user_login_test.rb @@ -355,7 +355,7 @@ class UserLoginTest < ActionDispatch::IntegrationTest get "/login", :params => { :referer => "/history" } assert_response :redirect - assert_redirected_to :controller => :users, :action => :login, :cookie_test => true, :referer => "/history" + assert_redirected_to login_path(:cookie_test => true, :referer => "/history") follow_redirect! assert_response :success assert_template "users/login" @@ -376,7 +376,7 @@ class UserLoginTest < ActionDispatch::IntegrationTest get "/login", :params => { :referer => "/history" } assert_response :redirect - assert_redirected_to :controller => :users, :action => :login, :cookie_test => true, :referer => "/history" + assert_redirected_to login_path(:cookie_test => true, :referer => "/history") follow_redirect! assert_response :success assert_template "users/login" @@ -401,7 +401,7 @@ class UserLoginTest < ActionDispatch::IntegrationTest get "/login", :params => { :referer => "/history" } assert_response :redirect - assert_redirected_to :controller => :users, :action => :login, :cookie_test => true, :referer => "/history" + assert_redirected_to login_path(:cookie_test => true, :referer => "/history") follow_redirect! assert_response :success assert_template "users/login" @@ -425,7 +425,7 @@ class UserLoginTest < ActionDispatch::IntegrationTest get "/login", :params => { :referer => "/history" } assert_response :redirect - assert_redirected_to :controller => :users, :action => :login, :cookie_test => true, :referer => "/history" + assert_redirected_to login_path(:cookie_test => true, :referer => "/history") follow_redirect! assert_response :success assert_template "users/login" @@ -448,7 +448,7 @@ class UserLoginTest < ActionDispatch::IntegrationTest get "/login", :params => { :referer => "/history" } assert_response :redirect - assert_redirected_to "controller" => "users", "action" => "login", "cookie_test" => "true", "referer" => "/history" + assert_redirected_to login_path("cookie_test" => "true", "referer" => "/history") follow_redirect! assert_response :success assert_template "users/login" @@ -468,7 +468,7 @@ class UserLoginTest < ActionDispatch::IntegrationTest get "/login", :params => { :referer => "/history" } assert_response :redirect - assert_redirected_to "controller" => "users", "action" => "login", "cookie_test" => "true", "referer" => "/history" + assert_redirected_to login_path("cookie_test" => "true", "referer" => "/history") follow_redirect! assert_response :success assert_template "users/login" @@ -492,7 +492,7 @@ class UserLoginTest < ActionDispatch::IntegrationTest get "/login", :params => { :referer => "/history" } assert_response :redirect - assert_redirected_to "controller" => "users", "action" => "login", "cookie_test" => "true", "referer" => "/history" + assert_redirected_to login_path("cookie_test" => "true", "referer" => "/history") follow_redirect! assert_response :success assert_template "users/login" @@ -518,7 +518,7 @@ class UserLoginTest < ActionDispatch::IntegrationTest get "/login", :params => { :referer => "/history" } assert_response :redirect - assert_redirected_to "controller" => "users", "action" => "login", "cookie_test" => "true", "referer" => "/history" + assert_redirected_to login_path("cookie_test" => "true", "referer" => "/history") follow_redirect! assert_response :success assert_template "users/login" @@ -541,7 +541,7 @@ class UserLoginTest < ActionDispatch::IntegrationTest get "/login", :params => { :referer => "/history" } assert_response :redirect - assert_redirected_to "controller" => "users", "action" => "login", "cookie_test" => "true", "referer" => "/history" + assert_redirected_to login_path("cookie_test" => "true", "referer" => "/history") follow_redirect! assert_response :success assert_template "users/login" @@ -566,7 +566,7 @@ class UserLoginTest < ActionDispatch::IntegrationTest get "/login", :params => { :referer => "/history" } assert_response :redirect - assert_redirected_to "controller" => "users", "action" => "login", "cookie_test" => "true", "referer" => "/history" + assert_redirected_to login_path("cookie_test" => "true", "referer" => "/history") follow_redirect! assert_response :success assert_template "users/login" @@ -586,7 +586,7 @@ class UserLoginTest < ActionDispatch::IntegrationTest get "/login", :params => { :referer => "/history" } assert_response :redirect - assert_redirected_to "controller" => "users", "action" => "login", "cookie_test" => "true", "referer" => "/history" + assert_redirected_to login_path("cookie_test" => "true", "referer" => "/history") follow_redirect! assert_response :success assert_template "users/login" @@ -610,7 +610,7 @@ class UserLoginTest < ActionDispatch::IntegrationTest get "/login", :params => { :referer => "/history" } assert_response :redirect - assert_redirected_to "controller" => "users", "action" => "login", "cookie_test" => "true", "referer" => "/history" + assert_redirected_to login_path("cookie_test" => "true", "referer" => "/history") follow_redirect! assert_response :success assert_template "users/login" @@ -634,7 +634,7 @@ class UserLoginTest < ActionDispatch::IntegrationTest get "/login", :params => { :referer => "/history" } assert_response :redirect - assert_redirected_to "controller" => "users", "action" => "login", "cookie_test" => "true", "referer" => "/history" + assert_redirected_to login_path("cookie_test" => "true", "referer" => "/history") follow_redirect! assert_response :success assert_template "users/login" @@ -655,7 +655,7 @@ class UserLoginTest < ActionDispatch::IntegrationTest get "/login", :params => { :referer => "/history" } assert_response :redirect - assert_redirected_to "controller" => "users", "action" => "login", "cookie_test" => "true", "referer" => "/history" + assert_redirected_to login_path("cookie_test" => "true", "referer" => "/history") follow_redirect! assert_response :success assert_template "users/login" @@ -675,7 +675,7 @@ class UserLoginTest < ActionDispatch::IntegrationTest get "/login", :params => { :referer => "/history" } assert_response :redirect - assert_redirected_to "controller" => "users", "action" => "login", "cookie_test" => "true", "referer" => "/history" + assert_redirected_to login_path("cookie_test" => "true", "referer" => "/history") follow_redirect! assert_response :success assert_template "users/login" @@ -699,7 +699,7 @@ class UserLoginTest < ActionDispatch::IntegrationTest get "/login", :params => { :referer => "/history" } assert_response :redirect - assert_redirected_to "controller" => "users", "action" => "login", "cookie_test" => "true", "referer" => "/history" + assert_redirected_to login_path("cookie_test" => "true", "referer" => "/history") follow_redirect! assert_response :success assert_template "users/login" @@ -723,7 +723,7 @@ class UserLoginTest < ActionDispatch::IntegrationTest get "/login", :params => { :referer => "/history" } assert_response :redirect - assert_redirected_to "controller" => "users", "action" => "login", "cookie_test" => "true", "referer" => "/history" + assert_redirected_to login_path("cookie_test" => "true", "referer" => "/history") follow_redirect! assert_response :success assert_template "users/login" @@ -744,7 +744,7 @@ class UserLoginTest < ActionDispatch::IntegrationTest get "/login", :params => { :referer => "/history" } assert_response :redirect - assert_redirected_to "controller" => "users", "action" => "login", "cookie_test" => "true", "referer" => "/history" + assert_redirected_to login_path("cookie_test" => "true", "referer" => "/history") follow_redirect! assert_response :success assert_template "users/login" @@ -764,7 +764,7 @@ class UserLoginTest < ActionDispatch::IntegrationTest get "/login", :params => { :referer => "/history" } assert_response :redirect - assert_redirected_to "controller" => "users", "action" => "login", "cookie_test" => "true", "referer" => "/history" + assert_redirected_to login_path("cookie_test" => "true", "referer" => "/history") follow_redirect! assert_response :success assert_template "users/login" @@ -788,7 +788,7 @@ class UserLoginTest < ActionDispatch::IntegrationTest get "/login", :params => { :referer => "/history" } assert_response :redirect - assert_redirected_to "controller" => "users", "action" => "login", "cookie_test" => "true", "referer" => "/history" + assert_redirected_to login_path("cookie_test" => "true", "referer" => "/history") follow_redirect! assert_response :success assert_template "users/login" @@ -812,7 +812,7 @@ class UserLoginTest < ActionDispatch::IntegrationTest get "/login", :params => { :referer => "/history" } assert_response :redirect - assert_redirected_to "controller" => "users", "action" => "login", "cookie_test" => "true", "referer" => "/history" + assert_redirected_to login_path("cookie_test" => "true", "referer" => "/history") follow_redirect! assert_response :success assert_template "users/login" @@ -833,7 +833,7 @@ class UserLoginTest < ActionDispatch::IntegrationTest get "/login", :params => { :referer => "/history" } assert_response :redirect - assert_redirected_to "controller" => "users", "action" => "login", "cookie_test" => "true", "referer" => "/history" + assert_redirected_to login_path("cookie_test" => "true", "referer" => "/history") follow_redirect! assert_response :success assert_template "users/login" @@ -853,7 +853,7 @@ class UserLoginTest < ActionDispatch::IntegrationTest get "/login", :params => { :referer => "/history" } assert_response :redirect - assert_redirected_to "controller" => "users", "action" => "login", "cookie_test" => "true", "referer" => "/history" + assert_redirected_to login_path("cookie_test" => "true", "referer" => "/history") follow_redirect! assert_response :success assert_template "users/login" @@ -877,7 +877,7 @@ class UserLoginTest < ActionDispatch::IntegrationTest get "/login", :params => { :referer => "/history" } assert_response :redirect - assert_redirected_to "controller" => "users", "action" => "login", "cookie_test" => "true", "referer" => "/history" + assert_redirected_to login_path("cookie_test" => "true", "referer" => "/history") follow_redirect! assert_response :success assert_template "users/login" @@ -901,7 +901,7 @@ class UserLoginTest < ActionDispatch::IntegrationTest get "/login", :params => { :referer => "/history" } assert_response :redirect - assert_redirected_to "controller" => "users", "action" => "login", "cookie_test" => "true", "referer" => "/history" + assert_redirected_to login_path("cookie_test" => "true", "referer" => "/history") follow_redirect! assert_response :success assert_template "users/login" @@ -921,7 +921,7 @@ class UserLoginTest < ActionDispatch::IntegrationTest def try_password_login(username, password, remember_me = nil) get "/login" assert_response :redirect - assert_redirected_to :controller => :users, :action => :login, :cookie_test => true + assert_redirected_to login_path(:cookie_test => true) follow_redirect! assert_response :success assert_template "login"