]> git.openstreetmap.org Git - rails.git/blob - test/controllers/api/old_nodes_controller_test.rb
Convert api element history actions to versions resources
[rails.git] / test / controllers / api / old_nodes_controller_test.rb
1 require "test_helper"
2
3 module Api
4   class OldNodesControllerTest < ActionDispatch::IntegrationTest
5     ##
6     # test all routes which lead to this controller
7     def test_routes
8       assert_routing(
9         { :path => "/api/0.6/node/1/history", :method => :get },
10         { :controller => "api/old_nodes", :action => "index", :node_id => "1" }
11       )
12       assert_routing(
13         { :path => "/api/0.6/node/1/history.json", :method => :get },
14         { :controller => "api/old_nodes", :action => "index", :node_id => "1", :format => "json" }
15       )
16       assert_routing(
17         { :path => "/api/0.6/node/1/2", :method => :get },
18         { :controller => "api/old_nodes", :action => "show", :id => "1", :version => "2" }
19       )
20       assert_routing(
21         { :path => "/api/0.6/node/1/2.json", :method => :get },
22         { :controller => "api/old_nodes", :action => "show", :id => "1", :version => "2", :format => "json" }
23       )
24       assert_routing(
25         { :path => "/api/0.6/node/1/2/redact", :method => :post },
26         { :controller => "api/old_nodes", :action => "redact", :id => "1", :version => "2" }
27       )
28     end
29
30     def test_index
31       node = create(:node, :version => 2)
32       create(:old_node, :node_id => node.id, :version => 1, :latitude => 60 * OldNode::SCALE, :longitude => 30 * OldNode::SCALE)
33       create(:old_node, :node_id => node.id, :version => 2, :latitude => 61 * OldNode::SCALE, :longitude => 31 * OldNode::SCALE)
34
35       get api_node_versions_path(node)
36
37       assert_response :success
38       assert_dom "osm:root", 1 do
39         assert_dom "> node", 2 do |dom_nodes|
40           assert_dom dom_nodes[0], "> @id", node.id.to_s
41           assert_dom dom_nodes[0], "> @version", "1"
42           assert_dom dom_nodes[0], "> @lat", "60.0000000"
43           assert_dom dom_nodes[0], "> @lon", "30.0000000"
44
45           assert_dom dom_nodes[1], "> @id", node.id.to_s
46           assert_dom dom_nodes[1], "> @version", "2"
47           assert_dom dom_nodes[1], "> @lat", "61.0000000"
48           assert_dom dom_nodes[1], "> @lon", "31.0000000"
49         end
50       end
51     end
52
53     ##
54     # test that redacted nodes aren't visible in the history
55     def test_index_redacted
56       node = create(:node, :with_history, :version => 2)
57       node_v1 = node.old_nodes.find_by(:version => 1)
58       node_v1.redact!(create(:redaction))
59
60       get api_node_versions_path(node)
61       assert_response :success, "Redaction shouldn't have stopped history working."
62       assert_select "osm node[id='#{node_v1.node_id}'][version='#{node_v1.version}']", 0,
63                     "redacted node #{node_v1.node_id} version #{node_v1.version} shouldn't be present in the history."
64
65       # not even to a logged-in user
66       auth_header = bearer_authorization_header
67       get api_node_versions_path(node), :headers => auth_header
68       assert_response :success, "Redaction shouldn't have stopped history working."
69       assert_select "osm node[id='#{node_v1.node_id}'][version='#{node_v1.version}']", 0,
70                     "redacted node #{node_v1.node_id} version #{node_v1.version} shouldn't be present in the history, even when logged in."
71     end
72
73     ##
74     # test the version call by submitting several revisions of a new node
75     # to the API and ensuring that later calls to version return the
76     # matching versions of the object.
77     #
78     ##
79     # FIXME: Move this test to being an integration test since it spans multiple controllers
80     def test_version
81       private_user = create(:user, :data_public => false)
82       private_node = create(:node, :with_history, :version => 4, :lat => 0, :lon => 0, :changeset => create(:changeset, :user => private_user))
83       user = create(:user)
84       node = create(:node, :with_history, :version => 4, :lat => 0, :lon => 0, :changeset => create(:changeset, :user => user))
85       create_list(:node_tag, 2, :node => node)
86       # Ensure that the current tags are propagated to the history too
87       propagate_tags(node, node.old_nodes.last)
88
89       ## First try this with a non-public user
90       auth_header = bearer_authorization_header private_user
91
92       # setup a simple XML node
93       xml_doc = xml_for_node(private_node)
94       xml_node = xml_doc.find("//osm/node").first
95       nodeid = private_node.id
96
97       # keep a hash of the versions => string, as we'll need something
98       # to test against later
99       versions = {}
100
101       # save a version for later checking
102       versions[xml_node["version"]] = xml_doc.to_s
103
104       # randomly move the node about
105       3.times do
106         # move the node somewhere else
107         xml_node["lat"] = precision(rand - 0.5).to_s
108         xml_node["lon"] = precision(rand - 0.5).to_s
109         with_controller(NodesController.new) do
110           put api_node_path(nodeid), :params => xml_doc.to_s, :headers => auth_header
111           assert_response :forbidden, "Should have rejected node update"
112           xml_node["version"] = @response.body.to_s
113         end
114         # save a version for later checking
115         versions[xml_node["version"]] = xml_doc.to_s
116       end
117
118       # add a bunch of random tags
119       3.times do
120         xml_tag = XML::Node.new("tag")
121         xml_tag["k"] = random_string
122         xml_tag["v"] = random_string
123         xml_node << xml_tag
124         with_controller(NodesController.new) do
125           put api_node_path(nodeid), :params => xml_doc.to_s, :headers => auth_header
126           assert_response :forbidden,
127                           "should have rejected node #{nodeid} (#{@response.body}) with forbidden"
128           xml_node["version"] = @response.body.to_s
129         end
130         # save a version for later checking
131         versions[xml_node["version"]] = xml_doc.to_s
132       end
133
134       # probably should check that they didn't get written to the database
135
136       ## Now do it with the public user
137       auth_header = bearer_authorization_header user
138
139       # setup a simple XML node
140
141       xml_doc = xml_for_node(node)
142       xml_node = xml_doc.find("//osm/node").first
143       nodeid = node.id
144
145       # keep a hash of the versions => string, as we'll need something
146       # to test against later
147       versions = {}
148
149       # save a version for later checking
150       versions[xml_node["version"]] = xml_doc.to_s
151
152       # randomly move the node about
153       3.times do
154         # move the node somewhere else
155         xml_node["lat"] = precision(rand - 0.5).to_s
156         xml_node["lon"] = precision(rand - 0.5).to_s
157         with_controller(NodesController.new) do
158           put api_node_path(nodeid), :params => xml_doc.to_s, :headers => auth_header
159           assert_response :success
160           xml_node["version"] = @response.body.to_s
161         end
162         # save a version for later checking
163         versions[xml_node["version"]] = xml_doc.to_s
164       end
165
166       # add a bunch of random tags
167       3.times do
168         xml_tag = XML::Node.new("tag")
169         xml_tag["k"] = random_string
170         xml_tag["v"] = random_string
171         xml_node << xml_tag
172         with_controller(NodesController.new) do
173           put api_node_path(nodeid), :params => xml_doc.to_s, :headers => auth_header
174           assert_response :success,
175                           "couldn't update node #{nodeid} (#{@response.body})"
176           xml_node["version"] = @response.body.to_s
177         end
178         # save a version for later checking
179         versions[xml_node["version"]] = xml_doc.to_s
180       end
181
182       # check all the versions
183       versions.each_key do |key|
184         get api_old_node_path(nodeid, key.to_i)
185
186         assert_response :success,
187                         "couldn't get version #{key.to_i} of node #{nodeid}"
188
189         check_node = Node.from_xml(versions[key])
190         api_node = Node.from_xml(@response.body.to_s)
191
192         assert_nodes_are_equal check_node, api_node
193       end
194     end
195
196     def test_not_found_version
197       check_not_found_id_version(70000, 312344)
198       check_not_found_id_version(-1, -13)
199       check_not_found_id_version(create(:node).id, 24354)
200       check_not_found_id_version(24356, create(:node).version)
201     end
202
203     ##
204     # Test that getting the current version is identical to picking
205     # that version with the version URI call.
206     def test_current_version
207       node = create(:node, :with_history)
208       used_node = create(:node, :with_history)
209       create(:way_node, :node => used_node)
210       node_used_by_relationship = create(:node, :with_history)
211       create(:relation_member, :member => node_used_by_relationship)
212       node_with_versions = create(:node, :with_history, :version => 4)
213
214       create(:node_tag, :node => node)
215       create(:node_tag, :node => used_node)
216       create(:node_tag, :node => node_used_by_relationship)
217       create(:node_tag, :node => node_with_versions)
218       propagate_tags(node, node.old_nodes.last)
219       propagate_tags(used_node, used_node.old_nodes.last)
220       propagate_tags(node_used_by_relationship, node_used_by_relationship.old_nodes.last)
221       propagate_tags(node_with_versions, node_with_versions.old_nodes.last)
222
223       check_current_version(node)
224       check_current_version(used_node)
225       check_current_version(node_used_by_relationship)
226       check_current_version(node_with_versions)
227     end
228
229     # Ensure the lat/lon is formatted as a decimal e.g. not 4.0e-05
230     def test_lat_lon_xml_format
231       old_node = create(:old_node, :latitude => (0.00004 * OldNode::SCALE).to_i, :longitude => (0.00008 * OldNode::SCALE).to_i)
232
233       get api_node_versions_path(old_node.node_id)
234       assert_match(/lat="0.0000400"/, response.body)
235       assert_match(/lon="0.0000800"/, response.body)
236     end
237
238     ##
239     # test the redaction of an old version of a node, while not being
240     # authorised.
241     def test_redact_node_unauthorised
242       node = create(:node, :with_history, :version => 4)
243       node_v3 = node.old_nodes.find_by(:version => 3)
244
245       do_redact_node(node_v3,
246                      create(:redaction))
247       assert_response :unauthorized, "should need to be authenticated to redact."
248     end
249
250     ##
251     # test the redaction of an old version of a node, while being
252     # authorised as a normal user.
253     def test_redact_node_normal_user
254       auth_header = bearer_authorization_header
255
256       node = create(:node, :with_history, :version => 4)
257       node_v3 = node.old_nodes.find_by(:version => 3)
258
259       do_redact_node(node_v3,
260                      create(:redaction),
261                      auth_header)
262       assert_response :forbidden, "should need to be moderator to redact."
263     end
264
265     ##
266     # test that, even as moderator, the current version of a node
267     # can't be redacted.
268     def test_redact_node_current_version
269       auth_header = bearer_authorization_header create(:moderator_user)
270
271       node = create(:node, :with_history, :version => 4)
272       node_v4 = node.old_nodes.find_by(:version => 4)
273
274       do_redact_node(node_v4,
275                      create(:redaction),
276                      auth_header)
277       assert_response :bad_request, "shouldn't be OK to redact current version as moderator."
278     end
279
280     def test_redact_node_by_regular_without_write_redactions_scope
281       auth_header = bearer_authorization_header(create(:user), :scopes => %w[read_prefs write_api])
282       do_redact_redactable_node(auth_header)
283       assert_response :forbidden, "should need to be moderator to redact."
284     end
285
286     def test_redact_node_by_regular_with_write_redactions_scope
287       auth_header = bearer_authorization_header(create(:user), :scopes => %w[write_redactions])
288       do_redact_redactable_node(auth_header)
289       assert_response :forbidden, "should need to be moderator to redact."
290     end
291
292     def test_redact_node_by_moderator_without_write_redactions_scope
293       auth_header = bearer_authorization_header(create(:moderator_user), :scopes => %w[read_prefs write_api])
294       do_redact_redactable_node(auth_header)
295       assert_response :forbidden, "should need to have write_redactions scope to redact."
296     end
297
298     def test_redact_node_by_moderator_with_write_redactions_scope
299       auth_header = bearer_authorization_header(create(:moderator_user), :scopes => %w[write_redactions])
300       do_redact_redactable_node(auth_header)
301       assert_response :success, "should be OK to redact old version as moderator with write_redactions scope."
302     end
303
304     ##
305     # test that redacted nodes aren't visible, regardless of
306     # authorisation except as moderator...
307     def test_version_redacted
308       node = create(:node, :with_history, :version => 2)
309       node_v1 = node.old_nodes.find_by(:version => 1)
310       node_v1.redact!(create(:redaction))
311
312       get api_old_node_path(node_v1.node_id, node_v1.version)
313       assert_response :forbidden, "Redacted node shouldn't be visible via the version API."
314
315       # not even to a logged-in user
316       auth_header = bearer_authorization_header
317       get api_old_node_path(node_v1.node_id, node_v1.version), :headers => auth_header
318       assert_response :forbidden, "Redacted node shouldn't be visible via the version API, even when logged in."
319     end
320
321     ##
322     # test the redaction of an old version of a node, while being
323     # authorised as a moderator.
324     def test_redact_node_moderator
325       node = create(:node, :with_history, :version => 4)
326       node_v3 = node.old_nodes.find_by(:version => 3)
327       auth_header = bearer_authorization_header create(:moderator_user)
328
329       do_redact_node(node_v3, create(:redaction), auth_header)
330       assert_response :success, "should be OK to redact old version as moderator."
331
332       # check moderator can still see the redacted data, when passing
333       # the appropriate flag
334       get api_old_node_path(node_v3.node_id, node_v3.version), :headers => auth_header
335       assert_response :forbidden, "After redaction, node should be gone for moderator, when flag not passed."
336       get api_old_node_path(node_v3.node_id, node_v3.version, :show_redactions => "true"), :headers => auth_header
337       assert_response :success, "After redaction, node should not be gone for moderator, when flag passed."
338
339       # and when accessed via history
340       get api_node_versions_path(node)
341       assert_response :success, "Redaction shouldn't have stopped history working."
342       assert_select "osm node[id='#{node_v3.node_id}'][version='#{node_v3.version}']", 0,
343                     "node #{node_v3.node_id} version #{node_v3.version} should not be present in the history for moderators when not passing flag."
344       get api_node_versions_path(node, :show_redactions => "true"), :headers => auth_header
345       assert_response :success, "Redaction shouldn't have stopped history working."
346       assert_select "osm node[id='#{node_v3.node_id}'][version='#{node_v3.version}']", 1,
347                     "node #{node_v3.node_id} version #{node_v3.version} should still be present in the history for moderators when passing flag."
348     end
349
350     # testing that if the moderator drops auth, he can't see the
351     # redacted stuff any more.
352     def test_redact_node_is_redacted
353       node = create(:node, :with_history, :version => 4)
354       node_v3 = node.old_nodes.find_by(:version => 3)
355       auth_header = bearer_authorization_header create(:moderator_user)
356
357       do_redact_node(node_v3, create(:redaction), auth_header)
358       assert_response :success, "should be OK to redact old version as moderator."
359
360       # re-auth as non-moderator
361       auth_header = bearer_authorization_header
362
363       # check can't see the redacted data
364       get api_old_node_path(node_v3.node_id, node_v3.version), :headers => auth_header
365       assert_response :forbidden, "Redacted node shouldn't be visible via the version API."
366
367       # and when accessed via history
368       get api_node_versions_path(node), :headers => auth_header
369       assert_response :success, "Redaction shouldn't have stopped history working."
370       assert_select "osm node[id='#{node_v3.node_id}'][version='#{node_v3.version}']", 0,
371                     "redacted node #{node_v3.node_id} version #{node_v3.version} shouldn't be present in the history."
372     end
373
374     ##
375     # test the unredaction of an old version of a node, while not being
376     # authorised.
377     def test_unredact_node_unauthorised
378       node = create(:node, :with_history, :version => 2)
379       node_v1 = node.old_nodes.find_by(:version => 1)
380       node_v1.redact!(create(:redaction))
381
382       post node_version_redact_path(node_v1.node_id, node_v1.version)
383       assert_response :unauthorized, "should need to be authenticated to unredact."
384     end
385
386     ##
387     # test the unredaction of an old version of a node, while being
388     # authorised as a normal user.
389     def test_unredact_node_normal_user
390       user = create(:user)
391       node = create(:node, :with_history, :version => 2)
392       node_v1 = node.old_nodes.find_by(:version => 1)
393       node_v1.redact!(create(:redaction))
394
395       auth_header = bearer_authorization_header user
396
397       post node_version_redact_path(node_v1.node_id, node_v1.version), :headers => auth_header
398       assert_response :forbidden, "should need to be moderator to unredact."
399     end
400
401     ##
402     # test the unredaction of an old version of a node, while being
403     # authorised as a moderator.
404     def test_unredact_node_moderator
405       moderator_user = create(:moderator_user)
406       node = create(:node, :with_history, :version => 2)
407       node_v1 = node.old_nodes.find_by(:version => 1)
408       node_v1.redact!(create(:redaction))
409
410       auth_header = bearer_authorization_header moderator_user
411
412       post node_version_redact_path(node_v1.node_id, node_v1.version), :headers => auth_header
413       assert_response :success, "should be OK to unredact old version as moderator."
414
415       # check moderator can now see the redacted data, when not
416       # passing the aspecial flag
417       get api_old_node_path(node_v1.node_id, node_v1.version), :headers => auth_header
418       assert_response :success, "After unredaction, node should not be gone for moderator."
419
420       # and when accessed via history
421       get api_node_versions_path(node)
422       assert_response :success, "Unredaction shouldn't have stopped history working."
423       assert_select "osm node[id='#{node_v1.node_id}'][version='#{node_v1.version}']", 1,
424                     "node #{node_v1.node_id} version #{node_v1.version} should now be present in the history for moderators without passing flag."
425
426       auth_header = bearer_authorization_header
427
428       # check normal user can now see the redacted data
429       get api_old_node_path(node_v1.node_id, node_v1.version), :headers => auth_header
430       assert_response :success, "After unredaction, node should be visible to normal users."
431
432       # and when accessed via history
433       get api_node_versions_path(node)
434       assert_response :success, "Unredaction shouldn't have stopped history working."
435       assert_select "osm node[id='#{node_v1.node_id}'][version='#{node_v1.version}']", 1,
436                     "node #{node_v1.node_id} version #{node_v1.version} should now be present in the history for normal users without passing flag."
437     end
438
439     private
440
441     def do_redact_redactable_node(headers = {})
442       node = create(:node, :with_history, :version => 4)
443       node_v3 = node.old_nodes.find_by(:version => 3)
444       do_redact_node(node_v3, create(:redaction), headers)
445     end
446
447     def do_redact_node(node, redaction, headers = {})
448       get api_old_node_path(node.node_id, node.version), :headers => headers
449       assert_response :success, "should be able to get version #{node.version} of node #{node.node_id}."
450
451       # now redact it
452       post node_version_redact_path(node.node_id, node.version), :params => { :redaction => redaction.id }, :headers => headers
453     end
454
455     def check_current_version(node_id)
456       # get the current version of the node
457       current_node = with_controller(NodesController.new) do
458         get api_node_path(node_id)
459         assert_response :success, "cant get current node #{node_id}"
460         Node.from_xml(@response.body)
461       end
462       assert_not_nil current_node, "getting node #{node_id} returned nil"
463
464       # get the "old" version of the node from the old_node interface
465       get api_old_node_path(node_id, current_node.version)
466       assert_response :success, "cant get old node #{node_id}, v#{current_node.version}"
467       old_node = Node.from_xml(@response.body)
468
469       # check the nodes are the same
470       assert_nodes_are_equal current_node, old_node
471     end
472
473     def check_not_found_id_version(id, version)
474       get api_old_node_path(id, version)
475       assert_response :not_found
476     rescue ActionController::UrlGenerationError => e
477       assert_match(/No route matches/, e.to_s)
478     end
479
480     ##
481     # returns a 16 character long string with some nasty characters in it.
482     # this ought to stress-test the tag handling as well as the versioning.
483     def random_string
484       letters = [["!", '"', "$", "&", ";", "@"],
485                  ("a".."z").to_a,
486                  ("A".."Z").to_a,
487                  ("0".."9").to_a].flatten
488       (1..16).map { letters[rand(letters.length)] }.join
489     end
490
491     ##
492     # truncate a floating point number to the scale that it is stored in
493     # the database. otherwise rounding errors can produce failing unit
494     # tests when they shouldn't.
495     def precision(f)
496       (f * GeoRecord::SCALE).round.to_f / GeoRecord::SCALE
497     end
498
499     def propagate_tags(node, old_node)
500       node.tags.each do |k, v|
501         create(:old_node_tag, :old_node => old_node, :k => k, :v => v)
502       end
503     end
504   end
505 end