4 class OldNodesControllerTest < ActionDispatch::IntegrationTest
6 # test all routes which lead to this controller
9 { :path => "/api/0.6/node/1/history", :method => :get },
10 { :controller => "api/old_nodes", :action => "index", :node_id => "1" }
13 { :path => "/api/0.6/node/1/history.json", :method => :get },
14 { :controller => "api/old_nodes", :action => "index", :node_id => "1", :format => "json" }
17 { :path => "/api/0.6/node/1/2", :method => :get },
18 { :controller => "api/old_nodes", :action => "show", :id => "1", :version => "2" }
21 { :path => "/api/0.6/node/1/2.json", :method => :get },
22 { :controller => "api/old_nodes", :action => "show", :id => "1", :version => "2", :format => "json" }
25 { :path => "/api/0.6/node/1/2/redact", :method => :post },
26 { :controller => "api/old_nodes", :action => "redact", :id => "1", :version => "2" }
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)
35 get api_node_versions_path(node)
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"
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"
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))
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."
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."
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.
79 # FIXME: Move this test to being an integration test since it spans multiple controllers
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))
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)
89 ## First try this with a non-public user
90 auth_header = bearer_authorization_header private_user
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
97 # keep a hash of the versions => string, as we'll need something
98 # to test against later
101 # save a version for later checking
102 versions[xml_node["version"]] = xml_doc.to_s
104 # randomly move the node about
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
114 # save a version for later checking
115 versions[xml_node["version"]] = xml_doc.to_s
118 # add a bunch of random tags
120 xml_tag = XML::Node.new("tag")
121 xml_tag["k"] = random_string
122 xml_tag["v"] = random_string
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
130 # save a version for later checking
131 versions[xml_node["version"]] = xml_doc.to_s
134 # probably should check that they didn't get written to the database
136 ## Now do it with the public user
137 auth_header = bearer_authorization_header user
139 # setup a simple XML node
141 xml_doc = xml_for_node(node)
142 xml_node = xml_doc.find("//osm/node").first
145 # keep a hash of the versions => string, as we'll need something
146 # to test against later
149 # save a version for later checking
150 versions[xml_node["version"]] = xml_doc.to_s
152 # randomly move the node about
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
162 # save a version for later checking
163 versions[xml_node["version"]] = xml_doc.to_s
166 # add a bunch of random tags
168 xml_tag = XML::Node.new("tag")
169 xml_tag["k"] = random_string
170 xml_tag["v"] = random_string
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
178 # save a version for later checking
179 versions[xml_node["version"]] = xml_doc.to_s
182 # check all the versions
183 versions.each_key do |key|
184 get api_old_node_path(nodeid, key.to_i)
186 assert_response :success,
187 "couldn't get version #{key.to_i} of node #{nodeid}"
189 check_node = Node.from_xml(versions[key])
190 api_node = Node.from_xml(@response.body.to_s)
192 assert_nodes_are_equal check_node, api_node
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)
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)
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)
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)
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)
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)
239 # test the redaction of an old version of a node, while not being
241 def test_redact_node_unauthorised
242 node = create(:node, :with_history, :version => 4)
243 node_v3 = node.old_nodes.find_by(:version => 3)
245 do_redact_node(node_v3,
247 assert_response :unauthorized, "should need to be authenticated to redact."
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
256 node = create(:node, :with_history, :version => 4)
257 node_v3 = node.old_nodes.find_by(:version => 3)
259 do_redact_node(node_v3,
262 assert_response :forbidden, "should need to be moderator to redact."
266 # test that, even as moderator, the current version of a node
268 def test_redact_node_current_version
269 auth_header = bearer_authorization_header create(:moderator_user)
271 node = create(:node, :with_history, :version => 4)
272 node_v4 = node.old_nodes.find_by(:version => 4)
274 do_redact_node(node_v4,
277 assert_response :bad_request, "shouldn't be OK to redact current version as moderator."
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."
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."
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."
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."
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))
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."
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."
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)
329 do_redact_node(node_v3, create(:redaction), auth_header)
330 assert_response :success, "should be OK to redact old version as moderator."
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."
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."
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)
357 do_redact_node(node_v3, create(:redaction), auth_header)
358 assert_response :success, "should be OK to redact old version as moderator."
360 # re-auth as non-moderator
361 auth_header = bearer_authorization_header
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."
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."
375 # test the unredaction of an old version of a node, while not being
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))
382 post node_version_redact_path(node_v1.node_id, node_v1.version)
383 assert_response :unauthorized, "should need to be authenticated to unredact."
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
391 node = create(:node, :with_history, :version => 2)
392 node_v1 = node.old_nodes.find_by(:version => 1)
393 node_v1.redact!(create(:redaction))
395 auth_header = bearer_authorization_header user
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."
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))
410 auth_header = bearer_authorization_header moderator_user
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."
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."
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."
426 auth_header = bearer_authorization_header
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."
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."
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)
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}."
452 post node_version_redact_path(node.node_id, node.version), :params => { :redaction => redaction.id }, :headers => headers
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)
462 assert_not_nil current_node, "getting node #{node_id} returned nil"
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)
469 # check the nodes are the same
470 assert_nodes_are_equal current_node, old_node
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)
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.
484 letters = [["!", '"', "$", "&", ";", "@"],
487 ("0".."9").to_a].flatten
488 (1..16).map { letters[rand(letters.length)] }.join
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.
496 (f * GeoRecord::SCALE).round.to_f / GeoRecord::SCALE
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)