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
71 check_relations_for_element(:relations_for_node, "node",
72 current_nodes(:node_used_by_relationship).id,
73 [ :visible_relation, :used_relation ])
76 def test_relations_for_way
77 check_relations_for_element(:relations_for_way, "way",
78 current_ways(:used_way).id,
79 [ :visible_relation ])
82 def test_relations_for_relation
83 check_relations_for_element(:relations_for_relation, "relation",
84 current_relations(:used_relation).id,
85 [ :visible_relation ])
88 def check_relations_for_element(method, type, id, expected_relations)
89 # check the "relations for relation" mode
91 assert_response :success
93 # count one osm element
94 assert_select "osm[version='#{API_VERSION}'][generator='OpenStreetMap server']", 1
96 # we should have only the expected number of relations
97 assert_select "osm>relation", expected_relations.size
99 # and each of them should contain the node we originally searched for
100 expected_relations.each do |r|
101 relation_id = current_relations(r).id
102 assert_select "osm>relation[id='#{relation_id}']>member[type='#{type}'][ref='#{id}']", 1
107 # check the "full" mode
108 get :full, :id => current_relations(:visible_relation).id
109 assert_response :success
110 # FIXME check whether this contains the stuff we want!
117 # test fetching multiple relations
119 # check error when no parameter provided
121 assert_response :bad_request
123 # check error when no parameter value provided
124 get :relations, :relations => ""
125 assert_response :bad_request
127 # test a working call
128 get :relations, :relations => "1,2,4,7"
129 assert_response :success
130 assert_select "osm" do
131 assert_select "relation", :count => 4
132 assert_select "relation[id='1'][visible='true']", :count => 1
133 assert_select "relation[id='2'][visible='false']", :count => 1
134 assert_select "relation[id='4'][visible='true']", :count => 1
135 assert_select "relation[id='7'][visible='true']", :count => 1
138 # check error when a non-existent relation is included
139 get :relations, :relations => "1,2,4,7,400"
140 assert_response :not_found
143 # -------------------------------------
144 # Test simple relation creation.
145 # -------------------------------------
148 basic_authorization users(:normal_user).email, "test"
150 # put the relation in a dummy fixture changset
151 changeset_id = changesets(:normal_user_first_change).id
153 # create an relation without members
154 content "<osm><relation changeset='#{changeset_id}'><tag k='test' v='yes' /></relation></osm>"
156 # hope for forbidden, due to user
157 assert_response :forbidden,
158 "relation upload should have failed with forbidden"
161 # create an relation with a node as member
162 # This time try with a role attribute in the relation
163 nid = current_nodes(:used_node_1).id
164 content "<osm><relation changeset='#{changeset_id}'>" +
165 "<member ref='#{nid}' type='node' role='some'/>" +
166 "<tag k='test' v='yes' /></relation></osm>"
168 # hope for forbidden due to user
169 assert_response :forbidden,
170 "relation upload did not return forbidden status"
173 # create an relation with a node as member, this time test that we don't
174 # need a role attribute to be included
175 nid = current_nodes(:used_node_1).id
176 content "<osm><relation changeset='#{changeset_id}'>" +
177 "<member ref='#{nid}' type='node'/>"+
178 "<tag k='test' v='yes' /></relation></osm>"
180 # hope for forbidden due to user
181 assert_response :forbidden,
182 "relation upload did not return forbidden status"
185 # create an relation with a way and a node as members
186 nid = current_nodes(:used_node_1).id
187 wid = current_ways(:used_way).id
188 content "<osm><relation changeset='#{changeset_id}'>" +
189 "<member type='node' ref='#{nid}' role='some'/>" +
190 "<member type='way' ref='#{wid}' role='other'/>" +
191 "<tag k='test' v='yes' /></relation></osm>"
193 # hope for forbidden, due to user
194 assert_response :forbidden,
195 "relation upload did not return success status"
199 ## Now try with the public user
200 basic_authorization users(:public_user).email, "test"
202 # put the relation in a dummy fixture changset
203 changeset_id = changesets(:public_user_first_change).id
205 # create an relation without members
206 content "<osm><relation changeset='#{changeset_id}'><tag k='test' v='yes' /></relation></osm>"
209 assert_response :success,
210 "relation upload did not return success status"
211 # read id of created relation and search for it
212 relationid = @response.body
213 checkrelation = Relation.find(relationid)
214 assert_not_nil checkrelation,
215 "uploaded relation not found in data base after upload"
217 assert_equal checkrelation.members.length, 0,
218 "saved relation contains members but should not"
219 assert_equal checkrelation.tags.length, 1,
220 "saved relation does not contain exactly one tag"
221 assert_equal changeset_id, checkrelation.changeset.id,
222 "saved relation does not belong in the changeset it was assigned to"
223 assert_equal users(:public_user).id, checkrelation.changeset.user_id,
224 "saved relation does not belong to user that created it"
225 assert_equal true, checkrelation.visible,
226 "saved relation is not visible"
227 # ok the relation is there but can we also retrieve it?
228 get :read, :id => relationid
229 assert_response :success
233 # create an relation with a node as member
234 # This time try with a role attribute in the relation
235 nid = current_nodes(:used_node_1).id
236 content "<osm><relation changeset='#{changeset_id}'>" +
237 "<member ref='#{nid}' type='node' role='some'/>" +
238 "<tag k='test' v='yes' /></relation></osm>"
241 assert_response :success,
242 "relation upload did not return success status"
243 # read id of created relation and search for it
244 relationid = @response.body
245 checkrelation = Relation.find(relationid)
246 assert_not_nil checkrelation,
247 "uploaded relation not found in data base after upload"
249 assert_equal checkrelation.members.length, 1,
250 "saved relation does not contain exactly one member"
251 assert_equal checkrelation.tags.length, 1,
252 "saved relation does not contain exactly one tag"
253 assert_equal changeset_id, checkrelation.changeset.id,
254 "saved relation does not belong in the changeset it was assigned to"
255 assert_equal users(:public_user).id, checkrelation.changeset.user_id,
256 "saved relation does not belong to user that created it"
257 assert_equal true, checkrelation.visible,
258 "saved relation is not visible"
259 # ok the relation is there but can we also retrieve it?
261 get :read, :id => relationid
262 assert_response :success
266 # create an relation with a node as member, this time test that we don't
267 # need a role attribute to be included
268 nid = current_nodes(:used_node_1).id
269 content "<osm><relation changeset='#{changeset_id}'>" +
270 "<member ref='#{nid}' type='node'/>"+
271 "<tag k='test' v='yes' /></relation></osm>"
274 assert_response :success,
275 "relation upload did not return success status"
276 # read id of created relation and search for it
277 relationid = @response.body
278 checkrelation = Relation.find(relationid)
279 assert_not_nil checkrelation,
280 "uploaded relation not found in data base after upload"
282 assert_equal checkrelation.members.length, 1,
283 "saved relation does not contain exactly one member"
284 assert_equal checkrelation.tags.length, 1,
285 "saved relation does not contain exactly one tag"
286 assert_equal changeset_id, checkrelation.changeset.id,
287 "saved relation does not belong in the changeset it was assigned to"
288 assert_equal users(:public_user).id, checkrelation.changeset.user_id,
289 "saved relation does not belong to user that created it"
290 assert_equal true, checkrelation.visible,
291 "saved relation is not visible"
292 # ok the relation is there but can we also retrieve it?
294 get :read, :id => relationid
295 assert_response :success
298 # create an relation with a way and a node as members
299 nid = current_nodes(:used_node_1).id
300 wid = current_ways(:used_way).id
301 content "<osm><relation changeset='#{changeset_id}'>" +
302 "<member type='node' ref='#{nid}' role='some'/>" +
303 "<member type='way' ref='#{wid}' role='other'/>" +
304 "<tag k='test' v='yes' /></relation></osm>"
307 assert_response :success,
308 "relation upload did not return success status"
309 # read id of created relation and search for it
310 relationid = @response.body
311 checkrelation = Relation.find(relationid)
312 assert_not_nil checkrelation,
313 "uploaded relation not found in data base after upload"
315 assert_equal checkrelation.members.length, 2,
316 "saved relation does not have exactly two members"
317 assert_equal checkrelation.tags.length, 1,
318 "saved relation does not contain exactly one tag"
319 assert_equal changeset_id, checkrelation.changeset.id,
320 "saved relation does not belong in the changeset it was assigned to"
321 assert_equal users(:public_user).id, checkrelation.changeset.user_id,
322 "saved relation does not belong to user that created it"
323 assert_equal true, checkrelation.visible,
324 "saved relation is not visible"
325 # ok the relation is there but can we also retrieve it?
326 get :read, :id => relationid
327 assert_response :success
331 # ------------------------------------
332 # Test updating relations
333 # ------------------------------------
336 # test that, when tags are updated on a relation, the correct things
337 # happen to the correct tables and the API gives sensible results.
338 # this is to test a case that gregory marler noticed and posted to
340 ## FIXME Move this to an integration test
341 def test_update_relation_tags
342 basic_authorization "test@example.com", "test"
343 rel_id = current_relations(:multi_tag_relation).id
344 cs_id = changesets(:public_user_first_change).id
346 with_relation(rel_id) do |rel|
347 # alter one of the tags
348 tag = rel.find("//osm/relation/tag").first
349 tag['v'] = 'some changed value'
350 update_changeset(rel, cs_id)
352 # check that the downloaded tags are the same as the uploaded tags...
353 new_version = with_update(rel) do |new_rel|
354 assert_tags_equal rel, new_rel
357 # check the original one in the current_* table again
358 with_relation(rel_id) { |r| assert_tags_equal rel, r }
360 # now check the version in the history
361 with_relation(rel_id, new_version) { |r| assert_tags_equal rel, r }
366 # test that, when tags are updated on a relation when using the diff
367 # upload function, the correct things happen to the correct tables
368 # and the API gives sensible results. this is to test a case that
369 # gregory marler noticed and posted to josm-dev.
370 def test_update_relation_tags_via_upload
371 basic_authorization users(:public_user).email, "test"
372 rel_id = current_relations(:multi_tag_relation).id
373 cs_id = changesets(:public_user_first_change).id
375 with_relation(rel_id) do |rel|
376 # alter one of the tags
377 tag = rel.find("//osm/relation/tag").first
378 tag['v'] = 'some changed value'
379 update_changeset(rel, cs_id)
381 # check that the downloaded tags are the same as the uploaded tags...
382 new_version = with_update_diff(rel) do |new_rel|
383 assert_tags_equal rel, new_rel
386 # check the original one in the current_* table again
387 with_relation(rel_id) { |r| assert_tags_equal rel, r }
389 # now check the version in the history
390 with_relation(rel_id, new_version) { |r| assert_tags_equal rel, r }
394 # -------------------------------------
395 # Test creating some invalid relations.
396 # -------------------------------------
398 def test_create_invalid
399 basic_authorization users(:public_user).email, "test"
401 # put the relation in a dummy fixture changset
402 changeset_id = changesets(:public_user_first_change).id
404 # create a relation with non-existing node as member
405 content "<osm><relation changeset='#{changeset_id}'>" +
406 "<member type='node' ref='0'/><tag k='test' v='yes' />" +
410 assert_response :precondition_failed,
411 "relation upload with invalid node did not return 'precondition failed'"
412 assert_equal "Precondition failed: Relation with id cannot be saved due to Node with id 0", @response.body
415 # -------------------------------------
416 # Test creating a relation, with some invalid XML
417 # -------------------------------------
418 def test_create_invalid_xml
419 basic_authorization users(:public_user).email, "test"
421 # put the relation in a dummy fixture changeset that works
422 changeset_id = changesets(:public_user_first_change).id
424 # create some xml that should return an error
425 content "<osm><relation changeset='#{changeset_id}'>" +
426 "<member type='type' ref='#{current_nodes(:used_node_1).id}' role=''/>" +
427 "<tag k='tester' v='yep'/></relation></osm>"
430 assert_response :bad_request
431 assert_match(/Cannot parse valid relation from xml string/, @response.body)
432 assert_match(/The type is not allowed only, /, @response.body)
436 # -------------------------------------
437 # Test deleting relations.
438 # -------------------------------------
441 ## First try to delete relation without auth
442 delete :delete, :id => current_relations(:visible_relation).id
443 assert_response :unauthorized
446 ## Then try with the private user, to make sure that you get a forbidden
447 basic_authorization(users(:normal_user).email, "test")
449 # this shouldn't work, as we should need the payload...
450 delete :delete, :id => current_relations(:visible_relation).id
451 assert_response :forbidden
453 # try to delete without specifying a changeset
454 content "<osm><relation id='#{current_relations(:visible_relation).id}'/></osm>"
455 delete :delete, :id => current_relations(:visible_relation).id
456 assert_response :forbidden
458 # try to delete with an invalid (closed) changeset
459 content update_changeset(current_relations(:visible_relation).to_xml,
460 changesets(:normal_user_closed_change).id)
461 delete :delete, :id => current_relations(:visible_relation).id
462 assert_response :forbidden
464 # try to delete with an invalid (non-existent) changeset
465 content update_changeset(current_relations(:visible_relation).to_xml,0)
466 delete :delete, :id => current_relations(:visible_relation).id
467 assert_response :forbidden
469 # this won't work because the relation is in-use by another relation
470 content(relations(:used_relation).to_xml)
471 delete :delete, :id => current_relations(:used_relation).id
472 assert_response :forbidden
474 # this should work when we provide the appropriate payload...
475 content(relations(:visible_relation).to_xml)
476 delete :delete, :id => current_relations(:visible_relation).id
477 assert_response :forbidden
479 # this won't work since the relation is already deleted
480 content(relations(:invisible_relation).to_xml)
481 delete :delete, :id => current_relations(:invisible_relation).id
482 assert_response :forbidden
484 # this works now because the relation which was using this one
486 content(relations(:used_relation).to_xml)
487 delete :delete, :id => current_relations(:used_relation).id
488 assert_response :forbidden
490 # this won't work since the relation never existed
491 delete :delete, :id => 0
492 assert_response :forbidden
496 ## now set auth for the public user
497 basic_authorization(users(:public_user).email, "test");
499 # this shouldn't work, as we should need the payload...
500 delete :delete, :id => current_relations(:visible_relation).id
501 assert_response :bad_request
503 # try to delete without specifying a changeset
504 content "<osm><relation id='#{current_relations(:visible_relation).id}' version='#{current_relations(:visible_relation).version}' /></osm>"
505 delete :delete, :id => current_relations(:visible_relation).id
506 assert_response :bad_request
507 assert_match(/Changeset id is missing/, @response.body)
509 # try to delete with an invalid (closed) changeset
510 content update_changeset(current_relations(:visible_relation).to_xml,
511 changesets(:normal_user_closed_change).id)
512 delete :delete, :id => current_relations(:visible_relation).id
513 assert_response :conflict
515 # try to delete with an invalid (non-existent) changeset
516 content update_changeset(current_relations(:visible_relation).to_xml,0)
517 delete :delete, :id => current_relations(:visible_relation).id
518 assert_response :conflict
520 # this won't work because the relation is in a changeset owned by someone else
521 content(relations(:used_relation).to_xml)
522 delete :delete, :id => current_relations(:used_relation).id
523 assert_response :conflict,
524 "shouldn't be able to delete a relation in a changeset owned by someone else (#{@response.body})"
526 # this won't work because the relation in the payload is different to that passed
527 content(relations(:public_used_relation).to_xml)
528 delete :delete, :id => current_relations(:used_relation).id
529 assert_not_equal relations(:public_used_relation).id, current_relations(:used_relation).id
530 assert_response :bad_request, "shouldn't be able to delete a relation when payload is different to the url"
532 # this won't work because the relation is in-use by another relation
533 content(relations(:public_used_relation).to_xml)
534 delete :delete, :id => current_relations(:public_used_relation).id
535 assert_response :precondition_failed,
536 "shouldn't be able to delete a relation used in a relation (#{@response.body})"
537 assert_equal "Precondition failed: The relation 5 is used in relation 6.", @response.body
539 # this should work when we provide the appropriate payload...
540 content(relations(:multi_tag_relation).to_xml)
541 delete :delete, :id => current_relations(:multi_tag_relation).id
542 assert_response :success
544 # valid delete should return the new version number, which should
545 # be greater than the old version number
546 assert @response.body.to_i > current_relations(:visible_relation).version,
547 "delete request should return a new version number for relation"
549 # this won't work since the relation is already deleted
550 content(relations(:invisible_relation).to_xml)
551 delete :delete, :id => current_relations(:invisible_relation).id
552 assert_response :gone
554 # Public visible relation needs to be deleted
555 content(relations(:public_visible_relation).to_xml)
556 delete :delete, :id => current_relations(:public_visible_relation).id
557 assert_response :success
559 # this works now because the relation which was using this one
561 content(relations(:public_used_relation).to_xml)
562 delete :delete, :id => current_relations(:public_used_relation).id
563 assert_response :success,
564 "should be able to delete a relation used in an old relation (#{@response.body})"
566 # this won't work since the relation never existed
567 delete :delete, :id => 0
568 assert_response :not_found
572 # when a relation's tag is modified then it should put the bounding
573 # box of all its members into the changeset.
574 def test_tag_modify_bounding_box
575 # in current fixtures, relation 5 contains nodes 3 and 5 (node 3
576 # indirectly via way 3), so the bbox should be [3,3,5,5].
577 check_changeset_modify(BoundingBox.new(3,3,5,5)) do |changeset_id|
578 # add a tag to an existing relation
579 relation_xml = current_relations(:visible_relation).to_xml
580 relation_element = relation_xml.find("//osm/relation").first
581 new_tag = XML::Node.new("tag")
582 new_tag['k'] = "some_new_tag"
583 new_tag['v'] = "some_new_value"
584 relation_element << new_tag
586 # update changeset ID to point to new changeset
587 update_changeset(relation_xml, changeset_id)
591 put :update, :id => current_relations(:visible_relation).id
592 assert_response :success, "can't update relation for tag/bbox test"
597 # add a member to a relation and check the bounding box is only that
599 def test_add_member_bounding_box
600 relation_id = current_relations(:visible_relation).id
602 [current_nodes(:used_node_1),
603 current_nodes(:used_node_2),
604 current_ways(:used_way),
605 current_ways(:way_with_versions)
606 ].each_with_index do |element, version|
607 bbox = element.bbox.to_unscaled
608 check_changeset_modify(bbox) do |changeset_id|
609 relation_xml = Relation.find(relation_id).to_xml
610 relation_element = relation_xml.find("//osm/relation").first
611 new_member = XML::Node.new("member")
612 new_member['ref'] = element.id.to_s
613 new_member['type'] = element.class.to_s.downcase
614 new_member['role'] = "some_role"
615 relation_element << new_member
617 # update changeset ID to point to new changeset
618 update_changeset(relation_xml, changeset_id)
622 put :update, :id => current_relations(:visible_relation).id
623 assert_response :success, "can't update relation for add #{element.class}/bbox test: #{@response.body}"
625 # get it back and check the ordering
626 get :read, :id => relation_id
627 assert_response :success, "can't read back the relation: #{@response.body}"
628 check_ordering(relation_xml, @response.body)
634 # remove a member from a relation and check the bounding box is
636 def test_remove_member_bounding_box
637 check_changeset_modify(BoundingBox.new(5,5,5,5)) do |changeset_id|
638 # remove node 5 (5,5) from an existing relation
639 relation_xml = current_relations(:visible_relation).to_xml
641 find("//osm/relation/member[@type='node'][@ref='5']").
644 # update changeset ID to point to new changeset
645 update_changeset(relation_xml, changeset_id)
649 put :update, :id => current_relations(:visible_relation).id
650 assert_response :success, "can't update relation for remove node/bbox test"
655 # check that relations are ordered
656 def test_relation_member_ordering
657 basic_authorization(users(:public_user).email, "test")
661 <relation changeset='4'>
662 <member ref='1' type='node' role='first'/>
663 <member ref='3' type='node' role='second'/>
664 <member ref='1' type='way' role='third'/>
665 <member ref='3' type='way' role='fourth'/>
669 doc = XML::Parser.string(doc_str).parse
673 assert_response :success, "can't create a relation: #{@response.body}"
674 relation_id = @response.body.to_i
676 # get it back and check the ordering
677 get :read, :id => relation_id
678 assert_response :success, "can't read back the relation: #{@response.body}"
679 check_ordering(doc, @response.body)
681 # insert a member at the front
682 new_member = XML::Node.new "member"
683 new_member['ref'] = 5.to_s
684 new_member['type'] = 'node'
685 new_member['role'] = 'new first'
686 doc.find("//osm/relation").first.child.prev = new_member
687 # update the version, should be 1?
688 doc.find("//osm/relation").first['id'] = relation_id.to_s
689 doc.find("//osm/relation").first['version'] = 1.to_s
691 # upload the next version of the relation
693 put :update, :id => relation_id
694 assert_response :success, "can't update relation: #{@response.body}"
695 new_version = @response.body.to_i
697 # get it back again and check the ordering again
698 get :read, :id => relation_id
699 assert_response :success, "can't read back the relation: #{@response.body}"
700 check_ordering(doc, @response.body)
702 # check the ordering in the history tables:
703 with_controller(OldRelationController.new) do
704 get :version, :id => relation_id, :version => 2
705 assert_response :success, "can't read back version 2 of the relation #{relation_id}"
706 check_ordering(doc, @response.body)
711 # check that relations can contain duplicate members
712 def test_relation_member_duplicates
715 <relation changeset='4'>
716 <member ref='1' type='node' role='forward'/>
717 <member ref='3' type='node' role='forward'/>
718 <member ref='1' type='node' role='forward'/>
719 <member ref='3' type='node' role='forward'/>
723 doc = XML::Parser.string(doc_str).parse
725 ## First try with the private user
726 basic_authorization(users(:normal_user).email, "test");
730 assert_response :forbidden
732 ## Now try with the public user
733 basic_authorization(users(:public_user).email, "test");
737 assert_response :success, "can't create a relation: #{@response.body}"
738 relation_id = @response.body.to_i
740 # get it back and check the ordering
741 get :read, :id => relation_id
742 assert_response :success, "can't read back the relation: #{relation_id}"
743 check_ordering(doc, @response.body)
747 # test that the ordering of elements in the history is the same as in current.
748 def test_history_ordering
751 <relation changeset='4'>
752 <member ref='1' type='node' role='forward'/>
753 <member ref='5' type='node' role='forward'/>
754 <member ref='4' type='node' role='forward'/>
755 <member ref='3' type='node' role='forward'/>
759 doc = XML::Parser.string(doc_str).parse
760 basic_authorization(users(:public_user).email, "test");
764 assert_response :success, "can't create a relation: #{@response.body}"
765 relation_id = @response.body.to_i
767 # check the ordering in the current tables:
768 get :read, :id => relation_id
769 assert_response :success, "can't read back the relation: #{@response.body}"
770 check_ordering(doc, @response.body)
772 # check the ordering in the history tables:
773 with_controller(OldRelationController.new) do
774 get :version, :id => relation_id, :version => 1
775 assert_response :success, "can't read back version 1 of the relation: #{@response.body}"
776 check_ordering(doc, @response.body)
781 # remove all the members from a relation. the result is pretty useless, but
782 # still technically valid.
783 def test_remove_all_members
784 check_changeset_modify(BoundingBox.new(3,3,5,5)) do |changeset_id|
785 relation_xml = current_relations(:visible_relation).to_xml
787 find("//osm/relation/member").
790 # update changeset ID to point to new changeset
791 update_changeset(relation_xml, changeset_id)
795 put :update, :id => current_relations(:visible_relation).id
796 assert_response :success, "can't update relation for remove all members test"
797 checkrelation = Relation.find(current_relations(:visible_relation).id)
798 assert_not_nil(checkrelation,
799 "uploaded relation not found in database after upload")
800 assert_equal(0, checkrelation.members.length,
801 "relation contains members but they should have all been deleted")
805 # ============================================================
807 # ============================================================
810 # checks that the XML document and the string arguments have
811 # members in the same order.
812 def check_ordering(doc, xml)
813 new_doc = XML::Parser.string(xml).parse
815 doc_members = doc.find("//osm/relation/member").collect do |m|
816 [m['ref'].to_i, m['type'].to_sym, m['role']]
819 new_members = new_doc.find("//osm/relation/member").collect do |m|
820 [m['ref'].to_i, m['type'].to_sym, m['role']]
823 doc_members.zip(new_members).each do |d, n|
824 assert_equal d, n, "members are not equal - ordering is wrong? (#{doc}, #{xml})"
829 # create a changeset and yield to the caller to set it up, then assert
830 # that the changeset bounding box is +bbox+.
831 def check_changeset_modify(bbox)
832 ## First test with the private user to check that you get a forbidden
833 basic_authorization(users(:normal_user).email, "test");
835 # create a new changeset for this operation, so we are assured
836 # that the bounding box will be newly-generated.
837 changeset_id = with_controller(ChangesetController.new) do
838 content "<osm><changeset/></osm>"
840 assert_response :forbidden, "shouldn't be able to create changeset for modify test, as should get forbidden"
844 ## Now do the whole thing with the public user
845 basic_authorization(users(:public_user).email, "test")
847 # create a new changeset for this operation, so we are assured
848 # that the bounding box will be newly-generated.
849 changeset_id = with_controller(ChangesetController.new) do
850 content "<osm><changeset/></osm>"
852 assert_response :success, "couldn't create changeset for modify test"
856 # go back to the block to do the actual modifies
859 # now download the changeset to check its bounding box
860 with_controller(ChangesetController.new) do
861 get :read, :id => changeset_id
862 assert_response :success, "can't re-read changeset for modify test"
863 assert_select "osm>changeset", 1, "Changeset element doesn't exist in #{@response.body}"
864 assert_select "osm>changeset[id='#{changeset_id}']", 1, "Changeset id=#{changeset_id} doesn't exist in #{@response.body}"
865 assert_select "osm>changeset[min_lon='#{bbox.min_lon}']", 1, "Changeset min_lon wrong in #{@response.body}"
866 assert_select "osm>changeset[min_lat='#{bbox.min_lat}']", 1, "Changeset min_lat wrong in #{@response.body}"
867 assert_select "osm>changeset[max_lon='#{bbox.max_lon}']", 1, "Changeset max_lon wrong in #{@response.body}"
868 assert_select "osm>changeset[max_lat='#{bbox.max_lat}']", 1, "Changeset max_lat wrong in #{@response.body}"
873 # yields the relation with the given +id+ (and optional +version+
874 # to read from the history tables) into the block. the parsed XML
876 def with_relation(id, ver = nil)
880 with_controller(OldRelationController.new) do
881 get :version, :id => id, :version => ver
884 assert_response :success
885 yield xml_parse(@response.body)
889 # updates the relation (XML) +rel+ and
890 # yields the new version of that relation into the block.
891 # the parsed XML doc is retured.
893 rel_id = rel.find("//osm/relation").first["id"].to_i
895 put :update, :id => rel_id
896 assert_response :success, "can't update relation: #{@response.body}"
897 version = @response.body.to_i
899 # now get the new version
900 get :read, :id => rel_id
901 assert_response :success
902 new_rel = xml_parse(@response.body)
910 # updates the relation (XML) +rel+ via the diff-upload API and
911 # yields the new version of that relation into the block.
912 # the parsed XML doc is retured.
913 def with_update_diff(rel)
914 rel_id = rel.find("//osm/relation").first["id"].to_i
915 cs_id = rel.find("//osm/relation").first['changeset'].to_i
918 with_controller(ChangesetController.new) do
919 doc = OSM::API.new.get_xml_doc
920 change = XML::Node.new 'osmChange'
922 modify = XML::Node.new 'modify'
924 modify << doc.import(rel.find("//osm/relation").first)
927 post :upload, :id => cs_id
928 assert_response :success, "can't upload diff relation: #{@response.body}"
929 version = xml_parse(@response.body).find("//diffResult/relation").first["new_version"].to_i
932 # now get the new version
933 get :read, :id => rel_id
934 assert_response :success
935 new_rel = xml_parse(@response.body)
943 # returns a k->v hash of tags from an xml doc
944 def get_tags_as_hash(a)
945 a.find("//osm/relation/tag").sort_by { |v| v['k'] }.inject({}) do |h,v|
952 # assert that all tags on relation documents +a+ and +b+
954 def assert_tags_equal(a, b)
955 # turn the XML doc into tags hashes
956 a_tags = get_tags_as_hash(a)
957 b_tags = get_tags_as_hash(b)
959 assert_equal a_tags.keys, b_tags.keys, "Tag keys should be identical."
960 a_tags.each do |k, v|
961 assert_equal v, b_tags[k],
962 "Tags which were not altered should be the same. " +
963 "#{a_tags.inspect} != #{b_tags.inspect}"
968 # update the changeset_id of a node element
969 def update_changeset(xml, changeset_id)
970 xml_attr_rewrite(xml, 'changeset', changeset_id)
974 # update an attribute in the node element
975 def xml_attr_rewrite(xml, name, value)
976 xml.find("//osm/relation").first[name] = value.to_s
983 parser = XML::Parser.string(xml)