1 require File.dirname(__FILE__) + '/../test_helper'
2 require 'relation_controller'
4 class RelationControllerTest < ActionController::TestCase
7 # -------------------------------------
8 # Test reading relations.
9 # -------------------------------------
12 # check that a visible relation is returned properly
13 get :read, :id => current_relations(:visible_relation).id
14 assert_response :success
16 # check that an invisible relation is not returned
17 get :read, :id => current_relations(:invisible_relation).id
20 # check chat a non-existent relation is not returned
22 assert_response :not_found
26 # check that all relations containing a particular node, and no extra
27 # relations, are returned from the relations_for_node call.
28 def test_relations_for_node
29 check_relations_for_element(:relations_for_node, "node",
30 current_nodes(:node_used_by_relationship).id,
31 [ :visible_relation, :used_relation ])
34 def test_relations_for_way
35 check_relations_for_element(:relations_for_way, "way",
36 current_ways(:used_way).id,
37 [ :visible_relation ])
40 def test_relations_for_relation
41 check_relations_for_element(:relations_for_relation, "relation",
42 current_relations(:used_relation).id,
43 [ :visible_relation ])
46 def check_relations_for_element(method, type, id, expected_relations)
47 # check the "relations for relation" mode
49 assert_response :success
51 # count one osm element
52 assert_select "osm[version=#{API_VERSION}][generator=\"OpenStreetMap server\"]", 1
54 # we should have only the expected number of relations
55 assert_select "osm>relation", expected_relations.size
57 # and each of them should contain the node we originally searched for
58 expected_relations.each do |r|
59 relation_id = current_relations(r).id
60 assert_select "osm>relation#?", relation_id
61 assert_select "osm>relation#?>member[type=\"#{type}\"][ref=#{id}]", relation_id
66 # check the "full" mode
67 get :full, :id => current_relations(:visible_relation).id
68 assert_response :success
69 # FIXME check whether this contains the stuff we want!
75 # -------------------------------------
76 # Test simple relation creation.
77 # -------------------------------------
80 basic_authorization "test@openstreetmap.org", "test"
82 # put the relation in a dummy fixture changset
83 changeset_id = changesets(:normal_user_first_change).id
85 # create an relation without members
86 content "<osm><relation changeset='#{changeset_id}'><tag k='test' v='yes' /></relation></osm>"
89 assert_response :success,
90 "relation upload did not return success status"
91 # read id of created relation and search for it
92 relationid = @response.body
93 checkrelation = Relation.find(relationid)
94 assert_not_nil checkrelation,
95 "uploaded relation not found in data base after upload"
97 assert_equal checkrelation.members.length, 0,
98 "saved relation contains members but should not"
99 assert_equal checkrelation.tags.length, 1,
100 "saved relation does not contain exactly one tag"
101 assert_equal changeset_id, checkrelation.changeset.id,
102 "saved relation does not belong in the changeset it was assigned to"
103 assert_equal users(:normal_user).id, checkrelation.changeset.user_id,
104 "saved relation does not belong to user that created it"
105 assert_equal true, checkrelation.visible,
106 "saved relation is not visible"
107 # ok the relation is there but can we also retrieve it?
108 get :read, :id => relationid
109 assert_response :success
113 # create an relation with a node as member
114 # This time try with a role attribute in the relation
115 nid = current_nodes(:used_node_1).id
116 content "<osm><relation changeset='#{changeset_id}'>" +
117 "<member ref='#{nid}' type='node' role='some'/>" +
118 "<tag k='test' v='yes' /></relation></osm>"
121 assert_response :success,
122 "relation upload did not return success status"
123 # read id of created relation and search for it
124 relationid = @response.body
125 checkrelation = Relation.find(relationid)
126 assert_not_nil checkrelation,
127 "uploaded relation not found in data base after upload"
129 assert_equal checkrelation.members.length, 1,
130 "saved relation does not contain exactly one member"
131 assert_equal checkrelation.tags.length, 1,
132 "saved relation does not contain exactly one tag"
133 assert_equal changeset_id, checkrelation.changeset.id,
134 "saved relation does not belong in the changeset it was assigned to"
135 assert_equal users(:normal_user).id, checkrelation.changeset.user_id,
136 "saved relation does not belong to user that created it"
137 assert_equal true, checkrelation.visible,
138 "saved relation is not visible"
139 # ok the relation is there but can we also retrieve it?
141 get :read, :id => relationid
142 assert_response :success
146 # create an relation with a node as member, this time test that we don't
147 # need a role attribute to be included
148 nid = current_nodes(:used_node_1).id
149 content "<osm><relation changeset='#{changeset_id}'>" +
150 "<member ref='#{nid}' type='node'/>"+
151 "<tag k='test' v='yes' /></relation></osm>"
154 assert_response :success,
155 "relation upload did not return success status"
156 # read id of created relation and search for it
157 relationid = @response.body
158 checkrelation = Relation.find(relationid)
159 assert_not_nil checkrelation,
160 "uploaded relation not found in data base after upload"
162 assert_equal checkrelation.members.length, 1,
163 "saved relation does not contain exactly one member"
164 assert_equal checkrelation.tags.length, 1,
165 "saved relation does not contain exactly one tag"
166 assert_equal changeset_id, checkrelation.changeset.id,
167 "saved relation does not belong in the changeset it was assigned to"
168 assert_equal users(:normal_user).id, checkrelation.changeset.user_id,
169 "saved relation does not belong to user that created it"
170 assert_equal true, checkrelation.visible,
171 "saved relation is not visible"
172 # ok the relation is there but can we also retrieve it?
174 get :read, :id => relationid
175 assert_response :success
178 # create an relation with a way and a node as members
179 nid = current_nodes(:used_node_1).id
180 wid = current_ways(:used_way).id
181 content "<osm><relation changeset='#{changeset_id}'>" +
182 "<member type='node' ref='#{nid}' role='some'/>" +
183 "<member type='way' ref='#{wid}' role='other'/>" +
184 "<tag k='test' v='yes' /></relation></osm>"
187 assert_response :success,
188 "relation upload did not return success status"
189 # read id of created relation and search for it
190 relationid = @response.body
191 checkrelation = Relation.find(relationid)
192 assert_not_nil checkrelation,
193 "uploaded relation not found in data base after upload"
195 assert_equal checkrelation.members.length, 2,
196 "saved relation does not have exactly two members"
197 assert_equal checkrelation.tags.length, 1,
198 "saved relation does not contain exactly one tag"
199 assert_equal changeset_id, checkrelation.changeset.id,
200 "saved relation does not belong in the changeset it was assigned to"
201 assert_equal users(:normal_user).id, checkrelation.changeset.user_id,
202 "saved relation does not belong to user that created it"
203 assert_equal true, checkrelation.visible,
204 "saved relation is not visible"
205 # ok the relation is there but can we also retrieve it?
206 get :read, :id => relationid
207 assert_response :success
211 # ------------------------------------
212 # Test updating relations
213 # ------------------------------------
216 # test that, when tags are updated on a relation, the correct things
217 # happen to the correct tables and the API gives sensible results.
218 # this is to test a case that gregory marler noticed and posted to
220 def test_update_relation_tags
221 basic_authorization "test@example.com", "test"
222 rel_id = current_relations(:multi_tag_relation).id
223 cs_id = changesets(:public_user_first_change).id
225 with_relation(rel_id) do |rel|
226 # alter one of the tags
227 tag = rel.find("//osm/relation/tag").first
228 tag['v'] = 'some changed value'
229 update_changeset(rel, cs_id)
231 # check that the downloaded tags are the same as the uploaded tags...
232 new_version = with_update(rel) do |new_rel|
233 assert_tags_equal rel, new_rel
236 # check the original one in the current_* table again
237 with_relation(rel_id) { |r| assert_tags_equal rel, r }
239 # now check the version in the history
240 with_relation(rel_id, new_version) { |r| assert_tags_equal rel, r }
245 # test that, when tags are updated on a relation when using the diff
246 # upload function, the correct things happen to the correct tables
247 # and the API gives sensible results. this is to test a case that
248 # gregory marler noticed and posted to josm-dev.
249 def test_update_relation_tags_via_upload
250 basic_authorization "test@example.com", "test"
251 rel_id = current_relations(:multi_tag_relation).id
252 cs_id = changesets(:public_user_first_change).id
254 with_relation(rel_id) do |rel|
255 # alter one of the tags
256 tag = rel.find("//osm/relation/tag").first
257 tag['v'] = 'some changed value'
258 update_changeset(rel, cs_id)
260 # check that the downloaded tags are the same as the uploaded tags...
261 new_version = with_update_diff(rel) do |new_rel|
262 assert_tags_equal rel, new_rel
265 # check the original one in the current_* table again
266 with_relation(rel_id) { |r| assert_tags_equal rel, r }
268 # now check the version in the history
269 with_relation(rel_id, new_version) { |r| assert_tags_equal rel, r }
273 # -------------------------------------
274 # Test creating some invalid relations.
275 # -------------------------------------
277 def test_create_invalid
278 basic_authorization "test@openstreetmap.org", "test"
280 # put the relation in a dummy fixture changset
281 changeset_id = changesets(:normal_user_first_change).id
283 # create a relation with non-existing node as member
284 content "<osm><relation changeset='#{changeset_id}'>" +
285 "<member type='node' ref='0'/><tag k='test' v='yes' />" +
289 assert_response :precondition_failed,
290 "relation upload with invalid node did not return 'precondition failed'"
293 # -------------------------------------
294 # Test creating a relation, with some invalid XML
295 # -------------------------------------
296 def test_create_invalid_xml
297 basic_authorization "test@openstreetmap.org", "test"
299 # put the relation in a dummy fixture changeset that works
300 changeset_id = changesets(:normal_user_first_change).id
302 # create some xml that should return an error
303 content "<osm><relation changeset='#{changeset_id}'>" +
304 "<member type='type' ref='#{current_nodes(:used_node_1).id}' role=''/>" +
305 "<tag k='tester' v='yep'/></relation></osm>"
308 assert_response :bad_request
309 assert_match(/Cannot parse valid relation from xml string/, @response.body)
310 assert_match(/The type is not allowed only, /, @response.body)
314 # -------------------------------------
315 # Test deleting relations.
316 # -------------------------------------
319 # first try to delete relation without auth
320 delete :delete, :id => current_relations(:visible_relation).id
321 assert_response :unauthorized
323 ## First try with the private user, to make sure that you get a forbidden
324 basic_authorization(users(:normal_user).email, "test")
326 # this shouldn't work, as we should need the payload...
327 delete :delete, :id => current_relations(:visible_relation).id
328 assert_response :forbidden
330 # try to delete without specifying a changeset
331 content "<osm><relation id='#{current_relations(:visible_relation).id}'/></osm>"
332 delete :delete, :id => current_relations(:visible_relation).id
333 assert_response :forbidden
335 # try to delete with an invalid (closed) changeset
336 content update_changeset(current_relations(:visible_relation).to_xml,
337 changesets(:normal_user_closed_change).id)
338 delete :delete, :id => current_relations(:visible_relation).id
339 assert_response :forbidden
341 # try to delete with an invalid (non-existent) changeset
342 content update_changeset(current_relations(:visible_relation).to_xml,0)
343 delete :delete, :id => current_relations(:visible_relation).id
344 assert_response :forbidden
346 # this won't work because the relation is in-use by another relation
347 content(relations(:used_relation).to_xml)
348 delete :delete, :id => current_relations(:used_relation).id
349 assert_response :forbidden
351 # this should work when we provide the appropriate payload...
352 content(relations(:visible_relation).to_xml)
353 delete :delete, :id => current_relations(:visible_relation).id
354 assert_response :forbidden
356 # this won't work since the relation is already deleted
357 content(relations(:invisible_relation).to_xml)
358 delete :delete, :id => current_relations(:invisible_relation).id
359 assert_response :forbidden
361 # this works now because the relation which was using this one
363 content(relations(:used_relation).to_xml)
364 delete :delete, :id => current_relations(:used_relation).id
365 assert_response :forbidden
367 # this won't work since the relation never existed
368 delete :delete, :id => 0
369 assert_response :forbidden
373 # now set auth for the private user
374 basic_authorization(users(:public_user).email, "test");
376 # this shouldn't work, as we should need the payload...
377 delete :delete, :id => current_relations(:visible_relation).id
378 assert_response :bad_request
380 # try to delete without specifying a changeset
381 content "<osm><relation id='#{current_relations(:visible_relation).id}'/></osm>"
382 delete :delete, :id => current_relations(:visible_relation).id
383 assert_response :bad_request
384 assert_match(/You are missing the required changeset in the relation/, @response.body)
386 # try to delete with an invalid (closed) changeset
387 content update_changeset(current_relations(:visible_relation).to_xml,
388 changesets(:normal_user_closed_change).id)
389 delete :delete, :id => current_relations(:visible_relation).id
390 assert_response :conflict
392 # try to delete with an invalid (non-existent) changeset
393 content update_changeset(current_relations(:visible_relation).to_xml,0)
394 delete :delete, :id => current_relations(:visible_relation).id
395 assert_response :conflict
397 # this won't work because the relation is in a changeset owned by someone else
398 content(relations(:used_relation).to_xml)
399 delete :delete, :id => current_relations(:used_relation).id
400 assert_response :conflict,
401 "shouldn't be able to delete a relation in a changeset owned by someone else (#{@response.body})"
403 # this won't work because the relation in the payload is different to that passed
404 content(relations(:public_used_relation).to_xml)
405 delete :delete, :id => current_relations(:used_relation).id
406 assert_not_equal relations(:public_used_relation).id, current_relations(:used_relation).id
407 assert_response :bad_request, "shouldn't be able to delete a relation when payload is different to the url"
409 # this won't work because the relation is in-use by another relation
410 content(relations(:public_used_relation).to_xml)
411 delete :delete, :id => current_relations(:public_used_relation).id
412 assert_response :precondition_failed,
413 "shouldn't be able to delete a relation used in a relation (#{@response.body})"
415 # this should work when we provide the appropriate payload...
416 content(relations(:multi_tag_relation).to_xml)
417 delete :delete, :id => current_relations(:multi_tag_relation).id
418 assert_response :success
420 # valid delete should return the new version number, which should
421 # be greater than the old version number
422 assert @response.body.to_i > current_relations(:visible_relation).version,
423 "delete request should return a new version number for relation"
425 # this won't work since the relation is already deleted
426 content(relations(:invisible_relation).to_xml)
427 delete :delete, :id => current_relations(:invisible_relation).id
428 assert_response :gone
430 # this works now because the relation which was using this one
432 content(relations(:public_used_relation).to_xml)
433 delete :delete, :id => current_relations(:public_used_relation).id
434 assert_response :success,
435 "should be able to delete a relation used in an old relation (#{@response.body})"
437 # this won't work since the relation never existed
438 delete :delete, :id => 0
439 assert_response :not_found
443 # when a relation's tag is modified then it should put the bounding
444 # box of all its members into the changeset.
445 def test_tag_modify_bounding_box
446 # in current fixtures, relation 5 contains nodes 3 and 5 (node 3
447 # indirectly via way 3), so the bbox should be [3,3,5,5].
448 check_changeset_modify([3,3,5,5]) do |changeset_id|
449 # add a tag to an existing relation
450 relation_xml = current_relations(:visible_relation).to_xml
451 relation_element = relation_xml.find("//osm/relation").first
452 new_tag = XML::Node.new("tag")
453 new_tag['k'] = "some_new_tag"
454 new_tag['v'] = "some_new_value"
455 relation_element << new_tag
457 # update changeset ID to point to new changeset
458 update_changeset(relation_xml, changeset_id)
462 put :update, :id => current_relations(:visible_relation).id
463 assert_response :success, "can't update relation for tag/bbox test"
468 # add a member to a relation and check the bounding box is only that
470 def test_add_member_bounding_box
471 check_changeset_modify([4,4,4,4]) do |changeset_id|
472 # add node 4 (4,4) to an existing relation
473 relation_xml = current_relations(:visible_relation).to_xml
474 relation_element = relation_xml.find("//osm/relation").first
475 new_member = XML::Node.new("member")
476 new_member['ref'] = current_nodes(:used_node_2).id.to_s
477 new_member['type'] = "node"
478 new_member['role'] = "some_role"
479 relation_element << new_member
481 # update changeset ID to point to new changeset
482 update_changeset(relation_xml, changeset_id)
486 put :update, :id => current_relations(:visible_relation).id
487 assert_response :success, "can't update relation for add node/bbox test"
492 # remove a member from a relation and check the bounding box is
494 def test_remove_member_bounding_box
495 check_changeset_modify([5,5,5,5]) do |changeset_id|
496 # remove node 5 (5,5) from an existing relation
497 relation_xml = current_relations(:visible_relation).to_xml
499 find("//osm/relation/member[@type='node'][@ref='5']").
502 # update changeset ID to point to new changeset
503 update_changeset(relation_xml, changeset_id)
507 put :update, :id => current_relations(:visible_relation).id
508 assert_response :success, "can't update relation for remove node/bbox test"
513 # check that relations are ordered
514 def test_relation_member_ordering
515 basic_authorization(users(:public_user).email, "test")
519 <relation changeset='4'>
520 <member ref='1' type='node' role='first'/>
521 <member ref='3' type='node' role='second'/>
522 <member ref='1' type='way' role='third'/>
523 <member ref='3' type='way' role='fourth'/>
527 doc = XML::Parser.string(doc_str).parse
531 assert_response :success, "can't create a relation: #{@response.body}"
532 relation_id = @response.body.to_i
534 # get it back and check the ordering
535 get :read, :id => relation_id
536 assert_response :success, "can't read back the relation: #{@response.body}"
537 check_ordering(doc, @response.body)
539 # insert a member at the front
540 new_member = XML::Node.new "member"
541 new_member['ref'] = 5.to_s
542 new_member['type'] = 'node'
543 new_member['role'] = 'new first'
544 doc.find("//osm/relation").first.child.prev = new_member
545 # update the version, should be 1?
546 doc.find("//osm/relation").first['id'] = relation_id.to_s
547 doc.find("//osm/relation").first['version'] = 1.to_s
549 # upload the next version of the relation
551 put :update, :id => relation_id
552 assert_response :success, "can't update relation: #{@response.body}"
553 new_version = @response.body.to_i
555 # get it back again and check the ordering again
556 get :read, :id => relation_id
557 assert_response :success, "can't read back the relation: #{@response.body}"
558 check_ordering(doc, @response.body)
562 # check that relations can contain duplicate members
563 def test_relation_member_duplicates
564 ## First try with the private user
565 basic_authorization(users(:normal_user).email, "test");
569 <relation changeset='4'>
570 <member ref='1' type='node' role='forward'/>
571 <member ref='3' type='node' role='forward'/>
572 <member ref='1' type='node' role='forward'/>
573 <member ref='3' type='node' role='forward'/>
577 doc = XML::Parser.string(doc_str).parse
581 assert_response :forbidden
584 ## Now try with the public user
585 basic_authorization(users(:public_user).email, "test");
589 <relation changeset='4'>
590 <member ref='1' type='node' role='forward'/>
591 <member ref='3' type='node' role='forward'/>
592 <member ref='1' type='node' role='forward'/>
593 <member ref='3' type='node' role='forward'/>
597 doc = XML::Parser.string(doc_str).parse
601 assert_response :success, "can't create a relation: #{@response.body}"
602 relation_id = @response.body.to_i
604 # get it back and check the ordering
605 get :read, :id => relation_id
606 assert_response :success, "can't read back the relation: #{@response.body}"
607 check_ordering(doc, @response.body)
610 # ============================================================
612 # ============================================================
615 # checks that the XML document and the string arguments have
616 # members in the same order.
617 def check_ordering(doc, xml)
618 new_doc = XML::Parser.string(xml).parse
620 doc_members = doc.find("//osm/relation/member").collect do |m|
621 [m['ref'].to_i, m['type'].to_sym, m['role']]
624 new_members = new_doc.find("//osm/relation/member").collect do |m|
625 [m['ref'].to_i, m['type'].to_sym, m['role']]
628 doc_members.zip(new_members).each do |d, n|
629 assert_equal d, n, "members are not equal - ordering is wrong? (#{doc}, #{xml})"
634 # create a changeset and yield to the caller to set it up, then assert
635 # that the changeset bounding box is +bbox+.
636 def check_changeset_modify(bbox)
637 ## First test with the private user to check that you get a forbidden
638 basic_authorization(users(:normal_user).email, "test");
640 # create a new changeset for this operation, so we are assured
641 # that the bounding box will be newly-generated.
642 changeset_id = with_controller(ChangesetController.new) do
643 content "<osm><changeset/></osm>"
645 assert_response :forbidden, "shouldn't be able to create changeset for modify test, as should get forbidden"
649 ## Now do the whole thing with the public user
650 basic_authorization(users(:public_user).email, "test")
652 # create a new changeset for this operation, so we are assured
653 # that the bounding box will be newly-generated.
654 changeset_id = with_controller(ChangesetController.new) do
655 content "<osm><changeset/></osm>"
657 assert_response :success, "couldn't create changeset for modify test"
661 # go back to the block to do the actual modifies
664 # now download the changeset to check its bounding box
665 with_controller(ChangesetController.new) do
666 get :read, :id => changeset_id
667 assert_response :success, "can't re-read changeset for modify test"
668 assert_select "osm>changeset", 1
669 assert_select "osm>changeset[id=#{changeset_id}]", 1
670 assert_select "osm>changeset[min_lon=#{bbox[0].to_f}]", 1
671 assert_select "osm>changeset[min_lat=#{bbox[1].to_f}]", 1
672 assert_select "osm>changeset[max_lon=#{bbox[2].to_f}]", 1
673 assert_select "osm>changeset[max_lat=#{bbox[3].to_f}]", 1
678 # yields the relation with the given +id+ (and optional +version+
679 # to read from the history tables) into the block. the parsed XML
681 def with_relation(id, ver = nil)
685 with_controller(OldRelationController.new) do
686 get :version, :id => id, :version => ver
689 assert_response :success
690 yield xml_parse(@response.body)
694 # updates the relation (XML) +rel+ and
695 # yields the new version of that relation into the block.
696 # the parsed XML doc is retured.
698 rel_id = rel.find("//osm/relation").first["id"].to_i
700 put :update, :id => rel_id
701 assert_response :success, "can't update relation: #{@response.body}"
702 version = @response.body.to_i
704 # now get the new version
705 get :read, :id => rel_id
706 assert_response :success
707 new_rel = xml_parse(@response.body)
715 # updates the relation (XML) +rel+ via the diff-upload API and
716 # yields the new version of that relation into the block.
717 # the parsed XML doc is retured.
718 def with_update_diff(rel)
719 rel_id = rel.find("//osm/relation").first["id"].to_i
720 cs_id = rel.find("//osm/relation").first['changeset'].to_i
723 with_controller(ChangesetController.new) do
724 doc = OSM::API.new.get_xml_doc
725 change = XML::Node.new 'osmChange'
727 modify = XML::Node.new 'modify'
729 modify << doc.import(rel.find("//osm/relation").first)
732 post :upload, :id => cs_id
733 assert_response :success, "can't upload diff relation: #{@response.body}"
734 version = xml_parse(@response.body).find("//diffResult/relation").first["new_version"].to_i
737 # now get the new version
738 get :read, :id => rel_id
739 assert_response :success
740 new_rel = xml_parse(@response.body)
748 # returns a k->v hash of tags from an xml doc
749 def get_tags_as_hash(a)
750 a.find("//osm/relation/tag").inject({}) do |h,v|
757 # assert that all tags on relation documents +a+ and +b+
759 def assert_tags_equal(a, b)
760 # turn the XML doc into tags hashes
761 a_tags = get_tags_as_hash(a)
762 b_tags = get_tags_as_hash(b)
764 assert_equal a_tags.keys, b_tags.keys, "Tag keys should be identical."
765 a_tags.each do |k, v|
766 assert_equal v, b_tags[k],
767 "Tags which were not altered should be the same. " +
768 "#{a_tags.inspect} != #{b_tags.inspect}"
773 # update the changeset_id of a node element
774 def update_changeset(xml, changeset_id)
775 xml_attr_rewrite(xml, 'changeset', changeset_id)
779 # update an attribute in the node element
780 def xml_attr_rewrite(xml, name, value)
781 xml.find("//osm/relation").first[name] = value.to_s
788 parser = XML::Parser.string(xml)