1 require File.dirname(__FILE__) + '/../test_helper'
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 # cannot read password from fixture as it is stored as MD5 digest
33 ## First try with no auth
35 # create a node with random lat/lon
36 lat = rand(100)-50 + rand
37 lon = rand(100)-50 + rand
38 # normal user has a changeset open, so we'll use that.
39 changeset = changesets(:normal_user_first_change)
40 # create a minimal xml file
41 content("<osm><node lat='#{lat}' lon='#{lon}' changeset='#{changeset.id}'/></osm>")
42 assert_difference('OldNode.count', 0) do
45 # hope for unauthorized
46 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(users(:normal_user).email, "test")
53 # create a node with random lat/lon
54 lat = rand(100)-50 + rand
55 lon = rand(100)-50 + rand
56 # normal user has a changeset open, so we'll use that.
57 changeset = changesets(:normal_user_first_change)
58 # create a minimal xml file
59 content("<osm><node lat='#{lat}' lon='#{lon}' changeset='#{changeset.id}'/></osm>")
60 assert_difference('Node.count', 0) do
64 assert_require_public_data "node create did not return forbidden status"
68 ## Now try with the user that has the public data
69 basic_authorization(users(:public_user).email, "test")
71 # create a node with random lat/lon
72 lat = rand(100)-50 + rand
73 lon = rand(100)-50 + rand
74 # normal user has a changeset open, so we'll use that.
75 changeset = changesets(:public_user_first_change)
76 # create a minimal xml file
77 content("<osm><node lat='#{lat}' lon='#{lon}' changeset='#{changeset.id}'/></osm>")
80 assert_response :success, "node upload did not return success status"
82 # read id of created node and search for it
83 nodeid = @response.body
84 checknode = Node.find(nodeid)
85 assert_not_nil checknode, "uploaded node not found in data base after upload"
87 assert_in_delta lat * 10000000, checknode.latitude, 1, "saved node does not match requested latitude"
88 assert_in_delta lon * 10000000, checknode.longitude, 1, "saved node does not match requested longitude"
89 assert_equal changesets(:public_user_first_change).id, checknode.changeset_id, "saved node does not belong to changeset that it was created in"
90 assert_equal true, checknode.visible, "saved node is not visible"
93 def test_create_invalid_xml
94 ## Only test public user here, as test_create should cover what's the forbiddens
95 ## that would occur here
97 basic_authorization(users(:public_user).email, "test")
98 # normal user has a changeset open, so we'll use that.
99 changeset = changesets(:public_user_first_change)
103 # test that the upload is rejected when xml is valid, but osm doc isn't
106 assert_response :bad_request, "node upload did not return bad_request status"
107 assert_equal "Cannot parse valid node from xml string <create/>. XML doesn't contain an osm/node element.", @response.body
109 # test that the upload is rejected when no lat is supplied
110 # create a minimal xml file
111 content("<osm><node lon='#{lon}' changeset='#{changeset.id}'/></osm>")
114 assert_response :bad_request, "node upload did not return bad_request status"
115 assert_equal "Cannot parse valid node from xml string <node lon=\"3.23\" changeset=\"#{changeset.id}\"/>. lat missing", @response.body
117 # test that the upload is rejected when no lon is supplied
118 # create a minimal xml file
119 content("<osm><node lat='#{lat}' changeset='#{changeset.id}'/></osm>")
122 assert_response :bad_request, "node upload did not return bad_request status"
123 assert_equal "Cannot parse valid node from xml string <node lat=\"3.434\" changeset=\"#{changeset.id}\"/>. lon missing", @response.body
125 # test that the upload is rejected when lat is non-numeric
126 # create a minimal xml file
127 content("<osm><node lat='abc' lon='#{lon}' changeset='#{changeset.id}'/></osm>")
130 assert_response :bad_request, "node upload did not return bad_request status"
131 assert_equal "Cannot parse valid node from xml string <node lat=\"abc\" lon=\"#{lon}\" changeset=\"#{changeset.id}\"/>. lat not a number", @response.body
133 # test that the upload is rejected when lon is non-numeric
134 # create a minimal xml file
135 content("<osm><node lat='#{lat}' lon='abc' changeset='#{changeset.id}'/></osm>")
138 assert_response :bad_request, "node upload did not return bad_request status"
139 assert_equal "Cannot parse valid node from xml string <node lat=\"#{lat}\" lon=\"abc\" changeset=\"#{changeset.id}\"/>. lon not a number", @response.body
141 # test that the upload is rejected when we have a tag which is too long
142 content("<osm><node lat='#{lat}' lon='#{lon}' changeset='#{changeset.id}'><tag k='foo' v='#{'x'*256}'/></node></osm>")
144 assert_response :bad_request, "node upload did not return bad_request status"
145 assert_equal ["NodeTag ", " v: is too long (maximum is 255 characters) (\"#{'x'*256}\")"], @response.body.split(/[0-9]+,foo:/)
150 # check that a visible node is returned properly
151 get :read, :id => current_nodes(:visible_node).id
152 assert_response :success
154 # check that an invisible node is not returned
155 get :read, :id => current_nodes(:invisible_node).id
156 assert_response :gone
158 # check chat a non-existent node is not returned
160 assert_response :not_found
163 # this tests deletion restrictions - basic deletion is tested in the unit
166 ## first try to delete node without auth
167 delete :delete, :id => current_nodes(:visible_node).id
168 assert_response :unauthorized
171 ## now set auth for the non-data public user
172 basic_authorization(users(:normal_user).email, "test");
174 # try to delete with an invalid (closed) changeset
175 content update_changeset(current_nodes(:visible_node).to_xml,
176 changesets(:normal_user_closed_change).id)
177 delete :delete, :id => current_nodes(:visible_node).id
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 content update_changeset(current_nodes(:visible_node).to_xml,0)
182 delete :delete, :id => current_nodes(:visible_node).id
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 content(nodes(:visible_node).to_xml)
187 delete :delete, :id => current_nodes(:visible_node).id
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 content(nodes(:invisible_node).to_xml)
192 delete :delete, :id => current_nodes(:invisible_node).id
193 assert_require_public_data
195 # this won't work since the node never existed
196 delete :delete, :id => 0
197 assert_require_public_data
199 ## these test whether nodes which are in-use can be deleted:
201 content(nodes(:used_node_1).to_xml)
202 delete :delete, :id => current_nodes(:used_node_1).id
203 assert_require_public_data
204 "shouldn't be able to delete a node used in a way (#{@response.body})"
207 content(nodes(:node_used_by_relationship).to_xml)
208 delete :delete, :id => current_nodes(:node_used_by_relationship).id
209 assert_require_public_data
210 "shouldn't be able to delete a node used in a relation (#{@response.body})"
214 ## now set auth for the public data user
215 basic_authorization(users(:public_user).email, "test");
217 # try to delete with an invalid (closed) changeset
218 content update_changeset(current_nodes(:visible_node).to_xml,
219 changesets(:normal_user_closed_change).id)
220 delete :delete, :id => current_nodes(:visible_node).id
221 assert_response :conflict
223 # try to delete with an invalid (non-existent) changeset
224 content update_changeset(current_nodes(:visible_node).to_xml,0)
225 delete :delete, :id => current_nodes(:visible_node).id
226 assert_response :conflict
228 # try to delete a node with a different ID
229 content(nodes(:public_visible_node).to_xml)
230 delete :delete, :id => current_nodes(:visible_node).id
231 assert_response :bad_request,
232 "should not be able to delete a node with a different ID from the XML"
234 # try to delete a node rubbish in the payloads
236 delete :delete, :id => current_nodes(:visible_node).id
237 assert_response :bad_request,
238 "should not be able to delete a node without a valid XML payload"
240 # valid delete now takes a payload
241 content(nodes(:public_visible_node).to_xml)
242 delete :delete, :id => current_nodes(:public_visible_node).id
243 assert_response :success
245 # valid delete should return the new version number, which should
246 # be greater than the old version number
247 assert @response.body.to_i > current_nodes(:public_visible_node).version,
248 "delete request should return a new version number for node"
250 # deleting the same node twice doesn't work
251 content(nodes(:public_visible_node).to_xml)
252 delete :delete, :id => current_nodes(:public_visible_node).id
253 assert_response :gone
255 # this won't work since the node never existed
256 delete :delete, :id => 0
257 assert_response :not_found
259 ## these test whether nodes which are in-use can be deleted:
261 content(nodes(:used_node_1).to_xml)
262 delete :delete, :id => current_nodes(:used_node_1).id
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 3 is still used by ways 1,3.", @response.body
268 content(nodes(:node_used_by_relationship).to_xml)
269 delete :delete, :id => current_nodes(:node_used_by_relationship).id
270 assert_response :precondition_failed,
271 "shouldn't be able to delete a node used in a relation (#{@response.body})"
272 assert_equal "Precondition failed: Node 5 is still used by relations 1,3.", @response.body
276 # tests whether the API works and prevents incorrect use while trying
279 ## First test with no user credentials
280 # try and update a node without authorisation
281 # first try to delete node without auth
282 content current_nodes(:visible_node).to_xml
283 put :update, :id => current_nodes(:visible_node).id
284 assert_response :unauthorized
288 ## Second test with the private user
291 basic_authorization(users(:normal_user).email, "test")
293 ## trying to break changesets
295 # try and update in someone else's changeset
296 content update_changeset(current_nodes(:visible_node).to_xml,
297 changesets(:public_user_first_change).id)
298 put :update, :id => current_nodes(:visible_node).id
299 assert_require_public_data "update with other user's changeset should be forbidden when date isn't public"
301 # try and update in a closed changeset
302 content update_changeset(current_nodes(:visible_node).to_xml,
303 changesets(:normal_user_closed_change).id)
304 put :update, :id => current_nodes(:visible_node).id
305 assert_require_public_data "update with closed changeset should be forbidden, when data isn't public"
307 # try and update in a non-existant changeset
308 content update_changeset(current_nodes(:visible_node).to_xml, 0)
309 put :update, :id => current_nodes(:visible_node).id
310 assert_require_public_data("update with changeset=0 should be forbidden, when data isn't public")
312 ## try and submit invalid updates
313 content xml_attr_rewrite(current_nodes(:visible_node).to_xml, 'lat', 91.0);
314 put :update, :id => current_nodes(:visible_node).id
315 assert_require_public_data "node at lat=91 should be forbidden, when data isn't public"
317 content xml_attr_rewrite(current_nodes(:visible_node).to_xml, 'lat', -91.0);
318 put :update, :id => current_nodes(:visible_node).id
319 assert_require_public_data "node at lat=-91 should be forbidden, when data isn't public"
321 content xml_attr_rewrite(current_nodes(:visible_node).to_xml, 'lon', 181.0);
322 put :update, :id => current_nodes(:visible_node).id
323 assert_require_public_data "node at lon=181 should be forbidden, when data isn't public"
325 content xml_attr_rewrite(current_nodes(:visible_node).to_xml, 'lon', -181.0);
326 put :update, :id => current_nodes(:visible_node).id
327 assert_require_public_data "node at lon=-181 should be forbidden, when data isn't public"
329 ## finally, produce a good request which should work
330 content current_nodes(:visible_node).to_xml
331 put :update, :id => current_nodes(:visible_node).id
332 assert_require_public_data "should have failed with a forbidden when data isn't public"
334 ## Finally test with the public user
336 # try and update a node without authorisation
337 # first try to delete node without auth
338 content current_nodes(:visible_node).to_xml
339 put :update, :id => current_nodes(:visible_node).id
340 assert_response :forbidden
343 basic_authorization(users(:public_user).email, "test")
345 ## trying to break changesets
347 # try and update in someone else's changeset
348 content update_changeset(current_nodes(:visible_node).to_xml,
349 changesets(:normal_user_first_change).id)
350 put :update, :id => current_nodes(:visible_node).id
351 assert_response :conflict, "update with other user's changeset should be rejected"
353 # try and update in a closed changeset
354 content update_changeset(current_nodes(:visible_node).to_xml,
355 changesets(:normal_user_closed_change).id)
356 put :update, :id => current_nodes(:visible_node).id
357 assert_response :conflict, "update with closed changeset should be rejected"
359 # try and update in a non-existant changeset
360 content update_changeset(current_nodes(:visible_node).to_xml, 0)
361 put :update, :id => current_nodes(:visible_node).id
362 assert_response :conflict, "update with changeset=0 should be rejected"
364 ## try and submit invalid updates
365 content xml_attr_rewrite(current_nodes(:visible_node).to_xml, 'lat', 91.0);
366 put :update, :id => current_nodes(:visible_node).id
367 assert_response :bad_request, "node at lat=91 should be rejected"
369 content xml_attr_rewrite(current_nodes(:visible_node).to_xml, 'lat', -91.0);
370 put :update, :id => current_nodes(:visible_node).id
371 assert_response :bad_request, "node at lat=-91 should be rejected"
373 content xml_attr_rewrite(current_nodes(:visible_node).to_xml, 'lon', 181.0);
374 put :update, :id => current_nodes(:visible_node).id
375 assert_response :bad_request, "node at lon=181 should be rejected"
377 content xml_attr_rewrite(current_nodes(:visible_node).to_xml, 'lon', -181.0);
378 put :update, :id => current_nodes(:visible_node).id
379 assert_response :bad_request, "node at lon=-181 should be rejected"
381 ## next, attack the versioning
382 current_node_version = current_nodes(:visible_node).version
384 # try and submit a version behind
385 content xml_attr_rewrite(current_nodes(:visible_node).to_xml,
386 'version', current_node_version - 1);
387 put :update, :id => current_nodes(:visible_node).id
388 assert_response :conflict, "should have failed on old version number"
390 # try and submit a version ahead
391 content xml_attr_rewrite(current_nodes(:visible_node).to_xml,
392 'version', current_node_version + 1);
393 put :update, :id => current_nodes(:visible_node).id
394 assert_response :conflict, "should have failed on skipped version number"
396 # try and submit total crap in the version field
397 content xml_attr_rewrite(current_nodes(:visible_node).to_xml,
398 'version', 'p1r4t3s!');
399 put :update, :id => current_nodes(:visible_node).id
400 assert_response :conflict,
401 "should not be able to put 'p1r4at3s!' in the version field"
403 ## try an update with the wrong ID
404 content current_nodes(:public_visible_node).to_xml
405 put :update, :id => current_nodes(:visible_node).id
406 assert_response :bad_request,
407 "should not be able to update a node with a different ID from the XML"
409 ## try an update with a minimal valid XML doc which isn't a well-formed OSM doc.
411 put :update, :id => current_nodes(:visible_node).id
412 assert_response :bad_request,
413 "should not be able to update a node with non-OSM XML doc."
415 ## finally, produce a good request which should work
416 content current_nodes(:public_visible_node).to_xml
417 put :update, :id => current_nodes(:public_visible_node).id
418 assert_response :success, "a valid update request failed"
422 # test fetching multiple nodes
424 # check error when no parameter provided
426 assert_response :bad_request
428 # check error when no parameter value provided
429 get :nodes, :nodes => ""
430 assert_response :bad_request
432 # test a working call
433 get :nodes, :nodes => "1,2,4,15,17"
434 assert_response :success
435 assert_select "osm" do
436 assert_select "node", :count => 5
437 assert_select "node[id=1][visible=true]", :count => 1
438 assert_select "node[id=2][visible=false]", :count => 1
439 assert_select "node[id=4][visible=true]", :count => 1
440 assert_select "node[id=15][visible=true]", :count => 1
441 assert_select "node[id=17][visible=false]", :count => 1
444 # check error when a non-existent node is included
445 get :nodes, :nodes => "1,2,4,15,17,400"
446 assert_response :not_found
450 # test adding tags to a node
451 def test_duplicate_tags
453 basic_authorization(users(:public_user).email, "test")
455 # add an identical tag to the node
456 tag_xml = XML::Node.new("tag")
457 tag_xml['k'] = current_node_tags(:public_v_t1).k
458 tag_xml['v'] = current_node_tags(:public_v_t1).v
460 # add the tag into the existing xml
461 node_xml = current_nodes(:public_visible_node).to_xml
462 node_xml.find("//osm/node").first << tag_xml
466 put :update, :id => current_nodes(:public_visible_node).id
467 assert_response :bad_request,
468 "adding duplicate tags to a node should fail with 'bad request'"
469 assert_equal "Element node/#{current_nodes(:public_visible_node).id} has duplicate tags with key #{current_node_tags(:t1).k}", @response.body
472 # test whether string injection is possible
473 def test_string_injection
474 ## First try with the non-data public user
475 basic_authorization(users(:normal_user).email, "test")
476 changeset_id = changesets(:normal_user_first_change).id
478 # try and put something into a string that the API might
479 # use unquoted and therefore allow code injection...
480 content "<osm><node lat='0' lon='0' changeset='#{changeset_id}'>" +
481 '<tag k="#{@user.inspect}" v="0"/>' +
484 assert_require_public_data "Shouldn't be able to create with non-public user"
487 ## Then try with the public data user
488 basic_authorization(users(:public_user).email, "test")
489 changeset_id = changesets(:public_user_first_change).id
491 # try and put something into a string that the API might
492 # use unquoted and therefore allow code injection...
493 content "<osm><node lat='0' lon='0' changeset='#{changeset_id}'>" +
494 '<tag k="#{@user.inspect}" v="0"/>' +
497 assert_response :success
498 nodeid = @response.body
500 # find the node in the database
501 checknode = Node.find(nodeid)
502 assert_not_nil checknode, "node not found in data base after upload"
504 # and grab it using the api
505 get :read, :id => nodeid
506 assert_response :success
507 apinode = Node.from_xml(@response.body)
508 assert_not_nil apinode, "downloaded node is nil, but shouldn't be"
510 # check the tags are not corrupted
511 assert_equal checknode.tags, apinode.tags
512 assert apinode.tags.include?('#{@user.inspect}')
515 def basic_authorization(user, pass)
516 @request.env["HTTP_AUTHORIZATION"] = "Basic %s" % Base64.encode64("#{user}:#{pass}")
520 @request.env["RAW_POST_DATA"] = c.to_s
524 # update the changeset_id of a node element
525 def update_changeset(xml, changeset_id)
526 xml_attr_rewrite(xml, 'changeset', changeset_id)
530 # update an attribute in the node element
531 def xml_attr_rewrite(xml, name, value)
532 xml.find("//osm/node").first[name] = value.to_s
539 parser = XML::Parser.string(xml)