3 class NodesControllerTest < ActionController::TestCase
5 # test all routes which lead to this controller
8 { :path => "/api/0.6/node/create", :method => :put },
9 { :controller => "nodes", :action => "create" }
12 { :path => "/api/0.6/node/1", :method => :get },
13 { :controller => "nodes", :action => "read", :id => "1" }
16 { :path => "/api/0.6/node/1", :method => :put },
17 { :controller => "nodes", :action => "update", :id => "1" }
20 { :path => "/api/0.6/node/1", :method => :delete },
21 { :controller => "nodes", :action => "delete", :id => "1" }
24 { :path => "/api/0.6/nodes", :method => :get },
25 { :controller => "nodes", :action => "nodes" }
30 private_user = create(:user, :data_public => false)
31 private_changeset = create(:changeset, :user => private_user)
33 changeset = create(:changeset, :user => user)
35 # create a node with random lat/lon
36 lat = rand(-50..50) + rand
37 lon = rand(-50..50) + rand
39 ## First try with no auth
40 # create a minimal xml file
41 xml = "<osm><node lat='#{lat}' lon='#{lon}' changeset='#{changeset.id}'/></osm>"
42 assert_difference("OldNode.count", 0) do
43 put :create, :body => xml
45 # hope for unauthorized
46 assert_response :unauthorized, "node upload did not return unauthorized status"
48 ## Now try with the user which doesn't have their data public
49 basic_authorization private_user.email, "test"
51 # create a minimal xml file
52 xml = "<osm><node lat='#{lat}' lon='#{lon}' changeset='#{private_changeset.id}'/></osm>"
53 assert_difference("Node.count", 0) do
54 put :create, :body => xml
57 assert_require_public_data "node create did not return forbidden status"
59 ## Now try with the user that has the public data
60 basic_authorization user.email, "test"
62 # create a minimal xml file
63 xml = "<osm><node lat='#{lat}' lon='#{lon}' changeset='#{changeset.id}'/></osm>"
64 put :create, :body => xml
66 assert_response :success, "node upload did not return success status"
68 # read id of created node and search for it
69 nodeid = @response.body
70 checknode = Node.find(nodeid)
71 assert_not_nil checknode, "uploaded node not found in data base after upload"
73 assert_in_delta lat * 10000000, checknode.latitude, 1, "saved node does not match requested latitude"
74 assert_in_delta lon * 10000000, checknode.longitude, 1, "saved node does not match requested longitude"
75 assert_equal changeset.id, checknode.changeset_id, "saved node does not belong to changeset that it was created in"
76 assert_equal true, checknode.visible, "saved node is not visible"
79 def test_create_invalid_xml
80 ## Only test public user here, as test_create should cover what's the forbiddens
81 ## that would occur here
84 changeset = create(:changeset, :user => user)
86 basic_authorization user.email, "test"
90 # test that the upload is rejected when xml is valid, but osm doc isn't
92 put :create, :body => xml
93 assert_response :bad_request, "node upload did not return bad_request status"
94 assert_equal "Cannot parse valid node from xml string <create/>. XML doesn't contain an osm/node element.", @response.body
96 # test that the upload is rejected when no lat is supplied
97 # create a minimal xml file
98 xml = "<osm><node lon='#{lon}' changeset='#{changeset.id}'/></osm>"
99 put :create, :body => xml
101 assert_response :bad_request, "node upload did not return bad_request status"
102 assert_equal "Cannot parse valid node from xml string <node lon=\"3.23\" changeset=\"#{changeset.id}\"/>. lat missing", @response.body
104 # test that the upload is rejected when no lon is supplied
105 # create a minimal xml file
106 xml = "<osm><node lat='#{lat}' changeset='#{changeset.id}'/></osm>"
107 put :create, :body => xml
109 assert_response :bad_request, "node upload did not return bad_request status"
110 assert_equal "Cannot parse valid node from xml string <node lat=\"3.434\" changeset=\"#{changeset.id}\"/>. lon missing", @response.body
112 # test that the upload is rejected when lat is non-numeric
113 # create a minimal xml file
114 xml = "<osm><node lat='abc' lon='#{lon}' changeset='#{changeset.id}'/></osm>"
115 put :create, :body => xml
117 assert_response :bad_request, "node upload did not return bad_request status"
118 assert_equal "Cannot parse valid node from xml string <node lat=\"abc\" lon=\"#{lon}\" changeset=\"#{changeset.id}\"/>. lat not a number", @response.body
120 # test that the upload is rejected when lon is non-numeric
121 # create a minimal xml file
122 xml = "<osm><node lat='#{lat}' lon='abc' changeset='#{changeset.id}'/></osm>"
123 put :create, :body => xml
125 assert_response :bad_request, "node upload did not return bad_request status"
126 assert_equal "Cannot parse valid node from xml string <node lat=\"#{lat}\" lon=\"abc\" changeset=\"#{changeset.id}\"/>. lon not a number", @response.body
128 # test that the upload is rejected when we have a tag which is too long
129 xml = "<osm><node lat='#{lat}' lon='#{lon}' changeset='#{changeset.id}'><tag k='foo' v='#{'x' * 256}'/></node></osm>"
130 put :create, :body => xml
131 assert_response :bad_request, "node upload did not return bad_request status"
132 assert_equal ["NodeTag ", " v: is too long (maximum is 255 characters) (\"#{'x' * 256}\")"], @response.body.split(/[0-9]+,foo:/)
136 # check that a visible node is returned properly
137 get :read, :params => { :id => create(:node).id }
138 assert_response :success
140 # check that an deleted node is not returned
141 get :read, :params => { :id => create(:node, :deleted).id }
142 assert_response :gone
144 # check chat a non-existent node is not returned
145 get :read, :params => { :id => 0 }
146 assert_response :not_found
149 # this tests deletion restrictions - basic deletion is tested in the unit
152 private_user = create(:user, :data_public => false)
153 private_user_changeset = create(:changeset, :user => private_user)
154 private_user_closed_changeset = create(:changeset, :closed, :user => private_user)
155 private_node = create(:node, :changeset => private_user_changeset)
156 private_deleted_node = create(:node, :deleted, :changeset => private_user_changeset)
158 ## first try to delete node without auth
159 delete :delete, :params => { :id => private_node.id }
160 assert_response :unauthorized
162 ## now set auth for the non-data public user
163 basic_authorization private_user.email, "test"
165 # try to delete with an invalid (closed) changeset
166 xml = update_changeset(private_node.to_xml, private_user_closed_changeset.id)
167 delete :delete, :params => { :id => private_node.id }, :body => xml.to_s
168 assert_require_public_data("non-public user shouldn't be able to delete node")
170 # try to delete with an invalid (non-existent) changeset
171 xml = update_changeset(private_node.to_xml, 0)
172 delete :delete, :params => { :id => private_node.id }, :body => xml.to_s
173 assert_require_public_data("shouldn't be able to delete node, when user's data is private")
175 # valid delete now takes a payload
176 xml = private_node.to_xml
177 delete :delete, :params => { :id => private_node.id }, :body => xml.to_s
178 assert_require_public_data("shouldn't be able to delete node when user's data isn't public'")
180 # this won't work since the node is already deleted
181 xml = private_deleted_node.to_xml
182 delete :delete, :params => { :id => private_deleted_node.id }, :body => xml.to_s
183 assert_require_public_data
185 # this won't work since the node never existed
186 delete :delete, :params => { :id => 0 }
187 assert_require_public_data
189 ## these test whether nodes which are in-use can be deleted:
191 private_used_node = create(:node, :changeset => private_user_changeset)
192 create(:way_node, :node => private_used_node)
194 xml = private_used_node.to_xml
195 delete :delete, :params => { :id => private_used_node.id }, :body => xml.to_s
196 assert_require_public_data "shouldn't be able to delete a node used in a way (#{@response.body})"
199 private_used_node2 = create(:node, :changeset => private_user_changeset)
200 create(:relation_member, :member => private_used_node2)
202 xml = private_used_node2.to_xml
203 delete :delete, :params => { :id => private_used_node2.id }, :body => xml.to_s
204 assert_require_public_data "shouldn't be able to delete a node used in a relation (#{@response.body})"
206 ## now setup for the public data user
207 user = create(:user, :data_public => true)
208 changeset = create(:changeset, :user => user)
209 closed_changeset = create(:changeset, :closed, :user => user)
210 node = create(:node, :changeset => changeset)
211 basic_authorization user.email, "test"
213 # try to delete with an invalid (closed) changeset
214 xml = update_changeset(node.to_xml, closed_changeset.id)
215 delete :delete, :params => { :id => node.id }, :body => xml.to_s
216 assert_response :conflict
218 # try to delete with an invalid (non-existent) changeset
219 xml = update_changeset(node.to_xml, 0)
220 delete :delete, :params => { :id => node.id }, :body => xml.to_s
221 assert_response :conflict
223 # try to delete a node with a different ID
224 other_node = create(:node)
225 xml = other_node.to_xml
226 delete :delete, :params => { :id => node.id }, :body => xml.to_s
227 assert_response :bad_request,
228 "should not be able to delete a node with a different ID from the XML"
230 # try to delete a node rubbish in the payloads
232 delete :delete, :params => { :id => node.id }, :body => xml.to_s
233 assert_response :bad_request,
234 "should not be able to delete a node without a valid XML payload"
236 # valid delete now takes a payload
238 delete :delete, :params => { :id => node.id }, :body => xml.to_s
239 assert_response :success
241 # valid delete should return the new version number, which should
242 # be greater than the old version number
243 assert @response.body.to_i > node.version,
244 "delete request should return a new version number for node"
246 # deleting the same node twice doesn't work
248 delete :delete, :params => { :id => node.id }, :body => xml.to_s
249 assert_response :gone
251 # this won't work since the node never existed
252 delete :delete, :params => { :id => 0 }
253 assert_response :not_found
255 ## these test whether nodes which are in-use can be deleted:
257 used_node = create(:node, :changeset => create(:changeset, :user => user))
258 way_node = create(:way_node, :node => used_node)
259 way_node2 = create(:way_node, :node => used_node)
261 xml = used_node.to_xml
262 delete :delete, :params => { :id => used_node.id }, :body => xml.to_s
263 assert_response :precondition_failed,
264 "shouldn't be able to delete a node used in a way (#{@response.body})"
265 assert_equal "Precondition failed: Node #{used_node.id} is still used by ways #{way_node.way.id},#{way_node2.way.id}.", @response.body
268 used_node2 = create(:node, :changeset => create(:changeset, :user => user))
269 relation_member = create(:relation_member, :member => used_node2)
270 relation_member2 = create(:relation_member, :member => used_node2)
272 xml = used_node2.to_xml
273 delete :delete, :params => { :id => used_node2.id }, :body => xml.to_s
274 assert_response :precondition_failed,
275 "shouldn't be able to delete a node used in a relation (#{@response.body})"
276 assert_equal "Precondition failed: Node #{used_node2.id} is still used by relations #{relation_member.relation.id},#{relation_member2.relation.id}.", @response.body
280 # tests whether the API works and prevents incorrect use while trying
283 ## First test with no user credentials
284 # try and update a node without authorisation
285 # first try to delete node without auth
286 private_user = create(:user, :data_public => false)
287 private_node = create(:node, :changeset => create(:changeset, :user => private_user))
289 node = create(:node, :changeset => create(:changeset, :user => user))
292 put :update, :params => { :id => node.id }, :body => xml.to_s
293 assert_response :unauthorized
295 ## Second test with the private user
298 basic_authorization private_user.email, "test"
300 ## trying to break changesets
302 # try and update in someone else's changeset
303 xml = update_changeset(private_node.to_xml,
304 create(:changeset).id)
305 put :update, :params => { :id => private_node.id }, :body => xml.to_s
306 assert_require_public_data "update with other user's changeset should be forbidden when data isn't public"
308 # try and update in a closed changeset
309 xml = update_changeset(private_node.to_xml,
310 create(:changeset, :closed, :user => private_user).id)
311 put :update, :params => { :id => private_node.id }, :body => xml.to_s
312 assert_require_public_data "update with closed changeset should be forbidden, when data isn't public"
314 # try and update in a non-existant changeset
315 xml = update_changeset(private_node.to_xml, 0)
316 put :update, :params => { :id => private_node.id }, :body => xml.to_s
317 assert_require_public_data "update with changeset=0 should be forbidden, when data isn't public"
319 ## try and submit invalid updates
320 xml = xml_attr_rewrite(private_node.to_xml, "lat", 91.0)
321 put :update, :params => { :id => private_node.id }, :body => xml.to_s
322 assert_require_public_data "node at lat=91 should be forbidden, when data isn't public"
324 xml = xml_attr_rewrite(private_node.to_xml, "lat", -91.0)
325 put :update, :params => { :id => private_node.id }, :body => xml.to_s
326 assert_require_public_data "node at lat=-91 should be forbidden, when data isn't public"
328 xml = xml_attr_rewrite(private_node.to_xml, "lon", 181.0)
329 put :update, :params => { :id => private_node.id }, :body => xml.to_s
330 assert_require_public_data "node at lon=181 should be forbidden, when data isn't public"
332 xml = xml_attr_rewrite(private_node.to_xml, "lon", -181.0)
333 put :update, :params => { :id => private_node.id }, :body => xml.to_s
334 assert_require_public_data "node at lon=-181 should be forbidden, when data isn't public"
336 ## finally, produce a good request which still won't work
337 xml = private_node.to_xml
338 put :update, :params => { :id => private_node.id }, :body => xml.to_s
339 assert_require_public_data "should have failed with a forbidden when data isn't public"
341 ## Finally test with the public user
343 # try and update a node without authorisation
344 # first try to update node without auth
346 put :update, :params => { :id => node.id }, :body => xml.to_s
347 assert_response :forbidden
350 basic_authorization user.email, "test"
352 ## trying to break changesets
354 # try and update in someone else's changeset
355 xml = update_changeset(node.to_xml,
356 create(:changeset).id)
357 put :update, :params => { :id => node.id }, :body => xml.to_s
358 assert_response :conflict, "update with other user's changeset should be rejected"
360 # try and update in a closed changeset
361 xml = update_changeset(node.to_xml,
362 create(:changeset, :closed, :user => user).id)
363 put :update, :params => { :id => node.id }, :body => xml.to_s
364 assert_response :conflict, "update with closed changeset should be rejected"
366 # try and update in a non-existant changeset
367 xml = update_changeset(node.to_xml, 0)
368 put :update, :params => { :id => node.id }, :body => xml.to_s
369 assert_response :conflict, "update with changeset=0 should be rejected"
371 ## try and submit invalid updates
372 xml = xml_attr_rewrite(node.to_xml, "lat", 91.0)
373 put :update, :params => { :id => node.id }, :body => xml.to_s
374 assert_response :bad_request, "node at lat=91 should be rejected"
376 xml = xml_attr_rewrite(node.to_xml, "lat", -91.0)
377 put :update, :params => { :id => node.id }, :body => xml.to_s
378 assert_response :bad_request, "node at lat=-91 should be rejected"
380 xml = xml_attr_rewrite(node.to_xml, "lon", 181.0)
381 put :update, :params => { :id => node.id }, :body => xml.to_s
382 assert_response :bad_request, "node at lon=181 should be rejected"
384 xml = xml_attr_rewrite(node.to_xml, "lon", -181.0)
385 put :update, :params => { :id => node.id }, :body => xml.to_s
386 assert_response :bad_request, "node at lon=-181 should be rejected"
388 ## next, attack the versioning
389 current_node_version = node.version
391 # try and submit a version behind
392 xml = xml_attr_rewrite(node.to_xml,
393 "version", current_node_version - 1)
394 put :update, :params => { :id => node.id }, :body => xml.to_s
395 assert_response :conflict, "should have failed on old version number"
397 # try and submit a version ahead
398 xml = xml_attr_rewrite(node.to_xml,
399 "version", current_node_version + 1)
400 put :update, :params => { :id => node.id }, :body => xml.to_s
401 assert_response :conflict, "should have failed on skipped version number"
403 # try and submit total crap in the version field
404 xml = xml_attr_rewrite(node.to_xml,
405 "version", "p1r4t3s!")
406 put :update, :params => { :id => node.id }, :body => xml.to_s
407 assert_response :conflict,
408 "should not be able to put 'p1r4at3s!' in the version field"
410 ## try an update with the wrong ID
411 xml = create(:node).to_xml
412 put :update, :params => { :id => node.id }, :body => xml.to_s
413 assert_response :bad_request,
414 "should not be able to update a node with a different ID from the XML"
416 ## try an update with a minimal valid XML doc which isn't a well-formed OSM doc.
418 put :update, :params => { :id => node.id }, :body => xml.to_s
419 assert_response :bad_request,
420 "should not be able to update a node with non-OSM XML doc."
422 ## finally, produce a good request which should work
424 put :update, :params => { :id => node.id }, :body => xml.to_s
425 assert_response :success, "a valid update request failed"
429 # test fetching multiple nodes
431 node1 = create(:node)
432 node2 = create(:node, :deleted)
433 node3 = create(:node)
434 node4 = create(:node, :with_history, :version => 2)
435 node5 = create(:node, :deleted, :with_history, :version => 2)
437 # check error when no parameter provided
439 assert_response :bad_request
441 # check error when no parameter value provided
442 get :nodes, :params => { :nodes => "" }
443 assert_response :bad_request
445 # test a working call
446 get :nodes, :params => { :nodes => "#{node1.id},#{node2.id},#{node3.id},#{node4.id},#{node5.id}" }
447 assert_response :success
448 assert_select "osm" do
449 assert_select "node", :count => 5
450 assert_select "node[id='#{node1.id}'][visible='true']", :count => 1
451 assert_select "node[id='#{node2.id}'][visible='false']", :count => 1
452 assert_select "node[id='#{node3.id}'][visible='true']", :count => 1
453 assert_select "node[id='#{node4.id}'][visible='true']", :count => 1
454 assert_select "node[id='#{node5.id}'][visible='false']", :count => 1
457 # check error when a non-existent node is included
458 get :nodes, :params => { :nodes => "#{node1.id},#{node2.id},#{node3.id},#{node4.id},#{node5.id},0" }
459 assert_response :not_found
463 # test adding tags to a node
464 def test_duplicate_tags
465 existing_tag = create(:node_tag)
466 assert_equal true, existing_tag.node.changeset.user.data_public
468 basic_authorization existing_tag.node.changeset.user.email, "test"
470 # add an identical tag to the node
471 tag_xml = XML::Node.new("tag")
472 tag_xml["k"] = existing_tag.k
473 tag_xml["v"] = existing_tag.v
475 # add the tag into the existing xml
476 node_xml = existing_tag.node.to_xml
477 node_xml.find("//osm/node").first << tag_xml
480 put :update, :params => { :id => existing_tag.node.id }, :body => node_xml.to_s
481 assert_response :bad_request,
482 "adding duplicate tags to a node should fail with 'bad request'"
483 assert_equal "Element node/#{existing_tag.node.id} has duplicate tags with key #{existing_tag.k}", @response.body
486 # test whether string injection is possible
487 def test_string_injection
488 private_user = create(:user, :data_public => false)
489 private_changeset = create(:changeset, :user => private_user)
491 changeset = create(:changeset, :user => user)
493 ## First try with the non-data public user
494 basic_authorization private_user.email, "test"
496 # try and put something into a string that the API might
497 # use unquoted and therefore allow code injection...
498 xml = "<osm><node lat='0' lon='0' changeset='#{private_changeset.id}'>" \
499 '<tag k="#{@user.inspect}" v="0"/>' \
501 put :create, :body => xml
502 assert_require_public_data "Shouldn't be able to create with non-public user"
504 ## Then try with the public data user
505 basic_authorization user.email, "test"
507 # try and put something into a string that the API might
508 # use unquoted and therefore allow code injection...
509 xml = "<osm><node lat='0' lon='0' changeset='#{changeset.id}'>" \
510 '<tag k="#{@user.inspect}" v="0"/>' \
512 put :create, :body => xml
513 assert_response :success
514 nodeid = @response.body
516 # find the node in the database
517 checknode = Node.find(nodeid)
518 assert_not_nil checknode, "node not found in data base after upload"
520 # and grab it using the api
521 get :read, :params => { :id => nodeid }
522 assert_response :success
523 apinode = Node.from_xml(@response.body)
524 assert_not_nil apinode, "downloaded node is nil, but shouldn't be"
526 # check the tags are not corrupted
527 assert_equal checknode.tags, apinode.tags
528 assert apinode.tags.include?("\#{@user.inspect}")
532 # update the changeset_id of a node element
533 def update_changeset(xml, changeset_id)
534 xml_attr_rewrite(xml, "changeset", changeset_id)
538 # update an attribute in the node element
539 def xml_attr_rewrite(xml, name, value)
540 xml.find("//osm/node").first[name] = value.to_s
547 parser = XML::Parser.string(xml)