3 class WaysControllerTest < ActionController::TestCase
5 # test all routes which lead to this controller
8 { :path => "/api/0.6/way/create", :method => :put },
9 { :controller => "ways", :action => "create" }
12 { :path => "/api/0.6/way/1/full", :method => :get },
13 { :controller => "ways", :action => "full", :id => "1" }
16 { :path => "/api/0.6/way/1", :method => :get },
17 { :controller => "ways", :action => "read", :id => "1" }
20 { :path => "/api/0.6/way/1", :method => :put },
21 { :controller => "ways", :action => "update", :id => "1" }
24 { :path => "/api/0.6/way/1", :method => :delete },
25 { :controller => "ways", :action => "delete", :id => "1" }
28 { :path => "/api/0.6/ways", :method => :get },
29 { :controller => "ways", :action => "ways" }
33 # -------------------------------------
35 # -------------------------------------
38 # check that a visible way is returned properly
39 get :read, :params => { :id => create(:way).id }
40 assert_response :success
42 # check that an invisible way is not returned
43 get :read, :params => { :id => create(:way, :deleted).id }
46 # check chat a non-existent way is not returned
47 get :read, :params => { :id => 0 }
48 assert_response :not_found
52 # check the "full" mode
55 get :full, :params => { :id => way.id }
57 # full call should say "gone" for non-visible ways...
63 # otherwise it should say success
64 assert_response :success
66 # Check the way is correctly returned
67 assert_select "osm way[id='#{way.id}'][version='#{way.version}'][visible='#{way.visible}']", 1
69 # check that each node in the way appears once in the output as a
70 # reference and as the node element.
72 count = (way.nodes - (way.nodes - [n])).length
73 assert_select "osm way nd[ref='#{n.id}']", count
74 assert_select "osm node[id='#{n.id}'][version='#{n.version}'][lat='#{format('%.7f', n.lat)}'][lon='#{format('%.7f', n.lon)}']", 1
80 # test fetching multiple ways
83 way2 = create(:way, :deleted)
87 # check error when no parameter provided
89 assert_response :bad_request
91 # check error when no parameter value provided
92 get :ways, :params => { :ways => "" }
93 assert_response :bad_request
96 get :ways, :params => { :ways => "#{way1.id},#{way2.id},#{way3.id},#{way4.id}" }
97 assert_response :success
98 assert_select "osm" do
99 assert_select "way", :count => 4
100 assert_select "way[id='#{way1.id}'][visible='true']", :count => 1
101 assert_select "way[id='#{way2.id}'][visible='false']", :count => 1
102 assert_select "way[id='#{way3.id}'][visible='true']", :count => 1
103 assert_select "way[id='#{way4.id}'][visible='true']", :count => 1
106 # check error when a non-existent way is included
107 get :ways, :params => { :ways => "#{way1.id},#{way2.id},#{way3.id},#{way4.id},0" }
108 assert_response :not_found
111 # -------------------------------------
112 # Test simple way creation.
113 # -------------------------------------
116 node1 = create(:node)
117 node2 = create(:node)
118 private_user = create(:user, :data_public => false)
119 private_changeset = create(:changeset, :user => private_user)
121 changeset = create(:changeset, :user => user)
123 ## First check that it fails when creating a way using a non-public user
124 basic_authorization private_user.email, "test"
126 # use the first user's open changeset
127 changeset_id = private_changeset.id
129 # create a way with pre-existing nodes
130 content "<osm><way changeset='#{changeset_id}'>" \
131 "<nd ref='#{node1.id}'/><nd ref='#{node2.id}'/>" \
132 "<tag k='test' v='yes' /></way></osm>"
135 assert_response :forbidden,
136 "way upload did not return forbidden status"
138 ## Now use a public user
139 basic_authorization user.email, "test"
141 # use the first user's open changeset
142 changeset_id = changeset.id
144 # create a way with pre-existing nodes
145 content "<osm><way changeset='#{changeset_id}'>" \
146 "<nd ref='#{node1.id}'/><nd ref='#{node2.id}'/>" \
147 "<tag k='test' v='yes' /></way></osm>"
150 assert_response :success,
151 "way upload did not return success status"
152 # read id of created way and search for it
153 wayid = @response.body
154 checkway = Way.find(wayid)
155 assert_not_nil checkway,
156 "uploaded way not found in data base after upload"
158 assert_equal checkway.nds.length, 2,
159 "saved way does not contain exactly one node"
160 assert_equal checkway.nds[0], node1.id,
161 "saved way does not contain the right node on pos 0"
162 assert_equal checkway.nds[1], node2.id,
163 "saved way does not contain the right node on pos 1"
164 assert_equal checkway.changeset_id, changeset_id,
165 "saved way does not belong to the correct changeset"
166 assert_equal user.id, checkway.changeset.user_id,
167 "saved way does not belong to user that created it"
168 assert_equal true, checkway.visible,
169 "saved way is not visible"
172 # -------------------------------------
173 # Test creating some invalid ways.
174 # -------------------------------------
176 def test_create_invalid
178 private_user = create(:user, :data_public => false)
179 private_open_changeset = create(:changeset, :user => private_user)
180 private_closed_changeset = create(:changeset, :closed, :user => private_user)
182 open_changeset = create(:changeset, :user => user)
183 closed_changeset = create(:changeset, :closed, :user => user)
185 ## First test with a private user to make sure that they are not authorized
186 basic_authorization private_user.email, "test"
188 # use the first user's open changeset
189 # create a way with non-existing node
190 content "<osm><way changeset='#{private_open_changeset.id}'>" \
191 "<nd ref='0'/><tag k='test' v='yes' /></way></osm>"
194 assert_response :forbidden,
195 "way upload with invalid node using a private user did not return 'forbidden'"
197 # create a way with no nodes
198 content "<osm><way changeset='#{private_open_changeset.id}'>" \
199 "<tag k='test' v='yes' /></way></osm>"
202 assert_response :forbidden,
203 "way upload with no node using a private userdid not return 'forbidden'"
205 # create a way inside a closed changeset
206 content "<osm><way changeset='#{private_closed_changeset.id}'>" \
207 "<nd ref='#{node.id}'/></way></osm>"
210 assert_response :forbidden,
211 "way upload to closed changeset with a private user did not return 'forbidden'"
213 ## Now test with a public user
214 basic_authorization user.email, "test"
216 # use the first user's open changeset
217 # create a way with non-existing node
218 content "<osm><way changeset='#{open_changeset.id}'>" \
219 "<nd ref='0'/><tag k='test' v='yes' /></way></osm>"
222 assert_response :precondition_failed,
223 "way upload with invalid node did not return 'precondition failed'"
224 assert_equal "Precondition failed: Way requires the nodes with id in (0), which either do not exist, or are not visible.", @response.body
226 # create a way with no nodes
227 content "<osm><way changeset='#{open_changeset.id}'>" \
228 "<tag k='test' v='yes' /></way></osm>"
231 assert_response :precondition_failed,
232 "way upload with no node did not return 'precondition failed'"
233 assert_equal "Precondition failed: Cannot create way: data is invalid.", @response.body
235 # create a way inside a closed changeset
236 content "<osm><way changeset='#{closed_changeset.id}'>" \
237 "<nd ref='#{node.id}'/></way></osm>"
240 assert_response :conflict,
241 "way upload to closed changeset did not return 'conflict'"
243 # create a way with a tag which is too long
244 content "<osm><way changeset='#{open_changeset.id}'>" \
245 "<nd ref='#{node.id}'/>" \
246 "<tag k='foo' v='#{'x' * 256}'/>" \
250 assert_response :bad_request,
251 "way upload to with too long tag did not return 'bad_request'"
254 # -------------------------------------
255 # Test deleting ways.
256 # -------------------------------------
259 private_user = create(:user, :data_public => false)
260 private_open_changeset = create(:changeset, :user => private_user)
261 private_closed_changeset = create(:changeset, :closed, :user => private_user)
262 private_way = create(:way, :changeset => private_open_changeset)
263 private_deleted_way = create(:way, :deleted, :changeset => private_open_changeset)
264 private_used_way = create(:way, :changeset => private_open_changeset)
265 create(:relation_member, :member => private_used_way)
267 open_changeset = create(:changeset, :user => user)
268 closed_changeset = create(:changeset, :closed, :user => user)
269 way = create(:way, :changeset => open_changeset)
270 deleted_way = create(:way, :deleted, :changeset => open_changeset)
271 used_way = create(:way, :changeset => open_changeset)
272 relation_member = create(:relation_member, :member => used_way)
273 relation = relation_member.relation
275 # first try to delete way without auth
276 delete :delete, :params => { :id => way.id }
277 assert_response :unauthorized
279 # now set auth using the private user
280 basic_authorization private_user.email, "test"
282 # this shouldn't work as with the 0.6 api we need pay load to delete
283 delete :delete, :params => { :id => private_way.id }
284 assert_response :forbidden
286 # Now try without having a changeset
287 content "<osm><way id='#{private_way.id}'/></osm>"
288 delete :delete, :params => { :id => private_way.id }
289 assert_response :forbidden
291 # try to delete with an invalid (closed) changeset
292 content update_changeset(private_way.to_xml, private_closed_changeset.id)
293 delete :delete, :params => { :id => private_way.id }
294 assert_response :forbidden
296 # try to delete with an invalid (non-existent) changeset
297 content update_changeset(private_way.to_xml, 0)
298 delete :delete, :params => { :id => private_way.id }
299 assert_response :forbidden
301 # Now try with a valid changeset
302 content private_way.to_xml
303 delete :delete, :params => { :id => private_way.id }
304 assert_response :forbidden
306 # check the returned value - should be the new version number
307 # valid delete should return the new version number, which should
308 # be greater than the old version number
309 # assert @response.body.to_i > current_ways(:visible_way).version,
310 # "delete request should return a new version number for way"
312 # this won't work since the way is already deleted
313 content private_deleted_way.to_xml
314 delete :delete, :params => { :id => private_deleted_way.id }
315 assert_response :forbidden
317 # this shouldn't work as the way is used in a relation
318 content private_used_way.to_xml
319 delete :delete, :params => { :id => private_used_way.id }
320 assert_response :forbidden,
321 "shouldn't be able to delete a way used in a relation (#{@response.body}), when done by a private user"
323 # this won't work since the way never existed
324 delete :delete, :params => { :id => 0 }
325 assert_response :forbidden
327 ### Now check with a public user
329 basic_authorization user.email, "test"
331 # this shouldn't work as with the 0.6 api we need pay load to delete
332 delete :delete, :params => { :id => way.id }
333 assert_response :bad_request
335 # Now try without having a changeset
336 content "<osm><way id='#{way.id}'/></osm>"
337 delete :delete, :params => { :id => way.id }
338 assert_response :bad_request
340 # try to delete with an invalid (closed) changeset
341 content update_changeset(way.to_xml, closed_changeset.id)
342 delete :delete, :params => { :id => way.id }
343 assert_response :conflict
345 # try to delete with an invalid (non-existent) changeset
346 content update_changeset(way.to_xml, 0)
347 delete :delete, :params => { :id => way.id }
348 assert_response :conflict
350 # Now try with a valid changeset
352 delete :delete, :params => { :id => way.id }
353 assert_response :success
355 # check the returned value - should be the new version number
356 # valid delete should return the new version number, which should
357 # be greater than the old version number
358 assert @response.body.to_i > way.version,
359 "delete request should return a new version number for way"
361 # this won't work since the way is already deleted
362 content deleted_way.to_xml
363 delete :delete, :params => { :id => deleted_way.id }
364 assert_response :gone
366 # this shouldn't work as the way is used in a relation
367 content used_way.to_xml
368 delete :delete, :params => { :id => used_way.id }
369 assert_response :precondition_failed,
370 "shouldn't be able to delete a way used in a relation (#{@response.body})"
371 assert_equal "Precondition failed: Way #{used_way.id} is still used by relations #{relation.id}.", @response.body
373 # this won't work since the way never existed
374 delete :delete, :params => { :id => 0 }
375 assert_response :not_found
379 # tests whether the API works and prevents incorrect use while trying
382 private_user = create(:user, :data_public => false)
383 private_way = create(:way, :changeset => create(:changeset, :user => private_user))
385 way = create(:way, :changeset => create(:changeset, :user => user))
387 create(:way_node, :way => private_way, :node => node)
388 create(:way_node, :way => way, :node => node)
390 ## First test with no user credentials
391 # try and update a way without authorisation
393 put :update, :params => { :id => way.id }
394 assert_response :unauthorized
396 ## Second test with the private user
399 basic_authorization private_user.email, "test"
401 ## trying to break changesets
403 # try and update in someone else's changeset
404 content update_changeset(private_way.to_xml,
405 create(:changeset).id)
406 put :update, :params => { :id => private_way.id }
407 assert_require_public_data "update with other user's changeset should be forbidden when date isn't public"
409 # try and update in a closed changeset
410 content update_changeset(private_way.to_xml,
411 create(:changeset, :closed, :user => private_user).id)
412 put :update, :params => { :id => private_way.id }
413 assert_require_public_data "update with closed changeset should be forbidden, when data isn't public"
415 # try and update in a non-existant changeset
416 content update_changeset(private_way.to_xml, 0)
417 put :update, :params => { :id => private_way.id }
418 assert_require_public_data("update with changeset=0 should be forbidden, when data isn't public")
420 ## try and submit invalid updates
421 content xml_replace_node(private_way.to_xml, node.id, 9999)
422 put :update, :params => { :id => private_way.id }
423 assert_require_public_data "way with non-existent node should be forbidden, when data isn't public"
425 content xml_replace_node(private_way.to_xml, node.id, create(:node, :deleted).id)
426 put :update, :params => { :id => private_way.id }
427 assert_require_public_data "way with deleted node should be forbidden, when data isn't public"
429 ## finally, produce a good request which will still not work
430 content private_way.to_xml
431 put :update, :params => { :id => private_way.id }
432 assert_require_public_data "should have failed with a forbidden when data isn't public"
434 ## Finally test with the public user
437 basic_authorization user.email, "test"
439 ## trying to break changesets
441 # try and update in someone else's changeset
442 content update_changeset(way.to_xml,
443 create(:changeset).id)
444 put :update, :params => { :id => way.id }
445 assert_response :conflict, "update with other user's changeset should be rejected"
447 # try and update in a closed changeset
448 content update_changeset(way.to_xml,
449 create(:changeset, :closed, :user => user).id)
450 put :update, :params => { :id => way.id }
451 assert_response :conflict, "update with closed changeset should be rejected"
453 # try and update in a non-existant changeset
454 content update_changeset(way.to_xml, 0)
455 put :update, :params => { :id => way.id }
456 assert_response :conflict, "update with changeset=0 should be rejected"
458 ## try and submit invalid updates
459 content xml_replace_node(way.to_xml, node.id, 9999)
460 put :update, :params => { :id => way.id }
461 assert_response :precondition_failed, "way with non-existent node should be rejected"
463 content xml_replace_node(way.to_xml, node.id, create(:node, :deleted).id)
464 put :update, :params => { :id => way.id }
465 assert_response :precondition_failed, "way with deleted node should be rejected"
467 ## next, attack the versioning
468 current_way_version = way.version
470 # try and submit a version behind
471 content xml_attr_rewrite(way.to_xml,
472 "version", current_way_version - 1)
473 put :update, :params => { :id => way.id }
474 assert_response :conflict, "should have failed on old version number"
476 # try and submit a version ahead
477 content xml_attr_rewrite(way.to_xml,
478 "version", current_way_version + 1)
479 put :update, :params => { :id => way.id }
480 assert_response :conflict, "should have failed on skipped version number"
482 # try and submit total crap in the version field
483 content xml_attr_rewrite(way.to_xml,
484 "version", "p1r4t3s!")
485 put :update, :params => { :id => way.id }
486 assert_response :conflict,
487 "should not be able to put 'p1r4at3s!' in the version field"
489 ## try an update with the wrong ID
490 content create(:way).to_xml
491 put :update, :params => { :id => way.id }
492 assert_response :bad_request,
493 "should not be able to update a way with a different ID from the XML"
495 ## try an update with a minimal valid XML doc which isn't a well-formed OSM doc.
497 put :update, :params => { :id => way.id }
498 assert_response :bad_request,
499 "should not be able to update a way with non-OSM XML doc."
501 ## finally, produce a good request which should work
503 put :update, :params => { :id => way.id }
504 assert_response :success, "a valid update request failed"
507 # ------------------------------------------------------------
509 # ------------------------------------------------------------
512 # Try adding a new tag to a way
514 private_user = create(:user, :data_public => false)
515 private_way = create(:way_with_nodes, :nodes_count => 2, :changeset => create(:changeset, :user => private_user))
517 way = create(:way_with_nodes, :nodes_count => 2, :changeset => create(:changeset, :user => user))
519 ## Try with the non-public user
521 basic_authorization private_user.email, "test"
523 # add an identical tag to the way
524 tag_xml = XML::Node.new("tag")
528 # add the tag into the existing xml
529 way_xml = private_way.to_xml
530 way_xml.find("//osm/way").first << tag_xml
534 put :update, :params => { :id => private_way.id }
535 assert_response :forbidden,
536 "adding a duplicate tag to a way for a non-public should fail with 'forbidden'"
538 ## Now try with the public user
540 basic_authorization user.email, "test"
542 # add an identical tag to the way
543 tag_xml = XML::Node.new("tag")
547 # add the tag into the existing xml
549 way_xml.find("//osm/way").first << tag_xml
553 put :update, :params => { :id => way.id }
554 assert_response :success,
555 "adding a new tag to a way should succeed"
556 assert_equal way.version + 1, @response.body.to_i
560 # Try adding a duplicate of an existing tag to a way
561 def test_add_duplicate_tags
562 private_user = create(:user, :data_public => false)
563 private_way = create(:way, :changeset => create(:changeset, :user => private_user))
564 private_existing_tag = create(:way_tag, :way => private_way)
566 way = create(:way, :changeset => create(:changeset, :user => user))
567 existing_tag = create(:way_tag, :way => way)
569 ## Try with the non-public user
571 basic_authorization private_user.email, "test"
573 # add an identical tag to the way
574 tag_xml = XML::Node.new("tag")
575 tag_xml["k"] = private_existing_tag.k
576 tag_xml["v"] = private_existing_tag.v
578 # add the tag into the existing xml
579 way_xml = private_way.to_xml
580 way_xml.find("//osm/way").first << tag_xml
584 put :update, :params => { :id => private_way.id }
585 assert_response :forbidden,
586 "adding a duplicate tag to a way for a non-public should fail with 'forbidden'"
588 ## Now try with the public user
590 basic_authorization user.email, "test"
592 # add an identical tag to the way
593 tag_xml = XML::Node.new("tag")
594 tag_xml["k"] = existing_tag.k
595 tag_xml["v"] = existing_tag.v
597 # add the tag into the existing xml
599 way_xml.find("//osm/way").first << tag_xml
603 put :update, :params => { :id => way.id }
604 assert_response :bad_request,
605 "adding a duplicate tag to a way should fail with 'bad request'"
606 assert_equal "Element way/#{way.id} has duplicate tags with key #{existing_tag.k}", @response.body
610 # Try adding a new duplicate tags to a way
611 def test_new_duplicate_tags
612 private_user = create(:user, :data_public => false)
613 private_way = create(:way, :changeset => create(:changeset, :user => private_user))
615 way = create(:way, :changeset => create(:changeset, :user => user))
617 ## First test with the non-public user so should be rejected
619 basic_authorization private_user.email, "test"
621 # create duplicate tag
622 tag_xml = XML::Node.new("tag")
623 tag_xml["k"] = "i_am_a_duplicate"
624 tag_xml["v"] = "foobar"
626 # add the tag into the existing xml
627 way_xml = private_way.to_xml
629 # add two copies of the tag
630 way_xml.find("//osm/way").first << tag_xml.copy(true) << tag_xml
634 put :update, :params => { :id => private_way.id }
635 assert_response :forbidden,
636 "adding new duplicate tags to a way using a non-public user should fail with 'forbidden'"
638 ## Now test with the public user
640 basic_authorization user.email, "test"
642 # create duplicate tag
643 tag_xml = XML::Node.new("tag")
644 tag_xml["k"] = "i_am_a_duplicate"
645 tag_xml["v"] = "foobar"
647 # add the tag into the existing xml
650 # add two copies of the tag
651 way_xml.find("//osm/way").first << tag_xml.copy(true) << tag_xml
655 put :update, :params => { :id => way.id }
656 assert_response :bad_request,
657 "adding new duplicate tags to a way should fail with 'bad request'"
658 assert_equal "Element way/#{way.id} has duplicate tags with key i_am_a_duplicate", @response.body
662 # Try adding a new duplicate tags to a way.
663 # But be a bit subtle - use unicode decoding ambiguities to use different
664 # binary strings which have the same decoding.
665 def test_invalid_duplicate_tags
666 private_user = create(:user, :data_public => false)
667 private_changeset = create(:changeset, :user => private_user)
669 changeset = create(:changeset, :user => user)
671 ## First make sure that you can't with a non-public user
673 basic_authorization private_user.email, "test"
675 # add the tag into the existing xml
676 way_str = "<osm><way changeset='#{private_changeset.id}'>"
677 way_str << "<tag k='addr:housenumber' v='1'/>"
678 way_str << "<tag k='addr:housenumber' v='2'/>"
679 way_str << "</way></osm>"
684 assert_response :forbidden,
685 "adding new duplicate tags to a way with a non-public user should fail with 'forbidden'"
687 ## Now do it with a public user
689 basic_authorization user.email, "test"
691 # add the tag into the existing xml
692 way_str = "<osm><way changeset='#{changeset.id}'>"
693 way_str << "<tag k='addr:housenumber' v='1'/>"
694 way_str << "<tag k='addr:housenumber' v='2'/>"
695 way_str << "</way></osm>"
700 assert_response :bad_request,
701 "adding new duplicate tags to a way should fail with 'bad request'"
702 assert_equal "Element way/ has duplicate tags with key addr:housenumber", @response.body
706 # test that a call to ways_for_node returns all ways that contain the node
707 # and none that don't.
708 def test_ways_for_node
712 create(:way_node, :way => way1, :node => node)
713 create(:way_node, :way => way2, :node => node)
714 # create an unrelated way
715 create(:way_with_nodes, :nodes_count => 2)
716 # create a way which used to use the node
717 way3_v1 = create(:old_way, :version => 1)
718 _way3_v2 = create(:old_way, :current_way => way3_v1.current_way, :version => 2)
719 create(:old_way_node, :old_way => way3_v1, :node => node)
721 get :ways_for_node, :params => { :id => node.id }
722 assert_response :success
723 ways_xml = XML::Parser.string(@response.body).parse
724 assert_not_nil ways_xml, "failed to parse ways_for_node response"
726 # check that the set of IDs match expectations
727 expected_way_ids = [way1.id,
729 found_way_ids = ways_xml.find("//osm/way").collect { |w| w["id"].to_i }
730 assert_equal expected_way_ids.sort, found_way_ids.sort,
731 "expected ways for node #{node.id} did not match found"
733 # check the full ways to ensure we're not missing anything
734 expected_way_ids.each do |id|
735 way_xml = ways_xml.find("//osm/way[@id='#{id}']").first
736 assert_ways_are_equal(Way.find(id),
737 Way.from_xml_node(way_xml))
742 # update the changeset_id of a way element
743 def update_changeset(xml, changeset_id)
744 xml_attr_rewrite(xml, "changeset", changeset_id)
748 # update an attribute in the way element
749 def xml_attr_rewrite(xml, name, value)
750 xml.find("//osm/way").first[name] = value.to_s
755 # replace a node in a way element
756 def xml_replace_node(xml, old_node, new_node)
757 xml.find("//osm/way/nd[@ref='#{old_node}']").first["ref"] = new_node.to_s