2 require_relative "../user_blocks/table_test_helper"
5 class ReceivedBlocksControllerTest < ActionDispatch::IntegrationTest
6 include UserBlocks::TableTestHelper
9 # test all routes which lead to this controller
12 { :path => "/user/username/blocks", :method => :get },
13 { :controller => "users/received_blocks", :action => "show", :user_display_name => "username" }
16 { :path => "/user/username/blocks/edit", :method => :get },
17 { :controller => "users/received_blocks", :action => "edit", :user_display_name => "username" }
20 { :path => "/user/username/blocks", :method => :delete },
21 { :controller => "users/received_blocks", :action => "destroy", :user_display_name => "username" }
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)
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"
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."
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
52 assert_select "a[href='#{user_block_path(active_block)}']", 1
53 assert_select "a[href='#{user_block_path(revoked_block)}']", 1
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
63 assert_select "a[href='#{user_block_path(expired_block)}']", 1
69 user_blocks = create_list(:user_block, 50, :user => user).reverse
70 next_path = user_received_blocks_path(user)
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"
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"
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"
91 def test_show_invalid_paged
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
98 get user_received_blocks_path(user, :after => id)
99 assert_redirected_to :controller => "/errors", :action => :bad_request
104 # test the revoke all blocks page
106 blocked_user = create(:user)
107 create(:user_block, :user => blocked_user)
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
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))
117 # Login as a normal user
118 session_for(create(:user))
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"
124 # Login as a moderator
125 session_for(create(:moderator_user))
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
134 # test the revoke all action
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)
143 assert_predicate active_block1, :active?
144 assert_predicate active_block2, :active?
145 assert_not_predicate expired_block1, :active?
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"
152 blocks.each(&:reload)
153 assert_predicate active_block1, :active?
154 assert_predicate active_block2, :active?
155 assert_not_predicate expired_block1, :active?
157 # Check that confirmation is required
158 session_for(moderator_user)
159 delete user_received_blocks_path(blocked_user)
161 blocks.each(&:reload)
162 assert_predicate active_block1, :active?
163 assert_predicate active_block2, :active?
164 assert_not_predicate expired_block1, :active?
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)
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