2 require "relation_controller"
4 class RelationControllerTest < ActionController::TestCase
6 # test all routes which lead to this controller
9 { :path => "/api/0.6/relation/create", :method => :put },
10 { :controller => "relation", :action => "create" }
13 { :path => "/api/0.6/relation/1/full", :method => :get },
14 { :controller => "relation", :action => "full", :id => "1" }
17 { :path => "/api/0.6/relation/1", :method => :get },
18 { :controller => "relation", :action => "read", :id => "1" }
21 { :path => "/api/0.6/relation/1", :method => :put },
22 { :controller => "relation", :action => "update", :id => "1" }
25 { :path => "/api/0.6/relation/1", :method => :delete },
26 { :controller => "relation", :action => "delete", :id => "1" }
29 { :path => "/api/0.6/relations", :method => :get },
30 { :controller => "relation", :action => "relations" }
34 { :path => "/api/0.6/node/1/relations", :method => :get },
35 { :controller => "relation", :action => "relations_for_node", :id => "1" }
38 { :path => "/api/0.6/way/1/relations", :method => :get },
39 { :controller => "relation", :action => "relations_for_way", :id => "1" }
42 { :path => "/api/0.6/relation/1/relations", :method => :get },
43 { :controller => "relation", :action => "relations_for_relation", :id => "1" }
47 # -------------------------------------
48 # Test reading relations.
49 # -------------------------------------
52 # check that a visible relation is returned properly
53 get :read, :params => { :id => create(:relation).id }
54 assert_response :success
56 # check that an invisible relation is not returned
57 get :read, :params => { :id => create(:relation, :deleted).id }
60 # check chat a non-existent relation is not returned
61 get :read, :params => { :id => 0 }
62 assert_response :not_found
66 # check that all relations containing a particular node, and no extra
67 # relations, are returned from the relations_for_node call.
68 def test_relations_for_node
70 # should include relations with that node as a member
71 relation_with_node = create(:relation_member, :member => node).relation
72 # should ignore relations without that node as a member
73 _relation_without_node = create(:relation_member).relation
74 # should ignore relations with the node involved indirectly, via a way
75 way = create(:way_node, :node => node).way
76 _relation_with_way = create(:relation_member, :member => way).relation
77 # should ignore relations with the node involved indirectly, via a relation
78 second_relation = create(:relation_member, :member => node).relation
79 _super_relation = create(:relation_member, :member => second_relation).relation
80 # should combine multiple relation_member references into just one relation entry
81 create(:relation_member, :member => node, :relation => relation_with_node, :sequence_id => 2)
82 # should not include deleted relations
83 deleted_relation = create(:relation, :deleted)
84 create(:relation_member, :member => node, :relation => deleted_relation)
86 check_relations_for_element(:relations_for_node, "node",
88 [relation_with_node, second_relation])
91 def test_relations_for_way
93 # should include relations with that way as a member
94 relation_with_way = create(:relation_member, :member => way).relation
95 # should ignore relations without that way as a member
96 _relation_without_way = create(:relation_member).relation
97 # should ignore relations with the way involved indirectly, via a relation
98 second_relation = create(:relation_member, :member => way).relation
99 _super_relation = create(:relation_member, :member => second_relation).relation
100 # should combine multiple relation_member references into just one relation entry
101 create(:relation_member, :member => way, :relation => relation_with_way, :sequence_id => 2)
102 # should not include deleted relations
103 deleted_relation = create(:relation, :deleted)
104 create(:relation_member, :member => way, :relation => deleted_relation)
106 check_relations_for_element(:relations_for_way, "way",
108 [relation_with_way, second_relation])
111 def test_relations_for_relation
112 relation = create(:relation)
113 # should include relations with that relation as a member
114 relation_with_relation = create(:relation_member, :member => relation).relation
115 # should ignore any relation without that relation as a member
116 _relation_without_relation = create(:relation_member).relation
117 # should ignore relations with the relation involved indirectly, via a relation
118 second_relation = create(:relation_member, :member => relation).relation
119 _super_relation = create(:relation_member, :member => second_relation).relation
120 # should combine multiple relation_member references into just one relation entry
121 create(:relation_member, :member => relation, :relation => relation_with_relation, :sequence_id => 2)
122 # should not include deleted relations
123 deleted_relation = create(:relation, :deleted)
124 create(:relation_member, :member => relation, :relation => deleted_relation)
125 check_relations_for_element(:relations_for_relation, "relation",
127 [relation_with_relation, second_relation])
130 def check_relations_for_element(method, type, id, expected_relations)
131 # check the "relations for relation" mode
132 get method, :params => { :id => id }
133 assert_response :success
135 # count one osm element
136 assert_select "osm[version='#{API_VERSION}'][generator='OpenStreetMap server']", 1
138 # we should have only the expected number of relations
139 assert_select "osm>relation", expected_relations.size
141 # and each of them should contain the element we originally searched for
142 expected_relations.each do |relation|
143 # The relation should appear once, but the element could appear multiple times
144 assert_select "osm>relation[id='#{relation.id}']", 1
145 assert_select "osm>relation[id='#{relation.id}']>member[type='#{type}'][ref='#{id}']"
150 # check the "full" mode
151 get :full, :params => { :id => 999999 }
152 assert_response :not_found
154 get :full, :params => { :id => create(:relation, :deleted).id }
155 assert_response :gone
157 get :full, :params => { :id => create(:relation).id }
158 assert_response :success
159 # FIXME: check whether this contains the stuff we want!
163 # test fetching multiple relations
165 relation1 = create(:relation)
166 relation2 = create(:relation, :deleted)
167 relation3 = create(:relation, :with_history, :version => 2)
168 relation4 = create(:relation, :with_history, :version => 2)
169 relation4.old_relations.find_by(:version => 1).redact!(create(:redaction))
171 # check error when no parameter provided
173 assert_response :bad_request
175 # check error when no parameter value provided
176 get :relations, :params => { :relations => "" }
177 assert_response :bad_request
179 # test a working call
180 get :relations, :params => { :relations => "#{relation1.id},#{relation2.id},#{relation3.id},#{relation4.id}" }
181 assert_response :success
182 assert_select "osm" do
183 assert_select "relation", :count => 4
184 assert_select "relation[id='#{relation1.id}'][visible='true']", :count => 1
185 assert_select "relation[id='#{relation2.id}'][visible='false']", :count => 1
186 assert_select "relation[id='#{relation3.id}'][visible='true']", :count => 1
187 assert_select "relation[id='#{relation4.id}'][visible='true']", :count => 1
190 # check error when a non-existent relation is included
191 get :relations, :params => { :relations => "#{relation1.id},#{relation2.id},#{relation3.id},#{relation4.id},400" }
192 assert_response :not_found
195 # -------------------------------------
196 # Test simple relation creation.
197 # -------------------------------------
200 private_user = create(:user, :data_public => false)
201 private_changeset = create(:changeset, :user => private_user)
203 changeset = create(:changeset, :user => user)
205 way = create(:way_with_nodes, :nodes_count => 2)
207 basic_authorization private_user.email, "test"
209 # create an relation without members
210 content "<osm><relation changeset='#{private_changeset.id}'><tag k='test' v='yes' /></relation></osm>"
212 # hope for forbidden, due to user
213 assert_response :forbidden,
214 "relation upload should have failed with forbidden"
217 # create an relation with a node as member
218 # This time try with a role attribute in the relation
219 content "<osm><relation changeset='#{private_changeset.id}'>" \
220 "<member ref='#{node.id}' type='node' role='some'/>" \
221 "<tag k='test' v='yes' /></relation></osm>"
223 # hope for forbidden due to user
224 assert_response :forbidden,
225 "relation upload did not return forbidden status"
228 # create an relation with a node as member, this time test that we don't
229 # need a role attribute to be included
230 content "<osm><relation changeset='#{private_changeset.id}'>" \
231 "<member ref='#{node.id}' type='node'/>" + "<tag k='test' v='yes' /></relation></osm>"
233 # hope for forbidden due to user
234 assert_response :forbidden,
235 "relation upload did not return forbidden status"
238 # create an relation with a way and a node as members
239 content "<osm><relation changeset='#{private_changeset.id}'>" \
240 "<member type='node' ref='#{node.id}' role='some'/>" \
241 "<member type='way' ref='#{way.id}' role='other'/>" \
242 "<tag k='test' v='yes' /></relation></osm>"
244 # hope for forbidden, due to user
245 assert_response :forbidden,
246 "relation upload did not return success status"
248 ## Now try with the public user
249 basic_authorization user.email, "test"
251 # create an relation without members
252 content "<osm><relation changeset='#{changeset.id}'><tag k='test' v='yes' /></relation></osm>"
255 assert_response :success,
256 "relation upload did not return success status"
257 # read id of created relation and search for it
258 relationid = @response.body
259 checkrelation = Relation.find(relationid)
260 assert_not_nil checkrelation,
261 "uploaded relation not found in data base after upload"
263 assert_equal checkrelation.members.length, 0,
264 "saved relation contains members but should not"
265 assert_equal checkrelation.tags.length, 1,
266 "saved relation does not contain exactly one tag"
267 assert_equal changeset.id, checkrelation.changeset.id,
268 "saved relation does not belong in the changeset it was assigned to"
269 assert_equal user.id, checkrelation.changeset.user_id,
270 "saved relation does not belong to user that created it"
271 assert_equal true, checkrelation.visible,
272 "saved relation is not visible"
273 # ok the relation is there but can we also retrieve it?
274 get :read, :params => { :id => relationid }
275 assert_response :success
278 # create an relation with a node as member
279 # This time try with a role attribute in the relation
280 content "<osm><relation changeset='#{changeset.id}'>" \
281 "<member ref='#{node.id}' 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 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, :params => { :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 content "<osm><relation changeset='#{changeset.id}'>" \
312 "<member ref='#{node.id}' type='node'/>" + "<tag k='test' v='yes' /></relation></osm>"
315 assert_response :success,
316 "relation upload did not return success status"
317 # read id of created relation and search for it
318 relationid = @response.body
319 checkrelation = Relation.find(relationid)
320 assert_not_nil checkrelation,
321 "uploaded relation not found in data base after upload"
323 assert_equal checkrelation.members.length, 1,
324 "saved relation does not contain exactly one member"
325 assert_equal checkrelation.tags.length, 1,
326 "saved relation does not contain exactly one tag"
327 assert_equal changeset.id, checkrelation.changeset.id,
328 "saved relation does not belong in the changeset it was assigned to"
329 assert_equal user.id, checkrelation.changeset.user_id,
330 "saved relation does not belong to user that created it"
331 assert_equal true, checkrelation.visible,
332 "saved relation is not visible"
333 # ok the relation is there but can we also retrieve it?
335 get :read, :params => { :id => relationid }
336 assert_response :success
339 # create an relation with a way and a node as members
340 content "<osm><relation changeset='#{changeset.id}'>" \
341 "<member type='node' ref='#{node.id}' role='some'/>" \
342 "<member type='way' ref='#{way.id}' role='other'/>" \
343 "<tag k='test' v='yes' /></relation></osm>"
346 assert_response :success,
347 "relation upload did not return success status"
348 # read id of created relation and search for it
349 relationid = @response.body
350 checkrelation = Relation.find(relationid)
351 assert_not_nil checkrelation,
352 "uploaded relation not found in data base after upload"
354 assert_equal checkrelation.members.length, 2,
355 "saved relation does not have exactly two members"
356 assert_equal checkrelation.tags.length, 1,
357 "saved relation does not contain exactly one tag"
358 assert_equal changeset.id, checkrelation.changeset.id,
359 "saved relation does not belong in the changeset it was assigned to"
360 assert_equal user.id, checkrelation.changeset.user_id,
361 "saved relation does not belong to user that created it"
362 assert_equal true, checkrelation.visible,
363 "saved relation is not visible"
364 # ok the relation is there but can we also retrieve it?
365 get :read, :params => { :id => relationid }
366 assert_response :success
369 # ------------------------------------
370 # Test updating relations
371 # ------------------------------------
374 # test that, when tags are updated on a relation, the correct things
375 # happen to the correct tables and the API gives sensible results.
376 # this is to test a case that gregory marler noticed and posted to
378 ## FIXME Move this to an integration test
379 def test_update_relation_tags
381 changeset = create(:changeset, :user => user)
382 relation = create(:relation)
383 create_list(:relation_tag, 4, :relation => relation)
385 basic_authorization user.email, "test"
387 with_relation(relation.id) do |rel|
388 # alter one of the tags
389 tag = rel.find("//osm/relation/tag").first
390 tag["v"] = "some changed value"
391 update_changeset(rel, changeset.id)
393 # check that the downloaded tags are the same as the uploaded tags...
394 new_version = with_update(rel) do |new_rel|
395 assert_tags_equal rel, new_rel
398 # check the original one in the current_* table again
399 with_relation(relation.id) { |r| assert_tags_equal rel, r }
401 # now check the version in the history
402 with_relation(relation.id, new_version) { |r| assert_tags_equal rel, r }
407 # test that, when tags are updated on a relation when using the diff
408 # upload function, the correct things happen to the correct tables
409 # and the API gives sensible results. this is to test a case that
410 # gregory marler noticed and posted to josm-dev.
411 def test_update_relation_tags_via_upload
413 changeset = create(:changeset, :user => user)
414 relation = create(:relation)
415 create_list(:relation_tag, 4, :relation => relation)
417 basic_authorization user.email, "test"
419 with_relation(relation.id) do |rel|
420 # alter one of the tags
421 tag = rel.find("//osm/relation/tag").first
422 tag["v"] = "some changed value"
423 update_changeset(rel, changeset.id)
425 # check that the downloaded tags are the same as the uploaded tags...
426 new_version = with_update_diff(rel) do |new_rel|
427 assert_tags_equal rel, new_rel
430 # check the original one in the current_* table again
431 with_relation(relation.id) { |r| assert_tags_equal rel, r }
433 # now check the version in the history
434 with_relation(relation.id, new_version) { |r| assert_tags_equal rel, r }
438 def test_update_wrong_id
440 changeset = create(:changeset, :user => user)
441 relation = create(:relation)
442 other_relation = create(:relation)
444 basic_authorization user.email, "test"
445 with_relation(relation.id) do |rel|
446 update_changeset(rel, changeset.id)
448 put :update, :params => { :id => other_relation.id }
449 assert_response :bad_request
453 # -------------------------------------
454 # Test creating some invalid relations.
455 # -------------------------------------
457 def test_create_invalid
459 changeset = create(:changeset, :user => user)
461 basic_authorization user.email, "test"
463 # create a relation with non-existing node as member
464 content "<osm><relation changeset='#{changeset.id}'>" \
465 "<member type='node' ref='0'/><tag k='test' v='yes' />" \
469 assert_response :precondition_failed,
470 "relation upload with invalid node did not return 'precondition failed'"
471 assert_equal "Precondition failed: Relation with id cannot be saved due to Node with id 0", @response.body
474 # -------------------------------------
475 # Test creating a relation, with some invalid XML
476 # -------------------------------------
477 def test_create_invalid_xml
479 changeset = create(:changeset, :user => user)
482 basic_authorization user.email, "test"
484 # create some xml that should return an error
485 content "<osm><relation changeset='#{changeset.id}'>" \
486 "<member type='type' ref='#{node.id}' role=''/>" \
487 "<tag k='tester' v='yep'/></relation></osm>"
490 assert_response :bad_request
491 assert_match(/Cannot parse valid relation from xml string/, @response.body)
492 assert_match(/The type is not allowed only, /, @response.body)
495 # -------------------------------------
496 # Test deleting relations.
497 # -------------------------------------
500 private_user = create(:user, :data_public => false)
501 private_user_closed_changeset = create(:changeset, :closed, :user => private_user)
503 closed_changeset = create(:changeset, :closed, :user => user)
504 changeset = create(:changeset, :user => user)
505 relation = create(:relation)
506 used_relation = create(:relation)
507 super_relation = create(:relation_member, :member => used_relation).relation
508 deleted_relation = create(:relation, :deleted)
509 multi_tag_relation = create(:relation)
510 create_list(:relation_tag, 4, :relation => multi_tag_relation)
512 ## First try to delete relation without auth
513 delete :delete, :params => { :id => relation.id }
514 assert_response :unauthorized
516 ## Then try with the private user, to make sure that you get a forbidden
517 basic_authorization private_user.email, "test"
519 # this shouldn't work, as we should need the payload...
520 delete :delete, :params => { :id => relation.id }
521 assert_response :forbidden
523 # try to delete without specifying a changeset
524 content "<osm><relation id='#{relation.id}'/></osm>"
525 delete :delete, :params => { :id => relation.id }
526 assert_response :forbidden
528 # try to delete with an invalid (closed) changeset
529 content update_changeset(relation.to_xml,
530 private_user_closed_changeset.id)
531 delete :delete, :params => { :id => relation.id }
532 assert_response :forbidden
534 # try to delete with an invalid (non-existent) changeset
535 content update_changeset(relation.to_xml, 0)
536 delete :delete, :params => { :id => relation.id }
537 assert_response :forbidden
539 # this won't work because the relation is in-use by another relation
540 content(used_relation.to_xml)
541 delete :delete, :params => { :id => used_relation.id }
542 assert_response :forbidden
544 # this should work when we provide the appropriate payload...
545 content(relation.to_xml)
546 delete :delete, :params => { :id => relation.id }
547 assert_response :forbidden
549 # this won't work since the relation is already deleted
550 content(deleted_relation.to_xml)
551 delete :delete, :params => { :id => deleted_relation.id }
552 assert_response :forbidden
554 # this won't work since the relation never existed
555 delete :delete, :params => { :id => 0 }
556 assert_response :forbidden
558 ## now set auth for the public user
559 basic_authorization user.email, "test"
561 # this shouldn't work, as we should need the payload...
562 delete :delete, :params => { :id => relation.id }
563 assert_response :bad_request
565 # try to delete without specifying a changeset
566 content "<osm><relation id='#{relation.id}' version='#{relation.version}' /></osm>"
567 delete :delete, :params => { :id => relation.id }
568 assert_response :bad_request
569 assert_match(/Changeset id is missing/, @response.body)
571 # try to delete with an invalid (closed) changeset
572 content update_changeset(relation.to_xml,
574 delete :delete, :params => { :id => relation.id }
575 assert_response :conflict
577 # try to delete with an invalid (non-existent) changeset
578 content update_changeset(relation.to_xml, 0)
579 delete :delete, :params => { :id => relation.id }
580 assert_response :conflict
582 # this won't work because the relation is in a changeset owned by someone else
583 content update_changeset(relation.to_xml, create(:changeset).id)
584 delete :delete, :params => { :id => relation.id }
585 assert_response :conflict,
586 "shouldn't be able to delete a relation in a changeset owned by someone else (#{@response.body})"
588 # this won't work because the relation in the payload is different to that passed
589 content update_changeset(relation.to_xml, changeset.id)
590 delete :delete, :params => { :id => create(:relation).id }
591 assert_response :bad_request, "shouldn't be able to delete a relation when payload is different to the url"
593 # this won't work because the relation is in-use by another relation
594 content update_changeset(used_relation.to_xml, changeset.id)
595 delete :delete, :params => { :id => used_relation.id }
596 assert_response :precondition_failed,
597 "shouldn't be able to delete a relation used in a relation (#{@response.body})"
598 assert_equal "Precondition failed: The relation #{used_relation.id} is used in relation #{super_relation.id}.", @response.body
600 # this should work when we provide the appropriate payload...
601 content update_changeset(multi_tag_relation.to_xml, changeset.id)
602 delete :delete, :params => { :id => multi_tag_relation.id }
603 assert_response :success
605 # valid delete should return the new version number, which should
606 # be greater than the old version number
607 assert @response.body.to_i > multi_tag_relation.version,
608 "delete request should return a new version number for relation"
610 # this won't work since the relation is already deleted
611 content update_changeset(deleted_relation.to_xml, changeset.id)
612 delete :delete, :params => { :id => deleted_relation.id }
613 assert_response :gone
615 # Public visible relation needs to be deleted
616 content update_changeset(super_relation.to_xml, changeset.id)
617 delete :delete, :params => { :id => super_relation.id }
618 assert_response :success
620 # this works now because the relation which was using this one
622 content update_changeset(used_relation.to_xml, changeset.id)
623 delete :delete, :params => { :id => used_relation.id }
624 assert_response :success,
625 "should be able to delete a relation used in an old relation (#{@response.body})"
627 # this won't work since the relation never existed
628 delete :delete, :params => { :id => 0 }
629 assert_response :not_found
633 # when a relation's tag is modified then it should put the bounding
634 # box of all its members into the changeset.
635 def test_tag_modify_bounding_box
636 relation = create(:relation)
637 node1 = create(:node, :lat => 3, :lon => 3)
638 node2 = create(:node, :lat => 5, :lon => 5)
640 create(:way_node, :way => way, :node => node1)
641 create(:relation_member, :relation => relation, :member => way)
642 create(:relation_member, :relation => relation, :member => node2)
643 # the relation contains nodes1 and node2 (node1
644 # indirectly via the way), so the bbox should be [3,3,5,5].
645 check_changeset_modify(BoundingBox.new(3, 3, 5, 5)) do |changeset_id|
646 # add a tag to an existing relation
647 relation_xml = relation.to_xml
648 relation_element = relation_xml.find("//osm/relation").first
649 new_tag = XML::Node.new("tag")
650 new_tag["k"] = "some_new_tag"
651 new_tag["v"] = "some_new_value"
652 relation_element << new_tag
654 # update changeset ID to point to new changeset
655 update_changeset(relation_xml, changeset_id)
659 put :update, :params => { :id => relation.id }
660 assert_response :success, "can't update relation for tag/bbox test"
665 # add a member to a relation and check the bounding box is only that
667 def test_add_member_bounding_box
668 relation = create(:relation)
669 node1 = create(:node, :lat => 4, :lon => 4)
670 node2 = create(:node, :lat => 7, :lon => 7)
672 create(:way_node, :way => way1, :node => create(:node, :lat => 8, :lon => 8))
674 create(:way_node, :way => way2, :node => create(:node, :lat => 9, :lon => 9), :sequence_id => 1)
675 create(:way_node, :way => way2, :node => create(:node, :lat => 10, :lon => 10), :sequence_id => 2)
677 [node1, node2, way1, way2].each do |element|
678 bbox = element.bbox.to_unscaled
679 check_changeset_modify(bbox) do |changeset_id|
680 relation_xml = Relation.find(relation.id).to_xml
681 relation_element = relation_xml.find("//osm/relation").first
682 new_member = XML::Node.new("member")
683 new_member["ref"] = element.id.to_s
684 new_member["type"] = element.class.to_s.downcase
685 new_member["role"] = "some_role"
686 relation_element << new_member
688 # update changeset ID to point to new changeset
689 update_changeset(relation_xml, changeset_id)
693 put :update, :params => { :id => relation.id }
694 assert_response :success, "can't update relation for add #{element.class}/bbox test: #{@response.body}"
696 # get it back and check the ordering
697 get :read, :params => { :id => relation.id }
698 assert_response :success, "can't read back the relation: #{@response.body}"
699 check_ordering(relation_xml, @response.body)
705 # remove a member from a relation and check the bounding box is
707 def test_remove_member_bounding_box
708 relation = create(:relation)
709 node1 = create(:node, :lat => 3, :lon => 3)
710 node2 = create(:node, :lat => 5, :lon => 5)
711 create(:relation_member, :relation => relation, :member => node1)
712 create(:relation_member, :relation => relation, :member => node2)
714 check_changeset_modify(BoundingBox.new(5, 5, 5, 5)) do |changeset_id|
715 # remove node 5 (5,5) from an existing relation
716 relation_xml = relation.to_xml
718 .find("//osm/relation/member[@type='node'][@ref='#{node2.id}']")
721 # update changeset ID to point to new changeset
722 update_changeset(relation_xml, changeset_id)
726 put :update, :params => { :id => relation.id }
727 assert_response :success, "can't update relation for remove node/bbox test"
732 # check that relations are ordered
733 def test_relation_member_ordering
735 changeset = create(:changeset, :user => user)
736 node1 = create(:node)
737 node2 = create(:node)
738 node3 = create(:node)
739 way1 = create(:way_with_nodes, :nodes_count => 2)
740 way2 = create(:way_with_nodes, :nodes_count => 2)
742 basic_authorization user.email, "test"
744 doc_str = <<OSM.strip_heredoc
746 <relation changeset='#{changeset.id}'>
747 <member ref='#{node1.id}' type='node' role='first'/>
748 <member ref='#{node2.id}' type='node' role='second'/>
749 <member ref='#{way1.id}' type='way' role='third'/>
750 <member ref='#{way2.id}' type='way' role='fourth'/>
754 doc = XML::Parser.string(doc_str).parse
758 assert_response :success, "can't create a relation: #{@response.body}"
759 relation_id = @response.body.to_i
761 # get it back and check the ordering
762 get :read, :params => { :id => relation_id }
763 assert_response :success, "can't read back the relation: #{@response.body}"
764 check_ordering(doc, @response.body)
766 # insert a member at the front
767 new_member = XML::Node.new "member"
768 new_member["ref"] = node3.id.to_s
769 new_member["type"] = "node"
770 new_member["role"] = "new first"
771 doc.find("//osm/relation").first.child.prev = new_member
772 # update the version, should be 1?
773 doc.find("//osm/relation").first["id"] = relation_id.to_s
774 doc.find("//osm/relation").first["version"] = 1.to_s
776 # upload the next version of the relation
778 put :update, :params => { :id => relation_id }
779 assert_response :success, "can't update relation: #{@response.body}"
780 assert_equal 2, @response.body.to_i
782 # get it back again and check the ordering again
783 get :read, :params => { :id => relation_id }
784 assert_response :success, "can't read back the relation: #{@response.body}"
785 check_ordering(doc, @response.body)
787 # check the ordering in the history tables:
788 with_controller(OldRelationController.new) do
789 get :version, :params => { :id => relation_id, :version => 2 }
790 assert_response :success, "can't read back version 2 of the relation #{relation_id}"
791 check_ordering(doc, @response.body)
796 # check that relations can contain duplicate members
797 def test_relation_member_duplicates
798 private_user = create(:user, :data_public => false)
800 changeset = create(:changeset, :user => user)
801 node1 = create(:node)
802 node2 = create(:node)
804 doc_str = <<OSM.strip_heredoc
806 <relation changeset='#{changeset.id}'>
807 <member ref='#{node1.id}' type='node' role='forward'/>
808 <member ref='#{node2.id}' type='node' role='forward'/>
809 <member ref='#{node1.id}' type='node' role='forward'/>
810 <member ref='#{node2.id}' type='node' role='forward'/>
814 doc = XML::Parser.string(doc_str).parse
816 ## First try with the private user
817 basic_authorization private_user.email, "test"
821 assert_response :forbidden
823 ## Now try with the public user
824 basic_authorization user.email, "test"
828 assert_response :success, "can't create a relation: #{@response.body}"
829 relation_id = @response.body.to_i
831 # get it back and check the ordering
832 get :read, :params => { :id => relation_id }
833 assert_response :success, "can't read back the relation: #{relation_id}"
834 check_ordering(doc, @response.body)
838 # test that the ordering of elements in the history is the same as in current.
839 def test_history_ordering
841 changeset = create(:changeset, :user => user)
842 node1 = create(:node)
843 node2 = create(:node)
844 node3 = create(:node)
845 node4 = create(:node)
847 doc_str = <<OSM.strip_heredoc
849 <relation changeset='#{changeset.id}'>
850 <member ref='#{node1.id}' type='node' role='forward'/>
851 <member ref='#{node4.id}' type='node' role='forward'/>
852 <member ref='#{node3.id}' type='node' role='forward'/>
853 <member ref='#{node2.id}' type='node' role='forward'/>
857 doc = XML::Parser.string(doc_str).parse
858 basic_authorization user.email, "test"
862 assert_response :success, "can't create a relation: #{@response.body}"
863 relation_id = @response.body.to_i
865 # check the ordering in the current tables:
866 get :read, :params => { :id => relation_id }
867 assert_response :success, "can't read back the relation: #{@response.body}"
868 check_ordering(doc, @response.body)
870 # check the ordering in the history tables:
871 with_controller(OldRelationController.new) do
872 get :version, :params => { :id => relation_id, :version => 1 }
873 assert_response :success, "can't read back version 1 of the relation: #{@response.body}"
874 check_ordering(doc, @response.body)
879 # remove all the members from a relation. the result is pretty useless, but
880 # still technically valid.
881 def test_remove_all_members
882 relation = create(:relation)
883 node1 = create(:node, :lat => 3, :lon => 3)
884 node2 = create(:node, :lat => 5, :lon => 5)
886 create(:way_node, :way => way, :node => node1)
887 create(:relation_member, :relation => relation, :member => way)
888 create(:relation_member, :relation => relation, :member => node2)
890 check_changeset_modify(BoundingBox.new(3, 3, 5, 5)) do |changeset_id|
891 relation_xml = relation.to_xml
893 .find("//osm/relation/member")
896 # update changeset ID to point to new changeset
897 update_changeset(relation_xml, changeset_id)
901 put :update, :params => { :id => relation.id }
902 assert_response :success, "can't update relation for remove all members test"
903 checkrelation = Relation.find(relation.id)
904 assert_not_nil(checkrelation,
905 "uploaded relation not found in database after upload")
906 assert_equal(0, checkrelation.members.length,
907 "relation contains members but they should have all been deleted")
911 # ============================================================
913 # ============================================================
916 # checks that the XML document and the string arguments have
917 # members in the same order.
918 def check_ordering(doc, xml)
919 new_doc = XML::Parser.string(xml).parse
921 doc_members = doc.find("//osm/relation/member").collect do |m|
922 [m["ref"].to_i, m["type"].to_sym, m["role"]]
925 new_members = new_doc.find("//osm/relation/member").collect do |m|
926 [m["ref"].to_i, m["type"].to_sym, m["role"]]
929 doc_members.zip(new_members).each do |d, n|
930 assert_equal d, n, "members are not equal - ordering is wrong? (#{doc}, #{xml})"
935 # create a changeset and yield to the caller to set it up, then assert
936 # that the changeset bounding box is +bbox+.
937 def check_changeset_modify(bbox)
938 ## First test with the private user to check that you get a forbidden
939 basic_authorization create(:user, :data_public => false).email, "test"
941 # create a new changeset for this operation, so we are assured
942 # that the bounding box will be newly-generated.
943 changeset_id = with_controller(ChangesetController.new) do
944 content "<osm><changeset/></osm>"
946 assert_response :forbidden, "shouldn't be able to create changeset for modify test, as should get forbidden"
949 ## Now do the whole thing with the public user
950 basic_authorization create(:user).email, "test"
952 # create a new changeset for this operation, so we are assured
953 # that the bounding box will be newly-generated.
954 changeset_id = with_controller(ChangesetController.new) do
955 content "<osm><changeset/></osm>"
957 assert_response :success, "couldn't create changeset for modify test"
961 # go back to the block to do the actual modifies
964 # now download the changeset to check its bounding box
965 with_controller(ChangesetController.new) do
966 get :read, :params => { :id => changeset_id }
967 assert_response :success, "can't re-read changeset for modify test"
968 assert_select "osm>changeset", 1, "Changeset element doesn't exist in #{@response.body}"
969 assert_select "osm>changeset[id='#{changeset_id}']", 1, "Changeset id=#{changeset_id} doesn't exist in #{@response.body}"
970 assert_select "osm>changeset[min_lon='#{format('%.7f', bbox.min_lon)}']", 1, "Changeset min_lon wrong in #{@response.body}"
971 assert_select "osm>changeset[min_lat='#{format('%.7f', bbox.min_lat)}']", 1, "Changeset min_lat wrong in #{@response.body}"
972 assert_select "osm>changeset[max_lon='#{format('%.7f', bbox.max_lon)}']", 1, "Changeset max_lon wrong in #{@response.body}"
973 assert_select "osm>changeset[max_lat='#{format('%.7f', bbox.max_lat)}']", 1, "Changeset max_lat wrong in #{@response.body}"
978 # yields the relation with the given +id+ (and optional +version+
979 # to read from the history tables) into the block. the parsed XML
981 def with_relation(id, ver = nil)
983 get :read, :params => { :id => id }
985 with_controller(OldRelationController.new) do
986 get :version, :params => { :id => id, :version => ver }
989 assert_response :success
990 yield xml_parse(@response.body)
994 # updates the relation (XML) +rel+ and
995 # yields the new version of that relation into the block.
996 # the parsed XML doc is retured.
998 rel_id = rel.find("//osm/relation").first["id"].to_i
1000 put :update, :params => { :id => rel_id }
1001 assert_response :success, "can't update relation: #{@response.body}"
1002 version = @response.body.to_i
1004 # now get the new version
1005 get :read, :params => { :id => rel_id }
1006 assert_response :success
1007 new_rel = xml_parse(@response.body)
1015 # updates the relation (XML) +rel+ via the diff-upload API and
1016 # yields the new version of that relation into the block.
1017 # the parsed XML doc is retured.
1018 def with_update_diff(rel)
1019 rel_id = rel.find("//osm/relation").first["id"].to_i
1020 cs_id = rel.find("//osm/relation").first["changeset"].to_i
1023 with_controller(ChangesetController.new) do
1024 doc = OSM::API.new.get_xml_doc
1025 change = XML::Node.new "osmChange"
1027 modify = XML::Node.new "modify"
1029 modify << doc.import(rel.find("//osm/relation").first)
1032 post :upload, :params => { :id => cs_id }
1033 assert_response :success, "can't upload diff relation: #{@response.body}"
1034 version = xml_parse(@response.body).find("//diffResult/relation").first["new_version"].to_i
1037 # now get the new version
1038 get :read, :params => { :id => rel_id }
1039 assert_response :success
1040 new_rel = xml_parse(@response.body)
1048 # returns a k->v hash of tags from an xml doc
1049 def get_tags_as_hash(a)
1050 a.find("//osm/relation/tag").sort_by { |v| v["k"] }.each_with_object({}) do |v, h|
1056 # assert that all tags on relation documents +a+ and +b+
1058 def assert_tags_equal(a, b)
1059 # turn the XML doc into tags hashes
1060 a_tags = get_tags_as_hash(a)
1061 b_tags = get_tags_as_hash(b)
1063 assert_equal a_tags.keys, b_tags.keys, "Tag keys should be identical."
1064 a_tags.each do |k, v|
1065 assert_equal v, b_tags[k],
1066 "Tags which were not altered should be the same. " \
1067 "#{a_tags.inspect} != #{b_tags.inspect}"
1072 # update the changeset_id of a node element
1073 def update_changeset(xml, changeset_id)
1074 xml_attr_rewrite(xml, "changeset", changeset_id)
1078 # update an attribute in the node element
1079 def xml_attr_rewrite(xml, name, value)
1080 xml.find("//osm/relation").first[name] = value.to_s
1087 parser = XML::Parser.string(xml)