X-Git-Url: https://git.openstreetmap.org./rails.git/blobdiff_plain/15b550182cd893468d4f12f89e94dedc95097e0c..965f32d545d7c44ad7b36264b818b930a50d8901:/test/controllers/user_controller_test.rb diff --git a/test/controllers/user_controller_test.rb b/test/controllers/user_controller_test.rb index 8072f895b..ede841032 100644 --- a/test/controllers/user_controller_test.rb +++ b/test/controllers/user_controller_test.rb @@ -209,6 +209,24 @@ class UserControllerTest < ActionController::TestCase end end + def test_new_view_logged_in + session[:user] = users(:normal_user).id + + get :new + assert_response :redirect + assert_redirected_to user_new_path(:cookie_test => "true") + get :new, :cookie_test => "true" + assert_response :redirect + assert_redirected_to root_path + + get :new, :referer => "/test" + assert_response :redirect + assert_redirected_to user_new_path(:referer => "/test", :cookie_test => "true") + get :new, :referer => "/test", :cookie_test => "true" + assert_response :redirect + assert_redirected_to "/test" + end + def test_new_success user = new_user @@ -332,6 +350,27 @@ class UserControllerTest < ActionController::TestCase assert_redirected_to "/test" end + def test_logout_with_token + token = users(:normal_user).tokens.create + + session[:token] = token.token + + get :logout + assert_response :success + assert_template :logout + assert_select "input[name=referer][value=?]", "" + assert_equal token.token, session[:token] + assert_not_nil UserToken.where(:id => token.id).first + + session_id = assert_select("input[name=session]").first["value"] + + get :logout, :session => session_id + assert_response :redirect + assert_redirected_to root_path + assert_nil session[:token] + assert_nil UserToken.where(:id => token.id).first + end + def test_confirm_get user = users(:inactive_user) confirm_string = user.tokens.create.token @@ -435,6 +474,8 @@ class UserControllerTest < ActionController::TestCase end def test_confirm_resend_success + session[:token] = users(:inactive_user).tokens.create.token + assert_difference "ActionMailer::Base.deliveries.size", 1 do get :confirm_resend, :display_name => users(:inactive_user).display_name end @@ -450,7 +491,17 @@ class UserControllerTest < ActionController::TestCase ActionMailer::Base.deliveries.clear end - def test_confirm_resend_failure + def test_confirm_resend_no_token + assert_no_difference "ActionMailer::Base.deliveries.size" do + get :confirm_resend, :display_name => users(:inactive_user).display_name + end + + assert_response :redirect + assert_redirected_to login_path + assert_match "User Inactive User not found.", flash[:error] + end + + def test_confirm_resend_unknown_user assert_no_difference "ActionMailer::Base.deliveries.size" do get :confirm_resend, :display_name => "No Such User" end @@ -496,6 +547,42 @@ class UserControllerTest < ActionController::TestCase assert_match /confirmation code has expired or does not exist/, flash[:error] end + ## + # test if testing for a gravatar works + # this happens when the email is actually changed + # which is triggered by the confirmation mail + def test_gravatar_auto_enable + with_http_stubs "gravatar" do + # switch to email that has a gravatar + user = users(:first_gravatar_user) + confirm_string = user.tokens.create.token + # precondition gravatar should be turned off + assert !user.image_use_gravatar + post :confirm_email, :confirm_string => confirm_string + assert_response :redirect + assert_redirected_to :action => :account, :display_name => user.display_name + assert_match /Confirmed your change of email address/, flash[:notice] + # gravatar use should now be enabled + assert User.find(users(:first_gravatar_user).id).image_use_gravatar + end + end + + def test_gravatar_auto_disable + with_http_stubs "gravatar" do + # switch to email without a gravatar + user = users(:second_gravatar_user) + confirm_string = user.tokens.create.token + # precondition gravatar should be turned on + assert user.image_use_gravatar + post :confirm_email, :confirm_string => confirm_string + assert_response :redirect + assert_redirected_to :action => :account, :display_name => user.display_name + assert_match /Confirmed your change of email address/, flash[:notice] + # gravatar use should now be disabled + assert !User.find(users(:second_gravatar_user).id).image_use_gravatar + end + end + def test_terms_new_user get :terms, {}, { :new_user => User.new } assert_response :success @@ -505,17 +592,53 @@ class UserControllerTest < ActionController::TestCase def test_terms_seen user = users(:normal_user) - get :terms, {}, { :user => user } + session[:user] = user.id + + get :terms + assert_response :redirect + assert_redirected_to :action => :account, :display_name => user.display_name + end + + def test_terms_not_seen_without_referer + user = users(:terms_not_seen_user) + + session[:user] = user.id + + get :terms + assert_response :success + assert_template :terms + + post :save, :user => { :consider_pd => true } assert_response :redirect assert_redirected_to :action => :account, :display_name => user.display_name + assert_equal "Thanks for accepting the new contributor terms!", flash[:notice] + + user.reload + + assert_equal true, user.consider_pd + assert_not_nil user.terms_agreed + assert_equal true, user.terms_seen end - def test_terms_not_seen + def test_terms_not_seen_with_referer user = users(:terms_not_seen_user) - get :terms, {}, { :user => user } + session[:user] = user.id + + get :terms, :referer => "/test" assert_response :success assert_template :terms + + post :save, :user => { :consider_pd => true }, :referer => "/test" + assert_response :redirect + assert_redirected_to "/test" + assert_equal "Thanks for accepting the new contributor terms!", flash[:notice] + + user.reload + + assert_equal true, user.consider_pd + assert_not_nil user.terms_agreed + assert_equal true, user.terms_seen end def test_go_public @@ -615,7 +738,8 @@ class UserControllerTest < ActionController::TestCase # Test setting a new password post :reset_password, :token => token.token, :user => { :pass_crypt => "new_password", :pass_crypt_confirmation => "new_password" } assert_response :redirect - assert_redirected_to :action => :login + assert_redirected_to root_path + assert_equal users(:inactive_user).id, session[:user] user = User.find(users(:inactive_user).id) assert_equal "active", user.status assert_equal true, user.email_valid @@ -636,26 +760,83 @@ class UserControllerTest < ActionController::TestCase assert_redirected_to :controller => :user, :action => "login", :referer => "/user/test/account" # Make sure that you are blocked when not logged in as the right user - get :account, { :display_name => user.display_name }, { "user" => users(:public_user).id } + get :account, { :display_name => user.display_name }, { :user => users(:public_user).id } assert_response :forbidden # Make sure we get the page when we are logged in as the right user - get :account, { :display_name => user.display_name }, { "user" => user } + get :account, { :display_name => user.display_name }, { :user => user } assert_response :success assert_template :account # Updating the description should work user.description = "new description" - post :account, { :display_name => user.display_name, :user => user.attributes }, { "user" => user.id } + post :account, { :display_name => user.display_name, :user => user.attributes }, { :user => user.id } assert_response :success assert_template :account assert_select "div#errorExplanation", false assert_select ".notice", /^User information updated successfully/ assert_select "form#accountForm > fieldset > div.form-row > div#user_description_container > div#user_description_content > textarea#user_description", user.description + # Changing to a invalid editor should fail + user.preferred_editor = "unknown" + post :account, { :display_name => user.display_name, :user => user.attributes }, { :user => user.id } + assert_response :success + assert_template :account + assert_select ".notice", false + assert_select "div#errorExplanation" + assert_select "form#accountForm > fieldset > div.form-row > select#user_preferred_editor > option[selected]", false + + # Changing to a valid editor should work + user.preferred_editor = "potlatch2" + post :account, { :display_name => user.display_name, :user => user.attributes }, { :user => user.id } + assert_response :success + assert_template :account + assert_select "div#errorExplanation", false + assert_select ".notice", /^User information updated successfully/ + assert_select "form#accountForm > fieldset > div.form-row > select#user_preferred_editor > option[selected][value=?]", "potlatch2" + + # Changing to the default editor should work + user.preferred_editor = "default" + post :account, { :display_name => user.display_name, :user => user.attributes }, { :user => user.id } + assert_response :success + assert_template :account + assert_select "div#errorExplanation", false + assert_select ".notice", /^User information updated successfully/ + assert_select "form#accountForm > fieldset > div.form-row > select#user_preferred_editor > option[selected]", false + + # Changing to an uploaded image should work + image = Rack::Test::UploadedFile.new("test/traces/1.gif", "image/gif") + post :account, { :display_name => user.display_name, :image_action => "new", :user => user.attributes.merge(:image => image) }, { :user => user.id } + assert_response :success + assert_template :account + assert_select "div#errorExplanation", false + assert_select ".notice", /^User information updated successfully/ + assert_select "form#accountForm > fieldset > div.form-row.accountImage input[name=image_action][checked][value=?]", "keep" + + # Changing to a gravatar image should work + post :account, { :display_name => user.display_name, :image_action => "gravatar", :user => user.attributes }, { :user => user.id } + assert_response :success + assert_template :account + assert_select "div#errorExplanation", false + assert_select ".notice", /^User information updated successfully/ + assert_select "form#accountForm > fieldset > div.form-row.accountImage input[name=image_action][checked][value=?]", "gravatar" + + # Removing the image should work + post :account, { :display_name => user.display_name, :image_action => "delete", :user => user.attributes }, { :user => user.id } + assert_response :success + assert_template :account + assert_select "div#errorExplanation", false + assert_select ".notice", /^User information updated successfully/ + assert_select "form#accountForm > fieldset > div.form-row.accountImage input[name=image_action][checked]", false + + # Adding external authentication should redirect to the auth provider + post :account, { :display_name => user.display_name, :user => user.attributes.merge(:auth_provider => "openid", :auth_uid => "gmail.com") }, { :user => user.id } + assert_response :redirect + assert_redirected_to auth_path(:provider => "openid", :openid_url => "https://www.google.com/accounts/o8/id", :origin => "/user/#{user.display_name}/account") + # Changing name to one that exists should fail new_attributes = user.attributes.dup.merge(:display_name => users(:public_user).display_name) - post :account, { :display_name => user.display_name, :user => new_attributes }, { "user" => user.id } + post :account, { :display_name => user.display_name, :user => new_attributes }, { :user => user.id } assert_response :success assert_template :account assert_select ".notice", false @@ -664,7 +845,7 @@ class UserControllerTest < ActionController::TestCase # Changing name to one that exists should fail, regardless of case new_attributes = user.attributes.dup.merge(:display_name => users(:public_user).display_name.upcase) - post :account, { :display_name => user.display_name, :user => new_attributes }, { "user" => user.id } + post :account, { :display_name => user.display_name, :user => new_attributes }, { :user => user.id } assert_response :success assert_template :account assert_select ".notice", false @@ -673,7 +854,7 @@ class UserControllerTest < ActionController::TestCase # Changing name to one that doesn't exist should work new_attributes = user.attributes.dup.merge(:display_name => "new tester") - post :account, { :display_name => user.display_name, :user => new_attributes }, { "user" => user.id } + post :account, { :display_name => user.display_name, :user => new_attributes }, { :user => user.id } assert_response :success assert_template :account assert_select "div#errorExplanation", false @@ -686,7 +867,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 - post :account, { :display_name => user.display_name, :user => user.attributes }, { "user" => user.id } + post :account, { :display_name => user.display_name, :user => user.attributes }, { :user => user.id } end assert_response :success assert_template :account @@ -697,7 +878,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 - post :account, { :display_name => user.display_name, :user => user.attributes }, { "user" => user.id } + post :account, { :display_name => user.display_name, :user => user.attributes }, { :user => user.id } end assert_response :success assert_template :account @@ -708,7 +889,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 - post :account, { :display_name => user.display_name, :user => user.attributes }, { "user" => user.id } + post :account, { :display_name => user.display_name, :user => user.attributes }, { :user => user.id } end assert_response :success assert_template :account @@ -816,7 +997,7 @@ class UserControllerTest < ActionController::TestCase assert_select "contributor-terms", :count => 1 do assert_select "[agreed='true']" end - assert_select "img", :count => 1 + assert_select "img", :count => 0 assert_select "roles", :count => 1 do assert_select "role", :count => 0 end @@ -868,7 +1049,7 @@ class UserControllerTest < ActionController::TestCase assert_select "contributor-terms", :count => 1 do assert_select "[agreed='true'][pd='false']" end - assert_select "img", :count => 1 + assert_select "img", :count => 0 assert_select "roles", :count => 1 do assert_select "role", :count => 0 end @@ -1126,6 +1307,166 @@ class UserControllerTest < ActionController::TestCase assert_equal "deleted", user.status end + def test_list_get + # Shouldn't work when not logged in + get :list + assert_response :redirect + assert_redirected_to :action => :login, :referer => users_path + + session[:user] = users(:normal_user).id + + # Shouldn't work when logged in as a normal user + get :list + assert_response :redirect + assert_redirected_to :action => :login, :referer => users_path + + session[:user] = users(:moderator_user).id + + # Shouldn't work when logged in as a moderator + get :list + assert_response :redirect + assert_redirected_to :action => :login, :referer => users_path + + session[:user] = users(:administrator_user).id + + # Should work when logged in as an administrator + get :list + assert_response :success + assert_template :list + assert_select "table#user_list tr", :count => User.count + 1 + + # Should be able to limit by status + get :list, :status => "suspended" + assert_response :success + assert_template :list + assert_select "table#user_list tr", :count => User.where(:status => "suspended").count + 1 + + # Should be able to limit by IP address + get :list, :ip => "1.2.3.4" + assert_response :success + assert_template :list + assert_select "table#user_list tr", :count => User.where(:creation_ip => "1.2.3.4").count + 1 + end + + def test_list_get_paginated + 1.upto(100).each do |n| + User.create(:display_name => "extra_#{n}", + :email => "extra#{n}@example.com", + :pass_crypt => "extraextra") + end + + session[:user] = users(:administrator_user).id + + get :list + assert_response :success + assert_template :list + assert_select "table#user_list tr", :count => 51 + + get :list, :page => 2 + assert_response :success + assert_template :list + assert_select "table#user_list tr", :count => 51 + + get :list, :page => 3 + assert_response :success + assert_template :list + assert_select "table#user_list tr", :count => 25 + end + + def test_list_post_confirm + inactive_user = users(:inactive_user) + suspended_user = users(:suspended_user) + + # Shouldn't work when not logged in + assert_no_difference "User.active.count" do + post :list, :confirm => 1, :user => { inactive_user.id => 1, suspended_user.id => 1 } + end + assert_response :redirect + assert_redirected_to :action => :login, :referer => users_path(:confirm => 1, :user => { inactive_user.id => 1, suspended_user.id => 1 }) + assert_equal "pending", inactive_user.reload.status + assert_equal "suspended", suspended_user.reload.status + + session[:user] = users(:normal_user).id + + # Shouldn't work when logged in as a normal user + assert_no_difference "User.active.count" do + post :list, :confirm => 1, :user => { inactive_user.id => 1, suspended_user.id => 1 } + end + assert_response :redirect + assert_redirected_to :action => :login, :referer => users_path(:confirm => 1, :user => { inactive_user.id => 1, suspended_user.id => 1 }) + assert_equal "pending", inactive_user.reload.status + assert_equal "suspended", suspended_user.reload.status + + session[:user] = users(:moderator_user).id + + # Shouldn't work when logged in as a moderator + assert_no_difference "User.active.count" do + post :list, :confirm => 1, :user => { inactive_user.id => 1, suspended_user.id => 1 } + end + assert_response :redirect + assert_redirected_to :action => :login, :referer => users_path(:confirm => 1, :user => { inactive_user.id => 1, suspended_user.id => 1 }) + assert_equal "pending", inactive_user.reload.status + assert_equal "suspended", suspended_user.reload.status + + session[:user] = users(:administrator_user).id + + # Should work when logged in as an administrator + assert_difference "User.active.count", 2 do + post :list, :confirm => 1, :user => { inactive_user.id => 1, suspended_user.id => 1 } + end + assert_response :redirect + assert_redirected_to :action => :list + assert_equal "confirmed", inactive_user.reload.status + assert_equal "confirmed", suspended_user.reload.status + end + + def test_list_post_hide + normal_user = users(:normal_user) + confirmed_user = users(:confirmed_user) + + # Shouldn't work when not logged in + assert_no_difference "User.active.count" do + post :list, :hide => 1, :user => { normal_user.id => 1, confirmed_user.id => 1 } + end + assert_response :redirect + assert_redirected_to :action => :login, :referer => users_path(:hide => 1, :user => { normal_user.id => 1, confirmed_user.id => 1 }) + assert_equal "active", normal_user.reload.status + assert_equal "confirmed", confirmed_user.reload.status + + session[:user] = users(:normal_user).id + + # Shouldn't work when logged in as a normal user + assert_no_difference "User.active.count" do + post :list, :hide => 1, :user => { normal_user.id => 1, confirmed_user.id => 1 } + end + assert_response :redirect + assert_redirected_to :action => :login, :referer => users_path(:hide => 1, :user => { normal_user.id => 1, confirmed_user.id => 1 }) + assert_equal "active", normal_user.reload.status + assert_equal "confirmed", confirmed_user.reload.status + + session[:user] = users(:moderator_user).id + + # Shouldn't work when logged in as a moderator + assert_no_difference "User.active.count" do + post :list, :hide => 1, :user => { normal_user.id => 1, confirmed_user.id => 1 } + end + assert_response :redirect + assert_redirected_to :action => :login, :referer => users_path(:hide => 1, :user => { normal_user.id => 1, confirmed_user.id => 1 }) + assert_equal "active", normal_user.reload.status + assert_equal "confirmed", confirmed_user.reload.status + + session[:user] = users(:administrator_user).id + + # Should work when logged in as an administrator + assert_difference "User.active.count", -2 do + post :list, :hide => 1, :user => { normal_user.id => 1, confirmed_user.id => 1 } + end + assert_response :redirect + assert_redirected_to :action => :list + assert_equal "deleted", normal_user.reload.status + assert_equal "deleted", confirmed_user.reload.status + end + private def new_user