]> git.openstreetmap.org Git - rails.git/blob - test/controllers/api/old_ways_controller_test.rb
Make api old element show paths resourceful
[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     ##
84     # check that we can retrieve versions of a way
85     def test_show
86       way = create(:way, :with_history)
87       used_way = create(:way, :with_history)
88       create(:relation_member, :member => used_way)
89       way_with_versions = create(:way, :with_history, :version => 4)
90
91       create(:way_tag, :way => way)
92       create(:way_tag, :way => used_way)
93       create(:way_tag, :way => way_with_versions)
94       propagate_tags(way, way.old_ways.last)
95       propagate_tags(used_way, used_way.old_ways.last)
96       propagate_tags(way_with_versions, way_with_versions.old_ways.last)
97
98       check_current_version(way.id)
99       check_current_version(used_way.id)
100       check_current_version(way_with_versions.id)
101     end
102
103     ##
104     # test that redacted ways aren't visible, regardless of
105     # authorisation except as moderator...
106     def test_show_redacted
107       way = create(:way, :with_history, :version => 2)
108       way_v1 = way.old_ways.find_by(:version => 1)
109       way_v1.redact!(create(:redaction))
110
111       get api_way_version_path(way_v1.way_id, way_v1.version)
112       assert_response :forbidden, "Redacted way shouldn't be visible via the version API."
113
114       # not even to a logged-in user
115       auth_header = bearer_authorization_header
116       get api_way_version_path(way_v1.way_id, way_v1.version), :headers => auth_header
117       assert_response :forbidden, "Redacted way shouldn't be visible via the version API, even when logged in."
118     end
119
120     ##
121     # check that returned history is the same as getting all
122     # versions of a way from the api.
123     def test_history_equals_versions
124       way = create(:way, :with_history)
125       used_way = create(:way, :with_history)
126       create(:relation_member, :member => used_way)
127       way_with_versions = create(:way, :with_history, :version => 4)
128
129       check_history_equals_versions(way.id)
130       check_history_equals_versions(used_way.id)
131       check_history_equals_versions(way_with_versions.id)
132     end
133
134     ##
135     # test the redaction of an old version of a way, while not being
136     # authorised.
137     def test_redact_way_unauthorised
138       way = create(:way, :with_history, :version => 4)
139       way_v3 = way.old_ways.find_by(:version => 3)
140
141       do_redact_way(way_v3, create(:redaction))
142       assert_response :unauthorized, "should need to be authenticated to redact."
143     end
144
145     ##
146     # test the redaction of an old version of a way, while being
147     # authorised as a normal user.
148     def test_redact_way_normal_user
149       auth_header = bearer_authorization_header
150       way = create(:way, :with_history, :version => 4)
151       way_v3 = way.old_ways.find_by(:version => 3)
152
153       do_redact_way(way_v3, create(:redaction), auth_header)
154       assert_response :forbidden, "should need to be moderator to redact."
155     end
156
157     ##
158     # test that, even as moderator, the current version of a way
159     # can't be redacted.
160     def test_redact_way_current_version
161       auth_header = bearer_authorization_header create(:moderator_user)
162       way = create(:way, :with_history, :version => 4)
163       way_latest = way.old_ways.last
164
165       do_redact_way(way_latest, create(:redaction), auth_header)
166       assert_response :bad_request, "shouldn't be OK to redact current version as moderator."
167     end
168
169     def test_redact_way_by_regular_without_write_redactions_scope
170       auth_header = bearer_authorization_header(create(:user), :scopes => %w[read_prefs write_api])
171       do_redact_redactable_way(auth_header)
172       assert_response :forbidden, "should need to be moderator to redact."
173     end
174
175     def test_redact_way_by_regular_with_write_redactions_scope
176       auth_header = bearer_authorization_header(create(:user), :scopes => %w[write_redactions])
177       do_redact_redactable_way(auth_header)
178       assert_response :forbidden, "should need to be moderator to redact."
179     end
180
181     def test_redact_way_by_moderator_without_write_redactions_scope
182       auth_header = bearer_authorization_header(create(:moderator_user), :scopes => %w[read_prefs write_api])
183       do_redact_redactable_way(auth_header)
184       assert_response :forbidden, "should need to have write_redactions scope to redact."
185     end
186
187     def test_redact_way_by_moderator_with_write_redactions_scope
188       auth_header = bearer_authorization_header(create(:moderator_user), :scopes => %w[write_redactions])
189       do_redact_redactable_way(auth_header)
190       assert_response :success, "should be OK to redact old version as moderator with write_redactions scope."
191     end
192
193     ##
194     # test the redaction of an old version of a way, while being
195     # authorised as a moderator.
196     def test_redact_way_moderator
197       way = create(:way, :with_history, :version => 4)
198       way_v3 = way.old_ways.find_by(:version => 3)
199       auth_header = bearer_authorization_header create(:moderator_user)
200
201       do_redact_way(way_v3, create(:redaction), auth_header)
202       assert_response :success, "should be OK to redact old version as moderator."
203
204       # check moderator can still see the redacted data, when passing
205       # the appropriate flag
206       get api_way_version_path(way_v3.way_id, way_v3.version), :headers => auth_header
207       assert_response :forbidden, "After redaction, node should be gone for moderator, when flag not passed."
208       get api_way_version_path(way_v3.way_id, way_v3.version, :show_redactions => "true"), :headers => auth_header
209       assert_response :success, "After redaction, node should not be gone for moderator, when flag passed."
210
211       # and when accessed via history
212       get api_way_versions_path(way), :headers => auth_header
213       assert_response :success, "Redaction shouldn't have stopped history working."
214       assert_select "osm way[id='#{way_v3.way_id}'][version='#{way_v3.version}']", 0,
215                     "way #{way_v3.way_id} version #{way_v3.version} should not be present in the history for moderators when not passing flag."
216       get api_way_versions_path(way, :show_redactions => "true"), :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}']", 1,
219                     "way #{way_v3.way_id} version #{way_v3.version} should still be present in the history for moderators when passing flag."
220     end
221
222     # testing that if the moderator drops auth, he can't see the
223     # redacted stuff any more.
224     def test_redact_way_is_redacted
225       way = create(:way, :with_history, :version => 4)
226       way_v3 = way.old_ways.find_by(:version => 3)
227       auth_header = bearer_authorization_header create(:moderator_user)
228
229       do_redact_way(way_v3, create(:redaction), auth_header)
230       assert_response :success, "should be OK to redact old version as moderator."
231
232       # re-auth as non-moderator
233       auth_header = bearer_authorization_header
234
235       # check can't see the redacted data
236       get api_way_version_path(way_v3.way_id, way_v3.version), :headers => auth_header
237       assert_response :forbidden, "Redacted node shouldn't be visible via the version API."
238
239       # and when accessed via history
240       get api_way_versions_path(way), :headers => auth_header
241       assert_response :success, "Redaction shouldn't have stopped history working."
242       assert_select "osm way[id='#{way_v3.way_id}'][version='#{way_v3.version}']", 0,
243                     "redacted way #{way_v3.way_id} version #{way_v3.version} shouldn't be present in the history."
244     end
245
246     ##
247     # test the unredaction of an old version of a way, while not being
248     # authorised.
249     def test_unredact_way_unauthorised
250       way = create(:way, :with_history, :version => 2)
251       way_v1 = way.old_ways.find_by(:version => 1)
252       way_v1.redact!(create(:redaction))
253
254       post way_version_redact_path(way_v1.way_id, way_v1.version)
255       assert_response :unauthorized, "should need to be authenticated to unredact."
256     end
257
258     ##
259     # test the unredaction of an old version of a way, while being
260     # authorised as a normal user.
261     def test_unredact_way_normal_user
262       way = create(:way, :with_history, :version => 2)
263       way_v1 = way.old_ways.find_by(:version => 1)
264       way_v1.redact!(create(:redaction))
265
266       auth_header = bearer_authorization_header
267
268       post way_version_redact_path(way_v1.way_id, way_v1.version), :headers => auth_header
269       assert_response :forbidden, "should need to be moderator to unredact."
270     end
271
272     ##
273     # test the unredaction of an old version of a way, while being
274     # authorised as a moderator.
275     def test_unredact_way_moderator
276       moderator_user = create(:moderator_user)
277       way = create(:way, :with_history, :version => 2)
278       way_v1 = way.old_ways.find_by(:version => 1)
279       way_v1.redact!(create(:redaction))
280
281       auth_header = bearer_authorization_header moderator_user
282
283       post way_version_redact_path(way_v1.way_id, way_v1.version), :headers => auth_header
284       assert_response :success, "should be OK to unredact old version as moderator."
285
286       # check moderator can still see the unredacted data, without passing
287       # the appropriate flag
288       get api_way_version_path(way_v1.way_id, way_v1.version), :headers => auth_header
289       assert_response :success, "After unredaction, node should not be gone for moderator."
290
291       # and when accessed via history
292       get api_way_versions_path(way), :headers => auth_header
293       assert_response :success, "Unredaction shouldn't have stopped history working."
294       assert_select "osm way[id='#{way_v1.way_id}'][version='#{way_v1.version}']", 1,
295                     "way #{way_v1.way_id} version #{way_v1.version} should still be present in the history for moderators."
296
297       auth_header = bearer_authorization_header
298
299       # check normal user can now see the unredacted data
300       get api_way_version_path(way_v1.way_id, way_v1.version), :headers => auth_header
301       assert_response :success, "After redaction, node should not be gone for moderator, when flag passed."
302
303       # and when accessed via history
304       get api_way_versions_path(way), :headers => auth_header
305       assert_response :success, "Redaction shouldn't have stopped history working."
306       assert_select "osm way[id='#{way_v1.way_id}'][version='#{way_v1.version}']", 1,
307                     "way #{way_v1.way_id} version #{way_v1.version} should still be present in the history for normal users."
308     end
309
310     private
311
312     ##
313     # check that the current version of a way is equivalent to the
314     # version which we're getting from the versions call.
315     def check_current_version(way_id)
316       # get the current version
317       current_way = with_controller(WaysController.new) do
318         get api_way_path(way_id)
319         assert_response :success, "can't get current way #{way_id}"
320         Way.from_xml(@response.body)
321       end
322       assert_not_nil current_way, "getting way #{way_id} returned nil"
323
324       # get the "old" version of the way from the version method
325       get api_way_version_path(way_id, current_way.version)
326       assert_response :success, "can't get old way #{way_id}, v#{current_way.version}"
327       old_way = Way.from_xml(@response.body)
328
329       # check that the ways are identical
330       assert_ways_are_equal current_way, old_way
331     end
332
333     ##
334     # look at all the versions of the way in the history and get each version from
335     # the versions call. check that they're the same.
336     def check_history_equals_versions(way_id)
337       get api_way_versions_path(way_id)
338       assert_response :success, "can't get way #{way_id} from API"
339       history_doc = XML::Parser.string(@response.body).parse
340       assert_not_nil history_doc, "parsing way #{way_id} history failed"
341
342       history_doc.find("//osm/way").each do |way_doc|
343         history_way = Way.from_xml_node(way_doc)
344         assert_not_nil history_way, "parsing way #{way_id} version failed"
345
346         get api_way_version_path(way_id, history_way.version)
347         assert_response :success, "couldn't get way #{way_id}, v#{history_way.version}"
348         version_way = Way.from_xml(@response.body)
349         assert_not_nil version_way, "failed to parse #{way_id}, v#{history_way.version}"
350
351         assert_ways_are_equal history_way, version_way
352       end
353     end
354
355     def do_redact_redactable_way(headers = {})
356       way = create(:way, :with_history, :version => 4)
357       way_v3 = way.old_ways.find_by(:version => 3)
358       do_redact_way(way_v3, create(:redaction), headers)
359     end
360
361     def do_redact_way(way, redaction, headers = {})
362       get api_way_version_path(way.way_id, way.version)
363       assert_response :success, "should be able to get version #{way.version} of way #{way.way_id}."
364
365       # now redact it
366       post way_version_redact_path(way.way_id, way.version), :params => { :redaction => redaction.id }, :headers => headers
367     end
368
369     def propagate_tags(way, old_way)
370       way.tags.each do |k, v|
371         create(:old_way_tag, :old_way => old_way, :k => k, :v => v)
372       end
373     end
374   end
375 end