2 require "relation_controller"
4 class RelationControllerTest < ActionController::TestCase
8 # test all routes which lead to this controller
11 { :path => "/api/0.6/relation/create", :method => :put },
12 { :controller => "relation", :action => "create" }
15 { :path => "/api/0.6/relation/1/full", :method => :get },
16 { :controller => "relation", :action => "full", :id => "1" }
19 { :path => "/api/0.6/relation/1", :method => :get },
20 { :controller => "relation", :action => "read", :id => "1" }
23 { :path => "/api/0.6/relation/1", :method => :put },
24 { :controller => "relation", :action => "update", :id => "1" }
27 { :path => "/api/0.6/relation/1", :method => :delete },
28 { :controller => "relation", :action => "delete", :id => "1" }
31 { :path => "/api/0.6/relations", :method => :get },
32 { :controller => "relation", :action => "relations" }
36 { :path => "/api/0.6/node/1/relations", :method => :get },
37 { :controller => "relation", :action => "relations_for_node", :id => "1" }
40 { :path => "/api/0.6/way/1/relations", :method => :get },
41 { :controller => "relation", :action => "relations_for_way", :id => "1" }
44 { :path => "/api/0.6/relation/1/relations", :method => :get },
45 { :controller => "relation", :action => "relations_for_relation", :id => "1" }
49 # -------------------------------------
50 # Test reading relations.
51 # -------------------------------------
54 # check that a visible relation is returned properly
55 get :read, :id => current_relations(:visible_relation).id
56 assert_response :success
58 # check that an invisible relation is not returned
59 get :read, :id => current_relations(:invisible_relation).id
62 # check chat a non-existent relation is not returned
64 assert_response :not_found
68 # check that all relations containing a particular node, and no extra
69 # relations, are returned from the relations_for_node call.
70 def test_relations_for_node
72 # should include relations with that node as a member
73 relation_with_node = create(:relation_member, :member => node).relation
74 # should ignore relations without that node as a member
75 _relation_without_node = create(:relation_member).relation
76 # should ignore relations with the node involved indirectly, via a way
77 way = create(:way_node, :node => node).way
78 _relation_with_way = create(:relation_member, :member => way).relation
79 # should ignore relations with the node involved indirectly, via a relation
80 second_relation = create(:relation_member, :member => node).relation
81 _super_relation = create(:relation_member, :member => second_relation).relation
82 # should combine multiple relation_member references into just one relation entry
83 create(:relation_member, :member => node, :relation => relation_with_node, :sequence_id => 2)
84 # should not include deleted relations
85 deleted_relation = create(:relation, :deleted)
86 create(:relation_member, :member => node, :relation => deleted_relation)
88 check_relations_for_element(:relations_for_node, "node",
90 [relation_with_node, second_relation])
93 def test_relations_for_way
95 # should include relations with that way as a member
96 relation_with_way = create(:relation_member, :member => way).relation
97 # should ignore relations without that way as a member
98 _relation_without_way = create(:relation_member).relation
99 # should ignore relations with the way involved indirectly, via a relation
100 second_relation = create(:relation_member, :member => way).relation
101 _super_relation = create(:relation_member, :member => second_relation).relation
102 # should combine multiple relation_member references into just one relation entry
103 create(:relation_member, :member => way, :relation => relation_with_way, :sequence_id => 2)
104 # should not include deleted relations
105 deleted_relation = create(:relation, :deleted)
106 create(:relation_member, :member => way, :relation => deleted_relation)
108 check_relations_for_element(:relations_for_way, "way",
110 [relation_with_way, second_relation])
113 def test_relations_for_relation
114 relation = create(:relation)
115 # should include relations with that relation as a member
116 relation_with_relation = create(:relation_member, :member => relation).relation
117 # should ignore any relation without that relation as a member
118 _relation_without_relation = create(:relation_member).relation
119 # should ignore relations with the relation involved indirectly, via a relation
120 second_relation = create(:relation_member, :member => relation).relation
121 _super_relation = create(:relation_member, :member => second_relation).relation
122 # should combine multiple relation_member references into just one relation entry
123 create(:relation_member, :member => relation, :relation => relation_with_relation, :sequence_id => 2)
124 # should not include deleted relations
125 deleted_relation = create(:relation, :deleted)
126 create(:relation_member, :member => relation, :relation => deleted_relation)
127 check_relations_for_element(:relations_for_relation, "relation",
129 [relation_with_relation, second_relation])
132 def check_relations_for_element(method, type, id, expected_relations)
133 # check the "relations for relation" mode
134 get method, :id => id
135 assert_response :success
137 # count one osm element
138 assert_select "osm[version='#{API_VERSION}'][generator='OpenStreetMap server']", 1
140 # we should have only the expected number of relations
141 assert_select "osm>relation", expected_relations.size
143 # and each of them should contain the element we originally searched for
144 expected_relations.each do |relation|
145 # The relation should appear once, but the element could appear multiple times
146 assert_select "osm>relation[id='#{relation.id}']", 1
147 assert_select "osm>relation[id='#{relation.id}']>member[type='#{type}'][ref='#{id}']"
152 # check the "full" mode
153 get :full, :id => 999999
154 assert_response :not_found
156 get :full, :id => current_relations(:invisible_relation).id
157 assert_response :gone
159 get :full, :id => current_relations(:visible_relation).id
160 assert_response :success
161 # FIXME: check whether this contains the stuff we want!
165 # test fetching multiple relations
167 # check error when no parameter provided
169 assert_response :bad_request
171 # check error when no parameter value provided
172 get :relations, :relations => ""
173 assert_response :bad_request
175 # test a working call
176 get :relations, :relations => "1,2,4,7"
177 assert_response :success
178 assert_select "osm" do
179 assert_select "relation", :count => 4
180 assert_select "relation[id='1'][visible='true']", :count => 1
181 assert_select "relation[id='2'][visible='false']", :count => 1
182 assert_select "relation[id='4'][visible='true']", :count => 1
183 assert_select "relation[id='7'][visible='true']", :count => 1
186 # check error when a non-existent relation is included
187 get :relations, :relations => "1,2,4,7,400"
188 assert_response :not_found
191 # -------------------------------------
192 # Test simple relation creation.
193 # -------------------------------------
196 private_user = create(:user, :data_public => false)
197 private_changeset = create(:changeset, :user => private_user)
199 changeset = create(:changeset, :user => user)
201 way = create(:way_with_nodes, :nodes_count => 2)
203 basic_authorization private_user.email, "test"
205 # create an relation without members
206 content "<osm><relation changeset='#{private_changeset.id}'><tag k='test' v='yes' /></relation></osm>"
208 # hope for forbidden, due to user
209 assert_response :forbidden,
210 "relation upload should have failed with forbidden"
213 # create an relation with a node as member
214 # This time try with a role attribute in the relation
215 content "<osm><relation changeset='#{private_changeset.id}'>" +
216 "<member ref='#{node.id}' type='node' role='some'/>" +
217 "<tag k='test' v='yes' /></relation></osm>"
219 # hope for forbidden due to user
220 assert_response :forbidden,
221 "relation upload did not return forbidden status"
224 # create an relation with a node as member, this time test that we don't
225 # need a role attribute to be included
226 content "<osm><relation changeset='#{private_changeset.id}'>" +
227 "<member ref='#{node.id}' type='node'/>" + "<tag k='test' v='yes' /></relation></osm>"
229 # hope for forbidden due to user
230 assert_response :forbidden,
231 "relation upload did not return forbidden status"
234 # create an relation with a way and a node as members
235 content "<osm><relation changeset='#{private_changeset.id}'>" +
236 "<member type='node' ref='#{node.id}' role='some'/>" +
237 "<member type='way' ref='#{way.id}' role='other'/>" +
238 "<tag k='test' v='yes' /></relation></osm>"
240 # hope for forbidden, due to user
241 assert_response :forbidden,
242 "relation upload did not return success status"
244 ## Now try with the public user
245 basic_authorization user.email, "test"
247 # create an relation without members
248 content "<osm><relation changeset='#{changeset.id}'><tag k='test' v='yes' /></relation></osm>"
251 assert_response :success,
252 "relation upload did not return success status"
253 # read id of created relation and search for it
254 relationid = @response.body
255 checkrelation = Relation.find(relationid)
256 assert_not_nil checkrelation,
257 "uploaded relation not found in data base after upload"
259 assert_equal checkrelation.members.length, 0,
260 "saved relation contains members but should not"
261 assert_equal checkrelation.tags.length, 1,
262 "saved relation does not contain exactly one tag"
263 assert_equal changeset.id, checkrelation.changeset.id,
264 "saved relation does not belong in the changeset it was assigned to"
265 assert_equal user.id, checkrelation.changeset.user_id,
266 "saved relation does not belong to user that created it"
267 assert_equal true, checkrelation.visible,
268 "saved relation is not visible"
269 # ok the relation is there but can we also retrieve it?
270 get :read, :id => relationid
271 assert_response :success
274 # create an relation with a node as member
275 # This time try with a role attribute in the relation
276 content "<osm><relation changeset='#{changeset.id}'>" +
277 "<member ref='#{node.id}' type='node' role='some'/>" +
278 "<tag k='test' v='yes' /></relation></osm>"
281 assert_response :success,
282 "relation upload did not return success status"
283 # read id of created relation and search for it
284 relationid = @response.body
285 checkrelation = Relation.find(relationid)
286 assert_not_nil checkrelation,
287 "uploaded relation not found in data base after upload"
289 assert_equal checkrelation.members.length, 1,
290 "saved relation does not contain exactly one member"
291 assert_equal checkrelation.tags.length, 1,
292 "saved relation does not contain exactly one tag"
293 assert_equal changeset.id, checkrelation.changeset.id,
294 "saved relation does not belong in the changeset it was assigned to"
295 assert_equal user.id, checkrelation.changeset.user_id,
296 "saved relation does not belong to user that created it"
297 assert_equal true, checkrelation.visible,
298 "saved relation is not visible"
299 # ok the relation is there but can we also retrieve it?
301 get :read, :id => relationid
302 assert_response :success
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 content "<osm><relation changeset='#{changeset.id}'>" +
308 "<member ref='#{node.id}' type='node'/>" + "<tag k='test' v='yes' /></relation></osm>"
311 assert_response :success,
312 "relation upload did not return success status"
313 # read id of created relation and search for it
314 relationid = @response.body
315 checkrelation = Relation.find(relationid)
316 assert_not_nil checkrelation,
317 "uploaded relation not found in data base after upload"
319 assert_equal checkrelation.members.length, 1,
320 "saved relation does not contain exactly one member"
321 assert_equal checkrelation.tags.length, 1,
322 "saved relation does not contain exactly one tag"
323 assert_equal changeset.id, checkrelation.changeset.id,
324 "saved relation does not belong in the changeset it was assigned to"
325 assert_equal user.id, checkrelation.changeset.user_id,
326 "saved relation does not belong to user that created it"
327 assert_equal true, checkrelation.visible,
328 "saved relation is not visible"
329 # ok the relation is there but can we also retrieve it?
331 get :read, :id => relationid
332 assert_response :success
335 # create an relation with a way and a node as members
336 content "<osm><relation changeset='#{changeset.id}'>" +
337 "<member type='node' ref='#{node.id}' role='some'/>" +
338 "<member type='way' ref='#{way.id}' role='other'/>" +
339 "<tag k='test' v='yes' /></relation></osm>"
342 assert_response :success,
343 "relation upload did not return success status"
344 # read id of created relation and search for it
345 relationid = @response.body
346 checkrelation = Relation.find(relationid)
347 assert_not_nil checkrelation,
348 "uploaded relation not found in data base after upload"
350 assert_equal checkrelation.members.length, 2,
351 "saved relation does not have exactly two members"
352 assert_equal checkrelation.tags.length, 1,
353 "saved relation does not contain exactly one tag"
354 assert_equal changeset.id, checkrelation.changeset.id,
355 "saved relation does not belong in the changeset it was assigned to"
356 assert_equal user.id, checkrelation.changeset.user_id,
357 "saved relation does not belong to user that created it"
358 assert_equal true, checkrelation.visible,
359 "saved relation is not visible"
360 # ok the relation is there but can we also retrieve it?
361 get :read, :id => relationid
362 assert_response :success
365 # ------------------------------------
366 # Test updating relations
367 # ------------------------------------
370 # test that, when tags are updated on a relation, the correct things
371 # happen to the correct tables and the API gives sensible results.
372 # this is to test a case that gregory marler noticed and posted to
374 ## FIXME Move this to an integration test
375 def test_update_relation_tags
376 basic_authorization "test@example.com", "test"
377 rel_id = current_relations(:multi_tag_relation).id
378 create_list(:relation_tag, 4, :relation => current_relations(:multi_tag_relation))
379 cs_id = changesets(:public_user_first_change).id
381 with_relation(rel_id) do |rel|
382 # alter one of the tags
383 tag = rel.find("//osm/relation/tag").first
384 tag["v"] = "some changed value"
385 update_changeset(rel, cs_id)
387 # check that the downloaded tags are the same as the uploaded tags...
388 new_version = with_update(rel) do |new_rel|
389 assert_tags_equal rel, new_rel
392 # check the original one in the current_* table again
393 with_relation(rel_id) { |r| assert_tags_equal rel, r }
395 # now check the version in the history
396 with_relation(rel_id, new_version) { |r| assert_tags_equal rel, r }
401 # test that, when tags are updated on a relation when using the diff
402 # upload function, the correct things happen to the correct tables
403 # and the API gives sensible results. this is to test a case that
404 # gregory marler noticed and posted to josm-dev.
405 def test_update_relation_tags_via_upload
407 changeset = create(:changeset, :user => user)
408 relation = create(:relation)
409 create_list(:relation_tag, 4, :relation => relation)
411 basic_authorization user.email, "test"
413 with_relation(relation.id) do |rel|
414 # alter one of the tags
415 tag = rel.find("//osm/relation/tag").first
416 tag["v"] = "some changed value"
417 update_changeset(rel, changeset.id)
419 # check that the downloaded tags are the same as the uploaded tags...
420 new_version = with_update_diff(rel) do |new_rel|
421 assert_tags_equal rel, new_rel
424 # check the original one in the current_* table again
425 with_relation(relation.id) { |r| assert_tags_equal rel, r }
427 # now check the version in the history
428 with_relation(relation.id, new_version) { |r| assert_tags_equal rel, r }
432 def test_update_wrong_id
434 changeset = create(:changeset, :user => user)
435 relation = create(:relation)
436 other_relation = create(:relation)
438 basic_authorization user.email, "test"
439 with_relation(relation.id) do |rel|
440 update_changeset(rel, changeset.id)
442 put :update, :id => other_relation.id
443 assert_response :bad_request
447 # -------------------------------------
448 # Test creating some invalid relations.
449 # -------------------------------------
451 def test_create_invalid
453 changeset = create(:changeset, :user => user)
455 basic_authorization user.email, "test"
457 # create a relation with non-existing node as member
458 content "<osm><relation changeset='#{changeset.id}'>" +
459 "<member type='node' ref='0'/><tag k='test' v='yes' />" +
463 assert_response :precondition_failed,
464 "relation upload with invalid node did not return 'precondition failed'"
465 assert_equal "Precondition failed: Relation with id cannot be saved due to Node with id 0", @response.body
468 # -------------------------------------
469 # Test creating a relation, with some invalid XML
470 # -------------------------------------
471 def test_create_invalid_xml
473 changeset = create(:changeset, :user => user)
476 basic_authorization user.email, "test"
478 # create some xml that should return an error
479 content "<osm><relation changeset='#{changeset.id}'>" +
480 "<member type='type' ref='#{node.id}' role=''/>" +
481 "<tag k='tester' v='yep'/></relation></osm>"
484 assert_response :bad_request
485 assert_match(/Cannot parse valid relation from xml string/, @response.body)
486 assert_match(/The type is not allowed only, /, @response.body)
489 # -------------------------------------
490 # Test deleting relations.
491 # -------------------------------------
494 private_user = create(:user, :data_public => false)
495 private_user_closed_changeset = create(:changeset, :closed, :user => private_user)
497 closed_changeset = create(:changeset, :closed, :user => user)
498 changeset = create(:changeset, :user => user)
499 relation = create(:relation)
500 used_relation = create(:relation)
501 super_relation = create(:relation_member, :member => used_relation).relation
502 deleted_relation = create(:relation, :deleted)
503 multi_tag_relation = create(:relation)
504 create_list(:relation_tag, 4, :relation => multi_tag_relation)
506 ## First try to delete relation without auth
507 delete :delete, :id => relation.id
508 assert_response :unauthorized
510 ## Then try with the private user, to make sure that you get a forbidden
511 basic_authorization(private_user.email, "test")
513 # this shouldn't work, as we should need the payload...
514 delete :delete, :id => relation.id
515 assert_response :forbidden
517 # try to delete without specifying a changeset
518 content "<osm><relation id='#{relation.id}'/></osm>"
519 delete :delete, :id => relation.id
520 assert_response :forbidden
522 # try to delete with an invalid (closed) changeset
523 content update_changeset(relation.to_xml,
524 private_user_closed_changeset.id)
525 delete :delete, :id => relation.id
526 assert_response :forbidden
528 # try to delete with an invalid (non-existent) changeset
529 content update_changeset(relation.to_xml, 0)
530 delete :delete, :id => relation.id
531 assert_response :forbidden
533 # this won't work because the relation is in-use by another relation
534 content(used_relation.to_xml)
535 delete :delete, :id => used_relation.id
536 assert_response :forbidden
538 # this should work when we provide the appropriate payload...
539 content(relation.to_xml)
540 delete :delete, :id => relation.id
541 assert_response :forbidden
543 # this won't work since the relation is already deleted
544 content(deleted_relation.to_xml)
545 delete :delete, :id => deleted_relation.id
546 assert_response :forbidden
548 # this won't work since the relation never existed
549 delete :delete, :id => 0
550 assert_response :forbidden
552 ## now set auth for the public user
553 basic_authorization(user.email, "test")
555 # this shouldn't work, as we should need the payload...
556 delete :delete, :id => relation.id
557 assert_response :bad_request
559 # try to delete without specifying a changeset
560 content "<osm><relation id='#{relation.id}' version='#{relation.version}' /></osm>"
561 delete :delete, :id => relation.id
562 assert_response :bad_request
563 assert_match(/Changeset id is missing/, @response.body)
565 # try to delete with an invalid (closed) changeset
566 content update_changeset(relation.to_xml,
568 delete :delete, :id => relation.id
569 assert_response :conflict
571 # try to delete with an invalid (non-existent) changeset
572 content update_changeset(relation.to_xml, 0)
573 delete :delete, :id => relation.id
574 assert_response :conflict
576 # this won't work because the relation is in a changeset owned by someone else
577 content update_changeset(relation.to_xml, create(:changeset).id)
578 delete :delete, :id => relation.id
579 assert_response :conflict,
580 "shouldn't be able to delete a relation in a changeset owned by someone else (#{@response.body})"
582 # this won't work because the relation in the payload is different to that passed
583 content update_changeset(relation.to_xml, changeset.id)
584 delete :delete, :id => create(:relation).id
585 assert_response :bad_request, "shouldn't be able to delete a relation when payload is different to the url"
587 # this won't work because the relation is in-use by another relation
588 content update_changeset(used_relation.to_xml, changeset.id)
589 delete :delete, :id => used_relation.id
590 assert_response :precondition_failed,
591 "shouldn't be able to delete a relation used in a relation (#{@response.body})"
592 assert_equal "Precondition failed: The relation #{used_relation.id} is used in relation #{super_relation.id}.", @response.body
594 # this should work when we provide the appropriate payload...
595 content update_changeset(multi_tag_relation.to_xml, changeset.id)
596 delete :delete, :id => multi_tag_relation.id
597 assert_response :success
599 # valid delete should return the new version number, which should
600 # be greater than the old version number
601 assert @response.body.to_i > multi_tag_relation.version,
602 "delete request should return a new version number for relation"
604 # this won't work since the relation is already deleted
605 content update_changeset(deleted_relation.to_xml, changeset.id)
606 delete :delete, :id => deleted_relation.id
607 assert_response :gone
609 # Public visible relation needs to be deleted
610 content update_changeset(super_relation.to_xml, changeset.id)
611 delete :delete, :id => super_relation.id
612 assert_response :success
614 # this works now because the relation which was using this one
616 content update_changeset(used_relation.to_xml, changeset.id)
617 delete :delete, :id => used_relation.id
618 assert_response :success,
619 "should be able to delete a relation used in an old relation (#{@response.body})"
621 # this won't work since the relation never existed
622 delete :delete, :id => 0
623 assert_response :not_found
627 # when a relation's tag is modified then it should put the bounding
628 # box of all its members into the changeset.
629 def test_tag_modify_bounding_box
630 # in current fixtures, relation 5 contains nodes 3 and 5 (node 3
631 # indirectly via way 3), so the bbox should be [3,3,5,5].
632 check_changeset_modify(BoundingBox.new(3, 3, 5, 5)) do |changeset_id|
633 # add a tag to an existing relation
634 relation_xml = current_relations(:visible_relation).to_xml
635 relation_element = relation_xml.find("//osm/relation").first
636 new_tag = XML::Node.new("tag")
637 new_tag["k"] = "some_new_tag"
638 new_tag["v"] = "some_new_value"
639 relation_element << new_tag
641 # update changeset ID to point to new changeset
642 update_changeset(relation_xml, changeset_id)
646 put :update, :id => current_relations(:visible_relation).id
647 assert_response :success, "can't update relation for tag/bbox test"
652 # add a member to a relation and check the bounding box is only that
654 def test_add_member_bounding_box
655 relation_id = current_relations(:visible_relation).id
657 [current_nodes(:used_node_1),
658 current_nodes(:used_node_2),
659 current_ways(:used_way),
660 current_ways(:way_with_versions)].each_with_index do |element, _version|
661 bbox = element.bbox.to_unscaled
662 check_changeset_modify(bbox) do |changeset_id|
663 relation_xml = Relation.find(relation_id).to_xml
664 relation_element = relation_xml.find("//osm/relation").first
665 new_member = XML::Node.new("member")
666 new_member["ref"] = element.id.to_s
667 new_member["type"] = element.class.to_s.downcase
668 new_member["role"] = "some_role"
669 relation_element << new_member
671 # update changeset ID to point to new changeset
672 update_changeset(relation_xml, changeset_id)
676 put :update, :id => current_relations(:visible_relation).id
677 assert_response :success, "can't update relation for add #{element.class}/bbox test: #{@response.body}"
679 # get it back and check the ordering
680 get :read, :id => relation_id
681 assert_response :success, "can't read back the relation: #{@response.body}"
682 check_ordering(relation_xml, @response.body)
688 # remove a member from a relation and check the bounding box is
690 def test_remove_member_bounding_box
691 check_changeset_modify(BoundingBox.new(5, 5, 5, 5)) do |changeset_id|
692 # remove node 5 (5,5) from an existing relation
693 relation_xml = current_relations(:visible_relation).to_xml
695 .find("//osm/relation/member[@type='node'][@ref='5']")
698 # update changeset ID to point to new changeset
699 update_changeset(relation_xml, changeset_id)
703 put :update, :id => current_relations(:visible_relation).id
704 assert_response :success, "can't update relation for remove node/bbox test"
709 # check that relations are ordered
710 def test_relation_member_ordering
712 changeset = create(:changeset, :user => user)
713 node1 = create(:node)
714 node2 = create(:node)
715 node3 = create(:node)
716 way1 = create(:way_with_nodes, :nodes_count => 2)
717 way2 = create(:way_with_nodes, :nodes_count => 2)
719 basic_authorization(user.email, "test")
723 <relation changeset='#{changeset.id}'>
724 <member ref='#{node1.id}' type='node' role='first'/>
725 <member ref='#{node2.id}' type='node' role='second'/>
726 <member ref='#{way1.id}' type='way' role='third'/>
727 <member ref='#{way2.id}' type='way' role='fourth'/>
731 doc = XML::Parser.string(doc_str).parse
735 assert_response :success, "can't create a relation: #{@response.body}"
736 relation_id = @response.body.to_i
738 # get it back and check the ordering
739 get :read, :id => relation_id
740 assert_response :success, "can't read back the relation: #{@response.body}"
741 check_ordering(doc, @response.body)
743 # insert a member at the front
744 new_member = XML::Node.new "member"
745 new_member["ref"] = node3.id.to_s
746 new_member["type"] = "node"
747 new_member["role"] = "new first"
748 doc.find("//osm/relation").first.child.prev = new_member
749 # update the version, should be 1?
750 doc.find("//osm/relation").first["id"] = relation_id.to_s
751 doc.find("//osm/relation").first["version"] = 1.to_s
753 # upload the next version of the relation
755 put :update, :id => relation_id
756 assert_response :success, "can't update relation: #{@response.body}"
757 assert_equal 2, @response.body.to_i
759 # get it back again and check the ordering again
760 get :read, :id => relation_id
761 assert_response :success, "can't read back the relation: #{@response.body}"
762 check_ordering(doc, @response.body)
764 # check the ordering in the history tables:
765 with_controller(OldRelationController.new) do
766 get :version, :id => relation_id, :version => 2
767 assert_response :success, "can't read back version 2 of the relation #{relation_id}"
768 check_ordering(doc, @response.body)
773 # check that relations can contain duplicate members
774 def test_relation_member_duplicates
775 private_user = create(:user, :data_public => false)
777 changeset = create(:changeset, :user => user)
778 node1 = create(:node)
779 node2 = create(:node)
783 <relation changeset='#{changeset.id}'>
784 <member ref='#{node1.id}' type='node' role='forward'/>
785 <member ref='#{node2.id}' type='node' role='forward'/>
786 <member ref='#{node1.id}' type='node' role='forward'/>
787 <member ref='#{node2.id}' type='node' role='forward'/>
791 doc = XML::Parser.string(doc_str).parse
793 ## First try with the private user
794 basic_authorization(private_user.email, "test")
798 assert_response :forbidden
800 ## Now try with the public user
801 basic_authorization(user.email, "test")
805 assert_response :success, "can't create a relation: #{@response.body}"
806 relation_id = @response.body.to_i
808 # get it back and check the ordering
809 get :read, :id => relation_id
810 assert_response :success, "can't read back the relation: #{relation_id}"
811 check_ordering(doc, @response.body)
815 # test that the ordering of elements in the history is the same as in current.
816 def test_history_ordering
818 changeset = create(:changeset, :user => user)
819 node1 = create(:node)
820 node2 = create(:node)
821 node3 = create(:node)
822 node4 = create(:node)
826 <relation changeset='#{changeset.id}'>
827 <member ref='#{node1.id}' type='node' role='forward'/>
828 <member ref='#{node4.id}' type='node' role='forward'/>
829 <member ref='#{node3.id}' type='node' role='forward'/>
830 <member ref='#{node2.id}' type='node' role='forward'/>
834 doc = XML::Parser.string(doc_str).parse
835 basic_authorization(user.email, "test")
839 assert_response :success, "can't create a relation: #{@response.body}"
840 relation_id = @response.body.to_i
842 # check the ordering in the current tables:
843 get :read, :id => 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(OldRelationController.new) do
849 get :version, :id => relation_id, :version => 1
850 assert_response :success, "can't read back version 1 of the relation: #{@response.body}"
851 check_ordering(doc, @response.body)
856 # remove all the members from a relation. the result is pretty useless, but
857 # still technically valid.
858 def test_remove_all_members
859 check_changeset_modify(BoundingBox.new(3, 3, 5, 5)) do |changeset_id|
860 relation_xml = current_relations(:visible_relation).to_xml
862 .find("//osm/relation/member")
865 # update changeset ID to point to new changeset
866 update_changeset(relation_xml, changeset_id)
870 put :update, :id => current_relations(:visible_relation).id
871 assert_response :success, "can't update relation for remove all members test"
872 checkrelation = Relation.find(current_relations(:visible_relation).id)
873 assert_not_nil(checkrelation,
874 "uploaded relation not found in database after upload")
875 assert_equal(0, checkrelation.members.length,
876 "relation contains members but they should have all been deleted")
880 # ============================================================
882 # ============================================================
885 # checks that the XML document and the string arguments have
886 # members in the same order.
887 def check_ordering(doc, xml)
888 new_doc = XML::Parser.string(xml).parse
890 doc_members = doc.find("//osm/relation/member").collect do |m|
891 [m["ref"].to_i, m["type"].to_sym, m["role"]]
894 new_members = new_doc.find("//osm/relation/member").collect do |m|
895 [m["ref"].to_i, m["type"].to_sym, m["role"]]
898 doc_members.zip(new_members).each do |d, n|
899 assert_equal d, n, "members are not equal - ordering is wrong? (#{doc}, #{xml})"
904 # create a changeset and yield to the caller to set it up, then assert
905 # that the changeset bounding box is +bbox+.
906 def check_changeset_modify(bbox)
907 ## First test with the private user to check that you get a forbidden
908 basic_authorization(users(:normal_user).email, "test")
910 # create a new changeset for this operation, so we are assured
911 # that the bounding box will be newly-generated.
912 changeset_id = with_controller(ChangesetController.new) do
913 content "<osm><changeset/></osm>"
915 assert_response :forbidden, "shouldn't be able to create changeset for modify test, as should get forbidden"
918 ## Now do the whole thing with the public user
919 basic_authorization(users(:public_user).email, "test")
921 # create a new changeset for this operation, so we are assured
922 # that the bounding box will be newly-generated.
923 changeset_id = with_controller(ChangesetController.new) do
924 content "<osm><changeset/></osm>"
926 assert_response :success, "couldn't create changeset for modify test"
930 # go back to the block to do the actual modifies
933 # now download the changeset to check its bounding box
934 with_controller(ChangesetController.new) do
935 get :read, :id => changeset_id
936 assert_response :success, "can't re-read changeset for modify test"
937 assert_select "osm>changeset", 1, "Changeset element doesn't exist in #{@response.body}"
938 assert_select "osm>changeset[id='#{changeset_id}']", 1, "Changeset id=#{changeset_id} doesn't exist in #{@response.body}"
939 assert_select "osm>changeset[min_lon='#{bbox.min_lon}']", 1, "Changeset min_lon wrong in #{@response.body}"
940 assert_select "osm>changeset[min_lat='#{bbox.min_lat}']", 1, "Changeset min_lat wrong in #{@response.body}"
941 assert_select "osm>changeset[max_lon='#{bbox.max_lon}']", 1, "Changeset max_lon wrong in #{@response.body}"
942 assert_select "osm>changeset[max_lat='#{bbox.max_lat}']", 1, "Changeset max_lat wrong in #{@response.body}"
947 # yields the relation with the given +id+ (and optional +version+
948 # to read from the history tables) into the block. the parsed XML
950 def with_relation(id, ver = nil)
954 with_controller(OldRelationController.new) do
955 get :version, :id => id, :version => ver
958 assert_response :success
959 yield xml_parse(@response.body)
963 # updates the relation (XML) +rel+ and
964 # yields the new version of that relation into the block.
965 # the parsed XML doc is retured.
967 rel_id = rel.find("//osm/relation").first["id"].to_i
969 put :update, :id => rel_id
970 assert_response :success, "can't update relation: #{@response.body}"
971 version = @response.body.to_i
973 # now get the new version
974 get :read, :id => rel_id
975 assert_response :success
976 new_rel = xml_parse(@response.body)
984 # updates the relation (XML) +rel+ via the diff-upload API and
985 # yields the new version of that relation into the block.
986 # the parsed XML doc is retured.
987 def with_update_diff(rel)
988 rel_id = rel.find("//osm/relation").first["id"].to_i
989 cs_id = rel.find("//osm/relation").first["changeset"].to_i
992 with_controller(ChangesetController.new) do
993 doc = OSM::API.new.get_xml_doc
994 change = XML::Node.new "osmChange"
996 modify = XML::Node.new "modify"
998 modify << doc.import(rel.find("//osm/relation").first)
1001 post :upload, :id => cs_id
1002 assert_response :success, "can't upload diff relation: #{@response.body}"
1003 version = xml_parse(@response.body).find("//diffResult/relation").first["new_version"].to_i
1006 # now get the new version
1007 get :read, :id => rel_id
1008 assert_response :success
1009 new_rel = xml_parse(@response.body)
1017 # returns a k->v hash of tags from an xml doc
1018 def get_tags_as_hash(a)
1019 a.find("//osm/relation/tag").sort_by { |v| v["k"] }.each_with_object({}) do |v, h|
1025 # assert that all tags on relation documents +a+ and +b+
1027 def assert_tags_equal(a, b)
1028 # turn the XML doc into tags hashes
1029 a_tags = get_tags_as_hash(a)
1030 b_tags = get_tags_as_hash(b)
1032 assert_equal a_tags.keys, b_tags.keys, "Tag keys should be identical."
1033 a_tags.each do |k, v|
1034 assert_equal v, b_tags[k],
1035 "Tags which were not altered should be the same. " +
1036 "#{a_tags.inspect} != #{b_tags.inspect}"
1041 # update the changeset_id of a node element
1042 def update_changeset(xml, changeset_id)
1043 xml_attr_rewrite(xml, "changeset", changeset_id)
1047 # update an attribute in the node element
1048 def xml_attr_rewrite(xml, name, value)
1049 xml.find("//osm/relation").first[name] = value.to_s
1056 parser = XML::Parser.string(xml)