1 # frozen_string_literal: true
6 class HeatmapsControllerTest < ActionDispatch::IntegrationTest
8 # test all routes which lead to this controller
11 { :path => "/user/username/heatmap", :method => :get },
12 { :controller => "users/heatmaps", :action => "show", :user_display_name => "username" }
18 # Create two changesets
19 create(:changeset, :user => user, :created_at => 6.months.ago, :num_changes => 10)
20 create(:changeset, :user => user, :created_at => 3.months.ago, :num_changes => 20)
22 get user_heatmap_path(user)
24 assert_response :success
25 # The data should not be empty
26 heatmap_data = assigns(:heatmap_data)
27 assert_not_nil heatmap_data
28 assert_predicate heatmap_data[:data], :any?
29 # The data should be in the right format
30 heatmap_data[:data].each_value do |entry|
31 assert_equal [:date, :max_id, :total_changes], entry.keys.sort, "Heatmap data entries should have expected keys"
33 assert_equal 30, heatmap_data[:count]
36 def test_show_data_caching
37 # Enable caching to be able to test
39 @original_cache_store = Rails.cache
40 Rails.cache = ActiveSupport::Cache::MemoryStore.new
44 # Create an initial changeset
45 create(:changeset, :user => user, :created_at => 6.months.ago, :num_changes => 15)
47 # First request to populate the cache
48 get user_heatmap_path(user)
49 first_response_data = assigns(:heatmap_data)
50 assert_not_nil first_response_data, "Expected heatmap data to be assigned on the first request"
51 assert_equal 1, first_response_data[:data].values.count { |day| day[:total_changes].positive? }, "Expected one entry in the heatmap data"
53 # Inspect cache after the first request
54 cached_data = Rails.cache.read("heatmap_data_of_user_#{user.id}")
55 assert_equal first_response_data, cached_data, "Expected the cache to contain the first response data"
57 # Add a new changeset to the database
58 create(:changeset, :user => user, :created_at => 3.months.ago, :num_changes => 20)
61 get user_heatmap_path(user)
62 second_response_data = assigns(:heatmap_data)
64 # Confirm that the cache is still being used
65 assert_equal first_response_data, second_response_data, "Expected cached data to be returned on the second request"
67 # Clear the cache and make a third request to confirm new data is retrieved
69 get user_heatmap_path(user)
70 third_response_data = assigns(:heatmap_data)
72 # Ensure the new entry is now included
73 assert_equal 2, third_response_data[:data].values.count { |day| day[:total_changes].positive? }, "Expected two entries in the heatmap data after clearing the cache"
75 # Reset caching config to defaults
77 Rails.cache = @original_cache_store
80 def test_show_data_no_changesets
83 get user_heatmap_path(user)
85 assert_response :success
86 assert_empty(assigns(:heatmap_data)[:data].values)
87 assert_select ".heatmap", :count => 0
90 def test_show_data_suspended_user
91 user = create(:user, :suspended)
92 # Create two changesets
93 create(:changeset, :user => user, :created_at => 6.months.ago, :num_changes => 10)
94 create(:changeset, :user => user, :created_at => 3.months.ago, :num_changes => 20)
96 get user_heatmap_path(user)
98 # Should fail for suspended users
99 assert_response :not_found
101 session_for(create(:administrator_user))
103 get user_heatmap_path(user)
105 # Should work when requested by an administrator
106 assert_response :success
107 # The data should not be empty
108 heatmap_data = assigns(:heatmap_data)
109 assert_not_nil heatmap_data
110 assert_predicate heatmap_data[:data], :any?
111 # The data should be in the right format
112 heatmap_data[:data].each_value do |entry|
113 assert_equal [:date, :max_id, :total_changes], entry.keys.sort, "Heatmap data entries should have expected keys"
115 assert_equal 30, heatmap_data[:count]
118 def test_show_data_deleted_user
119 user = create(:user, :deleted)
120 # Create two changesets
121 create(:changeset, :user => user, :created_at => 6.months.ago, :num_changes => 10)
122 create(:changeset, :user => user, :created_at => 3.months.ago, :num_changes => 20)
124 get user_heatmap_path(user)
126 # Should fail for deleted users
127 assert_response :not_found
129 session_for(create(:administrator_user))
131 get user_heatmap_path(user)
133 # Should work when requested by an administrator
134 assert_response :success
135 # The data should not be empty
136 heatmap_data = assigns(:heatmap_data)
137 assert_not_nil heatmap_data
138 assert_predicate heatmap_data[:data], :any?
139 # The data should be in the right format
140 heatmap_data[:data].each_value do |entry|
141 assert_equal [:date, :max_id, :total_changes], entry.keys.sort, "Heatmap data entries should have expected keys"
143 assert_equal 30, heatmap_data[:count]
146 def test_show_data_unknown_user
147 get user_heatmap_path(:user_display_name => "unknown_user")
149 # Should fail for unknown users
150 assert_response :not_found
152 session_for(create(:administrator_user))
154 get user_heatmap_path(:user_display_name => "unknown_user")
156 # Should still fail when requested by an administrator
157 assert_response :not_found
160 def test_show_not_public
162 user.update(:public_heatmap => false)
164 get user_heatmap_path(user)
165 assert_response :not_found
167 session_for(create(:user))
168 get user_heatmap_path(user)
169 assert_response :not_found
171 session_for(create(:moderator_user))
172 get user_heatmap_path(user)
173 assert_response :not_found
175 session_for(create(:administrator_user))
176 get user_heatmap_path(user)
177 assert_response :not_found
180 def test_show_rendering_of_user_with_no_changesets
181 user_without_changesets = create(:user)
183 get user_heatmap_path(user_without_changesets)
185 assert_response :success
186 assert_select ".heatmap", 0
189 def test_show_rendering_of_user_with_changesets
191 changeset39 = create(:changeset, :user => user, :created_at => 4.months.ago.beginning_of_day, :num_changes => 39)
192 _changeset5 = create(:changeset, :user => user, :created_at => 3.months.ago.beginning_of_day, :num_changes => 5)
193 changeset11 = create(:changeset, :user => user, :created_at => 3.months.ago.beginning_of_day, :num_changes => 11)
195 get user_heatmap_path(user)
197 assert_response :success
198 assert_select ".heatmap a", 2
200 history_path = user_history_path(user)
201 assert_select ".heatmap a[data-date='#{4.months.ago.to_date}'][data-count='39'][href='#{history_path}?before=#{changeset39.id + 1}']"
202 assert_select ".heatmap a[data-date='#{3.months.ago.to_date}'][data-count='16'][href='#{history_path}?before=#{changeset11.id + 1}']"
203 assert_select ".heatmap [data-date='#{5.months.ago.to_date}']:not([data-count])"
206 def test_headline_changeset_zero
209 get user_heatmap_path(user)
211 assert_response :success
212 assert_select "h2.text-body-secondary.fs-5", :count => 0
215 def test_headline_changeset_singular
217 create(:changeset, :user => user, :created_at => 4.months.ago.beginning_of_day, :num_changes => 1)
219 get user_heatmap_path(user)
221 assert_response :success
222 assert_select "h2.text-body-secondary.fs-5", :text => "1 contribution in the last year"
225 def test_headline_changeset_plural
227 create(:changeset, :user => user, :created_at => 4.months.ago.beginning_of_day, :num_changes => 12)
229 get user_heatmap_path(user)
231 assert_response :success
232 assert_select "h2.text-body-secondary.fs-5", :text => "12 contributions in the last year"