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 # this tests deletion restrictions - basic deletion is tested in the unit
153 private_user = create(:user, :data_public => false)
154 private_user_changeset = create(:changeset, :user => private_user)
155 private_user_closed_changeset = create(:changeset, :closed, :user => private_user)
156 private_node = create(:node, :changeset => private_user_changeset)
157 private_deleted_node = create(:node, :deleted, :changeset => private_user_changeset)
159 ## first try to delete node without auth
160 delete :delete, :params => { :id => private_node.id }
161 assert_response :unauthorized
163 ## now set auth for the non-data public user
164 basic_authorization private_user.email, "test"
166 # try to delete with an invalid (closed) changeset
167 xml = update_changeset(private_node.to_xml, private_user_closed_changeset.id)
168 delete :delete, :params => { :id => private_node.id }, :body => xml.to_s
169 assert_require_public_data("non-public user shouldn't be able to delete node")
171 # try to delete with an invalid (non-existent) changeset
172 xml = update_changeset(private_node.to_xml, 0)
173 delete :delete, :params => { :id => private_node.id }, :body => xml.to_s
174 assert_require_public_data("shouldn't be able to delete node, when user's data is private")
176 # valid delete now takes a payload
177 xml = private_node.to_xml
178 delete :delete, :params => { :id => private_node.id }, :body => xml.to_s
179 assert_require_public_data("shouldn't be able to delete node when user's data isn't public'")
181 # this won't work since the node is already deleted
182 xml = private_deleted_node.to_xml
183 delete :delete, :params => { :id => private_deleted_node.id }, :body => xml.to_s
184 assert_require_public_data
186 # this won't work since the node never existed
187 delete :delete, :params => { :id => 0 }
188 assert_require_public_data
190 ## these test whether nodes which are in-use can be deleted:
192 private_used_node = create(:node, :changeset => private_user_changeset)
193 create(:way_node, :node => private_used_node)
195 xml = private_used_node.to_xml
196 delete :delete, :params => { :id => private_used_node.id }, :body => xml.to_s
197 assert_require_public_data "shouldn't be able to delete a node used in a way (#{@response.body})"
200 private_used_node2 = create(:node, :changeset => private_user_changeset)
201 create(:relation_member, :member => private_used_node2)
203 xml = private_used_node2.to_xml
204 delete :delete, :params => { :id => private_used_node2.id }, :body => xml.to_s
205 assert_require_public_data "shouldn't be able to delete a node used in a relation (#{@response.body})"
207 ## now setup for the public data user
208 user = create(:user, :data_public => true)
209 changeset = create(:changeset, :user => user)
210 closed_changeset = create(:changeset, :closed, :user => user)
211 node = create(:node, :changeset => changeset)
212 basic_authorization user.email, "test"
214 # try to delete with an invalid (closed) changeset
215 xml = update_changeset(node.to_xml, closed_changeset.id)
216 delete :delete, :params => { :id => node.id }, :body => xml.to_s
217 assert_response :conflict
219 # try to delete with an invalid (non-existent) changeset
220 xml = update_changeset(node.to_xml, 0)
221 delete :delete, :params => { :id => node.id }, :body => xml.to_s
222 assert_response :conflict
224 # try to delete a node with a different ID
225 other_node = create(:node)
226 xml = other_node.to_xml
227 delete :delete, :params => { :id => node.id }, :body => xml.to_s
228 assert_response :bad_request,
229 "should not be able to delete a node with a different ID from the XML"
231 # try to delete a node rubbish in the payloads
233 delete :delete, :params => { :id => node.id }, :body => xml.to_s
234 assert_response :bad_request,
235 "should not be able to delete a node without a valid XML payload"
237 # valid delete now takes a payload
239 delete :delete, :params => { :id => node.id }, :body => xml.to_s
240 assert_response :success
242 # valid delete should return the new version number, which should
243 # be greater than the old version number
244 assert @response.body.to_i > node.version,
245 "delete request should return a new version number for node"
247 # deleting the same node twice doesn't work
249 delete :delete, :params => { :id => node.id }, :body => xml.to_s
250 assert_response :gone
252 # this won't work since the node never existed
253 delete :delete, :params => { :id => 0 }
254 assert_response :not_found
256 ## these test whether nodes which are in-use can be deleted:
258 used_node = create(:node, :changeset => create(:changeset, :user => user))
259 way_node = create(:way_node, :node => used_node)
260 way_node2 = create(:way_node, :node => used_node)
262 xml = used_node.to_xml
263 delete :delete, :params => { :id => used_node.id }, :body => xml.to_s
264 assert_response :precondition_failed,
265 "shouldn't be able to delete a node used in a way (#{@response.body})"
266 assert_equal "Precondition failed: Node #{used_node.id} is still used by ways #{way_node.way.id},#{way_node2.way.id}.", @response.body
269 used_node2 = create(:node, :changeset => create(:changeset, :user => user))
270 relation_member = create(:relation_member, :member => used_node2)
271 relation_member2 = create(:relation_member, :member => used_node2)
273 xml = used_node2.to_xml
274 delete :delete, :params => { :id => used_node2.id }, :body => xml.to_s
275 assert_response :precondition_failed,
276 "shouldn't be able to delete a node used in a relation (#{@response.body})"
277 assert_equal "Precondition failed: Node #{used_node2.id} is still used by relations #{relation_member.relation.id},#{relation_member2.relation.id}.", @response.body
281 # tests whether the API works and prevents incorrect use while trying
284 ## First test with no user credentials
285 # try and update a node without authorisation
286 # first try to delete node without auth
287 private_user = create(:user, :data_public => false)
288 private_node = create(:node, :changeset => create(:changeset, :user => private_user))
290 node = create(:node, :changeset => create(:changeset, :user => user))
293 put :update, :params => { :id => node.id }, :body => xml.to_s
294 assert_response :unauthorized
296 ## Second test with the private user
299 basic_authorization private_user.email, "test"
301 ## trying to break changesets
303 # try and update in someone else's changeset
304 xml = update_changeset(private_node.to_xml,
305 create(:changeset).id)
306 put :update, :params => { :id => private_node.id }, :body => xml.to_s
307 assert_require_public_data "update with other user's changeset should be forbidden when data isn't public"
309 # try and update in a closed changeset
310 xml = update_changeset(private_node.to_xml,
311 create(:changeset, :closed, :user => private_user).id)
312 put :update, :params => { :id => private_node.id }, :body => xml.to_s
313 assert_require_public_data "update with closed changeset should be forbidden, when data isn't public"
315 # try and update in a non-existant changeset
316 xml = update_changeset(private_node.to_xml, 0)
317 put :update, :params => { :id => private_node.id }, :body => xml.to_s
318 assert_require_public_data "update with changeset=0 should be forbidden, when data isn't public"
320 ## try and submit invalid updates
321 xml = xml_attr_rewrite(private_node.to_xml, "lat", 91.0)
322 put :update, :params => { :id => private_node.id }, :body => xml.to_s
323 assert_require_public_data "node at lat=91 should be forbidden, when data isn't public"
325 xml = xml_attr_rewrite(private_node.to_xml, "lat", -91.0)
326 put :update, :params => { :id => private_node.id }, :body => xml.to_s
327 assert_require_public_data "node at lat=-91 should be forbidden, when data isn't public"
329 xml = xml_attr_rewrite(private_node.to_xml, "lon", 181.0)
330 put :update, :params => { :id => private_node.id }, :body => xml.to_s
331 assert_require_public_data "node at lon=181 should be forbidden, when data isn't public"
333 xml = xml_attr_rewrite(private_node.to_xml, "lon", -181.0)
334 put :update, :params => { :id => private_node.id }, :body => xml.to_s
335 assert_require_public_data "node at lon=-181 should be forbidden, when data isn't public"
337 ## finally, produce a good request which still won't work
338 xml = private_node.to_xml
339 put :update, :params => { :id => private_node.id }, :body => xml.to_s
340 assert_require_public_data "should have failed with a forbidden when data isn't public"
342 ## Finally test with the public user
344 # try and update a node without authorisation
345 # first try to update node without auth
347 put :update, :params => { :id => node.id }, :body => xml.to_s
348 assert_response :forbidden
351 basic_authorization user.email, "test"
353 ## trying to break changesets
355 # try and update in someone else's changeset
356 xml = update_changeset(node.to_xml,
357 create(:changeset).id)
358 put :update, :params => { :id => node.id }, :body => xml.to_s
359 assert_response :conflict, "update with other user's changeset should be rejected"
361 # try and update in a closed changeset
362 xml = update_changeset(node.to_xml,
363 create(:changeset, :closed, :user => user).id)
364 put :update, :params => { :id => node.id }, :body => xml.to_s
365 assert_response :conflict, "update with closed changeset should be rejected"
367 # try and update in a non-existant changeset
368 xml = update_changeset(node.to_xml, 0)
369 put :update, :params => { :id => node.id }, :body => xml.to_s
370 assert_response :conflict, "update with changeset=0 should be rejected"
372 ## try and submit invalid updates
373 xml = xml_attr_rewrite(node.to_xml, "lat", 91.0)
374 put :update, :params => { :id => node.id }, :body => xml.to_s
375 assert_response :bad_request, "node at lat=91 should be rejected"
377 xml = xml_attr_rewrite(node.to_xml, "lat", -91.0)
378 put :update, :params => { :id => node.id }, :body => xml.to_s
379 assert_response :bad_request, "node at lat=-91 should be rejected"
381 xml = xml_attr_rewrite(node.to_xml, "lon", 181.0)
382 put :update, :params => { :id => node.id }, :body => xml.to_s
383 assert_response :bad_request, "node at lon=181 should be rejected"
385 xml = xml_attr_rewrite(node.to_xml, "lon", -181.0)
386 put :update, :params => { :id => node.id }, :body => xml.to_s
387 assert_response :bad_request, "node at lon=-181 should be rejected"
389 ## next, attack the versioning
390 current_node_version = node.version
392 # try and submit a version behind
393 xml = xml_attr_rewrite(node.to_xml,
394 "version", current_node_version - 1)
395 put :update, :params => { :id => node.id }, :body => xml.to_s
396 assert_response :conflict, "should have failed on old version number"
398 # try and submit a version ahead
399 xml = xml_attr_rewrite(node.to_xml,
400 "version", current_node_version + 1)
401 put :update, :params => { :id => node.id }, :body => xml.to_s
402 assert_response :conflict, "should have failed on skipped version number"
404 # try and submit total crap in the version field
405 xml = xml_attr_rewrite(node.to_xml,
406 "version", "p1r4t3s!")
407 put :update, :params => { :id => node.id }, :body => xml.to_s
408 assert_response :conflict,
409 "should not be able to put 'p1r4at3s!' in the version field"
411 ## try an update with the wrong ID
412 xml = create(:node).to_xml
413 put :update, :params => { :id => node.id }, :body => xml.to_s
414 assert_response :bad_request,
415 "should not be able to update a node with a different ID from the XML"
417 ## try an update with a minimal valid XML doc which isn't a well-formed OSM doc.
419 put :update, :params => { :id => node.id }, :body => xml.to_s
420 assert_response :bad_request,
421 "should not be able to update a node with non-OSM XML doc."
423 ## finally, produce a good request which should work
425 put :update, :params => { :id => node.id }, :body => xml.to_s
426 assert_response :success, "a valid update request failed"
430 # test fetching multiple nodes
432 node1 = create(:node)
433 node2 = create(:node, :deleted)
434 node3 = create(:node)
435 node4 = create(:node, :with_history, :version => 2)
436 node5 = create(:node, :deleted, :with_history, :version => 2)
438 # check error when no parameter provided
440 assert_response :bad_request
442 # check error when no parameter value provided
443 get :index, :params => { :nodes => "" }
444 assert_response :bad_request
446 # test a working call
447 get :index, :params => { :nodes => "#{node1.id},#{node2.id},#{node3.id},#{node4.id},#{node5.id}" }
448 assert_response :success
449 assert_select "osm" do
450 assert_select "node", :count => 5
451 assert_select "node[id='#{node1.id}'][visible='true']", :count => 1
452 assert_select "node[id='#{node2.id}'][visible='false']", :count => 1
453 assert_select "node[id='#{node3.id}'][visible='true']", :count => 1
454 assert_select "node[id='#{node4.id}'][visible='true']", :count => 1
455 assert_select "node[id='#{node5.id}'][visible='false']", :count => 1
458 # check error when a non-existent node is included
459 get :index, :params => { :nodes => "#{node1.id},#{node2.id},#{node3.id},#{node4.id},#{node5.id},0" }
460 assert_response :not_found
464 # test adding tags to a node
465 def test_duplicate_tags
466 existing_tag = create(:node_tag)
467 assert_equal true, existing_tag.node.changeset.user.data_public
469 basic_authorization existing_tag.node.changeset.user.email, "test"
471 # add an identical tag to the node
472 tag_xml = XML::Node.new("tag")
473 tag_xml["k"] = existing_tag.k
474 tag_xml["v"] = existing_tag.v
476 # add the tag into the existing xml
477 node_xml = existing_tag.node.to_xml
478 node_xml.find("//osm/node").first << tag_xml
481 put :update, :params => { :id => existing_tag.node.id }, :body => node_xml.to_s
482 assert_response :bad_request,
483 "adding duplicate tags to a node should fail with 'bad request'"
484 assert_equal "Element node/#{existing_tag.node.id} has duplicate tags with key #{existing_tag.k}", @response.body
487 # test whether string injection is possible
488 def test_string_injection
489 private_user = create(:user, :data_public => false)
490 private_changeset = create(:changeset, :user => private_user)
492 changeset = create(:changeset, :user => user)
494 ## First try with the non-data public user
495 basic_authorization private_user.email, "test"
497 # try and put something into a string that the API might
498 # use unquoted and therefore allow code injection...
499 xml = "<osm><node lat='0' lon='0' changeset='#{private_changeset.id}'>" \
500 '<tag k="#{@user.inspect}" v="0"/>' \
502 put :create, :body => xml
503 assert_require_public_data "Shouldn't be able to create with non-public user"
505 ## Then try with the public data user
506 basic_authorization user.email, "test"
508 # try and put something into a string that the API might
509 # use unquoted and therefore allow code injection...
510 xml = "<osm><node lat='0' lon='0' changeset='#{changeset.id}'>" \
511 '<tag k="#{@user.inspect}" v="0"/>' \
513 put :create, :body => xml
514 assert_response :success
515 nodeid = @response.body
517 # find the node in the database
518 checknode = Node.find(nodeid)
519 assert_not_nil checknode, "node not found in data base after upload"
521 # and grab it using the api
522 get :show, :params => { :id => nodeid }
523 assert_response :success
524 apinode = Node.from_xml(@response.body)
525 assert_not_nil apinode, "downloaded node is nil, but shouldn't be"
527 # check the tags are not corrupted
528 assert_equal checknode.tags, apinode.tags
529 assert apinode.tags.include?("\#{@user.inspect}")
533 # update the changeset_id of a node element
534 def update_changeset(xml, changeset_id)
535 xml_attr_rewrite(xml, "changeset", changeset_id)
539 # update an attribute in the node element
540 def xml_attr_rewrite(xml, name, value)
541 xml.find("//osm/node").first[name] = value.to_s
548 parser = XML::Parser.string(xml)