4 class NodesControllerTest < ActionController::TestCase
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", :method => :put },
18 { :controller => "api/nodes", :action => "update", :id => "1" }
21 { :path => "/api/0.6/node/1", :method => :delete },
22 { :controller => "api/nodes", :action => "delete", :id => "1" }
25 { :path => "/api/0.6/nodes", :method => :get },
26 { :controller => "api/nodes", :action => "index" }
31 private_user = create(:user, :data_public => false)
32 private_changeset = create(:changeset, :user => private_user)
34 changeset = create(:changeset, :user => user)
36 # create a node with random lat/lon
37 lat = rand(-50..50) + rand
38 lon = rand(-50..50) + rand
40 ## First try with no auth
41 # create a minimal xml file
42 xml = "<osm><node lat='#{lat}' lon='#{lon}' changeset='#{changeset.id}'/></osm>"
43 assert_difference("OldNode.count", 0) do
44 put :create, :body => xml
46 # hope for unauthorized
47 assert_response :unauthorized, "node upload did not return unauthorized status"
49 ## Now try with the user which doesn't have their data public
50 basic_authorization private_user.email, "test"
52 # create a minimal xml file
53 xml = "<osm><node lat='#{lat}' lon='#{lon}' changeset='#{private_changeset.id}'/></osm>"
54 assert_difference("Node.count", 0) do
55 put :create, :body => xml
58 assert_require_public_data "node create did not return forbidden status"
60 ## Now try with the user that has the public data
61 basic_authorization user.email, "test"
63 # create a minimal xml file
64 xml = "<osm><node lat='#{lat}' lon='#{lon}' changeset='#{changeset.id}'/></osm>"
65 put :create, :body => xml
67 assert_response :success, "node upload did not return success status"
69 # read id of created node and search for it
70 nodeid = @response.body
71 checknode = Node.find(nodeid)
72 assert_not_nil checknode, "uploaded node not found in data base after upload"
74 assert_in_delta lat * 10000000, checknode.latitude, 1, "saved node does not match requested latitude"
75 assert_in_delta lon * 10000000, checknode.longitude, 1, "saved node does not match requested longitude"
76 assert_equal changeset.id, checknode.changeset_id, "saved node does not belong to changeset that it was created in"
77 assert_equal true, checknode.visible, "saved node is not visible"
80 def test_create_invalid_xml
81 ## Only test public user here, as test_create should cover what's the forbiddens
82 ## that would occur here
85 changeset = create(:changeset, :user => user)
87 basic_authorization user.email, "test"
91 # test that the upload is rejected when xml is valid, but osm doc isn't
93 put :create, :body => xml
94 assert_response :bad_request, "node upload did not return bad_request status"
95 assert_equal "Cannot parse valid node from xml string <create/>. XML doesn't contain an osm/node element.", @response.body
97 # test that the upload is rejected when no lat is supplied
98 # create a minimal xml file
99 xml = "<osm><node lon='#{lon}' changeset='#{changeset.id}'/></osm>"
100 put :create, :body => xml
102 assert_response :bad_request, "node upload did not return bad_request status"
103 assert_equal "Cannot parse valid node from xml string <node lon=\"3.23\" changeset=\"#{changeset.id}\"/>. lat missing", @response.body
105 # test that the upload is rejected when no lon is supplied
106 # create a minimal xml file
107 xml = "<osm><node lat='#{lat}' changeset='#{changeset.id}'/></osm>"
108 put :create, :body => xml
110 assert_response :bad_request, "node upload did not return bad_request status"
111 assert_equal "Cannot parse valid node from xml string <node lat=\"3.434\" changeset=\"#{changeset.id}\"/>. lon missing", @response.body
113 # test that the upload is rejected when lat is non-numeric
114 # create a minimal xml file
115 xml = "<osm><node lat='abc' lon='#{lon}' changeset='#{changeset.id}'/></osm>"
116 put :create, :body => xml
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=\"abc\" lon=\"#{lon}\" changeset=\"#{changeset.id}\"/>. lat not a number", @response.body
121 # test that the upload is rejected when lon is non-numeric
122 # create a minimal xml file
123 xml = "<osm><node lat='#{lat}' lon='abc' changeset='#{changeset.id}'/></osm>"
124 put :create, :body => xml
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=\"#{lat}\" lon=\"abc\" changeset=\"#{changeset.id}\"/>. lon not a number", @response.body
129 # test that the upload is rejected when we have a tag which is too long
130 xml = "<osm><node lat='#{lat}' lon='#{lon}' changeset='#{changeset.id}'><tag k='foo' v='#{'x' * 256}'/></node></osm>"
131 put :create, :body => xml
132 assert_response :bad_request, "node upload did not return bad_request status"
133 assert_equal ["NodeTag ", " v: is too long (maximum is 255 characters) (\"#{'x' * 256}\")"], @response.body.split(/[0-9]+,foo:/)
137 # check that a visible node is returned properly
138 get :show, :params => { :id => create(:node).id }
139 assert_response :success
141 # check that an deleted node is not returned
142 get :show, :params => { :id => create(:node, :deleted).id }
143 assert_response :gone
145 # check chat a non-existent node is not returned
146 get :show, :params => { :id => 0 }
147 assert_response :not_found
150 # Ensure the lat/lon is formatted as a decimal e.g. not 4.0e-05
151 def test_lat_lon_xml_format
152 node = create(:node, :latitude => (0.00004 * OldNode::SCALE).to_i, :longitude => (0.00008 * OldNode::SCALE).to_i)
154 get :show, :params => { :id => node.id }
155 assert_match(/lat="0.0000400"/, response.body)
156 assert_match(/lon="0.0000800"/, response.body)
159 # this tests deletion restrictions - basic deletion is tested in the unit
162 private_user = create(:user, :data_public => false)
163 private_user_changeset = create(:changeset, :user => private_user)
164 private_user_closed_changeset = create(:changeset, :closed, :user => private_user)
165 private_node = create(:node, :changeset => private_user_changeset)
166 private_deleted_node = create(:node, :deleted, :changeset => private_user_changeset)
168 ## first try to delete node without auth
169 delete :delete, :params => { :id => private_node.id }
170 assert_response :unauthorized
172 ## now set auth for the non-data public user
173 basic_authorization private_user.email, "test"
175 # try to delete with an invalid (closed) changeset
176 xml = update_changeset(xml_for_node(private_node), private_user_closed_changeset.id)
177 delete :delete, :params => { :id => private_node.id }, :body => xml.to_s
178 assert_require_public_data("non-public user shouldn't be able to delete node")
180 # try to delete with an invalid (non-existent) changeset
181 xml = update_changeset(xml_for_node(private_node), 0)
182 delete :delete, :params => { :id => private_node.id }, :body => xml.to_s
183 assert_require_public_data("shouldn't be able to delete node, when user's data is private")
185 # valid delete now takes a payload
186 xml = xml_for_node(private_node)
187 delete :delete, :params => { :id => private_node.id }, :body => xml.to_s
188 assert_require_public_data("shouldn't be able to delete node when user's data isn't public'")
190 # this won't work since the node is already deleted
191 xml = xml_for_node(private_deleted_node)
192 delete :delete, :params => { :id => private_deleted_node.id }, :body => xml.to_s
193 assert_require_public_data
195 # this won't work since the node never existed
196 delete :delete, :params => { :id => 0 }
197 assert_require_public_data
199 ## these test whether nodes which are in-use can be deleted:
201 private_used_node = create(:node, :changeset => private_user_changeset)
202 create(:way_node, :node => private_used_node)
204 xml = xml_for_node(private_used_node)
205 delete :delete, :params => { :id => private_used_node.id }, :body => xml.to_s
206 assert_require_public_data "shouldn't be able to delete a node used in a way (#{@response.body})"
209 private_used_node2 = create(:node, :changeset => private_user_changeset)
210 create(:relation_member, :member => private_used_node2)
212 xml = xml_for_node(private_used_node2)
213 delete :delete, :params => { :id => private_used_node2.id }, :body => xml.to_s
214 assert_require_public_data "shouldn't be able to delete a node used in a relation (#{@response.body})"
216 ## now setup for the public data user
217 user = create(:user, :data_public => true)
218 changeset = create(:changeset, :user => user)
219 closed_changeset = create(:changeset, :closed, :user => user)
220 node = create(:node, :changeset => changeset)
221 basic_authorization user.email, "test"
223 # try to delete with an invalid (closed) changeset
224 xml = update_changeset(xml_for_node(node), closed_changeset.id)
225 delete :delete, :params => { :id => node.id }, :body => xml.to_s
226 assert_response :conflict
228 # try to delete with an invalid (non-existent) changeset
229 xml = update_changeset(xml_for_node(node), 0)
230 delete :delete, :params => { :id => node.id }, :body => xml.to_s
231 assert_response :conflict
233 # try to delete a node with a different ID
234 other_node = create(:node)
235 xml = xml_for_node(other_node)
236 delete :delete, :params => { :id => node.id }, :body => xml.to_s
237 assert_response :bad_request,
238 "should not be able to delete a node with a different ID from the XML"
240 # try to delete a node rubbish in the payloads
242 delete :delete, :params => { :id => node.id }, :body => xml.to_s
243 assert_response :bad_request,
244 "should not be able to delete a node without a valid XML payload"
246 # valid delete now takes a payload
247 xml = xml_for_node(node)
248 delete :delete, :params => { :id => node.id }, :body => xml.to_s
249 assert_response :success
251 # valid delete should return the new version number, which should
252 # be greater than the old version number
253 assert @response.body.to_i > node.version,
254 "delete request should return a new version number for node"
256 # deleting the same node twice doesn't work
257 xml = xml_for_node(node)
258 delete :delete, :params => { :id => node.id }, :body => xml.to_s
259 assert_response :gone
261 # this won't work since the node never existed
262 delete :delete, :params => { :id => 0 }
263 assert_response :not_found
265 ## these test whether nodes which are in-use can be deleted:
267 used_node = create(:node, :changeset => create(:changeset, :user => user))
268 way_node = create(:way_node, :node => used_node)
269 way_node2 = create(:way_node, :node => used_node)
271 xml = xml_for_node(used_node)
272 delete :delete, :params => { :id => used_node.id }, :body => xml.to_s
273 assert_response :precondition_failed,
274 "shouldn't be able to delete a node used in a way (#{@response.body})"
275 assert_equal "Precondition failed: Node #{used_node.id} is still used by ways #{way_node.way.id},#{way_node2.way.id}.", @response.body
278 used_node2 = create(:node, :changeset => create(:changeset, :user => user))
279 relation_member = create(:relation_member, :member => used_node2)
280 relation_member2 = create(:relation_member, :member => used_node2)
282 xml = xml_for_node(used_node2)
283 delete :delete, :params => { :id => used_node2.id }, :body => xml.to_s
284 assert_response :precondition_failed,
285 "shouldn't be able to delete a node used in a relation (#{@response.body})"
286 assert_equal "Precondition failed: Node #{used_node2.id} is still used by relations #{relation_member.relation.id},#{relation_member2.relation.id}.", @response.body
290 # tests whether the API works and prevents incorrect use while trying
293 ## First test with no user credentials
294 # try and update a node without authorisation
295 # first try to delete node without auth
296 private_user = create(:user, :data_public => false)
297 private_node = create(:node, :changeset => create(:changeset, :user => private_user))
299 node = create(:node, :changeset => create(:changeset, :user => user))
301 xml = xml_for_node(node)
302 put :update, :params => { :id => node.id }, :body => xml.to_s
303 assert_response :unauthorized
305 ## Second test with the private user
308 basic_authorization private_user.email, "test"
310 ## trying to break changesets
312 # try and update in someone else's changeset
313 xml = update_changeset(xml_for_node(private_node),
314 create(:changeset).id)
315 put :update, :params => { :id => private_node.id }, :body => xml.to_s
316 assert_require_public_data "update with other user's changeset should be forbidden when data isn't public"
318 # try and update in a closed changeset
319 xml = update_changeset(xml_for_node(private_node),
320 create(:changeset, :closed, :user => private_user).id)
321 put :update, :params => { :id => private_node.id }, :body => xml.to_s
322 assert_require_public_data "update with closed changeset should be forbidden, when data isn't public"
324 # try and update in a non-existant changeset
325 xml = update_changeset(xml_for_node(private_node), 0)
326 put :update, :params => { :id => private_node.id }, :body => xml.to_s
327 assert_require_public_data "update with changeset=0 should be forbidden, when data isn't public"
329 ## try and submit invalid updates
330 xml = xml_attr_rewrite(xml_for_node(private_node), "lat", 91.0)
331 put :update, :params => { :id => private_node.id }, :body => xml.to_s
332 assert_require_public_data "node at lat=91 should be forbidden, when data isn't public"
334 xml = xml_attr_rewrite(xml_for_node(private_node), "lat", -91.0)
335 put :update, :params => { :id => private_node.id }, :body => xml.to_s
336 assert_require_public_data "node at lat=-91 should be forbidden, when data isn't public"
338 xml = xml_attr_rewrite(xml_for_node(private_node), "lon", 181.0)
339 put :update, :params => { :id => private_node.id }, :body => xml.to_s
340 assert_require_public_data "node at lon=181 should be forbidden, when data isn't public"
342 xml = xml_attr_rewrite(xml_for_node(private_node), "lon", -181.0)
343 put :update, :params => { :id => private_node.id }, :body => xml.to_s
344 assert_require_public_data "node at lon=-181 should be forbidden, when data isn't public"
346 ## finally, produce a good request which still won't work
347 xml = xml_for_node(private_node)
348 put :update, :params => { :id => private_node.id }, :body => xml.to_s
349 assert_require_public_data "should have failed with a forbidden when data isn't public"
351 ## Finally test with the public user
353 # try and update a node without authorisation
354 # first try to update node without auth
355 xml = xml_for_node(node)
356 put :update, :params => { :id => node.id }, :body => xml.to_s
357 assert_response :forbidden
360 basic_authorization user.email, "test"
362 ## trying to break changesets
364 # try and update in someone else's changeset
365 xml = update_changeset(xml_for_node(node),
366 create(:changeset).id)
367 put :update, :params => { :id => node.id }, :body => xml.to_s
368 assert_response :conflict, "update with other user's changeset should be rejected"
370 # try and update in a closed changeset
371 xml = update_changeset(xml_for_node(node),
372 create(:changeset, :closed, :user => user).id)
373 put :update, :params => { :id => node.id }, :body => xml.to_s
374 assert_response :conflict, "update with closed changeset should be rejected"
376 # try and update in a non-existant changeset
377 xml = update_changeset(xml_for_node(node), 0)
378 put :update, :params => { :id => node.id }, :body => xml.to_s
379 assert_response :conflict, "update with changeset=0 should be rejected"
381 ## try and submit invalid updates
382 xml = xml_attr_rewrite(xml_for_node(node), "lat", 91.0)
383 put :update, :params => { :id => node.id }, :body => xml.to_s
384 assert_response :bad_request, "node at lat=91 should be rejected"
386 xml = xml_attr_rewrite(xml_for_node(node), "lat", -91.0)
387 put :update, :params => { :id => node.id }, :body => xml.to_s
388 assert_response :bad_request, "node at lat=-91 should be rejected"
390 xml = xml_attr_rewrite(xml_for_node(node), "lon", 181.0)
391 put :update, :params => { :id => node.id }, :body => xml.to_s
392 assert_response :bad_request, "node at lon=181 should be rejected"
394 xml = xml_attr_rewrite(xml_for_node(node), "lon", -181.0)
395 put :update, :params => { :id => node.id }, :body => xml.to_s
396 assert_response :bad_request, "node at lon=-181 should be rejected"
398 ## next, attack the versioning
399 current_node_version = node.version
401 # try and submit a version behind
402 xml = xml_attr_rewrite(xml_for_node(node),
403 "version", current_node_version - 1)
404 put :update, :params => { :id => node.id }, :body => xml.to_s
405 assert_response :conflict, "should have failed on old version number"
407 # try and submit a version ahead
408 xml = xml_attr_rewrite(xml_for_node(node),
409 "version", current_node_version + 1)
410 put :update, :params => { :id => node.id }, :body => xml.to_s
411 assert_response :conflict, "should have failed on skipped version number"
413 # try and submit total crap in the version field
414 xml = xml_attr_rewrite(xml_for_node(node),
415 "version", "p1r4t3s!")
416 put :update, :params => { :id => node.id }, :body => xml.to_s
417 assert_response :conflict,
418 "should not be able to put 'p1r4at3s!' in the version field"
420 ## try an update with the wrong ID
421 xml = xml_for_node(create(:node))
422 put :update, :params => { :id => node.id }, :body => xml.to_s
423 assert_response :bad_request,
424 "should not be able to update a node with a different ID from the XML"
426 ## try an update with a minimal valid XML doc which isn't a well-formed OSM doc.
428 put :update, :params => { :id => node.id }, :body => xml.to_s
429 assert_response :bad_request,
430 "should not be able to update a node with non-OSM XML doc."
432 ## finally, produce a good request which should work
433 xml = xml_for_node(node)
434 put :update, :params => { :id => node.id }, :body => xml.to_s
435 assert_response :success, "a valid update request failed"
439 # test fetching multiple nodes
441 node1 = create(:node)
442 node2 = create(:node, :deleted)
443 node3 = create(:node)
444 node4 = create(:node, :with_history, :version => 2)
445 node5 = create(:node, :deleted, :with_history, :version => 2)
447 # check error when no parameter provided
449 assert_response :bad_request
451 # check error when no parameter value provided
452 get :index, :params => { :nodes => "" }
453 assert_response :bad_request
455 # test a working call
456 get :index, :params => { :nodes => "#{node1.id},#{node2.id},#{node3.id},#{node4.id},#{node5.id}" }
457 assert_response :success
458 assert_select "osm" do
459 assert_select "node", :count => 5
460 assert_select "node[id='#{node1.id}'][visible='true']", :count => 1
461 assert_select "node[id='#{node2.id}'][visible='false']", :count => 1
462 assert_select "node[id='#{node3.id}'][visible='true']", :count => 1
463 assert_select "node[id='#{node4.id}'][visible='true']", :count => 1
464 assert_select "node[id='#{node5.id}'][visible='false']", :count => 1
467 # check error when a non-existent node is included
468 get :index, :params => { :nodes => "#{node1.id},#{node2.id},#{node3.id},#{node4.id},#{node5.id},0" }
469 assert_response :not_found
473 # test adding tags to a node
474 def test_duplicate_tags
475 existing_tag = create(:node_tag)
476 assert_equal true, existing_tag.node.changeset.user.data_public
478 basic_authorization existing_tag.node.changeset.user.email, "test"
480 # add an identical tag to the node
481 tag_xml = XML::Node.new("tag")
482 tag_xml["k"] = existing_tag.k
483 tag_xml["v"] = existing_tag.v
485 # add the tag into the existing xml
486 node_xml = xml_for_node(existing_tag.node)
487 node_xml.find("//osm/node").first << tag_xml
490 put :update, :params => { :id => existing_tag.node.id }, :body => node_xml.to_s
491 assert_response :bad_request,
492 "adding duplicate tags to a node should fail with 'bad request'"
493 assert_equal "Element node/#{existing_tag.node.id} has duplicate tags with key #{existing_tag.k}", @response.body
496 # test whether string injection is possible
497 def test_string_injection
498 private_user = create(:user, :data_public => false)
499 private_changeset = create(:changeset, :user => private_user)
501 changeset = create(:changeset, :user => user)
503 ## First try with the non-data public user
504 basic_authorization private_user.email, "test"
506 # try and put something into a string that the API might
507 # use unquoted and therefore allow code injection...
508 xml = "<osm><node lat='0' lon='0' changeset='#{private_changeset.id}'>" \
509 '<tag k="#{@user.inspect}" v="0"/>' \
511 put :create, :body => xml
512 assert_require_public_data "Shouldn't be able to create with non-public user"
514 ## Then try with the public data user
515 basic_authorization user.email, "test"
517 # try and put something into a string that the API might
518 # use unquoted and therefore allow code injection...
519 xml = "<osm><node lat='0' lon='0' changeset='#{changeset.id}'>" \
520 '<tag k="#{@user.inspect}" v="0"/>' \
522 put :create, :body => xml
523 assert_response :success
524 nodeid = @response.body
526 # find the node in the database
527 checknode = Node.find(nodeid)
528 assert_not_nil checknode, "node not found in data base after upload"
530 # and grab it using the api
531 get :show, :params => { :id => nodeid }
532 assert_response :success
533 apinode = Node.from_xml(@response.body)
534 assert_not_nil apinode, "downloaded node is nil, but shouldn't be"
536 # check the tags are not corrupted
537 assert_equal checknode.tags, apinode.tags
538 assert apinode.tags.include?("\#{@user.inspect}")
542 # update the changeset_id of a node element
543 def update_changeset(xml, changeset_id)
544 xml_attr_rewrite(xml, "changeset", changeset_id)
548 # update an attribute in the node element
549 def xml_attr_rewrite(xml, name, value)
550 xml.find("//osm/node").first[name] = value.to_s
557 parser = XML::Parser.string(xml)