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 we have a tag which is too long
126 content("<osm><node lat='#{lat}' lon='#{lon}' changeset='#{changeset.id}'><tag k='foo' v='#{'x'*256}'/></node></osm>")
128 assert_response :bad_request, "node upload did not return bad_request status"
129 assert_equal ["NodeTag ", " v: is too long (maximum is 255 characters) (\"#{'x'*256}\")"], @response.body.split(/[0-9]+,foo:/)
134 # check that a visible node is returned properly
135 get :read, :id => current_nodes(:visible_node).id
136 assert_response :success
138 # check that an invisible node is not returned
139 get :read, :id => current_nodes(:invisible_node).id
140 assert_response :gone
142 # check chat a non-existent node is not returned
144 assert_response :not_found
147 # this tests deletion restrictions - basic deletion is tested in the unit
150 ## first try to delete node without auth
151 delete :delete, :id => current_nodes(:visible_node).id
152 assert_response :unauthorized
155 ## now set auth for the non-data public user
156 basic_authorization(users(:normal_user).email, "test");
158 # try to delete with an invalid (closed) changeset
159 content update_changeset(current_nodes(:visible_node).to_xml,
160 changesets(:normal_user_closed_change).id)
161 delete :delete, :id => current_nodes(:visible_node).id
162 assert_require_public_data("non-public user shouldn't be able to delete node")
164 # try to delete with an invalid (non-existent) changeset
165 content update_changeset(current_nodes(:visible_node).to_xml,0)
166 delete :delete, :id => current_nodes(:visible_node).id
167 assert_require_public_data("shouldn't be able to delete node, when user's data is private")
169 # valid delete now takes a payload
170 content(nodes(:visible_node).to_xml)
171 delete :delete, :id => current_nodes(:visible_node).id
172 assert_require_public_data("shouldn't be able to delete node when user's data isn't public'")
174 # this won't work since the node is already deleted
175 content(nodes(:invisible_node).to_xml)
176 delete :delete, :id => current_nodes(:invisible_node).id
177 assert_require_public_data
179 # this won't work since the node never existed
180 delete :delete, :id => 0
181 assert_require_public_data
183 ## these test whether nodes which are in-use can be deleted:
185 content(nodes(:used_node_1).to_xml)
186 delete :delete, :id => current_nodes(:used_node_1).id
187 assert_require_public_data
188 "shouldn't be able to delete a node used in a way (#{@response.body})"
191 content(nodes(:node_used_by_relationship).to_xml)
192 delete :delete, :id => current_nodes(:node_used_by_relationship).id
193 assert_require_public_data
194 "shouldn't be able to delete a node used in a relation (#{@response.body})"
198 ## now set auth for the public data user
199 basic_authorization(users(:public_user).email, "test");
201 # try to delete with an invalid (closed) changeset
202 content update_changeset(current_nodes(:visible_node).to_xml,
203 changesets(:normal_user_closed_change).id)
204 delete :delete, :id => current_nodes(:visible_node).id
205 assert_response :conflict
207 # try to delete with an invalid (non-existent) changeset
208 content update_changeset(current_nodes(:visible_node).to_xml,0)
209 delete :delete, :id => current_nodes(:visible_node).id
210 assert_response :conflict
212 # try to delete a node with a different ID
213 content(nodes(:public_visible_node).to_xml)
214 delete :delete, :id => current_nodes(:visible_node).id
215 assert_response :bad_request,
216 "should not be able to delete a node with a different ID from the XML"
218 # try to delete a node rubbish in the payloads
220 delete :delete, :id => current_nodes(:visible_node).id
221 assert_response :bad_request,
222 "should not be able to delete a node without a valid XML payload"
224 # valid delete now takes a payload
225 content(nodes(:public_visible_node).to_xml)
226 delete :delete, :id => current_nodes(:public_visible_node).id
227 assert_response :success
229 # valid delete should return the new version number, which should
230 # be greater than the old version number
231 assert @response.body.to_i > current_nodes(:public_visible_node).version,
232 "delete request should return a new version number for node"
234 # deleting the same node twice doesn't work
235 content(nodes(:public_visible_node).to_xml)
236 delete :delete, :id => current_nodes(:public_visible_node).id
237 assert_response :gone
239 # this won't work since the node never existed
240 delete :delete, :id => 0
241 assert_response :not_found
243 ## these test whether nodes which are in-use can be deleted:
245 content(nodes(:used_node_1).to_xml)
246 delete :delete, :id => current_nodes(:used_node_1).id
247 assert_response :precondition_failed,
248 "shouldn't be able to delete a node used in a way (#{@response.body})"
249 assert_equal "Precondition failed: Node 3 is still used by ways 1,3.", @response.body
252 content(nodes(:node_used_by_relationship).to_xml)
253 delete :delete, :id => current_nodes(:node_used_by_relationship).id
254 assert_response :precondition_failed,
255 "shouldn't be able to delete a node used in a relation (#{@response.body})"
256 assert_equal "Precondition failed: Node 5 is still used by relations 1,3.", @response.body
260 # tests whether the API works and prevents incorrect use while trying
263 ## First test with no user credentials
264 # try and update a node without authorisation
265 # first try to delete node without auth
266 content current_nodes(:visible_node).to_xml
267 put :update, :id => current_nodes(:visible_node).id
268 assert_response :unauthorized
272 ## Second test with the private user
275 basic_authorization(users(:normal_user).email, "test")
277 ## trying to break changesets
279 # try and update in someone else's changeset
280 content update_changeset(current_nodes(:visible_node).to_xml,
281 changesets(:public_user_first_change).id)
282 put :update, :id => current_nodes(:visible_node).id
283 assert_require_public_data "update with other user's changeset should be forbidden when date isn't public"
285 # try and update in a closed changeset
286 content update_changeset(current_nodes(:visible_node).to_xml,
287 changesets(:normal_user_closed_change).id)
288 put :update, :id => current_nodes(:visible_node).id
289 assert_require_public_data "update with closed changeset should be forbidden, when data isn't public"
291 # try and update in a non-existant changeset
292 content update_changeset(current_nodes(:visible_node).to_xml, 0)
293 put :update, :id => current_nodes(:visible_node).id
294 assert_require_public_data("update with changeset=0 should be forbidden, when data isn't public")
296 ## try and submit invalid updates
297 content xml_attr_rewrite(current_nodes(:visible_node).to_xml, 'lat', 91.0);
298 put :update, :id => current_nodes(:visible_node).id
299 assert_require_public_data "node at lat=91 should be forbidden, when data isn't public"
301 content xml_attr_rewrite(current_nodes(:visible_node).to_xml, 'lat', -91.0);
302 put :update, :id => current_nodes(:visible_node).id
303 assert_require_public_data "node at lat=-91 should be forbidden, when data isn't public"
305 content xml_attr_rewrite(current_nodes(:visible_node).to_xml, 'lon', 181.0);
306 put :update, :id => current_nodes(:visible_node).id
307 assert_require_public_data "node at lon=181 should be forbidden, when data isn't public"
309 content xml_attr_rewrite(current_nodes(:visible_node).to_xml, 'lon', -181.0);
310 put :update, :id => current_nodes(:visible_node).id
311 assert_require_public_data "node at lon=-181 should be forbidden, when data isn't public"
313 ## finally, produce a good request which should work
314 content current_nodes(:visible_node).to_xml
315 put :update, :id => current_nodes(:visible_node).id
316 assert_require_public_data "should have failed with a forbidden when data isn't public"
318 ## Finally test with the public user
320 # try and update a node without authorisation
321 # first try to delete node without auth
322 content current_nodes(:visible_node).to_xml
323 put :update, :id => current_nodes(:visible_node).id
324 assert_response :forbidden
327 basic_authorization(users(:public_user).email, "test")
329 ## trying to break changesets
331 # try and update in someone else's changeset
332 content update_changeset(current_nodes(:visible_node).to_xml,
333 changesets(:normal_user_first_change).id)
334 put :update, :id => current_nodes(:visible_node).id
335 assert_response :conflict, "update with other user's changeset should be rejected"
337 # try and update in a closed changeset
338 content update_changeset(current_nodes(:visible_node).to_xml,
339 changesets(:normal_user_closed_change).id)
340 put :update, :id => current_nodes(:visible_node).id
341 assert_response :conflict, "update with closed changeset should be rejected"
343 # try and update in a non-existant changeset
344 content update_changeset(current_nodes(:visible_node).to_xml, 0)
345 put :update, :id => current_nodes(:visible_node).id
346 assert_response :conflict, "update with changeset=0 should be rejected"
348 ## try and submit invalid updates
349 content xml_attr_rewrite(current_nodes(:visible_node).to_xml, 'lat', 91.0);
350 put :update, :id => current_nodes(:visible_node).id
351 assert_response :bad_request, "node at lat=91 should be rejected"
353 content xml_attr_rewrite(current_nodes(:visible_node).to_xml, 'lat', -91.0);
354 put :update, :id => current_nodes(:visible_node).id
355 assert_response :bad_request, "node at lat=-91 should be rejected"
357 content xml_attr_rewrite(current_nodes(:visible_node).to_xml, 'lon', 181.0);
358 put :update, :id => current_nodes(:visible_node).id
359 assert_response :bad_request, "node at lon=181 should be rejected"
361 content xml_attr_rewrite(current_nodes(:visible_node).to_xml, 'lon', -181.0);
362 put :update, :id => current_nodes(:visible_node).id
363 assert_response :bad_request, "node at lon=-181 should be rejected"
365 ## next, attack the versioning
366 current_node_version = current_nodes(:visible_node).version
368 # try and submit a version behind
369 content xml_attr_rewrite(current_nodes(:visible_node).to_xml,
370 'version', current_node_version - 1);
371 put :update, :id => current_nodes(:visible_node).id
372 assert_response :conflict, "should have failed on old version number"
374 # try and submit a version ahead
375 content xml_attr_rewrite(current_nodes(:visible_node).to_xml,
376 'version', current_node_version + 1);
377 put :update, :id => current_nodes(:visible_node).id
378 assert_response :conflict, "should have failed on skipped version number"
380 # try and submit total crap in the version field
381 content xml_attr_rewrite(current_nodes(:visible_node).to_xml,
382 'version', 'p1r4t3s!');
383 put :update, :id => current_nodes(:visible_node).id
384 assert_response :conflict,
385 "should not be able to put 'p1r4at3s!' in the version field"
387 ## try an update with the wrong ID
388 content current_nodes(:public_visible_node).to_xml
389 put :update, :id => current_nodes(:visible_node).id
390 assert_response :bad_request,
391 "should not be able to update a node with a different ID from the XML"
393 ## try an update with a minimal valid XML doc which isn't a well-formed OSM doc.
395 put :update, :id => current_nodes(:visible_node).id
396 assert_response :bad_request,
397 "should not be able to update a node with non-OSM XML doc."
399 ## finally, produce a good request which should work
400 content current_nodes(:public_visible_node).to_xml
401 put :update, :id => current_nodes(:public_visible_node).id
402 assert_response :success, "a valid update request failed"
406 # test adding tags to a node
407 def test_duplicate_tags
409 basic_authorization(users(:public_user).email, "test")
411 # add an identical tag to the node
412 tag_xml = XML::Node.new("tag")
413 tag_xml['k'] = current_node_tags(:public_v_t1).k
414 tag_xml['v'] = current_node_tags(:public_v_t1).v
416 # add the tag into the existing xml
417 node_xml = current_nodes(:public_visible_node).to_xml
418 node_xml.find("//osm/node").first << tag_xml
422 put :update, :id => current_nodes(:public_visible_node).id
423 assert_response :bad_request,
424 "adding duplicate tags to a node should fail with 'bad request'"
425 assert_equal "Element node/#{current_nodes(:public_visible_node).id} has duplicate tags with key #{current_node_tags(:t1).k}", @response.body
428 # test whether string injection is possible
429 def test_string_injection
430 ## First try with the non-data public user
431 basic_authorization(users(:normal_user).email, "test")
432 changeset_id = changesets(:normal_user_first_change).id
434 # try and put something into a string that the API might
435 # use unquoted and therefore allow code injection...
436 content "<osm><node lat='0' lon='0' changeset='#{changeset_id}'>" +
437 '<tag k="#{@user.inspect}" v="0"/>' +
440 assert_require_public_data "Shouldn't be able to create with non-public user"
443 ## Then try with the public data user
444 basic_authorization(users(:public_user).email, "test")
445 changeset_id = changesets(:public_user_first_change).id
447 # try and put something into a string that the API might
448 # use unquoted and therefore allow code injection...
449 content "<osm><node lat='0' lon='0' changeset='#{changeset_id}'>" +
450 '<tag k="#{@user.inspect}" v="0"/>' +
453 assert_response :success
454 nodeid = @response.body
456 # find the node in the database
457 checknode = Node.find(nodeid)
458 assert_not_nil checknode, "node not found in data base after upload"
460 # and grab it using the api
461 get :read, :id => nodeid
462 assert_response :success
463 apinode = Node.from_xml(@response.body)
464 assert_not_nil apinode, "downloaded node is nil, but shouldn't be"
466 # check the tags are not corrupted
467 assert_equal checknode.tags, apinode.tags
468 assert apinode.tags.include?('#{@user.inspect}')
471 def basic_authorization(user, pass)
472 @request.env["HTTP_AUTHORIZATION"] = "Basic %s" % Base64.encode64("#{user}:#{pass}")
476 @request.env["RAW_POST_DATA"] = c.to_s
480 # update the changeset_id of a node element
481 def update_changeset(xml, changeset_id)
482 xml_attr_rewrite(xml, 'changeset', changeset_id)
486 # update an attribute in the node element
487 def xml_attr_rewrite(xml, name, value)
488 xml.find("//osm/node").first[name] = value.to_s
495 parser = XML::Parser.string(xml)