]> git.openstreetmap.org Git - rails.git/blobdiff - test/controllers/users_controller_test.rb
Add history changesets layer module
[rails.git] / test / controllers / users_controller_test.rb
index 10875b068b9d586c8c81ae38b438364d3ad33447..5066faec3097377b794233d20087e87ab9ad4cbd 100644 (file)
@@ -14,16 +14,6 @@ class UsersControllerTest < ActionDispatch::IntegrationTest
       { :controller => "users", :action => "create" }
     )
 
-    assert_routing(
-      { :path => "/user/terms", :method => :get },
-      { :controller => "users", :action => "terms" }
-    )
-
-    assert_routing(
-      { :path => "/user/save", :method => :post },
-      { :controller => "users", :action => "save" }
-    )
-
     assert_routing(
       { :path => "/user/go_public", :method => :post },
       { :controller => "users", :action => "go_public" }
@@ -38,15 +28,6 @@ class UsersControllerTest < ActionDispatch::IntegrationTest
       { :path => "/user/username", :method => :get },
       { :controller => "users", :action => "show", :display_name => "username" }
     )
-
-    assert_routing(
-      { :path => "/user/username/set_status", :method => :post },
-      { :controller => "users", :action => "set_status", :display_name => "username" }
-    )
-    assert_routing(
-      { :path => "/user/username", :method => :delete },
-      { :controller => "users", :action => "destroy", :display_name => "username" }
-    )
   end
 
   # The user creation page loads
@@ -57,6 +38,8 @@ class UsersControllerTest < ActionDispatch::IntegrationTest
     get new_user_path, :params => { :cookie_test => "true" }
     assert_response :success
 
+    assert_no_match(/img-src \* data:;/, @response.headers["Content-Security-Policy-Report-Only"])
+
     assert_select "html", :count => 1 do
       assert_select "head", :count => 1 do
         assert_select "title", :text => /Sign Up/, :count => 1
@@ -104,8 +87,6 @@ class UsersControllerTest < ActionDispatch::IntegrationTest
 
     # Check the page
     assert_redirected_to :controller => :confirmations, :action => :confirm, :display_name => user.display_name
-
-    ActionMailer::Base.deliveries.clear
   end
 
   def test_create_duplicate_email
@@ -206,73 +187,6 @@ class UsersControllerTest < ActionDispatch::IntegrationTest
         perform_enqueued_jobs
       end
     end
-
-    ActionMailer::Base.deliveries.clear
-  end
-
-  def test_terms_agreed
-    user = create(:user, :terms_seen => true, :terms_agreed => Date.yesterday)
-
-    session_for(user)
-
-    get user_terms_path
-    assert_redirected_to edit_account_path
-  end
-
-  def test_terms_not_seen_without_referer
-    user = create(:user, :terms_seen => false, :terms_agreed => nil)
-
-    session_for(user)
-
-    get user_terms_path
-    assert_response :success
-    assert_template :terms
-
-    post user_save_path, :params => { :user => { :consider_pd => true }, :read_ct => 1, :read_tou => 1 }
-    assert_redirected_to edit_account_path
-    assert_equal "Thanks for accepting the new contributor terms!", flash[:notice]
-
-    user.reload
-
-    assert user.consider_pd
-    assert_not_nil user.terms_agreed
-    assert user.terms_seen
-  end
-
-  def test_terms_not_seen_with_referer
-    user = create(:user, :terms_seen => false, :terms_agreed => nil)
-
-    session_for(user)
-
-    get user_terms_path, :params => { :referer => "/test" }
-    assert_response :success
-    assert_template :terms
-
-    post user_save_path, :params => { :user => { :consider_pd => true }, :referer => "/test", :read_ct => 1, :read_tou => 1 }
-    assert_redirected_to "/test"
-    assert_equal "Thanks for accepting the new contributor terms!", flash[:notice]
-
-    user.reload
-
-    assert user.consider_pd
-    assert_not_nil user.terms_agreed
-    assert user.terms_seen
-  end
-
-  # Check that if you haven't seen the terms, and make a request that requires authentication,
-  # that your request is redirected to view the terms
-  def test_terms_not_seen_redirection
-    user = create(:user, :terms_seen => false, :terms_agreed => nil)
-    session_for(user)
-
-    get edit_account_path
-    assert_redirected_to :controller => :users, :action => :terms, :referer => "/account/edit"
-  end
-
-  def test_terms_not_logged_in
-    get user_terms_path
-
-    assert_redirected_to login_path(:referer => "/user/terms")
   end
 
   def test_go_public
