]> git.openstreetmap.org Git - rails.git/blob - test/integration/user_blocks_test.rb
38bb3338a6c15a455de3ce1229ba6a59d47d577d
[rails.git] / test / integration / user_blocks_test.rb
1 require "test_helper"
2
3 class UserBlocksTest < ActionDispatch::IntegrationTest
4   def auth_header(user, pass)
5     { "HTTP_AUTHORIZATION" => format("Basic %s", Base64.encode64("#{user}:#{pass}")) }
6   end
7
8   def test_api_blocked
9     blocked_user = create(:user)
10
11     get "/api/#{API_VERSION}/user/details"
12     assert_response :unauthorized
13
14     get "/api/#{API_VERSION}/user/details", nil, auth_header(blocked_user.display_name, "test")
15     assert_response :success
16
17     # now block the user
18     UserBlock.create(
19       :user_id => blocked_user.id,
20       :creator_id => create(:moderator_user).id,
21       :reason => "testing",
22       :ends_at => Time.now.getutc + 5.minutes
23     )
24     get "/api/#{API_VERSION}/user/details", nil, auth_header(blocked_user.display_name, "test")
25     assert_response :forbidden
26   end
27
28   def test_api_revoke
29     blocked_user = create(:user)
30     moderator = create(:moderator_user)
31
32     block = UserBlock.create(
33       :user_id => blocked_user.id,
34       :creator_id => moderator.id,
35       :reason => "testing",
36       :ends_at => Time.now.getutc + 5.minutes
37     )
38     get "/api/#{API_VERSION}/user/details", nil, auth_header(blocked_user.display_name, "test")
39     assert_response :forbidden
40
41     # revoke the ban
42     get "/login"
43     assert_response :success
44     post "/login", "username" => moderator.email, "password" => "test", :referer => "/blocks/#{block.id}/revoke"
45     assert_response :redirect
46     follow_redirect!
47     assert_response :success
48     assert_template "user_blocks/revoke"
49     post "/blocks/#{block.id}/revoke", "confirm" => "yes"
50     assert_response :redirect
51     follow_redirect!
52     assert_response :success
53     assert_template "user_blocks/show"
54     reset!
55
56     # access the API again. this time it should work
57     get "/api/#{API_VERSION}/user/details", nil, auth_header(blocked_user.display_name, "test")
58     assert_response :success
59   end
60 end