2 require "way_controller"
4 class WayControllerTest < ActionController::TestCase
6 # test all routes which lead to this controller
9 { :path => "/api/0.6/way/create", :method => :put },
10 { :controller => "way", :action => "create" }
13 { :path => "/api/0.6/way/1/full", :method => :get },
14 { :controller => "way", :action => "full", :id => "1" }
17 { :path => "/api/0.6/way/1", :method => :get },
18 { :controller => "way", :action => "read", :id => "1" }
21 { :path => "/api/0.6/way/1", :method => :put },
22 { :controller => "way", :action => "update", :id => "1" }
25 { :path => "/api/0.6/way/1", :method => :delete },
26 { :controller => "way", :action => "delete", :id => "1" }
29 { :path => "/api/0.6/ways", :method => :get },
30 { :controller => "way", :action => "ways" }
34 # -------------------------------------
36 # -------------------------------------
39 # check that a visible way is returned properly
40 get :read, :params => { :id => create(:way).id }
41 assert_response :success
43 # check that an invisible way is not returned
44 get :read, :params => { :id => create(:way, :deleted).id }
47 # check chat a non-existent way is not returned
48 get :read, :params => { :id => 0 }
49 assert_response :not_found
53 # check the "full" mode
56 get :full, :params => { :id => way.id }
58 # full call should say "gone" for non-visible ways...
64 # otherwise it should say success
65 assert_response :success
67 # Check the way is correctly returned
68 assert_select "osm way[id='#{way.id}'][version='#{way.version}'][visible='#{way.visible}']", 1
70 # check that each node in the way appears once in the output as a
71 # reference and as the node element.
73 count = (way.nodes - (way.nodes - [n])).length
74 assert_select "osm way nd[ref='#{n.id}']", count
75 assert_select "osm node[id='#{n.id}'][version='#{n.version}'][lat='#{format('%.7f', n.lat)}'][lon='#{format('%.7f', n.lon)}']", 1
81 # test fetching multiple ways
84 way2 = create(:way, :deleted)
88 # check error when no parameter provided
90 assert_response :bad_request
92 # check error when no parameter value provided
93 get :ways, :params => { :ways => "" }
94 assert_response :bad_request
97 get :ways, :params => { :ways => "#{way1.id},#{way2.id},#{way3.id},#{way4.id}" }
98 assert_response :success
99 assert_select "osm" do
100 assert_select "way", :count => 4
101 assert_select "way[id='#{way1.id}'][visible='true']", :count => 1
102 assert_select "way[id='#{way2.id}'][visible='false']", :count => 1
103 assert_select "way[id='#{way3.id}'][visible='true']", :count => 1
104 assert_select "way[id='#{way4.id}'][visible='true']", :count => 1
107 # check error when a non-existent way is included
108 get :ways, :params => { :ways => "#{way1.id},#{way2.id},#{way3.id},#{way4.id},400" }
109 assert_response :not_found
112 # -------------------------------------
113 # Test simple way creation.
114 # -------------------------------------
117 node1 = create(:node)
118 node2 = create(:node)
119 private_user = create(:user, :data_public => false)
120 private_changeset = create(:changeset, :user => private_user)
122 changeset = create(:changeset, :user => user)
124 ## First check that it fails when creating a way using a non-public user
125 basic_authorization private_user.email, "test"
127 # use the first user's open changeset
128 changeset_id = private_changeset.id
130 # create a way with pre-existing nodes
131 content "<osm><way changeset='#{changeset_id}'>" +
132 "<nd ref='#{node1.id}'/><nd ref='#{node2.id}'/>" +
133 "<tag k='test' v='yes' /></way></osm>"
136 assert_response :forbidden,
137 "way upload did not return forbidden status"
139 ## Now use a public user
140 basic_authorization user.email, "test"
142 # use the first user's open changeset
143 changeset_id = changeset.id
145 # create a way with pre-existing nodes
146 content "<osm><way changeset='#{changeset_id}'>" +
147 "<nd ref='#{node1.id}'/><nd ref='#{node2.id}'/>" +
148 "<tag k='test' v='yes' /></way></osm>"
151 assert_response :success,
152 "way upload did not return success status"
153 # read id of created way and search for it
154 wayid = @response.body
155 checkway = Way.find(wayid)
156 assert_not_nil checkway,
157 "uploaded way not found in data base after upload"
159 assert_equal checkway.nds.length, 2,
160 "saved way does not contain exactly one node"
161 assert_equal checkway.nds[0], node1.id,
162 "saved way does not contain the right node on pos 0"
163 assert_equal checkway.nds[1], node2.id,
164 "saved way does not contain the right node on pos 1"
165 assert_equal checkway.changeset_id, changeset_id,
166 "saved way does not belong to the correct changeset"
167 assert_equal user.id, checkway.changeset.user_id,
168 "saved way does not belong to user that created it"
169 assert_equal true, checkway.visible,
170 "saved way is not visible"
173 # -------------------------------------
174 # Test creating some invalid ways.
175 # -------------------------------------
177 def test_create_invalid
179 private_user = create(:user, :data_public => false)
180 private_open_changeset = create(:changeset, :user => private_user)
181 private_closed_changeset = create(:changeset, :closed, :user => private_user)
183 open_changeset = create(:changeset, :user => user)
184 closed_changeset = create(:changeset, :closed, :user => user)
186 ## First test with a private user to make sure that they are not authorized
187 basic_authorization private_user.email, "test"
189 # use the first user's open changeset
190 # create a way with non-existing node
191 content "<osm><way changeset='#{private_open_changeset.id}'>" +
192 "<nd ref='0'/><tag k='test' v='yes' /></way></osm>"
195 assert_response :forbidden,
196 "way upload with invalid node using a private user did not return 'forbidden'"
198 # create a way with no nodes
199 content "<osm><way changeset='#{private_open_changeset.id}'>" +
200 "<tag k='test' v='yes' /></way></osm>"
203 assert_response :forbidden,
204 "way upload with no node using a private userdid not return 'forbidden'"
206 # create a way inside a closed changeset
207 content "<osm><way changeset='#{private_closed_changeset.id}'>" +
208 "<nd ref='#{node.id}'/></way></osm>"
211 assert_response :forbidden,
212 "way upload to closed changeset with a private user did not return 'forbidden'"
214 ## Now test with a public user
215 basic_authorization user.email, "test"
217 # use the first user's open changeset
218 # create a way with non-existing node
219 content "<osm><way changeset='#{open_changeset.id}'>" +
220 "<nd ref='0'/><tag k='test' v='yes' /></way></osm>"
223 assert_response :precondition_failed,
224 "way upload with invalid node did not return 'precondition failed'"
225 assert_equal "Precondition failed: Way requires the nodes with id in (0), which either do not exist, or are not visible.", @response.body
227 # create a way with no nodes
228 content "<osm><way changeset='#{open_changeset.id}'>" +
229 "<tag k='test' v='yes' /></way></osm>"
232 assert_response :precondition_failed,
233 "way upload with no node did not return 'precondition failed'"
234 assert_equal "Precondition failed: Cannot create way: data is invalid.", @response.body
236 # create a way inside a closed changeset
237 content "<osm><way changeset='#{closed_changeset.id}'>" +
238 "<nd ref='#{node.id}'/></way></osm>"
241 assert_response :conflict,
242 "way upload to closed changeset did not return 'conflict'"
244 # create a way with a tag which is too long
245 content "<osm><way changeset='#{open_changeset.id}'>" +
246 "<nd ref='#{node.id}'/>" +
247 "<tag k='foo' v='#{'x' * 256}'/>" +
251 assert_response :bad_request,
252 "way upload to with too long tag did not return 'bad_request'"
255 # -------------------------------------
256 # Test deleting ways.
257 # -------------------------------------
260 private_user = create(:user, :data_public => false)
261 private_open_changeset = create(:changeset, :user => private_user)
262 private_closed_changeset = create(:changeset, :closed, :user => private_user)
263 private_way = create(:way, :changeset => private_open_changeset)
264 private_deleted_way = create(:way, :deleted, :changeset => private_open_changeset)
265 private_used_way = create(:way, :changeset => private_open_changeset)
266 create(:relation_member, :member => private_used_way)
268 open_changeset = create(:changeset, :user => user)
269 closed_changeset = create(:changeset, :closed, :user => user)
270 way = create(:way, :changeset => open_changeset)
271 deleted_way = create(:way, :deleted, :changeset => open_changeset)
272 used_way = create(:way, :changeset => open_changeset)
273 relation_member = create(:relation_member, :member => used_way)
274 relation = relation_member.relation
276 # first try to delete way without auth
277 delete :delete, :params => { :id => way.id }
278 assert_response :unauthorized
280 # now set auth using the private user
281 basic_authorization(private_user.email, "test")
283 # this shouldn't work as with the 0.6 api we need pay load to delete
284 delete :delete, :params => { :id => private_way.id }
285 assert_response :forbidden
287 # Now try without having a changeset
288 content "<osm><way id='#{private_way.id}'/></osm>"
289 delete :delete, :params => { :id => private_way.id }
290 assert_response :forbidden
292 # try to delete with an invalid (closed) changeset
293 content update_changeset(private_way.to_xml, private_closed_changeset.id)
294 delete :delete, :params => { :id => private_way.id }
295 assert_response :forbidden
297 # try to delete with an invalid (non-existent) changeset
298 content update_changeset(private_way.to_xml, 0)
299 delete :delete, :params => { :id => private_way.id }
300 assert_response :forbidden
302 # Now try with a valid changeset
303 content private_way.to_xml
304 delete :delete, :params => { :id => private_way.id }
305 assert_response :forbidden
307 # check the returned value - should be the new version number
308 # valid delete should return the new version number, which should
309 # be greater than the old version number
310 # assert @response.body.to_i > current_ways(:visible_way).version,
311 # "delete request should return a new version number for way"
313 # this won't work since the way is already deleted
314 content private_deleted_way.to_xml
315 delete :delete, :params => { :id => private_deleted_way.id }
316 assert_response :forbidden
318 # this shouldn't work as the way is used in a relation
319 content private_used_way.to_xml
320 delete :delete, :params => { :id => private_used_way.id }
321 assert_response :forbidden,
322 "shouldn't be able to delete a way used in a relation (#{@response.body}), when done by a private user"
324 # this won't work since the way never existed
325 delete :delete, :params => { :id => 0 }
326 assert_response :forbidden
328 ### Now check with a public user
330 basic_authorization(user.email, "test")
332 # this shouldn't work as with the 0.6 api we need pay load to delete
333 delete :delete, :params => { :id => way.id }
334 assert_response :bad_request
336 # Now try without having a changeset
337 content "<osm><way id='#{way.id}'/></osm>"
338 delete :delete, :params => { :id => way.id }
339 assert_response :bad_request
341 # try to delete with an invalid (closed) changeset
342 content update_changeset(way.to_xml, closed_changeset.id)
343 delete :delete, :params => { :id => way.id }
344 assert_response :conflict
346 # try to delete with an invalid (non-existent) changeset
347 content update_changeset(way.to_xml, 0)
348 delete :delete, :params => { :id => way.id }
349 assert_response :conflict
351 # Now try with a valid changeset
353 delete :delete, :params => { :id => way.id }
354 assert_response :success
356 # check the returned value - should be the new version number
357 # valid delete should return the new version number, which should
358 # be greater than the old version number
359 assert @response.body.to_i > way.version,
360 "delete request should return a new version number for way"
362 # this won't work since the way is already deleted
363 content deleted_way.to_xml
364 delete :delete, :params => { :id => deleted_way.id }
365 assert_response :gone
367 # this shouldn't work as the way is used in a relation
368 content used_way.to_xml
369 delete :delete, :params => { :id => used_way.id }
370 assert_response :precondition_failed,
371 "shouldn't be able to delete a way used in a relation (#{@response.body})"
372 assert_equal "Precondition failed: Way #{used_way.id} is still used by relations #{relation.id}.", @response.body
374 # this won't work since the way never existed
375 delete :delete, :params => { :id => 0 }
376 assert_response :not_found
380 # tests whether the API works and prevents incorrect use while trying
383 private_user = create(:user, :data_public => false)
384 private_way = create(:way, :changeset => create(:changeset, :user => private_user))
386 way = create(:way, :changeset => create(:changeset, :user => user))
388 create(:way_node, :way => private_way, :node => node)
389 create(:way_node, :way => way, :node => node)
391 ## First test with no user credentials
392 # try and update a way without authorisation
394 put :update, :params => { :id => way.id }
395 assert_response :unauthorized
397 ## Second test with the private user
400 basic_authorization(private_user.email, "test")
402 ## trying to break changesets
404 # try and update in someone else's changeset
405 content update_changeset(private_way.to_xml,
406 create(:changeset).id)
407 put :update, :params => { :id => private_way.id }
408 assert_require_public_data "update with other user's changeset should be forbidden when date isn't public"
410 # try and update in a closed changeset
411 content update_changeset(private_way.to_xml,
412 create(:changeset, :closed, :user => private_user).id)
413 put :update, :params => { :id => private_way.id }
414 assert_require_public_data "update with closed changeset should be forbidden, when data isn't public"
416 # try and update in a non-existant changeset
417 content update_changeset(private_way.to_xml, 0)
418 put :update, :params => { :id => private_way.id }
419 assert_require_public_data("update with changeset=0 should be forbidden, when data isn't public")
421 ## try and submit invalid updates
422 content xml_replace_node(private_way.to_xml, node.id, 9999)
423 put :update, :params => { :id => private_way.id }
424 assert_require_public_data "way with non-existent node should be forbidden, when data isn't public"
426 content xml_replace_node(private_way.to_xml, node.id, create(:node, :deleted).id)
427 put :update, :params => { :id => private_way.id }
428 assert_require_public_data "way with deleted node should be forbidden, when data isn't public"
430 ## finally, produce a good request which will still not work
431 content private_way.to_xml
432 put :update, :params => { :id => private_way.id }
433 assert_require_public_data "should have failed with a forbidden when data isn't public"
435 ## Finally test with the public user
438 basic_authorization(user.email, "test")
440 ## trying to break changesets
442 # try and update in someone else's changeset
443 content update_changeset(way.to_xml,
444 create(:changeset).id)
445 put :update, :params => { :id => way.id }
446 assert_response :conflict, "update with other user's changeset should be rejected"
448 # try and update in a closed changeset
449 content update_changeset(way.to_xml,
450 create(:changeset, :closed, :user => user).id)
451 put :update, :params => { :id => way.id }
452 assert_response :conflict, "update with closed changeset should be rejected"
454 # try and update in a non-existant changeset
455 content update_changeset(way.to_xml, 0)
456 put :update, :params => { :id => way.id }
457 assert_response :conflict, "update with changeset=0 should be rejected"
459 ## try and submit invalid updates
460 content xml_replace_node(way.to_xml, node.id, 9999)
461 put :update, :params => { :id => way.id }
462 assert_response :precondition_failed, "way with non-existent node should be rejected"
464 content xml_replace_node(way.to_xml, node.id, create(:node, :deleted).id)
465 put :update, :params => { :id => way.id }
466 assert_response :precondition_failed, "way with deleted node should be rejected"
468 ## next, attack the versioning
469 current_way_version = way.version
471 # try and submit a version behind
472 content xml_attr_rewrite(way.to_xml,
473 "version", current_way_version - 1)
474 put :update, :params => { :id => way.id }
475 assert_response :conflict, "should have failed on old version number"
477 # try and submit a version ahead
478 content xml_attr_rewrite(way.to_xml,
479 "version", current_way_version + 1)
480 put :update, :params => { :id => way.id }
481 assert_response :conflict, "should have failed on skipped version number"
483 # try and submit total crap in the version field
484 content xml_attr_rewrite(way.to_xml,
485 "version", "p1r4t3s!")
486 put :update, :params => { :id => way.id }
487 assert_response :conflict,
488 "should not be able to put 'p1r4at3s!' in the version field"
490 ## try an update with the wrong ID
491 content create(:way).to_xml
492 put :update, :params => { :id => way.id }
493 assert_response :bad_request,
494 "should not be able to update a way with a different ID from the XML"
496 ## try an update with a minimal valid XML doc which isn't a well-formed OSM doc.
498 put :update, :params => { :id => way.id }
499 assert_response :bad_request,
500 "should not be able to update a way with non-OSM XML doc."
502 ## finally, produce a good request which should work
504 put :update, :params => { :id => way.id }
505 assert_response :success, "a valid update request failed"
508 # ------------------------------------------------------------
510 # ------------------------------------------------------------
513 # Try adding a new tag to a way
515 private_user = create(:user, :data_public => false)
516 private_way = create(:way_with_nodes, :nodes_count => 2, :changeset => create(:changeset, :user => private_user))
518 way = create(:way_with_nodes, :nodes_count => 2, :changeset => create(:changeset, :user => user))
520 ## Try with the non-public user
522 basic_authorization(private_user.email, "test")
524 # add an identical tag to the way
525 tag_xml = XML::Node.new("tag")
529 # add the tag into the existing xml
530 way_xml = private_way.to_xml
531 way_xml.find("//osm/way").first << tag_xml
535 put :update, :params => { :id => private_way.id }
536 assert_response :forbidden,
537 "adding a duplicate tag to a way for a non-public should fail with 'forbidden'"
539 ## Now try with the public user
541 basic_authorization(user.email, "test")
543 # add an identical tag to the way
544 tag_xml = XML::Node.new("tag")
548 # add the tag into the existing xml
550 way_xml.find("//osm/way").first << tag_xml
554 put :update, :params => { :id => way.id }
555 assert_response :success,
556 "adding a new tag to a way should succeed"
557 assert_equal way.version + 1, @response.body.to_i
561 # Try adding a duplicate of an existing tag to a way
562 def test_add_duplicate_tags
563 private_user = create(:user, :data_public => false)
564 private_way = create(:way, :changeset => create(:changeset, :user => private_user))
565 private_existing_tag = create(:way_tag, :way => private_way)
567 way = create(:way, :changeset => create(:changeset, :user => user))
568 existing_tag = create(:way_tag, :way => way)
570 ## Try with the non-public user
572 basic_authorization(private_user.email, "test")
574 # add an identical tag to the way
575 tag_xml = XML::Node.new("tag")
576 tag_xml["k"] = private_existing_tag.k
577 tag_xml["v"] = private_existing_tag.v
579 # add the tag into the existing xml
580 way_xml = private_way.to_xml
581 way_xml.find("//osm/way").first << tag_xml
585 put :update, :params => { :id => private_way.id }
586 assert_response :forbidden,
587 "adding a duplicate tag to a way for a non-public should fail with 'forbidden'"
589 ## Now try with the public user
591 basic_authorization(user.email, "test")
593 # add an identical tag to the way
594 tag_xml = XML::Node.new("tag")
595 tag_xml["k"] = existing_tag.k
596 tag_xml["v"] = existing_tag.v
598 # add the tag into the existing xml
600 way_xml.find("//osm/way").first << tag_xml
604 put :update, :params => { :id => way.id }
605 assert_response :bad_request,
606 "adding a duplicate tag to a way should fail with 'bad request'"
607 assert_equal "Element way/#{way.id} has duplicate tags with key #{existing_tag.k}", @response.body
611 # Try adding a new duplicate tags to a way
612 def test_new_duplicate_tags
613 private_user = create(:user, :data_public => false)
614 private_way = create(:way, :changeset => create(:changeset, :user => private_user))
616 way = create(:way, :changeset => create(:changeset, :user => user))
618 ## First test with the non-public user so should be rejected
620 basic_authorization(private_user.email, "test")
622 # create duplicate tag
623 tag_xml = XML::Node.new("tag")
624 tag_xml["k"] = "i_am_a_duplicate"
625 tag_xml["v"] = "foobar"
627 # add the tag into the existing xml
628 way_xml = private_way.to_xml
630 # add two copies of the tag
631 way_xml.find("//osm/way").first << tag_xml.copy(true) << tag_xml
635 put :update, :params => { :id => private_way.id }
636 assert_response :forbidden,
637 "adding new duplicate tags to a way using a non-public user should fail with 'forbidden'"
639 ## Now test with the public user
641 basic_authorization(user.email, "test")
643 # create duplicate tag
644 tag_xml = XML::Node.new("tag")
645 tag_xml["k"] = "i_am_a_duplicate"
646 tag_xml["v"] = "foobar"
648 # add the tag into the existing xml
651 # add two copies of the tag
652 way_xml.find("//osm/way").first << tag_xml.copy(true) << tag_xml
656 put :update, :params => { :id => way.id }
657 assert_response :bad_request,
658 "adding new duplicate tags to a way should fail with 'bad request'"
659 assert_equal "Element way/#{way.id} has duplicate tags with key i_am_a_duplicate", @response.body
663 # Try adding a new duplicate tags to a way.
664 # But be a bit subtle - use unicode decoding ambiguities to use different
665 # binary strings which have the same decoding.
666 def test_invalid_duplicate_tags
667 private_user = create(:user, :data_public => false)
668 private_changeset = create(:changeset, :user => private_user)
670 changeset = create(:changeset, :user => user)
672 ## First make sure that you can't with a non-public user
674 basic_authorization(private_user.email, "test")
676 # add the tag into the existing xml
677 way_str = "<osm><way changeset='#{private_changeset.id}'>"
678 way_str << "<tag k='addr:housenumber' v='1'/>"
679 way_str << "<tag k='addr:housenumber' v='2'/>"
680 way_str << "</way></osm>"
685 assert_response :forbidden,
686 "adding new duplicate tags to a way with a non-public user should fail with 'forbidden'"
688 ## Now do it with a public user
690 basic_authorization(user.email, "test")
692 # add the tag into the existing xml
693 way_str = "<osm><way changeset='#{changeset.id}'>"
694 way_str << "<tag k='addr:housenumber' v='1'/>"
695 way_str << "<tag k='addr:housenumber' v='2'/>"
696 way_str << "</way></osm>"
701 assert_response :bad_request,
702 "adding new duplicate tags to a way should fail with 'bad request'"
703 assert_equal "Element way/ has duplicate tags with key addr:housenumber", @response.body
707 # test that a call to ways_for_node returns all ways that contain the node
708 # and none that don't.
709 def test_ways_for_node
713 create(:way_node, :way => way1, :node => node)
714 create(:way_node, :way => way2, :node => node)
715 # create an unrelated way
716 create(:way_with_nodes, :nodes_count => 2)
717 # create a way which used to use the node
718 way3_v1 = create(:old_way, :version => 1)
719 _way3_v2 = create(:old_way, :current_way => way3_v1.current_way, :version => 2)
720 create(:old_way_node, :old_way => way3_v1, :node => node)
722 get :ways_for_node, :params => { :id => node.id }
723 assert_response :success
724 ways_xml = XML::Parser.string(@response.body).parse
725 assert_not_nil ways_xml, "failed to parse ways_for_node response"
727 # check that the set of IDs match expectations
728 expected_way_ids = [way1.id,
730 found_way_ids = ways_xml.find("//osm/way").collect { |w| w["id"].to_i }
731 assert_equal expected_way_ids.sort, found_way_ids.sort,
732 "expected ways for node #{node.id} did not match found"
734 # check the full ways to ensure we're not missing anything
735 expected_way_ids.each do |id|
736 way_xml = ways_xml.find("//osm/way[@id='#{id}']").first
737 assert_ways_are_equal(Way.find(id),
738 Way.from_xml_node(way_xml))
743 # update the changeset_id of a way element
744 def update_changeset(xml, changeset_id)
745 xml_attr_rewrite(xml, "changeset", changeset_id)
749 # update an attribute in the way element
750 def xml_attr_rewrite(xml, name, value)
751 xml.find("//osm/way").first[name] = value.to_s
756 # replace a node in a way element
757 def xml_replace_node(xml, old_node, new_node)
758 xml.find("//osm/way/nd[@ref='#{old_node}']").first["ref"] = new_node.to_s