@@ -281,7 +195,7 @@ class UsersControllerTest < ActionDispatch::IntegrationTest
 
     post user_go_public_path
 
-    assert_redirected_to edit_account_path
+    assert_redirected_to account_path
     assert User.find(user.id).data_public
   end
 
@@ -297,11 +211,12 @@ class UsersControllerTest < ActionDispatch::IntegrationTest
 
     get user_path(user)
     assert_response :success
+    assert_match(/img-src \* data:;/, @response.headers["Content-Security-Policy-Report-Only"])
     assert_select "div.content-heading" do
       assert_select "a[href^='/user/#{ERB::Util.u(user.display_name)}/history']", 1
       assert_select "a[href='/user/#{ERB::Util.u(user.display_name)}/traces']", 1
       assert_select "a[href='/user/#{ERB::Util.u(user.display_name)}/diary']", 1
-      assert_select "a[href='/user/#{ERB::Util.u(user.display_name)}/diary/comments']", 1
+      assert_select "a[href='/user/#{ERB::Util.u(user.display_name)}/diary_comments']", 1
       assert_select "a[href='/user/#{ERB::Util.u(user.display_name)}/account']", 0
       assert_select "a[href='/user/#{ERB::Util.u(user.display_name)}/blocks']", 0
       assert_select "a[href='/user/#{ERB::Util.u(user.display_name)}/blocks_by']", 0
@@ -317,7 +232,7 @@ class UsersControllerTest < ActionDispatch::IntegrationTest
       assert_select "a[href^='/user/#{ERB::Util.u(blocked_user.display_name)}/history']", 1
       assert_select "a[href='/user/#{ERB::Util.u(blocked_user.display_name)}/traces']", 1
       assert_select "a[href='/user/#{ERB::Util.u(blocked_user.display_name)}/diary']", 1
-      assert_select "a[href='/user/#{ERB::Util.u(blocked_user.display_name)}/diary/comments']", 1
+      assert_select "a[href='/user/#{ERB::Util.u(blocked_user.display_name)}/diary_comments']", 1
       assert_select "a[href='/user/#{ERB::Util.u(blocked_user.display_name)}/account']", 0
       assert_select "a[href='/user/#{ERB::Util.u(blocked_user.display_name)}/blocks']", 1
       assert_select "a[href='/user/#{ERB::Util.u(blocked_user.display_name)}/blocks_by']", 0
@@ -333,8 +248,8 @@ class UsersControllerTest < ActionDispatch::IntegrationTest
       assert_select "a[href^='/user/#{ERB::Util.u(moderator_user.display_name)}/history']", 1
       assert_select "a[href='/user/#{ERB::Util.u(moderator_user.display_name)}/traces']", 1
       assert_select "a[href='/user/#{ERB::Util.u(moderator_user.display_name)}/diary']", 1
-      assert_select "a[href='/user/#{ERB::Util.u(moderator_user.display_name)}/diary/comments']", 1
-      assert_select "a[href='/user/#{ERB::Util.u(moderator_user.display_name)}/account']", 0
+      assert_select "a[href='/user/#{ERB::Util.u(moderator_user.display_name)}/diary_comments']", 1
+      assert_select "a[href='/account']", 0
       assert_select "a[href='/user/#{ERB::Util.u(moderator_user.display_name)}/blocks']", 0
       assert_select "a[href='/user/#{ERB::Util.u(moderator_user.display_name)}/blocks_by']", 1
       assert_select "a[href='/user_blocks/new/#{ERB::Util.u(moderator_user.display_name)}']", 0
