source: subversion/sites/rails_port/test/functional/relation_controller_test.rb @ 14862

Last change on this file since 14862 was 14862, checked in by Shaun McDonald, 10 years ago

a fix for the relation controller test

File size: 30.5 KB
Line 
1require File.dirname(__FILE__) + '/../test_helper'
2require 'relation_controller'
3
4class RelationControllerTest < ActionController::TestCase
5  api_fixtures
6
7  # -------------------------------------
8  # Test reading relations.
9  # -------------------------------------
10
11  def test_read
12    # check that a visible relation is returned properly
13    get :read, :id => current_relations(:visible_relation).id
14    assert_response :success
15
16    # check that an invisible relation is not returned
17    get :read, :id => current_relations(:invisible_relation).id
18    assert_response :gone
19
20    # check chat a non-existent relation is not returned
21    get :read, :id => 0
22    assert_response :not_found
23  end
24
25  ##
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 ])
32  end
33
34  def test_relations_for_way
35    check_relations_for_element(:relations_for_way, "way",
36                                current_ways(:used_way).id,
37                                [ :visible_relation ])
38  end
39
40  def test_relations_for_relation
41    check_relations_for_element(:relations_for_relation, "relation",
42                                current_relations(:used_relation).id,
43                                [ :visible_relation ])
44  end
45
46  def check_relations_for_element(method, type, id, expected_relations)
47    # check the "relations for relation" mode
48    get method, :id => id
49    assert_response :success
50
51    # count one osm element
52    assert_select "osm[version=#{API_VERSION}][generator=\"OpenStreetMap server\"]", 1
53
54    # we should have only the expected number of relations
55    assert_select "osm>relation", expected_relations.size
56
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
62    end
63  end
64
65  def test_full
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!
70    if $VERBOSE
71        print @response.body
72    end
73  end
74
75  # -------------------------------------
76  # Test simple relation creation.
77  # -------------------------------------
78
79  def test_create
80    basic_authorization users(:normal_user).email, "test"
81   
82    # put the relation in a dummy fixture changset
83    changeset_id = changesets(:normal_user_first_change).id
84
85    # create an relation without members
86    content "<osm><relation changeset='#{changeset_id}'><tag k='test' v='yes' /></relation></osm>"
87    put :create
88    # hope for forbidden, due to user
89    assert_response :forbidden, 
90    "relation upload should have failed with forbidden"
91
92    ###
93    # create an relation with a node as member
94    # This time try with a role attribute in the relation
95    nid = current_nodes(:used_node_1).id
96    content "<osm><relation changeset='#{changeset_id}'>" +
97      "<member  ref='#{nid}' type='node' role='some'/>" +
98      "<tag k='test' v='yes' /></relation></osm>"
99    put :create
100    # hope for forbidden due to user
101    assert_response :forbidden, 
102    "relation upload did not return forbidden status"
103   
104    ###
105    # create an relation with a node as member, this time test that we don't
106    # need a role attribute to be included
107    nid = current_nodes(:used_node_1).id
108    content "<osm><relation changeset='#{changeset_id}'>" +
109      "<member  ref='#{nid}' type='node'/>"+
110      "<tag k='test' v='yes' /></relation></osm>"
111    put :create
112    # hope for forbidden due to user
113    assert_response :forbidden, 
114    "relation upload did not return forbidden status"
115
116    ###
117    # create an relation with a way and a node as members
118    nid = current_nodes(:used_node_1).id
119    wid = current_ways(:used_way).id
120    content "<osm><relation changeset='#{changeset_id}'>" +
121      "<member type='node' ref='#{nid}' role='some'/>" +
122      "<member type='way' ref='#{wid}' role='other'/>" +
123      "<tag k='test' v='yes' /></relation></osm>"
124    put :create
125    # hope for forbidden, due to user
126    assert_response :forbidden, 
127        "relation upload did not return success status"
128
129
130
131    ## Now try with the public user
132    basic_authorization users(:public_user).email, "test"
133   
134    # put the relation in a dummy fixture changset
135    changeset_id = changesets(:public_user_first_change).id
136
137    # create an relation without members
138    content "<osm><relation changeset='#{changeset_id}'><tag k='test' v='yes' /></relation></osm>"
139    put :create
140    # hope for success
141    assert_response :success, 
142        "relation upload did not return success status"
143    # read id of created relation and search for it
144    relationid = @response.body
145    checkrelation = Relation.find(relationid)
146    assert_not_nil checkrelation, 
147        "uploaded relation not found in data base after upload"
148    # compare values
149    assert_equal checkrelation.members.length, 0, 
150        "saved relation contains members but should not"
151    assert_equal checkrelation.tags.length, 1, 
152        "saved relation does not contain exactly one tag"
153    assert_equal changeset_id, checkrelation.changeset.id,
154        "saved relation does not belong in the changeset it was assigned to"
155    assert_equal users(:public_user).id, checkrelation.changeset.user_id, 
156        "saved relation does not belong to user that created it"
157    assert_equal true, checkrelation.visible, 
158        "saved relation is not visible"
159    # ok the relation is there but can we also retrieve it?
160    get :read, :id => relationid
161    assert_response :success
162
163
164    ###
165    # create an relation with a node as member
166    # This time try with a role attribute in the relation
167    nid = current_nodes(:used_node_1).id
168    content "<osm><relation changeset='#{changeset_id}'>" +
169      "<member  ref='#{nid}' type='node' role='some'/>" +
170      "<tag k='test' v='yes' /></relation></osm>"
171    put :create
172    # hope for success
173    assert_response :success, 
174        "relation upload did not return success status"
175    # read id of created relation and search for it
176    relationid = @response.body
177    checkrelation = Relation.find(relationid)
178    assert_not_nil checkrelation, 
179        "uploaded relation not found in data base after upload"
180    # compare values
181    assert_equal checkrelation.members.length, 1, 
182        "saved relation does not contain exactly one member"
183    assert_equal checkrelation.tags.length, 1, 
184        "saved relation does not contain exactly one tag"
185    assert_equal changeset_id, checkrelation.changeset.id,
186        "saved relation does not belong in the changeset it was assigned to"
187    assert_equal users(:public_user).id, checkrelation.changeset.user_id, 
188        "saved relation does not belong to user that created it"
189    assert_equal true, checkrelation.visible, 
190        "saved relation is not visible"
191    # ok the relation is there but can we also retrieve it?
192   
193    get :read, :id => relationid
194    assert_response :success
195   
196   
197    ###
198    # create an relation with a node as member, this time test that we don't
199    # need a role attribute to be included
200    nid = current_nodes(:used_node_1).id
201    content "<osm><relation changeset='#{changeset_id}'>" +
202      "<member  ref='#{nid}' type='node'/>"+
203      "<tag k='test' v='yes' /></relation></osm>"
204    put :create
205    # hope for success
206    assert_response :success, 
207        "relation upload did not return success status"
208    # read id of created relation and search for it
209    relationid = @response.body
210    checkrelation = Relation.find(relationid)
211    assert_not_nil checkrelation, 
212        "uploaded relation not found in data base after upload"
213    # compare values
214    assert_equal checkrelation.members.length, 1, 
215        "saved relation does not contain exactly one member"
216    assert_equal checkrelation.tags.length, 1, 
217        "saved relation does not contain exactly one tag"
218    assert_equal changeset_id, checkrelation.changeset.id,
219        "saved relation does not belong in the changeset it was assigned to"
220    assert_equal users(:public_user).id, checkrelation.changeset.user_id, 
221        "saved relation does not belong to user that created it"
222    assert_equal true, checkrelation.visible, 
223        "saved relation is not visible"
224    # ok the relation is there but can we also retrieve it?
225   
226    get :read, :id => relationid
227    assert_response :success
228
229    ###
230    # create an relation with a way and a node as members
231    nid = current_nodes(:used_node_1).id
232    wid = current_ways(:used_way).id
233    content "<osm><relation changeset='#{changeset_id}'>" +
234      "<member type='node' ref='#{nid}' role='some'/>" +
235      "<member type='way' ref='#{wid}' role='other'/>" +
236      "<tag k='test' v='yes' /></relation></osm>"
237    put :create
238    # hope for success
239    assert_response :success, 
240        "relation upload did not return success status"
241    # read id of created relation and search for it
242    relationid = @response.body
243    checkrelation = Relation.find(relationid)
244    assert_not_nil checkrelation, 
245        "uploaded relation not found in data base after upload"
246    # compare values
247    assert_equal checkrelation.members.length, 2, 
248        "saved relation does not have exactly two members"
249    assert_equal checkrelation.tags.length, 1, 
250        "saved relation does not contain exactly one tag"
251    assert_equal changeset_id, checkrelation.changeset.id,
252        "saved relation does not belong in the changeset it was assigned to"
253    assert_equal users(:public_user).id, checkrelation.changeset.user_id, 
254        "saved relation does not belong to user that created it"
255    assert_equal true, checkrelation.visible, 
256        "saved relation is not visible"
257    # ok the relation is there but can we also retrieve it?
258    get :read, :id => relationid
259    assert_response :success
260
261  end
262
263  # ------------------------------------
264  # Test updating relations
265  # ------------------------------------
266
267  ##
268  # test that, when tags are updated on a relation, the correct things
269  # happen to the correct tables and the API gives sensible results.
270  # this is to test a case that gregory marler noticed and posted to
271  # josm-dev.
272  ## FIXME Move this to an integration test
273  def test_update_relation_tags
274    basic_authorization "test@example.com", "test"
275    rel_id = current_relations(:multi_tag_relation).id
276    cs_id = changesets(:public_user_first_change).id
277
278    with_relation(rel_id) do |rel|
279      # alter one of the tags
280      tag = rel.find("//osm/relation/tag").first
281      tag['v'] = 'some changed value'
282      update_changeset(rel, cs_id)
283
284      # check that the downloaded tags are the same as the uploaded tags...
285      new_version = with_update(rel) do |new_rel|
286        assert_tags_equal rel, new_rel
287      end
288
289      # check the original one in the current_* table again
290      with_relation(rel_id) { |r| assert_tags_equal rel, r }
291
292      # now check the version in the history
293      with_relation(rel_id, new_version) { |r| assert_tags_equal rel, r }
294    end
295  end
296
297  ##
298  # test that, when tags are updated on a relation when using the diff
299  # upload function, the correct things happen to the correct tables
300  # and the API gives sensible results. this is to test a case that
301  # gregory marler noticed and posted to josm-dev.
302  def test_update_relation_tags_via_upload
303    basic_authorization users(:public_user).email, "test"
304    rel_id = current_relations(:multi_tag_relation).id
305    cs_id = changesets(:public_user_first_change).id
306
307    with_relation(rel_id) do |rel|
308      # alter one of the tags
309      tag = rel.find("//osm/relation/tag").first
310      tag['v'] = 'some changed value'
311      update_changeset(rel, cs_id)
312
313      # check that the downloaded tags are the same as the uploaded tags...
314      new_version = with_update_diff(rel) do |new_rel|
315        assert_tags_equal rel, new_rel
316      end
317
318      # check the original one in the current_* table again
319      with_relation(rel_id) { |r| assert_tags_equal rel, r }
320
321      # now check the version in the history
322      with_relation(rel_id, new_version) { |r| assert_tags_equal rel, r }
323    end
324  end
325
326  # -------------------------------------
327  # Test creating some invalid relations.
328  # -------------------------------------
329
330  def test_create_invalid
331    basic_authorization users(:public_user).email, "test"
332
333    # put the relation in a dummy fixture changset
334    changeset_id = changesets(:public_user_first_change).id
335
336    # create a relation with non-existing node as member
337    content "<osm><relation changeset='#{changeset_id}'>" +
338      "<member type='node' ref='0'/><tag k='test' v='yes' />" +
339      "</relation></osm>"
340    put :create
341    # expect failure
342    assert_response :precondition_failed, 
343        "relation upload with invalid node did not return 'precondition failed'"
344  end
345
346  # -------------------------------------
347  # Test creating a relation, with some invalid XML
348  # -------------------------------------
349  def test_create_invalid_xml
350    basic_authorization users(:public_user).email, "test"
351   
352    # put the relation in a dummy fixture changeset that works
353    changeset_id = changesets(:public_user_first_change).id
354   
355    # create some xml that should return an error
356    content "<osm><relation changeset='#{changeset_id}'>" +
357    "<member type='type' ref='#{current_nodes(:used_node_1).id}' role=''/>" +
358    "<tag k='tester' v='yep'/></relation></osm>"
359    put :create
360    # expect failure
361    assert_response :bad_request
362    assert_match(/Cannot parse valid relation from xml string/, @response.body)
363    assert_match(/The type is not allowed only, /, @response.body)
364  end
365 
366 
367  # -------------------------------------
368  # Test deleting relations.
369  # -------------------------------------
370 
371  def test_delete
372    ## First try to delete relation without auth
373    delete :delete, :id => current_relations(:visible_relation).id
374    assert_response :unauthorized
375   
376   
377    ## Then try with the private user, to make sure that you get a forbidden
378    basic_authorization(users(:normal_user).email, "test")
379   
380    # this shouldn't work, as we should need the payload...
381    delete :delete, :id => current_relations(:visible_relation).id
382    assert_response :forbidden
383
384    # try to delete without specifying a changeset
385    content "<osm><relation id='#{current_relations(:visible_relation).id}'/></osm>"
386    delete :delete, :id => current_relations(:visible_relation).id
387    assert_response :forbidden
388
389    # try to delete with an invalid (closed) changeset
390    content update_changeset(current_relations(:visible_relation).to_xml,
391                             changesets(:normal_user_closed_change).id)
392    delete :delete, :id => current_relations(:visible_relation).id
393    assert_response :forbidden
394
395    # try to delete with an invalid (non-existent) changeset
396    content update_changeset(current_relations(:visible_relation).to_xml,0)
397    delete :delete, :id => current_relations(:visible_relation).id
398    assert_response :forbidden
399
400    # this won't work because the relation is in-use by another relation
401    content(relations(:used_relation).to_xml)
402    delete :delete, :id => current_relations(:used_relation).id
403    assert_response :forbidden
404
405    # this should work when we provide the appropriate payload...
406    content(relations(:visible_relation).to_xml)
407    delete :delete, :id => current_relations(:visible_relation).id
408    assert_response :forbidden
409
410    # this won't work since the relation is already deleted
411    content(relations(:invisible_relation).to_xml)
412    delete :delete, :id => current_relations(:invisible_relation).id
413    assert_response :forbidden
414
415    # this works now because the relation which was using this one
416    # has been deleted.
417    content(relations(:used_relation).to_xml)
418    delete :delete, :id => current_relations(:used_relation).id
419    assert_response :forbidden
420
421    # this won't work since the relation never existed
422    delete :delete, :id => 0
423    assert_response :forbidden
424
425   
426
427    ## now set auth for the public user
428    basic_authorization(users(:public_user).email, "test"); 
429
430    # this shouldn't work, as we should need the payload...
431    delete :delete, :id => current_relations(:visible_relation).id
432    assert_response :bad_request
433
434    # try to delete without specifying a changeset
435    content "<osm><relation id='#{current_relations(:visible_relation).id}'/></osm>"
436    delete :delete, :id => current_relations(:visible_relation).id
437    assert_response :bad_request
438    assert_match(/You are missing the required changeset in the relation/, @response.body)
439
440    # try to delete with an invalid (closed) changeset
441    content update_changeset(current_relations(:visible_relation).to_xml,
442                             changesets(:normal_user_closed_change).id)
443    delete :delete, :id => current_relations(:visible_relation).id
444    assert_response :conflict
445
446    # try to delete with an invalid (non-existent) changeset
447    content update_changeset(current_relations(:visible_relation).to_xml,0)
448    delete :delete, :id => current_relations(:visible_relation).id
449    assert_response :conflict
450
451    # this won't work because the relation is in a changeset owned by someone else
452    content(relations(:used_relation).to_xml)
453    delete :delete, :id => current_relations(:used_relation).id
454    assert_response :conflict, 
455    "shouldn't be able to delete a relation in a changeset owned by someone else (#{@response.body})"
456
457    # this won't work because the relation in the payload is different to that passed
458    content(relations(:public_used_relation).to_xml)
459    delete :delete, :id => current_relations(:used_relation).id
460    assert_not_equal relations(:public_used_relation).id, current_relations(:used_relation).id
461    assert_response :bad_request, "shouldn't be able to delete a relation when payload is different to the url"
462   
463    # this won't work because the relation is in-use by another relation
464    content(relations(:public_used_relation).to_xml)
465    delete :delete, :id => current_relations(:public_used_relation).id
466    assert_response :precondition_failed, 
467       "shouldn't be able to delete a relation used in a relation (#{@response.body})"
468
469    # this should work when we provide the appropriate payload...
470    content(relations(:multi_tag_relation).to_xml)
471    delete :delete, :id => current_relations(:multi_tag_relation).id
472    assert_response :success
473
474    # valid delete should return the new version number, which should
475    # be greater than the old version number
476    assert @response.body.to_i > current_relations(:visible_relation).version,
477       "delete request should return a new version number for relation"
478
479    # this won't work since the relation is already deleted
480    content(relations(:invisible_relation).to_xml)
481    delete :delete, :id => current_relations(:invisible_relation).id
482    assert_response :gone
483   
484    # Public visible relation needs to be deleted
485    content(relations(:public_visible_relation).to_xml)
486    delete :delete, :id => current_relations(:public_visible_relation).id
487    assert_response :success
488
489    # this works now because the relation which was using this one
490    # has been deleted.
491    content(relations(:public_used_relation).to_xml)
492    delete :delete, :id => current_relations(:public_used_relation).id
493    assert_response :success, 
494       "should be able to delete a relation used in an old relation (#{@response.body})"
495
496    # this won't work since the relation never existed
497    delete :delete, :id => 0
498    assert_response :not_found
499  end
500
501  ##
502  # when a relation's tag is modified then it should put the bounding
503  # box of all its members into the changeset.
504  def test_tag_modify_bounding_box
505    # in current fixtures, relation 5 contains nodes 3 and 5 (node 3
506    # indirectly via way 3), so the bbox should be [3,3,5,5].
507    check_changeset_modify([3,3,5,5]) do |changeset_id|
508      # add a tag to an existing relation
509      relation_xml = current_relations(:visible_relation).to_xml
510      relation_element = relation_xml.find("//osm/relation").first
511      new_tag = XML::Node.new("tag")
512      new_tag['k'] = "some_new_tag"
513      new_tag['v'] = "some_new_value"
514      relation_element << new_tag
515     
516      # update changeset ID to point to new changeset
517      update_changeset(relation_xml, changeset_id)
518     
519      # upload the change
520      content relation_xml
521      put :update, :id => current_relations(:visible_relation).id
522      assert_response :success, "can't update relation for tag/bbox test"
523    end
524  end
525
526  ##
527  # add a member to a relation and check the bounding box is only that
528  # element.
529  def test_add_member_bounding_box
530    check_changeset_modify([4,4,4,4]) do |changeset_id|
531      # add node 4 (4,4) to an existing relation
532      relation_xml = current_relations(:visible_relation).to_xml
533      relation_element = relation_xml.find("//osm/relation").first
534      new_member = XML::Node.new("member")
535      new_member['ref'] = current_nodes(:used_node_2).id.to_s
536      new_member['type'] = "node"
537      new_member['role'] = "some_role"
538      relation_element << new_member
539     
540      # update changeset ID to point to new changeset
541      update_changeset(relation_xml, changeset_id)
542     
543      # upload the change
544      content relation_xml
545      put :update, :id => current_relations(:visible_relation).id
546      assert_response :success, "can't update relation for add node/bbox test"
547    end
548  end
549 
550  ##
551  # remove a member from a relation and check the bounding box is
552  # only that element.
553  def test_remove_member_bounding_box
554    check_changeset_modify([5,5,5,5]) do |changeset_id|
555      # remove node 5 (5,5) from an existing relation
556      relation_xml = current_relations(:visible_relation).to_xml
557      relation_xml.
558        find("//osm/relation/member[@type='node'][@ref='5']").
559        first.remove!
560     
561      # update changeset ID to point to new changeset
562      update_changeset(relation_xml, changeset_id)
563     
564      # upload the change
565      content relation_xml
566      put :update, :id => current_relations(:visible_relation).id
567      assert_response :success, "can't update relation for remove node/bbox test"
568    end
569  end
570 
571  ##
572  # check that relations are ordered
573  def test_relation_member_ordering
574    basic_authorization(users(:public_user).email, "test")
575   
576    doc_str = <<OSM
577<osm>
578 <relation changeset='4'>
579  <member ref='1' type='node' role='first'/>
580  <member ref='3' type='node' role='second'/>
581  <member ref='1' type='way' role='third'/>
582  <member ref='3' type='way' role='fourth'/>
583 </relation>
584</osm>
585OSM
586    doc = XML::Parser.string(doc_str).parse
587
588    content doc
589    put :create
590    assert_response :success, "can't create a relation: #{@response.body}"
591    relation_id = @response.body.to_i
592
593    # get it back and check the ordering
594    get :read, :id => relation_id
595    assert_response :success, "can't read back the relation: #{@response.body}"
596    check_ordering(doc, @response.body)
597
598    # insert a member at the front
599    new_member = XML::Node.new "member"
600    new_member['ref'] = 5.to_s
601    new_member['type'] = 'node'
602    new_member['role'] = 'new first'
603    doc.find("//osm/relation").first.child.prev = new_member
604    # update the version, should be 1?
605    doc.find("//osm/relation").first['id'] = relation_id.to_s
606    doc.find("//osm/relation").first['version'] = 1.to_s
607
608    # upload the next version of the relation
609    content doc
610    put :update, :id => relation_id
611    assert_response :success, "can't update relation: #{@response.body}"
612    new_version = @response.body.to_i
613
614    # get it back again and check the ordering again
615    get :read, :id => relation_id
616    assert_response :success, "can't read back the relation: #{@response.body}"
617    check_ordering(doc, @response.body)
618  end
619
620  ##
621  # check that relations can contain duplicate members
622  def test_relation_member_duplicates
623    ## First try with the private user
624    basic_authorization(users(:normal_user).email, "test"); 
625
626    doc_str = <<OSM
627<osm>
628 <relation changeset='4'>
629  <member ref='1' type='node' role='forward'/>
630  <member ref='3' type='node' role='forward'/>
631  <member ref='1' type='node' role='forward'/>
632  <member ref='3' type='node' role='forward'/>
633 </relation>
634</osm>
635OSM
636    doc = XML::Parser.string(doc_str).parse
637
638    content doc
639    put :create
640    assert_response :forbidden
641
642
643    ## Now try with the public user
644    basic_authorization(users(:public_user).email, "test"); 
645
646    doc_str = <<OSM
647<osm>
648 <relation changeset='4'>
649  <member ref='1' type='node' role='forward'/>
650  <member ref='3' type='node' role='forward'/>
651  <member ref='1' type='node' role='forward'/>
652  <member ref='3' type='node' role='forward'/>
653 </relation>
654</osm>
655OSM
656    doc = XML::Parser.string(doc_str).parse
657
658    content doc
659    put :create
660    assert_response :success, "can't create a relation: #{@response.body}"
661    relation_id = @response.body.to_i
662
663    # get it back and check the ordering
664    get :read, :id => relation_id
665    assert_response :success, "can't read back the relation: #{@response.body}"
666    check_ordering(doc, @response.body)
667  end
668
669  # ============================================================
670  # utility functions
671  # ============================================================
672
673  ##
674  # checks that the XML document and the string arguments have
675  # members in the same order.
676  def check_ordering(doc, xml)
677    new_doc = XML::Parser.string(xml).parse
678
679    doc_members = doc.find("//osm/relation/member").collect do |m|
680      [m['ref'].to_i, m['type'].to_sym, m['role']]
681    end
682
683    new_members = new_doc.find("//osm/relation/member").collect do |m|
684      [m['ref'].to_i, m['type'].to_sym, m['role']]
685    end
686
687    doc_members.zip(new_members).each do |d, n|
688      assert_equal d, n, "members are not equal - ordering is wrong? (#{doc}, #{xml})"
689    end
690  end
691
692  ##
693  # create a changeset and yield to the caller to set it up, then assert
694  # that the changeset bounding box is +bbox+.
695  def check_changeset_modify(bbox)
696    ## First test with the private user to check that you get a forbidden
697    basic_authorization(users(:normal_user).email, "test"); 
698
699    # create a new changeset for this operation, so we are assured
700    # that the bounding box will be newly-generated.
701    changeset_id = with_controller(ChangesetController.new) do
702      content "<osm><changeset/></osm>"
703      put :create
704      assert_response :forbidden, "shouldn't be able to create changeset for modify test, as should get forbidden"
705    end
706
707   
708    ## Now do the whole thing with the public user
709    basic_authorization(users(:public_user).email, "test")
710   
711    # create a new changeset for this operation, so we are assured
712    # that the bounding box will be newly-generated.
713    changeset_id = with_controller(ChangesetController.new) do
714      content "<osm><changeset/></osm>"
715      put :create
716      assert_response :success, "couldn't create changeset for modify test"
717      @response.body.to_i
718    end
719
720    # go back to the block to do the actual modifies
721    yield changeset_id
722
723    # now download the changeset to check its bounding box
724    with_controller(ChangesetController.new) do
725      get :read, :id => changeset_id
726      assert_response :success, "can't re-read changeset for modify test"
727      assert_select "osm>changeset", 1
728      assert_select "osm>changeset[id=#{changeset_id}]", 1
729      assert_select "osm>changeset[min_lon=#{bbox[0].to_f}]", 1
730      assert_select "osm>changeset[min_lat=#{bbox[1].to_f}]", 1
731      assert_select "osm>changeset[max_lon=#{bbox[2].to_f}]", 1
732      assert_select "osm>changeset[max_lat=#{bbox[3].to_f}]", 1
733    end
734  end
735
736  ##
737  # yields the relation with the given +id+ (and optional +version+
738  # to read from the history tables) into the block. the parsed XML
739  # doc is returned.
740  def with_relation(id, ver = nil)
741    if ver.nil?
742      get :read, :id => id
743    else
744      with_controller(OldRelationController.new) do
745        get :version, :id => id, :version => ver
746      end
747    end
748    assert_response :success
749    yield xml_parse(@response.body)
750  end
751
752  ##
753  # updates the relation (XML) +rel+ and
754  # yields the new version of that relation into the block.
755  # the parsed XML doc is retured.
756  def with_update(rel)
757    rel_id = rel.find("//osm/relation").first["id"].to_i
758    content rel
759    put :update, :id => rel_id
760    assert_response :success, "can't update relation: #{@response.body}"
761    version = @response.body.to_i
762
763    # now get the new version
764    get :read, :id => rel_id
765    assert_response :success
766    new_rel = xml_parse(@response.body)
767
768    yield new_rel
769
770    return version
771  end
772
773  ##
774  # updates the relation (XML) +rel+ via the diff-upload API and
775  # yields the new version of that relation into the block.
776  # the parsed XML doc is retured.
777  def with_update_diff(rel)
778    rel_id = rel.find("//osm/relation").first["id"].to_i
779    cs_id = rel.find("//osm/relation").first['changeset'].to_i
780    version = nil
781
782    with_controller(ChangesetController.new) do
783      doc = OSM::API.new.get_xml_doc
784      change = XML::Node.new 'osmChange'
785      doc.root = change
786      modify = XML::Node.new 'modify'
787      change << modify
788      modify << doc.import(rel.find("//osm/relation").first)
789
790      content doc.to_s
791      post :upload, :id => cs_id
792      assert_response :success, "can't upload diff relation: #{@response.body}"
793      version = xml_parse(@response.body).find("//diffResult/relation").first["new_version"].to_i
794    end     
795   
796    # now get the new version
797    get :read, :id => rel_id
798    assert_response :success
799    new_rel = xml_parse(@response.body)
800   
801    yield new_rel
802   
803    return version
804  end
805
806  ##
807  # returns a k->v hash of tags from an xml doc
808  def get_tags_as_hash(a) 
809    a.find("//osm/relation/tag").inject({}) do |h,v|
810      h[v['k']] = v['v']
811      h
812    end
813  end
814 
815  ##
816  # assert that all tags on relation documents +a+ and +b+
817  # are equal
818  def assert_tags_equal(a, b)
819    # turn the XML doc into tags hashes
820    a_tags = get_tags_as_hash(a)
821    b_tags = get_tags_as_hash(b)
822
823    assert_equal a_tags.keys, b_tags.keys, "Tag keys should be identical."
824    a_tags.each do |k, v|
825      assert_equal v, b_tags[k], 
826        "Tags which were not altered should be the same. " +
827        "#{a_tags.inspect} != #{b_tags.inspect}"
828    end
829  end
830
831  ##
832  # update the changeset_id of a node element
833  def update_changeset(xml, changeset_id)
834    xml_attr_rewrite(xml, 'changeset', changeset_id)
835  end
836
837  ##
838  # update an attribute in the node element
839  def xml_attr_rewrite(xml, name, value)
840    xml.find("//osm/relation").first[name] = value.to_s
841    return xml
842  end
843
844  ##
845  # parse some xml
846  def xml_parse(xml)
847    parser = XML::Parser.string(xml)
848    parser.parse
849  end
850end
Note: See TracBrowser for help on using the repository browser.