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 => "full", :id => "1" }
33 { :path => "/api/0.6/relation/1/full.json", :method => :get },
34 { :controller => "api/relations", :action => "full", :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 { :path => "/api/0.6/node/1/relations", :method => :get },
47 { :controller => "api/relations", :action => "relations_for_node", :id => "1" }
50 { :path => "/api/0.6/way/1/relations", :method => :get },
51 { :controller => "api/relations", :action => "relations_for_way", :id => "1" }
54 { :path => "/api/0.6/relation/1/relations", :method => :get },
55 { :controller => "api/relations", :action => "relations_for_relation", :id => "1" }
58 { :path => "/api/0.6/node/1/relations.json", :method => :get },
59 { :controller => "api/relations", :action => "relations_for_node", :id => "1", :format => "json" }
62 { :path => "/api/0.6/way/1/relations.json", :method => :get },
63 { :controller => "api/relations", :action => "relations_for_way", :id => "1", :format => "json" }
66 { :path => "/api/0.6/relation/1/relations.json", :method => :get },
67 { :controller => "api/relations", :action => "relations_for_relation", :id => "1", :format => "json" }
71 { :controller => "api/relations", :action => "create" },
72 { :path => "/api/0.6/relation/create", :method => :put }
77 # test fetching multiple relations
79 relation1 = create(:relation)
80 relation2 = create(:relation, :deleted)
81 relation3 = create(:relation, :with_history, :version => 2)
82 relation4 = create(:relation, :with_history, :version => 2)
83 relation4.old_relations.find_by(:version => 1).redact!(create(:redaction))
85 # check error when no parameter provided
86 get api_relations_path
87 assert_response :bad_request
89 # check error when no parameter value provided
90 get api_relations_path(:relations => "")
91 assert_response :bad_request
94 get api_relations_path(:relations => "#{relation1.id},#{relation2.id},#{relation3.id},#{relation4.id}")
95 assert_response :success
96 assert_select "osm" do
97 assert_select "relation", :count => 4
98 assert_select "relation[id='#{relation1.id}'][visible='true']", :count => 1
99 assert_select "relation[id='#{relation2.id}'][visible='false']", :count => 1
100 assert_select "relation[id='#{relation3.id}'][visible='true']", :count => 1
101 assert_select "relation[id='#{relation4.id}'][visible='true']", :count => 1
104 # test a working call with json format
105 get api_relations_path(:relations => "#{relation1.id},#{relation2.id},#{relation3.id},#{relation4.id}", :format => "json")
107 js = ActiveSupport::JSON.decode(@response.body)
109 assert_equal 4, js["elements"].count
110 assert_equal 4, (js["elements"].count { |a| a["type"] == "relation" })
111 assert_equal 1, (js["elements"].count { |a| a["id"] == relation1.id && a["visible"].nil? })
112 assert_equal 1, (js["elements"].count { |a| a["id"] == relation2.id && a["visible"] == false })
113 assert_equal 1, (js["elements"].count { |a| a["id"] == relation3.id && a["visible"].nil? })
114 assert_equal 1, (js["elements"].count { |a| a["id"] == relation4.id && a["visible"].nil? })
116 # check error when a non-existent relation is included
117 get api_relations_path(:relations => "#{relation1.id},#{relation2.id},#{relation3.id},#{relation4.id},0")
118 assert_response :not_found
121 # -------------------------------------
122 # Test showing relations.
123 # -------------------------------------
125 def test_show_not_found
126 get api_relation_path(0)
127 assert_response :not_found
130 def test_show_deleted
131 get api_relation_path(create(:relation, :deleted))
132 assert_response :gone
136 relation = create(:relation, :timestamp => "2021-02-03T00:00:00Z")
137 node = create(:node, :timestamp => "2021-04-05T00:00:00Z")
138 create(:relation_member, :relation => relation, :member => node)
140 get api_relation_path(relation)
142 assert_response :success
143 assert_not_nil @response.header["Last-Modified"]
144 assert_equal "2021-02-03T00:00:00Z", Time.parse(@response.header["Last-Modified"]).utc.xmlschema
145 assert_dom "node", :count => 0
146 assert_dom "relation", :count => 1 do
147 assert_dom "> @id", :text => relation.id.to_s
151 def test_full_not_found
152 get relation_full_path(999999)
153 assert_response :not_found
156 def test_full_deleted
157 get relation_full_path(create(:relation, :deleted))
158 assert_response :gone
162 relation = create(:relation)
164 get relation_full_path(relation)
166 assert_response :success
167 assert_dom "relation", :count => 1 do
168 assert_dom "> @id", :text => relation.id.to_s
172 def test_full_with_node_member
173 relation = create(:relation)
175 create(:relation_member, :relation => relation, :member => node)
177 get relation_full_path(relation)
179 assert_response :success
180 assert_dom "node", :count => 1 do
181 assert_dom "> @id", :text => node.id.to_s
183 assert_dom "relation", :count => 1 do
184 assert_dom "> @id", :text => relation.id.to_s
188 def test_full_with_way_member
189 relation = create(:relation)
190 way = create(:way_with_nodes)
191 create(:relation_member, :relation => relation, :member => way)
193 get relation_full_path(relation)
195 assert_response :success
196 assert_dom "node", :count => 1 do
197 assert_dom "> @id", :text => way.nodes[0].id.to_s
199 assert_dom "way", :count => 1 do
200 assert_dom "> @id", :text => way.id.to_s
202 assert_dom "relation", :count => 1 do
203 assert_dom "> @id", :text => relation.id.to_s
208 # check that all relations containing a particular node, and no extra
209 # relations, are returned from the relations_for_node call.
210 def test_relations_for_node
212 # should include relations with that node as a member
213 relation_with_node = create(:relation_member, :member => node).relation
214 # should ignore relations without that node as a member
215 _relation_without_node = create(:relation_member).relation
216 # should ignore relations with the node involved indirectly, via a way
217 way = create(:way_node, :node => node).way
218 _relation_with_way = create(:relation_member, :member => way).relation
219 # should ignore relations with the node involved indirectly, via a relation
220 second_relation = create(:relation_member, :member => node).relation
221 _super_relation = create(:relation_member, :member => second_relation).relation
222 # should combine multiple relation_member references into just one relation entry
223 create(:relation_member, :member => node, :relation => relation_with_node)
224 # should not include deleted relations
225 deleted_relation = create(:relation, :deleted)
226 create(:relation_member, :member => node, :relation => deleted_relation)
228 check_relations_for_element(node_relations_path(node), "node",
230 [relation_with_node, second_relation])
233 def test_relations_for_way
235 # should include relations with that way as a member
236 relation_with_way = create(:relation_member, :member => way).relation
237 # should ignore relations without that way as a member
238 _relation_without_way = create(:relation_member).relation
239 # should ignore relations with the way involved indirectly, via a relation
240 second_relation = create(:relation_member, :member => way).relation
241 _super_relation = create(:relation_member, :member => second_relation).relation
242 # should combine multiple relation_member references into just one relation entry
243 create(:relation_member, :member => way, :relation => relation_with_way)
244 # should not include deleted relations
245 deleted_relation = create(:relation, :deleted)
246 create(:relation_member, :member => way, :relation => deleted_relation)
248 check_relations_for_element(way_relations_path(way), "way",
250 [relation_with_way, second_relation])
253 def test_relations_for_relation
254 relation = create(:relation)
255 # should include relations with that relation as a member
256 relation_with_relation = create(:relation_member, :member => relation).relation
257 # should ignore any relation without that relation as a member
258 _relation_without_relation = create(:relation_member).relation
259 # should ignore relations with the relation involved indirectly, via a relation
260 second_relation = create(:relation_member, :member => relation).relation
261 _super_relation = create(:relation_member, :member => second_relation).relation
262 # should combine multiple relation_member references into just one relation entry
263 create(:relation_member, :member => relation, :relation => relation_with_relation)
264 # should not include deleted relations
265 deleted_relation = create(:relation, :deleted)
266 create(:relation_member, :member => relation, :relation => deleted_relation)
267 check_relations_for_element(relation_relations_path(relation), "relation",
269 [relation_with_relation, second_relation])
272 # -------------------------------------
273 # Test simple relation creation.
274 # -------------------------------------
277 private_user = create(:user, :data_public => false)
278 private_changeset = create(:changeset, :user => private_user)
280 changeset = create(:changeset, :user => user)
282 way = create(:way_with_nodes, :nodes_count => 2)
284 auth_header = bearer_authorization_header private_user
286 # create an relation without members
287 xml = "<osm><relation changeset='#{private_changeset.id}'><tag k='test' v='yes' /></relation></osm>"
288 post api_relations_path, :params => xml, :headers => auth_header
289 # hope for forbidden, due to user
290 assert_response :forbidden,
291 "relation upload should have failed with forbidden"
294 # create an relation with a node as member
295 # This time try with a role attribute in the relation
296 xml = "<osm><relation changeset='#{private_changeset.id}'>" \
297 "<member ref='#{node.id}' type='node' role='some'/>" \
298 "<tag k='test' v='yes' /></relation></osm>"
299 post api_relations_path, :params => xml, :headers => auth_header
300 # hope for forbidden due to user
301 assert_response :forbidden,
302 "relation upload did not return forbidden status"
305 # create an relation with a node as member, this time test that we don't
306 # need a role attribute to be included
307 xml = "<osm><relation changeset='#{private_changeset.id}'>" \
308 "<member ref='#{node.id}' type='node'/><tag k='test' v='yes' /></relation></osm>"
309 post api_relations_path, :params => xml, :headers => auth_header
310 # hope for forbidden due to user
311 assert_response :forbidden,
312 "relation upload did not return forbidden status"
315 # create an relation with a way and a node as members
316 xml = "<osm><relation changeset='#{private_changeset.id}'>" \
317 "<member type='node' ref='#{node.id}' role='some'/>" \
318 "<member type='way' ref='#{way.id}' role='other'/>" \
319 "<tag k='test' v='yes' /></relation></osm>"
320 post api_relations_path, :params => xml, :headers => auth_header
321 # hope for forbidden, due to user
322 assert_response :forbidden,
323 "relation upload did not return success status"
325 ## Now try with the public user
326 auth_header = bearer_authorization_header user
328 # create an relation without members
329 xml = "<osm><relation changeset='#{changeset.id}'><tag k='test' v='yes' /></relation></osm>"
330 post api_relations_path, :params => xml, :headers => auth_header
332 assert_response :success,
333 "relation upload did not return success status"
334 # read id of created relation and search for it
335 relationid = @response.body
336 checkrelation = Relation.find(relationid)
337 assert_not_nil checkrelation,
338 "uploaded relation not found in data base after upload"
340 assert_equal(0, checkrelation.members.length, "saved relation contains members but should not")
341 assert_equal(1, checkrelation.tags.length, "saved relation does not contain exactly one tag")
342 assert_equal changeset.id, checkrelation.changeset.id,
343 "saved relation does not belong in the changeset it was assigned to"
344 assert_equal user.id, checkrelation.changeset.user_id,
345 "saved relation does not belong to user that created it"
346 assert checkrelation.visible,
347 "saved relation is not visible"
348 # ok the relation is there but can we also retrieve it?
349 get api_relation_path(relationid)
350 assert_response :success
353 # create an relation with a node as member
354 # This time try with a role attribute in the relation
355 xml = "<osm><relation changeset='#{changeset.id}'>" \
356 "<member ref='#{node.id}' type='node' role='some'/>" \
357 "<tag k='test' v='yes' /></relation></osm>"
358 post api_relations_path, :params => xml, :headers => auth_header
360 assert_response :success,
361 "relation upload did not return success status"
362 # read id of created relation and search for it
363 relationid = @response.body
364 checkrelation = Relation.find(relationid)
365 assert_not_nil checkrelation,
366 "uploaded relation not found in data base after upload"
368 assert_equal(1, checkrelation.members.length, "saved relation does not contain exactly one member")
369 assert_equal(1, checkrelation.tags.length, "saved relation does not contain exactly one tag")
370 assert_equal changeset.id, checkrelation.changeset.id,
371 "saved relation does not belong in the changeset it was assigned to"
372 assert_equal user.id, checkrelation.changeset.user_id,
373 "saved relation does not belong to user that created it"
374 assert checkrelation.visible,
375 "saved relation is not visible"
376 # ok the relation is there but can we also retrieve it?
378 get api_relation_path(relationid)
379 assert_response :success
382 # create an relation with a node as member, this time test that we don't
383 # need a role attribute to be included
384 xml = "<osm><relation changeset='#{changeset.id}'>" \
385 "<member ref='#{node.id}' type='node'/><tag k='test' v='yes' /></relation></osm>"
386 post api_relations_path, :params => xml, :headers => auth_header
388 assert_response :success,
389 "relation upload did not return success status"
390 # read id of created relation and search for it
391 relationid = @response.body
392 checkrelation = Relation.find(relationid)
393 assert_not_nil checkrelation,
394 "uploaded relation not found in data base after upload"
396 assert_equal(1, checkrelation.members.length, "saved relation does not contain exactly one member")
397 assert_equal(1, checkrelation.tags.length, "saved relation does not contain exactly one tag")
398 assert_equal changeset.id, checkrelation.changeset.id,
399 "saved relation does not belong in the changeset it was assigned to"
400 assert_equal user.id, checkrelation.changeset.user_id,
401 "saved relation does not belong to user that created it"
402 assert checkrelation.visible,
403 "saved relation is not visible"
404 # ok the relation is there but can we also retrieve it?
406 get api_relation_path(relationid)
407 assert_response :success
410 # create an relation with a way and a node as members
411 xml = "<osm><relation changeset='#{changeset.id}'>" \
412 "<member type='node' ref='#{node.id}' role='some'/>" \
413 "<member type='way' ref='#{way.id}' role='other'/>" \
414 "<tag k='test' v='yes' /></relation></osm>"
415 post api_relations_path, :params => xml, :headers => auth_header
417 assert_response :success,
418 "relation upload did not return success status"
419 # read id of created relation and search for it
420 relationid = @response.body
421 checkrelation = Relation.find(relationid)
422 assert_not_nil checkrelation,
423 "uploaded relation not found in data base after upload"
425 assert_equal(2, checkrelation.members.length, "saved relation does not have exactly two members")
426 assert_equal(1, checkrelation.tags.length, "saved relation does not contain exactly one tag")
427 assert_equal changeset.id, checkrelation.changeset.id,
428 "saved relation does not belong in the changeset it was assigned to"
429 assert_equal user.id, checkrelation.changeset.user_id,
430 "saved relation does not belong to user that created it"
431 assert checkrelation.visible,
432 "saved relation is not visible"
433 # ok the relation is there but can we also retrieve it?
434 get api_relation_path(relationid)
435 assert_response :success
438 # ------------------------------------
439 # Test updating relations
440 # ------------------------------------
443 # test that, when tags are updated on a relation, the correct things
444 # happen to the correct tables and the API gives sensible results.
445 # this is to test a case that gregory marler noticed and posted to
447 ## FIXME Move this to an integration test
448 def test_update_relation_tags
450 changeset = create(:changeset, :user => user)
451 relation = create(:relation)
452 create_list(:relation_tag, 4, :relation => relation)
454 auth_header = bearer_authorization_header user
456 with_relation(relation.id) do |rel|
457 # alter one of the tags
458 tag = rel.find("//osm/relation/tag").first
459 tag["v"] = "some changed value"
460 update_changeset(rel, changeset.id)
462 # check that the downloaded tags are the same as the uploaded tags...
463 new_version = with_update(rel, auth_header) do |new_rel|
464 assert_tags_equal rel, new_rel
467 # check the original one in the current_* table again
468 with_relation(relation.id) { |r| assert_tags_equal rel, r }
470 # now check the version in the history
471 with_relation(relation.id, new_version) { |r| assert_tags_equal rel, r }
476 # test that, when tags are updated on a relation when using the diff
477 # upload function, the correct things happen to the correct tables
478 # and the API gives sensible results. this is to test a case that
479 # gregory marler noticed and posted to josm-dev.
480 def test_update_relation_tags_via_upload
482 changeset = create(:changeset, :user => user)
483 relation = create(:relation)
484 create_list(:relation_tag, 4, :relation => relation)
486 auth_header = bearer_authorization_header user
488 with_relation(relation.id) do |rel|
489 # alter one of the tags
490 tag = rel.find("//osm/relation/tag").first
491 tag["v"] = "some changed value"
492 update_changeset(rel, changeset.id)
494 # check that the downloaded tags are the same as the uploaded tags...
495 new_version = with_update_diff(rel, auth_header) do |new_rel|
496 assert_tags_equal rel, new_rel
499 # check the original one in the current_* table again
500 with_relation(relation.id) { |r| assert_tags_equal rel, r }
502 # now check the version in the history
503 with_relation(relation.id, new_version) { |r| assert_tags_equal rel, r }
507 def test_update_wrong_id
509 changeset = create(:changeset, :user => user)
510 relation = create(:relation)
511 other_relation = create(:relation)
513 auth_header = bearer_authorization_header user
514 with_relation(relation.id) do |rel|
515 update_changeset(rel, changeset.id)
516 put api_relation_path(other_relation), :params => rel.to_s, :headers => auth_header
517 assert_response :bad_request
521 # -------------------------------------
522 # Test creating some invalid relations.
523 # -------------------------------------
525 def test_create_invalid
527 changeset = create(:changeset, :user => user)
529 auth_header = bearer_authorization_header user
531 # create a relation with non-existing node as member
532 xml = "<osm><relation changeset='#{changeset.id}'>" \
533 "<member type='node' ref='0'/><tag k='test' v='yes' />" \
535 post api_relations_path, :params => xml, :headers => auth_header
537 assert_response :precondition_failed,
538 "relation upload with invalid node did not return 'precondition failed'"
539 assert_equal "Precondition failed: Relation with id cannot be saved due to Node with id 0", @response.body
542 # -------------------------------------
543 # Test creating a relation, with some invalid XML
544 # -------------------------------------
545 def test_create_invalid_xml
547 changeset = create(:changeset, :user => user)
550 auth_header = bearer_authorization_header user
552 # create some xml that should return an error
553 xml = "<osm><relation changeset='#{changeset.id}'>" \
554 "<member type='type' ref='#{node.id}' role=''/>" \
555 "<tag k='tester' v='yep'/></relation></osm>"
556 post api_relations_path, :params => xml, :headers => auth_header
558 assert_response :bad_request
559 assert_match(/Cannot parse valid relation from xml string/, @response.body)
560 assert_match(/The type is not allowed only, /, @response.body)
563 # -------------------------------------
564 # Test deleting relations.
565 # -------------------------------------
568 private_user = create(:user, :data_public => false)
569 private_user_closed_changeset = create(:changeset, :closed, :user => private_user)
571 closed_changeset = create(:changeset, :closed, :user => user)
572 changeset = create(:changeset, :user => user)
573 relation = create(:relation)
574 used_relation = create(:relation)
575 super_relation = create(:relation_member, :member => used_relation).relation
576 deleted_relation = create(:relation, :deleted)
577 multi_tag_relation = create(:relation)
578 create_list(:relation_tag, 4, :relation => multi_tag_relation)
580 ## First try to delete relation without auth
581 delete api_relation_path(relation)
582 assert_response :unauthorized
584 ## Then try with the private user, to make sure that you get a forbidden
585 auth_header = bearer_authorization_header private_user
587 # this shouldn't work, as we should need the payload...
588 delete api_relation_path(relation), :headers => auth_header
589 assert_response :forbidden
591 # try to delete without specifying a changeset
592 xml = "<osm><relation id='#{relation.id}'/></osm>"
593 delete api_relation_path(relation), :params => xml.to_s, :headers => auth_header
594 assert_response :forbidden
596 # try to delete with an invalid (closed) changeset
597 xml = update_changeset(xml_for_relation(relation),
598 private_user_closed_changeset.id)
599 delete api_relation_path(relation), :params => xml.to_s, :headers => auth_header
600 assert_response :forbidden
602 # try to delete with an invalid (non-existent) changeset
603 xml = update_changeset(xml_for_relation(relation), 0)
604 delete api_relation_path(relation), :params => xml.to_s, :headers => auth_header
605 assert_response :forbidden
607 # this won't work because the relation is in-use by another relation
608 xml = xml_for_relation(used_relation)
609 delete api_relation_path(used_relation), :params => xml.to_s, :headers => auth_header
610 assert_response :forbidden
612 # this should work when we provide the appropriate payload...
613 xml = xml_for_relation(relation)
614 delete api_relation_path(relation), :params => xml.to_s, :headers => auth_header
615 assert_response :forbidden
617 # this won't work since the relation is already deleted
618 xml = xml_for_relation(deleted_relation)
619 delete api_relation_path(deleted_relation), :params => xml.to_s, :headers => auth_header
620 assert_response :forbidden
622 # this won't work since the relation never existed
623 delete api_relation_path(0), :headers => auth_header
624 assert_response :forbidden
626 ## now set auth for the public user
627 auth_header = bearer_authorization_header user
629 # this shouldn't work, as we should need the payload...
630 delete api_relation_path(relation), :headers => auth_header
631 assert_response :bad_request
633 # try to delete without specifying a changeset
634 xml = "<osm><relation id='#{relation.id}' version='#{relation.version}' /></osm>"
635 delete api_relation_path(relation), :params => xml.to_s, :headers => auth_header
636 assert_response :bad_request
637 assert_match(/Changeset id is missing/, @response.body)
639 # try to delete with an invalid (closed) changeset
640 xml = update_changeset(xml_for_relation(relation),
642 delete api_relation_path(relation), :params => xml.to_s, :headers => auth_header
643 assert_response :conflict
645 # try to delete with an invalid (non-existent) changeset
646 xml = update_changeset(xml_for_relation(relation), 0)
647 delete api_relation_path(relation), :params => xml.to_s, :headers => auth_header
648 assert_response :conflict
650 # this won't work because the relation is in a changeset owned by someone else
651 xml = update_changeset(xml_for_relation(relation), create(:changeset).id)
652 delete api_relation_path(relation), :params => xml.to_s, :headers => auth_header
653 assert_response :conflict,
654 "shouldn't be able to delete a relation in a changeset owned by someone else (#{@response.body})"
656 # this won't work because the relation in the payload is different to that passed
657 xml = update_changeset(xml_for_relation(relation), changeset.id)
658 delete api_relation_path(create(:relation)), :params => xml.to_s, :headers => auth_header
659 assert_response :bad_request, "shouldn't be able to delete a relation when payload is different to the url"
661 # this won't work because the relation is in-use by another relation
662 xml = update_changeset(xml_for_relation(used_relation), changeset.id)
663 delete api_relation_path(used_relation), :params => xml.to_s, :headers => auth_header
664 assert_response :precondition_failed,
665 "shouldn't be able to delete a relation used in a relation (#{@response.body})"
666 assert_equal "Precondition failed: The relation #{used_relation.id} is used in relation #{super_relation.id}.", @response.body
668 # this should work when we provide the appropriate payload...
669 xml = update_changeset(xml_for_relation(multi_tag_relation), changeset.id)
670 delete api_relation_path(multi_tag_relation), :params => xml.to_s, :headers => auth_header
671 assert_response :success
673 # valid delete should return the new version number, which should
674 # be greater than the old version number
675 assert_operator @response.body.to_i, :>, multi_tag_relation.version, "delete request should return a new version number for relation"
677 # this won't work since the relation is already deleted
678 xml = update_changeset(xml_for_relation(deleted_relation), changeset.id)
679 delete api_relation_path(deleted_relation), :params => xml.to_s, :headers => auth_header
680 assert_response :gone
682 # Public visible relation needs to be deleted
683 xml = update_changeset(xml_for_relation(super_relation), changeset.id)
684 delete api_relation_path(super_relation), :params => xml.to_s, :headers => auth_header
685 assert_response :success
687 # this works now because the relation which was using this one
689 xml = update_changeset(xml_for_relation(used_relation), changeset.id)
690 delete api_relation_path(used_relation), :params => xml.to_s, :headers => auth_header
691 assert_response :success,
692 "should be able to delete a relation used in an old relation (#{@response.body})"
694 # this won't work since the relation never existed
695 delete api_relation_path(0), :headers => auth_header
696 assert_response :not_found
700 # when a relation's tag is modified then it should put the bounding
701 # box of all its members into the changeset.
702 def test_tag_modify_bounding_box
703 relation = create(:relation)
704 node1 = create(:node, :lat => 0.3, :lon => 0.3)
705 node2 = create(:node, :lat => 0.5, :lon => 0.5)
707 create(:way_node, :way => way, :node => node1)
708 create(:relation_member, :relation => relation, :member => way)
709 create(:relation_member, :relation => relation, :member => node2)
710 # the relation contains nodes1 and node2 (node1
711 # indirectly via the way), so the bbox should be [0.3,0.3,0.5,0.5].
712 check_changeset_modify(BoundingBox.new(0.3, 0.3, 0.5, 0.5)) do |changeset_id, auth_header|
713 # add a tag to an existing relation
714 relation_xml = xml_for_relation(relation)
715 relation_element = relation_xml.find("//osm/relation").first
716 new_tag = XML::Node.new("tag")
717 new_tag["k"] = "some_new_tag"
718 new_tag["v"] = "some_new_value"
719 relation_element << new_tag
721 # update changeset ID to point to new changeset
722 update_changeset(relation_xml, changeset_id)
725 put api_relation_path(relation), :params => relation_xml.to_s, :headers => auth_header
726 assert_response :success, "can't update relation for tag/bbox test"
731 # add a member to a relation and check the bounding box is only that
733 def test_add_member_bounding_box
734 relation = create(:relation)
735 node1 = create(:node, :lat => 4, :lon => 4)
736 node2 = create(:node, :lat => 7, :lon => 7)
738 create(:way_node, :way => way1, :node => create(:node, :lat => 8, :lon => 8))
740 create(:way_node, :way => way2, :node => create(:node, :lat => 9, :lon => 9), :sequence_id => 1)
741 create(:way_node, :way => way2, :node => create(:node, :lat => 10, :lon => 10), :sequence_id => 2)
743 [node1, node2, way1, way2].each do |element|
744 bbox = element.bbox.to_unscaled
745 check_changeset_modify(bbox) do |changeset_id, auth_header|
746 relation_xml = xml_for_relation(Relation.find(relation.id))
747 relation_element = relation_xml.find("//osm/relation").first
748 new_member = XML::Node.new("member")
749 new_member["ref"] = element.id.to_s
750 new_member["type"] = element.class.to_s.downcase
751 new_member["role"] = "some_role"
752 relation_element << new_member
754 # update changeset ID to point to new changeset
755 update_changeset(relation_xml, changeset_id)
758 put api_relation_path(relation), :params => relation_xml.to_s, :headers => auth_header
759 assert_response :success, "can't update relation for add #{element.class}/bbox test: #{@response.body}"
761 # get it back and check the ordering
762 get api_relation_path(relation)
763 assert_response :success, "can't read back the relation: #{@response.body}"
764 check_ordering(relation_xml, @response.body)
770 # remove a member from a relation and check the bounding box is
772 def test_remove_member_bounding_box
773 relation = create(:relation)
774 node1 = create(:node, :lat => 3, :lon => 3)
775 node2 = create(:node, :lat => 5, :lon => 5)
776 create(:relation_member, :relation => relation, :member => node1)
777 create(:relation_member, :relation => relation, :member => node2)
779 check_changeset_modify(BoundingBox.new(5, 5, 5, 5)) do |changeset_id, auth_header|
780 # remove node 5 (5,5) from an existing relation
781 relation_xml = xml_for_relation(relation)
783 .find("//osm/relation/member[@type='node'][@ref='#{node2.id}']")
786 # update changeset ID to point to new changeset
787 update_changeset(relation_xml, changeset_id)
790 put api_relation_path(relation), :params => relation_xml.to_s, :headers => auth_header
791 assert_response :success, "can't update relation for remove node/bbox test"
796 # check that relations are ordered
797 def test_relation_member_ordering
799 changeset = create(:changeset, :user => user)
800 node1 = create(:node)
801 node2 = create(:node)
802 node3 = create(:node)
803 way1 = create(:way_with_nodes, :nodes_count => 2)
804 way2 = create(:way_with_nodes, :nodes_count => 2)
806 auth_header = bearer_authorization_header user
810 <relation changeset='#{changeset.id}'>
811 <member ref='#{node1.id}' type='node' role='first'/>
812 <member ref='#{node2.id}' type='node' role='second'/>
813 <member ref='#{way1.id}' type='way' role='third'/>
814 <member ref='#{way2.id}' type='way' role='fourth'/>
818 doc = XML::Parser.string(doc_str).parse
820 post api_relations_path, :params => doc.to_s, :headers => auth_header
821 assert_response :success, "can't create a relation: #{@response.body}"
822 relation_id = @response.body.to_i
824 # get it back and check the ordering
825 get api_relation_path(relation_id)
826 assert_response :success, "can't read back the relation: #{@response.body}"
827 check_ordering(doc, @response.body)
829 # insert a member at the front
830 new_member = XML::Node.new "member"
831 new_member["ref"] = node3.id.to_s
832 new_member["type"] = "node"
833 new_member["role"] = "new first"
834 doc.find("//osm/relation").first.child.prev = new_member
835 # update the version, should be 1?
836 doc.find("//osm/relation").first["id"] = relation_id.to_s
837 doc.find("//osm/relation").first["version"] = 1.to_s
839 # upload the next version of the relation
840 put api_relation_path(relation_id), :params => doc.to_s, :headers => auth_header
841 assert_response :success, "can't update relation: #{@response.body}"
842 assert_equal 2, @response.body.to_i
844 # get it back again and check the ordering again
845 get api_relation_path(relation_id)
846 assert_response :success, "can't read back the relation: #{@response.body}"
847 check_ordering(doc, @response.body)
849 # check the ordering in the history tables:
850 with_controller(OldRelationsController.new) do
851 get api_old_relation_path(relation_id, 2)
852 assert_response :success, "can't read back version 2 of the relation #{relation_id}"
853 check_ordering(doc, @response.body)
858 # check that relations can contain duplicate members
859 def test_relation_member_duplicates
860 private_user = create(:user, :data_public => false)
862 changeset = create(:changeset, :user => user)
863 node1 = create(:node)
864 node2 = create(:node)
868 <relation changeset='#{changeset.id}'>
869 <member ref='#{node1.id}' type='node' role='forward'/>
870 <member ref='#{node2.id}' type='node' role='forward'/>
871 <member ref='#{node1.id}' type='node' role='forward'/>
872 <member ref='#{node2.id}' type='node' role='forward'/>
876 doc = XML::Parser.string(doc_str).parse
878 ## First try with the private user
879 auth_header = bearer_authorization_header private_user
881 post api_relations_path, :params => doc.to_s, :headers => auth_header
882 assert_response :forbidden
884 ## Now try with the public user
885 auth_header = bearer_authorization_header user
887 post api_relations_path, :params => doc.to_s, :headers => auth_header
888 assert_response :success, "can't create a relation: #{@response.body}"
889 relation_id = @response.body.to_i
891 # get it back and check the ordering
892 get api_relation_path(relation_id)
893 assert_response :success, "can't read back the relation: #{relation_id}"
894 check_ordering(doc, @response.body)
898 # test that the ordering of elements in the history is the same as in current.
899 def test_history_ordering
901 changeset = create(:changeset, :user => user)
902 node1 = create(:node)
903 node2 = create(:node)
904 node3 = create(:node)
905 node4 = create(:node)
909 <relation changeset='#{changeset.id}'>
910 <member ref='#{node1.id}' type='node' role='forward'/>
911 <member ref='#{node4.id}' type='node' role='forward'/>
912 <member ref='#{node3.id}' type='node' role='forward'/>
913 <member ref='#{node2.id}' type='node' role='forward'/>
917 doc = XML::Parser.string(doc_str).parse
918 auth_header = bearer_authorization_header user
920 post api_relations_path, :params => doc.to_s, :headers => auth_header
921 assert_response :success, "can't create a relation: #{@response.body}"
922 relation_id = @response.body.to_i
924 # check the ordering in the current tables:
925 get api_relation_path(relation_id)
926 assert_response :success, "can't read back the relation: #{@response.body}"
927 check_ordering(doc, @response.body)
929 # check the ordering in the history tables:
930 with_controller(OldRelationsController.new) do
931 get api_old_relation_path(relation_id, 1)
932 assert_response :success, "can't read back version 1 of the relation: #{@response.body}"
933 check_ordering(doc, @response.body)
938 # remove all the members from a relation. the result is pretty useless, but
939 # still technically valid.
940 def test_remove_all_members
941 relation = create(:relation)
942 node1 = create(:node, :lat => 0.3, :lon => 0.3)
943 node2 = create(:node, :lat => 0.5, :lon => 0.5)
945 create(:way_node, :way => way, :node => node1)
946 create(:relation_member, :relation => relation, :member => way)
947 create(:relation_member, :relation => relation, :member => node2)
949 check_changeset_modify(BoundingBox.new(0.3, 0.3, 0.5, 0.5)) do |changeset_id, auth_header|
950 relation_xml = xml_for_relation(relation)
952 .find("//osm/relation/member")
955 # update changeset ID to point to new changeset
956 update_changeset(relation_xml, changeset_id)
959 put api_relation_path(relation), :params => relation_xml.to_s, :headers => auth_header
960 assert_response :success, "can't update relation for remove all members test"
961 checkrelation = Relation.find(relation.id)
962 assert_not_nil(checkrelation,
963 "uploaded relation not found in database after upload")
964 assert_equal(0, checkrelation.members.length,
965 "relation contains members but they should have all been deleted")
970 # test initial rate limit
971 def test_initial_rate_limit
976 node1 = create(:node)
977 node2 = create(:node)
979 # create a changeset that puts us near the initial rate limit
980 changeset = create(:changeset, :user => user,
981 :created_at => Time.now.utc - 5.minutes,
982 :num_changes => Settings.initial_changes_per_hour - 1)
984 # create authentication header
985 auth_header = bearer_authorization_header user
987 # try creating a relation
988 xml = "<osm><relation changeset='#{changeset.id}'>" \
989 "<member ref='#{node1.id}' type='node' role='some'/>" \
990 "<member ref='#{node2.id}' type='node' role='some'/>" \
991 "<tag k='test' v='yes' /></relation></osm>"
992 post api_relations_path, :params => xml, :headers => auth_header
993 assert_response :success, "relation create did not return success status"
995 # get the id of the relation we created
996 relationid = @response.body
998 # try updating the relation, which should be rate limited
999 xml = "<osm><relation id='#{relationid}' version='1' changeset='#{changeset.id}'>" \
1000 "<member ref='#{node2.id}' type='node' role='some'/>" \
1001 "<member ref='#{node1.id}' type='node' role='some'/>" \
1002 "<tag k='test' v='yes' /></relation></osm>"
1003 put api_relation_path(relationid), :params => xml, :headers => auth_header
1004 assert_response :too_many_requests, "relation update did not hit rate limit"
1006 # try deleting the relation, which should be rate limited
1007 xml = "<osm><relation id='#{relationid}' version='2' changeset='#{changeset.id}'/></osm>"
1008 delete api_relation_path(relationid), :params => xml, :headers => auth_header
1009 assert_response :too_many_requests, "relation delete did not hit rate limit"
1011 # try creating a relation, which should be rate limited
1012 xml = "<osm><relation changeset='#{changeset.id}'>" \
1013 "<member ref='#{node1.id}' type='node' role='some'/>" \
1014 "<member ref='#{node2.id}' type='node' role='some'/>" \
1015 "<tag k='test' v='yes' /></relation></osm>"
1016 post api_relations_path, :params => xml, :headers => auth_header
1017 assert_response :too_many_requests, "relation create did not hit rate limit"
1021 # test maximum rate limit
1022 def test_maximum_rate_limit
1024 user = create(:user)
1027 node1 = create(:node)
1028 node2 = create(:node)
1030 # create a changeset to establish our initial edit time
1031 changeset = create(:changeset, :user => user,
1032 :created_at => Time.now.utc - 28.days)
1034 # create changeset to put us near the maximum rate limit
1035 total_changes = Settings.max_changes_per_hour - 1
1036 while total_changes.positive?
1037 changes = [total_changes, Changeset::MAX_ELEMENTS].min
1038 changeset = create(:changeset, :user => user,
1039 :created_at => Time.now.utc - 5.minutes,
1040 :num_changes => changes)
1041 total_changes -= changes
1044 # create authentication header
1045 auth_header = bearer_authorization_header user
1047 # try creating a relation
1048 xml = "<osm><relation changeset='#{changeset.id}'>" \
1049 "<member ref='#{node1.id}' type='node' role='some'/>" \
1050 "<member ref='#{node2.id}' type='node' role='some'/>" \
1051 "<tag k='test' v='yes' /></relation></osm>"
1052 post api_relations_path, :params => xml, :headers => auth_header
1053 assert_response :success, "relation create did not return success status"
1055 # get the id of the relation we created
1056 relationid = @response.body
1058 # try updating the relation, which should be rate limited
1059 xml = "<osm><relation id='#{relationid}' version='1' changeset='#{changeset.id}'>" \
1060 "<member ref='#{node2.id}' type='node' role='some'/>" \
1061 "<member ref='#{node1.id}' type='node' role='some'/>" \
1062 "<tag k='test' v='yes' /></relation></osm>"
1063 put api_relation_path(relationid), :params => xml, :headers => auth_header
1064 assert_response :too_many_requests, "relation update did not hit rate limit"
1066 # try deleting the relation, which should be rate limited
1067 xml = "<osm><relation id='#{relationid}' version='2' changeset='#{changeset.id}'/></osm>"
1068 delete api_relation_path(relationid), :params => xml, :headers => auth_header
1069 assert_response :too_many_requests, "relation delete did not hit rate limit"
1071 # try creating a relation, which should be rate limited
1072 xml = "<osm><relation changeset='#{changeset.id}'>" \
1073 "<member ref='#{node1.id}' type='node' role='some'/>" \
1074 "<member ref='#{node2.id}' type='node' role='some'/>" \
1075 "<tag k='test' v='yes' /></relation></osm>"
1076 post api_relations_path, :params => xml, :headers => auth_header
1077 assert_response :too_many_requests, "relation create did not hit rate limit"
1082 def check_relations_for_element(path, type, id, expected_relations)
1083 # check the "relations for relation" mode
1085 assert_response :success
1087 # count one osm element
1088 assert_select "osm[version='#{Settings.api_version}'][generator='#{Settings.generator}']", 1
1090 # we should have only the expected number of relations
1091 assert_select "osm>relation", expected_relations.size
1093 # and each of them should contain the element we originally searched for
1094 expected_relations.each do |relation|
1095 # The relation should appear once, but the element could appear multiple times
1096 assert_select "osm>relation[id='#{relation.id}']", 1
1097 assert_select "osm>relation[id='#{relation.id}']>member[type='#{type}'][ref='#{id}']"
1102 # checks that the XML document and the string arguments have
1103 # members in the same order.
1104 def check_ordering(doc, xml)
1105 new_doc = XML::Parser.string(xml).parse
1107 doc_members = doc.find("//osm/relation/member").collect do |m|
1108 [m["ref"].to_i, m["type"].to_sym, m["role"]]
1111 new_members = new_doc.find("//osm/relation/member").collect do |m|
1112 [m["ref"].to_i, m["type"].to_sym, m["role"]]
1115 doc_members.zip(new_members).each do |d, n|
1116 assert_equal d, n, "members are not equal - ordering is wrong? (#{doc}, #{xml})"
1121 # create a changeset and yield to the caller to set it up, then assert
1122 # that the changeset bounding box is +bbox+.
1123 def check_changeset_modify(bbox)
1124 ## First test with the private user to check that you get a forbidden
1125 auth_header = bearer_authorization_header create(:user, :data_public => false)
1127 # create a new changeset for this operation, so we are assured
1128 # that the bounding box will be newly-generated.
1129 with_controller(Api::ChangesetsController.new) do
1130 xml = "<osm><changeset/></osm>"
1131 put changeset_create_path, :params => xml, :headers => auth_header
1132 assert_response :forbidden, "shouldn't be able to create changeset for modify test, as should get forbidden"
1135 ## Now do the whole thing with the public user
1136 auth_header = bearer_authorization_header
1138 # create a new changeset for this operation, so we are assured
1139 # that the bounding box will be newly-generated.
1140 changeset_id = with_controller(Api::ChangesetsController.new) do
1141 xml = "<osm><changeset/></osm>"
1142 put changeset_create_path, :params => xml, :headers => auth_header
1143 assert_response :success, "couldn't create changeset for modify test"
1147 # go back to the block to do the actual modifies
1148 yield changeset_id, auth_header
1150 # now download the changeset to check its bounding box
1151 with_controller(Api::ChangesetsController.new) do
1152 get changeset_show_path(changeset_id)
1153 assert_response :success, "can't re-read changeset for modify test"
1154 assert_select "osm>changeset", 1, "Changeset element doesn't exist in #{@response.body}"
1155 assert_select "osm>changeset[id='#{changeset_id}']", 1, "Changeset id=#{changeset_id} doesn't exist in #{@response.body}"
1156 assert_select "osm>changeset[min_lon='#{format('%<lon>.7f', :lon => bbox.min_lon)}']", 1, "Changeset min_lon wrong in #{@response.body}"
1157 assert_select "osm>changeset[min_lat='#{format('%<lat>.7f', :lat => bbox.min_lat)}']", 1, "Changeset min_lat wrong in #{@response.body}"
1158 assert_select "osm>changeset[max_lon='#{format('%<lon>.7f', :lon => bbox.max_lon)}']", 1, "Changeset max_lon wrong in #{@response.body}"
1159 assert_select "osm>changeset[max_lat='#{format('%<lat>.7f', :lat => bbox.max_lat)}']", 1, "Changeset max_lat wrong in #{@response.body}"
1164 # yields the relation with the given +id+ (and optional +version+
1165 # to read from the history tables) into the block. the parsed XML
1167 def with_relation(id, ver = nil)
1169 get api_relation_path(id)
1171 with_controller(OldRelationsController.new) do
1172 get api_old_relation_path(id, ver)
1175 assert_response :success
1176 yield xml_parse(@response.body)
1180 # updates the relation (XML) +rel+ and
1181 # yields the new version of that relation into the block.
1182 # the parsed XML doc is returned.
1183 def with_update(rel, headers)
1184 rel_id = rel.find("//osm/relation").first["id"].to_i
1185 put api_relation_path(rel_id), :params => rel.to_s, :headers => headers
1186 assert_response :success, "can't update relation: #{@response.body}"
1187 version = @response.body.to_i
1189 # now get the new version
1190 get api_relation_path(rel_id)
1191 assert_response :success
1192 new_rel = xml_parse(@response.body)
1200 # updates the relation (XML) +rel+ via the diff-upload API and
1201 # yields the new version of that relation into the block.
1202 # the parsed XML doc is returned.
1203 def with_update_diff(rel, headers)
1204 rel_id = rel.find("//osm/relation").first["id"].to_i
1205 cs_id = rel.find("//osm/relation").first["changeset"].to_i
1208 with_controller(Api::ChangesetsController.new) do
1209 doc = OSM::API.new.xml_doc
1210 change = XML::Node.new "osmChange"
1212 modify = XML::Node.new "modify"
1214 modify << doc.import(rel.find("//osm/relation").first)
1216 post changeset_upload_path(cs_id), :params => doc.to_s, :headers => headers
1217 assert_response :success, "can't upload diff relation: #{@response.body}"
1218 version = xml_parse(@response.body).find("//diffResult/relation").first["new_version"].to_i
1221 # now get the new version
1222 get api_relation_path(rel_id)
1223 assert_response :success
1224 new_rel = xml_parse(@response.body)
1232 # returns a k->v hash of tags from an xml doc
1233 def get_tags_as_hash(a)
1234 a.find("//osm/relation/tag").sort_by { |v| v["k"] }.each_with_object({}) do |v, h|
1240 # assert that all tags on relation documents +a+ and +b+
1242 def assert_tags_equal(a, b)
1243 # turn the XML doc into tags hashes
1244 a_tags = get_tags_as_hash(a)
1245 b_tags = get_tags_as_hash(b)
1247 assert_equal a_tags.keys, b_tags.keys, "Tag keys should be identical."
1248 a_tags.each do |k, v|
1249 assert_equal v, b_tags[k],
1250 "Tags which were not altered should be the same. " \
1251 "#{a_tags.inspect} != #{b_tags.inspect}"
1256 # update the changeset_id of a node element
1257 def update_changeset(xml, changeset_id)
1258 xml_attr_rewrite(xml, "changeset", changeset_id)
1262 # update an attribute in the node element
1263 def xml_attr_rewrite(xml, name, value)
1264 xml.find("//osm/relation").first[name] = value.to_s
1271 parser = XML::Parser.string(xml)