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 => create(:node).id
147 assert_response :success
149 # check that an deleted node is not returned
150 get :read, :id => create(:node, :deleted).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 private_user = create(:user, :data_public => false)
162 private_user_changeset = create(:changeset, :user => private_user)
163 private_user_closed_changeset = create(:changeset, :closed, :user => private_user)
164 private_node = create(:node, :changeset => private_user_changeset)
165 private_deleted_node = create(:node, :deleted, :changeset => private_user_changeset)
167 ## first try to delete node without auth
168 delete :delete, :id => private_node.id
169 assert_response :unauthorized
171 ## now set auth for the non-data public user
172 basic_authorization(private_user.email, "test")
174 # try to delete with an invalid (closed) changeset
175 content update_changeset(private_node.to_xml, private_user_closed_changeset.id)
176 delete :delete, :id => private_node.id
177 assert_require_public_data("non-public user shouldn't be able to delete node")
179 # try to delete with an invalid (non-existent) changeset
180 content update_changeset(private_node.to_xml, 0)
181 delete :delete, :id => private_node.id
182 assert_require_public_data("shouldn't be able to delete node, when user's data is private")
184 # valid delete now takes a payload
185 content(private_node.to_xml)
186 delete :delete, :id => private_node.id
187 assert_require_public_data("shouldn't be able to delete node when user's data isn't public'")
189 # this won't work since the node is already deleted
190 content(private_deleted_node.to_xml)
191 delete :delete, :id => private_deleted_node.id
192 assert_require_public_data
194 # this won't work since the node never existed
195 delete :delete, :id => 0
196 assert_require_public_data
198 ## these test whether nodes which are in-use can be deleted:
200 private_used_node = create(:node, :changeset => private_user_changeset)
201 create(:way_node, :node => private_used_node)
203 content(private_used_node.to_xml)
204 delete :delete, :id => private_used_node.id
205 assert_require_public_data "shouldn't be able to delete a node used in a way (#{@response.body})"
208 private_used_node2 = create(:node, :changeset => private_user_changeset)
209 create(:relation_member, :member => private_used_node2)
211 content(private_used_node2.to_xml)
212 delete :delete, :id => private_used_node2.id
213 assert_require_public_data "shouldn't be able to delete a node used in a relation (#{@response.body})"
215 ## now setup for the public data user
216 user = create(:user, :data_public => true)
217 changeset = create(:changeset, :user => user)
218 closed_changeset = create(:changeset, :closed, :user => user)
219 node = create(:node, :changeset => changeset)
220 basic_authorization(user.email, "test")
222 # try to delete with an invalid (closed) changeset
223 content update_changeset(node.to_xml, closed_changeset.id)
224 delete :delete, :id => node.id
225 assert_response :conflict
227 # try to delete with an invalid (non-existent) changeset
228 content update_changeset(node.to_xml, 0)
229 delete :delete, :id => node.id
230 assert_response :conflict
232 # try to delete a node with a different ID
233 other_node = create(:node)
234 content(other_node.to_xml)
235 delete :delete, :id => node.id
236 assert_response :bad_request,
237 "should not be able to delete a node with a different ID from the XML"
239 # try to delete a node rubbish in the payloads
241 delete :delete, :id => node.id
242 assert_response :bad_request,
243 "should not be able to delete a node without a valid XML payload"
245 # valid delete now takes a payload
247 delete :delete, :id => node.id
248 assert_response :success
250 # valid delete should return the new version number, which should
251 # be greater than the old version number
252 assert @response.body.to_i > node.version,
253 "delete request should return a new version number for node"
255 # deleting the same node twice doesn't work
257 delete :delete, :id => node.id
258 assert_response :gone
260 # this won't work since the node never existed
261 delete :delete, :id => 0
262 assert_response :not_found
264 ## these test whether nodes which are in-use can be deleted:
266 used_node = create(:node, :changeset => create(:changeset, :user => user))
267 way_node = create(:way_node, :node => used_node)
268 way_node2 = create(:way_node, :node => used_node)
270 content(used_node.to_xml)
271 delete :delete, :id => used_node.id
272 assert_response :precondition_failed,
273 "shouldn't be able to delete a node used in a way (#{@response.body})"
274 assert_equal "Precondition failed: Node #{used_node.id} is still used by ways #{way_node.way.id},#{way_node2.way.id}.", @response.body
277 used_node2 = create(:node, :changeset => create(:changeset, :user => user))
278 relation_member = create(:relation_member, :member => used_node2)
279 relation_member2 = create(:relation_member, :member => used_node2)
281 content(used_node2.to_xml)
282 delete :delete, :id => used_node2.id
283 assert_response :precondition_failed,
284 "shouldn't be able to delete a node used in a relation (#{@response.body})"
285 assert_equal "Precondition failed: Node #{used_node2.id} is still used by relations #{relation_member.relation.id},#{relation_member2.relation.id}.", @response.body
289 # tests whether the API works and prevents incorrect use while trying
292 ## First test with no user credentials
293 # try and update a node without authorisation
294 # first try to delete node without auth
295 content current_nodes(:visible_node).to_xml
296 put :update, :id => current_nodes(:visible_node).id
297 assert_response :unauthorized
299 ## Second test with the private user
302 basic_authorization(users(:normal_user).email, "test")
304 ## trying to break changesets
306 # try and update in someone else's changeset
307 content update_changeset(current_nodes(:visible_node).to_xml,
308 changesets(:public_user_first_change).id)
309 put :update, :id => current_nodes(:visible_node).id
310 assert_require_public_data "update with other user's changeset should be forbidden when date isn't public"
312 # try and update in a closed changeset
313 content update_changeset(current_nodes(:visible_node).to_xml,
314 changesets(:normal_user_closed_change).id)
315 put :update, :id => current_nodes(:visible_node).id
316 assert_require_public_data "update with closed changeset should be forbidden, when data isn't public"
318 # try and update in a non-existant changeset
319 content update_changeset(current_nodes(:visible_node).to_xml, 0)
320 put :update, :id => current_nodes(:visible_node).id
321 assert_require_public_data("update with changeset=0 should be forbidden, when data isn't public")
323 ## try and submit invalid updates
324 content xml_attr_rewrite(current_nodes(:visible_node).to_xml, "lat", 91.0)
325 put :update, :id => current_nodes(:visible_node).id
326 assert_require_public_data "node at lat=91 should be forbidden, when data isn't public"
328 content xml_attr_rewrite(current_nodes(:visible_node).to_xml, "lat", -91.0)
329 put :update, :id => current_nodes(:visible_node).id
330 assert_require_public_data "node at lat=-91 should be forbidden, when data isn't public"
332 content xml_attr_rewrite(current_nodes(:visible_node).to_xml, "lon", 181.0)
333 put :update, :id => current_nodes(:visible_node).id
334 assert_require_public_data "node at lon=181 should be forbidden, when data isn't public"
336 content xml_attr_rewrite(current_nodes(:visible_node).to_xml, "lon", -181.0)
337 put :update, :id => current_nodes(:visible_node).id
338 assert_require_public_data "node at lon=-181 should be forbidden, when data isn't public"
340 ## finally, produce a good request which should work
341 content current_nodes(:visible_node).to_xml
342 put :update, :id => current_nodes(:visible_node).id
343 assert_require_public_data "should have failed with a forbidden when data isn't public"
345 ## Finally test with the public user
347 # try and update a node without authorisation
348 # first try to delete node without auth
349 content current_nodes(:visible_node).to_xml
350 put :update, :id => current_nodes(:visible_node).id
351 assert_response :forbidden
354 basic_authorization(users(:public_user).email, "test")
356 ## trying to break changesets
358 # try and update in someone else's changeset
359 content update_changeset(current_nodes(:visible_node).to_xml,
360 changesets(:normal_user_first_change).id)
361 put :update, :id => current_nodes(:visible_node).id
362 assert_response :conflict, "update with other user's changeset should be rejected"
364 # try and update in a closed changeset
365 content update_changeset(current_nodes(:visible_node).to_xml,
366 changesets(:normal_user_closed_change).id)
367 put :update, :id => current_nodes(:visible_node).id
368 assert_response :conflict, "update with closed changeset should be rejected"
370 # try and update in a non-existant changeset
371 content update_changeset(current_nodes(:visible_node).to_xml, 0)
372 put :update, :id => current_nodes(:visible_node).id
373 assert_response :conflict, "update with changeset=0 should be rejected"
375 ## try and submit invalid updates
376 content xml_attr_rewrite(current_nodes(:visible_node).to_xml, "lat", 91.0)
377 put :update, :id => current_nodes(:visible_node).id
378 assert_response :bad_request, "node at lat=91 should be rejected"
380 content xml_attr_rewrite(current_nodes(:visible_node).to_xml, "lat", -91.0)
381 put :update, :id => current_nodes(:visible_node).id
382 assert_response :bad_request, "node at lat=-91 should be rejected"
384 content xml_attr_rewrite(current_nodes(:visible_node).to_xml, "lon", 181.0)
385 put :update, :id => current_nodes(:visible_node).id
386 assert_response :bad_request, "node at lon=181 should be rejected"
388 content xml_attr_rewrite(current_nodes(:visible_node).to_xml, "lon", -181.0)
389 put :update, :id => current_nodes(:visible_node).id
390 assert_response :bad_request, "node at lon=-181 should be rejected"
392 ## next, attack the versioning
393 current_node_version = current_nodes(:visible_node).version
395 # try and submit a version behind
396 content xml_attr_rewrite(current_nodes(:visible_node).to_xml,
397 "version", current_node_version - 1)
398 put :update, :id => current_nodes(:visible_node).id
399 assert_response :conflict, "should have failed on old version number"
401 # try and submit a version ahead
402 content xml_attr_rewrite(current_nodes(:visible_node).to_xml,
403 "version", current_node_version + 1)
404 put :update, :id => current_nodes(:visible_node).id
405 assert_response :conflict, "should have failed on skipped version number"
407 # try and submit total crap in the version field
408 content xml_attr_rewrite(current_nodes(:visible_node).to_xml,
409 "version", "p1r4t3s!")
410 put :update, :id => current_nodes(:visible_node).id
411 assert_response :conflict,
412 "should not be able to put 'p1r4at3s!' in the version field"
414 ## try an update with the wrong ID
415 content current_nodes(:public_visible_node).to_xml
416 put :update, :id => current_nodes(:visible_node).id
417 assert_response :bad_request,
418 "should not be able to update a node with a different ID from the XML"
420 ## try an update with a minimal valid XML doc which isn't a well-formed OSM doc.
422 put :update, :id => current_nodes(:visible_node).id
423 assert_response :bad_request,
424 "should not be able to update a node with non-OSM XML doc."
426 ## finally, produce a good request which should work
427 content current_nodes(:public_visible_node).to_xml
428 put :update, :id => current_nodes(:public_visible_node).id
429 assert_response :success, "a valid update request failed"
433 # test fetching multiple nodes
435 # check error when no parameter provided
437 assert_response :bad_request
439 # check error when no parameter value provided
440 get :nodes, :nodes => ""
441 assert_response :bad_request
443 # test a working call
444 get :nodes, :nodes => "1,2,4,15,17"
445 assert_response :success
446 assert_select "osm" do
447 assert_select "node", :count => 5
448 assert_select "node[id='1'][visible='true']", :count => 1
449 assert_select "node[id='2'][visible='false']", :count => 1
450 assert_select "node[id='4'][visible='true']", :count => 1
451 assert_select "node[id='15'][visible='true']", :count => 1
452 assert_select "node[id='17'][visible='false']", :count => 1
455 # check error when a non-existent node is included
456 get :nodes, :nodes => "1,2,4,15,17,400"
457 assert_response :not_found
461 # test adding tags to a node
462 def test_duplicate_tags
463 existing = create(:node_tag, :node => current_nodes(:public_visible_node))
465 basic_authorization(users(:public_user).email, "test")
467 # add an identical tag to the node
468 tag_xml = XML::Node.new("tag")
469 tag_xml["k"] = existing.k
470 tag_xml["v"] = existing.v
472 # add the tag into the existing xml
473 node_xml = current_nodes(:public_visible_node).to_xml
474 node_xml.find("//osm/node").first << tag_xml
478 put :update, :id => current_nodes(:public_visible_node).id
479 assert_response :bad_request,
480 "adding duplicate tags to a node should fail with 'bad request'"
481 assert_equal "Element node/#{current_nodes(:public_visible_node).id} has duplicate tags with key #{existing.k}", @response.body
484 # test whether string injection is possible
485 def test_string_injection
486 ## First try with the non-data public user
487 basic_authorization(users(:normal_user).email, "test")
488 changeset_id = changesets(:normal_user_first_change).id
490 # try and put something into a string that the API might
491 # use unquoted and therefore allow code injection...
492 content "<osm><node lat='0' lon='0' changeset='#{changeset_id}'>" +
493 '<tag k="#{@user.inspect}" v="0"/>' +
496 assert_require_public_data "Shouldn't be able to create with non-public user"
498 ## Then try with the public data user
499 basic_authorization(users(:public_user).email, "test")
500 changeset_id = changesets(:public_user_first_change).id
502 # try and put something into a string that the API might
503 # use unquoted and therefore allow code injection...
504 content "<osm><node lat='0' lon='0' changeset='#{changeset_id}'>" +
505 '<tag k="#{@user.inspect}" v="0"/>' +
508 assert_response :success
509 nodeid = @response.body
511 # find the node in the database
512 checknode = Node.find(nodeid)
513 assert_not_nil checknode, "node not found in data base after upload"
515 # and grab it using the api
516 get :read, :id => nodeid
517 assert_response :success
518 apinode = Node.from_xml(@response.body)
519 assert_not_nil apinode, "downloaded node is nil, but shouldn't be"
521 # check the tags are not corrupted
522 assert_equal checknode.tags, apinode.tags
523 assert apinode.tags.include?("\#{@user.inspect}")
527 # update the changeset_id of a node element
528 def update_changeset(xml, changeset_id)
529 xml_attr_rewrite(xml, "changeset", changeset_id)
533 # update an attribute in the node element
534 def xml_attr_rewrite(xml, name, value)
535 xml.find("//osm/node").first[name] = value.to_s
542 parser = XML::Parser.string(xml)