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 => create(:way).id
43 assert_response :success
45 # check that an invisible way is not returned
46 get :read, :id => create(:way, :deleted).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='#{format('%.7f', n.lat)}'][lon='#{format('%.7f', 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 node1 = create(:node)
115 node2 = create(:node)
116 private_user = create(:user, :data_public => false)
117 private_changeset = create(:changeset, :user => private_user)
119 changeset = create(:changeset, :user => user)
121 ## First check that it fails when creating a way using a non-public user
122 basic_authorization private_user.email, "test"
124 # use the first user's open changeset
125 changeset_id = private_changeset.id
127 # create a way with pre-existing nodes
128 content "<osm><way changeset='#{changeset_id}'>" +
129 "<nd ref='#{node1.id}'/><nd ref='#{node2.id}'/>" +
130 "<tag k='test' v='yes' /></way></osm>"
133 assert_response :forbidden,
134 "way upload did not return forbidden status"
136 ## Now use a public user
137 basic_authorization user.email, "test"
139 # use the first user's open changeset
140 changeset_id = changeset.id
142 # create a way with pre-existing nodes
143 content "<osm><way changeset='#{changeset_id}'>" +
144 "<nd ref='#{node1.id}'/><nd ref='#{node2.id}'/>" +
145 "<tag k='test' v='yes' /></way></osm>"
148 assert_response :success,
149 "way upload did not return success status"
150 # read id of created way and search for it
151 wayid = @response.body
152 checkway = Way.find(wayid)
153 assert_not_nil checkway,
154 "uploaded way not found in data base after upload"
156 assert_equal checkway.nds.length, 2,
157 "saved way does not contain exactly one node"
158 assert_equal checkway.nds[0], node1.id,
159 "saved way does not contain the right node on pos 0"
160 assert_equal checkway.nds[1], node2.id,
161 "saved way does not contain the right node on pos 1"
162 assert_equal checkway.changeset_id, changeset_id,
163 "saved way does not belong to the correct changeset"
164 assert_equal user.id, checkway.changeset.user_id,
165 "saved way does not belong to user that created it"
166 assert_equal true, checkway.visible,
167 "saved way is not visible"
170 # -------------------------------------
171 # Test creating some invalid ways.
172 # -------------------------------------
174 def test_create_invalid
176 private_user = create(:user, :data_public => false)
177 private_open_changeset = create(:changeset, :user => private_user)
178 private_closed_changeset = create(:changeset, :closed, :user => private_user)
180 open_changeset = create(:changeset, :user => user)
181 closed_changeset = create(:changeset, :closed, :user => user)
183 ## First test with a private user to make sure that they are not authorized
184 basic_authorization private_user.email, "test"
186 # use the first user's open changeset
187 # create a way with non-existing node
188 content "<osm><way changeset='#{private_open_changeset.id}'>" +
189 "<nd ref='0'/><tag k='test' v='yes' /></way></osm>"
192 assert_response :forbidden,
193 "way upload with invalid node using a private user did not return 'forbidden'"
195 # create a way with no nodes
196 content "<osm><way changeset='#{private_open_changeset.id}'>" +
197 "<tag k='test' v='yes' /></way></osm>"
200 assert_response :forbidden,
201 "way upload with no node using a private userdid not return 'forbidden'"
203 # create a way inside a closed changeset
204 content "<osm><way changeset='#{private_closed_changeset.id}'>" +
205 "<nd ref='#{node.id}'/></way></osm>"
208 assert_response :forbidden,
209 "way upload to closed changeset with a private user did not return 'forbidden'"
211 ## Now test with a public user
212 basic_authorization user.email, "test"
214 # use the first user's open changeset
215 # create a way with non-existing node
216 content "<osm><way changeset='#{open_changeset.id}'>" +
217 "<nd ref='0'/><tag k='test' v='yes' /></way></osm>"
220 assert_response :precondition_failed,
221 "way upload with invalid node did not return 'precondition failed'"
222 assert_equal "Precondition failed: Way requires the nodes with id in (0), which either do not exist, or are not visible.", @response.body
224 # create a way with no nodes
225 content "<osm><way changeset='#{open_changeset.id}'>" +
226 "<tag k='test' v='yes' /></way></osm>"
229 assert_response :precondition_failed,
230 "way upload with no node did not return 'precondition failed'"
231 assert_equal "Precondition failed: Cannot create way: data is invalid.", @response.body
233 # create a way inside a closed changeset
234 content "<osm><way changeset='#{closed_changeset.id}'>" +
235 "<nd ref='#{node.id}'/></way></osm>"
238 assert_response :conflict,
239 "way upload to closed changeset did not return 'conflict'"
241 # create a way with a tag which is too long
242 content "<osm><way changeset='#{open_changeset.id}'>" +
243 "<nd ref='#{node.id}'/>" +
244 "<tag k='foo' v='#{'x' * 256}'/>" +
248 assert_response :bad_request,
249 "way upload to with too long tag did not return 'bad_request'"
252 # -------------------------------------
253 # Test deleting ways.
254 # -------------------------------------
257 private_user = create(:user, :data_public => false)
258 private_open_changeset = create(:changeset, :user => private_user)
259 private_closed_changeset = create(:changeset, :closed, :user => private_user)
260 private_way = create(:way, :changeset => private_open_changeset)
261 private_deleted_way = create(:way, :deleted, :changeset => private_open_changeset)
262 private_used_way = create(:way, :changeset => private_open_changeset)
263 create(:relation_member, :member => private_used_way)
265 open_changeset = create(:changeset, :user => user)
266 closed_changeset = create(:changeset, :closed, :user => user)
267 way = create(:way, :changeset => open_changeset)
268 deleted_way = create(:way, :deleted, :changeset => open_changeset)
269 used_way = create(:way, :changeset => open_changeset)
270 relation_member = create(:relation_member, :member => used_way)
271 relation = relation_member.relation
273 # first try to delete way without auth
274 delete :delete, :id => way.id
275 assert_response :unauthorized
277 # now set auth using the private user
278 basic_authorization(private_user.email, "test")
280 # this shouldn't work as with the 0.6 api we need pay load to delete
281 delete :delete, :id => private_way.id
282 assert_response :forbidden
284 # Now try without having a changeset
285 content "<osm><way id='#{private_way.id}'/></osm>"
286 delete :delete, :id => private_way.id
287 assert_response :forbidden
289 # try to delete with an invalid (closed) changeset
290 content update_changeset(private_way.to_xml, private_closed_changeset.id)
291 delete :delete, :id => private_way.id
292 assert_response :forbidden
294 # try to delete with an invalid (non-existent) changeset
295 content update_changeset(private_way.to_xml, 0)
296 delete :delete, :id => private_way.id
297 assert_response :forbidden
299 # Now try with a valid changeset
300 content private_way.to_xml
301 delete :delete, :id => private_way.id
302 assert_response :forbidden
304 # check the returned value - should be the new version number
305 # valid delete should return the new version number, which should
306 # be greater than the old version number
307 # assert @response.body.to_i > current_ways(:visible_way).version,
308 # "delete request should return a new version number for way"
310 # this won't work since the way is already deleted
311 content private_deleted_way.to_xml
312 delete :delete, :id => private_deleted_way.id
313 assert_response :forbidden
315 # this shouldn't work as the way is used in a relation
316 content private_used_way.to_xml
317 delete :delete, :id => private_used_way.id
318 assert_response :forbidden,
319 "shouldn't be able to delete a way used in a relation (#{@response.body}), when done by a private user"
321 # this won't work since the way never existed
322 delete :delete, :id => 0
323 assert_response :forbidden
325 ### Now check with a public user
327 basic_authorization(user.email, "test")
329 # this shouldn't work as with the 0.6 api we need pay load to delete
330 delete :delete, :id => way.id
331 assert_response :bad_request
333 # Now try without having a changeset
334 content "<osm><way id='#{way.id}'/></osm>"
335 delete :delete, :id => way.id
336 assert_response :bad_request
338 # try to delete with an invalid (closed) changeset
339 content update_changeset(way.to_xml, closed_changeset.id)
340 delete :delete, :id => way.id
341 assert_response :conflict
343 # try to delete with an invalid (non-existent) changeset
344 content update_changeset(way.to_xml, 0)
345 delete :delete, :id => way.id
346 assert_response :conflict
348 # Now try with a valid changeset
350 delete :delete, :id => way.id
351 assert_response :success
353 # check the returned value - should be the new version number
354 # valid delete should return the new version number, which should
355 # be greater than the old version number
356 assert @response.body.to_i > way.version,
357 "delete request should return a new version number for way"
359 # this won't work since the way is already deleted
360 content deleted_way.to_xml
361 delete :delete, :id => deleted_way.id
362 assert_response :gone
364 # this shouldn't work as the way is used in a relation
365 content used_way.to_xml
366 delete :delete, :id => used_way.id
367 assert_response :precondition_failed,
368 "shouldn't be able to delete a way used in a relation (#{@response.body})"
369 assert_equal "Precondition failed: Way #{used_way.id} is still used by relations #{relation.id}.", @response.body
371 # this won't work since the way never existed
372 delete :delete, :id => 0
373 assert_response :not_found
377 # tests whether the API works and prevents incorrect use while trying
380 private_user = create(:user, :data_public => false)
381 private_way = create(:way, :changeset => create(:changeset, :user => private_user))
383 way = create(:way, :changeset => create(:changeset, :user => user))
385 create(:way_node, :way => private_way, :node => node)
386 create(:way_node, :way => way, :node => node)
388 ## First test with no user credentials
389 # try and update a way without authorisation
391 put :update, :id => way.id
392 assert_response :unauthorized
394 ## Second test with the private user
397 basic_authorization(private_user.email, "test")
399 ## trying to break changesets
401 # try and update in someone else's changeset
402 content update_changeset(private_way.to_xml,
403 create(:changeset).id)
404 put :update, :id => private_way.id
405 assert_require_public_data "update with other user's changeset should be forbidden when date isn't public"
407 # try and update in a closed changeset
408 content update_changeset(private_way.to_xml,
409 create(:changeset, :closed, :user => private_user).id)
410 put :update, :id => private_way.id
411 assert_require_public_data "update with closed changeset should be forbidden, when data isn't public"
413 # try and update in a non-existant changeset
414 content update_changeset(private_way.to_xml, 0)
415 put :update, :id => private_way.id
416 assert_require_public_data("update with changeset=0 should be forbidden, when data isn't public")
418 ## try and submit invalid updates
419 content xml_replace_node(private_way.to_xml, node.id, 9999)
420 put :update, :id => private_way.id
421 assert_require_public_data "way with non-existent node should be forbidden, when data isn't public"
423 content xml_replace_node(private_way.to_xml, node.id, create(:node, :deleted).id)
424 put :update, :id => private_way.id
425 assert_require_public_data "way with deleted node should be forbidden, when data isn't public"
427 ## finally, produce a good request which will still not work
428 content private_way.to_xml
429 put :update, :id => private_way.id
430 assert_require_public_data "should have failed with a forbidden when data isn't public"
432 ## Finally test with the public user
435 basic_authorization(user.email, "test")
437 ## trying to break changesets
439 # try and update in someone else's changeset
440 content update_changeset(way.to_xml,
441 create(:changeset).id)
442 put :update, :id => way.id
443 assert_response :conflict, "update with other user's changeset should be rejected"
445 # try and update in a closed changeset
446 content update_changeset(way.to_xml,
447 create(:changeset, :closed, :user => user).id)
448 put :update, :id => way.id
449 assert_response :conflict, "update with closed changeset should be rejected"
451 # try and update in a non-existant changeset
452 content update_changeset(way.to_xml, 0)
453 put :update, :id => way.id
454 assert_response :conflict, "update with changeset=0 should be rejected"
456 ## try and submit invalid updates
457 content xml_replace_node(way.to_xml, node.id, 9999)
458 put :update, :id => way.id
459 assert_response :precondition_failed, "way with non-existent node should be rejected"
461 content xml_replace_node(way.to_xml, node.id, create(:node, :deleted).id)
462 put :update, :id => way.id
463 assert_response :precondition_failed, "way with deleted node should be rejected"
465 ## next, attack the versioning
466 current_way_version = way.version
468 # try and submit a version behind
469 content xml_attr_rewrite(way.to_xml,
470 "version", current_way_version - 1)
471 put :update, :id => way.id
472 assert_response :conflict, "should have failed on old version number"
474 # try and submit a version ahead
475 content xml_attr_rewrite(way.to_xml,
476 "version", current_way_version + 1)
477 put :update, :id => way.id
478 assert_response :conflict, "should have failed on skipped version number"
480 # try and submit total crap in the version field
481 content xml_attr_rewrite(way.to_xml,
482 "version", "p1r4t3s!")
483 put :update, :id => way.id
484 assert_response :conflict,
485 "should not be able to put 'p1r4at3s!' in the version field"
487 ## try an update with the wrong ID
488 content create(:way).to_xml
489 put :update, :id => way.id
490 assert_response :bad_request,
491 "should not be able to update a way with a different ID from the XML"
493 ## try an update with a minimal valid XML doc which isn't a well-formed OSM doc.
495 put :update, :id => way.id
496 assert_response :bad_request,
497 "should not be able to update a way with non-OSM XML doc."
499 ## finally, produce a good request which should work
501 put :update, :id => way.id
502 assert_response :success, "a valid update request failed"
505 # ------------------------------------------------------------
507 # ------------------------------------------------------------
510 # Try adding a new tag to a way
512 private_user = create(:user, :data_public => false)
513 private_way = create(:way_with_nodes, :nodes_count => 2, :changeset => create(:changeset, :user => private_user))
515 way = create(:way_with_nodes, :nodes_count => 2, :changeset => create(:changeset, :user => user))
517 ## Try with the non-public user
519 basic_authorization(private_user.email, "test")
521 # add an identical tag to the way
522 tag_xml = XML::Node.new("tag")
526 # add the tag into the existing xml
527 way_xml = private_way.to_xml
528 way_xml.find("//osm/way").first << tag_xml
532 put :update, :id => private_way.id
533 assert_response :forbidden,
534 "adding a duplicate tag to a way for a non-public should fail with 'forbidden'"
536 ## Now try with the public user
538 basic_authorization(user.email, "test")
540 # add an identical tag to the way
541 tag_xml = XML::Node.new("tag")
545 # add the tag into the existing xml
547 way_xml.find("//osm/way").first << tag_xml
551 put :update, :id => way.id
552 assert_response :success,
553 "adding a new tag to a way should succeed"
554 assert_equal way.version + 1, @response.body.to_i
558 # Try adding a duplicate of an existing tag to a way
559 def test_add_duplicate_tags
560 private_user = create(:user, :data_public => false)
561 private_way = create(:way, :changeset => create(:changeset, :user => private_user))
562 private_existing_tag = create(:way_tag, :way => private_way)
564 way = create(:way, :changeset => create(:changeset, :user => user))
565 existing_tag = create(:way_tag, :way => way)
567 ## Try with the non-public user
569 basic_authorization(private_user.email, "test")
571 # add an identical tag to the way
572 tag_xml = XML::Node.new("tag")
573 tag_xml["k"] = private_existing_tag.k
574 tag_xml["v"] = private_existing_tag.v
576 # add the tag into the existing xml
577 way_xml = private_way.to_xml
578 way_xml.find("//osm/way").first << tag_xml
582 put :update, :id => private_way.id
583 assert_response :forbidden,
584 "adding a duplicate tag to a way for a non-public should fail with 'forbidden'"
586 ## Now try with the public user
588 basic_authorization(user.email, "test")
590 # add an identical tag to the way
591 tag_xml = XML::Node.new("tag")
592 tag_xml["k"] = existing_tag.k
593 tag_xml["v"] = existing_tag.v
595 # add the tag into the existing xml
597 way_xml.find("//osm/way").first << tag_xml
601 put :update, :id => way.id
602 assert_response :bad_request,
603 "adding a duplicate tag to a way should fail with 'bad request'"
604 assert_equal "Element way/#{way.id} has duplicate tags with key #{existing_tag.k}", @response.body
608 # Try adding a new duplicate tags to a way
609 def test_new_duplicate_tags
610 private_user = create(:user, :data_public => false)
611 private_way = create(:way, :changeset => create(:changeset, :user => private_user))
613 way = create(:way, :changeset => create(:changeset, :user => user))
615 ## First test with the non-public user so should be rejected
617 basic_authorization(private_user.email, "test")
619 # create duplicate tag
620 tag_xml = XML::Node.new("tag")
621 tag_xml["k"] = "i_am_a_duplicate"
622 tag_xml["v"] = "foobar"
624 # add the tag into the existing xml
625 way_xml = private_way.to_xml
627 # add two copies of the tag
628 way_xml.find("//osm/way").first << tag_xml.copy(true) << tag_xml
632 put :update, :id => private_way.id
633 assert_response :forbidden,
634 "adding new duplicate tags to a way using a non-public user should fail with 'forbidden'"
636 ## Now test with the public user
638 basic_authorization(user.email, "test")
640 # create duplicate tag
641 tag_xml = XML::Node.new("tag")
642 tag_xml["k"] = "i_am_a_duplicate"
643 tag_xml["v"] = "foobar"
645 # add the tag into the existing xml
648 # add two copies of the tag
649 way_xml.find("//osm/way").first << tag_xml.copy(true) << tag_xml
653 put :update, :id => way.id
654 assert_response :bad_request,
655 "adding new duplicate tags to a way should fail with 'bad request'"
656 assert_equal "Element way/#{way.id} has duplicate tags with key i_am_a_duplicate", @response.body
660 # Try adding a new duplicate tags to a way.
661 # But be a bit subtle - use unicode decoding ambiguities to use different
662 # binary strings which have the same decoding.
663 def test_invalid_duplicate_tags
664 private_user = create(:user, :data_public => false)
665 private_changeset = create(:changeset, :user => private_user)
667 changeset = create(:changeset, :user => user)
669 ## First make sure that you can't with a non-public user
671 basic_authorization(private_user.email, "test")
673 # add the tag into the existing xml
674 way_str = "<osm><way changeset='#{private_changeset.id}'>"
675 way_str << "<tag k='addr:housenumber' v='1'/>"
676 way_str << "<tag k='addr:housenumber' v='2'/>"
677 way_str << "</way></osm>"
682 assert_response :forbidden,
683 "adding new duplicate tags to a way with a non-public user should fail with 'forbidden'"
685 ## Now do it with a public user
687 basic_authorization(user.email, "test")
689 # add the tag into the existing xml
690 way_str = "<osm><way changeset='#{changeset.id}'>"
691 way_str << "<tag k='addr:housenumber' v='1'/>"
692 way_str << "<tag k='addr:housenumber' v='2'/>"
693 way_str << "</way></osm>"
698 assert_response :bad_request,
699 "adding new duplicate tags to a way should fail with 'bad request'"
700 assert_equal "Element way/ has duplicate tags with key addr:housenumber", @response.body
704 # test that a call to ways_for_node returns all ways that contain the node
705 # and none that don't.
706 def test_ways_for_node
710 create(:way_node, :way => way1, :node => node)
711 create(:way_node, :way => way2, :node => node)
712 # create an unrelated way
713 create(:way_with_nodes, :nodes_count => 2)
714 # create a way which used to use the node
715 way3_v1 = create(:old_way, :version => 1)
716 _way3_v2 = create(:old_way, :current_way => way3_v1.current_way, :version => 2)
717 create(:old_way_node, :old_way => way3_v1, :node => node)
719 get :ways_for_node, :id => node.id
720 assert_response :success
721 ways_xml = XML::Parser.string(@response.body).parse
722 assert_not_nil ways_xml, "failed to parse ways_for_node response"
724 # check that the set of IDs match expectations
725 expected_way_ids = [way1.id,
727 found_way_ids = ways_xml.find("//osm/way").collect { |w| w["id"].to_i }
728 assert_equal expected_way_ids.sort, found_way_ids.sort,
729 "expected ways for node #{node.id} did not match found"
731 # check the full ways to ensure we're not missing anything
732 expected_way_ids.each do |id|
733 way_xml = ways_xml.find("//osm/way[@id='#{id}']").first
734 assert_ways_are_equal(Way.find(id),
735 Way.from_xml_node(way_xml))
740 # update the changeset_id of a way element
741 def update_changeset(xml, changeset_id)
742 xml_attr_rewrite(xml, "changeset", changeset_id)
746 # update an attribute in the way element
747 def xml_attr_rewrite(xml, name, value)
748 xml.find("//osm/way").first[name] = value.to_s
753 # replace a node in a way element
754 def xml_replace_node(xml, old_node, new_node)
755 xml.find("//osm/way/nd[@ref='#{old_node}']").first["ref"] = new_node.to_s