2 require "way_controller"
4 class WayControllerTest < ActionController::TestCase
8 # test all routes which lead to this controller
11 { :path => "/api/0.6/way/create", :method => :put },
12 { :controller => "way", :action => "create" }
15 { :path => "/api/0.6/way/1/full", :method => :get },
16 { :controller => "way", :action => "full", :id => "1" }
19 { :path => "/api/0.6/way/1", :method => :get },
20 { :controller => "way", :action => "read", :id => "1" }
23 { :path => "/api/0.6/way/1", :method => :put },
24 { :controller => "way", :action => "update", :id => "1" }
27 { :path => "/api/0.6/way/1", :method => :delete },
28 { :controller => "way", :action => "delete", :id => "1" }
31 { :path => "/api/0.6/ways", :method => :get },
32 { :controller => "way", :action => "ways" }
36 # -------------------------------------
38 # -------------------------------------
41 # check that a visible way is returned properly
42 get :read, :id => current_ways(:visible_way).id
43 assert_response :success
45 # check that an invisible way is not returned
46 get :read, :id => current_ways(:invisible_way).id
49 # check chat a non-existent way is not returned
51 assert_response :not_found
55 # check the "full" mode
58 get :full, :id => way.id
60 # full call should say "gone" for non-visible ways...
66 # otherwise it should say success
67 assert_response :success
69 # Check the way is correctly returned
70 assert_select "osm way[id='#{way.id}'][version='#{way.version}'][visible='#{way.visible}']", 1
72 # check that each node in the way appears once in the output as a
73 # reference and as the node element.
75 count = (way.nodes - (way.nodes - [n])).length
76 assert_select "osm way nd[ref='#{n.id}']", count
77 assert_select "osm node[id='#{n.id}'][version='#{n.version}'][lat='#{n.lat}'][lon='#{n.lon}']", 1
83 # test fetching multiple ways
85 # check error when no parameter provided
87 assert_response :bad_request
89 # check error when no parameter value provided
90 get :ways, :ways => ""
91 assert_response :bad_request
94 get :ways, :ways => "1,2,4,6"
95 assert_response :success
96 assert_select "osm" do
97 assert_select "way", :count => 4
98 assert_select "way[id='1'][visible='true']", :count => 1
99 assert_select "way[id='2'][visible='false']", :count => 1
100 assert_select "way[id='4'][visible='true']", :count => 1
101 assert_select "way[id='6'][visible='true']", :count => 1
104 # check error when a non-existent way is included
105 get :ways, :ways => "1,2,4,6,400"
106 assert_response :not_found
109 # -------------------------------------
110 # Test simple way creation.
111 # -------------------------------------
114 ## First check that it fails when creating a way using a non-public user
115 nid1 = current_nodes(:used_node_1).id
116 nid2 = current_nodes(:used_node_2).id
117 basic_authorization users(:normal_user).email, "test"
119 # use the first user's open changeset
120 changeset_id = changesets(:normal_user_first_change).id
122 # create a way with pre-existing nodes
123 content "<osm><way changeset='#{changeset_id}'>" +
124 "<nd ref='#{nid1}'/><nd ref='#{nid2}'/>" +
125 "<tag k='test' v='yes' /></way></osm>"
128 assert_response :forbidden,
129 "way upload did not return forbidden status"
131 ## Now use a public user
132 nid1 = current_nodes(:used_node_1).id
133 nid2 = current_nodes(:used_node_2).id
134 basic_authorization users(:public_user).email, "test"
136 # use the first user's open changeset
137 changeset_id = changesets(:public_user_first_change).id
139 # create a way with pre-existing nodes
140 content "<osm><way changeset='#{changeset_id}'>" +
141 "<nd ref='#{nid1}'/><nd ref='#{nid2}'/>" +
142 "<tag k='test' v='yes' /></way></osm>"
145 assert_response :success,
146 "way upload did not return success status"
147 # read id of created way and search for it
148 wayid = @response.body
149 checkway = Way.find(wayid)
150 assert_not_nil checkway,
151 "uploaded way not found in data base after upload"
153 assert_equal checkway.nds.length, 2,
154 "saved way does not contain exactly one node"
155 assert_equal checkway.nds[0], nid1,
156 "saved way does not contain the right node on pos 0"
157 assert_equal checkway.nds[1], nid2,
158 "saved way does not contain the right node on pos 1"
159 assert_equal checkway.changeset_id, changeset_id,
160 "saved way does not belong to the correct changeset"
161 assert_equal users(:public_user).id, checkway.changeset.user_id,
162 "saved way does not belong to user that created it"
163 assert_equal true, checkway.visible,
164 "saved way is not visible"
167 # -------------------------------------
168 # Test creating some invalid ways.
169 # -------------------------------------
171 def test_create_invalid
172 ## First test with a private user to make sure that they are not authorized
173 basic_authorization users(:normal_user).email, "test"
175 # use the first user's open changeset
176 open_changeset_id = changesets(:normal_user_first_change).id
177 closed_changeset_id = changesets(:normal_user_closed_change).id
178 nid1 = current_nodes(:used_node_1).id
180 # create a way with non-existing node
181 content "<osm><way changeset='#{open_changeset_id}'>" +
182 "<nd ref='0'/><tag k='test' v='yes' /></way></osm>"
185 assert_response :forbidden,
186 "way upload with invalid node using a private user did not return 'forbidden'"
188 # create a way with no nodes
189 content "<osm><way changeset='#{open_changeset_id}'>" +
190 "<tag k='test' v='yes' /></way></osm>"
193 assert_response :forbidden,
194 "way upload with no node using a private userdid not return 'forbidden'"
196 # create a way inside a closed changeset
197 content "<osm><way changeset='#{closed_changeset_id}'>" +
198 "<nd ref='#{nid1}'/></way></osm>"
201 assert_response :forbidden,
202 "way upload to closed changeset with a private user did not return 'forbidden'"
204 ## Now test with a public user
205 basic_authorization users(:public_user).email, "test"
207 # use the first user's open changeset
208 open_changeset_id = changesets(:public_user_first_change).id
209 closed_changeset_id = changesets(:public_user_closed_change).id
210 nid1 = current_nodes(:used_node_1).id
212 # create a way with non-existing node
213 content "<osm><way changeset='#{open_changeset_id}'>" +
214 "<nd ref='0'/><tag k='test' v='yes' /></way></osm>"
217 assert_response :precondition_failed,
218 "way upload with invalid node did not return 'precondition failed'"
219 assert_equal "Precondition failed: Way requires the nodes with id in (0), which either do not exist, or are not visible.", @response.body
221 # create a way with no nodes
222 content "<osm><way changeset='#{open_changeset_id}'>" +
223 "<tag k='test' v='yes' /></way></osm>"
226 assert_response :precondition_failed,
227 "way upload with no node did not return 'precondition failed'"
228 assert_equal "Precondition failed: Cannot create way: data is invalid.", @response.body
230 # create a way inside a closed changeset
231 content "<osm><way changeset='#{closed_changeset_id}'>" +
232 "<nd ref='#{nid1}'/></way></osm>"
235 assert_response :conflict,
236 "way upload to closed changeset did not return 'conflict'"
238 # create a way with a tag which is too long
239 content "<osm><way changeset='#{open_changeset_id}'>" +
240 "<nd ref='#{nid1}'/>" +
241 "<tag k='foo' v='#{'x' * 256}'/>" +
245 assert_response :bad_request,
246 "way upload to with too long tag did not return 'bad_request'"
249 # -------------------------------------
250 # Test deleting ways.
251 # -------------------------------------
254 # first try to delete way without auth
255 delete :delete, :id => current_ways(:visible_way).id
256 assert_response :unauthorized
258 # now set auth using the private user
259 basic_authorization(users(:normal_user).email, "test")
261 # this shouldn't work as with the 0.6 api we need pay load to delete
262 delete :delete, :id => current_ways(:visible_way).id
263 assert_response :forbidden
265 # Now try without having a changeset
266 content "<osm><way id='#{current_ways(:visible_way).id}'/></osm>"
267 delete :delete, :id => current_ways(:visible_way).id
268 assert_response :forbidden
270 # try to delete with an invalid (closed) changeset
271 content update_changeset(current_ways(:visible_way).to_xml,
272 changesets(:normal_user_closed_change).id)
273 delete :delete, :id => current_ways(:visible_way).id
274 assert_response :forbidden
276 # try to delete with an invalid (non-existent) changeset
277 content update_changeset(current_ways(:visible_way).to_xml, 0)
278 delete :delete, :id => current_ways(:visible_way).id
279 assert_response :forbidden
281 # Now try with a valid changeset
282 content current_ways(:visible_way).to_xml
283 delete :delete, :id => current_ways(:visible_way).id
284 assert_response :forbidden
286 # check the returned value - should be the new version number
287 # valid delete should return the new version number, which should
288 # be greater than the old version number
289 # assert @response.body.to_i > current_ways(:visible_way).version,
290 # "delete request should return a new version number for way"
292 # this won't work since the way is already deleted
293 content current_ways(:invisible_way).to_xml
294 delete :delete, :id => current_ways(:invisible_way).id
295 assert_response :forbidden
297 # this shouldn't work as the way is used in a relation
298 content current_ways(:used_way).to_xml
299 delete :delete, :id => current_ways(:used_way).id
300 assert_response :forbidden,
301 "shouldn't be able to delete a way used in a relation (#{@response.body}), when done by a private user"
303 # this won't work since the way never existed
304 delete :delete, :id => 0
305 assert_response :forbidden
307 ### Now check with a public user
309 basic_authorization(users(:public_user).email, "test")
311 # this shouldn't work as with the 0.6 api we need pay load to delete
312 delete :delete, :id => current_ways(:visible_way).id
313 assert_response :bad_request
315 # Now try without having a changeset
316 content "<osm><way id='#{current_ways(:visible_way).id}'/></osm>"
317 delete :delete, :id => current_ways(:visible_way).id
318 assert_response :bad_request
320 # try to delete with an invalid (closed) changeset
321 content update_changeset(current_ways(:visible_way).to_xml,
322 changesets(:public_user_closed_change).id)
323 delete :delete, :id => current_ways(:visible_way).id
324 assert_response :conflict
326 # try to delete with an invalid (non-existent) changeset
327 content update_changeset(current_ways(:visible_way).to_xml, 0)
328 delete :delete, :id => current_ways(:visible_way).id
329 assert_response :conflict
331 # Now try with a valid changeset
332 content current_ways(:visible_way).to_xml
333 delete :delete, :id => current_ways(:visible_way).id
334 assert_response :success
336 # check the returned value - should be the new version number
337 # valid delete should return the new version number, which should
338 # be greater than the old version number
339 assert @response.body.to_i > current_ways(:visible_way).version,
340 "delete request should return a new version number for way"
342 # this won't work since the way is already deleted
343 content current_ways(:invisible_way).to_xml
344 delete :delete, :id => current_ways(:invisible_way).id
345 assert_response :gone
347 # this shouldn't work as the way is used in a relation
348 content current_ways(:used_way).to_xml
349 delete :delete, :id => current_ways(:used_way).id
350 assert_response :precondition_failed,
351 "shouldn't be able to delete a way used in a relation (#{@response.body})"
352 assert_equal "Precondition failed: Way 3 is still used by relations 1.", @response.body
354 # this won't work since the way never existed
355 delete :delete, :id => 0
356 assert_response :not_found
360 # tests whether the API works and prevents incorrect use while trying
363 ## First test with no user credentials
364 # try and update a way without authorisation
365 # first try to delete way without auth
366 content current_ways(:visible_way).to_xml
367 put :update, :id => current_ways(:visible_way).id
368 assert_response :unauthorized
370 ## Second test with the private user
373 basic_authorization(users(:normal_user).email, "test")
375 ## trying to break changesets
377 # try and update in someone else's changeset
378 content update_changeset(current_ways(:visible_way).to_xml,
379 changesets(:public_user_first_change).id)
380 put :update, :id => current_ways(:visible_way).id
381 assert_require_public_data "update with other user's changeset should be forbidden when date isn't public"
383 # try and update in a closed changeset
384 content update_changeset(current_ways(:visible_way).to_xml,
385 changesets(:normal_user_closed_change).id)
386 put :update, :id => current_ways(:visible_way).id
387 assert_require_public_data "update with closed changeset should be forbidden, when data isn't public"
389 # try and update in a non-existant changeset
390 content update_changeset(current_ways(:visible_way).to_xml, 0)
391 put :update, :id => current_ways(:visible_way).id
392 assert_require_public_data("update with changeset=0 should be forbidden, when data isn't public")
394 ## try and submit invalid updates
395 content xml_replace_node(current_ways(:visible_way).to_xml, 3, 9999)
396 put :update, :id => current_ways(:visible_way).id
397 assert_require_public_data "way with non-existent node should be forbidden, when data isn't public"
399 content xml_replace_node(current_ways(:visible_way).to_xml, 3, current_nodes(:invisible_node).id)
400 put :update, :id => current_ways(:visible_way).id
401 assert_require_public_data "way with deleted node should be forbidden, when data isn't public"
403 ## finally, produce a good request which should work
404 content current_ways(:visible_way).to_xml
405 put :update, :id => current_ways(:visible_way).id
406 assert_require_public_data "should have failed with a forbidden when data isn't public"
408 ## Finally test with the public user
410 # try and update a way without authorisation
411 # first try to delete way without auth
412 content current_ways(:visible_way).to_xml
413 put :update, :id => current_ways(:visible_way).id
414 assert_response :forbidden
417 basic_authorization(users(:public_user).email, "test")
419 ## trying to break changesets
421 # try and update in someone else's changeset
422 content update_changeset(current_ways(:visible_way).to_xml,
423 changesets(:normal_user_first_change).id)
424 put :update, :id => current_ways(:visible_way).id
425 assert_response :conflict, "update with other user's changeset should be rejected"
427 # try and update in a closed changeset
428 content update_changeset(current_ways(:visible_way).to_xml,
429 changesets(:normal_user_closed_change).id)
430 put :update, :id => current_ways(:visible_way).id
431 assert_response :conflict, "update with closed changeset should be rejected"
433 # try and update in a non-existant changeset
434 content update_changeset(current_ways(:visible_way).to_xml, 0)
435 put :update, :id => current_ways(:visible_way).id
436 assert_response :conflict, "update with changeset=0 should be rejected"
438 ## try and submit invalid updates
439 content xml_replace_node(current_ways(:visible_way).to_xml, 3, 9999)
440 put :update, :id => current_ways(:visible_way).id
441 assert_response :precondition_failed, "way with non-existent node should be rejected"
443 content xml_replace_node(current_ways(:visible_way).to_xml, 3, current_nodes(:invisible_node).id)
444 put :update, :id => current_ways(:visible_way).id
445 assert_response :precondition_failed, "way with deleted node should be rejected"
447 ## next, attack the versioning
448 current_way_version = current_ways(:visible_way).version
450 # try and submit a version behind
451 content xml_attr_rewrite(current_ways(:visible_way).to_xml,
452 "version", current_way_version - 1)
453 put :update, :id => current_ways(:visible_way).id
454 assert_response :conflict, "should have failed on old version number"
456 # try and submit a version ahead
457 content xml_attr_rewrite(current_ways(:visible_way).to_xml,
458 "version", current_way_version + 1)
459 put :update, :id => current_ways(:visible_way).id
460 assert_response :conflict, "should have failed on skipped version number"
462 # try and submit total crap in the version field
463 content xml_attr_rewrite(current_ways(:visible_way).to_xml,
464 "version", "p1r4t3s!")
465 put :update, :id => current_ways(:visible_way).id
466 assert_response :conflict,
467 "should not be able to put 'p1r4at3s!' in the version field"
469 ## try an update with the wrong ID
470 content current_ways(:used_way).to_xml
471 put :update, :id => current_ways(:visible_way).id
472 assert_response :bad_request,
473 "should not be able to update a way with a different ID from the XML"
475 ## try an update with a minimal valid XML doc which isn't a well-formed OSM doc.
477 put :update, :id => current_ways(:visible_way).id
478 assert_response :bad_request,
479 "should not be able to update a way with non-OSM XML doc."
481 ## finally, produce a good request which should work
482 content current_ways(:visible_way).to_xml
483 put :update, :id => current_ways(:visible_way).id
484 assert_response :success, "a valid update request failed"
487 # ------------------------------------------------------------
489 # ------------------------------------------------------------
492 # Try adding a new tag to a way
494 ## Try with the non-public user
496 basic_authorization(users(:normal_user).email, "test")
498 # add an identical tag to the way
499 tag_xml = XML::Node.new("tag")
503 # add the tag into the existing xml
504 way_xml = current_ways(:visible_way).to_xml
505 way_xml.find("//osm/way").first << tag_xml
509 put :update, :id => current_ways(:visible_way).id
510 assert_response :forbidden,
511 "adding a duplicate tag to a way for a non-public should fail with 'forbidden'"
513 ## Now try with the public user
515 basic_authorization(users(:public_user).email, "test")
517 # add an identical tag to the way
518 tag_xml = XML::Node.new("tag")
522 # add the tag into the existing xml
523 way_xml = current_ways(:visible_way).to_xml
524 way_xml.find("//osm/way").first << tag_xml
528 put :update, :id => current_ways(:visible_way).id
529 assert_response :success,
530 "adding a new tag to a way should succeed"
531 assert_equal current_ways(:visible_way).version + 1, @response.body.to_i
535 # Try adding a duplicate of an existing tag to a way
536 def test_add_duplicate_tags
537 ## Try with the non-public user
539 basic_authorization(users(:normal_user).email, "test")
541 # add an identical tag to the way
542 tag_xml = XML::Node.new("tag")
543 tag_xml["k"] = current_way_tags(:t1).k
544 tag_xml["v"] = current_way_tags(:t1).v
546 # add the tag into the existing xml
547 way_xml = current_ways(:visible_way).to_xml
548 way_xml.find("//osm/way").first << tag_xml
552 put :update, :id => current_ways(:visible_way).id
553 assert_response :forbidden,
554 "adding a duplicate tag to a way for a non-public should fail with 'forbidden'"
556 ## Now try with the public user
558 basic_authorization(users(:public_user).email, "test")
560 # add an identical tag to the way
561 tag_xml = XML::Node.new("tag")
562 tag_xml["k"] = current_way_tags(:t1).k
563 tag_xml["v"] = current_way_tags(:t1).v
565 # add the tag into the existing xml
566 way_xml = current_ways(:visible_way).to_xml
567 way_xml.find("//osm/way").first << tag_xml
571 put :update, :id => current_ways(:visible_way).id
572 assert_response :bad_request,
573 "adding a duplicate tag to a way should fail with 'bad request'"
574 assert_equal "Element way/#{current_ways(:visible_way).id} has duplicate tags with key #{current_way_tags(:t1).k}", @response.body
578 # Try adding a new duplicate tags to a way
579 def test_new_duplicate_tags
580 ## First test with the non-public user so should be rejected
582 basic_authorization(users(:normal_user).email, "test")
584 # create duplicate tag
585 tag_xml = XML::Node.new("tag")
586 tag_xml["k"] = "i_am_a_duplicate"
587 tag_xml["v"] = "foobar"
589 # add the tag into the existing xml
590 way_xml = current_ways(:visible_way).to_xml
592 # add two copies of the tag
593 way_xml.find("//osm/way").first << tag_xml.copy(true) << tag_xml
597 put :update, :id => current_ways(:visible_way).id
598 assert_response :forbidden,
599 "adding new duplicate tags to a way using a non-public user should fail with 'forbidden'"
601 ## Now test with the public user
603 basic_authorization(users(:public_user).email, "test")
605 # create duplicate tag
606 tag_xml = XML::Node.new("tag")
607 tag_xml["k"] = "i_am_a_duplicate"
608 tag_xml["v"] = "foobar"
610 # add the tag into the existing xml
611 way_xml = current_ways(:visible_way).to_xml
613 # add two copies of the tag
614 way_xml.find("//osm/way").first << tag_xml.copy(true) << tag_xml
618 put :update, :id => current_ways(:visible_way).id
619 assert_response :bad_request,
620 "adding new duplicate tags to a way should fail with 'bad request'"
621 assert_equal "Element way/#{current_ways(:visible_way).id} has duplicate tags with key i_am_a_duplicate", @response.body
625 # Try adding a new duplicate tags to a way.
626 # But be a bit subtle - use unicode decoding ambiguities to use different
627 # binary strings which have the same decoding.
628 def test_invalid_duplicate_tags
629 ## First make sure that you can't with a non-public user
631 basic_authorization(users(:normal_user).email, "test")
633 # add the tag into the existing xml
634 way_str = "<osm><way changeset='1'>"
635 way_str << "<tag k='addr:housenumber' v='1'/>"
636 way_str << "<tag k='addr:housenumber' v='2'/>"
637 way_str << "</way></osm>"
642 assert_response :forbidden,
643 "adding new duplicate tags to a way with a non-public user should fail with 'forbidden'"
645 ## Now do it with a public user
647 basic_authorization(users(:public_user).email, "test")
649 # add the tag into the existing xml
650 way_str = "<osm><way changeset='1'>"
651 way_str << "<tag k='addr:housenumber' v='1'/>"
652 way_str << "<tag k='addr:housenumber' v='2'/>"
653 way_str << "</way></osm>"
658 assert_response :bad_request,
659 "adding new duplicate tags to a way should fail with 'bad request'"
660 assert_equal "Element way/ has duplicate tags with key addr:housenumber", @response.body
664 # test that a call to ways_for_node returns all ways that contain the node
665 # and none that don't.
666 def test_ways_for_node
667 # in current fixtures ways 1 and 3 all use node 3. ways 2 and 4
668 # *used* to use it but doesn't.
669 get :ways_for_node, :id => current_nodes(:used_node_1).id
670 assert_response :success
671 ways_xml = XML::Parser.string(@response.body).parse
672 assert_not_nil ways_xml, "failed to parse ways_for_node response"
674 # check that the set of IDs match expectations
675 expected_way_ids = [current_ways(:visible_way).id,
676 current_ways(:used_way).id
678 found_way_ids = ways_xml.find("//osm/way").collect { |w| w["id"].to_i }
679 assert_equal expected_way_ids.sort, found_way_ids.sort,
680 "expected ways for node #{current_nodes(:used_node_1).id} did not match found"
682 # check the full ways to ensure we're not missing anything
683 expected_way_ids.each do |id|
684 way_xml = ways_xml.find("//osm/way[@id='#{id}']").first
685 assert_ways_are_equal(Way.find(id),
686 Way.from_xml_node(way_xml))
691 # update the changeset_id of a way element
692 def update_changeset(xml, changeset_id)
693 xml_attr_rewrite(xml, "changeset", changeset_id)
697 # update an attribute in the way element
698 def xml_attr_rewrite(xml, name, value)
699 xml.find("//osm/way").first[name] = value.to_s
704 # replace a node in a way element
705 def xml_replace_node(xml, old_node, new_node)
706 xml.find("//osm/way/nd[@ref='#{old_node}']").first["ref"] = new_node.to_s