3 class NodeControllerTest < ActionController::TestCase
7 # test all routes which lead to this controller
10 { :path => "/api/0.6/node/create", :method => :put },
11 { :controller => "node", :action => "create" }
14 { :path => "/api/0.6/node/1", :method => :get },
15 { :controller => "node", :action => "read", :id => "1" }
18 { :path => "/api/0.6/node/1", :method => :put },
19 { :controller => "node", :action => "update", :id => "1" }
22 { :path => "/api/0.6/node/1", :method => :delete },
23 { :controller => "node", :action => "delete", :id => "1" }
26 { :path => "/api/0.6/nodes", :method => :get },
27 { :controller => "node", :action => "nodes" }
32 private_user = create(:user, :data_public => false)
33 private_changeset = create(:changeset, :user => private_user)
35 changeset = create(:changeset, :user => user)
37 # create a node with random lat/lon
38 lat = rand(100) - 50 + rand
39 lon = rand(100) - 50 + rand
41 ## First try with no auth
42 # create a minimal xml file
43 content("<osm><node lat='#{lat}' lon='#{lon}' changeset='#{changeset.id}'/></osm>")
44 assert_difference("OldNode.count", 0) do
47 # hope for unauthorized
48 assert_response :unauthorized, "node upload did not return unauthorized status"
50 ## Now try with the user which doesn't have their data public
51 basic_authorization(private_user.email, "test")
53 # create a minimal xml file
54 content("<osm><node lat='#{lat}' lon='#{lon}' changeset='#{private_changeset.id}'/></osm>")
55 assert_difference("Node.count", 0) do
59 assert_require_public_data "node create did not return forbidden status"
61 ## Now try with the user that has the public data
62 basic_authorization(user.email, "test")
64 # create a minimal xml file
65 content("<osm><node lat='#{lat}' lon='#{lon}' changeset='#{changeset.id}'/></osm>")
68 assert_response :success, "node upload did not return success status"
70 # read id of created node and search for it
71 nodeid = @response.body
72 checknode = Node.find(nodeid)
73 assert_not_nil checknode, "uploaded node not found in data base after upload"
75 assert_in_delta lat * 10000000, checknode.latitude, 1, "saved node does not match requested latitude"
76 assert_in_delta lon * 10000000, checknode.longitude, 1, "saved node does not match requested longitude"
77 assert_equal changeset.id, checknode.changeset_id, "saved node does not belong to changeset that it was created in"
78 assert_equal true, checknode.visible, "saved node is not visible"
81 def test_create_invalid_xml
82 ## Only test public user here, as test_create should cover what's the forbiddens
83 ## that would occur here
86 changeset = create(:changeset, :user => user)
88 basic_authorization(user.email, "test")
92 # test that the upload is rejected when xml is valid, but osm doc isn't
95 assert_response :bad_request, "node upload did not return bad_request status"
96 assert_equal "Cannot parse valid node from xml string <create/>. XML doesn't contain an osm/node element.", @response.body
98 # test that the upload is rejected when no lat is supplied
99 # create a minimal xml file
100 content("<osm><node lon='#{lon}' changeset='#{changeset.id}'/></osm>")
103 assert_response :bad_request, "node upload did not return bad_request status"
104 assert_equal "Cannot parse valid node from xml string <node lon=\"3.23\" changeset=\"#{changeset.id}\"/>. lat missing", @response.body
106 # test that the upload is rejected when no lon is supplied
107 # create a minimal xml file
108 content("<osm><node lat='#{lat}' changeset='#{changeset.id}'/></osm>")
111 assert_response :bad_request, "node upload did not return bad_request status"
112 assert_equal "Cannot parse valid node from xml string <node lat=\"3.434\" changeset=\"#{changeset.id}\"/>. lon missing", @response.body
114 # test that the upload is rejected when lat is non-numeric
115 # create a minimal xml file
116 content("<osm><node lat='abc' lon='#{lon}' changeset='#{changeset.id}'/></osm>")
119 assert_response :bad_request, "node upload did not return bad_request status"
120 assert_equal "Cannot parse valid node from xml string <node lat=\"abc\" lon=\"#{lon}\" changeset=\"#{changeset.id}\"/>. lat not a number", @response.body
122 # test that the upload is rejected when lon is non-numeric
123 # create a minimal xml file
124 content("<osm><node lat='#{lat}' lon='abc' changeset='#{changeset.id}'/></osm>")
127 assert_response :bad_request, "node upload did not return bad_request status"
128 assert_equal "Cannot parse valid node from xml string <node lat=\"#{lat}\" lon=\"abc\" changeset=\"#{changeset.id}\"/>. lon not a number", @response.body
130 # test that the upload is rejected when we have a tag which is too long
131 content("<osm><node lat='#{lat}' lon='#{lon}' changeset='#{changeset.id}'><tag k='foo' v='#{'x' * 256}'/></node></osm>")
133 assert_response :bad_request, "node upload did not return bad_request status"
134 assert_equal ["NodeTag ", " v: is too long (maximum is 255 characters) (\"#{'x' * 256}\")"], @response.body.split(/[0-9]+,foo:/)
138 # check that a visible node is returned properly
139 get :read, :id => create(:node).id
140 assert_response :success
142 # check that an deleted node is not returned
143 get :read, :id => create(:node, :deleted).id
144 assert_response :gone
146 # check chat a non-existent node is not returned
148 assert_response :not_found
151 # this tests deletion restrictions - basic deletion is tested in the unit
154 private_user = create(:user, :data_public => false)
155 private_user_changeset = create(:changeset, :user => private_user)
156 private_user_closed_changeset = create(:changeset, :closed, :user => private_user)
157 private_node = create(:node, :changeset => private_user_changeset)
158 private_deleted_node = create(:node, :deleted, :changeset => private_user_changeset)
160 ## first try to delete node without auth
161 delete :delete, :id => private_node.id
162 assert_response :unauthorized
164 ## now set auth for the non-data public user
165 basic_authorization(private_user.email, "test")
167 # try to delete with an invalid (closed) changeset
168 content update_changeset(private_node.to_xml, private_user_closed_changeset.id)
169 delete :delete, :id => private_node.id
170 assert_require_public_data("non-public user shouldn't be able to delete node")
172 # try to delete with an invalid (non-existent) changeset
173 content update_changeset(private_node.to_xml, 0)
174 delete :delete, :id => private_node.id
175 assert_require_public_data("shouldn't be able to delete node, when user's data is private")
177 # valid delete now takes a payload
178 content(private_node.to_xml)
179 delete :delete, :id => private_node.id
180 assert_require_public_data("shouldn't be able to delete node when user's data isn't public'")
182 # this won't work since the node is already deleted
183 content(private_deleted_node.to_xml)
184 delete :delete, :id => private_deleted_node.id
185 assert_require_public_data
187 # this won't work since the node never existed
188 delete :delete, :id => 0
189 assert_require_public_data
191 ## these test whether nodes which are in-use can be deleted:
193 private_used_node = create(:node, :changeset => private_user_changeset)
194 create(:way_node, :node => private_used_node)
196 content(private_used_node.to_xml)
197 delete :delete, :id => private_used_node.id
198 assert_require_public_data "shouldn't be able to delete a node used in a way (#{@response.body})"
201 private_used_node2 = create(:node, :changeset => private_user_changeset)
202 create(:relation_member, :member => private_used_node2)
204 content(private_used_node2.to_xml)
205 delete :delete, :id => private_used_node2.id
206 assert_require_public_data "shouldn't be able to delete a node used in a relation (#{@response.body})"
208 ## now setup for the public data user
209 user = create(:user, :data_public => true)
210 changeset = create(:changeset, :user => user)
211 closed_changeset = create(:changeset, :closed, :user => user)
212 node = create(:node, :changeset => changeset)
213 basic_authorization(user.email, "test")
215 # try to delete with an invalid (closed) changeset
216 content update_changeset(node.to_xml, closed_changeset.id)
217 delete :delete, :id => node.id
218 assert_response :conflict
220 # try to delete with an invalid (non-existent) changeset
221 content update_changeset(node.to_xml, 0)
222 delete :delete, :id => node.id
223 assert_response :conflict
225 # try to delete a node with a different ID
226 other_node = create(:node)
227 content(other_node.to_xml)
228 delete :delete, :id => node.id
229 assert_response :bad_request,
230 "should not be able to delete a node with a different ID from the XML"
232 # try to delete a node rubbish in the payloads
234 delete :delete, :id => node.id
235 assert_response :bad_request,
236 "should not be able to delete a node without a valid XML payload"
238 # valid delete now takes a payload
240 delete :delete, :id => node.id
241 assert_response :success
243 # valid delete should return the new version number, which should
244 # be greater than the old version number
245 assert @response.body.to_i > node.version,
246 "delete request should return a new version number for node"
248 # deleting the same node twice doesn't work
250 delete :delete, :id => node.id
251 assert_response :gone
253 # this won't work since the node never existed
254 delete :delete, :id => 0
255 assert_response :not_found
257 ## these test whether nodes which are in-use can be deleted:
259 used_node = create(:node, :changeset => create(:changeset, :user => user))
260 way_node = create(:way_node, :node => used_node)
261 way_node2 = create(:way_node, :node => used_node)
263 content(used_node.to_xml)
264 delete :delete, :id => used_node.id
265 assert_response :precondition_failed,
266 "shouldn't be able to delete a node used in a way (#{@response.body})"
267 assert_equal "Precondition failed: Node #{used_node.id} is still used by ways #{way_node.way.id},#{way_node2.way.id}.", @response.body
270 used_node2 = create(:node, :changeset => create(:changeset, :user => user))
271 relation_member = create(:relation_member, :member => used_node2)
272 relation_member2 = create(:relation_member, :member => used_node2)
274 content(used_node2.to_xml)
275 delete :delete, :id => used_node2.id
276 assert_response :precondition_failed,
277 "shouldn't be able to delete a node used in a relation (#{@response.body})"
278 assert_equal "Precondition failed: Node #{used_node2.id} is still used by relations #{relation_member.relation.id},#{relation_member2.relation.id}.", @response.body
282 # tests whether the API works and prevents incorrect use while trying
285 ## First test with no user credentials
286 # try and update a node without authorisation
287 # first try to delete node without auth
288 private_user = create(:user, :data_public => false)
289 private_node = create(:node, :changeset => create(:changeset, :user => private_user))
291 node = create(:node, :changeset => create(:changeset, :user => user))
294 put :update, :id => node.id
295 assert_response :unauthorized
297 ## Second test with the private user
300 basic_authorization(private_user.email, "test")
302 ## trying to break changesets
304 # try and update in someone else's changeset
305 content update_changeset(private_node.to_xml,
306 create(:changeset).id)
307 put :update, :id => private_node.id
308 assert_require_public_data "update with other user's changeset should be forbidden when data isn't public"
310 # try and update in a closed changeset
311 content update_changeset(private_node.to_xml,
312 create(:changeset, :closed, :user => private_user).id)
313 put :update, :id => private_node.id
314 assert_require_public_data "update with closed changeset should be forbidden, when data isn't public"
316 # try and update in a non-existant changeset
317 content update_changeset(private_node.to_xml, 0)
318 put :update, :id => private_node.id
319 assert_require_public_data "update with changeset=0 should be forbidden, when data isn't public"
321 ## try and submit invalid updates
322 content xml_attr_rewrite(private_node.to_xml, "lat", 91.0)
323 put :update, :id => private_node.id
324 assert_require_public_data "node at lat=91 should be forbidden, when data isn't public"
326 content xml_attr_rewrite(private_node.to_xml, "lat", -91.0)
327 put :update, :id => private_node.id
328 assert_require_public_data "node at lat=-91 should be forbidden, when data isn't public"
330 content xml_attr_rewrite(private_node.to_xml, "lon", 181.0)
331 put :update, :id => private_node.id
332 assert_require_public_data "node at lon=181 should be forbidden, when data isn't public"
334 content xml_attr_rewrite(private_node.to_xml, "lon", -181.0)
335 put :update, :id => private_node.id
336 assert_require_public_data "node at lon=-181 should be forbidden, when data isn't public"
338 ## finally, produce a good request which still won't work
339 content private_node.to_xml
340 put :update, :id => private_node.id
341 assert_require_public_data "should have failed with a forbidden when data isn't public"
343 ## Finally test with the public user
345 # try and update a node without authorisation
346 # first try to update node without auth
348 put :update, :id => node.id
349 assert_response :forbidden
352 basic_authorization(user.email, "test")
354 ## trying to break changesets
356 # try and update in someone else's changeset
357 content update_changeset(node.to_xml,
358 create(:changeset).id)
359 put :update, :id => node.id
360 assert_response :conflict, "update with other user's changeset should be rejected"
362 # try and update in a closed changeset
363 content update_changeset(node.to_xml,
364 create(:changeset, :closed, :user => user).id)
365 put :update, :id => node.id
366 assert_response :conflict, "update with closed changeset should be rejected"
368 # try and update in a non-existant changeset
369 content update_changeset(node.to_xml, 0)
370 put :update, :id => node.id
371 assert_response :conflict, "update with changeset=0 should be rejected"
373 ## try and submit invalid updates
374 content xml_attr_rewrite(node.to_xml, "lat", 91.0)
375 put :update, :id => node.id
376 assert_response :bad_request, "node at lat=91 should be rejected"
378 content xml_attr_rewrite(node.to_xml, "lat", -91.0)
379 put :update, :id => node.id
380 assert_response :bad_request, "node at lat=-91 should be rejected"
382 content xml_attr_rewrite(node.to_xml, "lon", 181.0)
383 put :update, :id => node.id
384 assert_response :bad_request, "node at lon=181 should be rejected"
386 content xml_attr_rewrite(node.to_xml, "lon", -181.0)
387 put :update, :id => node.id
388 assert_response :bad_request, "node at lon=-181 should be rejected"
390 ## next, attack the versioning
391 current_node_version = node.version
393 # try and submit a version behind
394 content xml_attr_rewrite(node.to_xml,
395 "version", current_node_version - 1)
396 put :update, :id => node.id
397 assert_response :conflict, "should have failed on old version number"
399 # try and submit a version ahead
400 content xml_attr_rewrite(node.to_xml,
401 "version", current_node_version + 1)
402 put :update, :id => node.id
403 assert_response :conflict, "should have failed on skipped version number"
405 # try and submit total crap in the version field
406 content xml_attr_rewrite(node.to_xml,
407 "version", "p1r4t3s!")
408 put :update, :id => node.id
409 assert_response :conflict,
410 "should not be able to put 'p1r4at3s!' in the version field"
412 ## try an update with the wrong ID
413 content create(:node).to_xml
414 put :update, :id => node.id
415 assert_response :bad_request,
416 "should not be able to update a node with a different ID from the XML"
418 ## try an update with a minimal valid XML doc which isn't a well-formed OSM doc.
420 put :update, :id => node.id
421 assert_response :bad_request,
422 "should not be able to update a node with non-OSM XML doc."
424 ## finally, produce a good request which should work
426 put :update, :id => node.id
427 assert_response :success, "a valid update request failed"
431 # test fetching multiple nodes
433 # check error when no parameter provided
435 assert_response :bad_request
437 # check error when no parameter value provided
438 get :nodes, :nodes => ""
439 assert_response :bad_request
441 # test a working call
442 get :nodes, :nodes => "1,2,4,15,17"
443 assert_response :success
444 assert_select "osm" do
445 assert_select "node", :count => 5
446 assert_select "node[id='1'][visible='true']", :count => 1
447 assert_select "node[id='2'][visible='false']", :count => 1
448 assert_select "node[id='4'][visible='true']", :count => 1
449 assert_select "node[id='15'][visible='true']", :count => 1
450 assert_select "node[id='17'][visible='false']", :count => 1
453 # check error when a non-existent node is included
454 get :nodes, :nodes => "1,2,4,15,17,400"
455 assert_response :not_found
459 # test adding tags to a node
460 def test_duplicate_tags
461 existing_tag = create(:node_tag)
462 assert_equal true, existing_tag.node.changeset.user.data_public
464 basic_authorization(existing_tag.node.changeset.user.email, "test")
466 # add an identical tag to the node
467 tag_xml = XML::Node.new("tag")
468 tag_xml["k"] = existing_tag.k
469 tag_xml["v"] = existing_tag.v
471 # add the tag into the existing xml
472 node_xml = existing_tag.node.to_xml
473 node_xml.find("//osm/node").first << tag_xml
477 put :update, :id => existing_tag.node.id
478 assert_response :bad_request,
479 "adding duplicate tags to a node should fail with 'bad request'"
480 assert_equal "Element node/#{existing_tag.node.id} has duplicate tags with key #{existing_tag.k}", @response.body
483 # test whether string injection is possible
484 def test_string_injection
485 private_user = create(:user, :data_public => false)
486 private_changeset = create(:changeset, :user => private_user)
488 changeset = create(:changeset, :user => user)
490 ## First try with the non-data public user
491 basic_authorization(private_user.email, "test")
493 # try and put something into a string that the API might
494 # use unquoted and therefore allow code injection...
495 content "<osm><node lat='0' lon='0' changeset='#{private_changeset.id}'>" +
496 '<tag k="#{@user.inspect}" v="0"/>' +
499 assert_require_public_data "Shouldn't be able to create with non-public user"
501 ## Then try with the public data user
502 basic_authorization(user.email, "test")
504 # try and put something into a string that the API might
505 # use unquoted and therefore allow code injection...
506 content "<osm><node lat='0' lon='0' changeset='#{changeset.id}'>" +
507 '<tag k="#{@user.inspect}" v="0"/>' +
510 assert_response :success
511 nodeid = @response.body
513 # find the node in the database
514 checknode = Node.find(nodeid)
515 assert_not_nil checknode, "node not found in data base after upload"
517 # and grab it using the api
518 get :read, :id => nodeid
519 assert_response :success
520 apinode = Node.from_xml(@response.body)
521 assert_not_nil apinode, "downloaded node is nil, but shouldn't be"
523 # check the tags are not corrupted
524 assert_equal checknode.tags, apinode.tags
525 assert apinode.tags.include?("\#{@user.inspect}")
529 # update the changeset_id of a node element
530 def update_changeset(xml, changeset_id)
531 xml_attr_rewrite(xml, "changeset", changeset_id)
535 # update an attribute in the node element
536 def xml_attr_rewrite(xml, name, value)
537 xml.find("//osm/node").first[name] = value.to_s
544 parser = XML::Parser.string(xml)