]> git.openstreetmap.org Git - rails.git/blob - test/controllers/api/old_ways_controller_test.rb
Merge remote-tracking branch 'upstream/pull/5645'
[rails.git] / test / controllers / api / old_ways_controller_test.rb
1 require "test_helper"
2
3 module Api
4   class OldWaysControllerTest < ActionDispatch::IntegrationTest
5     ##
6     # test all routes which lead to this controller
7     def test_routes
8       assert_routing(
9         { :path => "/api/0.6/way/1/history", :method => :get },
10         { :controller => "api/old_ways", :action => "index", :way_id => "1" }
11       )
12       assert_routing(
13         { :path => "/api/0.6/way/1/history.json", :method => :get },
14         { :controller => "api/old_ways", :action => "index", :way_id => "1", :format => "json" }
15       )
16       assert_routing(
17         { :path => "/api/0.6/way/1/2", :method => :get },
18         { :controller => "api/old_ways", :action => "show", :way_id => "1", :version => "2" }
19       )
20       assert_routing(
21         { :path => "/api/0.6/way/1/2.json", :method => :get },
22         { :controller => "api/old_ways", :action => "show", :way_id => "1", :version => "2", :format => "json" }
23       )
24       assert_routing(
25         { :path => "/api/0.6/way/1/2/redact", :method => :post },
26         { :controller => "api/old_ways", :action => "redact", :way_id => "1", :version => "2" }
27       )
28     end
29
30     ##
31     # check that a visible way is returned properly
32     def test_index
33       way = create(:way, :with_history, :version => 2)
34
35       get api_way_versions_path(way)
36
37       assert_response :success
38       assert_dom "osm:root", 1 do
39         assert_dom "> way", 2 do |dom_ways|
40           assert_dom dom_ways[0], "> @id", way.id.to_s
41           assert_dom dom_ways[0], "> @version", "1"
42
43           assert_dom dom_ways[1], "> @id", way.id.to_s
44           assert_dom dom_ways[1], "> @version", "2"
45         end
46       end
47     end
48
49     ##
50     # check that an invisible way's history is returned properly
51     def test_index_invisible
52       get api_way_versions_path(create(:way, :with_history, :deleted))
53       assert_response :success
54     end
55
56     ##
57     # check chat a non-existent way is not returned
58     def test_index_invalid
59       get api_way_versions_path(0)
60       assert_response :not_found
61     end
62
63     ##
64     # test that redacted ways aren't visible in the history
65     def test_index_redacted
66       way = create(:way, :with_history, :version => 2)
67       way_v1 = way.old_ways.find_by(:version => 1)
68       way_v1.redact!(create(:redaction))
69
70       get api_way_versions_path(way)
71       assert_response :success, "Redaction shouldn't have stopped history working."
72       assert_select "osm way[id='#{way_v1.way_id}'][version='#{way_v1.version}']", 0,
73                     "redacted way #{way_v1.way_id} version #{way_v1.version} shouldn't be present in the history."
74
75       # not even to a logged-in user
76       auth_header = bearer_authorization_header
77       get api_way_versions_path(way), :headers => auth_header
78       assert_response :success, "Redaction shouldn't have stopped history working."
79       assert_select "osm way[id='#{way_v1.way_id}'][version='#{way_v1.version}']", 0,
80                     "redacted node #{way_v1.way_id} version #{way_v1.version} shouldn't be present in the history, even when logged in."
81     end
82
83     def test_show
84       way = create(:way, :with_history, :version => 2)
85
86       get api_way_version_path(way, 1)
87
88       assert_response :success
89       assert_dom "osm:root", 1 do
90         assert_dom "> way", 1 do
91           assert_dom "> @id", way.id.to_s
92           assert_dom "> @version", "1"
93         end
94       end
95
96       get api_way_version_path(way, 2)
97
98       assert_response :success
99       assert_dom "osm:root", 1 do
100         assert_dom "> way", 1 do
101           assert_dom "> @id", way.id.to_s
102           assert_dom "> @version", "2"
103         end
104       end
105     end
106
107     ##
108     # test that redacted ways aren't visible, regardless of
109     # authorisation except as moderator...
110     def test_show_redacted
111       way = create(:way, :with_history, :version => 2)
112       way_v1 = way.old_ways.find_by(:version => 1)
113       way_v1.redact!(create(:redaction))
114
115       get api_way_version_path(way_v1.way_id, way_v1.version)
116       assert_response :forbidden, "Redacted way shouldn't be visible via the version API."
117
118       # not even to a logged-in user
119       auth_header = bearer_authorization_header
120       get api_way_version_path(way_v1.way_id, way_v1.version), :headers => auth_header
121       assert_response :forbidden, "Redacted way shouldn't be visible via the version API, even when logged in."
122     end
123
124     ##
125     # check that returned history is the same as getting all
126     # versions of a way from the api.
127     def test_history_equals_versions
128       way = create(:way, :with_history)
129       used_way = create(:way, :with_history)
130       create(:relation_member, :member => used_way)
131       way_with_versions = create(:way, :with_history, :version => 4)
132
133       check_history_equals_versions(way.id)
134       check_history_equals_versions(used_way.id)
135       check_history_equals_versions(way_with_versions.id)
136     end
137
138     ##
139     # test the redaction of an old version of a way, while not being
140     # authorised.
141     def test_redact_way_unauthorised
142       way = create(:way, :with_history, :version => 4)
143       way_v3 = way.old_ways.find_by(:version => 3)
144
145       do_redact_way(way_v3, create(:redaction))
146       assert_response :unauthorized, "should need to be authenticated to redact."
147     end
148
149     ##
150     # test the redaction of an old version of a way, while being
151     # authorised as a normal user.
152     def test_redact_way_normal_user
153       auth_header = bearer_authorization_header
154       way = create(:way, :with_history, :version => 4)
155       way_v3 = way.old_ways.find_by(:version => 3)
156
157       do_redact_way(way_v3, create(:redaction), auth_header)
158       assert_response :forbidden, "should need to be moderator to redact."
159     end
160
161     ##
162     # test that, even as moderator, the current version of a way
163     # can't be redacted.
164     def test_redact_way_current_version
165       auth_header = bearer_authorization_header create(:moderator_user)
166       way = create(:way, :with_history, :version => 4)
167       way_latest = way.old_ways.last
168
169       do_redact_way(way_latest, create(:redaction), auth_header)
170       assert_response :bad_request, "shouldn't be OK to redact current version as moderator."
171     end
172
173     def test_redact_way_by_regular_without_write_redactions_scope
174       auth_header = bearer_authorization_header(create(:user), :scopes => %w[read_prefs write_api])
175       do_redact_redactable_way(auth_header)
176       assert_response :forbidden, "should need to be moderator to redact."
177     end
178
179     def test_redact_way_by_regular_with_write_redactions_scope
180       auth_header = bearer_authorization_header(create(:user), :scopes => %w[write_redactions])
181       do_redact_redactable_way(auth_header)
182       assert_response :forbidden, "should need to be moderator to redact."
183     end
184
185     def test_redact_way_by_moderator_without_write_redactions_scope
186       auth_header = bearer_authorization_header(create(:moderator_user), :scopes => %w[read_prefs write_api])
187       do_redact_redactable_way(auth_header)
188       assert_response :forbidden, "should need to have write_redactions scope to redact."
189     end
190
191     def test_redact_way_by_moderator_with_write_redactions_scope
192       auth_header = bearer_authorization_header(create(:moderator_user), :scopes => %w[write_redactions])
193       do_redact_redactable_way(auth_header)
194       assert_response :success, "should be OK to redact old version as moderator with write_redactions scope."
195     end
196
197     ##
198     # test the redaction of an old version of a way, while being
199     # authorised as a moderator.
200     def test_redact_way_moderator
201       way = create(:way, :with_history, :version => 4)
202       way_v3 = way.old_ways.find_by(:version => 3)
203       auth_header = bearer_authorization_header create(:moderator_user)
204
205       do_redact_way(way_v3, create(:redaction), auth_header)
206       assert_response :success, "should be OK to redact old version as moderator."
207
208       # check moderator can still see the redacted data, when passing
209       # the appropriate flag
210       get api_way_version_path(way_v3.way_id, way_v3.version), :headers => auth_header
211       assert_response :forbidden, "After redaction, node should be gone for moderator, when flag not passed."
212       get api_way_version_path(way_v3.way_id, way_v3.version, :show_redactions => "true"), :headers => auth_header
213       assert_response :success, "After redaction, node should not be gone for moderator, when flag passed."
214
215       # and when accessed via history
216       get api_way_versions_path(way), :headers => auth_header
217       assert_response :success, "Redaction shouldn't have stopped history working."
218       assert_select "osm way[id='#{way_v3.way_id}'][version='#{way_v3.version}']", 0,
219                     "way #{way_v3.way_id} version #{way_v3.version} should not be present in the history for moderators when not passing flag."
220       get api_way_versions_path(way, :show_redactions => "true"), :headers => auth_header
221       assert_response :success, "Redaction shouldn't have stopped history working."
222       assert_select "osm way[id='#{way_v3.way_id}'][version='#{way_v3.version}']", 1,
223                     "way #{way_v3.way_id} version #{way_v3.version} should still be present in the history for moderators when passing flag."
224     end
225
226     # testing that if the moderator drops auth, he can't see the
227     # redacted stuff any more.
228     def test_redact_way_is_redacted
229       way = create(:way, :with_history, :version => 4)
230       way_v3 = way.old_ways.find_by(:version => 3)
231       auth_header = bearer_authorization_header create(:moderator_user)
232
233       do_redact_way(way_v3, create(:redaction), auth_header)
234       assert_response :success, "should be OK to redact old version as moderator."
235
236       # re-auth as non-moderator
237       auth_header = bearer_authorization_header
238
239       # check can't see the redacted data
240       get api_way_version_path(way_v3.way_id, way_v3.version), :headers => auth_header
241       assert_response :forbidden, "Redacted node shouldn't be visible via the version API."
242
243       # and when accessed via history
244       get api_way_versions_path(way), :headers => auth_header
245       assert_response :success, "Redaction shouldn't have stopped history working."
246       assert_select "osm way[id='#{way_v3.way_id}'][version='#{way_v3.version}']", 0,
247                     "redacted way #{way_v3.way_id} version #{way_v3.version} shouldn't be present in the history."
248     end
249
250     ##
251     # test the unredaction of an old version of a way, while not being
252     # authorised.
253     def test_unredact_way_unauthorised
254       way = create(:way, :with_history, :version => 2)
255       way_v1 = way.old_ways.find_by(:version => 1)
256       way_v1.redact!(create(:redaction))
257
258       post way_version_redact_path(way_v1.way_id, way_v1.version)
259       assert_response :unauthorized, "should need to be authenticated to unredact."
260     end
261
262     ##
263     # test the unredaction of an old version of a way, while being
264     # authorised as a normal user.
265     def test_unredact_way_normal_user
266       way = create(:way, :with_history, :version => 2)
267       way_v1 = way.old_ways.find_by(:version => 1)
268       way_v1.redact!(create(:redaction))
269
270       auth_header = bearer_authorization_header
271
272       post way_version_redact_path(way_v1.way_id, way_v1.version), :headers => auth_header
273       assert_response :forbidden, "should need to be moderator to unredact."
274     end
275
276     ##
277     # test the unredaction of an old version of a way, while being
278     # authorised as a moderator.
279     def test_unredact_way_moderator
280       moderator_user = create(:moderator_user)
281       way = create(:way, :with_history, :version => 2)
282       way_v1 = way.old_ways.find_by(:version => 1)
283       way_v1.redact!(create(:redaction))
284
285       auth_header = bearer_authorization_header moderator_user
286
287       post way_version_redact_path(way_v1.way_id, way_v1.version), :headers => auth_header
288       assert_response :success, "should be OK to unredact old version as moderator."
289
290       # check moderator can still see the unredacted data, without passing
291       # the appropriate flag
292       get api_way_version_path(way_v1.way_id, way_v1.version), :headers => auth_header
293       assert_response :success, "After unredaction, node should not be gone for moderator."
294
295       # and when accessed via history
296       get api_way_versions_path(way), :headers => auth_header
297       assert_response :success, "Unredaction shouldn't have stopped history working."
298       assert_select "osm way[id='#{way_v1.way_id}'][version='#{way_v1.version}']", 1,
299                     "way #{way_v1.way_id} version #{way_v1.version} should still be present in the history for moderators."
300
301       auth_header = bearer_authorization_header
302
303       # check normal user can now see the unredacted data
304       get api_way_version_path(way_v1.way_id, way_v1.version), :headers => auth_header
305       assert_response :success, "After redaction, node should not be gone for moderator, when flag passed."
306
307       # and when accessed via history
308       get api_way_versions_path(way), :headers => auth_header
309       assert_response :success, "Redaction shouldn't have stopped history working."
310       assert_select "osm way[id='#{way_v1.way_id}'][version='#{way_v1.version}']", 1,
311                     "way #{way_v1.way_id} version #{way_v1.version} should still be present in the history for normal users."
312     end
313
314     private
315
316     ##
317     # look at all the versions of the way in the history and get each version from
318     # the versions call. check that they're the same.
319     def check_history_equals_versions(way_id)
320       get api_way_versions_path(way_id)
321       assert_response :success, "can't get way #{way_id} from API"
322       history_doc = XML::Parser.string(@response.body).parse
323       assert_not_nil history_doc, "parsing way #{way_id} history failed"
324
325       history_doc.find("//osm/way").each do |way_doc|
326         history_way = Way.from_xml_node(way_doc)
327         assert_not_nil history_way, "parsing way #{way_id} version failed"
328
329         get api_way_version_path(way_id, history_way.version)
330         assert_response :success, "couldn't get way #{way_id}, v#{history_way.version}"
331         version_way = Way.from_xml(@response.body)
332         assert_not_nil version_way, "failed to parse #{way_id}, v#{history_way.version}"
333
334         assert_ways_are_equal history_way, version_way
335       end
336     end
337
338     def do_redact_redactable_way(headers = {})
339       way = create(:way, :with_history, :version => 4)
340       way_v3 = way.old_ways.find_by(:version => 3)
341       do_redact_way(way_v3, create(:redaction), headers)
342     end
343
344     def do_redact_way(way, redaction, headers = {})
345       get api_way_version_path(way.way_id, way.version)
346       assert_response :success, "should be able to get version #{way.version} of way #{way.way_id}."
347
348       # now redact it
349       post way_version_redact_path(way.way_id, way.version), :params => { :redaction => redaction.id }, :headers => headers
350     end
351   end
352 end