]> git.openstreetmap.org Git - rails.git/blob - test/controllers/users/received_blocks_controller_test.rb
Merge remote-tracking branch 'upstream/pull/5457'
[rails.git] / test / controllers / users / received_blocks_controller_test.rb
1 require "test_helper"
2 require_relative "../user_blocks/table_test_helper"
3
4 module Users
5   class ReceivedBlocksControllerTest < ActionDispatch::IntegrationTest
6     include UserBlocks::TableTestHelper
7
8     ##
9     # test all routes which lead to this controller
10     def test_routes
11       assert_routing(
12         { :path => "/user/username/blocks", :method => :get },
13         { :controller => "users/received_blocks", :action => "show", :user_display_name => "username" }
14       )
15       assert_routing(
16         { :path => "/user/username/blocks/edit", :method => :get },
17         { :controller => "users/received_blocks", :action => "edit", :user_display_name => "username" }
18       )
19       assert_routing(
20         { :path => "/user/username/blocks", :method => :delete },
21         { :controller => "users/received_blocks", :action => "destroy", :user_display_name => "username" }
22       )
23     end
24
25     def test_show
26       blocked_user = create(:user)
27       unblocked_user = create(:user)
28       normal_user = create(:user)
29       active_block = create(:user_block, :user => blocked_user)
30       revoked_block = create(:user_block, :revoked, :user => blocked_user)
31       expired_block = create(:user_block, :expired, :user => unblocked_user)
32
33       # Asking for a list of blocks with a bogus user name should fail
34       get user_received_blocks_path("non_existent_user")
35       assert_response :not_found
36       assert_template "users/no_such_user"
37       assert_select "h1", "The user non_existent_user does not exist"
38
39       # Check the list of blocks for a user that has never been blocked
40       get user_received_blocks_path(normal_user)
41       assert_response :success
42       assert_select "table#block_list", false
43       assert_select "p", "#{normal_user.display_name} has not been blocked yet."
44
45       # Check the list of blocks for a user that is currently blocked
46       get user_received_blocks_path(blocked_user)
47       assert_response :success
48       assert_select "h1 a[href='#{user_path blocked_user}']", :text => blocked_user.display_name
49       assert_select "a.active[href='#{user_received_blocks_path blocked_user}']"
50       assert_select "table#block_list tbody", :count => 1 do
51         assert_select "tr", 2
52         assert_select "a[href='#{user_block_path(active_block)}']", 1
53         assert_select "a[href='#{user_block_path(revoked_block)}']", 1
54       end
55
56       # Check the list of blocks for a user that has previously been blocked
57       get user_received_blocks_path(unblocked_user)
58       assert_response :success
59       assert_select "h1 a[href='#{user_path unblocked_user}']", :text => unblocked_user.display_name
60       assert_select "a.active[href='#{user_received_blocks_path unblocked_user}']"
61       assert_select "table#block_list tbody", :count => 1 do
62         assert_select "tr", 1
63         assert_select "a[href='#{user_block_path(expired_block)}']", 1
64       end
65     end
66
67     def test_show_paged
68       user = create(:user)
69       user_blocks = create_list(:user_block, 50, :user => user).reverse
70       next_path = user_received_blocks_path(user)
71
72       get next_path
73       assert_response :success
74       check_user_blocks_table user_blocks[0...20]
75       check_no_page_link "Newer Blocks"
76       next_path = check_page_link "Older Blocks"
77
78       get next_path
79       assert_response :success
80       check_user_blocks_table user_blocks[20...40]
81       check_page_link "Newer Blocks"
82       next_path = check_page_link "Older Blocks"
83
84       get next_path
85       assert_response :success
86       check_user_blocks_table user_blocks[40...50]
87       check_page_link "Newer Blocks"
88       check_no_page_link "Older Blocks"
89     end
90
91     def test_show_invalid_paged
92       user = create(:user)
93
94       %w[-1 0 fred].each do |id|
95         get user_received_blocks_path(user, :before => id)
96         assert_redirected_to :controller => "/errors", :action => :bad_request
97
98         get user_received_blocks_path(user, :after => id)
99         assert_redirected_to :controller => "/errors", :action => :bad_request
100       end
101     end
102
103     ##
104     # test the revoke all blocks page
105     def test_edit
106       blocked_user = create(:user)
107       create(:user_block, :user => blocked_user)
108
109       # Asking for the revoke all blocks page with a bogus user name should fail
110       get user_received_blocks_path("non_existent_user")
111       assert_response :not_found
112
113       # Check that the revoke all blocks page requires us to login
114       get edit_user_received_blocks_path(blocked_user)
115       assert_redirected_to login_path(:referer => edit_user_received_blocks_path(blocked_user))
116
117       # Login as a normal user
118       session_for(create(:user))
119
120       # Check that normal users can't load the revoke all blocks page
121       get edit_user_received_blocks_path(blocked_user)
122       assert_redirected_to :controller => "/errors", :action => "forbidden"
123
124       # Login as a moderator
125       session_for(create(:moderator_user))
126
127       # Check that the revoke all blocks page loads for moderators
128       get edit_user_received_blocks_path(blocked_user)
129       assert_response :success
130       assert_select "h1 a[href='#{user_path blocked_user}']", :text => blocked_user.display_name
131     end
132
133     ##
134     # test the revoke all action
135     def test_destroy
136       blocked_user = create(:user)
137       active_block1 = create(:user_block, :user => blocked_user)
138       active_block2 = create(:user_block, :user => blocked_user)
139       expired_block1 = create(:user_block, :expired, :user => blocked_user)
140       blocks = [active_block1, active_block2, expired_block1]
141       moderator_user = create(:moderator_user)
142
143       assert_predicate active_block1, :active?
144       assert_predicate active_block2, :active?
145       assert_not_predicate expired_block1, :active?
146
147       # Check that normal users can't revoke all blocks
148       session_for(create(:user))
149       delete user_received_blocks_path(blocked_user, :confirm => true)
150       assert_redirected_to :controller => "/errors", :action => "forbidden"
151
152       blocks.each(&:reload)
153       assert_predicate active_block1, :active?
154       assert_predicate active_block2, :active?
155       assert_not_predicate expired_block1, :active?
156
157       # Check that confirmation is required
158       session_for(moderator_user)
159       delete user_received_blocks_path(blocked_user)
160
161       blocks.each(&:reload)
162       assert_predicate active_block1, :active?
163       assert_predicate active_block2, :active?
164       assert_not_predicate expired_block1, :active?
165
166       # Check that moderators can revoke all blocks
167       delete user_received_blocks_path(blocked_user, :confirm => true)
168       assert_redirected_to user_received_blocks_path(blocked_user)
169
170       blocks.each(&:reload)
171       assert_not_predicate active_block1, :active?
172       assert_not_predicate active_block2, :active?
173       assert_not_predicate expired_block1, :active?
174       assert_equal moderator_user, active_block1.revoker
175       assert_equal moderator_user, active_block2.revoker
176       assert_not_equal moderator_user, expired_block1.revoker
177     end
178   end
179 end