]> git.openstreetmap.org Git - rails.git/blobdiff - test/controllers/accounts_controller_test.rb
Merge remote-tracking branch 'upstream/pull/5702'
[rails.git] / test / controllers / accounts_controller_test.rb
index 1e39a7329e6c3a7b9fccac22696e223edc3effeb..199bf4aab53ac23825d3ed6de8b25d4186510ac2 100644 (file)
@@ -5,16 +5,23 @@ class AccountsControllerTest < ActionDispatch::IntegrationTest
   # test all routes which lead to this controller
   def test_routes
     assert_routing(
-      { :path => "/account/edit", :method => :get },
-      { :controller => "accounts", :action => "edit" }
+      { :path => "/account", :method => :get },
+      { :controller => "accounts", :action => "show" }
     )
     assert_routing(
       { :path => "/account", :method => :put },
       { :controller => "accounts", :action => "update" }
     )
+    assert_routing(
+      { :path => "/account", :method => :delete },
+      { :controller => "accounts", :action => "destroy" }
+    )
+
+    get "/account/edit"
+    assert_redirected_to "/account"
   end
 
-  def test_account
+  def test_show_and_update
     # Get a user to work with - note that this user deliberately
     # conflicts with uppercase_user in the email and display name
     # fields to test that we can change other fields without any
@@ -24,15 +31,14 @@ class AccountsControllerTest < ActionDispatch::IntegrationTest
 
     # Make sure that you are redirected to the login page when
     # you are not logged in
-    get edit_account_path
-    assert_response :redirect
-    assert_redirected_to login_path(:referer => "/account/edit")
+    get account_path
+    assert_redirected_to login_path(:referer => account_path)
 
     # Make sure we get the page when we are logged in as the right user
     session_for(user)
-    get edit_account_path
+    get account_path
     assert_response :success
-    assert_template :edit
+    assert_template :show
     assert_select "form#accountForm" do |form|
       assert_equal "post", form.attr("method").to_s
       assert_select "input[name='_method']", true
@@ -42,44 +48,41 @@ class AccountsControllerTest < ActionDispatch::IntegrationTest
     # Updating the description using GET should fail
     user.description = "new description"
     user.preferred_editor = "default"
-    get edit_account_path, :params => { :user => user.attributes }
+    get account_path, :params => { :user => user.attributes }
     assert_response :success
-    assert_template :edit
+    assert_template :show
     assert_not_equal user.description, User.find(user.id).description
 
     # Adding external authentication should redirect to the auth provider
     patch account_path, :params => { :user => user.attributes.merge(:auth_provider => "google") }
-    assert_response :redirect
     assert_redirected_to auth_path(:provider => "google", :origin => "/account")
     follow_redirect!
-    assert_response :redirect
     assert_redirected_to %r{^https://accounts.google.com/o/oauth2/auth\?.*}
 
     # Changing name to one that exists should fail
     new_attributes = user.attributes.dup.merge(:display_name => create(:user).display_name)
     patch account_path, :params => { :user => new_attributes }
     assert_response :success
-    assert_template :edit
-    assert_select ".notice", false
+    assert_template :show
+    assert_select ".alert-success", false
     assert_select "form#accountForm > div > input.is-invalid#user_display_name"
 
     # Changing name to one that exists should fail, regardless of case
     new_attributes = user.attributes.dup.merge(:display_name => create(:user).display_name.upcase)
     patch account_path, :params => { :user => new_attributes }
     assert_response :success
-    assert_template :edit
-    assert_select ".notice", false
+    assert_template :show
+    assert_select ".alert-success", false
     assert_select "form#accountForm > div > input.is-invalid#user_display_name"
 
     # Changing name to one that doesn't exist should work
     new_attributes = user.attributes.dup.merge(:display_name => "new tester")
     patch account_path, :params => { :user => new_attributes }
-    assert_response :redirect
-    assert_redirected_to edit_account_url
-    get edit_account_path
+    assert_redirected_to account_path
+    follow_redirect!
     assert_response :success
-    assert_template :edit
-    assert_select ".notice", /^User information updated successfully/
+    assert_template :show
+    assert_select ".alert-success", /^User information updated successfully/
     assert_select "form#accountForm > div > input#user_display_name[value=?]", "new tester"
 
     # Record the change of name
@@ -93,8 +96,8 @@ class AccountsControllerTest < ActionDispatch::IntegrationTest
       end
     end
     assert_response :success
-    assert_template :edit
-    assert_select ".notice", false
+    assert_template :show
+    assert_select ".alert-success", false
     assert_select "form#accountForm > div > input.is-invalid#user_new_email"
 
     # Changing email to one that exists should fail, regardless of case
@@ -105,8 +108,8 @@ class AccountsControllerTest < ActionDispatch::IntegrationTest
       end
     end
     assert_response :success
-    assert_template :edit
-    assert_select ".notice", false
+    assert_template :show
+    assert_select ".alert-success", false
     assert_select "form#accountForm > div > input.is-invalid#user_new_email"
 
     # Changing email to one that doesn't exist should work
@@ -116,33 +119,30 @@ class AccountsControllerTest < ActionDispatch::IntegrationTest
         patch account_path, :params => { :user => user.attributes }
       end
     end
-    assert_response :redirect
-    assert_redirected_to edit_account_url
-    get edit_account_path
+    assert_redirected_to account_path
+    follow_redirect!
     assert_response :success
-    assert_template :edit
-    assert_select ".notice", /^User information updated successfully/
+    assert_template :show
+    assert_select ".alert-success", /^User information updated successfully/
     assert_select "form#accountForm > div > input#user_new_email[value=?]", user.new_email
     email = ActionMailer::Base.deliveries.first
     assert_equal 1, email.to.count
     assert_equal user.new_email, email.to.first
-    ActionMailer::Base.deliveries.clear
   end
 
-  def test_private_account
+  def test_show_private_account
     user = create(:user, :data_public => false)
 
     # Make sure that you are redirected to the login page when
     # you are not logged in
-    get edit_account_path
-    assert_response :redirect
-    assert_redirected_to login_path(:referer => "/account/edit")
+    get account_path
+    assert_redirected_to login_path(:referer => account_path)
 
     # Make sure we get the page when we are logged in as the right user
     session_for(user)
-    get edit_account_path
+    get account_path
     assert_response :success
-    assert_template :edit
+    assert_template :show
     assert_select "form#accountForm" do |form|
       assert_equal "post", form.attr("method").to_s
       assert_select "input[name='_method']", true
@@ -152,4 +152,23 @@ class AccountsControllerTest < ActionDispatch::IntegrationTest
     # Make sure we have a button to "go public"
     assert_select "form.button_to[action='/user/go_public']", true
   end
+
+  def test_destroy_allowed
+    user = create(:user)
+    session_for(user)
+
+    delete account_path
+    assert_response :redirect
+  end
+
+  def test_destroy_not_allowed
+    with_user_account_deletion_delay(24) do
+      user = create(:user)
+      create(:changeset, :user => user, :created_at => Time.now.utc)
+      session_for(user)
+
+      delete account_path
+      assert_response :bad_request
+    end
+  end
 end