@@ -350,8 +265,8 @@ class UsersControllerTest < ActionDispatch::IntegrationTest
       assert_select "a[href^='/user/#{ERB::Util.u(user.display_name)}/history']", 1
       assert_select "a[href='/traces/mine']", 1
       assert_select "a[href='/user/#{ERB::Util.u(user.display_name)}/diary']", 1
-      assert_select "a[href='/user/#{ERB::Util.u(user.display_name)}/diary/comments']", 1
-      assert_select "a[href='/account/edit']", 1
+      assert_select "a[href='/user/#{ERB::Util.u(user.display_name)}/diary_comments']", 1
+      assert_select "a[href='/account']", 1
       assert_select "a[href='/user/#{ERB::Util.u(user.display_name)}/blocks']", 0
       assert_select "a[href='/user/#{ERB::Util.u(user.display_name)}/blocks_by']", 0
       assert_select "a[href='/user_blocks/new/#{ERB::Util.u(user.display_name)}']", 0
@@ -368,8 +283,8 @@ class UsersControllerTest < ActionDispatch::IntegrationTest
       assert_select "a[href^='/user/#{ERB::Util.u(user.display_name)}/history']", 1
       assert_select "a[href='/user/#{ERB::Util.u(user.display_name)}/traces']", 1
       assert_select "a[href='/user/#{ERB::Util.u(user.display_name)}/diary']", 1
-      assert_select "a[href='/user/#{ERB::Util.u(user.display_name)}/diary/comments']", 1
-      assert_select "a[href='/account/edit']", 0
+      assert_select "a[href='/user/#{ERB::Util.u(user.display_name)}/diary_comments']", 1
+      assert_select "a[href='/account']", 0
       assert_select "a[href='/user/#{ERB::Util.u(user.display_name)}/blocks']", 0
       assert_select "a[href='/user/#{ERB::Util.u(user.display_name)}/blocks_by']", 0
       assert_select "a[href='/user_blocks/new/#{ERB::Util.u(user.display_name)}']", 1
@@ -404,64 +319,108 @@ class UsersControllerTest < ActionDispatch::IntegrationTest
     end
   end
 
-  def test_set_status
+  def test_auth_failure_callback
+    get auth_failure_path
+    assert_redirected_to login_path
+
+    get auth_failure_path, :params => { :origin => "/" }
+    assert_redirected_to root_path
+
+    get auth_failure_path, :params => { :origin => "http://www.google.com" }
+    assert_redirected_to login_path
+  end
+
+  def test_show_heatmap_data
     user = create(:user)
+    # Create two changesets
+    create(:changeset, :user => user, :created_at => 6.months.ago, :num_changes => 10)
+    create(:changeset, :user => user, :created_at => 3.months.ago, :num_changes => 20)
 
-    # Try without logging in
-    post set_status_user_path(user), :params => { :event => "confirm" }
-    assert_response :forbidden
+    get user_path(user.display_name)
+    assert_response :success
+    # The data should not be empty
+    assert_not_nil assigns(:heatmap_data)
 
-    # Now try as a normal user
-    session_for(user)
-    post set_status_user_path(user), :params => { :event => "confirm" }
-    assert_redirected_to :controller => :errors, :action => :forbidden
-
-    # Finally try as an administrator
-    session_for(create(:administrator_user))
-    post set_status_user_path(user), :params => { :event => "confirm" }
-    assert_redirected_to :action => :show, :display_name => user.display_name
-    assert_equal "confirmed", User.find(user.id).status
+    heatmap_data = assigns(:heatmap_data)
+    # The data should be in the right format
+    assert(heatmap_data.all? { |entry| entry[:date] && entry[:total_changes] }, "Heatmap data should have :date and :total_changes keys")
   end
 
-  def test_destroy
-    user = create(:user, :home_lat => 12.1, :home_lon => 12.1, :description => "test")
+  def test_show_heatmap_data_caching
+    # Enable caching to be able to test
+    Rails.cache.clear
+    @original_cache_store = Rails.cache
+    Rails.cache = ActiveSupport::Cache::MemoryStore.new
 
-    # Try without logging in
-    delete user_path(user)
-    assert_response :forbidden
+    user = create(:user)
 
