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"
48 ## Now try with the user which doesn't have their data public
49 basic_authorization(users(:normal_user).email, "test")
51 # create a node with random lat/lon
52 lat = rand(100) - 50 + rand
53 lon = rand(100) - 50 + rand
54 # normal user has a changeset open, so we'll use that.
55 changeset = changesets(:normal_user_first_change)
56 # create a minimal xml file
57 content("<osm><node lat='#{lat}' lon='#{lon}' changeset='#{changeset.id}'/></osm>")
58 assert_difference("Node.count", 0) do
62 assert_require_public_data "node create did not return forbidden status"
64 ## Now try with the user that has the public data
65 basic_authorization(users(:public_user).email, "test")
67 # create a node with random lat/lon
68 lat = rand(100) - 50 + rand
69 lon = rand(100) - 50 + rand
70 # normal user has a changeset open, so we'll use that.
71 changeset = changesets(:public_user_first_change)
72 # create a minimal xml file
73 content("<osm><node lat='#{lat}' lon='#{lon}' changeset='#{changeset.id}'/></osm>")
76 assert_response :success, "node upload did not return success status"
78 # read id of created node and search for it
79 nodeid = @response.body
80 checknode = Node.find(nodeid)
81 assert_not_nil checknode, "uploaded node not found in data base after upload"
83 assert_in_delta lat * 10000000, checknode.latitude, 1, "saved node does not match requested latitude"
84 assert_in_delta lon * 10000000, checknode.longitude, 1, "saved node does not match requested longitude"
85 assert_equal changesets(:public_user_first_change).id, checknode.changeset_id, "saved node does not belong to changeset that it was created in"
86 assert_equal true, checknode.visible, "saved node is not visible"
89 def test_create_invalid_xml
90 ## Only test public user here, as test_create should cover what's the forbiddens
91 ## that would occur here
93 basic_authorization(users(:public_user).email, "test")
94 # normal user has a changeset open, so we'll use that.
95 changeset = changesets(:public_user_first_change)
99 # test that the upload is rejected when xml is valid, but osm doc isn't
102 assert_response :bad_request, "node upload did not return bad_request status"
103 assert_equal "Cannot parse valid node from xml string <create/>. XML doesn't contain an osm/node element.", @response.body
105 # test that the upload is rejected when no lat is supplied
106 # create a minimal xml file
107 content("<osm><node lon='#{lon}' changeset='#{changeset.id}'/></osm>")
110 assert_response :bad_request, "node upload did not return bad_request status"
111 assert_equal "Cannot parse valid node from xml string <node lon=\"3.23\" changeset=\"#{changeset.id}\"/>. lat missing", @response.body
113 # test that the upload is rejected when no lon is supplied
114 # create a minimal xml file
115 content("<osm><node lat='#{lat}' changeset='#{changeset.id}'/></osm>")
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=\"3.434\" changeset=\"#{changeset.id}\"/>. lon missing", @response.body
121 # test that the upload is rejected when lat is non-numeric
122 # create a minimal xml file
123 content("<osm><node lat='abc' lon='#{lon}' changeset='#{changeset.id}'/></osm>")
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=\"abc\" lon=\"#{lon}\" changeset=\"#{changeset.id}\"/>. lat not a number", @response.body
129 # test that the upload is rejected when lon is non-numeric
130 # create a minimal xml file
131 content("<osm><node lat='#{lat}' lon='abc' changeset='#{changeset.id}'/></osm>")
134 assert_response :bad_request, "node upload did not return bad_request status"
135 assert_equal "Cannot parse valid node from xml string <node lat=\"#{lat}\" lon=\"abc\" changeset=\"#{changeset.id}\"/>. lon not a number", @response.body
137 # test that the upload is rejected when we have a tag which is too long
138 content("<osm><node lat='#{lat}' lon='#{lon}' changeset='#{changeset.id}'><tag k='foo' v='#{'x' * 256}'/></node></osm>")
140 assert_response :bad_request, "node upload did not return bad_request status"
141 assert_equal ["NodeTag ", " v: is too long (maximum is 255 characters) (\"#{'x' * 256}\")"], @response.body.split(/[0-9]+,foo:/)
145 # check that a visible node is returned properly
146 get :read, :id => current_nodes(:visible_node).id
147 assert_response :success
149 # check that an invisible node is not returned
150 get :read, :id => current_nodes(:invisible_node).id
151 assert_response :gone
153 # check chat a non-existent node is not returned
155 assert_response :not_found
158 # this tests deletion restrictions - basic deletion is tested in the unit
161 ## first try to delete node without auth
162 delete :delete, :id => current_nodes(:visible_node).id
163 assert_response :unauthorized
165 ## now set auth for the non-data public user
166 basic_authorization(users(:normal_user).email, "test")
168 # try to delete with an invalid (closed) changeset
169 content update_changeset(current_nodes(:visible_node).to_xml,
170 changesets(:normal_user_closed_change).id)
171 delete :delete, :id => current_nodes(:visible_node).id
172 assert_require_public_data("non-public user shouldn't be able to delete node")
174 # try to delete with an invalid (non-existent) changeset
175 content update_changeset(current_nodes(:visible_node).to_xml, 0)
176 delete :delete, :id => current_nodes(:visible_node).id
177 assert_require_public_data("shouldn't be able to delete node, when user's data is private")
179 # valid delete now takes a payload
180 content(nodes(:visible_node).to_xml)
181 delete :delete, :id => current_nodes(:visible_node).id
182 assert_require_public_data("shouldn't be able to delete node when user's data isn't public'")
184 # this won't work since the node is already deleted
185 content(nodes(:invisible_node).to_xml)
186 delete :delete, :id => current_nodes(:invisible_node).id
187 assert_require_public_data
189 # this won't work since the node never existed
190 delete :delete, :id => 0
191 assert_require_public_data
193 ## these test whether nodes which are in-use can be deleted:
195 content(nodes(:used_node_1).to_xml)
196 delete :delete, :id => current_nodes(:used_node_1).id
197 assert_require_public_data "shouldn't be able to delete a node used in a way (#{@response.body})"
200 content(nodes(:node_used_by_relationship).to_xml)
201 delete :delete, :id => current_nodes(:node_used_by_relationship).id
202 assert_require_public_data "shouldn't be able to delete a node used in a relation (#{@response.body})"
204 ## now set auth for the public data user
205 basic_authorization(users(:public_user).email, "test")
207 # try to delete with an invalid (closed) changeset
208 content update_changeset(current_nodes(:visible_node).to_xml,
209 changesets(:normal_user_closed_change).id)
210 delete :delete, :id => current_nodes(:visible_node).id
211 assert_response :conflict
213 # try to delete with an invalid (non-existent) changeset
214 content update_changeset(current_nodes(:visible_node).to_xml, 0)
215 delete :delete, :id => current_nodes(:visible_node).id
216 assert_response :conflict
218 # try to delete a node with a different ID
219 content(nodes(:public_visible_node).to_xml)
220 delete :delete, :id => current_nodes(:visible_node).id
221 assert_response :bad_request,
222 "should not be able to delete a node with a different ID from the XML"
224 # try to delete a node rubbish in the payloads
226 delete :delete, :id => current_nodes(:visible_node).id
227 assert_response :bad_request,
228 "should not be able to delete a node without a valid XML payload"
230 # valid delete now takes a payload
231 content(nodes(:public_visible_node).to_xml)
232 delete :delete, :id => current_nodes(:public_visible_node).id
233 assert_response :success
235 # valid delete should return the new version number, which should
236 # be greater than the old version number
237 assert @response.body.to_i > current_nodes(:public_visible_node).version,
238 "delete request should return a new version number for node"
240 # deleting the same node twice doesn't work
241 content(nodes(:public_visible_node).to_xml)
242 delete :delete, :id => current_nodes(:public_visible_node).id
243 assert_response :gone
245 # this won't work since the node never existed
246 delete :delete, :id => 0
247 assert_response :not_found
249 ## these test whether nodes which are in-use can be deleted:
251 content(nodes(:used_node_1).to_xml)
252 delete :delete, :id => current_nodes(:used_node_1).id
253 assert_response :precondition_failed,
254 "shouldn't be able to delete a node used in a way (#{@response.body})"
255 assert_equal "Precondition failed: Node 3 is still used by ways 1,3.", @response.body
258 content(nodes(:node_used_by_relationship).to_xml)
259 delete :delete, :id => current_nodes(:node_used_by_relationship).id
260 assert_response :precondition_failed,
261 "shouldn't be able to delete a node used in a relation (#{@response.body})"
262 assert_equal "Precondition failed: Node 5 is still used by relations 1,3.", @response.body
266 # tests whether the API works and prevents incorrect use while trying
269 ## First test with no user credentials
270 # try and update a node without authorisation
271 # first try to delete node without auth
272 content current_nodes(:visible_node).to_xml
273 put :update, :id => current_nodes(:visible_node).id
274 assert_response :unauthorized
276 ## Second test with the private user
279 basic_authorization(users(:normal_user).email, "test")
281 ## trying to break changesets
283 # try and update in someone else's changeset
284 content update_changeset(current_nodes(:visible_node).to_xml,
285 changesets(:public_user_first_change).id)
286 put :update, :id => current_nodes(:visible_node).id
287 assert_require_public_data "update with other user's changeset should be forbidden when date isn't public"
289 # try and update in a closed changeset
290 content update_changeset(current_nodes(:visible_node).to_xml,
291 changesets(:normal_user_closed_change).id)
292 put :update, :id => current_nodes(:visible_node).id
293 assert_require_public_data "update with closed changeset should be forbidden, when data isn't public"
295 # try and update in a non-existant changeset
296 content update_changeset(current_nodes(:visible_node).to_xml, 0)
297 put :update, :id => current_nodes(:visible_node).id
298 assert_require_public_data("update with changeset=0 should be forbidden, when data isn't public")
300 ## try and submit invalid updates
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, "lat", -91.0)
306 put :update, :id => current_nodes(:visible_node).id
307 assert_require_public_data "node at lat=-91 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 content xml_attr_rewrite(current_nodes(:visible_node).to_xml, "lon", -181.0)
314 put :update, :id => current_nodes(:visible_node).id
315 assert_require_public_data "node at lon=-181 should be forbidden, when data isn't public"
317 ## finally, produce a good request which should work
318 content current_nodes(:visible_node).to_xml
319 put :update, :id => current_nodes(:visible_node).id
320 assert_require_public_data "should have failed with a forbidden when data isn't public"
322 ## Finally test with the public user
324 # try and update a node without authorisation
325 # first try to delete node without auth
326 content current_nodes(:visible_node).to_xml
327 put :update, :id => current_nodes(:visible_node).id
328 assert_response :forbidden
331 basic_authorization(users(:public_user).email, "test")
333 ## trying to break changesets
335 # try and update in someone else's changeset
336 content update_changeset(current_nodes(:visible_node).to_xml,
337 changesets(:normal_user_first_change).id)
338 put :update, :id => current_nodes(:visible_node).id
339 assert_response :conflict, "update with other user's changeset should be rejected"
341 # try and update in a closed changeset
342 content update_changeset(current_nodes(:visible_node).to_xml,
343 changesets(:normal_user_closed_change).id)
344 put :update, :id => current_nodes(:visible_node).id
345 assert_response :conflict, "update with closed changeset should be rejected"
347 # try and update in a non-existant changeset
348 content update_changeset(current_nodes(:visible_node).to_xml, 0)
349 put :update, :id => current_nodes(:visible_node).id
350 assert_response :conflict, "update with changeset=0 should be rejected"
352 ## try and submit invalid updates
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, "lat", -91.0)
358 put :update, :id => current_nodes(:visible_node).id
359 assert_response :bad_request, "node at lat=-91 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 content xml_attr_rewrite(current_nodes(:visible_node).to_xml, "lon", -181.0)
366 put :update, :id => current_nodes(:visible_node).id
367 assert_response :bad_request, "node at lon=-181 should be rejected"
369 ## next, attack the versioning
370 current_node_version = current_nodes(:visible_node).version
372 # try and submit a version behind
373 content xml_attr_rewrite(current_nodes(:visible_node).to_xml,
374 "version", current_node_version - 1)
375 put :update, :id => current_nodes(:visible_node).id
376 assert_response :conflict, "should have failed on old version number"
378 # try and submit a version ahead
379 content xml_attr_rewrite(current_nodes(:visible_node).to_xml,
380 "version", current_node_version + 1)
381 put :update, :id => current_nodes(:visible_node).id
382 assert_response :conflict, "should have failed on skipped version number"
384 # try and submit total crap in the version field
385 content xml_attr_rewrite(current_nodes(:visible_node).to_xml,
386 "version", "p1r4t3s!")
387 put :update, :id => current_nodes(:visible_node).id
388 assert_response :conflict,
389 "should not be able to put 'p1r4at3s!' in the version field"
391 ## try an update with the wrong ID
392 content current_nodes(:public_visible_node).to_xml
393 put :update, :id => current_nodes(:visible_node).id
394 assert_response :bad_request,
395 "should not be able to update a node with a different ID from the XML"
397 ## try an update with a minimal valid XML doc which isn't a well-formed OSM doc.
399 put :update, :id => current_nodes(:visible_node).id
400 assert_response :bad_request,
401 "should not be able to update a node with non-OSM XML doc."
403 ## finally, produce a good request which should work
404 content current_nodes(:public_visible_node).to_xml
405 put :update, :id => current_nodes(:public_visible_node).id
406 assert_response :success, "a valid update request failed"
410 # test fetching multiple nodes
412 # check error when no parameter provided
414 assert_response :bad_request
416 # check error when no parameter value provided
417 get :nodes, :nodes => ""
418 assert_response :bad_request
420 # test a working call
421 get :nodes, :nodes => "1,2,4,15,17"
422 assert_response :success
423 assert_select "osm" do
424 assert_select "node", :count => 5
425 assert_select "node[id='1'][visible='true']", :count => 1
426 assert_select "node[id='2'][visible='false']", :count => 1
427 assert_select "node[id='4'][visible='true']", :count => 1
428 assert_select "node[id='15'][visible='true']", :count => 1
429 assert_select "node[id='17'][visible='false']", :count => 1
432 # check error when a non-existent node is included
433 get :nodes, :nodes => "1,2,4,15,17,400"
434 assert_response :not_found
438 # test adding tags to a node
439 def test_duplicate_tags
440 existing = create(:node_tag, :node => current_nodes(:public_visible_node))
442 basic_authorization(users(:public_user).email, "test")
444 # add an identical tag to the node
445 tag_xml = XML::Node.new("tag")
446 tag_xml["k"] = existing.k
447 tag_xml["v"] = existing.v
449 # add the tag into the existing xml
450 node_xml = current_nodes(:public_visible_node).to_xml
451 node_xml.find("//osm/node").first << tag_xml
455 put :update, :id => current_nodes(:public_visible_node).id
456 assert_response :bad_request,
457 "adding duplicate tags to a node should fail with 'bad request'"
458 assert_equal "Element node/#{current_nodes(:public_visible_node).id} has duplicate tags with key #{existing.k}", @response.body
461 # test whether string injection is possible
462 def test_string_injection
463 ## First try with the non-data public user
464 basic_authorization(users(:normal_user).email, "test")
465 changeset_id = changesets(:normal_user_first_change).id
467 # try and put something into a string that the API might
468 # use unquoted and therefore allow code injection...
469 content "<osm><node lat='0' lon='0' changeset='#{changeset_id}'>" +
470 '<tag k="#{@user.inspect}" v="0"/>' +
473 assert_require_public_data "Shouldn't be able to create with non-public user"
475 ## Then try with the public data user
476 basic_authorization(users(:public_user).email, "test")
477 changeset_id = changesets(:public_user_first_change).id
479 # try and put something into a string that the API might
480 # use unquoted and therefore allow code injection...
481 content "<osm><node lat='0' lon='0' changeset='#{changeset_id}'>" +
482 '<tag k="#{@user.inspect}" v="0"/>' +
485 assert_response :success
486 nodeid = @response.body
488 # find the node in the database
489 checknode = Node.find(nodeid)
490 assert_not_nil checknode, "node not found in data base after upload"
492 # and grab it using the api
493 get :read, :id => nodeid
494 assert_response :success
495 apinode = Node.from_xml(@response.body)
496 assert_not_nil apinode, "downloaded node is nil, but shouldn't be"
498 # check the tags are not corrupted
499 assert_equal checknode.tags, apinode.tags
500 assert apinode.tags.include?("\#{@user.inspect}")
504 # update the changeset_id of a node element
505 def update_changeset(xml, changeset_id)
506 xml_attr_rewrite(xml, "changeset", changeset_id)
510 # update an attribute in the node element
511 def xml_attr_rewrite(xml, name, value)
512 xml.find("//osm/node").first[name] = value.to_s
519 parser = XML::Parser.string(xml)