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_rendering_of_user_with_no_changesets
161 user_without_changesets = create(:user)
163 get user_heatmap_path(user_without_changesets)
165 assert_response :success
166 assert_select ".heatmap", 0
169 def test_show_rendering_of_user_with_changesets
171 changeset39 = create(:changeset, :user => user, :created_at => 4.months.ago.beginning_of_day, :num_changes => 39)
172 _changeset5 = create(:changeset, :user => user, :created_at => 3.months.ago.beginning_of_day, :num_changes => 5)
173 changeset11 = create(:changeset, :user => user, :created_at => 3.months.ago.beginning_of_day, :num_changes => 11)
175 get user_heatmap_path(user)
177 assert_response :success
178 assert_select ".heatmap a", 2
180 history_path = user_history_path(user)
181 assert_select ".heatmap a[data-date='#{4.months.ago.to_date}'][data-count='39'][href='#{history_path}?before=#{changeset39.id + 1}']"
182 assert_select ".heatmap a[data-date='#{3.months.ago.to_date}'][data-count='16'][href='#{history_path}?before=#{changeset11.id + 1}']"
183 assert_select ".heatmap [data-date='#{5.months.ago.to_date}']:not([data-count])"
186 def test_headline_changeset_zero
189 get user_heatmap_path(user)
191 assert_response :success
192 assert_select "h2.text-body-secondary.fs-5", :count => 0
195 def test_headline_changeset_singular
197 create(:changeset, :user => user, :created_at => 4.months.ago.beginning_of_day, :num_changes => 1)
199 get user_heatmap_path(user)
201 assert_response :success
202 assert_select "h2.text-body-secondary.fs-5", :text => "1 contribution in the last year"
205 def test_headline_changeset_plural
207 create(:changeset, :user => user, :created_at => 4.months.ago.beginning_of_day, :num_changes => 12)
209 get user_heatmap_path(user)
211 assert_response :success
212 assert_select "h2.text-body-secondary.fs-5", :text => "12 contributions in the last year"