4 class NodesControllerTest < ActionDispatch::IntegrationTest
6 # test all routes which lead to this controller
9 { :path => "/api/0.6/node/create", :method => :put },
10 { :controller => "api/nodes", :action => "create" }
13 { :path => "/api/0.6/node/1", :method => :get },
14 { :controller => "api/nodes", :action => "show", :id => "1" }
17 { :path => "/api/0.6/node/1.json", :method => :get },
18 { :controller => "api/nodes", :action => "show", :id => "1", :format => "json" }
21 { :path => "/api/0.6/node/1", :method => :put },
22 { :controller => "api/nodes", :action => "update", :id => "1" }
25 { :path => "/api/0.6/node/1", :method => :delete },
26 { :controller => "api/nodes", :action => "delete", :id => "1" }
29 { :path => "/api/0.6/nodes", :method => :get },
30 { :controller => "api/nodes", :action => "index" }
33 { :path => "/api/0.6/nodes.json", :method => :get },
34 { :controller => "api/nodes", :action => "index", :format => "json" }
39 private_user = create(:user, :data_public => false)
40 private_changeset = create(:changeset, :user => private_user)
42 changeset = create(:changeset, :user => user)
44 # create a node with random lat/lon
45 lat = rand(-50..50) + rand
46 lon = rand(-50..50) + rand
48 ## First try with no auth
49 # create a minimal xml file
50 xml = "<osm><node lat='#{lat}' lon='#{lon}' changeset='#{changeset.id}'/></osm>"
51 assert_difference("OldNode.count", 0) do
52 put node_create_path, :params => xml
54 # hope for unauthorized
55 assert_response :unauthorized, "node upload did not return unauthorized status"
57 ## Now try with the user which doesn't have their data public
58 auth_header = basic_authorization_header private_user.email, "test"
60 # create a minimal xml file
61 xml = "<osm><node lat='#{lat}' lon='#{lon}' changeset='#{private_changeset.id}'/></osm>"
62 assert_difference("Node.count", 0) do
63 put node_create_path, :params => xml, :headers => auth_header
66 assert_require_public_data "node create did not return forbidden status"
68 ## Now try with the user that has the public data
69 auth_header = basic_authorization_header user.email, "test"
71 # create a minimal xml file
72 xml = "<osm><node lat='#{lat}' lon='#{lon}' changeset='#{changeset.id}'/></osm>"
73 put node_create_path, :params => xml, :headers => auth_header
75 assert_response :success, "node upload did not return success status"
77 # read id of created node and search for it
78 nodeid = @response.body
79 checknode = Node.find(nodeid)
80 assert_not_nil checknode, "uploaded node not found in data base after upload"
82 assert_in_delta lat * 10000000, checknode.latitude, 1, "saved node does not match requested latitude"
83 assert_in_delta lon * 10000000, checknode.longitude, 1, "saved node does not match requested longitude"
84 assert_equal changeset.id, checknode.changeset_id, "saved node does not belong to changeset that it was created in"
85 assert checknode.visible, "saved node is not visible"
88 def test_create_invalid_xml
89 ## Only test public user here, as test_create should cover what's the forbiddens
90 ## that would occur here
93 changeset = create(:changeset, :user => user)
95 auth_header = basic_authorization_header user.email, "test"
99 # test that the upload is rejected when xml is valid, but osm doc isn't
101 put node_create_path, :params => xml, :headers => auth_header
102 assert_response :bad_request, "node upload did not return bad_request status"
103 assert_equal "Cannot parse valid node from xml string <create/>. XML doesn't contain an osm/node element.", @response.body
105 # test that the upload is rejected when no lat is supplied
106 # create a minimal xml file
107 xml = "<osm><node lon='#{lon}' changeset='#{changeset.id}'/></osm>"
108 put node_create_path, :params => xml, :headers => auth_header
110 assert_response :bad_request, "node upload did not return bad_request status"
111 assert_equal "Cannot parse valid node from xml string <node lon=\"3.23\" changeset=\"#{changeset.id}\"/>. lat missing", @response.body
113 # test that the upload is rejected when no lon is supplied
114 # create a minimal xml file
115 xml = "<osm><node lat='#{lat}' changeset='#{changeset.id}'/></osm>"
116 put node_create_path, :params => xml, :headers => auth_header
118 assert_response :bad_request, "node upload did not return bad_request status"
119 assert_equal "Cannot parse valid node from xml string <node lat=\"3.434\" changeset=\"#{changeset.id}\"/>. lon missing", @response.body
121 # test that the upload is rejected when lat is non-numeric
122 # create a minimal xml file
123 xml = "<osm><node lat='abc' lon='#{lon}' changeset='#{changeset.id}'/></osm>"
124 put node_create_path, :params => xml, :headers => auth_header
126 assert_response :bad_request, "node upload did not return bad_request status"
127 assert_equal "Cannot parse valid node from xml string <node lat=\"abc\" lon=\"#{lon}\" changeset=\"#{changeset.id}\"/>. lat not a number", @response.body
129 # test that the upload is rejected when lon is non-numeric
130 # create a minimal xml file
131 xml = "<osm><node lat='#{lat}' lon='abc' changeset='#{changeset.id}'/></osm>"
132 put node_create_path, :params => xml, :headers => auth_header
134 assert_response :bad_request, "node upload did not return bad_request status"
135 assert_equal "Cannot parse valid node from xml string <node lat=\"#{lat}\" lon=\"abc\" changeset=\"#{changeset.id}\"/>. lon not a number", @response.body
137 # test that the upload is rejected when we have a tag which is too long
138 xml = "<osm><node lat='#{lat}' lon='#{lon}' changeset='#{changeset.id}'><tag k='foo' v='#{'x' * 256}'/></node></osm>"
139 put node_create_path, :params => xml, :headers => auth_header
140 assert_response :bad_request, "node upload did not return bad_request status"
141 assert_equal ["NodeTag ", " v: is too long (maximum is 255 characters) (\"#{'x' * 256}\")"], @response.body.split(/[0-9]+,foo:/)
145 # check that a visible node is returned properly
146 get api_node_path(create(:node))
147 assert_response :success
149 # check that an deleted node is not returned
150 get api_node_path(create(:node, :deleted))
151 assert_response :gone
153 # check chat a non-existent node is not returned
154 get api_node_path(:id => 0)
155 assert_response :not_found
158 # Ensure the lat/lon is formatted as a decimal e.g. not 4.0e-05
159 def test_lat_lon_xml_format
160 node = create(:node, :latitude => (0.00004 * OldNode::SCALE).to_i, :longitude => (0.00008 * OldNode::SCALE).to_i)
162 get api_node_path(node)
163 assert_match(/lat="0.0000400"/, response.body)
164 assert_match(/lon="0.0000800"/, response.body)
167 # this tests deletion restrictions - basic deletion is tested in the unit
170 private_user = create(:user, :data_public => false)
171 private_user_changeset = create(:changeset, :user => private_user)
172 private_user_closed_changeset = create(:changeset, :closed, :user => private_user)
173 private_node = create(:node, :changeset => private_user_changeset)
174 private_deleted_node = create(:node, :deleted, :changeset => private_user_changeset)
176 ## first try to delete node without auth
177 delete api_node_path(private_node)
178 assert_response :unauthorized
180 ## now set auth for the non-data public user
181 auth_header = basic_authorization_header private_user.email, "test"
183 # try to delete with an invalid (closed) changeset
184 xml = update_changeset(xml_for_node(private_node), private_user_closed_changeset.id)
185 delete api_node_path(private_node), :params => xml.to_s, :headers => auth_header
186 assert_require_public_data("non-public user shouldn't be able to delete node")
188 # try to delete with an invalid (non-existent) changeset
189 xml = update_changeset(xml_for_node(private_node), 0)
190 delete api_node_path(private_node), :params => xml.to_s, :headers => auth_header
191 assert_require_public_data("shouldn't be able to delete node, when user's data is private")
193 # valid delete now takes a payload
194 xml = xml_for_node(private_node)
195 delete api_node_path(private_node), :params => xml.to_s, :headers => auth_header
196 assert_require_public_data("shouldn't be able to delete node when user's data isn't public'")
198 # this won't work since the node is already deleted
199 xml = xml_for_node(private_deleted_node)
200 delete api_node_path(private_deleted_node), :params => xml.to_s, :headers => auth_header
201 assert_require_public_data
203 # this won't work since the node never existed
204 delete api_node_path(:id => 0), :headers => auth_header
205 assert_require_public_data
207 ## these test whether nodes which are in-use can be deleted:
209 private_used_node = create(:node, :changeset => private_user_changeset)
210 create(:way_node, :node => private_used_node)
212 xml = xml_for_node(private_used_node)
213 delete api_node_path(private_used_node), :params => xml.to_s, :headers => auth_header
214 assert_require_public_data "shouldn't be able to delete a node used in a way (#{@response.body})"
217 private_used_node2 = create(:node, :changeset => private_user_changeset)
218 create(:relation_member, :member => private_used_node2)
220 xml = xml_for_node(private_used_node2)
221 delete api_node_path(private_used_node2), :params => xml.to_s, :headers => auth_header
222 assert_require_public_data "shouldn't be able to delete a node used in a relation (#{@response.body})"
224 ## now setup for the public data user
225 user = create(:user, :data_public => true)
226 changeset = create(:changeset, :user => user)
227 closed_changeset = create(:changeset, :closed, :user => user)
228 node = create(:node, :changeset => changeset)
229 auth_header = basic_authorization_header user.email, "test"
231 # try to delete with an invalid (closed) changeset
232 xml = update_changeset(xml_for_node(node), closed_changeset.id)
233 delete api_node_path(node), :params => xml.to_s, :headers => auth_header
234 assert_response :conflict
236 # try to delete with an invalid (non-existent) changeset
237 xml = update_changeset(xml_for_node(node), 0)
238 delete api_node_path(node), :params => xml.to_s, :headers => auth_header
239 assert_response :conflict
241 # try to delete a node with a different ID
242 other_node = create(:node)
243 xml = xml_for_node(other_node)
244 delete api_node_path(node.id), :params => xml.to_s, :headers => auth_header
245 assert_response :bad_request,
246 "should not be able to delete a node with a different ID from the XML"
248 # try to delete a node rubbish in the payloads
250 delete api_node_path(node), :params => xml.to_s, :headers => auth_header
251 assert_response :bad_request,
252 "should not be able to delete a node without a valid XML payload"
254 # valid delete now takes a payload
255 xml = xml_for_node(node)
256 delete api_node_path(node), :params => xml.to_s, :headers => auth_header
257 assert_response :success
259 # valid delete should return the new version number, which should
260 # be greater than the old version number
261 assert @response.body.to_i > node.version,
262 "delete request should return a new version number for node"
264 # deleting the same node twice doesn't work
265 xml = xml_for_node(node)
266 delete api_node_path(node), :params => xml.to_s, :headers => auth_header
267 assert_response :gone
269 # this won't work since the node never existed
270 delete api_node_path(:id => 0), :headers => auth_header
271 assert_response :not_found
273 ## these test whether nodes which are in-use can be deleted:
275 used_node = create(:node, :changeset => create(:changeset, :user => user))
276 way_node = create(:way_node, :node => used_node)
277 way_node2 = create(:way_node, :node => used_node)
279 xml = xml_for_node(used_node)
280 delete api_node_path(used_node), :params => xml.to_s, :headers => auth_header
281 assert_response :precondition_failed,
282 "shouldn't be able to delete a node used in a way (#{@response.body})"
283 assert_equal "Precondition failed: Node #{used_node.id} is still used by ways #{way_node.way.id},#{way_node2.way.id}.", @response.body
286 used_node2 = create(:node, :changeset => create(:changeset, :user => user))
287 relation_member = create(:relation_member, :member => used_node2)
288 relation_member2 = create(:relation_member, :member => used_node2)
290 xml = xml_for_node(used_node2)
291 delete api_node_path(used_node2), :params => xml.to_s, :headers => auth_header
292 assert_response :precondition_failed,
293 "shouldn't be able to delete a node used in a relation (#{@response.body})"
294 assert_equal "Precondition failed: Node #{used_node2.id} is still used by relations #{relation_member.relation.id},#{relation_member2.relation.id}.", @response.body
298 # tests whether the API works and prevents incorrect use while trying
301 ## First test with no user credentials
302 # try and update a node without authorisation
303 # first try to delete node without auth
304 private_user = create(:user, :data_public => false)
305 private_node = create(:node, :changeset => create(:changeset, :user => private_user))
307 node = create(:node, :changeset => create(:changeset, :user => user))
309 xml = xml_for_node(node)
310 put api_node_path(node), :params => xml.to_s
311 assert_response :unauthorized
313 ## Second test with the private user
316 auth_header = basic_authorization_header private_user.email, "test"
318 ## trying to break changesets
320 # try and update in someone else's changeset
321 xml = update_changeset(xml_for_node(private_node),
322 create(:changeset).id)
323 put api_node_path(private_node), :params => xml.to_s, :headers => auth_header
324 assert_require_public_data "update with other user's changeset should be forbidden when data isn't public"
326 # try and update in a closed changeset
327 xml = update_changeset(xml_for_node(private_node),
328 create(:changeset, :closed, :user => private_user).id)
329 put api_node_path(private_node), :params => xml.to_s, :headers => auth_header
330 assert_require_public_data "update with closed changeset should be forbidden, when data isn't public"
332 # try and update in a non-existant changeset
333 xml = update_changeset(xml_for_node(private_node), 0)
334 put api_node_path(private_node), :params => xml.to_s, :headers => auth_header
335 assert_require_public_data "update with changeset=0 should be forbidden, when data isn't public"
337 ## try and submit invalid updates
338 xml = xml_attr_rewrite(xml_for_node(private_node), "lat", 91.0)
339 put api_node_path(private_node), :params => xml.to_s, :headers => auth_header
340 assert_require_public_data "node at lat=91 should be forbidden, when data isn't public"
342 xml = xml_attr_rewrite(xml_for_node(private_node), "lat", -91.0)
343 put api_node_path(private_node), :params => xml.to_s, :headers => auth_header
344 assert_require_public_data "node at lat=-91 should be forbidden, when data isn't public"
346 xml = xml_attr_rewrite(xml_for_node(private_node), "lon", 181.0)
347 put api_node_path(private_node), :params => xml.to_s, :headers => auth_header
348 assert_require_public_data "node at lon=181 should be forbidden, when data isn't public"
350 xml = xml_attr_rewrite(xml_for_node(private_node), "lon", -181.0)
351 put api_node_path(private_node), :params => xml.to_s, :headers => auth_header
352 assert_require_public_data "node at lon=-181 should be forbidden, when data isn't public"
354 ## finally, produce a good request which still won't work
355 xml = xml_for_node(private_node)
356 put api_node_path(private_node), :params => xml.to_s, :headers => auth_header
357 assert_require_public_data "should have failed with a forbidden when data isn't public"
359 ## Finally test with the public user
361 # try and update a node without authorisation
362 # first try to update node without auth
363 xml = xml_for_node(node)
364 put api_node_path(node.id), :params => xml.to_s, :headers => auth_header
365 assert_response :forbidden
368 auth_header = basic_authorization_header user.email, "test"
370 ## trying to break changesets
372 # try and update in someone else's changeset
373 xml = update_changeset(xml_for_node(node),
374 create(:changeset).id)
375 put api_node_path(node), :params => xml.to_s, :headers => auth_header
376 assert_response :conflict, "update with other user's changeset should be rejected"
378 # try and update in a closed changeset
379 xml = update_changeset(xml_for_node(node),
380 create(:changeset, :closed, :user => user).id)
381 put api_node_path(node), :params => xml.to_s, :headers => auth_header
382 assert_response :conflict, "update with closed changeset should be rejected"
384 # try and update in a non-existant changeset
385 xml = update_changeset(xml_for_node(node), 0)
386 put api_node_path(node), :params => xml.to_s, :headers => auth_header
387 assert_response :conflict, "update with changeset=0 should be rejected"
389 ## try and submit invalid updates
390 xml = xml_attr_rewrite(xml_for_node(node), "lat", 91.0)
391 put api_node_path(node), :params => xml.to_s, :headers => auth_header
392 assert_response :bad_request, "node at lat=91 should be rejected"
394 xml = xml_attr_rewrite(xml_for_node(node), "lat", -91.0)
395 put api_node_path(node), :params => xml.to_s, :headers => auth_header
396 assert_response :bad_request, "node at lat=-91 should be rejected"
398 xml = xml_attr_rewrite(xml_for_node(node), "lon", 181.0)
399 put api_node_path(node), :params => xml.to_s, :headers => auth_header
400 assert_response :bad_request, "node at lon=181 should be rejected"
402 xml = xml_attr_rewrite(xml_for_node(node), "lon", -181.0)
403 put api_node_path(node), :params => xml.to_s, :headers => auth_header
404 assert_response :bad_request, "node at lon=-181 should be rejected"
406 ## next, attack the versioning
407 current_node_version = node.version
409 # try and submit a version behind
410 xml = xml_attr_rewrite(xml_for_node(node),
411 "version", current_node_version - 1)
412 put api_node_path(node), :params => xml.to_s, :headers => auth_header
413 assert_response :conflict, "should have failed on old version number"
415 # try and submit a version ahead
416 xml = xml_attr_rewrite(xml_for_node(node),
417 "version", current_node_version + 1)
418 put api_node_path(node), :params => xml.to_s, :headers => auth_header
419 assert_response :conflict, "should have failed on skipped version number"
421 # try and submit total crap in the version field
422 xml = xml_attr_rewrite(xml_for_node(node),
423 "version", "p1r4t3s!")
424 put api_node_path(node), :params => xml.to_s, :headers => auth_header
425 assert_response :conflict,
426 "should not be able to put 'p1r4at3s!' in the version field"
428 ## try an update with the wrong ID
429 xml = xml_for_node(create(:node))
430 put api_node_path(node), :params => xml.to_s, :headers => auth_header
431 assert_response :bad_request,
432 "should not be able to update a node with a different ID from the XML"
434 ## try an update with a minimal valid XML doc which isn't a well-formed OSM doc.
436 put api_node_path(node), :params => xml.to_s, :headers => auth_header
437 assert_response :bad_request,
438 "should not be able to update a node with non-OSM XML doc."
440 ## finally, produce a good request which should work
441 xml = xml_for_node(node)
442 put api_node_path(node), :params => xml.to_s, :headers => auth_header
443 assert_response :success, "a valid update request failed"
447 # test fetching multiple nodes
449 node1 = create(:node)
450 node2 = create(:node, :deleted)
451 node3 = create(:node)
452 node4 = create(:node, :with_history, :version => 2)
453 node5 = create(:node, :deleted, :with_history, :version => 2)
455 # check error when no parameter provided
457 assert_response :bad_request
459 # check error when no parameter value provided
460 get nodes_path, :params => { :nodes => "" }
461 assert_response :bad_request
463 # test a working call
464 get nodes_path, :params => { :nodes => "#{node1.id},#{node2.id},#{node3.id},#{node4.id},#{node5.id}" }
465 assert_response :success
466 assert_select "osm" do
467 assert_select "node", :count => 5
468 assert_select "node[id='#{node1.id}'][visible='true']", :count => 1
469 assert_select "node[id='#{node2.id}'][visible='false']", :count => 1
470 assert_select "node[id='#{node3.id}'][visible='true']", :count => 1
471 assert_select "node[id='#{node4.id}'][visible='true']", :count => 1
472 assert_select "node[id='#{node5.id}'][visible='false']", :count => 1
475 # test a working call with json format
476 get nodes_path, :params => { :nodes => "#{node1.id},#{node2.id},#{node3.id},#{node4.id},#{node5.id}", :format => "json" }
478 js = ActiveSupport::JSON.decode(@response.body)
480 assert_equal 5, js["elements"].count
481 assert_equal 5, (js["elements"].count { |a| a["type"] == "node" })
482 assert_equal 1, (js["elements"].count { |a| a["id"] == node1.id && a["visible"].nil? })
483 assert_equal 1, (js["elements"].count { |a| a["id"] == node2.id && a["visible"] == false })
484 assert_equal 1, (js["elements"].count { |a| a["id"] == node3.id && a["visible"].nil? })
485 assert_equal 1, (js["elements"].count { |a| a["id"] == node4.id && a["visible"].nil? })
486 assert_equal 1, (js["elements"].count { |a| a["id"] == node5.id && a["visible"] == false })
488 # check error when a non-existent node is included
489 get nodes_path, :params => { :nodes => "#{node1.id},#{node2.id},#{node3.id},#{node4.id},#{node5.id},0" }
490 assert_response :not_found
494 # test adding tags to a node
495 def test_duplicate_tags
496 existing_tag = create(:node_tag)
497 assert existing_tag.node.changeset.user.data_public
499 auth_header = basic_authorization_header existing_tag.node.changeset.user.email, "test"
501 # add an identical tag to the node
502 tag_xml = XML::Node.new("tag")
503 tag_xml["k"] = existing_tag.k
504 tag_xml["v"] = existing_tag.v
506 # add the tag into the existing xml
507 node_xml = xml_for_node(existing_tag.node)
508 node_xml.find("//osm/node").first << tag_xml
511 put api_node_path(existing_tag.node), :params => node_xml.to_s, :headers => auth_header
512 assert_response :bad_request,
513 "adding duplicate tags to a node should fail with 'bad request'"
514 assert_equal "Element node/#{existing_tag.node.id} has duplicate tags with key #{existing_tag.k}", @response.body
517 # test whether string injection is possible
518 def test_string_injection
519 private_user = create(:user, :data_public => false)
520 private_changeset = create(:changeset, :user => private_user)
522 changeset = create(:changeset, :user => user)
524 ## First try with the non-data public user
525 auth_header = basic_authorization_header private_user.email, "test"
527 # try and put something into a string that the API might
528 # use unquoted and therefore allow code injection...
529 xml = "<osm><node lat='0' lon='0' changeset='#{private_changeset.id}'>" \
530 '<tag k="#{@user.inspect}" v="0"/>' \
532 put node_create_path, :params => xml, :headers => auth_header
533 assert_require_public_data "Shouldn't be able to create with non-public user"
535 ## Then try with the public data user
536 auth_header = basic_authorization_header user.email, "test"
538 # try and put something into a string that the API might
539 # use unquoted and therefore allow code injection...
540 xml = "<osm><node lat='0' lon='0' changeset='#{changeset.id}'>" \
541 '<tag k="#{@user.inspect}" v="0"/>' \
543 put node_create_path, :params => xml, :headers => auth_header
544 assert_response :success
545 nodeid = @response.body
547 # find the node in the database
548 checknode = Node.find(nodeid)
549 assert_not_nil checknode, "node not found in data base after upload"
551 # and grab it using the api
552 get api_node_path(:id => nodeid)
553 assert_response :success
554 apinode = Node.from_xml(@response.body)
555 assert_not_nil apinode, "downloaded node is nil, but shouldn't be"
557 # check the tags are not corrupted
558 assert_equal checknode.tags, apinode.tags
559 assert_includes apinode.tags, "\#{@user.inspect}"
565 # update the changeset_id of a node element
566 def update_changeset(xml, changeset_id)
567 xml_attr_rewrite(xml, "changeset", changeset_id)
571 # update an attribute in the node element
572 def xml_attr_rewrite(xml, name, value)
573 xml.find("//osm/node").first[name] = value.to_s
580 parser = XML::Parser.string(xml)