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
223 <relation changeset='#{private_changeset.id}'>
224 <tag k='test' v='yes' />
228 post api_relations_path, :params => xml, :headers => auth_header
229 # hope for forbidden, due to user
230 assert_response :forbidden,
231 "relation upload should have failed with forbidden"
234 # create an relation with a node as member
235 # This time try with a role attribute in the relation
238 <relation changeset='#{private_changeset.id}'>
239 <member ref='#{node.id}' type='node' role='some'/>
240 <tag k='test' v='yes' />
244 post api_relations_path, :params => xml, :headers => auth_header
245 # hope for forbidden due to user
246 assert_response :forbidden,
247 "relation upload did not return forbidden status"
250 # create an relation with a node as member, this time test that we don't
251 # need a role attribute to be included
254 <relation changeset='#{private_changeset.id}'>
255 <member ref='#{node.id}' type='node'/>
256 <tag k='test' v='yes' />
260 post api_relations_path, :params => xml, :headers => auth_header
261 # hope for forbidden due to user
262 assert_response :forbidden,
263 "relation upload did not return forbidden status"
266 # create an relation with a way and a node as members
269 <relation changeset='#{private_changeset.id}'>
270 <member type='node' ref='#{node.id}' role='some'/>
271 <member type='way' ref='#{way.id}' role='other'/>
272 <tag k='test' v='yes' />
276 post api_relations_path, :params => xml, :headers => auth_header
277 # hope for forbidden, due to user
278 assert_response :forbidden,
279 "relation upload did not return success status"
281 ## Now try with the public user
282 auth_header = bearer_authorization_header user
284 # create an relation without members
287 <relation changeset='#{changeset.id}'>
288 <tag k='test' v='yes' />
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(0, checkrelation.members.length, "saved relation contains members but should not")
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?
311 get api_relation_path(relationid)
312 assert_response :success
315 # create an relation with a node as member
316 # This time try with a role attribute in the relation
319 <relation changeset='#{changeset.id}'>
320 <member ref='#{node.id}' type='node' role='some'/>
321 <tag k='test' v='yes' />
325 post api_relations_path, :params => xml, :headers => auth_header
327 assert_response :success,
328 "relation upload did not return success status"
329 # read id of created relation and search for it
330 relationid = @response.body
331 checkrelation = Relation.find(relationid)
332 assert_not_nil checkrelation,
333 "uploaded relation not found in data base after upload"
335 assert_equal(1, checkrelation.members.length, "saved relation does not contain exactly one member")
336 assert_equal(1, checkrelation.tags.length, "saved relation does not contain exactly one tag")
337 assert_equal changeset.id, checkrelation.changeset.id,
338 "saved relation does not belong in the changeset it was assigned to"
339 assert_equal user.id, checkrelation.changeset.user_id,
340 "saved relation does not belong to user that created it"
341 assert checkrelation.visible,
342 "saved relation is not visible"
343 # ok the relation is there but can we also retrieve it?
345 get api_relation_path(relationid)
346 assert_response :success
349 # create an relation with a node as member, this time test that we don't
350 # need a role attribute to be included
353 <relation changeset='#{changeset.id}'>
354 <member ref='#{node.id}' type='node'/>
355 <tag k='test' v='yes' />
359 post api_relations_path, :params => xml, :headers => auth_header
361 assert_response :success,
362 "relation upload did not return success status"
363 # read id of created relation and search for it
364 relationid = @response.body
365 checkrelation = Relation.find(relationid)
366 assert_not_nil checkrelation,
367 "uploaded relation not found in data base after upload"
369 assert_equal(1, checkrelation.members.length, "saved relation does not contain exactly one member")
370 assert_equal(1, checkrelation.tags.length, "saved relation does not contain exactly one tag")
371 assert_equal changeset.id, checkrelation.changeset.id,
372 "saved relation does not belong in the changeset it was assigned to"
373 assert_equal user.id, checkrelation.changeset.user_id,
374 "saved relation does not belong to user that created it"
375 assert checkrelation.visible,
376 "saved relation is not visible"
377 # ok the relation is there but can we also retrieve it?
379 get api_relation_path(relationid)
380 assert_response :success
383 # create an relation with a way and a node as members
386 <relation changeset='#{changeset.id}'>
387 <member type='node' ref='#{node.id}' role='some'/>
388 <member type='way' ref='#{way.id}' role='other'/>
389 <tag k='test' v='yes' />
393 post api_relations_path, :params => xml, :headers => auth_header
395 assert_response :success,
396 "relation upload did not return success status"
397 # read id of created relation and search for it
398 relationid = @response.body
399 checkrelation = Relation.find(relationid)
400 assert_not_nil checkrelation,
401 "uploaded relation not found in data base after upload"
403 assert_equal(2, checkrelation.members.length, "saved relation does not have exactly two members")
404 assert_equal(1, checkrelation.tags.length, "saved relation does not contain exactly one tag")
405 assert_equal changeset.id, checkrelation.changeset.id,
406 "saved relation does not belong in the changeset it was assigned to"
407 assert_equal user.id, checkrelation.changeset.user_id,
408 "saved relation does not belong to user that created it"
409 assert checkrelation.visible,
410 "saved relation is not visible"
411 # ok the relation is there but can we also retrieve it?
412 get api_relation_path(relationid)
413 assert_response :success
416 # ------------------------------------
417 # Test updating relations
418 # ------------------------------------
420 def test_update_wrong_id
422 changeset = create(:changeset, :user => user)
423 relation = create(:relation)
424 other_relation = create(:relation)
426 auth_header = bearer_authorization_header user
427 get api_relation_path(relation)
428 assert_response :success
429 rel = XML::Parser.string(@response.body).parse
431 update_changeset(rel, changeset.id)
432 put api_relation_path(other_relation), :params => rel.to_s, :headers => auth_header
433 assert_response :bad_request
436 # -------------------------------------
437 # Test creating some invalid relations.
438 # -------------------------------------
440 def test_create_invalid
442 changeset = create(:changeset, :user => user)
444 auth_header = bearer_authorization_header user
446 # create a relation with non-existing node as member
449 <relation changeset='#{changeset.id}'>
450 <member type='node' ref='0'/>
454 post api_relations_path, :params => xml, :headers => auth_header
456 assert_response :precondition_failed,
457 "relation upload with invalid node did not return 'precondition failed'"
458 assert_equal "Precondition failed: Relation with id cannot be saved due to Node with id 0", @response.body
461 # -------------------------------------
462 # Test creating a relation, with some invalid XML
463 # -------------------------------------
464 def test_create_invalid_xml
466 changeset = create(:changeset, :user => user)
469 auth_header = bearer_authorization_header user
471 # create some xml that should return an error
474 <relation changeset='#{changeset.id}'>
475 <member type='type' ref='#{node.id}' role=''/>
479 post api_relations_path, :params => xml, :headers => auth_header
481 assert_response :bad_request
482 assert_match(/Cannot parse valid relation from xml string/, @response.body)
483 assert_match(/The type is not allowed only, /, @response.body)
486 # -------------------------------------
487 # Test deleting relations.
488 # -------------------------------------
491 private_user = create(:user, :data_public => false)
492 private_user_closed_changeset = create(:changeset, :closed, :user => private_user)
494 closed_changeset = create(:changeset, :closed, :user => user)
495 changeset = create(:changeset, :user => user)
496 relation = create(:relation)
497 used_relation = create(:relation)
498 super_relation = create(:relation_member, :member => used_relation).relation
499 deleted_relation = create(:relation, :deleted)
500 multi_tag_relation = create(:relation)
501 create_list(:relation_tag, 4, :relation => multi_tag_relation)
503 ## First try to delete relation without auth
504 delete api_relation_path(relation)
505 assert_response :unauthorized
507 ## Then try with the private user, to make sure that you get a forbidden
508 auth_header = bearer_authorization_header private_user
510 # this shouldn't work, as we should need the payload...
511 delete api_relation_path(relation), :headers => auth_header
512 assert_response :forbidden
514 # try to delete without specifying a changeset
515 xml = "<osm><relation id='#{relation.id}'/></osm>"
516 delete api_relation_path(relation), :params => xml.to_s, :headers => auth_header
517 assert_response :forbidden
519 # try to delete with an invalid (closed) changeset
520 xml = update_changeset(xml_for_relation(relation),
521 private_user_closed_changeset.id)
522 delete api_relation_path(relation), :params => xml.to_s, :headers => auth_header
523 assert_response :forbidden
525 # try to delete with an invalid (non-existent) changeset
526 xml = update_changeset(xml_for_relation(relation), 0)
527 delete api_relation_path(relation), :params => xml.to_s, :headers => auth_header
528 assert_response :forbidden
530 # this won't work because the relation is in-use by another relation
531 xml = xml_for_relation(used_relation)
532 delete api_relation_path(used_relation), :params => xml.to_s, :headers => auth_header
533 assert_response :forbidden
535 # this should work when we provide the appropriate payload...
536 xml = xml_for_relation(relation)
537 delete api_relation_path(relation), :params => xml.to_s, :headers => auth_header
538 assert_response :forbidden
540 # this won't work since the relation is already deleted
541 xml = xml_for_relation(deleted_relation)
542 delete api_relation_path(deleted_relation), :params => xml.to_s, :headers => auth_header
543 assert_response :forbidden
545 # this won't work since the relation never existed
546 delete api_relation_path(0), :headers => auth_header
547 assert_response :forbidden
549 ## now set auth for the public user
550 auth_header = bearer_authorization_header user
552 # this shouldn't work, as we should need the payload...
553 delete api_relation_path(relation), :headers => auth_header
554 assert_response :bad_request
556 # try to delete without specifying a changeset
557 xml = "<osm><relation id='#{relation.id}' version='#{relation.version}' /></osm>"
558 delete api_relation_path(relation), :params => xml.to_s, :headers => auth_header
559 assert_response :bad_request
560 assert_match(/Changeset id is missing/, @response.body)
562 # try to delete with an invalid (closed) changeset
563 xml = update_changeset(xml_for_relation(relation),
565 delete api_relation_path(relation), :params => xml.to_s, :headers => auth_header
566 assert_response :conflict
568 # try to delete with an invalid (non-existent) changeset
569 xml = update_changeset(xml_for_relation(relation), 0)
570 delete api_relation_path(relation), :params => xml.to_s, :headers => auth_header
571 assert_response :conflict
573 # this won't work because the relation is in a changeset owned by someone else
574 xml = update_changeset(xml_for_relation(relation), create(:changeset).id)
575 delete api_relation_path(relation), :params => xml.to_s, :headers => auth_header
576 assert_response :conflict,
577 "shouldn't be able to delete a relation in a changeset owned by someone else (#{@response.body})"
579 # this won't work because the relation in the payload is different to that passed
580 xml = update_changeset(xml_for_relation(relation), changeset.id)
581 delete api_relation_path(create(:relation)), :params => xml.to_s, :headers => auth_header
582 assert_response :bad_request, "shouldn't be able to delete a relation when payload is different to the url"
584 # this won't work because the relation is in-use by another relation
585 xml = update_changeset(xml_for_relation(used_relation), changeset.id)
586 delete api_relation_path(used_relation), :params => xml.to_s, :headers => auth_header
587 assert_response :precondition_failed,
588 "shouldn't be able to delete a relation used in a relation (#{@response.body})"
589 assert_equal "Precondition failed: The relation #{used_relation.id} is used in relation #{super_relation.id}.", @response.body
591 # this should work when we provide the appropriate payload...
592 xml = update_changeset(xml_for_relation(multi_tag_relation), changeset.id)
593 delete api_relation_path(multi_tag_relation), :params => xml.to_s, :headers => auth_header
594 assert_response :success
596 # valid delete should return the new version number, which should
597 # be greater than the old version number
598 assert_operator @response.body.to_i, :>, multi_tag_relation.version, "delete request should return a new version number for relation"
600 # this won't work since the relation is already deleted
601 xml = update_changeset(xml_for_relation(deleted_relation), changeset.id)
602 delete api_relation_path(deleted_relation), :params => xml.to_s, :headers => auth_header
603 assert_response :gone
605 # Public visible relation needs to be deleted
606 xml = update_changeset(xml_for_relation(super_relation), changeset.id)
607 delete api_relation_path(super_relation), :params => xml.to_s, :headers => auth_header
608 assert_response :success
610 # this works now because the relation which was using this one
612 xml = update_changeset(xml_for_relation(used_relation), changeset.id)
613 delete api_relation_path(used_relation), :params => xml.to_s, :headers => auth_header
614 assert_response :success,
615 "should be able to delete a relation used in an old relation (#{@response.body})"
617 # this won't work since the relation never existed
618 delete api_relation_path(0), :headers => auth_header
619 assert_response :not_found
623 # test initial rate limit
624 def test_initial_rate_limit
629 node1 = create(:node)
630 node2 = create(:node)
632 # create a changeset that puts us near the initial rate limit
633 changeset = create(:changeset, :user => user,
634 :created_at => Time.now.utc - 5.minutes,
635 :num_changes => Settings.initial_changes_per_hour - 1)
637 # create authentication header
638 auth_header = bearer_authorization_header user
640 # try creating a relation
643 <relation changeset='#{changeset.id}'>
644 <member ref='#{node1.id}' type='node' role='some'/>
645 <member ref='#{node2.id}' type='node' role='some'/>
649 post api_relations_path, :params => xml, :headers => auth_header
650 assert_response :success, "relation create did not return success status"
652 # get the id of the relation we created
653 relationid = @response.body
655 # try updating the relation, which should be rate limited
658 <relation id='#{relationid}' version='1' changeset='#{changeset.id}'>
659 <member ref='#{node2.id}' type='node' role='some'/>
660 <member ref='#{node1.id}' type='node' role='some'/>
664 put api_relation_path(relationid), :params => xml, :headers => auth_header
665 assert_response :too_many_requests, "relation update did not hit rate limit"
667 # try deleting the relation, which should be rate limited
668 xml = "<osm><relation id='#{relationid}' version='2' changeset='#{changeset.id}'/></osm>"
669 delete api_relation_path(relationid), :params => xml, :headers => auth_header
670 assert_response :too_many_requests, "relation delete did not hit rate limit"
672 # try creating a relation, which should be rate limited
675 <relation changeset='#{changeset.id}'>
676 <member ref='#{node1.id}' type='node' role='some'/>
677 <member ref='#{node2.id}' type='node' role='some'/>
681 post api_relations_path, :params => xml, :headers => auth_header
682 assert_response :too_many_requests, "relation create did not hit rate limit"
686 # test maximum rate limit
687 def test_maximum_rate_limit
692 node1 = create(:node)
693 node2 = create(:node)
695 # create a changeset to establish our initial edit time
696 changeset = create(:changeset, :user => user,
697 :created_at => Time.now.utc - 28.days)
699 # create changeset to put us near the maximum rate limit
700 total_changes = Settings.max_changes_per_hour - 1
701 while total_changes.positive?
702 changes = [total_changes, Changeset::MAX_ELEMENTS].min
703 changeset = create(:changeset, :user => user,
704 :created_at => Time.now.utc - 5.minutes,
705 :num_changes => changes)
706 total_changes -= changes
709 # create authentication header
710 auth_header = bearer_authorization_header user
712 # try creating a relation
715 <relation changeset='#{changeset.id}'>
716 <member ref='#{node1.id}' type='node' role='some'/>
717 <member ref='#{node2.id}' type='node' role='some'/>
721 post api_relations_path, :params => xml, :headers => auth_header
722 assert_response :success, "relation create did not return success status"
724 # get the id of the relation we created
725 relationid = @response.body
727 # try updating the relation, which should be rate limited
730 <relation id='#{relationid}' version='1' changeset='#{changeset.id}'>
731 <member ref='#{node2.id}' type='node' role='some'/>
732 <member ref='#{node1.id}' type='node' role='some'/>
736 put api_relation_path(relationid), :params => xml, :headers => auth_header
737 assert_response :too_many_requests, "relation update did not hit rate limit"
739 # try deleting the relation, which should be rate limited
740 xml = "<osm><relation id='#{relationid}' version='2' changeset='#{changeset.id}'/></osm>"
741 delete api_relation_path(relationid), :params => xml, :headers => auth_header
742 assert_response :too_many_requests, "relation delete did not hit rate limit"
744 # try creating a relation, which should be rate limited
747 <relation changeset='#{changeset.id}'>
748 <member ref='#{node1.id}' type='node' role='some'/>
749 <member ref='#{node2.id}' type='node' role='some'/>
753 post api_relations_path, :params => xml, :headers => auth_header
754 assert_response :too_many_requests, "relation create did not hit rate limit"
760 # update the changeset_id of a node element
761 def update_changeset(xml, changeset_id)
762 xml_attr_rewrite(xml, "changeset", changeset_id)
766 # update an attribute in the node element
767 def xml_attr_rewrite(xml, name, value)
768 xml.find("//osm/relation").first[name] = value.to_s