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
324 basic_authorization("test@openstreetmap.org", "test");
326 # this shouldn't work, as we should need the payload...
327 delete :delete, :id => current_relations(:visible_relation).id
328 assert_response :bad_request
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 :bad_request
334 assert_match(/You are missing the required changeset in the relation/, @response.body)
336 # try to delete with an invalid (closed) changeset
337 content update_changeset(current_relations(:visible_relation).to_xml,
338 changesets(:normal_user_closed_change).id)
339 delete :delete, :id => current_relations(:visible_relation).id
340 assert_response :conflict
342 # try to delete with an invalid (non-existent) changeset
343 content update_changeset(current_relations(:visible_relation).to_xml,0)
344 delete :delete, :id => current_relations(:visible_relation).id
345 assert_response :conflict
347 # this won't work because the relation is in-use by another relation
348 content(relations(:used_relation).to_xml)
349 delete :delete, :id => current_relations(:used_relation).id
350 assert_response :precondition_failed,
351 "shouldn't be able to delete a relation used in a relation (#{@response.body})"
353 # this should work when we provide the appropriate payload...
354 content(relations(:visible_relation).to_xml)
355 delete :delete, :id => current_relations(:visible_relation).id
356 assert_response :success
358 # valid delete should return the new version number, which should
359 # be greater than the old version number
360 assert @response.body.to_i > current_relations(:visible_relation).version,
361 "delete request should return a new version number for relation"
363 # this won't work since the relation is already deleted
364 content(relations(:invisible_relation).to_xml)
365 delete :delete, :id => current_relations(:invisible_relation).id
366 assert_response :gone
368 # this works now because the relation which was using this one
370 content(relations(:used_relation).to_xml)
371 delete :delete, :id => current_relations(:used_relation).id
372 assert_response :success,
373 "should be able to delete a relation used in an old relation (#{@response.body})"
375 # this won't work since the relation never existed
376 delete :delete, :id => 0
377 assert_response :not_found
381 # when a relation's tag is modified then it should put the bounding
382 # box of all its members into the changeset.
383 def test_tag_modify_bounding_box
384 # in current fixtures, relation 5 contains nodes 3 and 5 (node 3
385 # indirectly via way 3), so the bbox should be [3,3,5,5].
386 check_changeset_modify([3,3,5,5]) do |changeset_id|
387 # add a tag to an existing relation
388 relation_xml = current_relations(:visible_relation).to_xml
389 relation_element = relation_xml.find("//osm/relation").first
390 new_tag = XML::Node.new("tag")
391 new_tag['k'] = "some_new_tag"
392 new_tag['v'] = "some_new_value"
393 relation_element << new_tag
395 # update changeset ID to point to new changeset
396 update_changeset(relation_xml, changeset_id)
400 put :update, :id => current_relations(:visible_relation).id
401 assert_response :success, "can't update relation for tag/bbox test"
406 # add a member to a relation and check the bounding box is only that
408 def test_add_member_bounding_box
409 check_changeset_modify([4,4,4,4]) do |changeset_id|
410 # add node 4 (4,4) to an existing relation
411 relation_xml = current_relations(:visible_relation).to_xml
412 relation_element = relation_xml.find("//osm/relation").first
413 new_member = XML::Node.new("member")
414 new_member['ref'] = current_nodes(:used_node_2).id.to_s
415 new_member['type'] = "node"
416 new_member['role'] = "some_role"
417 relation_element << new_member
419 # update changeset ID to point to new changeset
420 update_changeset(relation_xml, changeset_id)
424 put :update, :id => current_relations(:visible_relation).id
425 assert_response :success, "can't update relation for add node/bbox test"
430 # remove a member from a relation and check the bounding box is
432 def test_remove_member_bounding_box
433 check_changeset_modify([5,5,5,5]) do |changeset_id|
434 # remove node 5 (5,5) from an existing relation
435 relation_xml = current_relations(:visible_relation).to_xml
437 find("//osm/relation/member[@type='node'][@ref='5']").
440 # update changeset ID to point to new changeset
441 update_changeset(relation_xml, changeset_id)
445 put :update, :id => current_relations(:visible_relation).id
446 assert_response :success, "can't update relation for remove node/bbox test"
451 # check that relations are ordered
452 def test_relation_member_ordering
453 basic_authorization("test@openstreetmap.org", "test");
457 <relation changeset='1'>
458 <member ref='1' type='node' role='first'/>
459 <member ref='3' type='node' role='second'/>
460 <member ref='1' type='way' role='third'/>
461 <member ref='3' type='way' role='fourth'/>
465 doc = XML::Parser.string(doc_str).parse
469 assert_response :success, "can't create a relation: #{@response.body}"
470 relation_id = @response.body.to_i
472 # get it back and check the ordering
473 get :read, :id => relation_id
474 assert_response :success, "can't read back the relation: #{@response.body}"
475 check_ordering(doc, @response.body)
477 # insert a member at the front
478 new_member = XML::Node.new "member"
479 new_member['ref'] = 5.to_s
480 new_member['type'] = 'node'
481 new_member['role'] = 'new first'
482 doc.find("//osm/relation").first.child.prev = new_member
483 # update the version, should be 1?
484 doc.find("//osm/relation").first['id'] = relation_id.to_s
485 doc.find("//osm/relation").first['version'] = 1.to_s
487 # upload the next version of the relation
489 put :update, :id => relation_id
490 assert_response :success, "can't update relation: #{@response.body}"
491 new_version = @response.body.to_i
493 # get it back again and check the ordering again
494 get :read, :id => relation_id
495 assert_response :success, "can't read back the relation: #{@response.body}"
496 check_ordering(doc, @response.body)
500 # check that relations can contain duplicate members
501 def test_relation_member_duplicates
502 basic_authorization("test@openstreetmap.org", "test");
506 <relation changeset='1'>
507 <member ref='1' type='node' role='forward'/>
508 <member ref='3' type='node' role='forward'/>
509 <member ref='1' type='node' role='forward'/>
510 <member ref='3' type='node' role='forward'/>
514 doc = XML::Parser.string(doc_str).parse
518 assert_response :success, "can't create a relation: #{@response.body}"
519 relation_id = @response.body.to_i
521 # get it back and check the ordering
522 get :read, :id => relation_id
523 assert_response :success, "can't read back the relation: #{@response.body}"
524 check_ordering(doc, @response.body)
527 # ============================================================
529 # ============================================================
532 # checks that the XML document and the string arguments have
533 # members in the same order.
534 def check_ordering(doc, xml)
535 new_doc = XML::Parser.string(xml).parse
537 doc_members = doc.find("//osm/relation/member").collect do |m|
538 [m['ref'].to_i, m['type'].to_sym, m['role']]
541 new_members = new_doc.find("//osm/relation/member").collect do |m|
542 [m['ref'].to_i, m['type'].to_sym, m['role']]
545 doc_members.zip(new_members).each do |d, n|
546 assert_equal d, n, "members are not equal - ordering is wrong? (#{doc}, #{xml})"
551 # create a changeset and yield to the caller to set it up, then assert
552 # that the changeset bounding box is +bbox+.
553 def check_changeset_modify(bbox)
554 basic_authorization("test@openstreetmap.org", "test");
556 # create a new changeset for this operation, so we are assured
557 # that the bounding box will be newly-generated.
558 changeset_id = with_controller(ChangesetController.new) do
559 content "<osm><changeset/></osm>"
561 assert_response :success, "couldn't create changeset for modify test"
565 # go back to the block to do the actual modifies
568 # now download the changeset to check its bounding box
569 with_controller(ChangesetController.new) do
570 get :read, :id => changeset_id
571 assert_response :success, "can't re-read changeset for modify test"
572 assert_select "osm>changeset", 1
573 assert_select "osm>changeset[id=#{changeset_id}]", 1
574 assert_select "osm>changeset[min_lon=#{bbox[0].to_f}]", 1
575 assert_select "osm>changeset[min_lat=#{bbox[1].to_f}]", 1
576 assert_select "osm>changeset[max_lon=#{bbox[2].to_f}]", 1
577 assert_select "osm>changeset[max_lat=#{bbox[3].to_f}]", 1
582 # yields the relation with the given +id+ (and optional +version+
583 # to read from the history tables) into the block. the parsed XML
585 def with_relation(id, ver = nil)
589 with_controller(OldRelationController.new) do
590 get :version, :id => id, :version => ver
593 assert_response :success
594 yield xml_parse(@response.body)
598 # updates the relation (XML) +rel+ and
599 # yields the new version of that relation into the block.
600 # the parsed XML doc is retured.
602 rel_id = rel.find("//osm/relation").first["id"].to_i
604 put :update, :id => rel_id
605 assert_response :success, "can't update relation: #{@response.body}"
606 version = @response.body.to_i
608 # now get the new version
609 get :read, :id => rel_id
610 assert_response :success
611 new_rel = xml_parse(@response.body)
619 # updates the relation (XML) +rel+ via the diff-upload API and
620 # yields the new version of that relation into the block.
621 # the parsed XML doc is retured.
622 def with_update_diff(rel)
623 rel_id = rel.find("//osm/relation").first["id"].to_i
624 cs_id = rel.find("//osm/relation").first['changeset'].to_i
627 with_controller(ChangesetController.new) do
628 doc = OSM::API.new.get_xml_doc
629 change = XML::Node.new 'osmChange'
631 modify = XML::Node.new 'modify'
633 modify << doc.import(rel.find("//osm/relation").first)
636 post :upload, :id => cs_id
637 assert_response :success, "can't upload diff relation: #{@response.body}"
638 version = xml_parse(@response.body).find("//diffResult/relation").first["new_version"].to_i
641 # now get the new version
642 get :read, :id => rel_id
643 assert_response :success
644 new_rel = xml_parse(@response.body)
652 # returns a k->v hash of tags from an xml doc
653 def get_tags_as_hash(a)
654 a.find("//osm/relation/tag").inject({}) do |h,v|
661 # assert that all tags on relation documents +a+ and +b+
663 def assert_tags_equal(a, b)
664 # turn the XML doc into tags hashes
665 a_tags = get_tags_as_hash(a)
666 b_tags = get_tags_as_hash(b)
668 assert_equal a_tags.keys, b_tags.keys, "Tag keys should be identical."
669 a_tags.each do |k, v|
670 assert_equal v, b_tags[k],
671 "Tags which were not altered should be the same. " +
672 "#{a_tags.inspect} != #{b_tags.inspect}"
677 # update the changeset_id of a node element
678 def update_changeset(xml, changeset_id)
679 xml_attr_rewrite(xml, 'changeset', changeset_id)
683 # update an attribute in the node element
684 def xml_attr_rewrite(xml, name, value)
685 xml.find("//osm/relation").first[name] = value.to_s
692 parser = XML::Parser.string(xml)