2 require_relative "elements_test_helper"
5 class WaysControllerTest < ActionDispatch::IntegrationTest
6 include ElementsTestHelper
9 # test all routes which lead to this controller
12 { :path => "/api/0.6/ways", :method => :get },
13 { :controller => "api/ways", :action => "index" }
16 { :path => "/api/0.6/ways.json", :method => :get },
17 { :controller => "api/ways", :action => "index", :format => "json" }
20 { :path => "/api/0.6/ways", :method => :post },
21 { :controller => "api/ways", :action => "create" }
24 { :path => "/api/0.6/way/1", :method => :get },
25 { :controller => "api/ways", :action => "show", :id => "1" }
28 { :path => "/api/0.6/way/1.json", :method => :get },
29 { :controller => "api/ways", :action => "show", :id => "1", :format => "json" }
32 { :path => "/api/0.6/way/1/full", :method => :get },
33 { :controller => "api/ways", :action => "show", :full => true, :id => "1" }
36 { :path => "/api/0.6/way/1/full.json", :method => :get },
37 { :controller => "api/ways", :action => "show", :full => true, :id => "1", :format => "json" }
40 { :path => "/api/0.6/way/1", :method => :put },
41 { :controller => "api/ways", :action => "update", :id => "1" }
44 { :path => "/api/0.6/way/1", :method => :delete },
45 { :controller => "api/ways", :action => "destroy", :id => "1" }
49 { :controller => "api/ways", :action => "create" },
50 { :path => "/api/0.6/way/create", :method => :put }
55 # test fetching multiple ways
58 way2 = create(:way, :deleted)
62 # check error when no parameter provided
64 assert_response :bad_request
66 # check error when no parameter value provided
67 get api_ways_path(:ways => "")
68 assert_response :bad_request
71 get api_ways_path(:ways => "#{way1.id},#{way2.id},#{way3.id},#{way4.id}")
72 assert_response :success
73 assert_select "osm" do
74 assert_select "way", :count => 4
75 assert_select "way[id='#{way1.id}'][visible='true']", :count => 1
76 assert_select "way[id='#{way2.id}'][visible='false']", :count => 1
77 assert_select "way[id='#{way3.id}'][visible='true']", :count => 1
78 assert_select "way[id='#{way4.id}'][visible='true']", :count => 1
81 # test a working call with json format
82 get api_ways_path(:ways => "#{way1.id},#{way2.id},#{way3.id},#{way4.id}", :format => "json")
84 js = ActiveSupport::JSON.decode(@response.body)
86 assert_equal 4, js["elements"].count
87 assert_equal 4, (js["elements"].count { |a| a["type"] == "way" })
88 assert_equal 1, (js["elements"].count { |a| a["id"] == way1.id && a["visible"].nil? })
89 assert_equal 1, (js["elements"].count { |a| a["id"] == way2.id && a["visible"] == false })
90 assert_equal 1, (js["elements"].count { |a| a["id"] == way3.id && a["visible"].nil? })
91 assert_equal 1, (js["elements"].count { |a| a["id"] == way4.id && a["visible"].nil? })
93 # check error when a non-existent way is included
94 get api_ways_path(:ways => "#{way1.id},#{way2.id},#{way3.id},#{way4.id},0")
95 assert_response :not_found
98 # -------------------------------------
100 # -------------------------------------
102 def test_show_not_found
104 assert_response :not_found
107 def test_show_deleted
108 get api_way_path(create(:way, :deleted))
109 assert_response :gone
113 way = create(:way, :timestamp => "2021-02-03T00:00:00Z")
114 node = create(:node, :timestamp => "2021-04-05T00:00:00Z")
115 create(:way_node, :way => way, :node => node)
117 get api_way_path(way)
119 assert_response :success
120 assert_not_nil @response.header["Last-Modified"]
121 assert_equal "2021-02-03T00:00:00Z", Time.parse(@response.header["Last-Modified"]).utc.xmlschema
125 way = create(:way_with_nodes, :nodes_count => 3)
127 get api_way_path(way, :format => "json")
129 assert_response :success
131 js = ActiveSupport::JSON.decode(@response.body)
133 assert_equal 1, js["elements"].count
134 js_ways = js["elements"].filter { |e| e["type"] == "way" }
135 assert_equal 1, js_ways.count
136 assert_equal way.id, js_ways[0]["id"]
137 assert_equal 1, js_ways[0]["version"]
141 # check the "full" mode
143 way = create(:way_with_nodes, :nodes_count => 3)
145 get api_way_path(way, :full => true)
147 assert_response :success
149 # Check the way is correctly returned
150 assert_select "osm way[id='#{way.id}'][version='1'][visible='true']", 1
152 # check that each node in the way appears once in the output as a
153 # reference and as the node element.
154 way.nodes.each do |n|
155 assert_select "osm way nd[ref='#{n.id}']", 1
156 assert_select "osm node[id='#{n.id}'][version='1'][lat='#{format('%<lat>.7f', :lat => n.lat)}'][lon='#{format('%<lon>.7f', :lon => n.lon)}']", 1
160 def test_show_full_json
161 way = create(:way_with_nodes, :nodes_count => 3)
163 get api_way_path(way, :full => true, :format => "json")
165 assert_response :success
167 # Check the way is correctly returned
168 js = ActiveSupport::JSON.decode(@response.body)
170 assert_equal 4, js["elements"].count
171 js_ways = js["elements"].filter { |e| e["type"] == "way" }
172 assert_equal 1, js_ways.count
173 assert_equal way.id, js_ways[0]["id"]
174 assert_equal 1, js_ways[0]["version"]
176 # check that each node in the way appears once in the output as a
177 # reference and as the node element.
178 js_nodes = js["elements"].filter { |e| e["type"] == "node" }
179 assert_equal 3, js_nodes.count
181 way.nodes.each_with_index do |n, i|
182 assert_equal n.id, js_ways[0]["nodes"][i]
183 js_nodes_with_id = js_nodes.filter { |e| e["id"] == n.id }
184 assert_equal 1, js_nodes_with_id.count
185 assert_equal n.id, js_nodes_with_id[0]["id"]
186 assert_equal 1, js_nodes_with_id[0]["version"]
187 assert_equal n.lat, js_nodes_with_id[0]["lat"]
188 assert_equal n.lon, js_nodes_with_id[0]["lon"]
192 def test_show_full_deleted
193 way = create(:way, :deleted)
195 get api_way_path(way, :full => true)
197 assert_response :gone
200 # -------------------------------------
201 # Test creating ways.
202 # -------------------------------------
204 def test_create_by_private_user
205 node1 = create(:node)
206 node2 = create(:node)
208 with_unchanging_request([:data_public => false]) do |headers, changeset|
211 <way changeset='#{changeset.id}'>
212 <nd ref='#{node1.id}'/>
213 <nd ref='#{node2.id}'/>
214 <tag k='test' v='yes' />
219 post api_ways_path, :params => osm, :headers => headers
221 assert_response :forbidden, "way upload did not return forbidden status"
226 node1 = create(:node)
227 node2 = create(:node)
229 with_request do |headers, changeset|
230 assert_difference "Way.count" => 1,
231 "WayNode.count" => 2 do
234 <way changeset='#{changeset.id}'>
235 <nd ref='#{node1.id}'/>
236 <nd ref='#{node2.id}'/>
237 <tag k='test' v='yes' />
242 post api_ways_path, :params => osm, :headers => headers
244 assert_response :success, "way upload did not return success status"
247 created_way_id = @response.body
248 way = Way.find(created_way_id)
249 assert_equal [node1, node2], way.nodes
250 assert_equal changeset.id, way.changeset_id, "saved way does not belong to the correct changeset"
251 assert way.visible, "saved way is not visible"
255 def test_create_with_missing_node_by_private_user
256 with_unchanging_request([:data_public => false]) do |headers, changeset|
259 <way changeset='#{changeset.id}'>
265 post api_ways_path, :params => osm, :headers => headers
267 assert_response :forbidden, "way upload with invalid node using a private user did not return 'forbidden'"
271 def test_create_without_nodes_by_private_user
272 with_unchanging_request([:data_public => false]) do |headers, changeset|
275 <way changeset='#{changeset.id}' />
279 post api_ways_path, :params => osm, :headers => headers
281 assert_response :forbidden, "way upload with no node using a private user did not return 'forbidden'"
285 def test_create_in_closed_changeset_by_private_user
288 with_unchanging_request([:data_public => false]) do |headers, changeset|
291 <way changeset='#{changeset.id}'>
292 <nd ref='#{node.id}'/>
297 post api_ways_path, :params => osm, :headers => headers
299 assert_response :forbidden, "way upload to closed changeset with a private user did not return 'forbidden'"
303 def test_create_with_missing_node
304 with_unchanging_request do |headers, changeset|
307 <way changeset='#{changeset.id}'>
313 post api_ways_path, :params => osm, :headers => headers
315 assert_response :precondition_failed, "way upload with invalid node did not return 'precondition failed'"
316 assert_equal "Precondition failed: Way requires the nodes with id in (0), which either do not exist, or are not visible.", @response.body
320 def test_create_without_nodes
321 with_unchanging_request do |headers, changeset|
324 <way changeset='#{changeset.id}' />
328 post api_ways_path, :params => osm, :headers => headers
330 assert_response :precondition_failed, "way upload with no node did not return 'precondition failed'"
331 assert_equal "Precondition failed: Cannot create way: data is invalid.", @response.body
335 def test_create_in_closed_changeset
338 with_unchanging_request([], [:closed]) do |headers, changeset|
341 <way changeset='#{changeset.id}'>
342 <nd ref='#{node.id}'/>
347 post api_ways_path, :params => osm, :headers => headers
349 assert_response :conflict, "way upload to closed changeset did not return 'conflict'"
353 def test_create_with_tag_too_long
356 with_unchanging_request do |headers, changeset|
359 <way changeset='#{changeset.id}'>
360 <nd ref='#{node.id}'/>
361 <tag k='foo' v='#{'x' * 256}'/>
366 post api_ways_path, :params => osm, :headers => headers
368 assert_response :bad_request, "way upload to with too long tag did not return 'bad_request'"
372 # -------------------------------------
373 # Test deleting ways.
374 # -------------------------------------
376 def test_destroy_when_unauthorized
377 with_unchanging(:way) do |way|
378 delete api_way_path(way)
380 assert_response :unauthorized
384 def test_destroy_without_payload_by_private_user
385 with_unchanging(:way) do |way|
386 with_unchanging_request([:data_public => false]) do |headers|
387 delete api_way_path(way), :headers => headers
389 assert_response :forbidden
394 def test_destroy_without_changeset_id_by_private_user
395 with_unchanging(:way) do |way|
396 with_unchanging_request([:data_public => false]) do |headers|
397 osm = "<osm><way id='#{way.id}'/></osm>"
399 delete api_way_path(way), :params => osm, :headers => headers
401 assert_response :forbidden
406 def test_destroy_in_closed_changeset_by_private_user
407 with_unchanging(:way) do |way|
408 with_unchanging_request([:data_public => false], [:closed]) do |headers, changeset|
409 osm_xml = xml_for_way way
410 osm_xml = update_changeset osm_xml, changeset.id
412 delete api_way_path(way), :params => osm_xml.to_s, :headers => headers
414 assert_response :forbidden
419 def test_destroy_in_missing_changeset_by_private_user
420 with_unchanging(:way) do |way|
421 with_unchanging_request([:data_public => false]) do |headers|
422 osm_xml = xml_for_way way
423 osm_xml = update_changeset osm_xml, 0
425 delete api_way_path(way), :params => osm_xml.to_s, :headers => headers
427 assert_response :forbidden
432 def test_destroy_by_private_user
433 with_unchanging(:way) do |way|
434 with_unchanging_request([:data_public => false]) do |headers, changeset|
435 osm_xml = xml_for_way way
436 osm_xml = update_changeset osm_xml, changeset.id
438 delete api_way_path(way), :params => osm_xml.to_s, :headers => headers
440 assert_response :forbidden
445 def test_destroy_deleted_way_by_private_user
446 with_unchanging(:way, :deleted) do |way|
447 with_unchanging_request([:data_public => false]) do |headers, changeset|
448 osm_xml = xml_for_way way
449 osm_xml = update_changeset osm_xml, changeset.id
451 delete api_way_path(way), :params => osm_xml.to_s, :headers => headers
453 assert_response :forbidden
458 def test_destroy_way_in_relation_by_private_user
459 with_unchanging(:way) do |way|
460 create(:relation_member, :member => way)
462 with_unchanging_request([:data_public => false]) do |headers, changeset|
463 osm_xml = xml_for_way way
464 osm_xml = update_changeset osm_xml, changeset.id
466 delete api_way_path(way), :params => osm_xml.to_s, :headers => headers
468 assert_response :forbidden, "shouldn't be able to delete a way used in a relation (#{@response.body}), when done by a private user"
473 def test_destroy_missing_way_by_private_user
474 with_unchanging_request([:data_public => false]) do |headers|
475 delete api_way_path(0), :headers => headers
477 assert_response :forbidden
481 def test_destroy_without_payload
482 with_unchanging(:way) do |way|
483 with_unchanging_request do |headers|
484 delete api_way_path(way), :headers => headers
486 assert_response :bad_request
491 def test_destroy_without_changeset_id
492 with_unchanging(:way) do |way|
493 with_unchanging_request do |headers|
494 osm = "<osm><way id='#{way.id}'/></osm>"
496 delete api_way_path(way), :params => osm, :headers => headers
498 assert_response :bad_request
503 def test_destroy_in_closed_changeset
504 with_unchanging(:way) do |way|
505 with_unchanging_request([], [:closed]) do |headers, changeset|
506 osm_xml = xml_for_way way
507 osm_xml = update_changeset osm_xml, changeset.id
509 delete api_way_path(way), :params => osm_xml.to_s, :headers => headers
511 assert_response :conflict
516 def test_destroy_in_missing_changeset
517 with_unchanging(:way) do |way|
518 with_unchanging_request do |headers|
519 osm_xml = xml_for_way way
520 osm_xml = update_changeset osm_xml, 0
522 delete api_way_path(way), :params => osm_xml.to_s, :headers => headers
524 assert_response :conflict
532 with_request do |headers, changeset|
533 osm_xml = xml_for_way way
534 osm_xml = update_changeset osm_xml, changeset.id
536 delete api_way_path(way), :params => osm_xml.to_s, :headers => headers
538 assert_response :success
540 response_way_version = @response.body.to_i
541 assert_operator response_way_version, :>, way.version, "delete request should return a new version number for way"
543 assert_not_predicate way, :visible?
544 assert_equal response_way_version, way.version
548 def test_destroy_deleted_way
549 with_unchanging(:way, :deleted) do |way|
550 with_unchanging_request do |headers, changeset|
551 osm_xml = xml_for_way way
552 osm_xml = update_changeset osm_xml, changeset.id
554 delete api_way_path(way), :params => osm_xml.to_s, :headers => headers
556 assert_response :gone
561 def test_destroy_way_in_relation
562 with_unchanging(:way) do |way|
563 relation_member = create(:relation_member, :member => way)
565 with_unchanging_request do |headers, changeset|
566 osm_xml = xml_for_way way
567 osm_xml = update_changeset osm_xml, changeset.id
569 delete api_way_path(way), :params => osm_xml.to_s, :headers => headers
571 assert_response :precondition_failed, "shouldn't be able to delete a way used in a relation (#{@response.body})"
572 assert_equal "Precondition failed: Way #{way.id} is still used by relations #{relation_member.relation.id}.", @response.body
577 def test_destroy_missing_way_with_payload
578 with_unchanging(:way) do |way|
579 with_unchanging_request do |headers, changeset|
580 osm_xml = xml_for_way way
581 osm_xml = update_changeset osm_xml, changeset.id
583 delete api_way_path(0), :params => osm_xml.to_s, :headers => headers
585 assert_response :not_found
590 # -------------------------------------
591 # Test updating ways.
592 # -------------------------------------
594 def test_update_when_unauthorized
595 with_unchanging(:way_with_nodes) do |way|
596 osm_xml = xml_for_way way
598 put api_way_path(way), :params => osm_xml.to_s
600 assert_response :unauthorized
604 def test_update_in_changeset_of_other_user_by_private_user
605 with_unchanging(:way_with_nodes) do |way|
606 other_user = create(:user)
608 with_unchanging_request([:data_public => false], [:user => other_user]) do |headers, changeset|
609 osm_xml = xml_for_way way
610 osm_xml = update_changeset osm_xml, changeset.id
612 put api_way_path(way), :params => osm_xml.to_s, :headers => headers
614 assert_require_public_data "update with other user's changeset should be forbidden when date isn't public"
619 def test_update_in_closed_changeset_by_private_user
620 with_unchanging(:way_with_nodes) do |way|
621 with_unchanging_request([:data_public => false], [:closed]) do |headers, changeset|
622 osm_xml = xml_for_way way
623 osm_xml = update_changeset osm_xml, changeset.id
625 put api_way_path(way), :params => osm_xml.to_s, :headers => headers
627 assert_require_public_data "update with closed changeset should be forbidden, when data isn't public"
632 def test_update_in_missing_changeset_by_private_user
633 with_unchanging(:way_with_nodes) do |way|
634 with_unchanging_request([:data_public => false]) do |headers|
635 osm_xml = xml_for_way way
636 osm_xml = update_changeset osm_xml, 0
638 put api_way_path(way), :params => osm_xml.to_s, :headers => headers
640 assert_require_public_data "update with changeset=0 should be forbidden, when data isn't public"
645 def test_update_with_missing_node_by_private_user
646 with_unchanging(:way) do |way|
648 create(:way_node, :way => way, :node => node)
650 with_unchanging_request([:data_public => false]) do |headers, changeset|
651 osm_xml = xml_for_way way
652 osm_xml = xml_replace_node osm_xml, node.id, 9999
653 osm_xml = update_changeset osm_xml, changeset.id
655 put api_way_path(way), :params => osm_xml.to_s, :headers => headers
657 assert_require_public_data "way with non-existent node should be forbidden, when data isn't public"
662 def test_update_with_deleted_node_by_private_user
663 with_unchanging(:way) do |way|
665 deleted_node = create(:node, :deleted)
666 create(:way_node, :way => way, :node => node)
668 with_unchanging_request([:data_public => false]) do |headers, changeset|
669 osm_xml = xml_for_way way
670 osm_xml = xml_replace_node osm_xml, node.id, deleted_node.id
671 osm_xml = update_changeset osm_xml, changeset.id
673 put api_way_path(way), :params => osm_xml.to_s, :headers => headers
675 assert_require_public_data "way with deleted node should be forbidden, when data isn't public"
680 def test_update_by_private_user
681 with_unchanging(:way_with_nodes) do |way|
682 with_unchanging_request([:data_public => false]) do |headers, changeset|
683 osm_xml = xml_for_way way
684 osm_xml = update_changeset osm_xml, changeset.id
686 put api_way_path(way), :params => osm_xml.to_s, :headers => headers
688 assert_require_public_data "should have failed with a forbidden when data isn't public"
693 def test_update_in_changeset_of_other_user
694 with_unchanging(:way_with_nodes) do |way|
695 other_user = create(:user)
697 with_unchanging_request([], [:user => other_user]) do |headers, changeset|
698 osm_xml = xml_for_way way
699 osm_xml = update_changeset osm_xml, changeset.id
701 put api_way_path(way), :params => osm_xml.to_s, :headers => headers
703 assert_response :conflict, "update with other user's changeset should be rejected"
708 def test_update_in_closed_changeset
709 with_unchanging(:way_with_nodes) do |way|
710 with_unchanging_request([], [:closed]) do |headers, changeset|
711 osm_xml = xml_for_way way
712 osm_xml = update_changeset osm_xml, changeset.id
714 put api_way_path(way), :params => osm_xml.to_s, :headers => headers
716 assert_response :conflict, "update with closed changeset should be rejected"
721 def test_update_in_missing_changeset
722 with_unchanging(:way_with_nodes) do |way|
723 with_unchanging_request do |headers|
724 osm_xml = xml_for_way way
725 osm_xml = update_changeset osm_xml, 0
727 put api_way_path(way), :params => osm_xml.to_s, :headers => headers
729 assert_response :conflict, "update with changeset=0 should be rejected"
734 def test_update_with_missing_node
735 with_unchanging(:way) do |way|
737 create(:way_node, :way => way, :node => node)
739 with_unchanging_request do |headers, changeset|
740 osm_xml = xml_for_way way
741 osm_xml = xml_replace_node osm_xml, node.id, 9999
742 osm_xml = update_changeset osm_xml, changeset.id
744 put api_way_path(way), :params => osm_xml.to_s, :headers => headers
746 assert_response :precondition_failed, "way with non-existent node should be rejected"
751 def test_update_with_deleted_node
752 with_unchanging(:way) do |way|
754 deleted_node = create(:node, :deleted)
755 create(:way_node, :way => way, :node => node)
757 with_unchanging_request do |headers, changeset|
758 osm_xml = xml_for_way way
759 osm_xml = xml_replace_node osm_xml, node.id, deleted_node.id
760 osm_xml = update_changeset osm_xml, changeset.id
762 put api_way_path(way), :params => osm_xml.to_s, :headers => headers
764 assert_response :precondition_failed, "way with deleted node should be rejected"
769 def test_update_with_version_behind
770 with_unchanging(:way_with_nodes, :version => 2) do |way|
771 with_unchanging_request do |headers, changeset|
772 osm_xml = xml_for_way way
773 osm_xml = xml_attr_rewrite osm_xml, "version", way.version - 1
774 osm_xml = update_changeset osm_xml, changeset.id
776 put api_way_path(way), :params => osm_xml.to_s, :headers => headers
778 assert_response :conflict, "should have failed on old version number"
783 def test_update_with_version_ahead
784 with_unchanging(:way_with_nodes, :version => 2) do |way|
785 with_unchanging_request do |headers, changeset|
786 osm_xml = xml_for_way way
787 osm_xml = xml_attr_rewrite osm_xml, "version", way.version + 1
788 osm_xml = update_changeset osm_xml, changeset.id
790 put api_way_path(way), :params => osm_xml.to_s, :headers => headers
792 assert_response :conflict, "should have failed on skipped version number"
797 def test_update_with_invalid_version
798 with_unchanging(:way_with_nodes) do |way|
799 with_unchanging_request do |headers, changeset|
800 osm_xml = xml_for_way way
801 osm_xml = xml_attr_rewrite osm_xml, "version", "p1r4t3s!"
802 osm_xml = update_changeset osm_xml, changeset.id
804 put api_way_path(way), :params => osm_xml.to_s, :headers => headers
806 assert_response :conflict, "should not be able to put 'p1r4at3s!' in the version field"
811 def test_update_other_way
812 with_unchanging(:way_with_nodes) do |way|
813 with_unchanging(:way_with_nodes) do |other_way|
814 with_unchanging_request do |headers, changeset|
815 osm_xml = xml_for_way other_way
816 osm_xml = update_changeset osm_xml, changeset.id
818 put api_way_path(way), :params => osm_xml.to_s, :headers => headers
820 assert_response :bad_request, "should not be able to update a way with a different ID from the XML"
826 def test_update_with_invalid_osm_structure
827 with_unchanging(:way_with_nodes) do |way|
828 with_unchanging_request do |headers|
831 put api_way_path(way), :params => osm, :headers => headers
833 assert_response :bad_request, "should not be able to update a way with non-OSM XML doc."
839 way = create(:way_with_nodes)
841 with_request do |headers, changeset|
842 osm_xml = xml_for_way way
843 osm_xml = update_changeset osm_xml, changeset.id
845 put api_way_path(way), :params => osm_xml.to_s, :headers => headers
847 assert_response :success, "a valid update request failed"
851 def test_update_with_new_tags_by_private_user
852 with_unchanging(:way_with_nodes, :nodes_count => 2) do |way|
853 with_unchanging_request([:data_public => false]) do |headers, changeset|
854 tag_xml = XML::Node.new("tag")
858 osm_xml = xml_for_way way
859 osm_xml.find("//osm/way").first << tag_xml
860 osm_xml = update_changeset osm_xml, changeset.id
862 put api_way_path(way), :params => osm_xml.to_s, :headers => headers
864 assert_response :forbidden, "adding a tag to a way for a non-public should fail with 'forbidden'"
869 def test_update_with_new_tags
870 way = create(:way_with_nodes, :nodes_count => 2)
872 with_request do |headers, changeset|
873 tag_xml = XML::Node.new("tag")
877 osm_xml = xml_for_way way
878 osm_xml.find("//osm/way").first << tag_xml
879 osm_xml = update_changeset osm_xml, changeset.id
881 put api_way_path(way), :params => osm_xml.to_s, :headers => headers
883 assert_response :success, "adding a new tag to a way should succeed"
884 assert_equal way.version + 1, @response.body.to_i
889 # Try adding a duplicate of an existing tag to a way
890 def test_add_duplicate_tags
891 private_user = create(:user, :data_public => false)
892 private_way = create(:way, :changeset => create(:changeset, :user => private_user))
893 private_existing_tag = create(:way_tag, :way => private_way)
895 way = create(:way, :changeset => create(:changeset, :user => user))
896 existing_tag = create(:way_tag, :way => way)
898 ## Try with the non-public user
900 auth_header = bearer_authorization_header private_user
902 # add an identical tag to the way
903 tag_xml = XML::Node.new("tag")
904 tag_xml["k"] = private_existing_tag.k
905 tag_xml["v"] = private_existing_tag.v
907 # add the tag into the existing xml
908 way_xml = xml_for_way(private_way)
909 way_xml.find("//osm/way").first << tag_xml
912 put api_way_path(private_way), :params => way_xml.to_s, :headers => auth_header
913 assert_response :forbidden,
914 "adding a duplicate tag to a way for a non-public should fail with 'forbidden'"
916 ## Now try with the public user
918 auth_header = bearer_authorization_header user
920 # add an identical tag to the way
921 tag_xml = XML::Node.new("tag")
922 tag_xml["k"] = existing_tag.k
923 tag_xml["v"] = existing_tag.v
925 # add the tag into the existing xml
926 way_xml = xml_for_way(way)
927 way_xml.find("//osm/way").first << tag_xml
930 put api_way_path(way), :params => way_xml.to_s, :headers => auth_header
931 assert_response :bad_request,
932 "adding a duplicate tag to a way should fail with 'bad request'"
933 assert_equal "Element way/#{way.id} has duplicate tags with key #{existing_tag.k}", @response.body
937 # Try adding a new duplicate tags to a way
938 def test_new_duplicate_tags
939 private_user = create(:user, :data_public => false)
940 private_way = create(:way, :changeset => create(:changeset, :user => private_user))
942 way = create(:way, :changeset => create(:changeset, :user => user))
944 ## First test with the non-public user so should be rejected
946 auth_header = bearer_authorization_header private_user
948 # create duplicate tag
949 tag_xml = XML::Node.new("tag")
950 tag_xml["k"] = "i_am_a_duplicate"
951 tag_xml["v"] = "foobar"
953 # add the tag into the existing xml
954 way_xml = xml_for_way(private_way)
956 # add two copies of the tag
957 way_xml.find("//osm/way").first << tag_xml.copy(true) << tag_xml
960 put api_way_path(private_way), :params => way_xml.to_s, :headers => auth_header
961 assert_response :forbidden,
962 "adding new duplicate tags to a way using a non-public user should fail with 'forbidden'"
964 ## Now test with the public user
966 auth_header = bearer_authorization_header user
968 # create duplicate tag
969 tag_xml = XML::Node.new("tag")
970 tag_xml["k"] = "i_am_a_duplicate"
971 tag_xml["v"] = "foobar"
973 # add the tag into the existing xml
974 way_xml = xml_for_way(way)
976 # add two copies of the tag
977 way_xml.find("//osm/way").first << tag_xml.copy(true) << tag_xml
980 put api_way_path(way), :params => way_xml.to_s, :headers => auth_header
981 assert_response :bad_request,
982 "adding new duplicate tags to a way should fail with 'bad request'"
983 assert_equal "Element way/#{way.id} has duplicate tags with key i_am_a_duplicate", @response.body
987 # Try adding a new duplicate tags to a way.
988 # But be a bit subtle - use unicode decoding ambiguities to use different
989 # binary strings which have the same decoding.
990 def test_invalid_duplicate_tags
991 private_user = create(:user, :data_public => false)
992 private_changeset = create(:changeset, :user => private_user)
994 changeset = create(:changeset, :user => user)
996 ## First make sure that you can't with a non-public user
998 auth_header = bearer_authorization_header private_user
1000 # add the tag into the existing xml
1003 <way changeset='#{private_changeset.id}'>
1004 <tag k='addr:housenumber' v='1'/>
1005 <tag k='addr:housenumber' v='2'/>
1011 post api_ways_path, :params => way_str, :headers => auth_header
1012 assert_response :forbidden,
1013 "adding new duplicate tags to a way with a non-public user should fail with 'forbidden'"
1015 ## Now do it with a public user
1017 auth_header = bearer_authorization_header user
1019 # add the tag into the existing xml
1022 <way changeset='#{changeset.id}'>
1023 <tag k='addr:housenumber' v='1'/>
1024 <tag k='addr:housenumber' v='2'/>
1030 post api_ways_path, :params => way_str, :headers => auth_header
1031 assert_response :bad_request,
1032 "adding new duplicate tags to a way should fail with 'bad request'"
1033 assert_equal "Element way/ has duplicate tags with key addr:housenumber", @response.body
1037 # test initial rate limit
1038 def test_initial_rate_limit
1040 user = create(:user)
1043 node1 = create(:node)
1044 node2 = create(:node)
1046 # create a changeset that puts us near the initial rate limit
1047 changeset = create(:changeset, :user => user,
1048 :created_at => Time.now.utc - 5.minutes,
1049 :num_changes => Settings.initial_changes_per_hour - 1)
1051 # create authentication header
1052 auth_header = bearer_authorization_header user
1054 # try creating a way
1057 <way changeset='#{changeset.id}'>
1058 <nd ref='#{node1.id}'/>
1059 <nd ref='#{node2.id}'/>
1063 post api_ways_path, :params => xml, :headers => auth_header
1064 assert_response :success, "way create did not return success status"
1066 # get the id of the way we created
1067 wayid = @response.body
1069 # try updating the way, which should be rate limited
1072 <way id='#{wayid}' version='1' changeset='#{changeset.id}'>
1073 <nd ref='#{node2.id}'/>
1074 <nd ref='#{node1.id}'/>
1078 put api_way_path(wayid), :params => xml, :headers => auth_header
1079 assert_response :too_many_requests, "way update did not hit rate limit"
1081 # try deleting the way, which should be rate limited
1082 xml = "<osm><way id='#{wayid}' version='2' changeset='#{changeset.id}'/></osm>"
1083 delete api_way_path(wayid), :params => xml, :headers => auth_header
1084 assert_response :too_many_requests, "way delete did not hit rate limit"
1086 # try creating a way, which should be rate limited
1089 <way changeset='#{changeset.id}'>
1090 <nd ref='#{node1.id}'/>
1091 <nd ref='#{node2.id}'/>
1095 post api_ways_path, :params => xml, :headers => auth_header
1096 assert_response :too_many_requests, "way create did not hit rate limit"
1100 # test maximum rate limit
1101 def test_maximum_rate_limit
1103 user = create(:user)
1106 node1 = create(:node)
1107 node2 = create(:node)
1109 # create a changeset to establish our initial edit time
1110 changeset = create(:changeset, :user => user,
1111 :created_at => Time.now.utc - 28.days)
1113 # create changeset to put us near the maximum rate limit
1114 total_changes = Settings.max_changes_per_hour - 1
1115 while total_changes.positive?
1116 changes = [total_changes, Changeset::MAX_ELEMENTS].min
1117 changeset = create(:changeset, :user => user,
1118 :created_at => Time.now.utc - 5.minutes,
1119 :num_changes => changes)
1120 total_changes -= changes
1123 # create authentication header
1124 auth_header = bearer_authorization_header user
1126 # try creating a way
1129 <way changeset='#{changeset.id}'>
1130 <nd ref='#{node1.id}'/>
1131 <nd ref='#{node2.id}'/>
1135 post api_ways_path, :params => xml, :headers => auth_header
1136 assert_response :success, "way create did not return success status"
1138 # get the id of the way we created
1139 wayid = @response.body
1141 # try updating the way, which should be rate limited
1144 <way id='#{wayid}' version='1' changeset='#{changeset.id}'>
1145 <nd ref='#{node2.id}'/>
1146 <nd ref='#{node1.id}'/>
1150 put api_way_path(wayid), :params => xml, :headers => auth_header
1151 assert_response :too_many_requests, "way update did not hit rate limit"
1153 # try deleting the way, which should be rate limited
1154 xml = "<osm><way id='#{wayid}' version='2' changeset='#{changeset.id}'/></osm>"
1155 delete api_way_path(wayid), :params => xml, :headers => auth_header
1156 assert_response :too_many_requests, "way delete did not hit rate limit"
1158 # try creating a way, which should be rate limited
1161 <way changeset='#{changeset.id}'>
1162 <nd ref='#{node1.id}'/>
1163 <nd ref='#{node2.id}'/>
1167 post api_ways_path, :params => xml, :headers => auth_header
1168 assert_response :too_many_requests, "way create did not hit rate limit"
1174 [Way, WayNode, WayTag,
1175 OldWay, OldWayNode, OldWayTag]
1179 # update an attribute in the way element
1180 def xml_attr_rewrite(xml, name, value)
1181 xml.find("//osm/way").first[name] = value.to_s
1186 # replace a node in a way element
1187 def xml_replace_node(xml, old_node, new_node)
1188 xml.find("//osm/way/nd[@ref='#{old_node}']").first["ref"] = new_node.to_s