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)
138 create(:relation_member, :relation => relation, :member => node)
140 get api_relation_path(relation)
142 assert_response :success
143 assert_dom "node", :count => 0
144 assert_dom "relation", :count => 1 do
145 assert_dom "> @id", :text => relation.id.to_s
149 def test_full_not_found
150 get relation_full_path(999999)
151 assert_response :not_found
154 def test_full_deleted
155 get relation_full_path(create(:relation, :deleted))
156 assert_response :gone
160 relation = create(:relation)
162 get relation_full_path(relation)
164 assert_response :success
165 assert_dom "relation", :count => 1 do
166 assert_dom "> @id", :text => relation.id.to_s
170 def test_full_with_node_member
171 relation = create(:relation)
173 create(:relation_member, :relation => relation, :member => node)
175 get relation_full_path(relation)
177 assert_response :success
178 assert_dom "node", :count => 1 do
179 assert_dom "> @id", :text => node.id.to_s
181 assert_dom "relation", :count => 1 do
182 assert_dom "> @id", :text => relation.id.to_s
186 def test_full_with_way_member
187 relation = create(:relation)
188 way = create(:way_with_nodes)
189 create(:relation_member, :relation => relation, :member => way)
191 get relation_full_path(relation)
193 assert_response :success
194 assert_dom "node", :count => 1 do
195 assert_dom "> @id", :text => way.nodes[0].id.to_s
197 assert_dom "way", :count => 1 do
198 assert_dom "> @id", :text => way.id.to_s
200 assert_dom "relation", :count => 1 do
201 assert_dom "> @id", :text => relation.id.to_s
206 # check that all relations containing a particular node, and no extra
207 # relations, are returned from the relations_for_node call.
208 def test_relations_for_node
210 # should include relations with that node as a member
211 relation_with_node = create(:relation_member, :member => node).relation
212 # should ignore relations without that node as a member
213 _relation_without_node = create(:relation_member).relation
214 # should ignore relations with the node involved indirectly, via a way
215 way = create(:way_node, :node => node).way
216 _relation_with_way = create(:relation_member, :member => way).relation
217 # should ignore relations with the node involved indirectly, via a relation
218 second_relation = create(:relation_member, :member => node).relation
219 _super_relation = create(:relation_member, :member => second_relation).relation
220 # should combine multiple relation_member references into just one relation entry
221 create(:relation_member, :member => node, :relation => relation_with_node)
222 # should not include deleted relations
223 deleted_relation = create(:relation, :deleted)
224 create(:relation_member, :member => node, :relation => deleted_relation)
226 check_relations_for_element(node_relations_path(node), "node",
228 [relation_with_node, second_relation])
231 def test_relations_for_way
233 # should include relations with that way as a member
234 relation_with_way = create(:relation_member, :member => way).relation
235 # should ignore relations without that way as a member
236 _relation_without_way = create(:relation_member).relation
237 # should ignore relations with the way involved indirectly, via a relation
238 second_relation = create(:relation_member, :member => way).relation
239 _super_relation = create(:relation_member, :member => second_relation).relation
240 # should combine multiple relation_member references into just one relation entry
241 create(:relation_member, :member => way, :relation => relation_with_way)
242 # should not include deleted relations
243 deleted_relation = create(:relation, :deleted)
244 create(:relation_member, :member => way, :relation => deleted_relation)
246 check_relations_for_element(way_relations_path(way), "way",
248 [relation_with_way, second_relation])
251 def test_relations_for_relation
252 relation = create(:relation)
253 # should include relations with that relation as a member
254 relation_with_relation = create(:relation_member, :member => relation).relation
255 # should ignore any relation without that relation as a member
256 _relation_without_relation = create(:relation_member).relation
257 # should ignore relations with the relation involved indirectly, via a relation
258 second_relation = create(:relation_member, :member => relation).relation
259 _super_relation = create(:relation_member, :member => second_relation).relation
260 # should combine multiple relation_member references into just one relation entry
261 create(:relation_member, :member => relation, :relation => relation_with_relation)
262 # should not include deleted relations
263 deleted_relation = create(:relation, :deleted)
264 create(:relation_member, :member => relation, :relation => deleted_relation)
265 check_relations_for_element(relation_relations_path(relation), "relation",
267 [relation_with_relation, second_relation])
270 # -------------------------------------
271 # Test simple relation creation.
272 # -------------------------------------
275 private_user = create(:user, :data_public => false)
276 private_changeset = create(:changeset, :user => private_user)
278 changeset = create(:changeset, :user => user)
280 way = create(:way_with_nodes, :nodes_count => 2)
282 auth_header = bearer_authorization_header private_user
284 # create an relation without members
285 xml = "<osm><relation changeset='#{private_changeset.id}'><tag k='test' v='yes' /></relation></osm>"
286 post api_relations_path, :params => xml, :headers => auth_header
287 # hope for forbidden, due to user
288 assert_response :forbidden,
289 "relation upload should have failed with forbidden"
292 # create an relation with a node as member
293 # This time try with a role attribute in the relation
294 xml = "<osm><relation changeset='#{private_changeset.id}'>" \
295 "<member ref='#{node.id}' type='node' role='some'/>" \
296 "<tag k='test' v='yes' /></relation></osm>"
297 post api_relations_path, :params => xml, :headers => auth_header
298 # hope for forbidden due to user
299 assert_response :forbidden,
300 "relation upload did not return forbidden status"
303 # create an relation with a node as member, this time test that we don't
304 # need a role attribute to be included
305 xml = "<osm><relation changeset='#{private_changeset.id}'>" \
306 "<member ref='#{node.id}' type='node'/><tag k='test' v='yes' /></relation></osm>"
307 post api_relations_path, :params => xml, :headers => auth_header
308 # hope for forbidden due to user
309 assert_response :forbidden,
310 "relation upload did not return forbidden status"
313 # create an relation with a way and a node as members
314 xml = "<osm><relation changeset='#{private_changeset.id}'>" \
315 "<member type='node' ref='#{node.id}' role='some'/>" \
316 "<member type='way' ref='#{way.id}' role='other'/>" \
317 "<tag k='test' v='yes' /></relation></osm>"
318 post api_relations_path, :params => xml, :headers => auth_header
319 # hope for forbidden, due to user
320 assert_response :forbidden,
321 "relation upload did not return success status"
323 ## Now try with the public user
324 auth_header = bearer_authorization_header user
326 # create an relation without members
327 xml = "<osm><relation changeset='#{changeset.id}'><tag k='test' v='yes' /></relation></osm>"
328 post api_relations_path, :params => xml, :headers => auth_header
330 assert_response :success,
331 "relation upload did not return success status"
332 # read id of created relation and search for it
333 relationid = @response.body
334 checkrelation = Relation.find(relationid)
335 assert_not_nil checkrelation,
336 "uploaded relation not found in data base after upload"
338 assert_equal(0, checkrelation.members.length, "saved relation contains members but should not")
339 assert_equal(1, checkrelation.tags.length, "saved relation does not contain exactly one tag")
340 assert_equal changeset.id, checkrelation.changeset.id,
341 "saved relation does not belong in the changeset it was assigned to"
342 assert_equal user.id, checkrelation.changeset.user_id,
343 "saved relation does not belong to user that created it"
344 assert checkrelation.visible,
345 "saved relation is not visible"
346 # ok the relation is there but can we also retrieve it?
347 get api_relation_path(relationid)
348 assert_response :success
351 # create an relation with a node as member
352 # This time try with a role attribute in the relation
353 xml = "<osm><relation changeset='#{changeset.id}'>" \
354 "<member ref='#{node.id}' type='node' role='some'/>" \
355 "<tag k='test' v='yes' /></relation></osm>"
356 post api_relations_path, :params => xml, :headers => auth_header
358 assert_response :success,
359 "relation upload did not return success status"
360 # read id of created relation and search for it
361 relationid = @response.body
362 checkrelation = Relation.find(relationid)
363 assert_not_nil checkrelation,
364 "uploaded relation not found in data base after upload"
366 assert_equal(1, checkrelation.members.length, "saved relation does not contain exactly one member")
367 assert_equal(1, checkrelation.tags.length, "saved relation does not contain exactly one tag")
368 assert_equal changeset.id, checkrelation.changeset.id,
369 "saved relation does not belong in the changeset it was assigned to"
370 assert_equal user.id, checkrelation.changeset.user_id,
371 "saved relation does not belong to user that created it"
372 assert checkrelation.visible,
373 "saved relation is not visible"
374 # ok the relation is there but can we also retrieve it?
376 get api_relation_path(relationid)
377 assert_response :success
380 # create an relation with a node as member, this time test that we don't
381 # need a role attribute to be included
382 xml = "<osm><relation changeset='#{changeset.id}'>" \
383 "<member ref='#{node.id}' type='node'/><tag k='test' v='yes' /></relation></osm>"
384 post api_relations_path, :params => xml, :headers => auth_header
386 assert_response :success,
387 "relation upload did not return success status"
388 # read id of created relation and search for it
389 relationid = @response.body
390 checkrelation = Relation.find(relationid)
391 assert_not_nil checkrelation,
392 "uploaded relation not found in data base after upload"
394 assert_equal(1, checkrelation.members.length, "saved relation does not contain exactly one member")
395 assert_equal(1, checkrelation.tags.length, "saved relation does not contain exactly one tag")
396 assert_equal changeset.id, checkrelation.changeset.id,
397 "saved relation does not belong in the changeset it was assigned to"
398 assert_equal user.id, checkrelation.changeset.user_id,
399 "saved relation does not belong to user that created it"
400 assert checkrelation.visible,
401 "saved relation is not visible"
402 # ok the relation is there but can we also retrieve it?
404 get api_relation_path(relationid)
405 assert_response :success
408 # create an relation with a way and a node as members
409 xml = "<osm><relation changeset='#{changeset.id}'>" \
410 "<member type='node' ref='#{node.id}' role='some'/>" \
411 "<member type='way' ref='#{way.id}' role='other'/>" \
412 "<tag k='test' v='yes' /></relation></osm>"
413 post api_relations_path, :params => xml, :headers => auth_header
415 assert_response :success,
416 "relation upload did not return success status"
417 # read id of created relation and search for it
418 relationid = @response.body
419 checkrelation = Relation.find(relationid)
420 assert_not_nil checkrelation,
421 "uploaded relation not found in data base after upload"
423 assert_equal(2, checkrelation.members.length, "saved relation does not have exactly two members")
424 assert_equal(1, checkrelation.tags.length, "saved relation does not contain exactly one tag")
425 assert_equal changeset.id, checkrelation.changeset.id,
426 "saved relation does not belong in the changeset it was assigned to"
427 assert_equal user.id, checkrelation.changeset.user_id,
428 "saved relation does not belong to user that created it"
429 assert checkrelation.visible,
430 "saved relation is not visible"
431 # ok the relation is there but can we also retrieve it?
432 get api_relation_path(relationid)
433 assert_response :success
436 # ------------------------------------
437 # Test updating relations
438 # ------------------------------------
441 # test that, when tags are updated on a relation, the correct things
442 # happen to the correct tables and the API gives sensible results.
443 # this is to test a case that gregory marler noticed and posted to
445 ## FIXME Move this to an integration test
446 def test_update_relation_tags
448 changeset = create(:changeset, :user => user)
449 relation = create(:relation)
450 create_list(:relation_tag, 4, :relation => relation)
452 auth_header = bearer_authorization_header user
454 with_relation(relation.id) do |rel|
455 # alter one of the tags
456 tag = rel.find("//osm/relation/tag").first
457 tag["v"] = "some changed value"
458 update_changeset(rel, changeset.id)
460 # check that the downloaded tags are the same as the uploaded tags...
461 new_version = with_update(rel, auth_header) do |new_rel|
462 assert_tags_equal rel, new_rel
465 # check the original one in the current_* table again
466 with_relation(relation.id) { |r| assert_tags_equal rel, r }
468 # now check the version in the history
469 with_relation(relation.id, new_version) { |r| assert_tags_equal rel, r }
474 # test that, when tags are updated on a relation when using the diff
475 # upload function, the correct things happen to the correct tables
476 # and the API gives sensible results. this is to test a case that
477 # gregory marler noticed and posted to josm-dev.
478 def test_update_relation_tags_via_upload
480 changeset = create(:changeset, :user => user)
481 relation = create(:relation)
482 create_list(:relation_tag, 4, :relation => relation)
484 auth_header = bearer_authorization_header user
486 with_relation(relation.id) do |rel|
487 # alter one of the tags
488 tag = rel.find("//osm/relation/tag").first
489 tag["v"] = "some changed value"
490 update_changeset(rel, changeset.id)
492 # check that the downloaded tags are the same as the uploaded tags...
493 new_version = with_update_diff(rel, auth_header) do |new_rel|
494 assert_tags_equal rel, new_rel
497 # check the original one in the current_* table again
498 with_relation(relation.id) { |r| assert_tags_equal rel, r }
500 # now check the version in the history
501 with_relation(relation.id, new_version) { |r| assert_tags_equal rel, r }
505 def test_update_wrong_id
507 changeset = create(:changeset, :user => user)
508 relation = create(:relation)
509 other_relation = create(:relation)
511 auth_header = bearer_authorization_header user
512 with_relation(relation.id) do |rel|
513 update_changeset(rel, changeset.id)
514 put api_relation_path(other_relation), :params => rel.to_s, :headers => auth_header
515 assert_response :bad_request
519 # -------------------------------------
520 # Test creating some invalid relations.
521 # -------------------------------------
523 def test_create_invalid
525 changeset = create(:changeset, :user => user)
527 auth_header = bearer_authorization_header user
529 # create a relation with non-existing node as member
530 xml = "<osm><relation changeset='#{changeset.id}'>" \
531 "<member type='node' ref='0'/><tag k='test' v='yes' />" \
533 post api_relations_path, :params => xml, :headers => auth_header
535 assert_response :precondition_failed,
536 "relation upload with invalid node did not return 'precondition failed'"
537 assert_equal "Precondition failed: Relation with id cannot be saved due to Node with id 0", @response.body
540 # -------------------------------------
541 # Test creating a relation, with some invalid XML
542 # -------------------------------------
543 def test_create_invalid_xml
545 changeset = create(:changeset, :user => user)
548 auth_header = bearer_authorization_header user
550 # create some xml that should return an error
551 xml = "<osm><relation changeset='#{changeset.id}'>" \
552 "<member type='type' ref='#{node.id}' role=''/>" \
553 "<tag k='tester' v='yep'/></relation></osm>"
554 post api_relations_path, :params => xml, :headers => auth_header
556 assert_response :bad_request
557 assert_match(/Cannot parse valid relation from xml string/, @response.body)
558 assert_match(/The type is not allowed only, /, @response.body)
561 # -------------------------------------
562 # Test deleting relations.
563 # -------------------------------------
566 private_user = create(:user, :data_public => false)
567 private_user_closed_changeset = create(:changeset, :closed, :user => private_user)
569 closed_changeset = create(:changeset, :closed, :user => user)
570 changeset = create(:changeset, :user => user)
571 relation = create(:relation)
572 used_relation = create(:relation)
573 super_relation = create(:relation_member, :member => used_relation).relation
574 deleted_relation = create(:relation, :deleted)
575 multi_tag_relation = create(:relation)
576 create_list(:relation_tag, 4, :relation => multi_tag_relation)
578 ## First try to delete relation without auth
579 delete api_relation_path(relation)
580 assert_response :unauthorized
582 ## Then try with the private user, to make sure that you get a forbidden
583 auth_header = bearer_authorization_header private_user
585 # this shouldn't work, as we should need the payload...
586 delete api_relation_path(relation), :headers => auth_header
587 assert_response :forbidden
589 # try to delete without specifying a changeset
590 xml = "<osm><relation id='#{relation.id}'/></osm>"
591 delete api_relation_path(relation), :params => xml.to_s, :headers => auth_header
592 assert_response :forbidden
594 # try to delete with an invalid (closed) changeset
595 xml = update_changeset(xml_for_relation(relation),
596 private_user_closed_changeset.id)
597 delete api_relation_path(relation), :params => xml.to_s, :headers => auth_header
598 assert_response :forbidden
600 # try to delete with an invalid (non-existent) changeset
601 xml = update_changeset(xml_for_relation(relation), 0)
602 delete api_relation_path(relation), :params => xml.to_s, :headers => auth_header
603 assert_response :forbidden
605 # this won't work because the relation is in-use by another relation
606 xml = xml_for_relation(used_relation)
607 delete api_relation_path(used_relation), :params => xml.to_s, :headers => auth_header
608 assert_response :forbidden
610 # this should work when we provide the appropriate payload...
611 xml = xml_for_relation(relation)
612 delete api_relation_path(relation), :params => xml.to_s, :headers => auth_header
613 assert_response :forbidden
615 # this won't work since the relation is already deleted
616 xml = xml_for_relation(deleted_relation)
617 delete api_relation_path(deleted_relation), :params => xml.to_s, :headers => auth_header
618 assert_response :forbidden
620 # this won't work since the relation never existed
621 delete api_relation_path(0), :headers => auth_header
622 assert_response :forbidden
624 ## now set auth for the public user
625 auth_header = bearer_authorization_header user
627 # this shouldn't work, as we should need the payload...
628 delete api_relation_path(relation), :headers => auth_header
629 assert_response :bad_request
631 # try to delete without specifying a changeset
632 xml = "<osm><relation id='#{relation.id}' version='#{relation.version}' /></osm>"
633 delete api_relation_path(relation), :params => xml.to_s, :headers => auth_header
634 assert_response :bad_request
635 assert_match(/Changeset id is missing/, @response.body)
637 # try to delete with an invalid (closed) changeset
638 xml = update_changeset(xml_for_relation(relation),
640 delete api_relation_path(relation), :params => xml.to_s, :headers => auth_header
641 assert_response :conflict
643 # try to delete with an invalid (non-existent) changeset
644 xml = update_changeset(xml_for_relation(relation), 0)
645 delete api_relation_path(relation), :params => xml.to_s, :headers => auth_header
646 assert_response :conflict
648 # this won't work because the relation is in a changeset owned by someone else
649 xml = update_changeset(xml_for_relation(relation), create(:changeset).id)
650 delete api_relation_path(relation), :params => xml.to_s, :headers => auth_header
651 assert_response :conflict,
652 "shouldn't be able to delete a relation in a changeset owned by someone else (#{@response.body})"
654 # this won't work because the relation in the payload is different to that passed
655 xml = update_changeset(xml_for_relation(relation), changeset.id)
656 delete api_relation_path(create(:relation)), :params => xml.to_s, :headers => auth_header
657 assert_response :bad_request, "shouldn't be able to delete a relation when payload is different to the url"
659 # this won't work because the relation is in-use by another relation
660 xml = update_changeset(xml_for_relation(used_relation), changeset.id)
661 delete api_relation_path(used_relation), :params => xml.to_s, :headers => auth_header
662 assert_response :precondition_failed,
663 "shouldn't be able to delete a relation used in a relation (#{@response.body})"
664 assert_equal "Precondition failed: The relation #{used_relation.id} is used in relation #{super_relation.id}.", @response.body
666 # this should work when we provide the appropriate payload...
667 xml = update_changeset(xml_for_relation(multi_tag_relation), changeset.id)
668 delete api_relation_path(multi_tag_relation), :params => xml.to_s, :headers => auth_header
669 assert_response :success
671 # valid delete should return the new version number, which should
672 # be greater than the old version number
673 assert_operator @response.body.to_i, :>, multi_tag_relation.version, "delete request should return a new version number for relation"
675 # this won't work since the relation is already deleted
676 xml = update_changeset(xml_for_relation(deleted_relation), changeset.id)
677 delete api_relation_path(deleted_relation), :params => xml.to_s, :headers => auth_header
678 assert_response :gone
680 # Public visible relation needs to be deleted
681 xml = update_changeset(xml_for_relation(super_relation), changeset.id)
682 delete api_relation_path(super_relation), :params => xml.to_s, :headers => auth_header
683 assert_response :success
685 # this works now because the relation which was using this one
687 xml = update_changeset(xml_for_relation(used_relation), changeset.id)
688 delete api_relation_path(used_relation), :params => xml.to_s, :headers => auth_header
689 assert_response :success,
690 "should be able to delete a relation used in an old relation (#{@response.body})"
692 # this won't work since the relation never existed
693 delete api_relation_path(0), :headers => auth_header
694 assert_response :not_found
698 # when a relation's tag is modified then it should put the bounding
699 # box of all its members into the changeset.
700 def test_tag_modify_bounding_box
701 relation = create(:relation)
702 node1 = create(:node, :lat => 0.3, :lon => 0.3)
703 node2 = create(:node, :lat => 0.5, :lon => 0.5)
705 create(:way_node, :way => way, :node => node1)
706 create(:relation_member, :relation => relation, :member => way)
707 create(:relation_member, :relation => relation, :member => node2)
708 # the relation contains nodes1 and node2 (node1
709 # indirectly via the way), so the bbox should be [0.3,0.3,0.5,0.5].
710 check_changeset_modify(BoundingBox.new(0.3, 0.3, 0.5, 0.5)) do |changeset_id, auth_header|
711 # add a tag to an existing relation
712 relation_xml = xml_for_relation(relation)
713 relation_element = relation_xml.find("//osm/relation").first
714 new_tag = XML::Node.new("tag")
715 new_tag["k"] = "some_new_tag"
716 new_tag["v"] = "some_new_value"
717 relation_element << new_tag
719 # update changeset ID to point to new changeset
720 update_changeset(relation_xml, changeset_id)
723 put api_relation_path(relation), :params => relation_xml.to_s, :headers => auth_header
724 assert_response :success, "can't update relation for tag/bbox test"
729 # add a member to a relation and check the bounding box is only that
731 def test_add_member_bounding_box
732 relation = create(:relation)
733 node1 = create(:node, :lat => 4, :lon => 4)
734 node2 = create(:node, :lat => 7, :lon => 7)
736 create(:way_node, :way => way1, :node => create(:node, :lat => 8, :lon => 8))
738 create(:way_node, :way => way2, :node => create(:node, :lat => 9, :lon => 9), :sequence_id => 1)
739 create(:way_node, :way => way2, :node => create(:node, :lat => 10, :lon => 10), :sequence_id => 2)
741 [node1, node2, way1, way2].each do |element|
742 bbox = element.bbox.to_unscaled
743 check_changeset_modify(bbox) do |changeset_id, auth_header|
744 relation_xml = xml_for_relation(Relation.find(relation.id))
745 relation_element = relation_xml.find("//osm/relation").first
746 new_member = XML::Node.new("member")
747 new_member["ref"] = element.id.to_s
748 new_member["type"] = element.class.to_s.downcase
749 new_member["role"] = "some_role"
750 relation_element << new_member
752 # update changeset ID to point to new changeset
753 update_changeset(relation_xml, changeset_id)
756 put api_relation_path(relation), :params => relation_xml.to_s, :headers => auth_header
757 assert_response :success, "can't update relation for add #{element.class}/bbox test: #{@response.body}"
759 # get it back and check the ordering
760 get api_relation_path(relation)
761 assert_response :success, "can't read back the relation: #{@response.body}"
762 check_ordering(relation_xml, @response.body)
768 # remove a member from a relation and check the bounding box is
770 def test_remove_member_bounding_box
771 relation = create(:relation)
772 node1 = create(:node, :lat => 3, :lon => 3)
773 node2 = create(:node, :lat => 5, :lon => 5)
774 create(:relation_member, :relation => relation, :member => node1)
775 create(:relation_member, :relation => relation, :member => node2)
777 check_changeset_modify(BoundingBox.new(5, 5, 5, 5)) do |changeset_id, auth_header|
778 # remove node 5 (5,5) from an existing relation
779 relation_xml = xml_for_relation(relation)
781 .find("//osm/relation/member[@type='node'][@ref='#{node2.id}']")
784 # update changeset ID to point to new changeset
785 update_changeset(relation_xml, changeset_id)
788 put api_relation_path(relation), :params => relation_xml.to_s, :headers => auth_header
789 assert_response :success, "can't update relation for remove node/bbox test"
794 # check that relations are ordered
795 def test_relation_member_ordering
797 changeset = create(:changeset, :user => user)
798 node1 = create(:node)
799 node2 = create(:node)
800 node3 = create(:node)
801 way1 = create(:way_with_nodes, :nodes_count => 2)
802 way2 = create(:way_with_nodes, :nodes_count => 2)
804 auth_header = bearer_authorization_header user
808 <relation changeset='#{changeset.id}'>
809 <member ref='#{node1.id}' type='node' role='first'/>
810 <member ref='#{node2.id}' type='node' role='second'/>
811 <member ref='#{way1.id}' type='way' role='third'/>
812 <member ref='#{way2.id}' type='way' role='fourth'/>
816 doc = XML::Parser.string(doc_str).parse
818 post api_relations_path, :params => doc.to_s, :headers => auth_header
819 assert_response :success, "can't create a relation: #{@response.body}"
820 relation_id = @response.body.to_i
822 # get it back and check the ordering
823 get api_relation_path(relation_id)
824 assert_response :success, "can't read back the relation: #{@response.body}"
825 check_ordering(doc, @response.body)
827 # insert a member at the front
828 new_member = XML::Node.new "member"
829 new_member["ref"] = node3.id.to_s
830 new_member["type"] = "node"
831 new_member["role"] = "new first"
832 doc.find("//osm/relation").first.child.prev = new_member
833 # update the version, should be 1?
834 doc.find("//osm/relation").first["id"] = relation_id.to_s
835 doc.find("//osm/relation").first["version"] = 1.to_s
837 # upload the next version of the relation
838 put api_relation_path(relation_id), :params => doc.to_s, :headers => auth_header
839 assert_response :success, "can't update relation: #{@response.body}"
840 assert_equal 2, @response.body.to_i
842 # get it back again and check the ordering again
843 get api_relation_path(relation_id)
844 assert_response :success, "can't read back the relation: #{@response.body}"
845 check_ordering(doc, @response.body)
847 # check the ordering in the history tables:
848 with_controller(OldRelationsController.new) do
849 get api_old_relation_path(relation_id, 2)
850 assert_response :success, "can't read back version 2 of the relation #{relation_id}"
851 check_ordering(doc, @response.body)
856 # check that relations can contain duplicate members
857 def test_relation_member_duplicates
858 private_user = create(:user, :data_public => false)
860 changeset = create(:changeset, :user => user)
861 node1 = create(:node)
862 node2 = create(:node)
866 <relation changeset='#{changeset.id}'>
867 <member ref='#{node1.id}' type='node' role='forward'/>
868 <member ref='#{node2.id}' type='node' role='forward'/>
869 <member ref='#{node1.id}' type='node' role='forward'/>
870 <member ref='#{node2.id}' type='node' role='forward'/>
874 doc = XML::Parser.string(doc_str).parse
876 ## First try with the private user
877 auth_header = bearer_authorization_header private_user
879 post api_relations_path, :params => doc.to_s, :headers => auth_header
880 assert_response :forbidden
882 ## Now try with the public user
883 auth_header = bearer_authorization_header user
885 post api_relations_path, :params => doc.to_s, :headers => auth_header
886 assert_response :success, "can't create a relation: #{@response.body}"
887 relation_id = @response.body.to_i
889 # get it back and check the ordering
890 get api_relation_path(relation_id)
891 assert_response :success, "can't read back the relation: #{relation_id}"
892 check_ordering(doc, @response.body)
896 # test that the ordering of elements in the history is the same as in current.
897 def test_history_ordering
899 changeset = create(:changeset, :user => user)
900 node1 = create(:node)
901 node2 = create(:node)
902 node3 = create(:node)
903 node4 = create(:node)
907 <relation changeset='#{changeset.id}'>
908 <member ref='#{node1.id}' type='node' role='forward'/>
909 <member ref='#{node4.id}' type='node' role='forward'/>
910 <member ref='#{node3.id}' type='node' role='forward'/>
911 <member ref='#{node2.id}' type='node' role='forward'/>
915 doc = XML::Parser.string(doc_str).parse
916 auth_header = bearer_authorization_header user
918 post api_relations_path, :params => doc.to_s, :headers => auth_header
919 assert_response :success, "can't create a relation: #{@response.body}"
920 relation_id = @response.body.to_i
922 # check the ordering in the current tables:
923 get api_relation_path(relation_id)
924 assert_response :success, "can't read back the relation: #{@response.body}"
925 check_ordering(doc, @response.body)
927 # check the ordering in the history tables:
928 with_controller(OldRelationsController.new) do
929 get api_old_relation_path(relation_id, 1)
930 assert_response :success, "can't read back version 1 of the relation: #{@response.body}"
931 check_ordering(doc, @response.body)
936 # remove all the members from a relation. the result is pretty useless, but
937 # still technically valid.
938 def test_remove_all_members
939 relation = create(:relation)
940 node1 = create(:node, :lat => 0.3, :lon => 0.3)
941 node2 = create(:node, :lat => 0.5, :lon => 0.5)
943 create(:way_node, :way => way, :node => node1)
944 create(:relation_member, :relation => relation, :member => way)
945 create(:relation_member, :relation => relation, :member => node2)
947 check_changeset_modify(BoundingBox.new(0.3, 0.3, 0.5, 0.5)) do |changeset_id, auth_header|
948 relation_xml = xml_for_relation(relation)
950 .find("//osm/relation/member")
953 # update changeset ID to point to new changeset
954 update_changeset(relation_xml, changeset_id)
957 put api_relation_path(relation), :params => relation_xml.to_s, :headers => auth_header
958 assert_response :success, "can't update relation for remove all members test"
959 checkrelation = Relation.find(relation.id)
960 assert_not_nil(checkrelation,
961 "uploaded relation not found in database after upload")
962 assert_equal(0, checkrelation.members.length,
963 "relation contains members but they should have all been deleted")
968 # test initial rate limit
969 def test_initial_rate_limit
974 node1 = create(:node)
975 node2 = create(:node)
977 # create a changeset that puts us near the initial rate limit
978 changeset = create(:changeset, :user => user,
979 :created_at => Time.now.utc - 5.minutes,
980 :num_changes => Settings.initial_changes_per_hour - 1)
982 # create authentication header
983 auth_header = bearer_authorization_header user
985 # try creating a relation
986 xml = "<osm><relation changeset='#{changeset.id}'>" \
987 "<member ref='#{node1.id}' type='node' role='some'/>" \
988 "<member ref='#{node2.id}' type='node' role='some'/>" \
989 "<tag k='test' v='yes' /></relation></osm>"
990 post api_relations_path, :params => xml, :headers => auth_header
991 assert_response :success, "relation create did not return success status"
993 # get the id of the relation we created
994 relationid = @response.body
996 # try updating the relation, which should be rate limited
997 xml = "<osm><relation id='#{relationid}' version='1' changeset='#{changeset.id}'>" \
998 "<member ref='#{node2.id}' type='node' role='some'/>" \
999 "<member ref='#{node1.id}' type='node' role='some'/>" \
1000 "<tag k='test' v='yes' /></relation></osm>"
1001 put api_relation_path(relationid), :params => xml, :headers => auth_header
1002 assert_response :too_many_requests, "relation update did not hit rate limit"
1004 # try deleting the relation, which should be rate limited
1005 xml = "<osm><relation id='#{relationid}' version='2' changeset='#{changeset.id}'/></osm>"
1006 delete api_relation_path(relationid), :params => xml, :headers => auth_header
1007 assert_response :too_many_requests, "relation delete did not hit rate limit"
1009 # try creating a relation, which should be rate limited
1010 xml = "<osm><relation changeset='#{changeset.id}'>" \
1011 "<member ref='#{node1.id}' type='node' role='some'/>" \
1012 "<member ref='#{node2.id}' type='node' role='some'/>" \
1013 "<tag k='test' v='yes' /></relation></osm>"
1014 post api_relations_path, :params => xml, :headers => auth_header
1015 assert_response :too_many_requests, "relation create did not hit rate limit"
1019 # test maximum rate limit
1020 def test_maximum_rate_limit
1022 user = create(:user)
1025 node1 = create(:node)
1026 node2 = create(:node)
1028 # create a changeset to establish our initial edit time
1029 changeset = create(:changeset, :user => user,
1030 :created_at => Time.now.utc - 28.days)
1032 # create changeset to put us near the maximum rate limit
1033 total_changes = Settings.max_changes_per_hour - 1
1034 while total_changes.positive?
1035 changes = [total_changes, Changeset::MAX_ELEMENTS].min
1036 changeset = create(:changeset, :user => user,
1037 :created_at => Time.now.utc - 5.minutes,
1038 :num_changes => changes)
1039 total_changes -= changes
1042 # create authentication header
1043 auth_header = bearer_authorization_header user
1045 # try creating a relation
1046 xml = "<osm><relation changeset='#{changeset.id}'>" \
1047 "<member ref='#{node1.id}' type='node' role='some'/>" \
1048 "<member ref='#{node2.id}' type='node' role='some'/>" \
1049 "<tag k='test' v='yes' /></relation></osm>"
1050 post api_relations_path, :params => xml, :headers => auth_header
1051 assert_response :success, "relation create did not return success status"
1053 # get the id of the relation we created
1054 relationid = @response.body
1056 # try updating the relation, which should be rate limited
1057 xml = "<osm><relation id='#{relationid}' version='1' changeset='#{changeset.id}'>" \
1058 "<member ref='#{node2.id}' type='node' role='some'/>" \
1059 "<member ref='#{node1.id}' type='node' role='some'/>" \
1060 "<tag k='test' v='yes' /></relation></osm>"
1061 put api_relation_path(relationid), :params => xml, :headers => auth_header
1062 assert_response :too_many_requests, "relation update did not hit rate limit"
1064 # try deleting the relation, which should be rate limited
1065 xml = "<osm><relation id='#{relationid}' version='2' changeset='#{changeset.id}'/></osm>"
1066 delete api_relation_path(relationid), :params => xml, :headers => auth_header
1067 assert_response :too_many_requests, "relation delete did not hit rate limit"
1069 # try creating a relation, which should be rate limited
1070 xml = "<osm><relation changeset='#{changeset.id}'>" \
1071 "<member ref='#{node1.id}' type='node' role='some'/>" \
1072 "<member ref='#{node2.id}' type='node' role='some'/>" \
1073 "<tag k='test' v='yes' /></relation></osm>"
1074 post api_relations_path, :params => xml, :headers => auth_header
1075 assert_response :too_many_requests, "relation create did not hit rate limit"
1080 def check_relations_for_element(path, type, id, expected_relations)
1081 # check the "relations for relation" mode
1083 assert_response :success
1085 # count one osm element
1086 assert_select "osm[version='#{Settings.api_version}'][generator='#{Settings.generator}']", 1
1088 # we should have only the expected number of relations
1089 assert_select "osm>relation", expected_relations.size
1091 # and each of them should contain the element we originally searched for
1092 expected_relations.each do |relation|
1093 # The relation should appear once, but the element could appear multiple times
1094 assert_select "osm>relation[id='#{relation.id}']", 1
1095 assert_select "osm>relation[id='#{relation.id}']>member[type='#{type}'][ref='#{id}']"
1100 # checks that the XML document and the string arguments have
1101 # members in the same order.
1102 def check_ordering(doc, xml)
1103 new_doc = XML::Parser.string(xml).parse
1105 doc_members = doc.find("//osm/relation/member").collect do |m|
1106 [m["ref"].to_i, m["type"].to_sym, m["role"]]
1109 new_members = new_doc.find("//osm/relation/member").collect do |m|
1110 [m["ref"].to_i, m["type"].to_sym, m["role"]]
1113 doc_members.zip(new_members).each do |d, n|
1114 assert_equal d, n, "members are not equal - ordering is wrong? (#{doc}, #{xml})"
1119 # create a changeset and yield to the caller to set it up, then assert
1120 # that the changeset bounding box is +bbox+.
1121 def check_changeset_modify(bbox)
1122 ## First test with the private user to check that you get a forbidden
1123 auth_header = bearer_authorization_header create(:user, :data_public => false)
1125 # create a new changeset for this operation, so we are assured
1126 # that the bounding box will be newly-generated.
1127 with_controller(Api::ChangesetsController.new) do
1128 xml = "<osm><changeset/></osm>"
1129 put changeset_create_path, :params => xml, :headers => auth_header
1130 assert_response :forbidden, "shouldn't be able to create changeset for modify test, as should get forbidden"
1133 ## Now do the whole thing with the public user
1134 auth_header = bearer_authorization_header
1136 # create a new changeset for this operation, so we are assured
1137 # that the bounding box will be newly-generated.
1138 changeset_id = with_controller(Api::ChangesetsController.new) do
1139 xml = "<osm><changeset/></osm>"
1140 put changeset_create_path, :params => xml, :headers => auth_header
1141 assert_response :success, "couldn't create changeset for modify test"
1145 # go back to the block to do the actual modifies
1146 yield changeset_id, auth_header
1148 # now download the changeset to check its bounding box
1149 with_controller(Api::ChangesetsController.new) do
1150 get changeset_show_path(changeset_id)
1151 assert_response :success, "can't re-read changeset for modify test"
1152 assert_select "osm>changeset", 1, "Changeset element doesn't exist in #{@response.body}"
1153 assert_select "osm>changeset[id='#{changeset_id}']", 1, "Changeset id=#{changeset_id} doesn't exist in #{@response.body}"
1154 assert_select "osm>changeset[min_lon='#{format('%<lon>.7f', :lon => bbox.min_lon)}']", 1, "Changeset min_lon wrong in #{@response.body}"
1155 assert_select "osm>changeset[min_lat='#{format('%<lat>.7f', :lat => bbox.min_lat)}']", 1, "Changeset min_lat wrong in #{@response.body}"
1156 assert_select "osm>changeset[max_lon='#{format('%<lon>.7f', :lon => bbox.max_lon)}']", 1, "Changeset max_lon wrong in #{@response.body}"
1157 assert_select "osm>changeset[max_lat='#{format('%<lat>.7f', :lat => bbox.max_lat)}']", 1, "Changeset max_lat wrong in #{@response.body}"
1162 # yields the relation with the given +id+ (and optional +version+
1163 # to read from the history tables) into the block. the parsed XML
1165 def with_relation(id, ver = nil)
1167 get api_relation_path(id)
1169 with_controller(OldRelationsController.new) do
1170 get api_old_relation_path(id, ver)
1173 assert_response :success
1174 yield xml_parse(@response.body)
1178 # updates the relation (XML) +rel+ and
1179 # yields the new version of that relation into the block.
1180 # the parsed XML doc is returned.
1181 def with_update(rel, headers)
1182 rel_id = rel.find("//osm/relation").first["id"].to_i
1183 put api_relation_path(rel_id), :params => rel.to_s, :headers => headers
1184 assert_response :success, "can't update relation: #{@response.body}"
1185 version = @response.body.to_i
1187 # now get the new version
1188 get api_relation_path(rel_id)
1189 assert_response :success
1190 new_rel = xml_parse(@response.body)
1198 # updates the relation (XML) +rel+ via the diff-upload API and
1199 # yields the new version of that relation into the block.
1200 # the parsed XML doc is returned.
1201 def with_update_diff(rel, headers)
1202 rel_id = rel.find("//osm/relation").first["id"].to_i
1203 cs_id = rel.find("//osm/relation").first["changeset"].to_i
1206 with_controller(Api::ChangesetsController.new) do
1207 doc = OSM::API.new.xml_doc
1208 change = XML::Node.new "osmChange"
1210 modify = XML::Node.new "modify"
1212 modify << doc.import(rel.find("//osm/relation").first)
1214 post changeset_upload_path(cs_id), :params => doc.to_s, :headers => headers
1215 assert_response :success, "can't upload diff relation: #{@response.body}"
1216 version = xml_parse(@response.body).find("//diffResult/relation").first["new_version"].to_i
1219 # now get the new version
1220 get api_relation_path(rel_id)
1221 assert_response :success
1222 new_rel = xml_parse(@response.body)
1230 # returns a k->v hash of tags from an xml doc
1231 def get_tags_as_hash(a)
1232 a.find("//osm/relation/tag").sort_by { |v| v["k"] }.each_with_object({}) do |v, h|
1238 # assert that all tags on relation documents +a+ and +b+
1240 def assert_tags_equal(a, b)
1241 # turn the XML doc into tags hashes
1242 a_tags = get_tags_as_hash(a)
1243 b_tags = get_tags_as_hash(b)
1245 assert_equal a_tags.keys, b_tags.keys, "Tag keys should be identical."
1246 a_tags.each do |k, v|
1247 assert_equal v, b_tags[k],
1248 "Tags which were not altered should be the same. " \
1249 "#{a_tags.inspect} != #{b_tags.inspect}"
1254 # update the changeset_id of a node element
1255 def update_changeset(xml, changeset_id)
1256 xml_attr_rewrite(xml, "changeset", changeset_id)
1260 # update an attribute in the node element
1261 def xml_attr_rewrite(xml, name, value)
1262 xml.find("//osm/relation").first[name] = value.to_s
1269 parser = XML::Parser.string(xml)