4 class RelationsControllerTest < ActionDispatch::IntegrationTest
6 # test all routes which lead to this controller
9 { :path => "/api/0.6/relations", :method => :get },
10 { :controller => "api/relations", :action => "index" }
13 { :path => "/api/0.6/relations.json", :method => :get },
14 { :controller => "api/relations", :action => "index", :format => "json" }
17 { :path => "/api/0.6/relations", :method => :post },
18 { :controller => "api/relations", :action => "create" }
21 { :path => "/api/0.6/relation/1", :method => :get },
22 { :controller => "api/relations", :action => "show", :id => "1" }
25 { :path => "/api/0.6/relation/1.json", :method => :get },
26 { :controller => "api/relations", :action => "show", :id => "1", :format => "json" }
29 { :path => "/api/0.6/relation/1/full", :method => :get },
30 { :controller => "api/relations", :action => "show", :full => true, :id => "1" }
33 { :path => "/api/0.6/relation/1/full.json", :method => :get },
34 { :controller => "api/relations", :action => "show", :full => true, :id => "1", :format => "json" }
37 { :path => "/api/0.6/relation/1", :method => :put },
38 { :controller => "api/relations", :action => "update", :id => "1" }
41 { :path => "/api/0.6/relation/1", :method => :delete },
42 { :controller => "api/relations", :action => "destroy", :id => "1" }
46 { :controller => "api/relations", :action => "create" },
47 { :path => "/api/0.6/relation/create", :method => :put }
52 # test fetching multiple relations
54 relation1 = create(:relation)
55 relation2 = create(:relation, :deleted)
56 relation3 = create(:relation, :with_history, :version => 2)
57 relation4 = create(:relation, :with_history, :version => 2)
58 relation4.old_relations.find_by(:version => 1).redact!(create(:redaction))
60 # check error when no parameter provided
61 get api_relations_path
62 assert_response :bad_request
64 # check error when no parameter value provided
65 get api_relations_path(:relations => "")
66 assert_response :bad_request
69 get api_relations_path(:relations => "#{relation1.id},#{relation2.id},#{relation3.id},#{relation4.id}")
70 assert_response :success
71 assert_select "osm" do
72 assert_select "relation", :count => 4
73 assert_select "relation[id='#{relation1.id}'][visible='true']", :count => 1
74 assert_select "relation[id='#{relation2.id}'][visible='false']", :count => 1
75 assert_select "relation[id='#{relation3.id}'][visible='true']", :count => 1
76 assert_select "relation[id='#{relation4.id}'][visible='true']", :count => 1
79 # test a working call with json format
80 get api_relations_path(:relations => "#{relation1.id},#{relation2.id},#{relation3.id},#{relation4.id}", :format => "json")
82 js = ActiveSupport::JSON.decode(@response.body)
84 assert_equal 4, js["elements"].count
85 assert_equal 4, (js["elements"].count { |a| a["type"] == "relation" })
86 assert_equal 1, (js["elements"].count { |a| a["id"] == relation1.id && a["visible"].nil? })
87 assert_equal 1, (js["elements"].count { |a| a["id"] == relation2.id && a["visible"] == false })
88 assert_equal 1, (js["elements"].count { |a| a["id"] == relation3.id && a["visible"].nil? })
89 assert_equal 1, (js["elements"].count { |a| a["id"] == relation4.id && a["visible"].nil? })
91 # check error when a non-existent relation is included
92 get api_relations_path(:relations => "#{relation1.id},#{relation2.id},#{relation3.id},#{relation4.id},0")
93 assert_response :not_found
96 # -------------------------------------
97 # Test showing relations.
98 # -------------------------------------
100 def test_show_not_found
101 get api_relation_path(0)
102 assert_response :not_found
105 def test_show_deleted
106 get api_relation_path(create(:relation, :deleted))
107 assert_response :gone
111 relation = create(:relation, :timestamp => "2021-02-03T00:00:00Z")
112 node = create(:node, :timestamp => "2021-04-05T00:00:00Z")
113 create(:relation_member, :relation => relation, :member => node)
115 get api_relation_path(relation)
117 assert_response :success
118 assert_not_nil @response.header["Last-Modified"]
119 assert_equal "2021-02-03T00:00:00Z", Time.parse(@response.header["Last-Modified"]).utc.xmlschema
120 assert_dom "node", :count => 0
121 assert_dom "relation", :count => 1 do
122 assert_dom "> @id", :text => relation.id.to_s
126 def test_full_not_found
127 get api_relation_path(999999, :full => true)
128 assert_response :not_found
131 def test_full_deleted
132 get api_relation_path(create(:relation, :deleted), :full => true)
133 assert_response :gone
137 relation = create(:relation)
139 get api_relation_path(relation, :full => true)
141 assert_response :success
142 assert_dom "relation", :count => 1 do
143 assert_dom "> @id", :text => relation.id.to_s
147 def test_full_with_node_member
148 relation = create(:relation)
150 create(:relation_member, :relation => relation, :member => node)
152 get api_relation_path(relation, :full => true)
154 assert_response :success
155 assert_dom "node", :count => 1 do
156 assert_dom "> @id", :text => node.id.to_s
158 assert_dom "relation", :count => 1 do
159 assert_dom "> @id", :text => relation.id.to_s
163 def test_full_with_way_member
164 relation = create(:relation)
165 way = create(:way_with_nodes)
166 create(:relation_member, :relation => relation, :member => way)
168 get api_relation_path(relation, :full => true)
170 assert_response :success
171 assert_dom "node", :count => 1 do
172 assert_dom "> @id", :text => way.nodes[0].id.to_s
174 assert_dom "way", :count => 1 do
175 assert_dom "> @id", :text => way.id.to_s
177 assert_dom "relation", :count => 1 do
178 assert_dom "> @id", :text => relation.id.to_s
182 def test_full_with_node_member_json
183 relation = create(:relation)
185 create(:relation_member, :relation => relation, :member => node)
187 get api_relation_path(relation, :full => true, :format => "json")
189 assert_response :success
190 js = ActiveSupport::JSON.decode(@response.body)
192 assert_equal 2, js["elements"].count
194 js_relations = js["elements"].filter { |e| e["type"] == "relation" }
195 assert_equal 1, js_relations.count
196 assert_equal relation.id, js_relations[0]["id"]
197 assert_equal 1, js_relations[0]["members"].count
198 assert_equal "node", js_relations[0]["members"][0]["type"]
199 assert_equal node.id, js_relations[0]["members"][0]["ref"]
201 js_nodes = js["elements"].filter { |e| e["type"] == "node" }
202 assert_equal 1, js_nodes.count
203 assert_equal node.id, js_nodes[0]["id"]
206 # -------------------------------------
207 # Test simple relation creation.
208 # -------------------------------------
211 private_user = create(:user, :data_public => false)
212 private_changeset = create(:changeset, :user => private_user)
214 changeset = create(:changeset, :user => user)
216 way = create(:way_with_nodes, :nodes_count => 2)
218 auth_header = bearer_authorization_header private_user
220 # create an relation without members
221 xml = "<osm><relation changeset='#{private_changeset.id}'><tag k='test' v='yes' /></relation></osm>"
222 post api_relations_path, :params => xml, :headers => auth_header
223 # hope for forbidden, due to user
224 assert_response :forbidden,
225 "relation upload should have failed with forbidden"
228 # create an relation with a node as member
229 # This time try with a role attribute in the relation
230 xml = "<osm><relation changeset='#{private_changeset.id}'>" \
231 "<member ref='#{node.id}' type='node' role='some'/>" \
232 "<tag k='test' v='yes' /></relation></osm>"
233 post api_relations_path, :params => xml, :headers => auth_header
234 # hope for forbidden due to user
235 assert_response :forbidden,
236 "relation upload did not return forbidden status"
239 # create an relation with a node as member, this time test that we don't
240 # need a role attribute to be included
241 xml = "<osm><relation changeset='#{private_changeset.id}'>" \
242 "<member ref='#{node.id}' type='node'/><tag k='test' v='yes' /></relation></osm>"
243 post api_relations_path, :params => xml, :headers => auth_header
244 # hope for forbidden due to user
245 assert_response :forbidden,
246 "relation upload did not return forbidden status"
249 # create an relation with a way and a node as members
250 xml = "<osm><relation changeset='#{private_changeset.id}'>" \
251 "<member type='node' ref='#{node.id}' role='some'/>" \
252 "<member type='way' ref='#{way.id}' role='other'/>" \
253 "<tag k='test' v='yes' /></relation></osm>"
254 post api_relations_path, :params => xml, :headers => auth_header
255 # hope for forbidden, due to user
256 assert_response :forbidden,
257 "relation upload did not return success status"
259 ## Now try with the public user
260 auth_header = bearer_authorization_header user
262 # create an relation without members
263 xml = "<osm><relation changeset='#{changeset.id}'><tag k='test' v='yes' /></relation></osm>"
264 post api_relations_path, :params => xml, :headers => auth_header
266 assert_response :success,
267 "relation upload did not return success status"
268 # read id of created relation and search for it
269 relationid = @response.body
270 checkrelation = Relation.find(relationid)
271 assert_not_nil checkrelation,
272 "uploaded relation not found in data base after upload"
274 assert_equal(0, checkrelation.members.length, "saved relation contains members but should not")
275 assert_equal(1, checkrelation.tags.length, "saved relation does not contain exactly one tag")
276 assert_equal changeset.id, checkrelation.changeset.id,
277 "saved relation does not belong in the changeset it was assigned to"
278 assert_equal user.id, checkrelation.changeset.user_id,
279 "saved relation does not belong to user that created it"
280 assert checkrelation.visible,
281 "saved relation is not visible"
282 # ok the relation is there but can we also retrieve it?
283 get api_relation_path(relationid)
284 assert_response :success
287 # create an relation with a node as member
288 # This time try with a role attribute in the relation
289 xml = "<osm><relation changeset='#{changeset.id}'>" \
290 "<member ref='#{node.id}' type='node' role='some'/>" \
291 "<tag k='test' v='yes' /></relation></osm>"
292 post api_relations_path, :params => xml, :headers => auth_header
294 assert_response :success,
295 "relation upload did not return success status"
296 # read id of created relation and search for it
297 relationid = @response.body
298 checkrelation = Relation.find(relationid)
299 assert_not_nil checkrelation,
300 "uploaded relation not found in data base after upload"
302 assert_equal(1, checkrelation.members.length, "saved relation does not contain exactly one member")
303 assert_equal(1, checkrelation.tags.length, "saved relation does not contain exactly one tag")
304 assert_equal changeset.id, checkrelation.changeset.id,
305 "saved relation does not belong in the changeset it was assigned to"
306 assert_equal user.id, checkrelation.changeset.user_id,
307 "saved relation does not belong to user that created it"
308 assert checkrelation.visible,
309 "saved relation is not visible"
310 # ok the relation is there but can we also retrieve it?
312 get api_relation_path(relationid)
313 assert_response :success
316 # create an relation with a node as member, this time test that we don't
317 # need a role attribute to be included
318 xml = "<osm><relation changeset='#{changeset.id}'>" \
319 "<member ref='#{node.id}' type='node'/><tag k='test' v='yes' /></relation></osm>"
320 post api_relations_path, :params => xml, :headers => auth_header
322 assert_response :success,
323 "relation upload did not return success status"
324 # read id of created relation and search for it
325 relationid = @response.body
326 checkrelation = Relation.find(relationid)
327 assert_not_nil checkrelation,
328 "uploaded relation not found in data base after upload"
330 assert_equal(1, checkrelation.members.length, "saved relation does not contain exactly one member")
331 assert_equal(1, checkrelation.tags.length, "saved relation does not contain exactly one tag")
332 assert_equal changeset.id, checkrelation.changeset.id,
333 "saved relation does not belong in the changeset it was assigned to"
334 assert_equal user.id, checkrelation.changeset.user_id,
335 "saved relation does not belong to user that created it"
336 assert checkrelation.visible,
337 "saved relation is not visible"
338 # ok the relation is there but can we also retrieve it?
340 get api_relation_path(relationid)
341 assert_response :success
344 # create an relation with a way and a node as members
345 xml = "<osm><relation changeset='#{changeset.id}'>" \
346 "<member type='node' ref='#{node.id}' role='some'/>" \
347 "<member type='way' ref='#{way.id}' role='other'/>" \
348 "<tag k='test' v='yes' /></relation></osm>"
349 post api_relations_path, :params => xml, :headers => auth_header
351 assert_response :success,
352 "relation upload did not return success status"
353 # read id of created relation and search for it
354 relationid = @response.body
355 checkrelation = Relation.find(relationid)
356 assert_not_nil checkrelation,
357 "uploaded relation not found in data base after upload"
359 assert_equal(2, checkrelation.members.length, "saved relation does not have exactly two members")
360 assert_equal(1, checkrelation.tags.length, "saved relation does not contain exactly one tag")
361 assert_equal changeset.id, checkrelation.changeset.id,
362 "saved relation does not belong in the changeset it was assigned to"
363 assert_equal user.id, checkrelation.changeset.user_id,
364 "saved relation does not belong to user that created it"
365 assert checkrelation.visible,
366 "saved relation is not visible"
367 # ok the relation is there but can we also retrieve it?
368 get api_relation_path(relationid)
369 assert_response :success
372 # ------------------------------------
373 # Test updating relations
374 # ------------------------------------
377 # test that, when tags are updated on a relation, the correct things
378 # happen to the correct tables and the API gives sensible results.
379 # this is to test a case that gregory marler noticed and posted to
381 ## FIXME Move this to an integration test
382 def test_update_relation_tags
384 changeset = create(:changeset, :user => user)
385 relation = create(:relation)
386 create_list(:relation_tag, 4, :relation => relation)
388 auth_header = bearer_authorization_header user
390 get api_relation_path(relation)
391 assert_response :success
392 rel = xml_parse(@response.body)
394 # alter one of the tags
395 tag = rel.find("//osm/relation/tag").first
396 tag["v"] = "some changed value"
397 update_changeset(rel, changeset.id)
399 # check that the downloaded tags are the same as the uploaded tags...
400 new_version = with_update(rel, auth_header) do |new_rel|
401 assert_tags_equal rel, new_rel
404 # check the original one in the current_* table again
405 get api_relation_path(relation)
406 assert_response :success
407 assert_tags_equal rel, xml_parse(@response.body)
409 # now check the version in the history
410 get api_relation_version_path(relation, new_version)
411 assert_response :success
412 assert_tags_equal rel, xml_parse(@response.body)
416 # test that, when tags are updated on a relation when using the diff
417 # upload function, the correct things happen to the correct tables
418 # and the API gives sensible results. this is to test a case that
419 # gregory marler noticed and posted to josm-dev.
420 def test_update_relation_tags_via_upload
422 changeset = create(:changeset, :user => user)
423 relation = create(:relation)
424 create_list(:relation_tag, 4, :relation => relation)
426 auth_header = bearer_authorization_header user
428 get api_relation_path(relation)
429 assert_response :success
430 rel = xml_parse(@response.body)
432 # alter one of the tags
433 tag = rel.find("//osm/relation/tag").first
434 tag["v"] = "some changed value"
435 update_changeset(rel, changeset.id)
437 # check that the downloaded tags are the same as the uploaded tags...
438 new_version = with_update_diff(rel, auth_header) do |new_rel|
439 assert_tags_equal rel, new_rel
442 # check the original one in the current_* table again
443 get api_relation_path(relation)
444 assert_response :success
445 assert_tags_equal rel, xml_parse(@response.body)
447 # now check the version in the history
448 get api_relation_version_path(relation, new_version)
449 assert_response :success
450 assert_tags_equal rel, xml_parse(@response.body)
453 def test_update_wrong_id
455 changeset = create(:changeset, :user => user)
456 relation = create(:relation)
457 other_relation = create(:relation)
459 auth_header = bearer_authorization_header user
460 get api_relation_path(relation)
461 assert_response :success
462 rel = xml_parse(@response.body)
464 update_changeset(rel, changeset.id)
465 put api_relation_path(other_relation), :params => rel.to_s, :headers => auth_header
466 assert_response :bad_request
469 # -------------------------------------
470 # Test creating some invalid relations.
471 # -------------------------------------
473 def test_create_invalid
475 changeset = create(:changeset, :user => user)
477 auth_header = bearer_authorization_header user
479 # create a relation with non-existing node as member
480 xml = "<osm><relation changeset='#{changeset.id}'>" \
481 "<member type='node' ref='0'/><tag k='test' v='yes' />" \
483 post api_relations_path, :params => xml, :headers => auth_header
485 assert_response :precondition_failed,
486 "relation upload with invalid node did not return 'precondition failed'"
487 assert_equal "Precondition failed: Relation with id cannot be saved due to Node with id 0", @response.body
490 # -------------------------------------
491 # Test creating a relation, with some invalid XML
492 # -------------------------------------
493 def test_create_invalid_xml
495 changeset = create(:changeset, :user => user)
498 auth_header = bearer_authorization_header user
500 # create some xml that should return an error
501 xml = "<osm><relation changeset='#{changeset.id}'>" \
502 "<member type='type' ref='#{node.id}' role=''/>" \
503 "<tag k='tester' v='yep'/></relation></osm>"
504 post api_relations_path, :params => xml, :headers => auth_header
506 assert_response :bad_request
507 assert_match(/Cannot parse valid relation from xml string/, @response.body)
508 assert_match(/The type is not allowed only, /, @response.body)
511 # -------------------------------------
512 # Test deleting relations.
513 # -------------------------------------
516 private_user = create(:user, :data_public => false)
517 private_user_closed_changeset = create(:changeset, :closed, :user => private_user)
519 closed_changeset = create(:changeset, :closed, :user => user)
520 changeset = create(:changeset, :user => user)
521 relation = create(:relation)
522 used_relation = create(:relation)
523 super_relation = create(:relation_member, :member => used_relation).relation
524 deleted_relation = create(:relation, :deleted)
525 multi_tag_relation = create(:relation)
526 create_list(:relation_tag, 4, :relation => multi_tag_relation)
528 ## First try to delete relation without auth
529 delete api_relation_path(relation)
530 assert_response :unauthorized
532 ## Then try with the private user, to make sure that you get a forbidden
533 auth_header = bearer_authorization_header private_user
535 # this shouldn't work, as we should need the payload...
536 delete api_relation_path(relation), :headers => auth_header
537 assert_response :forbidden
539 # try to delete without specifying a changeset
540 xml = "<osm><relation id='#{relation.id}'/></osm>"
541 delete api_relation_path(relation), :params => xml.to_s, :headers => auth_header
542 assert_response :forbidden
544 # try to delete with an invalid (closed) changeset
545 xml = update_changeset(xml_for_relation(relation),
546 private_user_closed_changeset.id)
547 delete api_relation_path(relation), :params => xml.to_s, :headers => auth_header
548 assert_response :forbidden
550 # try to delete with an invalid (non-existent) changeset
551 xml = update_changeset(xml_for_relation(relation), 0)
552 delete api_relation_path(relation), :params => xml.to_s, :headers => auth_header
553 assert_response :forbidden
555 # this won't work because the relation is in-use by another relation
556 xml = xml_for_relation(used_relation)
557 delete api_relation_path(used_relation), :params => xml.to_s, :headers => auth_header
558 assert_response :forbidden
560 # this should work when we provide the appropriate payload...
561 xml = xml_for_relation(relation)
562 delete api_relation_path(relation), :params => xml.to_s, :headers => auth_header
563 assert_response :forbidden
565 # this won't work since the relation is already deleted
566 xml = xml_for_relation(deleted_relation)
567 delete api_relation_path(deleted_relation), :params => xml.to_s, :headers => auth_header
568 assert_response :forbidden
570 # this won't work since the relation never existed
571 delete api_relation_path(0), :headers => auth_header
572 assert_response :forbidden
574 ## now set auth for the public user
575 auth_header = bearer_authorization_header user
577 # this shouldn't work, as we should need the payload...
578 delete api_relation_path(relation), :headers => auth_header
579 assert_response :bad_request
581 # try to delete without specifying a changeset
582 xml = "<osm><relation id='#{relation.id}' version='#{relation.version}' /></osm>"
583 delete api_relation_path(relation), :params => xml.to_s, :headers => auth_header
584 assert_response :bad_request
585 assert_match(/Changeset id is missing/, @response.body)
587 # try to delete with an invalid (closed) changeset
588 xml = update_changeset(xml_for_relation(relation),
590 delete api_relation_path(relation), :params => xml.to_s, :headers => auth_header
591 assert_response :conflict
593 # try to delete with an invalid (non-existent) changeset
594 xml = update_changeset(xml_for_relation(relation), 0)
595 delete api_relation_path(relation), :params => xml.to_s, :headers => auth_header
596 assert_response :conflict
598 # this won't work because the relation is in a changeset owned by someone else
599 xml = update_changeset(xml_for_relation(relation), create(:changeset).id)
600 delete api_relation_path(relation), :params => xml.to_s, :headers => auth_header
601 assert_response :conflict,
602 "shouldn't be able to delete a relation in a changeset owned by someone else (#{@response.body})"
604 # this won't work because the relation in the payload is different to that passed
605 xml = update_changeset(xml_for_relation(relation), changeset.id)
606 delete api_relation_path(create(:relation)), :params => xml.to_s, :headers => auth_header
607 assert_response :bad_request, "shouldn't be able to delete a relation when payload is different to the url"
609 # this won't work because the relation is in-use by another relation
610 xml = update_changeset(xml_for_relation(used_relation), changeset.id)
611 delete api_relation_path(used_relation), :params => xml.to_s, :headers => auth_header
612 assert_response :precondition_failed,
613 "shouldn't be able to delete a relation used in a relation (#{@response.body})"
614 assert_equal "Precondition failed: The relation #{used_relation.id} is used in relation #{super_relation.id}.", @response.body
616 # this should work when we provide the appropriate payload...
617 xml = update_changeset(xml_for_relation(multi_tag_relation), changeset.id)
618 delete api_relation_path(multi_tag_relation), :params => xml.to_s, :headers => auth_header
619 assert_response :success
621 # valid delete should return the new version number, which should
622 # be greater than the old version number
623 assert_operator @response.body.to_i, :>, multi_tag_relation.version, "delete request should return a new version number for relation"
625 # this won't work since the relation is already deleted
626 xml = update_changeset(xml_for_relation(deleted_relation), changeset.id)
627 delete api_relation_path(deleted_relation), :params => xml.to_s, :headers => auth_header
628 assert_response :gone
630 # Public visible relation needs to be deleted
631 xml = update_changeset(xml_for_relation(super_relation), changeset.id)
632 delete api_relation_path(super_relation), :params => xml.to_s, :headers => auth_header
633 assert_response :success
635 # this works now because the relation which was using this one
637 xml = update_changeset(xml_for_relation(used_relation), changeset.id)
638 delete api_relation_path(used_relation), :params => xml.to_s, :headers => auth_header
639 assert_response :success,
640 "should be able to delete a relation used in an old relation (#{@response.body})"
642 # this won't work since the relation never existed
643 delete api_relation_path(0), :headers => auth_header
644 assert_response :not_found
648 # when a relation's tag is modified then it should put the bounding
649 # box of all its members into the changeset.
650 def test_tag_modify_bounding_box
651 relation = create(:relation)
652 node1 = create(:node, :lat => 0.3, :lon => 0.3)
653 node2 = create(:node, :lat => 0.5, :lon => 0.5)
655 create(:way_node, :way => way, :node => node1)
656 create(:relation_member, :relation => relation, :member => way)
657 create(:relation_member, :relation => relation, :member => node2)
658 # the relation contains nodes1 and node2 (node1
659 # indirectly via the way), so the bbox should be [0.3,0.3,0.5,0.5].
660 check_changeset_modify(BoundingBox.new(0.3, 0.3, 0.5, 0.5)) do |changeset_id, auth_header|
661 # add a tag to an existing relation
662 relation_xml = xml_for_relation(relation)
663 relation_element = relation_xml.find("//osm/relation").first
664 new_tag = XML::Node.new("tag")
665 new_tag["k"] = "some_new_tag"
666 new_tag["v"] = "some_new_value"
667 relation_element << new_tag
669 # update changeset ID to point to new changeset
670 update_changeset(relation_xml, changeset_id)
673 put api_relation_path(relation), :params => relation_xml.to_s, :headers => auth_header
674 assert_response :success, "can't update relation for tag/bbox test"
679 # add a member to a relation and check the bounding box is only that
681 def test_add_member_bounding_box
682 relation = create(:relation)
683 node1 = create(:node, :lat => 4, :lon => 4)
684 node2 = create(:node, :lat => 7, :lon => 7)
686 create(:way_node, :way => way1, :node => create(:node, :lat => 8, :lon => 8))
688 create(:way_node, :way => way2, :node => create(:node, :lat => 9, :lon => 9), :sequence_id => 1)
689 create(:way_node, :way => way2, :node => create(:node, :lat => 10, :lon => 10), :sequence_id => 2)
691 [node1, node2, way1, way2].each do |element|
692 bbox = element.bbox.to_unscaled
693 check_changeset_modify(bbox) do |changeset_id, auth_header|
694 relation_xml = xml_for_relation(Relation.find(relation.id))
695 relation_element = relation_xml.find("//osm/relation").first
696 new_member = XML::Node.new("member")
697 new_member["ref"] = element.id.to_s
698 new_member["type"] = element.class.to_s.downcase
699 new_member["role"] = "some_role"
700 relation_element << new_member
702 # update changeset ID to point to new changeset
703 update_changeset(relation_xml, changeset_id)
706 put api_relation_path(relation), :params => relation_xml.to_s, :headers => auth_header
707 assert_response :success, "can't update relation for add #{element.class}/bbox test: #{@response.body}"
709 # get it back and check the ordering
710 get api_relation_path(relation)
711 assert_response :success, "can't read back the relation: #{@response.body}"
712 check_ordering(relation_xml, @response.body)
718 # remove a member from a relation and check the bounding box is
720 def test_remove_member_bounding_box
721 relation = create(:relation)
722 node1 = create(:node, :lat => 3, :lon => 3)
723 node2 = create(:node, :lat => 5, :lon => 5)
724 create(:relation_member, :relation => relation, :member => node1)
725 create(:relation_member, :relation => relation, :member => node2)
727 check_changeset_modify(BoundingBox.new(5, 5, 5, 5)) do |changeset_id, auth_header|
728 # remove node 5 (5,5) from an existing relation
729 relation_xml = xml_for_relation(relation)
731 .find("//osm/relation/member[@type='node'][@ref='#{node2.id}']")
734 # update changeset ID to point to new changeset
735 update_changeset(relation_xml, changeset_id)
738 put api_relation_path(relation), :params => relation_xml.to_s, :headers => auth_header
739 assert_response :success, "can't update relation for remove node/bbox test"
744 # check that relations are ordered
745 def test_relation_member_ordering
747 changeset = create(:changeset, :user => user)
748 node1 = create(:node)
749 node2 = create(:node)
750 node3 = create(:node)
751 way1 = create(:way_with_nodes, :nodes_count => 2)
752 way2 = create(:way_with_nodes, :nodes_count => 2)
754 auth_header = bearer_authorization_header user
758 <relation changeset='#{changeset.id}'>
759 <member ref='#{node1.id}' type='node' role='first'/>
760 <member ref='#{node2.id}' type='node' role='second'/>
761 <member ref='#{way1.id}' type='way' role='third'/>
762 <member ref='#{way2.id}' type='way' role='fourth'/>
766 doc = XML::Parser.string(doc_str).parse
768 post api_relations_path, :params => doc.to_s, :headers => auth_header
769 assert_response :success, "can't create a relation: #{@response.body}"
770 relation_id = @response.body.to_i
772 # get it back and check the ordering
773 get api_relation_path(relation_id)
774 assert_response :success, "can't read back the relation: #{@response.body}"
775 check_ordering(doc, @response.body)
777 # insert a member at the front
778 new_member = XML::Node.new "member"
779 new_member["ref"] = node3.id.to_s
780 new_member["type"] = "node"
781 new_member["role"] = "new first"
782 doc.find("//osm/relation").first.child.prev = new_member
783 # update the version, should be 1?
784 doc.find("//osm/relation").first["id"] = relation_id.to_s
785 doc.find("//osm/relation").first["version"] = 1.to_s
787 # upload the next version of the relation
788 put api_relation_path(relation_id), :params => doc.to_s, :headers => auth_header
789 assert_response :success, "can't update relation: #{@response.body}"
790 assert_equal 2, @response.body.to_i
792 # get it back again and check the ordering again
793 get api_relation_path(relation_id)
794 assert_response :success, "can't read back the relation: #{@response.body}"
795 check_ordering(doc, @response.body)
797 # check the ordering in the history tables:
798 with_controller(OldRelationsController.new) do
799 get api_relation_version_path(relation_id, 2)
800 assert_response :success, "can't read back version 2 of the relation #{relation_id}"
801 check_ordering(doc, @response.body)
806 # check that relations can contain duplicate members
807 def test_relation_member_duplicates
808 private_user = create(:user, :data_public => false)
810 changeset = create(:changeset, :user => user)
811 node1 = create(:node)
812 node2 = create(:node)
816 <relation changeset='#{changeset.id}'>
817 <member ref='#{node1.id}' type='node' role='forward'/>
818 <member ref='#{node2.id}' type='node' role='forward'/>
819 <member ref='#{node1.id}' type='node' role='forward'/>
820 <member ref='#{node2.id}' type='node' role='forward'/>
824 doc = XML::Parser.string(doc_str).parse
826 ## First try with the private user
827 auth_header = bearer_authorization_header private_user
829 post api_relations_path, :params => doc.to_s, :headers => auth_header
830 assert_response :forbidden
832 ## Now try with the public user
833 auth_header = bearer_authorization_header user
835 post api_relations_path, :params => doc.to_s, :headers => auth_header
836 assert_response :success, "can't create a relation: #{@response.body}"
837 relation_id = @response.body.to_i
839 # get it back and check the ordering
840 get api_relation_path(relation_id)
841 assert_response :success, "can't read back the relation: #{relation_id}"
842 check_ordering(doc, @response.body)
846 # test that the ordering of elements in the history is the same as in current.
847 def test_history_ordering
849 changeset = create(:changeset, :user => user)
850 node1 = create(:node)
851 node2 = create(:node)
852 node3 = create(:node)
853 node4 = create(:node)
857 <relation changeset='#{changeset.id}'>
858 <member ref='#{node1.id}' type='node' role='forward'/>
859 <member ref='#{node4.id}' type='node' role='forward'/>
860 <member ref='#{node3.id}' type='node' role='forward'/>
861 <member ref='#{node2.id}' type='node' role='forward'/>
865 doc = XML::Parser.string(doc_str).parse
866 auth_header = bearer_authorization_header user
868 post api_relations_path, :params => doc.to_s, :headers => auth_header
869 assert_response :success, "can't create a relation: #{@response.body}"
870 relation_id = @response.body.to_i
872 # check the ordering in the current tables:
873 get api_relation_path(relation_id)
874 assert_response :success, "can't read back the relation: #{@response.body}"
875 check_ordering(doc, @response.body)
877 # check the ordering in the history tables:
878 with_controller(OldRelationsController.new) do
879 get api_relation_version_path(relation_id, 1)
880 assert_response :success, "can't read back version 1 of the relation: #{@response.body}"
881 check_ordering(doc, @response.body)
886 # remove all the members from a relation. the result is pretty useless, but
887 # still technically valid.
888 def test_remove_all_members
889 relation = create(:relation)
890 node1 = create(:node, :lat => 0.3, :lon => 0.3)
891 node2 = create(:node, :lat => 0.5, :lon => 0.5)
893 create(:way_node, :way => way, :node => node1)
894 create(:relation_member, :relation => relation, :member => way)
895 create(:relation_member, :relation => relation, :member => node2)
897 check_changeset_modify(BoundingBox.new(0.3, 0.3, 0.5, 0.5)) do |changeset_id, auth_header|
898 relation_xml = xml_for_relation(relation)
900 .find("//osm/relation/member")
903 # update changeset ID to point to new changeset
904 update_changeset(relation_xml, changeset_id)
907 put api_relation_path(relation), :params => relation_xml.to_s, :headers => auth_header
908 assert_response :success, "can't update relation for remove all members test"
909 checkrelation = Relation.find(relation.id)
910 assert_not_nil(checkrelation,
911 "uploaded relation not found in database after upload")
912 assert_equal(0, checkrelation.members.length,
913 "relation contains members but they should have all been deleted")
918 # test initial rate limit
919 def test_initial_rate_limit
924 node1 = create(:node)
925 node2 = create(:node)
927 # create a changeset that puts us near the initial rate limit
928 changeset = create(:changeset, :user => user,
929 :created_at => Time.now.utc - 5.minutes,
930 :num_changes => Settings.initial_changes_per_hour - 1)
932 # create authentication header
933 auth_header = bearer_authorization_header user
935 # try creating a relation
936 xml = "<osm><relation changeset='#{changeset.id}'>" \
937 "<member ref='#{node1.id}' type='node' role='some'/>" \
938 "<member ref='#{node2.id}' type='node' role='some'/>" \
939 "<tag k='test' v='yes' /></relation></osm>"
940 post api_relations_path, :params => xml, :headers => auth_header
941 assert_response :success, "relation create did not return success status"
943 # get the id of the relation we created
944 relationid = @response.body
946 # try updating the relation, which should be rate limited
947 xml = "<osm><relation id='#{relationid}' version='1' changeset='#{changeset.id}'>" \
948 "<member ref='#{node2.id}' type='node' role='some'/>" \
949 "<member ref='#{node1.id}' type='node' role='some'/>" \
950 "<tag k='test' v='yes' /></relation></osm>"
951 put api_relation_path(relationid), :params => xml, :headers => auth_header
952 assert_response :too_many_requests, "relation update did not hit rate limit"
954 # try deleting the relation, which should be rate limited
955 xml = "<osm><relation id='#{relationid}' version='2' changeset='#{changeset.id}'/></osm>"
956 delete api_relation_path(relationid), :params => xml, :headers => auth_header
957 assert_response :too_many_requests, "relation delete did not hit rate limit"
959 # try creating a relation, which should be rate limited
960 xml = "<osm><relation changeset='#{changeset.id}'>" \
961 "<member ref='#{node1.id}' type='node' role='some'/>" \
962 "<member ref='#{node2.id}' type='node' role='some'/>" \
963 "<tag k='test' v='yes' /></relation></osm>"
964 post api_relations_path, :params => xml, :headers => auth_header
965 assert_response :too_many_requests, "relation create did not hit rate limit"
969 # test maximum rate limit
970 def test_maximum_rate_limit
975 node1 = create(:node)
976 node2 = create(:node)
978 # create a changeset to establish our initial edit time
979 changeset = create(:changeset, :user => user,
980 :created_at => Time.now.utc - 28.days)
982 # create changeset to put us near the maximum rate limit
983 total_changes = Settings.max_changes_per_hour - 1
984 while total_changes.positive?
985 changes = [total_changes, Changeset::MAX_ELEMENTS].min
986 changeset = create(:changeset, :user => user,
987 :created_at => Time.now.utc - 5.minutes,
988 :num_changes => changes)
989 total_changes -= changes
992 # create authentication header
993 auth_header = bearer_authorization_header user
995 # try creating a relation
996 xml = "<osm><relation changeset='#{changeset.id}'>" \
997 "<member ref='#{node1.id}' type='node' role='some'/>" \
998 "<member ref='#{node2.id}' type='node' role='some'/>" \
999 "<tag k='test' v='yes' /></relation></osm>"
1000 post api_relations_path, :params => xml, :headers => auth_header
1001 assert_response :success, "relation create did not return success status"
1003 # get the id of the relation we created
1004 relationid = @response.body
1006 # try updating the relation, which should be rate limited
1007 xml = "<osm><relation id='#{relationid}' version='1' changeset='#{changeset.id}'>" \
1008 "<member ref='#{node2.id}' type='node' role='some'/>" \
1009 "<member ref='#{node1.id}' type='node' role='some'/>" \
1010 "<tag k='test' v='yes' /></relation></osm>"
1011 put api_relation_path(relationid), :params => xml, :headers => auth_header
1012 assert_response :too_many_requests, "relation update did not hit rate limit"
1014 # try deleting the relation, which should be rate limited
1015 xml = "<osm><relation id='#{relationid}' version='2' changeset='#{changeset.id}'/></osm>"
1016 delete api_relation_path(relationid), :params => xml, :headers => auth_header
1017 assert_response :too_many_requests, "relation delete did not hit rate limit"
1019 # try creating a relation, which should be rate limited
1020 xml = "<osm><relation changeset='#{changeset.id}'>" \
1021 "<member ref='#{node1.id}' type='node' role='some'/>" \
1022 "<member ref='#{node2.id}' type='node' role='some'/>" \
1023 "<tag k='test' v='yes' /></relation></osm>"
1024 post api_relations_path, :params => xml, :headers => auth_header
1025 assert_response :too_many_requests, "relation create did not hit rate limit"
1031 # checks that the XML document and the string arguments have
1032 # members in the same order.
1033 def check_ordering(doc, xml)
1034 new_doc = XML::Parser.string(xml).parse
1036 doc_members = doc.find("//osm/relation/member").collect do |m|
1037 [m["ref"].to_i, m["type"].to_sym, m["role"]]
1040 new_members = new_doc.find("//osm/relation/member").collect do |m|
1041 [m["ref"].to_i, m["type"].to_sym, m["role"]]
1044 assert_equal doc_members, new_members, "members are not equal - ordering is wrong? (#{doc}, #{xml})"
1048 # create a changeset and yield to the caller to set it up, then assert
1049 # that the changeset bounding box is +bbox+.
1050 def check_changeset_modify(bbox)
1051 ## First test with the private user to check that you get a forbidden
1052 auth_header = bearer_authorization_header create(:user, :data_public => false)
1054 # create a new changeset for this operation, so we are assured
1055 # that the bounding box will be newly-generated.
1056 with_controller(Api::ChangesetsController.new) do
1057 xml = "<osm><changeset/></osm>"
1058 post api_changesets_path, :params => xml, :headers => auth_header
1059 assert_response :forbidden, "shouldn't be able to create changeset for modify test, as should get forbidden"
1062 ## Now do the whole thing with the public user
1063 auth_header = bearer_authorization_header
1065 # create a new changeset for this operation, so we are assured
1066 # that the bounding box will be newly-generated.
1067 changeset_id = with_controller(Api::ChangesetsController.new) do
1068 xml = "<osm><changeset/></osm>"
1069 post api_changesets_path, :params => xml, :headers => auth_header
1070 assert_response :success, "couldn't create changeset for modify test"
1074 # go back to the block to do the actual modifies
1075 yield changeset_id, auth_header
1077 # now download the changeset to check its bounding box
1078 with_controller(Api::ChangesetsController.new) do
1079 get api_changeset_path(changeset_id)
1080 assert_response :success, "can't re-read changeset for modify test"
1081 assert_select "osm>changeset", 1, "Changeset element doesn't exist in #{@response.body}"
1082 assert_select "osm>changeset[id='#{changeset_id}']", 1, "Changeset id=#{changeset_id} doesn't exist in #{@response.body}"
1083 assert_select "osm>changeset[min_lon='#{format('%<lon>.7f', :lon => bbox.min_lon)}']", 1, "Changeset min_lon wrong in #{@response.body}"
1084 assert_select "osm>changeset[min_lat='#{format('%<lat>.7f', :lat => bbox.min_lat)}']", 1, "Changeset min_lat wrong in #{@response.body}"
1085 assert_select "osm>changeset[max_lon='#{format('%<lon>.7f', :lon => bbox.max_lon)}']", 1, "Changeset max_lon wrong in #{@response.body}"
1086 assert_select "osm>changeset[max_lat='#{format('%<lat>.7f', :lat => bbox.max_lat)}']", 1, "Changeset max_lat wrong in #{@response.body}"
1091 # updates the relation (XML) +rel+ and
1092 # yields the new version of that relation into the block.
1093 # the parsed XML doc is returned.
1094 def with_update(rel, headers)
1095 rel_id = rel.find("//osm/relation").first["id"].to_i
1096 put api_relation_path(rel_id), :params => rel.to_s, :headers => headers
1097 assert_response :success, "can't update relation: #{@response.body}"
1098 version = @response.body.to_i
1100 # now get the new version
1101 get api_relation_path(rel_id)
1102 assert_response :success
1103 new_rel = xml_parse(@response.body)
1111 # updates the relation (XML) +rel+ via the diff-upload API and
1112 # yields the new version of that relation into the block.
1113 # the parsed XML doc is returned.
1114 def with_update_diff(rel, headers)
1115 rel_id = rel.find("//osm/relation").first["id"].to_i
1116 cs_id = rel.find("//osm/relation").first["changeset"].to_i
1119 with_controller(Api::ChangesetsController.new) do
1120 doc = OSM::API.new.xml_doc
1121 change = XML::Node.new "osmChange"
1123 modify = XML::Node.new "modify"
1125 modify << doc.import(rel.find("//osm/relation").first)
1127 post api_changeset_upload_path(cs_id), :params => doc.to_s, :headers => headers
1128 assert_response :success, "can't upload diff relation: #{@response.body}"
1129 version = xml_parse(@response.body).find("//diffResult/relation").first["new_version"].to_i
1132 # now get the new version
1133 get api_relation_path(rel_id)
1134 assert_response :success
1135 new_rel = xml_parse(@response.body)
1143 # returns a k->v hash of tags from an xml doc
1144 def get_tags_as_hash(a)
1145 a.find("//osm/relation/tag").to_h do |tag|
1146 [tag["k"], tag["v"]]
1151 # assert that all tags on relation documents +a+ and +b+
1153 def assert_tags_equal(a, b)
1154 # turn the XML doc into tags hashes
1155 a_tags = get_tags_as_hash(a)
1156 b_tags = get_tags_as_hash(b)
1158 assert_equal a_tags, b_tags, "Tags should be identical."
1162 # update the changeset_id of a node element
1163 def update_changeset(xml, changeset_id)
1164 xml_attr_rewrite(xml, "changeset", changeset_id)
1168 # update an attribute in the node element
1169 def xml_attr_rewrite(xml, name, value)
1170 xml.find("//osm/relation").first[name] = value.to_s
1177 parser = XML::Parser.string(xml)