X-Git-Url: https://git.openstreetmap.org./rails.git/blobdiff_plain/6af8f8a9e37e92c139779fbb5fb2590ad040993e..eaf4b32a7f6bf0cdc47dfc0c8b5ddb3358110ee4:/test/controllers/user_controller_test.rb?ds=sidebyside diff --git a/test/controllers/user_controller_test.rb b/test/controllers/user_controller_test.rb index 75d057b9f..7b5f77d62 100644 --- a/test/controllers/user_controller_test.rb +++ b/test/controllers/user_controller_test.rb @@ -1,8 +1,9 @@ -require 'test_helper' +require "test_helper" class UserControllerTest < ActionController::TestCase - fixtures :users - + api_fixtures + fixtures :user_blocks, :messages, :friends + ## # test all routes which lead to this controller def test_routes @@ -222,9 +223,9 @@ class UserControllerTest < ActionController::TestCase def test_user_create_success user = new_user - assert_difference('User.count', 1) do - assert_difference('ActionMailer::Base.deliveries.size', 1) do - post :save, {}, {:new_user => user} + assert_difference("User.count", 1) do + assert_difference("ActionMailer::Base.deliveries.size", 1) do + post :save, {}, { :new_user => user } end end @@ -235,7 +236,7 @@ class UserControllerTest < ActionController::TestCase assert_match /#{@url}/, register_email.body.to_s # Check the page - assert_redirected_to :action => 'confirm', :display_name => user.display_name + assert_redirected_to :action => "confirm", :display_name => user.display_name ActionMailer::Base.deliveries.clear end @@ -244,14 +245,14 @@ class UserControllerTest < ActionController::TestCase user = new_user user.email = users(:public_user).email - assert_no_difference('User.count') do - assert_no_difference('ActionMailer::Base.deliveries.size') do - post :save, {}, {:new_user => user} + assert_no_difference("User.count") do + assert_no_difference("ActionMailer::Base.deliveries.size") do + post :save, {}, { :new_user => user } end end assert_response :success - assert_template 'new' + assert_template "new" assert_select "form > fieldset > div.form-row > input.field_with_errors#user_email" end @@ -259,58 +260,58 @@ class UserControllerTest < ActionController::TestCase user = new_user user.email = users(:public_user).email.upcase - assert_no_difference('User.count') do - assert_no_difference('ActionMailer::Base.deliveries.size') do - post :save, {}, {:new_user => user} + assert_no_difference("User.count") do + assert_no_difference("ActionMailer::Base.deliveries.size") do + post :save, {}, { :new_user => user } end end assert_response :success - assert_template 'new' + assert_template "new" assert_select "form > fieldset > div.form-row > input.field_with_errors#user_email" end - + def test_user_create_submit_duplicate_name user = new_user user.display_name = users(:public_user).display_name - assert_no_difference('User.count') do - assert_no_difference('ActionMailer::Base.deliveries.size') do - post :save, {}, {:new_user => user} + assert_no_difference("User.count") do + assert_no_difference("ActionMailer::Base.deliveries.size") do + post :save, {}, { :new_user => user } end end assert_response :success - assert_template 'new' + assert_template "new" assert_select "form > fieldset > div.form-row > input.field_with_errors#user_display_name" end - + def test_user_create_submit_duplicate_name_uppercase user = new_user user.display_name = users(:public_user).display_name.upcase - assert_no_difference('User.count') do - assert_no_difference('ActionMailer::Base.deliveries.size') do - post :save, {}, {:new_user => user} + assert_no_difference("User.count") do + assert_no_difference("ActionMailer::Base.deliveries.size") do + post :save, {}, { :new_user => user } end end assert_response :success - assert_template 'new' + assert_template "new" assert_select "form > fieldset > div.form-row > input.field_with_errors#user_display_name" end def test_user_save_referer_params user = new_user - assert_difference('User.count', 1) do - assert_difference('ActionMailer::Base.deliveries.size', 1) do - post :save, {}, {:new_user => user, - :referer => '/edit?editor=id#map=1/2/3'} - end + assert_difference("User.count", 1) do + assert_difference("ActionMailer::Base.deliveries.size", 1) do + post :save, {}, { :new_user => user, + :referer => "/edit?editor=id#map=1/2/3" } + end end - assert_equal welcome_path(:editor => 'id', :zoom => 1, :lat => 2, :lon => 3), + assert_equal welcome_path(:editor => "id", :zoom => 1, :lat => 2, :lon => 3), user.tokens.order("id DESC").first.referer ActionMailer::Base.deliveries.clear @@ -325,7 +326,7 @@ class UserControllerTest < ActionController::TestCase @request.cookies["_osm_session"] = user.display_name post :confirm, :confirm_string => token.token - assert_redirected_to :action => 'confirm' + assert_redirected_to :action => "confirm" assert_match /expired/, flash[:error] end @@ -336,7 +337,7 @@ class UserControllerTest < ActionController::TestCase @request.cookies["_osm_session"] = user.display_name post :confirm, :confirm_string => token.token - assert_redirected_to :action => 'login' + assert_redirected_to :action => "login" assert_match /confirmed/, flash[:error] end @@ -370,7 +371,7 @@ class UserControllerTest < ActionController::TestCase # Test resetting using the address as recorded for a user that has an # address which is duplicated in a different case by another user - assert_difference('ActionMailer::Base.deliveries.size', 1) do + assert_difference("ActionMailer::Base.deliveries.size", 1) do post :lost_password, :user => { :email => users(:normal_user).email } end assert_response :redirect @@ -383,7 +384,7 @@ class UserControllerTest < ActionController::TestCase # Test resetting using an address that matches a different user # that has the same address in a different case - assert_difference('ActionMailer::Base.deliveries.size', 1) do + assert_difference("ActionMailer::Base.deliveries.size", 1) do post :lost_password, :user => { :email => users(:normal_user).email.upcase } end assert_response :redirect @@ -396,7 +397,7 @@ class UserControllerTest < ActionController::TestCase # Test resetting using an address that is a case insensitive match # for more than one user but not an exact match for either - assert_difference('ActionMailer::Base.deliveries.size', 0) do + assert_difference("ActionMailer::Base.deliveries.size", 0) do post :lost_password, :user => { :email => users(:normal_user).email.titlecase } end assert_response :success @@ -405,7 +406,7 @@ class UserControllerTest < ActionController::TestCase # Test resetting using the address as recorded for a user that has an # address which is case insensitively unique - assert_difference('ActionMailer::Base.deliveries.size', 1) do + assert_difference("ActionMailer::Base.deliveries.size", 1) do post :lost_password, :user => { :email => users(:public_user).email } end assert_response :redirect @@ -418,7 +419,7 @@ class UserControllerTest < ActionController::TestCase # Test resetting using an address that matches a user that has the # same (case insensitively unique) address in a different case - assert_difference('ActionMailer::Base.deliveries.size', 1) do + assert_difference("ActionMailer::Base.deliveries.size", 1) do post :lost_password, :user => { :email => users(:public_user).email.upcase } end assert_response :redirect @@ -467,7 +468,7 @@ class UserControllerTest < ActionController::TestCase # Make sure that you are redirected to the login page when # you are not logged in - get :account, { :display_name => user.display_name } + get :account, :display_name => user.display_name assert_response :redirect assert_redirected_to :controller => :user, :action => "login", :referer => "/user/test/account" @@ -521,7 +522,7 @@ class UserControllerTest < ActionController::TestCase # Changing email to one that exists should fail user.new_email = users(:public_user).email - assert_no_difference('ActionMailer::Base.deliveries.size') do + assert_no_difference("ActionMailer::Base.deliveries.size") do post :account, { :display_name => user.display_name, :user => user.attributes }, { "user" => user.id } end assert_response :success @@ -532,7 +533,7 @@ class UserControllerTest < ActionController::TestCase # Changing email to one that exists should fail, regardless of case user.new_email = users(:public_user).email.upcase - assert_no_difference('ActionMailer::Base.deliveries.size') do + assert_no_difference("ActionMailer::Base.deliveries.size") do post :account, { :display_name => user.display_name, :user => user.attributes }, { "user" => user.id } end assert_response :success @@ -543,7 +544,7 @@ class UserControllerTest < ActionController::TestCase # Changing email to one that doesn't exist should work user.new_email = "new_tester@example.com" - assert_difference('ActionMailer::Base.deliveries.size', 1) do + assert_difference("ActionMailer::Base.deliveries.size", 1) do post :account, { :display_name => user.display_name, :user => user.attributes }, { "user" => user.id } end assert_response :success @@ -556,16 +557,16 @@ class UserControllerTest < ActionController::TestCase assert_equal user.new_email, email.to.first ActionMailer::Base.deliveries.clear end - + # Check that the user account page will display and contains some relevant # information for the user def test_user_view_account # Test a non-existent user - get :view, {:display_name => "unknown"} + get :view, :display_name => "unknown" assert_response :not_found - + # Test a normal user - get :view, {:display_name => "test"} + get :view, :display_name => "test" assert_response :success assert_select "div#userinformation" do assert_select "a[href^='/user/test/history']", 1 @@ -577,9 +578,9 @@ class UserControllerTest < ActionController::TestCase assert_select "a[href='/user/test/blocks_by']", 0 assert_select "a[href='/blocks/new/test']", 0 end - + # Test a user who has been blocked - get :view, {:display_name => "blocked"} + get :view, :display_name => "blocked" assert_response :success assert_select "div#userinformation" do assert_select "a[href^='/user/blocked/history']", 1 @@ -591,9 +592,9 @@ class UserControllerTest < ActionController::TestCase assert_select "a[href='/user/blocked/blocks_by']", 0 assert_select "a[href='/blocks/new/blocked']", 0 end - + # Test a moderator who has applied blocks - get :view, {:display_name => "moderator"} + get :view, :display_name => "moderator" assert_response :success assert_select "div#userinformation" do assert_select "a[href^='/user/moderator/history']", 1 @@ -610,7 +611,7 @@ class UserControllerTest < ActionController::TestCase session[:user] = users(:normal_user).id # Test the normal user - get :view, {:display_name => "test"} + get :view, :display_name => "test" assert_response :success assert_select "div#userinformation" do assert_select "a[href^='/user/test/history']", 1 @@ -627,7 +628,7 @@ class UserControllerTest < ActionController::TestCase session[:user] = users(:moderator_user).id # Test the normal user - get :view, {:display_name => "test"} + get :view, :display_name => "test" assert_response :success assert_select "div#userinformation" do assert_select "a[href^='/user/test/history']", 1 @@ -691,7 +692,7 @@ class UserControllerTest < ActionController::TestCase # check that nothing is returned when not logged in get :api_details assert_response :unauthorized - + # check that we get a response when logged in basic_authorization(users(:normal_user).email, "test") get :api_details @@ -743,16 +744,16 @@ class UserControllerTest < ActionController::TestCase assert_nil Friend.where(:user_id => user.id, :friend_user_id => friend.id).first # When not logged in a GET should ask us to login - get :make_friend, {:display_name => friend.display_name} + get :make_friend, :display_name => friend.display_name assert_redirected_to :controller => :user, :action => "login", :referer => make_friend_path(:display_name => friend.display_name) # When not logged in a POST should error - post :make_friend, {:display_name => friend.display_name} + post :make_friend, :display_name => friend.display_name assert_response :forbidden assert_nil Friend.where(:user_id => user.id, :friend_user_id => friend.id).first # When logged in a GET should get a confirmation page - get :make_friend, {:display_name => friend.display_name}, {"user" => user} + get :make_friend, { :display_name => friend.display_name }, { "user" => user } assert_response :success assert_template :make_friend assert_select "form" do @@ -762,7 +763,7 @@ class UserControllerTest < ActionController::TestCase assert_nil Friend.where(:user_id => user.id, :friend_user_id => friend.id).first # The GET should preserve any referer - get :make_friend, {:display_name => friend.display_name, :referer => "/test"}, {"user" => user} + get :make_friend, { :display_name => friend.display_name, :referer => "/test" }, { "user" => user } assert_response :success assert_template :make_friend assert_select "form" do @@ -772,8 +773,8 @@ class UserControllerTest < ActionController::TestCase assert_nil Friend.where(:user_id => user.id, :friend_user_id => friend.id).first # When logged in a POST should add the friendship - assert_difference('ActionMailer::Base.deliveries.size', 1) do - post :make_friend, {:display_name => friend.display_name}, {"user" => user} + assert_difference("ActionMailer::Base.deliveries.size", 1) do + post :make_friend, { :display_name => friend.display_name }, { "user" => user } end assert_redirected_to user_path(:display_name => friend.display_name) assert_match /is now your friend/, flash[:notice] @@ -784,8 +785,8 @@ class UserControllerTest < ActionController::TestCase ActionMailer::Base.deliveries.clear # A second POST should report that the friendship already exists - assert_no_difference('ActionMailer::Base.deliveries.size') do - post :make_friend, {:display_name => friend.display_name}, {"user" => user} + assert_no_difference("ActionMailer::Base.deliveries.size") do + post :make_friend, { :display_name => friend.display_name }, { "user" => user } end assert_redirected_to user_path(:display_name => friend.display_name) assert_match /You are already friends with/, flash[:warning] @@ -801,16 +802,16 @@ class UserControllerTest < ActionController::TestCase assert Friend.where(:user_id => user.id, :friend_user_id => friend.id).first # When not logged in a GET should ask us to login - get :remove_friend, {:display_name => friend.display_name} + get :remove_friend, :display_name => friend.display_name assert_redirected_to :controller => :user, :action => "login", :referer => remove_friend_path(:display_name => friend.display_name) # When not logged in a POST should error - post :remove_friend, {:display_name => friend.display_name} + post :remove_friend, :display_name => friend.display_name assert_response :forbidden assert Friend.where(:user_id => user.id, :friend_user_id => friend.id).first # When logged in a GET should get a confirmation page - get :remove_friend, {:display_name => friend.display_name}, {"user" => user} + get :remove_friend, { :display_name => friend.display_name }, { "user" => user } assert_response :success assert_template :remove_friend assert_select "form" do @@ -820,7 +821,7 @@ class UserControllerTest < ActionController::TestCase assert Friend.where(:user_id => user.id, :friend_user_id => friend.id).first # The GET should preserve any referer - get :remove_friend, {:display_name => friend.display_name, :referer => "/test"}, {"user" => user} + get :remove_friend, { :display_name => friend.display_name, :referer => "/test" }, { "user" => user } assert_response :success assert_template :remove_friend assert_select "form" do @@ -830,13 +831,13 @@ class UserControllerTest < ActionController::TestCase assert Friend.where(:user_id => user.id, :friend_user_id => friend.id).first # When logged in a POST should remove the friendship - post :remove_friend, {:display_name => friend.display_name}, {"user" => user} + post :remove_friend, { :display_name => friend.display_name }, { "user" => user } assert_redirected_to user_path(:display_name => friend.display_name) assert_match /was removed from your friends/, flash[:notice] assert_nil Friend.where(:user_id => user.id, :friend_user_id => friend.id).first # A second POST should report that the friendship does not exist - post :remove_friend, {:display_name => friend.display_name}, {"user" => user} + post :remove_friend, { :display_name => friend.display_name }, { "user" => user } assert_redirected_to user_path(:display_name => friend.display_name) assert_match /is not one of your friends/, flash[:error] assert_nil Friend.where(:user_id => user.id, :friend_user_id => friend.id).first @@ -844,17 +845,17 @@ class UserControllerTest < ActionController::TestCase def test_set_status # Try without logging in - get :set_status, {:display_name => users(:normal_user).display_name, :status => "suspended"} + get :set_status, :display_name => users(:normal_user).display_name, :status => "suspended" assert_response :redirect assert_redirected_to :action => :login, :referer => set_status_user_path(:status => "suspended") # Now try as a normal user - get :set_status, {:display_name => users(:normal_user).display_name, :status => "suspended"}, {:user => users(:normal_user).id} + get :set_status, { :display_name => users(:normal_user).display_name, :status => "suspended" }, { :user => users(:normal_user).id } assert_response :redirect assert_redirected_to :action => :view, :display_name => users(:normal_user).display_name # Finally try as an administrator - get :set_status, {:display_name => users(:normal_user).display_name, :status => "suspended"}, {:user => users(:administrator_user).id} + get :set_status, { :display_name => users(:normal_user).display_name, :status => "suspended" }, { :user => users(:administrator_user).id } assert_response :redirect assert_redirected_to :action => :view, :display_name => users(:normal_user).display_name assert_equal "suspended", User.find(users(:normal_user).id).status @@ -862,17 +863,17 @@ class UserControllerTest < ActionController::TestCase def test_delete # Try without logging in - get :delete, {:display_name => users(:normal_user).display_name, :status => "suspended"} + get :delete, :display_name => users(:normal_user).display_name, :status => "suspended" assert_response :redirect assert_redirected_to :action => :login, :referer => delete_user_path(:status => "suspended") # Now try as a normal user - get :delete, {:display_name => users(:normal_user).display_name, :status => "suspended"}, {:user => users(:normal_user).id} + get :delete, { :display_name => users(:normal_user).display_name, :status => "suspended" }, { :user => users(:normal_user).id } assert_response :redirect assert_redirected_to :action => :view, :display_name => users(:normal_user).display_name # Finally try as an administrator - get :delete, {:display_name => users(:normal_user).display_name, :status => "suspended"}, {:user => users(:administrator_user).id} + get :delete, { :display_name => users(:normal_user).display_name, :status => "suspended" }, { :user => users(:administrator_user).id } assert_response :redirect assert_redirected_to :action => :view, :display_name => users(:normal_user).display_name