5 class ClosesControllerTest < ActionDispatch::IntegrationTest
7 # test all routes which lead to this controller
10 { :path => "/api/0.6/changeset/1/close", :method => :put },
11 { :controller => "api/changesets/closes", :action => "update", :changeset_id => "1" }
16 # test that the user who opened a change can close it
18 private_user = create(:user, :data_public => false)
19 private_changeset = create(:changeset, :user => private_user)
21 changeset = create(:changeset, :user => user)
23 ## Try without authentication
24 put api_changeset_close_path(changeset)
25 assert_response :unauthorized
27 ## Try using the non-public user
28 auth_header = bearer_authorization_header private_user
29 put api_changeset_close_path(private_changeset), :headers => auth_header
30 assert_require_public_data
32 ## The try with the public user
33 auth_header = bearer_authorization_header user
36 put api_changeset_close_path(cs_id), :headers => auth_header
37 assert_response :success
39 # test that it really is closed now
40 cs = Changeset.find(changeset.id)
42 "changeset should be closed now (#{cs.closed_at} > #{Time.now.utc}.")
46 # test that a different user can't close another user's changeset
47 def test_update_invalid
49 changeset = create(:changeset)
51 auth_header = bearer_authorization_header user
53 put api_changeset_close_path(changeset), :headers => auth_header
54 assert_response :conflict
55 assert_equal "The user doesn't own that changeset", @response.body
59 # test that you can't close using another method
60 def test_update_method_invalid
62 changeset = create(:changeset, :user => user)
64 auth_header = bearer_authorization_header user
66 get api_changeset_close_path(changeset), :headers => auth_header
67 assert_response :not_found
68 assert_template "rescues/routing_error"
70 post api_changeset_close_path(changeset), :headers => auth_header
71 assert_response :not_found
72 assert_template "rescues/routing_error"
76 # check that you can't close a changeset that isn't found
77 def test_update_not_found
78 cs_ids = [0, -132, "123"]
80 # First try to do it with no auth
82 put api_changeset_close_path(id)
83 assert_response :unauthorized, "Shouldn't be able close the non-existant changeset #{id}, when not authorized"
84 rescue ActionController::UrlGenerationError => e
85 assert_match(/No route matches/, e.to_s)
89 auth_header = bearer_authorization_header
91 put api_changeset_close_path(id), :headers => auth_header
92 assert_response :not_found, "The changeset #{id} doesn't exist, so can't be closed"
93 rescue ActionController::UrlGenerationError => e
94 assert_match(/No route matches/, e.to_s)