]> git.openstreetmap.org Git - rails.git/blob - test/controllers/api/changeset_comments/visibilities_controller_test.rb
Create api changeset comment visibility resource
[rails.git] / test / controllers / api / changeset_comments / visibilities_controller_test.rb
1 require "test_helper"
2
3 module Api
4   module ChangesetComments
5     class VisibilitiesControllerTest < 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_comments/1/visibility", :method => :post },
11           { :controller => "api/changeset_comments/visibilities", :action => "create", :changeset_comment_id => "1" }
12         )
13         assert_routing(
14           { :path => "/api/0.6/changeset_comments/1/visibility.json", :method => :post },
15           { :controller => "api/changeset_comments/visibilities", :action => "create", :changeset_comment_id => "1", :format => "json" }
16         )
17         assert_routing(
18           { :path => "/api/0.6/changeset_comments/1/visibility", :method => :delete },
19           { :controller => "api/changeset_comments/visibilities", :action => "destroy", :changeset_comment_id => "1" }
20         )
21         assert_routing(
22           { :path => "/api/0.6/changeset_comments/1/visibility.json", :method => :delete },
23           { :controller => "api/changeset_comments/visibilities", :action => "destroy", :changeset_comment_id => "1", :format => "json" }
24         )
25
26         assert_recognizes(
27           { :controller => "api/changeset_comments/visibilities", :action => "create", :changeset_comment_id => "1" },
28           { :path => "/api/0.6/changeset/comment/1/unhide", :method => :post }
29         )
30         assert_recognizes(
31           { :controller => "api/changeset_comments/visibilities", :action => "create", :changeset_comment_id => "1", :format => "json" },
32           { :path => "/api/0.6/changeset/comment/1/unhide.json", :method => :post }
33         )
34         assert_recognizes(
35           { :controller => "api/changeset_comments/visibilities", :action => "destroy", :changeset_comment_id => "1" },
36           { :path => "/api/0.6/changeset/comment/1/hide", :method => :post }
37         )
38         assert_recognizes(
39           { :controller => "api/changeset_comments/visibilities", :action => "destroy", :changeset_comment_id => "1", :format => "json" },
40           { :path => "/api/0.6/changeset/comment/1/hide.json", :method => :post }
41         )
42       end
43
44       def test_create_by_unauthorized
45         comment = create(:changeset_comment, :visible => false)
46
47         post api_changeset_comment_visibility_path(comment)
48
49         assert_response :unauthorized
50         assert_not comment.reload.visible
51       end
52
53       def test_create_by_normal_user
54         comment = create(:changeset_comment, :visible => false)
55         auth_header = bearer_authorization_header
56
57         post api_changeset_comment_visibility_path(comment), :headers => auth_header
58
59         assert_response :forbidden
60         assert_not comment.reload.visible
61       end
62
63       def test_create_on_missing_comment
64         auth_header = bearer_authorization_header create(:moderator_user)
65
66         post api_changeset_comment_visibility_path(999111), :headers => auth_header
67
68         assert_response :not_found
69       end
70
71       def test_create_without_required_scope
72         comment = create(:changeset_comment, :visible => false)
73         auth_header = bearer_authorization_header create(:moderator_user), :scopes => %w[read_prefs]
74
75         post api_changeset_comment_visibility_path(comment), :headers => auth_header
76
77         assert_response :forbidden
78         assert_not comment.reload.visible
79       end
80
81       def test_create_with_write_changeset_comments_scope
82         comment = create(:changeset_comment, :visible => false)
83         auth_header = bearer_authorization_header create(:moderator_user), :scopes => %w[write_changeset_comments]
84
85         post api_changeset_comment_visibility_path(comment), :headers => auth_header
86
87         assert_response :success
88         assert_equal "application/xml", response.media_type
89         assert_dom "osm", 1 do
90           assert_dom "> changeset", 1 do
91             assert_dom "> @id", comment.changeset_id.to_s
92             assert_dom "> @comments_count", "1"
93           end
94         end
95
96         assert comment.reload.visible
97       end
98
99       def test_create_with_write_changeset_comments_scope_json
100         comment = create(:changeset_comment, :visible => false)
101         auth_header = bearer_authorization_header create(:moderator_user), :scopes => %w[write_changeset_comments]
102
103         post api_changeset_comment_visibility_path(comment, :format => "json"), :headers => auth_header
104
105         assert_response :success
106         assert_equal "application/json", response.media_type
107         js = ActiveSupport::JSON.decode(@response.body)
108         assert_not_nil js["changeset"]
109         assert_equal comment.changeset_id, js["changeset"]["id"]
110         assert_equal 1, js["changeset"]["comments_count"]
111
112         assert comment.reload.visible
113       end
114
115       def test_create_with_write_api_scope
116         comment = create(:changeset_comment, :visible => false)
117         auth_header = bearer_authorization_header create(:moderator_user), :scopes => %w[write_api]
118
119         post api_changeset_comment_visibility_path(comment), :headers => auth_header
120
121         assert_response :success
122         assert_equal "application/xml", response.media_type
123         assert_dom "osm", 1 do
124           assert_dom "> changeset", 1 do
125             assert_dom "> @id", comment.changeset_id.to_s
126             assert_dom "> @comments_count", "1"
127           end
128         end
129
130         assert comment.reload.visible
131       end
132
133       def test_create_with_write_api_scope_json
134         comment = create(:changeset_comment, :visible => false)
135         auth_header = bearer_authorization_header create(:moderator_user), :scopes => %w[write_api]
136
137         post api_changeset_comment_visibility_path(comment, :format => "json"), :headers => auth_header
138
139         assert_response :success
140         js = ActiveSupport::JSON.decode(@response.body)
141         assert_equal "application/json", response.media_type
142         assert_not_nil js["changeset"]
143         assert_equal comment.changeset_id, js["changeset"]["id"]
144         assert_equal 1, js["changeset"]["comments_count"]
145
146         assert comment.reload.visible
147       end
148
149       def test_destroy_by_unauthorized
150         comment = create(:changeset_comment)
151
152         delete api_changeset_comment_visibility_path(comment)
153
154         assert_response :unauthorized
155         assert comment.reload.visible
156       end
157
158       def test_destroy_by_normal_user
159         comment = create(:changeset_comment)
160         auth_header = bearer_authorization_header
161
162         delete api_changeset_comment_visibility_path(comment), :headers => auth_header
163
164         assert_response :forbidden
165         assert comment.reload.visible
166       end
167
168       def test_destroy_on_missing_comment
169         auth_header = bearer_authorization_header create(:moderator_user)
170
171         delete api_changeset_comment_visibility_path(999111), :headers => auth_header
172
173         assert_response :not_found
174       end
175
176       def test_destroy_without_required_scope
177         comment = create(:changeset_comment)
178         auth_header = bearer_authorization_header create(:moderator_user), :scopes => %w[read_prefs]
179
180         delete api_changeset_comment_visibility_path(comment), :headers => auth_header
181
182         assert_response :forbidden
183         assert comment.reload.visible
184       end
185
186       def test_destroy_with_write_changeset_comments_scope
187         comment = create(:changeset_comment)
188         auth_header = bearer_authorization_header create(:moderator_user), :scopes => %w[write_changeset_comments]
189
190         delete api_changeset_comment_visibility_path(comment), :headers => auth_header
191
192         assert_response :success
193         assert_equal "application/xml", response.media_type
194         assert_dom "osm", 1 do
195           assert_dom "> changeset", 1 do
196             assert_dom "> @id", comment.changeset_id.to_s
197             assert_dom "> @comments_count", "0"
198           end
199         end
200
201         assert_not comment.reload.visible
202       end
203
204       def test_destroy_with_write_changeset_comments_scope_json
205         comment = create(:changeset_comment)
206         auth_header = bearer_authorization_header create(:moderator_user), :scopes => %w[write_changeset_comments]
207
208         delete api_changeset_comment_visibility_path(comment, :format => "json"), :headers => auth_header
209
210         assert_response :success
211         assert_equal "application/json", response.media_type
212         js = ActiveSupport::JSON.decode(@response.body)
213         assert_not_nil js["changeset"]
214         assert_equal comment.changeset_id, js["changeset"]["id"]
215         assert_equal 0, js["changeset"]["comments_count"]
216
217         assert_not comment.reload.visible
218       end
219
220       def test_destroy_with_write_api_scope
221         comment = create(:changeset_comment)
222         auth_header = bearer_authorization_header create(:moderator_user), :scopes => %w[write_api]
223
224         delete api_changeset_comment_visibility_path(comment), :headers => auth_header
225
226         assert_response :success
227         assert_equal "application/xml", response.media_type
228         assert_dom "osm", 1 do
229           assert_dom "> changeset", 1 do
230             assert_dom "> @id", comment.changeset_id.to_s
231             assert_dom "> @comments_count", "0"
232           end
233         end
234
235         assert_not comment.reload.visible
236       end
237
238       def test_destroy_with_write_api_scope_json
239         comment = create(:changeset_comment)
240         auth_header = bearer_authorization_header create(:moderator_user), :scopes => %w[write_api]
241
242         delete api_changeset_comment_visibility_path(comment, :format => "json"), :headers => auth_header
243
244         assert_response :success
245         assert_equal "application/json", response.media_type
246         js = ActiveSupport::JSON.decode(@response.body)
247         assert_not_nil js["changeset"]
248         assert_equal comment.changeset_id, js["changeset"]["id"]
249         assert_equal 0, js["changeset"]["comments_count"]
250
251         assert_not comment.reload.visible
252       end
253     end
254   end
255 end