]> git.openstreetmap.org Git - rails.git/blob - test/controllers/users/issued_blocks_controller_test.rb
Merge remote-tracking branch 'upstream/pull/5457'
[rails.git] / test / controllers / users / issued_blocks_controller_test.rb
1 require "test_helper"
2 require_relative "../user_blocks/table_test_helper"
3
4 module Users
5   class IssuedBlocksControllerTest < 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_by", :method => :get },
13         { :controller => "users/issued_blocks", :action => "show", :user_display_name => "username" }
14       )
15     end
16
17     def test_show
18       moderator_user = create(:moderator_user)
19       second_moderator_user = create(:moderator_user)
20       normal_user = create(:user)
21       active_block = create(:user_block, :creator => moderator_user)
22       expired_block = create(:user_block, :expired, :creator => second_moderator_user)
23       revoked_block = create(:user_block, :revoked, :creator => second_moderator_user)
24
25       # Asking for a list of blocks with a bogus user name should fail
26       get user_issued_blocks_path("non_existent_user")
27       assert_response :not_found
28       assert_template "users/no_such_user"
29       assert_select "h1", "The user non_existent_user does not exist"
30
31       # Check the list of blocks given by one moderator
32       get user_issued_blocks_path(moderator_user)
33       assert_response :success
34       assert_select "h1 a[href='#{user_path moderator_user}']", :text => moderator_user.display_name
35       assert_select "a.active[href='#{user_issued_blocks_path moderator_user}']"
36       assert_select "table#block_list tbody", :count => 1 do
37         assert_select "tr", 1
38         assert_select "a[href='#{user_block_path(active_block)}']", 1
39       end
40
41       # Check the list of blocks given by a different moderator
42       get user_issued_blocks_path(second_moderator_user)
43       assert_response :success
44       assert_select "h1 a[href='#{user_path second_moderator_user}']", :text => second_moderator_user.display_name
45       assert_select "a.active[href='#{user_issued_blocks_path second_moderator_user}']"
46       assert_select "table#block_list tbody", :count => 1 do
47         assert_select "tr", 2
48         assert_select "a[href='#{user_block_path(expired_block)}']", 1
49         assert_select "a[href='#{user_block_path(revoked_block)}']", 1
50       end
51
52       # Check the list of blocks (not) given by a normal user
53       get user_issued_blocks_path(normal_user)
54       assert_response :success
55       assert_select "table#block_list", false
56       assert_select "p", "#{normal_user.display_name} has not made any blocks yet."
57     end
58
59     def test_show_paged
60       user = create(:moderator_user)
61       user_blocks = create_list(:user_block, 50, :creator => user).reverse
62       next_path = user_issued_blocks_path(user)
63
64       get next_path
65       assert_response :success
66       check_user_blocks_table user_blocks[0...20]
67       check_no_page_link "Newer Blocks"
68       next_path = check_page_link "Older Blocks"
69
70       get next_path
71       assert_response :success
72       check_user_blocks_table user_blocks[20...40]
73       check_page_link "Newer Blocks"
74       next_path = check_page_link "Older Blocks"
75
76       get next_path
77       assert_response :success
78       check_user_blocks_table user_blocks[40...50]
79       check_page_link "Newer Blocks"
80       check_no_page_link "Older Blocks"
81     end
82
83     def test_show_invalid_paged
84       user = create(:moderator_user)
85
86       %w[-1 0 fred].each do |id|
87         get user_issued_blocks_path(user, :before => id)
88         assert_redirected_to :controller => "/errors", :action => :bad_request
89
90         get user_issued_blocks_path(user, :after => id)
91         assert_redirected_to :controller => "/errors", :action => :bad_request
92       end
93     end
94   end
95 end