-    # Now try as a normal user
-    session_for(user)
-    delete user_path(user)
-    assert_redirected_to :controller => :errors, :action => :forbidden
-
-    # Finally try as an administrator
-    session_for(create(:administrator_user))
-    delete user_path(user)
-    assert_redirected_to :action => :show, :display_name => user.display_name
-
-    # Check that the user was deleted properly
-    user.reload
-    assert_equal "user_#{user.id}", user.display_name
-    assert_equal "", user.description
-    assert_nil user.home_lat
-    assert_nil user.home_lon
-    assert_not user.avatar.attached?
-    assert_not user.email_valid
-    assert_nil user.new_email
-    assert_nil user.auth_provider
-    assert_nil user.auth_uid
-    assert_equal "deleted", user.status
+    # Create an initial changeset
+    create(:changeset, :user => user, :created_at => 6.months.ago, :num_changes => 15)
+
+    # First request to populate the cache
+    get user_path(user.display_name)
+    first_response_data = assigns(:heatmap_data)
+    assert_not_nil first_response_data, "Expected heatmap data to be assigned on the first request"
+    assert_equal 1, first_response_data.size, "Expected one entry in the heatmap data"
+
+    # Inspect cache after the first request
+    cached_data = Rails.cache.read("heatmap_data_with_ids_user_#{user.id}")
+    assert_equal first_response_data, cached_data, "Expected the cache to contain the first response data"
+
+    # Add a new changeset to the database
+    create(:changeset, :user => user, :created_at => 3.months.ago, :num_changes => 20)
+
+    # Second request
+    get user_path(user.display_name)
+    second_response_data = assigns(:heatmap_data)
+
+    # Confirm that the cache is still being used
+    assert_equal first_response_data, second_response_data, "Expected cached data to be returned on the second request"
+
+    # Clear the cache and make a third request to confirm new data is retrieved
+    Rails.cache.clear
+    get user_path(user.display_name)
+    third_response_data = assigns(:heatmap_data)
+
+    # Ensure the new entry is now included
+    assert_equal 2, third_response_data.size, "Expected two entries in the heatmap data after clearing the cache"
+
+    # Reset caching config to defaults
+    Rails.cache.clear
+    Rails.cache = @original_cache_store
   end
 
-  def test_auth_failure_callback
-    get auth_failure_path
-    assert_redirected_to login_path
+  def test_show_heatmap_data_no_changesets
+    user = create(:user)
 
-    get auth_failure_path, :params => { :origin => "/" }
-    assert_redirected_to root_path
+    get user_path(user.display_name)
+    assert_response :success
+    # There should be no entries in heatmap data
+    assert_empty assigns(:heatmap_data)
+  end
 
-    get auth_failure_path, :params => { :origin => "http://www.google.com" }
-    assert_redirected_to login_path
+  def test_heatmap_rendering
+    # Test user with no changesets
+    user_without_changesets = create(:user)
+    get user_path(user_without_changesets)
+    assert_response :success
+    assert_select "div#cal-heatmap", 0
+
+    # Test user with changesets
+    user_with_changesets = create(:user)
+    changeset39 = create(:changeset, :user => user_with_changesets, :created_at => 4.months.ago.beginning_of_day, :num_changes => 39)
+    _changeset5 = create(:changeset, :user => user_with_changesets, :created_at => 3.months.ago.beginning_of_day, :num_changes => 5)
+    changeset11 = create(:changeset, :user => user_with_changesets, :created_at => 3.months.ago.beginning_of_day, :num_changes => 11)
+    get user_path(user_with_changesets)
+    assert_response :success
+    assert_select "div#cal-heatmap[data-heatmap]" do |elements|
+      # Check the data-heatmap attribute is present and contains expected JSON
+      heatmap_data = JSON.parse(elements.first["data-heatmap"])
+      expected_data = [
+        { "date" => 4.months.ago.to_date.to_s, "total_changes" => 39, "max_id" => changeset39.id },
+        { "date" => 3.months.ago.to_date.to_s, "total_changes" => 16, "max_id" => changeset11.id }
+      ]
+      assert_equal expected_data, heatmap_data
+    end
   end
 end