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 private_user = create(:user, :data_public => false)
296 private_node = create(:node, :changeset => create(:changeset, :user => private_user))
298 node = create(:node, :changeset => create(:changeset, :user => user))
301 put :update, :id => node.id
302 assert_response :unauthorized
304 ## Second test with the private user
307 basic_authorization(private_user.email, "test")
309 ## trying to break changesets
311 # try and update in someone else's changeset
312 content update_changeset(private_node.to_xml,
313 create(:changeset).id)
314 put :update, :id => private_node.id
315 assert_require_public_data "update with other user's changeset should be forbidden when data isn't public"
317 # try and update in a closed changeset
318 content update_changeset(private_node.to_xml,
319 create(:changeset, :closed, :user => private_user).id)
320 put :update, :id => private_node.id
321 assert_require_public_data "update with closed changeset should be forbidden, when data isn't public"
323 # try and update in a non-existant changeset
324 content update_changeset(private_node.to_xml, 0)
325 put :update, :id => private_node.id
326 assert_require_public_data "update with changeset=0 should be forbidden, when data isn't public"
328 ## try and submit invalid updates
329 content xml_attr_rewrite(private_node.to_xml, "lat", 91.0)
330 put :update, :id => private_node.id
331 assert_require_public_data "node at lat=91 should be forbidden, when data isn't public"
333 content xml_attr_rewrite(private_node.to_xml, "lat", -91.0)
334 put :update, :id => private_node.id
335 assert_require_public_data "node at lat=-91 should be forbidden, when data isn't public"
337 content xml_attr_rewrite(private_node.to_xml, "lon", 181.0)
338 put :update, :id => private_node.id
339 assert_require_public_data "node at lon=181 should be forbidden, when data isn't public"
341 content xml_attr_rewrite(private_node.to_xml, "lon", -181.0)
342 put :update, :id => private_node.id
343 assert_require_public_data "node at lon=-181 should be forbidden, when data isn't public"
345 ## finally, produce a good request which still won't work
346 content private_node.to_xml
347 put :update, :id => private_node.id
348 assert_require_public_data "should have failed with a forbidden when data isn't public"
350 ## Finally test with the public user
352 # try and update a node without authorisation
353 # first try to update node without auth
355 put :update, :id => node.id
356 assert_response :forbidden
359 basic_authorization(user.email, "test")
361 ## trying to break changesets
363 # try and update in someone else's changeset
364 content update_changeset(node.to_xml,
365 create(:changeset).id)
366 put :update, :id => node.id
367 assert_response :conflict, "update with other user's changeset should be rejected"
369 # try and update in a closed changeset
370 content update_changeset(node.to_xml,
371 create(:changeset, :closed, :user => user).id)
372 put :update, :id => node.id
373 assert_response :conflict, "update with closed changeset should be rejected"
375 # try and update in a non-existant changeset
376 content update_changeset(node.to_xml, 0)
377 put :update, :id => node.id
378 assert_response :conflict, "update with changeset=0 should be rejected"
380 ## try and submit invalid updates
381 content xml_attr_rewrite(node.to_xml, "lat", 91.0)
382 put :update, :id => node.id
383 assert_response :bad_request, "node at lat=91 should be rejected"
385 content xml_attr_rewrite(node.to_xml, "lat", -91.0)
386 put :update, :id => node.id
387 assert_response :bad_request, "node at lat=-91 should be rejected"
389 content xml_attr_rewrite(node.to_xml, "lon", 181.0)
390 put :update, :id => node.id
391 assert_response :bad_request, "node at lon=181 should be rejected"
393 content xml_attr_rewrite(node.to_xml, "lon", -181.0)
394 put :update, :id => node.id
395 assert_response :bad_request, "node at lon=-181 should be rejected"
397 ## next, attack the versioning
398 current_node_version = node.version
400 # try and submit a version behind
401 content xml_attr_rewrite(node.to_xml,
402 "version", current_node_version - 1)
403 put :update, :id => node.id
404 assert_response :conflict, "should have failed on old version number"
406 # try and submit a version ahead
407 content xml_attr_rewrite(node.to_xml,
408 "version", current_node_version + 1)
409 put :update, :id => node.id
410 assert_response :conflict, "should have failed on skipped version number"
412 # try and submit total crap in the version field
413 content xml_attr_rewrite(node.to_xml,
414 "version", "p1r4t3s!")
415 put :update, :id => node.id
416 assert_response :conflict,
417 "should not be able to put 'p1r4at3s!' in the version field"
419 ## try an update with the wrong ID
420 content create(:node).to_xml
421 put :update, :id => node.id
422 assert_response :bad_request,
423 "should not be able to update a node with a different ID from the XML"
425 ## try an update with a minimal valid XML doc which isn't a well-formed OSM doc.
427 put :update, :id => node.id
428 assert_response :bad_request,
429 "should not be able to update a node with non-OSM XML doc."
431 ## finally, produce a good request which should work
433 put :update, :id => node.id
434 assert_response :success, "a valid update request failed"
438 # test fetching multiple nodes
440 # check error when no parameter provided
442 assert_response :bad_request
444 # check error when no parameter value provided
445 get :nodes, :nodes => ""
446 assert_response :bad_request
448 # test a working call
449 get :nodes, :nodes => "1,2,4,15,17"
450 assert_response :success
451 assert_select "osm" do
452 assert_select "node", :count => 5
453 assert_select "node[id='1'][visible='true']", :count => 1
454 assert_select "node[id='2'][visible='false']", :count => 1
455 assert_select "node[id='4'][visible='true']", :count => 1
456 assert_select "node[id='15'][visible='true']", :count => 1
457 assert_select "node[id='17'][visible='false']", :count => 1
460 # check error when a non-existent node is included
461 get :nodes, :nodes => "1,2,4,15,17,400"
462 assert_response :not_found
466 # test adding tags to a node
467 def test_duplicate_tags
468 existing_tag = create(:node_tag)
469 assert_equal true, existing_tag.node.changeset.user.data_public
471 basic_authorization(existing_tag.node.changeset.user.email, "test")
473 # add an identical tag to the node
474 tag_xml = XML::Node.new("tag")
475 tag_xml["k"] = existing_tag.k
476 tag_xml["v"] = existing_tag.v
478 # add the tag into the existing xml
479 node_xml = existing_tag.node.to_xml
480 node_xml.find("//osm/node").first << tag_xml
484 put :update, :id => existing_tag.node.id
485 assert_response :bad_request,
486 "adding duplicate tags to a node should fail with 'bad request'"
487 assert_equal "Element node/#{existing_tag.node.id} has duplicate tags with key #{existing_tag.k}", @response.body
490 # test whether string injection is possible
491 def test_string_injection
492 ## First try with the non-data public user
493 basic_authorization(users(:normal_user).email, "test")
494 changeset_id = changesets(:normal_user_first_change).id
496 # try and put something into a string that the API might
497 # use unquoted and therefore allow code injection...
498 content "<osm><node lat='0' lon='0' changeset='#{changeset_id}'>" +
499 '<tag k="#{@user.inspect}" v="0"/>' +
502 assert_require_public_data "Shouldn't be able to create with non-public user"
504 ## Then try with the public data user
505 basic_authorization(users(:public_user).email, "test")
506 changeset_id = changesets(:public_user_first_change).id
508 # try and put something into a string that the API might
509 # use unquoted and therefore allow code injection...
510 content "<osm><node lat='0' lon='0' changeset='#{changeset_id}'>" +
511 '<tag k="#{@user.inspect}" v="0"/>' +
514 assert_response :success
515 nodeid = @response.body
517 # find the node in the database
518 checknode = Node.find(nodeid)
519 assert_not_nil checknode, "node not found in data base after upload"
521 # and grab it using the api
522 get :read, :id => nodeid
523 assert_response :success
524 apinode = Node.from_xml(@response.body)
525 assert_not_nil apinode, "downloaded node is nil, but shouldn't be"
527 # check the tags are not corrupted
528 assert_equal checknode.tags, apinode.tags
529 assert apinode.tags.include?("\#{@user.inspect}")
533 # update the changeset_id of a node element
534 def update_changeset(xml, changeset_id)
535 xml_attr_rewrite(xml, "changeset", changeset_id)
539 # update an attribute in the node element
540 def xml_attr_rewrite(xml, name, value)
541 xml.find("//osm/node").first[name] = value.to_s
548 parser = XML::Parser.string(xml)