]> git.openstreetmap.org Git - rails.git/blob - test/controllers/api/changesets/closes_controller_test.rb
Move api changeset close tests to their own module
[rails.git] / test / controllers / api / changesets / closes_controller_test.rb
1 require "test_helper"
2
3 module Api
4   module Changesets
5     class ClosesControllerTest < ActionDispatch::IntegrationTest
6       ##
7       # test all routes which lead to this controller
8       def test_routes
9         assert_routing(
10           { :path => "/api/0.6/changeset/1/close", :method => :put },
11           { :controller => "api/changesets/closes", :action => "update", :changeset_id => "1" }
12         )
13       end
14
15       ##
16       # test that the user who opened a change can close it
17       def test_update
18         private_user = create(:user, :data_public => false)
19         private_changeset = create(:changeset, :user => private_user)
20         user = create(:user)
21         changeset = create(:changeset, :user => user)
22
23         ## Try without authentication
24         put api_changeset_close_path(changeset)
25         assert_response :unauthorized
26
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
31
32         ## The try with the public user
33         auth_header = bearer_authorization_header user
34
35         cs_id = changeset.id
36         put api_changeset_close_path(cs_id), :headers => auth_header
37         assert_response :success
38
39         # test that it really is closed now
40         cs = Changeset.find(changeset.id)
41         assert_not(cs.open?,
42                    "changeset should be closed now (#{cs.closed_at} > #{Time.now.utc}.")
43       end
44
45       ##
46       # test that a different user can't close another user's changeset
47       def test_update_invalid
48         user = create(:user)
49         changeset = create(:changeset)
50
51         auth_header = bearer_authorization_header user
52
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
56       end
57
58       ##
59       # test that you can't close using another method
60       def test_update_method_invalid
61         user = create(:user)
62         changeset = create(:changeset, :user => user)
63
64         auth_header = bearer_authorization_header user
65
66         get api_changeset_close_path(changeset), :headers => auth_header
67         assert_response :not_found
68         assert_template "rescues/routing_error"
69
70         post api_changeset_close_path(changeset), :headers => auth_header
71         assert_response :not_found
72         assert_template "rescues/routing_error"
73       end
74
75       ##
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"]
79
80         # First try to do it with no auth
81         cs_ids.each do |id|
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)
86         end
87
88         # Now try with auth
89         auth_header = bearer_authorization_header
90         cs_ids.each do |id|
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)
95         end
96       end
97     end
98   end
99 end