]> git.openstreetmap.org Git - rails.git/blob - test/controllers/users/lists_controller_test.rb
Merge remote-tracking branch 'upstream/pull/5725'
[rails.git] / test / controllers / users / lists_controller_test.rb
1 require "test_helper"
2
3 module Users
4   class ListsControllerTest < ActionDispatch::IntegrationTest
5     ##
6     # test all routes which lead to this controller
7     def test_routes
8       assert_routing(
9         { :path => "/users", :method => :get },
10         { :controller => "users/lists", :action => "show" }
11       )
12       assert_routing(
13         { :path => "/users", :method => :put },
14         { :controller => "users/lists", :action => "update" }
15       )
16       assert_routing(
17         { :path => "/users/status", :method => :get },
18         { :controller => "users/lists", :action => "show", :status => "status" }
19       )
20       assert_routing(
21         { :path => "/users/status", :method => :put },
22         { :controller => "users/lists", :action => "update", :status => "status" }
23       )
24     end
25
26     def test_show
27       user = create(:user)
28       moderator_user = create(:moderator_user)
29       administrator_user = create(:administrator_user)
30       suspended_user = create(:user, :suspended)
31       name_user = create(:user, :display_name => "Test User")
32       email_user = create(:user, :email => "test@example.com")
33       ip_user = create(:user, :creation_address => "1.2.3.4")
34
35       # There are now 9 users - the 7 above, plus two extra "granters" for the
36       # moderator_user and administrator_user
37       assert_equal 9, User.count
38
39       # Shouldn't work when not logged in
40       get users_list_path
41       assert_redirected_to login_path(:referer => users_list_path)
42
43       session_for(user)
44
45       # Shouldn't work when logged in as a normal user
46       get users_list_path
47       assert_redirected_to :controller => "/errors", :action => :forbidden
48
49       session_for(moderator_user)
50
51       # Shouldn't work when logged in as a moderator
52       get users_list_path
53       assert_redirected_to :controller => "/errors", :action => :forbidden
54
55       session_for(administrator_user)
56
57       # Note there is a header row, so all row counts are users + 1
58       # Should work when logged in as an administrator
59       get users_list_path
60       assert_response :success
61       assert_template :show
62       assert_select "table#user_list tbody tr", :count => 9
63
64       # Should be able to limit by status
65       get users_list_path, :params => { :status => "suspended" }
66       assert_response :success
67       assert_template :show
68       assert_select "table#user_list tbody tr", :count => 1 do
69         assert_select "a[href='#{user_path(suspended_user)}']", :count => 1
70       end
71
72       # Should be able to limit by name
73       get users_list_path, :params => { :username => "Test User" }
74       assert_response :success
75       assert_template :show
76       assert_select "table#user_list tbody tr", :count => 1 do
77         assert_select "a[href='#{user_path(name_user)}']", :count => 1
78       end
79
80       # Should be able to limit by name ignoring case
81       get users_list_path, :params => { :username => "test user" }
82       assert_response :success
83       assert_template :show
84       assert_select "table#user_list tbody tr", :count => 1 do
85         assert_select "a[href='#{user_path(name_user)}']", :count => 1
86       end
87
88       # Should be able to limit by email
89       get users_list_path, :params => { :username => "test@example.com" }
90       assert_response :success
91       assert_template :show
92       assert_select "table#user_list tbody tr", :count => 1 do
93         assert_select "a[href='#{user_path(email_user)}']", :count => 1
94       end
95
96       # Should be able to limit by email ignoring case
97       get users_list_path, :params => { :username => "TEST@example.com" }
98       assert_response :success
99       assert_template :show
100       assert_select "table#user_list tbody tr", :count => 1 do
101         assert_select "a[href='#{user_path(email_user)}']", :count => 1
102       end
103
104       # Should be able to limit by IP address
105       get users_list_path, :params => { :ip => "1.2.3.4" }
106       assert_response :success
107       assert_template :show
108       assert_select "table#user_list tbody tr", :count => 1 do
109         assert_select "a[href='#{user_path(ip_user)}']", :count => 1
110       end
111     end
112
113     def test_show_paginated
114       1.upto(100).each do |n|
115         User.create(:display_name => "extra_#{n}",
116                     :email => "extra#{n}@example.com",
117                     :pass_crypt => "extraextra")
118       end
119
120       session_for(create(:administrator_user))
121
122       # 100 examples, an administrator, and a granter for the admin.
123       assert_equal 102, User.count
124       next_path = users_list_path
125
126       get next_path
127       assert_response :success
128       assert_template :show
129       assert_select "table#user_list tbody tr", :count => 50
130       check_no_page_link "Newer Users"
131       next_path = check_page_link "Older Users"
132
133       get next_path
134       assert_response :success
135       assert_template :show
136       assert_select "table#user_list tbody tr", :count => 50
137       check_page_link "Newer Users"
138       next_path = check_page_link "Older Users"
139
140       get next_path
141       assert_response :success
142       assert_template :show
143       assert_select "table#user_list tbody tr", :count => 2
144       check_page_link "Newer Users"
145       check_no_page_link "Older Users"
146     end
147
148     def test_show_invalid_paginated
149       session_for(create(:administrator_user))
150
151       %w[-1 0 fred].each do |id|
152         get users_list_path(:before => id)
153         assert_redirected_to :controller => "/errors", :action => :bad_request
154
155         get users_list_path(:after => id)
156         assert_redirected_to :controller => "/errors", :action => :bad_request
157       end
158     end
159
160     def test_update_confirm
161       inactive_user = create(:user, :pending)
162       suspended_user = create(:user, :suspended)
163
164       # Shouldn't work when not logged in
165       assert_no_difference "User.active.count" do
166         put users_list_path, :params => { :confirm => 1, :user => { inactive_user.id => 1, suspended_user.id => 1 } }
167       end
168       assert_response :forbidden
169
170       assert_equal "pending", inactive_user.reload.status
171       assert_equal "suspended", suspended_user.reload.status
172
173       session_for(create(:user))
174
175       # Shouldn't work when logged in as a normal user
176       assert_no_difference "User.active.count" do
177         put users_list_path, :params => { :confirm => 1, :user => { inactive_user.id => 1, suspended_user.id => 1 } }
178       end
179       assert_redirected_to :controller => "/errors", :action => :forbidden
180       assert_equal "pending", inactive_user.reload.status
181       assert_equal "suspended", suspended_user.reload.status
182
183       session_for(create(:moderator_user))
184
185       # Shouldn't work when logged in as a moderator
186       assert_no_difference "User.active.count" do
187         put users_list_path, :params => { :confirm => 1, :user => { inactive_user.id => 1, suspended_user.id => 1 } }
188       end
189       assert_redirected_to :controller => "/errors", :action => :forbidden
190       assert_equal "pending", inactive_user.reload.status
191       assert_equal "suspended", suspended_user.reload.status
192
193       session_for(create(:administrator_user))
194
195       # Should work when logged in as an administrator
196       assert_difference "User.active.count", 2 do
197         put users_list_path, :params => { :confirm => 1, :user => { inactive_user.id => 1, suspended_user.id => 1 } }
198       end
199       assert_redirected_to :action => :show
200       assert_equal "confirmed", inactive_user.reload.status
201       assert_equal "confirmed", suspended_user.reload.status
202     end
203
204     def test_update_hide
205       normal_user = create(:user)
206       confirmed_user = create(:user, :confirmed)
207
208       # Shouldn't work when not logged in
209       assert_no_difference "User.active.count" do
210         put users_list_path, :params => { :hide => 1, :user => { normal_user.id => 1, confirmed_user.id => 1 } }
211       end
212       assert_response :forbidden
213
214       assert_equal "active", normal_user.reload.status
215       assert_equal "confirmed", confirmed_user.reload.status
216
217       session_for(create(:user))
218
219       # Shouldn't work when logged in as a normal user
220       assert_no_difference "User.active.count" do
221         put users_list_path, :params => { :hide => 1, :user => { normal_user.id => 1, confirmed_user.id => 1 } }
222       end
223       assert_redirected_to :controller => "/errors", :action => :forbidden
224       assert_equal "active", normal_user.reload.status
225       assert_equal "confirmed", confirmed_user.reload.status
226
227       session_for(create(:moderator_user))
228
229       # Shouldn't work when logged in as a moderator
230       assert_no_difference "User.active.count" do
231         put users_list_path, :params => { :hide => 1, :user => { normal_user.id => 1, confirmed_user.id => 1 } }
232       end
233       assert_redirected_to :controller => "/errors", :action => :forbidden
234       assert_equal "active", normal_user.reload.status
235       assert_equal "confirmed", confirmed_user.reload.status
236
237       session_for(create(:administrator_user))
238
239       # Should work when logged in as an administrator
240       assert_difference "User.active.count", -2 do
241         put users_list_path, :params => { :hide => 1, :user => { normal_user.id => 1, confirmed_user.id => 1 } }
242       end
243       assert_redirected_to :action => :show
244       assert_equal "deleted", normal_user.reload.status
245       assert_equal "deleted", confirmed_user.reload.status
246     end
247
248     private
249
250     def check_no_page_link(name)
251       assert_select "a.page-link", { :text => /#{Regexp.quote(name)}/, :count => 0 }, "unexpected #{name} page link"
252     end
253
254     def check_page_link(name)
255       assert_select "a.page-link", { :text => /#{Regexp.quote(name)}/ }, "missing #{name} page link" do |buttons|
256         return buttons.first.attributes["href"].value
257       end
258     end
259   end
260 end