4 class RelationsControllerTest < ActionController::TestCase
6 # test all routes which lead to this controller
9 { :path => "/api/0.6/relation/create", :method => :put },
10 { :controller => "api/relations", :action => "create" }
13 { :path => "/api/0.6/relation/1/full", :method => :get },
14 { :controller => "api/relations", :action => "full", :id => "1" }
17 { :path => "/api/0.6/relation/1", :method => :get },
18 { :controller => "api/relations", :action => "show", :id => "1" }
21 { :path => "/api/0.6/relation/1", :method => :put },
22 { :controller => "api/relations", :action => "update", :id => "1" }
25 { :path => "/api/0.6/relation/1", :method => :delete },
26 { :controller => "api/relations", :action => "delete", :id => "1" }
29 { :path => "/api/0.6/relations", :method => :get },
30 { :controller => "api/relations", :action => "index" }
34 { :path => "/api/0.6/node/1/relations", :method => :get },
35 { :controller => "api/relations", :action => "relations_for_node", :id => "1" }
38 { :path => "/api/0.6/way/1/relations", :method => :get },
39 { :controller => "api/relations", :action => "relations_for_way", :id => "1" }
42 { :path => "/api/0.6/relation/1/relations", :method => :get },
43 { :controller => "api/relations", :action => "relations_for_relation", :id => "1" }
47 # -------------------------------------
48 # Test showing relations.
49 # -------------------------------------
52 # check that a visible relation is returned properly
53 get :show, :params => { :id => create(:relation).id }
54 assert_response :success
56 # check that an invisible relation is not returned
57 get :show, :params => { :id => create(:relation, :deleted).id }
60 # check chat a non-existent relation is not returned
61 get :show, :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='#{Settings.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 :index, :params => { :relations => "" }
177 assert_response :bad_request
179 # test a working call
180 get :index, :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 :index, :params => { :relations => "#{relation1.id},#{relation2.id},#{relation3.id},#{relation4.id},0" }
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 xml = "<osm><relation changeset='#{private_changeset.id}'><tag k='test' v='yes' /></relation></osm>"
211 put :create, :body => xml
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 xml = "<osm><relation changeset='#{private_changeset.id}'>" \
220 "<member ref='#{node.id}' type='node' role='some'/>" \
221 "<tag k='test' v='yes' /></relation></osm>"
222 put :create, :body => xml
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 xml = "<osm><relation changeset='#{private_changeset.id}'>" \
231 "<member ref='#{node.id}' type='node'/>" + "<tag k='test' v='yes' /></relation></osm>"
232 put :create, :body => xml
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 xml = "<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>"
243 put :create, :body => xml
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 xml = "<osm><relation changeset='#{changeset.id}'><tag k='test' v='yes' /></relation></osm>"
253 put :create, :body => xml
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 :show, :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 xml = "<osm><relation changeset='#{changeset.id}'>" \
281 "<member ref='#{node.id}' type='node' role='some'/>" \
282 "<tag k='test' v='yes' /></relation></osm>"
283 put :create, :body => xml
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 :show, :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 xml = "<osm><relation changeset='#{changeset.id}'>" \
312 "<member ref='#{node.id}' type='node'/>" + "<tag k='test' v='yes' /></relation></osm>"
313 put :create, :body => xml
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 :show, :params => { :id => relationid }
336 assert_response :success
339 # create an relation with a way and a node as members
340 xml = "<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>"
344 put :create, :body => xml
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 :show, :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)
447 put :update, :params => { :id => other_relation.id }, :body => rel.to_s
448 assert_response :bad_request
452 # -------------------------------------
453 # Test creating some invalid relations.
454 # -------------------------------------
456 def test_create_invalid
458 changeset = create(:changeset, :user => user)
460 basic_authorization user.email, "test"
462 # create a relation with non-existing node as member
463 xml = "<osm><relation changeset='#{changeset.id}'>" \
464 "<member type='node' ref='0'/><tag k='test' v='yes' />" \
466 put :create, :body => xml
468 assert_response :precondition_failed,
469 "relation upload with invalid node did not return 'precondition failed'"
470 assert_equal "Precondition failed: Relation with id cannot be saved due to Node with id 0", @response.body
473 # -------------------------------------
474 # Test creating a relation, with some invalid XML
475 # -------------------------------------
476 def test_create_invalid_xml
478 changeset = create(:changeset, :user => user)
481 basic_authorization user.email, "test"
483 # create some xml that should return an error
484 xml = "<osm><relation changeset='#{changeset.id}'>" \
485 "<member type='type' ref='#{node.id}' role=''/>" \
486 "<tag k='tester' v='yep'/></relation></osm>"
487 put :create, :body => xml
489 assert_response :bad_request
490 assert_match(/Cannot parse valid relation from xml string/, @response.body)
491 assert_match(/The type is not allowed only, /, @response.body)
494 # -------------------------------------
495 # Test deleting relations.
496 # -------------------------------------
499 private_user = create(:user, :data_public => false)
500 private_user_closed_changeset = create(:changeset, :closed, :user => private_user)
502 closed_changeset = create(:changeset, :closed, :user => user)
503 changeset = create(:changeset, :user => user)
504 relation = create(:relation)
505 used_relation = create(:relation)
506 super_relation = create(:relation_member, :member => used_relation).relation
507 deleted_relation = create(:relation, :deleted)
508 multi_tag_relation = create(:relation)
509 create_list(:relation_tag, 4, :relation => multi_tag_relation)
511 ## First try to delete relation without auth
512 delete :delete, :params => { :id => relation.id }
513 assert_response :unauthorized
515 ## Then try with the private user, to make sure that you get a forbidden
516 basic_authorization private_user.email, "test"
518 # this shouldn't work, as we should need the payload...
519 delete :delete, :params => { :id => relation.id }
520 assert_response :forbidden
522 # try to delete without specifying a changeset
523 xml = "<osm><relation id='#{relation.id}'/></osm>"
524 delete :delete, :params => { :id => relation.id }, :body => xml.to_s
525 assert_response :forbidden
527 # try to delete with an invalid (closed) changeset
528 xml = update_changeset(xml_for_relation(relation),
529 private_user_closed_changeset.id)
530 delete :delete, :params => { :id => relation.id }, :body => xml.to_s
531 assert_response :forbidden
533 # try to delete with an invalid (non-existent) changeset
534 xml = update_changeset(xml_for_relation(relation), 0)
535 delete :delete, :params => { :id => relation.id }, :body => xml.to_s
536 assert_response :forbidden
538 # this won't work because the relation is in-use by another relation
539 xml = xml_for_relation(used_relation)
540 delete :delete, :params => { :id => used_relation.id }, :body => xml.to_s
541 assert_response :forbidden
543 # this should work when we provide the appropriate payload...
544 xml = xml_for_relation(relation)
545 delete :delete, :params => { :id => relation.id }, :body => xml.to_s
546 assert_response :forbidden
548 # this won't work since the relation is already deleted
549 xml = xml_for_relation(deleted_relation)
550 delete :delete, :params => { :id => deleted_relation.id }, :body => xml.to_s
551 assert_response :forbidden
553 # this won't work since the relation never existed
554 delete :delete, :params => { :id => 0 }
555 assert_response :forbidden
557 ## now set auth for the public user
558 basic_authorization user.email, "test"
560 # this shouldn't work, as we should need the payload...
561 delete :delete, :params => { :id => relation.id }
562 assert_response :bad_request
564 # try to delete without specifying a changeset
565 xml = "<osm><relation id='#{relation.id}' version='#{relation.version}' /></osm>"
566 delete :delete, :params => { :id => relation.id }, :body => xml.to_s
567 assert_response :bad_request
568 assert_match(/Changeset id is missing/, @response.body)
570 # try to delete with an invalid (closed) changeset
571 xml = update_changeset(xml_for_relation(relation),
573 delete :delete, :params => { :id => relation.id }, :body => xml.to_s
574 assert_response :conflict
576 # try to delete with an invalid (non-existent) changeset
577 xml = update_changeset(xml_for_relation(relation), 0)
578 delete :delete, :params => { :id => relation.id }, :body => xml.to_s
579 assert_response :conflict
581 # this won't work because the relation is in a changeset owned by someone else
582 xml = update_changeset(xml_for_relation(relation), create(:changeset).id)
583 delete :delete, :params => { :id => relation.id }, :body => xml.to_s
584 assert_response :conflict,
585 "shouldn't be able to delete a relation in a changeset owned by someone else (#{@response.body})"
587 # this won't work because the relation in the payload is different to that passed
588 xml = update_changeset(xml_for_relation(relation), changeset.id)
589 delete :delete, :params => { :id => create(:relation).id }, :body => xml.to_s
590 assert_response :bad_request, "shouldn't be able to delete a relation when payload is different to the url"
592 # this won't work because the relation is in-use by another relation
593 xml = update_changeset(xml_for_relation(used_relation), changeset.id)
594 delete :delete, :params => { :id => used_relation.id }, :body => xml.to_s
595 assert_response :precondition_failed,
596 "shouldn't be able to delete a relation used in a relation (#{@response.body})"
597 assert_equal "Precondition failed: The relation #{used_relation.id} is used in relation #{super_relation.id}.", @response.body
599 # this should work when we provide the appropriate payload...
600 xml = update_changeset(xml_for_relation(multi_tag_relation), changeset.id)
601 delete :delete, :params => { :id => multi_tag_relation.id }, :body => xml.to_s
602 assert_response :success
604 # valid delete should return the new version number, which should
605 # be greater than the old version number
606 assert @response.body.to_i > multi_tag_relation.version,
607 "delete request should return a new version number for relation"
609 # this won't work since the relation is already deleted
610 xml = update_changeset(xml_for_relation(deleted_relation), changeset.id)
611 delete :delete, :params => { :id => deleted_relation.id }, :body => xml.to_s
612 assert_response :gone
614 # Public visible relation needs to be deleted
615 xml = update_changeset(xml_for_relation(super_relation), changeset.id)
616 delete :delete, :params => { :id => super_relation.id }, :body => xml.to_s
617 assert_response :success
619 # this works now because the relation which was using this one
621 xml = update_changeset(xml_for_relation(used_relation), changeset.id)
622 delete :delete, :params => { :id => used_relation.id }, :body => xml.to_s
623 assert_response :success,
624 "should be able to delete a relation used in an old relation (#{@response.body})"
626 # this won't work since the relation never existed
627 delete :delete, :params => { :id => 0 }
628 assert_response :not_found
632 # when a relation's tag is modified then it should put the bounding
633 # box of all its members into the changeset.
634 def test_tag_modify_bounding_box
635 relation = create(:relation)
636 node1 = create(:node, :lat => 3, :lon => 3)
637 node2 = create(:node, :lat => 5, :lon => 5)
639 create(:way_node, :way => way, :node => node1)
640 create(:relation_member, :relation => relation, :member => way)
641 create(:relation_member, :relation => relation, :member => node2)
642 # the relation contains nodes1 and node2 (node1
643 # indirectly via the way), so the bbox should be [3,3,5,5].
644 check_changeset_modify(BoundingBox.new(3, 3, 5, 5)) do |changeset_id|
645 # add a tag to an existing relation
646 relation_xml = xml_for_relation(relation)
647 relation_element = relation_xml.find("//osm/relation").first
648 new_tag = XML::Node.new("tag")
649 new_tag["k"] = "some_new_tag"
650 new_tag["v"] = "some_new_value"
651 relation_element << new_tag
653 # update changeset ID to point to new changeset
654 update_changeset(relation_xml, changeset_id)
657 put :update, :params => { :id => relation.id }, :body => relation_xml.to_s
658 assert_response :success, "can't update relation for tag/bbox test"
663 # add a member to a relation and check the bounding box is only that
665 def test_add_member_bounding_box
666 relation = create(:relation)
667 node1 = create(:node, :lat => 4, :lon => 4)
668 node2 = create(:node, :lat => 7, :lon => 7)
670 create(:way_node, :way => way1, :node => create(:node, :lat => 8, :lon => 8))
672 create(:way_node, :way => way2, :node => create(:node, :lat => 9, :lon => 9), :sequence_id => 1)
673 create(:way_node, :way => way2, :node => create(:node, :lat => 10, :lon => 10), :sequence_id => 2)
675 [node1, node2, way1, way2].each do |element|
676 bbox = element.bbox.to_unscaled
677 check_changeset_modify(bbox) do |changeset_id|
678 relation_xml = xml_for_relation(Relation.find(relation.id))
679 relation_element = relation_xml.find("//osm/relation").first
680 new_member = XML::Node.new("member")
681 new_member["ref"] = element.id.to_s
682 new_member["type"] = element.class.to_s.downcase
683 new_member["role"] = "some_role"
684 relation_element << new_member
686 # update changeset ID to point to new changeset
687 update_changeset(relation_xml, changeset_id)
690 put :update, :params => { :id => relation.id }, :body => relation_xml.to_s
691 assert_response :success, "can't update relation for add #{element.class}/bbox test: #{@response.body}"
693 # get it back and check the ordering
694 get :show, :params => { :id => relation.id }
695 assert_response :success, "can't read back the relation: #{@response.body}"
696 check_ordering(relation_xml, @response.body)
702 # remove a member from a relation and check the bounding box is
704 def test_remove_member_bounding_box
705 relation = create(:relation)
706 node1 = create(:node, :lat => 3, :lon => 3)
707 node2 = create(:node, :lat => 5, :lon => 5)
708 create(:relation_member, :relation => relation, :member => node1)
709 create(:relation_member, :relation => relation, :member => node2)
711 check_changeset_modify(BoundingBox.new(5, 5, 5, 5)) do |changeset_id|
712 # remove node 5 (5,5) from an existing relation
713 relation_xml = xml_for_relation(relation)
715 .find("//osm/relation/member[@type='node'][@ref='#{node2.id}']")
718 # update changeset ID to point to new changeset
719 update_changeset(relation_xml, changeset_id)
722 put :update, :params => { :id => relation.id }, :body => relation_xml.to_s
723 assert_response :success, "can't update relation for remove node/bbox test"
728 # check that relations are ordered
729 def test_relation_member_ordering
731 changeset = create(:changeset, :user => user)
732 node1 = create(:node)
733 node2 = create(:node)
734 node3 = create(:node)
735 way1 = create(:way_with_nodes, :nodes_count => 2)
736 way2 = create(:way_with_nodes, :nodes_count => 2)
738 basic_authorization user.email, "test"
742 <relation changeset='#{changeset.id}'>
743 <member ref='#{node1.id}' type='node' role='first'/>
744 <member ref='#{node2.id}' type='node' role='second'/>
745 <member ref='#{way1.id}' type='way' role='third'/>
746 <member ref='#{way2.id}' type='way' role='fourth'/>
750 doc = XML::Parser.string(doc_str).parse
752 put :create, :body => doc.to_s
753 assert_response :success, "can't create a relation: #{@response.body}"
754 relation_id = @response.body.to_i
756 # get it back and check the ordering
757 get :show, :params => { :id => relation_id }
758 assert_response :success, "can't read back the relation: #{@response.body}"
759 check_ordering(doc, @response.body)
761 # insert a member at the front
762 new_member = XML::Node.new "member"
763 new_member["ref"] = node3.id.to_s
764 new_member["type"] = "node"
765 new_member["role"] = "new first"
766 doc.find("//osm/relation").first.child.prev = new_member
767 # update the version, should be 1?
768 doc.find("//osm/relation").first["id"] = relation_id.to_s
769 doc.find("//osm/relation").first["version"] = 1.to_s
771 # upload the next version of the relation
772 put :update, :params => { :id => relation_id }, :body => doc.to_s
773 assert_response :success, "can't update relation: #{@response.body}"
774 assert_equal 2, @response.body.to_i
776 # get it back again and check the ordering again
777 get :show, :params => { :id => relation_id }
778 assert_response :success, "can't read back the relation: #{@response.body}"
779 check_ordering(doc, @response.body)
781 # check the ordering in the history tables:
782 with_controller(OldRelationsController.new) do
783 get :version, :params => { :id => relation_id, :version => 2 }
784 assert_response :success, "can't read back version 2 of the relation #{relation_id}"
785 check_ordering(doc, @response.body)
790 # check that relations can contain duplicate members
791 def test_relation_member_duplicates
792 private_user = create(:user, :data_public => false)
794 changeset = create(:changeset, :user => user)
795 node1 = create(:node)
796 node2 = create(:node)
800 <relation changeset='#{changeset.id}'>
801 <member ref='#{node1.id}' type='node' role='forward'/>
802 <member ref='#{node2.id}' type='node' role='forward'/>
803 <member ref='#{node1.id}' type='node' role='forward'/>
804 <member ref='#{node2.id}' type='node' role='forward'/>
808 doc = XML::Parser.string(doc_str).parse
810 ## First try with the private user
811 basic_authorization private_user.email, "test"
813 put :create, :body => doc.to_s
814 assert_response :forbidden
816 ## Now try with the public user
817 basic_authorization user.email, "test"
819 put :create, :body => doc.to_s
820 assert_response :success, "can't create a relation: #{@response.body}"
821 relation_id = @response.body.to_i
823 # get it back and check the ordering
824 get :show, :params => { :id => relation_id }
825 assert_response :success, "can't read back the relation: #{relation_id}"
826 check_ordering(doc, @response.body)
830 # test that the ordering of elements in the history is the same as in current.
831 def test_history_ordering
833 changeset = create(:changeset, :user => user)
834 node1 = create(:node)
835 node2 = create(:node)
836 node3 = create(:node)
837 node4 = create(:node)
841 <relation changeset='#{changeset.id}'>
842 <member ref='#{node1.id}' type='node' role='forward'/>
843 <member ref='#{node4.id}' type='node' role='forward'/>
844 <member ref='#{node3.id}' type='node' role='forward'/>
845 <member ref='#{node2.id}' type='node' role='forward'/>
849 doc = XML::Parser.string(doc_str).parse
850 basic_authorization user.email, "test"
852 put :create, :body => doc.to_s
853 assert_response :success, "can't create a relation: #{@response.body}"
854 relation_id = @response.body.to_i
856 # check the ordering in the current tables:
857 get :show, :params => { :id => relation_id }
858 assert_response :success, "can't read back the relation: #{@response.body}"
859 check_ordering(doc, @response.body)
861 # check the ordering in the history tables:
862 with_controller(OldRelationsController.new) do
863 get :version, :params => { :id => relation_id, :version => 1 }
864 assert_response :success, "can't read back version 1 of the relation: #{@response.body}"
865 check_ordering(doc, @response.body)
870 # remove all the members from a relation. the result is pretty useless, but
871 # still technically valid.
872 def test_remove_all_members
873 relation = create(:relation)
874 node1 = create(:node, :lat => 3, :lon => 3)
875 node2 = create(:node, :lat => 5, :lon => 5)
877 create(:way_node, :way => way, :node => node1)
878 create(:relation_member, :relation => relation, :member => way)
879 create(:relation_member, :relation => relation, :member => node2)
881 check_changeset_modify(BoundingBox.new(3, 3, 5, 5)) do |changeset_id|
882 relation_xml = xml_for_relation(relation)
884 .find("//osm/relation/member")
887 # update changeset ID to point to new changeset
888 update_changeset(relation_xml, changeset_id)
891 put :update, :params => { :id => relation.id }, :body => relation_xml.to_s
892 assert_response :success, "can't update relation for remove all members test"
893 checkrelation = Relation.find(relation.id)
894 assert_not_nil(checkrelation,
895 "uploaded relation not found in database after upload")
896 assert_equal(0, checkrelation.members.length,
897 "relation contains members but they should have all been deleted")
901 # ============================================================
903 # ============================================================
906 # checks that the XML document and the string arguments have
907 # members in the same order.
908 def check_ordering(doc, xml)
909 new_doc = XML::Parser.string(xml).parse
911 doc_members = doc.find("//osm/relation/member").collect do |m|
912 [m["ref"].to_i, m["type"].to_sym, m["role"]]
915 new_members = new_doc.find("//osm/relation/member").collect do |m|
916 [m["ref"].to_i, m["type"].to_sym, m["role"]]
919 doc_members.zip(new_members).each do |d, n|
920 assert_equal d, n, "members are not equal - ordering is wrong? (#{doc}, #{xml})"
925 # create a changeset and yield to the caller to set it up, then assert
926 # that the changeset bounding box is +bbox+.
927 def check_changeset_modify(bbox)
928 ## First test with the private user to check that you get a forbidden
929 basic_authorization create(:user, :data_public => false).email, "test"
931 # create a new changeset for this operation, so we are assured
932 # that the bounding box will be newly-generated.
933 changeset_id = with_controller(Api::ChangesetsController.new) do
934 xml = "<osm><changeset/></osm>"
935 put :create, :body => xml
936 assert_response :forbidden, "shouldn't be able to create changeset for modify test, as should get forbidden"
939 ## Now do the whole thing with the public user
940 basic_authorization create(:user).email, "test"
942 # create a new changeset for this operation, so we are assured
943 # that the bounding box will be newly-generated.
944 changeset_id = with_controller(Api::ChangesetsController.new) do
945 xml = "<osm><changeset/></osm>"
946 put :create, :body => xml
947 assert_response :success, "couldn't create changeset for modify test"
951 # go back to the block to do the actual modifies
954 # now download the changeset to check its bounding box
955 with_controller(Api::ChangesetsController.new) do
956 get :show, :params => { :id => changeset_id }
957 assert_response :success, "can't re-read changeset for modify test"
958 assert_select "osm>changeset", 1, "Changeset element doesn't exist in #{@response.body}"
959 assert_select "osm>changeset[id='#{changeset_id}']", 1, "Changeset id=#{changeset_id} doesn't exist in #{@response.body}"
960 assert_select "osm>changeset[min_lon='#{format('%.7f', bbox.min_lon)}']", 1, "Changeset min_lon wrong in #{@response.body}"
961 assert_select "osm>changeset[min_lat='#{format('%.7f', bbox.min_lat)}']", 1, "Changeset min_lat wrong in #{@response.body}"
962 assert_select "osm>changeset[max_lon='#{format('%.7f', bbox.max_lon)}']", 1, "Changeset max_lon wrong in #{@response.body}"
963 assert_select "osm>changeset[max_lat='#{format('%.7f', bbox.max_lat)}']", 1, "Changeset max_lat wrong in #{@response.body}"
968 # yields the relation with the given +id+ (and optional +version+
969 # to read from the history tables) into the block. the parsed XML
971 def with_relation(id, ver = nil)
973 get :show, :params => { :id => id }
975 with_controller(OldRelationsController.new) do
976 get :version, :params => { :id => id, :version => ver }
979 assert_response :success
980 yield xml_parse(@response.body)
984 # updates the relation (XML) +rel+ and
985 # yields the new version of that relation into the block.
986 # the parsed XML doc is retured.
988 rel_id = rel.find("//osm/relation").first["id"].to_i
989 put :update, :params => { :id => rel_id }, :body => rel.to_s
990 assert_response :success, "can't update relation: #{@response.body}"
991 version = @response.body.to_i
993 # now get the new version
994 get :show, :params => { :id => rel_id }
995 assert_response :success
996 new_rel = xml_parse(@response.body)
1004 # updates the relation (XML) +rel+ via the diff-upload API and
1005 # yields the new version of that relation into the block.
1006 # the parsed XML doc is retured.
1007 def with_update_diff(rel)
1008 rel_id = rel.find("//osm/relation").first["id"].to_i
1009 cs_id = rel.find("//osm/relation").first["changeset"].to_i
1012 with_controller(Api::ChangesetsController.new) do
1013 doc = OSM::API.new.get_xml_doc
1014 change = XML::Node.new "osmChange"
1016 modify = XML::Node.new "modify"
1018 modify << doc.import(rel.find("//osm/relation").first)
1020 post :upload, :params => { :id => cs_id }, :body => doc.to_s
1021 assert_response :success, "can't upload diff relation: #{@response.body}"
1022 version = xml_parse(@response.body).find("//diffResult/relation").first["new_version"].to_i
1025 # now get the new version
1026 get :show, :params => { :id => rel_id }
1027 assert_response :success
1028 new_rel = xml_parse(@response.body)
1036 # returns a k->v hash of tags from an xml doc
1037 def get_tags_as_hash(a)
1038 a.find("//osm/relation/tag").sort_by { |v| v["k"] }.each_with_object({}) do |v, h|
1044 # assert that all tags on relation documents +a+ and +b+
1046 def assert_tags_equal(a, b)
1047 # turn the XML doc into tags hashes
1048 a_tags = get_tags_as_hash(a)
1049 b_tags = get_tags_as_hash(b)
1051 assert_equal a_tags.keys, b_tags.keys, "Tag keys should be identical."
1052 a_tags.each do |k, v|
1053 assert_equal v, b_tags[k],
1054 "Tags which were not altered should be the same. " \
1055 "#{a_tags.inspect} != #{b_tags.inspect}"
1060 # update the changeset_id of a node element
1061 def update_changeset(xml, changeset_id)
1062 xml_attr_rewrite(xml, "changeset", changeset_id)
1066 # update an attribute in the node element
1067 def xml_attr_rewrite(xml, name, value)
1068 xml.find("//osm/relation").first[name] = value.to_s
1075 parser = XML::Parser.string(xml)