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 basic_authorization users(:normal_user).email, "test"
198 # put the relation in a dummy fixture changset
199 changeset_id = changesets(:normal_user_first_change).id
201 # create an relation without members
202 content "<osm><relation changeset='#{changeset_id}'><tag k='test' v='yes' /></relation></osm>"
204 # hope for forbidden, due to user
205 assert_response :forbidden,
206 "relation upload should have failed with forbidden"
209 # create an relation with a node as member
210 # This time try with a role attribute in the relation
211 nid = current_nodes(:used_node_1).id
212 content "<osm><relation changeset='#{changeset_id}'>" +
213 "<member ref='#{nid}' type='node' role='some'/>" +
214 "<tag k='test' v='yes' /></relation></osm>"
216 # hope for forbidden due to user
217 assert_response :forbidden,
218 "relation upload did not return forbidden status"
221 # create an relation with a node as member, this time test that we don't
222 # need a role attribute to be included
223 nid = current_nodes(:used_node_1).id
224 content "<osm><relation changeset='#{changeset_id}'>" +
225 "<member ref='#{nid}' type='node'/>" + "<tag k='test' v='yes' /></relation></osm>"
227 # hope for forbidden due to user
228 assert_response :forbidden,
229 "relation upload did not return forbidden status"
232 # create an relation with a way and a node as members
233 nid = current_nodes(:used_node_1).id
234 wid = current_ways(:used_way).id
235 content "<osm><relation changeset='#{changeset_id}'>" +
236 "<member type='node' ref='#{nid}' role='some'/>" +
237 "<member type='way' ref='#{wid}' 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 users(:public_user).email, "test"
247 # put the relation in a dummy fixture changset
248 changeset_id = changesets(:public_user_first_change).id
250 # create an relation without members
251 content "<osm><relation changeset='#{changeset_id}'><tag k='test' v='yes' /></relation></osm>"
254 assert_response :success,
255 "relation upload did not return success status"
256 # read id of created relation and search for it
257 relationid = @response.body
258 checkrelation = Relation.find(relationid)
259 assert_not_nil checkrelation,
260 "uploaded relation not found in data base after upload"
262 assert_equal checkrelation.members.length, 0,
263 "saved relation contains members but should not"
264 assert_equal checkrelation.tags.length, 1,
265 "saved relation does not contain exactly one tag"
266 assert_equal changeset_id, checkrelation.changeset.id,
267 "saved relation does not belong in the changeset it was assigned to"
268 assert_equal users(:public_user).id, checkrelation.changeset.user_id,
269 "saved relation does not belong to user that created it"
270 assert_equal true, checkrelation.visible,
271 "saved relation is not visible"
272 # ok the relation is there but can we also retrieve it?
273 get :read, :id => relationid
274 assert_response :success
277 # create an relation with a node as member
278 # This time try with a role attribute in the relation
279 nid = current_nodes(:used_node_1).id
280 content "<osm><relation changeset='#{changeset_id}'>" +
281 "<member ref='#{nid}' type='node' role='some'/>" +
282 "<tag k='test' v='yes' /></relation></osm>"
285 assert_response :success,
286 "relation upload did not return success status"
287 # read id of created relation and search for it
288 relationid = @response.body
289 checkrelation = Relation.find(relationid)
290 assert_not_nil checkrelation,
291 "uploaded relation not found in data base after upload"
293 assert_equal checkrelation.members.length, 1,
294 "saved relation does not contain exactly one member"
295 assert_equal checkrelation.tags.length, 1,
296 "saved relation does not contain exactly one tag"
297 assert_equal changeset_id, checkrelation.changeset.id,
298 "saved relation does not belong in the changeset it was assigned to"
299 assert_equal users(:public_user).id, checkrelation.changeset.user_id,
300 "saved relation does not belong to user that created it"
301 assert_equal true, checkrelation.visible,
302 "saved relation is not visible"
303 # ok the relation is there but can we also retrieve it?
305 get :read, :id => relationid
306 assert_response :success
309 # create an relation with a node as member, this time test that we don't
310 # need a role attribute to be included
311 nid = current_nodes(:used_node_1).id
312 content "<osm><relation changeset='#{changeset_id}'>" +
313 "<member ref='#{nid}' type='node'/>" + "<tag k='test' v='yes' /></relation></osm>"
316 assert_response :success,
317 "relation upload did not return success status"
318 # read id of created relation and search for it
319 relationid = @response.body
320 checkrelation = Relation.find(relationid)
321 assert_not_nil checkrelation,
322 "uploaded relation not found in data base after upload"
324 assert_equal checkrelation.members.length, 1,
325 "saved relation does not contain exactly one member"
326 assert_equal checkrelation.tags.length, 1,
327 "saved relation does not contain exactly one tag"
328 assert_equal changeset_id, checkrelation.changeset.id,
329 "saved relation does not belong in the changeset it was assigned to"
330 assert_equal users(:public_user).id, checkrelation.changeset.user_id,
331 "saved relation does not belong to user that created it"
332 assert_equal true, checkrelation.visible,
333 "saved relation is not visible"
334 # ok the relation is there but can we also retrieve it?
336 get :read, :id => relationid
337 assert_response :success
340 # create an relation with a way and a node as members
341 nid = current_nodes(:used_node_1).id
342 wid = current_ways(:used_way).id
343 content "<osm><relation changeset='#{changeset_id}'>" +
344 "<member type='node' ref='#{nid}' role='some'/>" +
345 "<member type='way' ref='#{wid}' role='other'/>" +
346 "<tag k='test' v='yes' /></relation></osm>"
349 assert_response :success,
350 "relation upload did not return success status"
351 # read id of created relation and search for it
352 relationid = @response.body
353 checkrelation = Relation.find(relationid)
354 assert_not_nil checkrelation,
355 "uploaded relation not found in data base after upload"
357 assert_equal checkrelation.members.length, 2,
358 "saved relation does not have exactly two members"
359 assert_equal checkrelation.tags.length, 1,
360 "saved relation does not contain exactly one tag"
361 assert_equal changeset_id, checkrelation.changeset.id,
362 "saved relation does not belong in the changeset it was assigned to"
363 assert_equal users(:public_user).id, checkrelation.changeset.user_id,
364 "saved relation does not belong to user that created it"
365 assert_equal true, checkrelation.visible,
366 "saved relation is not visible"
367 # ok the relation is there but can we also retrieve it?
368 get :read, :id => relationid
369 assert_response :success
372 # ------------------------------------
373 # Test updating relations
374 # ------------------------------------
377 # test that, when tags are updated on a relation, the correct things
378 # happen to the correct tables and the API gives sensible results.
379 # this is to test a case that gregory marler noticed and posted to
381 ## FIXME Move this to an integration test
382 def test_update_relation_tags
383 basic_authorization "test@example.com", "test"
384 rel_id = current_relations(:multi_tag_relation).id
385 create_list(:relation_tag, 4, :relation => current_relations(:multi_tag_relation))
386 cs_id = changesets(:public_user_first_change).id
388 with_relation(rel_id) do |rel|
389 # alter one of the tags
390 tag = rel.find("//osm/relation/tag").first
391 tag["v"] = "some changed value"
392 update_changeset(rel, cs_id)
394 # check that the downloaded tags are the same as the uploaded tags...
395 new_version = with_update(rel) do |new_rel|
396 assert_tags_equal rel, new_rel
399 # check the original one in the current_* table again
400 with_relation(rel_id) { |r| assert_tags_equal rel, r }
402 # now check the version in the history
403 with_relation(rel_id, new_version) { |r| assert_tags_equal rel, r }
408 # test that, when tags are updated on a relation when using the diff
409 # upload function, the correct things happen to the correct tables
410 # and the API gives sensible results. this is to test a case that
411 # gregory marler noticed and posted to josm-dev.
412 def test_update_relation_tags_via_upload
413 basic_authorization users(:public_user).email, "test"
414 rel_id = current_relations(:multi_tag_relation).id
415 create_list(:relation_tag, 4, :relation => current_relations(:multi_tag_relation))
416 cs_id = changesets(:public_user_first_change).id
418 with_relation(rel_id) do |rel|
419 # alter one of the tags
420 tag = rel.find("//osm/relation/tag").first
421 tag["v"] = "some changed value"
422 update_changeset(rel, cs_id)
424 # check that the downloaded tags are the same as the uploaded tags...
425 new_version = with_update_diff(rel) do |new_rel|
426 assert_tags_equal rel, new_rel
429 # check the original one in the current_* table again
430 with_relation(rel_id) { |r| assert_tags_equal rel, r }
432 # now check the version in the history
433 with_relation(rel_id, new_version) { |r| assert_tags_equal rel, r }
437 def test_update_wrong_id
438 basic_authorization users(:public_user).email, "test"
439 rel_id = current_relations(:multi_tag_relation).id
440 cs_id = changesets(:public_user_first_change).id
442 with_relation(rel_id) do |rel|
443 update_changeset(rel, cs_id)
445 put :update, :id => current_relations(:visible_relation).id
446 assert_response :bad_request
450 # -------------------------------------
451 # Test creating some invalid relations.
452 # -------------------------------------
454 def test_create_invalid
455 basic_authorization users(:public_user).email, "test"
457 # put the relation in a dummy fixture changset
458 changeset_id = changesets(:public_user_first_change).id
460 # create a relation with non-existing node as member
461 content "<osm><relation changeset='#{changeset_id}'>" +
462 "<member type='node' ref='0'/><tag k='test' v='yes' />" +
466 assert_response :precondition_failed,
467 "relation upload with invalid node did not return 'precondition failed'"
468 assert_equal "Precondition failed: Relation with id cannot be saved due to Node with id 0", @response.body
471 # -------------------------------------
472 # Test creating a relation, with some invalid XML
473 # -------------------------------------
474 def test_create_invalid_xml
475 basic_authorization users(:public_user).email, "test"
477 # put the relation in a dummy fixture changeset that works
478 changeset_id = changesets(:public_user_first_change).id
480 # create some xml that should return an error
481 content "<osm><relation changeset='#{changeset_id}'>" +
482 "<member type='type' ref='#{current_nodes(:used_node_1).id}' role=''/>" +
483 "<tag k='tester' v='yep'/></relation></osm>"
486 assert_response :bad_request
487 assert_match(/Cannot parse valid relation from xml string/, @response.body)
488 assert_match(/The type is not allowed only, /, @response.body)
491 # -------------------------------------
492 # Test deleting relations.
493 # -------------------------------------
496 ## First try to delete relation without auth
497 delete :delete, :id => current_relations(:visible_relation).id
498 assert_response :unauthorized
500 ## Then try with the private user, to make sure that you get a forbidden
501 basic_authorization(users(:normal_user).email, "test")
503 # this shouldn't work, as we should need the payload...
504 delete :delete, :id => current_relations(:visible_relation).id
505 assert_response :forbidden
507 # try to delete without specifying a changeset
508 content "<osm><relation id='#{current_relations(:visible_relation).id}'/></osm>"
509 delete :delete, :id => current_relations(:visible_relation).id
510 assert_response :forbidden
512 # try to delete with an invalid (closed) changeset
513 content update_changeset(current_relations(:visible_relation).to_xml,
514 changesets(:normal_user_closed_change).id)
515 delete :delete, :id => current_relations(:visible_relation).id
516 assert_response :forbidden
518 # try to delete with an invalid (non-existent) changeset
519 content update_changeset(current_relations(:visible_relation).to_xml, 0)
520 delete :delete, :id => current_relations(:visible_relation).id
521 assert_response :forbidden
523 # this won't work because the relation is in-use by another relation
524 content(relations(:used_relation).to_xml)
525 delete :delete, :id => current_relations(:used_relation).id
526 assert_response :forbidden
528 # this should work when we provide the appropriate payload...
529 content(relations(:visible_relation).to_xml)
530 delete :delete, :id => current_relations(:visible_relation).id
531 assert_response :forbidden
533 # this won't work since the relation is already deleted
534 content(relations(:invisible_relation).to_xml)
535 delete :delete, :id => current_relations(:invisible_relation).id
536 assert_response :forbidden
538 # this works now because the relation which was using this one
540 content(relations(:used_relation).to_xml)
541 delete :delete, :id => current_relations(:used_relation).id
542 assert_response :forbidden
544 # this won't work since the relation never existed
545 delete :delete, :id => 0
546 assert_response :forbidden
548 ## now set auth for the public user
549 basic_authorization(users(:public_user).email, "test")
551 # this shouldn't work, as we should need the payload...
552 delete :delete, :id => current_relations(:visible_relation).id
553 assert_response :bad_request
555 # try to delete without specifying a changeset
556 content "<osm><relation id='#{current_relations(:visible_relation).id}' version='#{current_relations(:visible_relation).version}' /></osm>"
557 delete :delete, :id => current_relations(:visible_relation).id
558 assert_response :bad_request
559 assert_match(/Changeset id is missing/, @response.body)
561 # try to delete with an invalid (closed) changeset
562 content update_changeset(current_relations(:visible_relation).to_xml,
563 changesets(:normal_user_closed_change).id)
564 delete :delete, :id => current_relations(:visible_relation).id
565 assert_response :conflict
567 # try to delete with an invalid (non-existent) changeset
568 content update_changeset(current_relations(:visible_relation).to_xml, 0)
569 delete :delete, :id => current_relations(:visible_relation).id
570 assert_response :conflict
572 # this won't work because the relation is in a changeset owned by someone else
573 content(relations(:used_relation).to_xml)
574 delete :delete, :id => current_relations(:used_relation).id
575 assert_response :conflict,
576 "shouldn't be able to delete a relation in a changeset owned by someone else (#{@response.body})"
578 # this won't work because the relation in the payload is different to that passed
579 content(relations(:public_used_relation).to_xml)
580 delete :delete, :id => current_relations(:used_relation).id
581 assert_not_equal relations(:public_used_relation).id, current_relations(:used_relation).id
582 assert_response :bad_request, "shouldn't be able to delete a relation when payload is different to the url"
584 # this won't work because the relation is in-use by another relation
585 content(relations(:public_used_relation).to_xml)
586 delete :delete, :id => current_relations(:public_used_relation).id
587 assert_response :precondition_failed,
588 "shouldn't be able to delete a relation used in a relation (#{@response.body})"
589 assert_equal "Precondition failed: The relation 5 is used in relation 6.", @response.body
591 # this should work when we provide the appropriate payload...
592 content(relations(:multi_tag_relation).to_xml)
593 delete :delete, :id => current_relations(:multi_tag_relation).id
594 assert_response :success
596 # valid delete should return the new version number, which should
597 # be greater than the old version number
598 assert @response.body.to_i > current_relations(:visible_relation).version,
599 "delete request should return a new version number for relation"
601 # this won't work since the relation is already deleted
602 content(relations(:invisible_relation).to_xml)
603 delete :delete, :id => current_relations(:invisible_relation).id
604 assert_response :gone
606 # Public visible relation needs to be deleted
607 content(relations(:public_visible_relation).to_xml)
608 delete :delete, :id => current_relations(:public_visible_relation).id
609 assert_response :success
611 # this works now because the relation which was using this one
613 content(relations(:public_used_relation).to_xml)
614 delete :delete, :id => current_relations(:public_used_relation).id
615 assert_response :success,
616 "should be able to delete a relation used in an old relation (#{@response.body})"
618 # this won't work since the relation never existed
619 delete :delete, :id => 0
620 assert_response :not_found
624 # when a relation's tag is modified then it should put the bounding
625 # box of all its members into the changeset.
626 def test_tag_modify_bounding_box
627 # in current fixtures, relation 5 contains nodes 3 and 5 (node 3
628 # indirectly via way 3), so the bbox should be [3,3,5,5].
629 check_changeset_modify(BoundingBox.new(3, 3, 5, 5)) do |changeset_id|
630 # add a tag to an existing relation
631 relation_xml = current_relations(:visible_relation).to_xml
632 relation_element = relation_xml.find("//osm/relation").first
633 new_tag = XML::Node.new("tag")
634 new_tag["k"] = "some_new_tag"
635 new_tag["v"] = "some_new_value"
636 relation_element << new_tag
638 # update changeset ID to point to new changeset
639 update_changeset(relation_xml, changeset_id)
643 put :update, :id => current_relations(:visible_relation).id
644 assert_response :success, "can't update relation for tag/bbox test"
649 # add a member to a relation and check the bounding box is only that
651 def test_add_member_bounding_box
652 relation_id = current_relations(:visible_relation).id
654 [current_nodes(:used_node_1),
655 current_nodes(:used_node_2),
656 current_ways(:used_way),
657 current_ways(:way_with_versions)].each_with_index do |element, _version|
658 bbox = element.bbox.to_unscaled
659 check_changeset_modify(bbox) do |changeset_id|
660 relation_xml = Relation.find(relation_id).to_xml
661 relation_element = relation_xml.find("//osm/relation").first
662 new_member = XML::Node.new("member")
663 new_member["ref"] = element.id.to_s
664 new_member["type"] = element.class.to_s.downcase
665 new_member["role"] = "some_role"
666 relation_element << new_member
668 # update changeset ID to point to new changeset
669 update_changeset(relation_xml, changeset_id)
673 put :update, :id => current_relations(:visible_relation).id
674 assert_response :success, "can't update relation for add #{element.class}/bbox test: #{@response.body}"
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(relation_xml, @response.body)
685 # remove a member from a relation and check the bounding box is
687 def test_remove_member_bounding_box
688 check_changeset_modify(BoundingBox.new(5, 5, 5, 5)) do |changeset_id|
689 # remove node 5 (5,5) from an existing relation
690 relation_xml = current_relations(:visible_relation).to_xml
692 .find("//osm/relation/member[@type='node'][@ref='5']")
695 # update changeset ID to point to new changeset
696 update_changeset(relation_xml, changeset_id)
700 put :update, :id => current_relations(:visible_relation).id
701 assert_response :success, "can't update relation for remove node/bbox test"
706 # check that relations are ordered
707 def test_relation_member_ordering
708 basic_authorization(users(:public_user).email, "test")
712 <relation changeset='4'>
713 <member ref='1' type='node' role='first'/>
714 <member ref='3' type='node' role='second'/>
715 <member ref='1' type='way' role='third'/>
716 <member ref='3' type='way' role='fourth'/>
720 doc = XML::Parser.string(doc_str).parse
724 assert_response :success, "can't create a relation: #{@response.body}"
725 relation_id = @response.body.to_i
727 # get it back and check the ordering
728 get :read, :id => relation_id
729 assert_response :success, "can't read back the relation: #{@response.body}"
730 check_ordering(doc, @response.body)
732 # insert a member at the front
733 new_member = XML::Node.new "member"
734 new_member["ref"] = 5.to_s
735 new_member["type"] = "node"
736 new_member["role"] = "new first"
737 doc.find("//osm/relation").first.child.prev = new_member
738 # update the version, should be 1?
739 doc.find("//osm/relation").first["id"] = relation_id.to_s
740 doc.find("//osm/relation").first["version"] = 1.to_s
742 # upload the next version of the relation
744 put :update, :id => relation_id
745 assert_response :success, "can't update relation: #{@response.body}"
746 assert_equal 2, @response.body.to_i
748 # get it back again and check the ordering again
749 get :read, :id => relation_id
750 assert_response :success, "can't read back the relation: #{@response.body}"
751 check_ordering(doc, @response.body)
753 # check the ordering in the history tables:
754 with_controller(OldRelationController.new) do
755 get :version, :id => relation_id, :version => 2
756 assert_response :success, "can't read back version 2 of the relation #{relation_id}"
757 check_ordering(doc, @response.body)
762 # check that relations can contain duplicate members
763 def test_relation_member_duplicates
766 <relation changeset='4'>
767 <member ref='1' type='node' role='forward'/>
768 <member ref='3' type='node' role='forward'/>
769 <member ref='1' type='node' role='forward'/>
770 <member ref='3' type='node' role='forward'/>
774 doc = XML::Parser.string(doc_str).parse
776 ## First try with the private user
777 basic_authorization(users(:normal_user).email, "test")
781 assert_response :forbidden
783 ## Now try with the public user
784 basic_authorization(users(:public_user).email, "test")
788 assert_response :success, "can't create a relation: #{@response.body}"
789 relation_id = @response.body.to_i
791 # get it back and check the ordering
792 get :read, :id => relation_id
793 assert_response :success, "can't read back the relation: #{relation_id}"
794 check_ordering(doc, @response.body)
798 # test that the ordering of elements in the history is the same as in current.
799 def test_history_ordering
802 <relation changeset='4'>
803 <member ref='1' type='node' role='forward'/>
804 <member ref='5' type='node' role='forward'/>
805 <member ref='4' type='node' role='forward'/>
806 <member ref='3' type='node' role='forward'/>
810 doc = XML::Parser.string(doc_str).parse
811 basic_authorization(users(:public_user).email, "test")
815 assert_response :success, "can't create a relation: #{@response.body}"
816 relation_id = @response.body.to_i
818 # check the ordering in the current tables:
819 get :read, :id => relation_id
820 assert_response :success, "can't read back the relation: #{@response.body}"
821 check_ordering(doc, @response.body)
823 # check the ordering in the history tables:
824 with_controller(OldRelationController.new) do
825 get :version, :id => relation_id, :version => 1
826 assert_response :success, "can't read back version 1 of the relation: #{@response.body}"
827 check_ordering(doc, @response.body)
832 # remove all the members from a relation. the result is pretty useless, but
833 # still technically valid.
834 def test_remove_all_members
835 check_changeset_modify(BoundingBox.new(3, 3, 5, 5)) do |changeset_id|
836 relation_xml = current_relations(:visible_relation).to_xml
838 .find("//osm/relation/member")
841 # update changeset ID to point to new changeset
842 update_changeset(relation_xml, changeset_id)
846 put :update, :id => current_relations(:visible_relation).id
847 assert_response :success, "can't update relation for remove all members test"
848 checkrelation = Relation.find(current_relations(:visible_relation).id)
849 assert_not_nil(checkrelation,
850 "uploaded relation not found in database after upload")
851 assert_equal(0, checkrelation.members.length,
852 "relation contains members but they should have all been deleted")
856 # ============================================================
858 # ============================================================
861 # checks that the XML document and the string arguments have
862 # members in the same order.
863 def check_ordering(doc, xml)
864 new_doc = XML::Parser.string(xml).parse
866 doc_members = doc.find("//osm/relation/member").collect do |m|
867 [m["ref"].to_i, m["type"].to_sym, m["role"]]
870 new_members = new_doc.find("//osm/relation/member").collect do |m|
871 [m["ref"].to_i, m["type"].to_sym, m["role"]]
874 doc_members.zip(new_members).each do |d, n|
875 assert_equal d, n, "members are not equal - ordering is wrong? (#{doc}, #{xml})"
880 # create a changeset and yield to the caller to set it up, then assert
881 # that the changeset bounding box is +bbox+.
882 def check_changeset_modify(bbox)
883 ## First test with the private user to check that you get a forbidden
884 basic_authorization(users(:normal_user).email, "test")
886 # create a new changeset for this operation, so we are assured
887 # that the bounding box will be newly-generated.
888 changeset_id = with_controller(ChangesetController.new) do
889 content "<osm><changeset/></osm>"
891 assert_response :forbidden, "shouldn't be able to create changeset for modify test, as should get forbidden"
894 ## Now do the whole thing with the public user
895 basic_authorization(users(:public_user).email, "test")
897 # create a new changeset for this operation, so we are assured
898 # that the bounding box will be newly-generated.
899 changeset_id = with_controller(ChangesetController.new) do
900 content "<osm><changeset/></osm>"
902 assert_response :success, "couldn't create changeset for modify test"
906 # go back to the block to do the actual modifies
909 # now download the changeset to check its bounding box
910 with_controller(ChangesetController.new) do
911 get :read, :id => changeset_id
912 assert_response :success, "can't re-read changeset for modify test"
913 assert_select "osm>changeset", 1, "Changeset element doesn't exist in #{@response.body}"
914 assert_select "osm>changeset[id='#{changeset_id}']", 1, "Changeset id=#{changeset_id} doesn't exist in #{@response.body}"
915 assert_select "osm>changeset[min_lon='#{bbox.min_lon}']", 1, "Changeset min_lon wrong in #{@response.body}"
916 assert_select "osm>changeset[min_lat='#{bbox.min_lat}']", 1, "Changeset min_lat wrong in #{@response.body}"
917 assert_select "osm>changeset[max_lon='#{bbox.max_lon}']", 1, "Changeset max_lon wrong in #{@response.body}"
918 assert_select "osm>changeset[max_lat='#{bbox.max_lat}']", 1, "Changeset max_lat wrong in #{@response.body}"
923 # yields the relation with the given +id+ (and optional +version+
924 # to read from the history tables) into the block. the parsed XML
926 def with_relation(id, ver = nil)
930 with_controller(OldRelationController.new) do
931 get :version, :id => id, :version => ver
934 assert_response :success
935 yield xml_parse(@response.body)
939 # updates the relation (XML) +rel+ and
940 # yields the new version of that relation into the block.
941 # the parsed XML doc is retured.
943 rel_id = rel.find("//osm/relation").first["id"].to_i
945 put :update, :id => rel_id
946 assert_response :success, "can't update relation: #{@response.body}"
947 version = @response.body.to_i
949 # now get the new version
950 get :read, :id => rel_id
951 assert_response :success
952 new_rel = xml_parse(@response.body)
960 # updates the relation (XML) +rel+ via the diff-upload API and
961 # yields the new version of that relation into the block.
962 # the parsed XML doc is retured.
963 def with_update_diff(rel)
964 rel_id = rel.find("//osm/relation").first["id"].to_i
965 cs_id = rel.find("//osm/relation").first["changeset"].to_i
968 with_controller(ChangesetController.new) do
969 doc = OSM::API.new.get_xml_doc
970 change = XML::Node.new "osmChange"
972 modify = XML::Node.new "modify"
974 modify << doc.import(rel.find("//osm/relation").first)
977 post :upload, :id => cs_id
978 assert_response :success, "can't upload diff relation: #{@response.body}"
979 version = xml_parse(@response.body).find("//diffResult/relation").first["new_version"].to_i
982 # now get the new version
983 get :read, :id => rel_id
984 assert_response :success
985 new_rel = xml_parse(@response.body)
993 # returns a k->v hash of tags from an xml doc
994 def get_tags_as_hash(a)
995 a.find("//osm/relation/tag").sort_by { |v| v["k"] }.each_with_object({}) do |v, h|
1001 # assert that all tags on relation documents +a+ and +b+
1003 def assert_tags_equal(a, b)
1004 # turn the XML doc into tags hashes
1005 a_tags = get_tags_as_hash(a)
1006 b_tags = get_tags_as_hash(b)
1008 assert_equal a_tags.keys, b_tags.keys, "Tag keys should be identical."
1009 a_tags.each do |k, v|
1010 assert_equal v, b_tags[k],
1011 "Tags which were not altered should be the same. " +
1012 "#{a_tags.inspect} != #{b_tags.inspect}"
1017 # update the changeset_id of a node element
1018 def update_changeset(xml, changeset_id)
1019 xml_attr_rewrite(xml, "changeset", changeset_id)
1023 # update an attribute in the node element
1024 def xml_attr_rewrite(xml, name, value)
1025 xml.find("//osm/relation").first[name] = value.to_s
1032 parser = XML::Parser.string(xml)