]> git.openstreetmap.org Git - rails.git/blob - test/functional/relation_controller_test.rb
Test and fix for #1567, for teh betterer osmChange compliance.
[rails.git] / test / functional / relation_controller_test.rb
1 require File.dirname(__FILE__) + '/../test_helper'
2 require 'relation_controller'
3
4 class RelationControllerTest < ActionController::TestCase
5   api_fixtures
6
7   def basic_authorization(user, pass)
8     @request.env["HTTP_AUTHORIZATION"] = "Basic %s" % Base64.encode64("#{user}:#{pass}")
9   end
10
11   def content(c)
12     @request.env["RAW_POST_DATA"] = c.to_s
13   end
14
15   # -------------------------------------
16   # Test reading relations.
17   # -------------------------------------
18
19   def test_read
20     # check that a visible relation is returned properly
21     get :read, :id => current_relations(:visible_relation).id
22     assert_response :success
23
24     # check that an invisible relation is not returned
25     get :read, :id => current_relations(:invisible_relation).id
26     assert_response :gone
27
28     # check chat a non-existent relation is not returned
29     get :read, :id => 0
30     assert_response :not_found
31   end
32
33   ##
34   # check that all relations containing a particular node, and no extra
35   # relations, are returned from the relations_for_node call.
36   def test_relations_for_node
37     check_relations_for_element(:relations_for_node, "node", 
38                                 current_nodes(:node_used_by_relationship).id,
39                                 [ :visible_relation, :used_relation ])
40   end
41
42   def test_relations_for_way
43     check_relations_for_element(:relations_for_way, "way",
44                                 current_ways(:used_way).id,
45                                 [ :visible_relation ])
46   end
47
48   def test_relations_for_relation
49     check_relations_for_element(:relations_for_relation, "relation",
50                                 current_relations(:used_relation).id,
51                                 [ :visible_relation ])
52   end
53
54   def check_relations_for_element(method, type, id, expected_relations)
55     # check the "relations for relation" mode
56     get method, :id => id
57     assert_response :success
58
59     # count one osm element
60     assert_select "osm[version=#{API_VERSION}][generator=\"OpenStreetMap server\"]", 1
61
62     # we should have only the expected number of relations
63     assert_select "osm>relation", expected_relations.size
64
65     # and each of them should contain the node we originally searched for
66     expected_relations.each do |r|
67       relation_id = current_relations(r).id
68       assert_select "osm>relation#?", relation_id
69       assert_select "osm>relation#?>member[type=\"#{type}\"][ref=#{id}]", relation_id
70     end
71   end
72
73   def test_full
74     # check the "full" mode
75     get :full, :id => current_relations(:visible_relation).id
76     assert_response :success
77     # FIXME check whether this contains the stuff we want!
78     if $VERBOSE
79         print @response.body
80     end
81   end
82
83   # -------------------------------------
84   # Test simple relation creation.
85   # -------------------------------------
86
87   def test_create
88     basic_authorization "test@openstreetmap.org", "test"
89     
90     # put the relation in a dummy fixture changset
91     changeset_id = changesets(:normal_user_first_change).id
92
93     # create an relation without members
94     content "<osm><relation changeset='#{changeset_id}'><tag k='test' v='yes' /></relation></osm>"
95     put :create
96     # hope for success
97     assert_response :success, 
98         "relation upload did not return success status"
99     # read id of created relation and search for it
100     relationid = @response.body
101     checkrelation = Relation.find(relationid)
102     assert_not_nil checkrelation, 
103         "uploaded relation not found in data base after upload"
104     # compare values
105     assert_equal checkrelation.members.length, 0, 
106         "saved relation contains members but should not"
107     assert_equal checkrelation.tags.length, 1, 
108         "saved relation does not contain exactly one tag"
109     assert_equal changeset_id, checkrelation.changeset.id,
110         "saved relation does not belong in the changeset it was assigned to"
111     assert_equal users(:normal_user).id, checkrelation.changeset.user_id, 
112         "saved relation does not belong to user that created it"
113     assert_equal true, checkrelation.visible, 
114         "saved relation is not visible"
115     # ok the relation is there but can we also retrieve it?
116     get :read, :id => relationid
117     assert_response :success
118
119
120     ###
121     # create an relation with a node as member
122     # This time try with a role attribute in the relation
123     nid = current_nodes(:used_node_1).id
124     content "<osm><relation changeset='#{changeset_id}'>" +
125       "<member  ref='#{nid}' type='node' role='some'/>" +
126       "<tag k='test' v='yes' /></relation></osm>"
127     put :create
128     # hope for success
129     assert_response :success, 
130         "relation upload did not return success status"
131     # read id of created relation and search for it
132     relationid = @response.body
133     checkrelation = Relation.find(relationid)
134     assert_not_nil checkrelation, 
135         "uploaded relation not found in data base after upload"
136     # compare values
137     assert_equal checkrelation.members.length, 1, 
138         "saved relation does not contain exactly one member"
139     assert_equal checkrelation.tags.length, 1, 
140         "saved relation does not contain exactly one tag"
141     assert_equal changeset_id, checkrelation.changeset.id,
142         "saved relation does not belong in the changeset it was assigned to"
143     assert_equal users(:normal_user).id, checkrelation.changeset.user_id, 
144         "saved relation does not belong to user that created it"
145     assert_equal true, checkrelation.visible, 
146         "saved relation is not visible"
147     # ok the relation is there but can we also retrieve it?
148     
149     get :read, :id => relationid
150     assert_response :success
151     
152     
153     ###
154     # create an relation with a node as member, this time test that we don't 
155     # need a role attribute to be included
156     nid = current_nodes(:used_node_1).id
157     content "<osm><relation changeset='#{changeset_id}'>" +
158       "<member  ref='#{nid}' type='node'/>"+
159       "<tag k='test' v='yes' /></relation></osm>"
160     put :create
161     # hope for success
162     assert_response :success, 
163         "relation upload did not return success status"
164     # read id of created relation and search for it
165     relationid = @response.body
166     checkrelation = Relation.find(relationid)
167     assert_not_nil checkrelation, 
168         "uploaded relation not found in data base after upload"
169     # compare values
170     assert_equal checkrelation.members.length, 1, 
171         "saved relation does not contain exactly one member"
172     assert_equal checkrelation.tags.length, 1, 
173         "saved relation does not contain exactly one tag"
174     assert_equal changeset_id, checkrelation.changeset.id,
175         "saved relation does not belong in the changeset it was assigned to"
176     assert_equal users(:normal_user).id, checkrelation.changeset.user_id, 
177         "saved relation does not belong to user that created it"
178     assert_equal true, checkrelation.visible, 
179         "saved relation is not visible"
180     # ok the relation is there but can we also retrieve it?
181     
182     get :read, :id => relationid
183     assert_response :success
184
185     ###
186     # create an relation with a way and a node as members
187     nid = current_nodes(:used_node_1).id
188     wid = current_ways(:used_way).id
189     content "<osm><relation changeset='#{changeset_id}'>" +
190       "<member type='node' ref='#{nid}' role='some'/>" +
191       "<member type='way' ref='#{wid}' role='other'/>" +
192       "<tag k='test' v='yes' /></relation></osm>"
193     put :create
194     # hope for success
195     assert_response :success, 
196         "relation upload did not return success status"
197     # read id of created relation and search for it
198     relationid = @response.body
199     checkrelation = Relation.find(relationid)
200     assert_not_nil checkrelation, 
201         "uploaded relation not found in data base after upload"
202     # compare values
203     assert_equal checkrelation.members.length, 2, 
204         "saved relation does not have exactly two members"
205     assert_equal checkrelation.tags.length, 1, 
206         "saved relation does not contain exactly one tag"
207     assert_equal changeset_id, checkrelation.changeset.id,
208         "saved relation does not belong in the changeset it was assigned to"
209     assert_equal users(:normal_user).id, checkrelation.changeset.user_id, 
210         "saved relation does not belong to user that created it"
211     assert_equal true, checkrelation.visible, 
212         "saved relation is not visible"
213     # ok the relation is there but can we also retrieve it?
214     get :read, :id => relationid
215     assert_response :success
216
217   end
218
219   # -------------------------------------
220   # Test creating some invalid relations.
221   # -------------------------------------
222
223   def test_create_invalid
224     basic_authorization "test@openstreetmap.org", "test"
225
226     # put the relation in a dummy fixture changset
227     changeset_id = changesets(:normal_user_first_change).id
228
229     # create a relation with non-existing node as member
230     content "<osm><relation changeset='#{changeset_id}'>" +
231       "<member type='node' ref='0'/><tag k='test' v='yes' />" +
232       "</relation></osm>"
233     put :create
234     # expect failure
235     assert_response :precondition_failed, 
236         "relation upload with invalid node did not return 'precondition failed'"
237   end
238
239   # -------------------------------------
240   # Test creating a relation, with some invalid XML
241   # -------------------------------------
242   def test_create_invalid_xml
243     basic_authorization "test@openstreetmap.org", "test"
244     
245     # put the relation in a dummy fixture changeset that works
246     changeset_id = changesets(:normal_user_first_change).id
247     
248     # create some xml that should return an error
249     content "<osm><relation changeset='#{changeset_id}'>" +
250     "<member type='type' ref='#{current_nodes(:used_node_1).id}' role=''/>" +
251     "<tag k='tester' v='yep'/></relation></osm>"
252     put :create
253     # expect failure
254     assert_response :bad_request
255     assert_match(/Cannot parse valid relation from xml string/, @response.body)
256     assert_match(/The type is not allowed only, /, @response.body)
257   end
258   
259   
260   # -------------------------------------
261   # Test deleting relations.
262   # -------------------------------------
263   
264   def test_delete
265     # first try to delete relation without auth
266     delete :delete, :id => current_relations(:visible_relation).id
267     assert_response :unauthorized
268
269     # now set auth
270     basic_authorization("test@openstreetmap.org", "test");  
271
272     # this shouldn't work, as we should need the payload...
273     delete :delete, :id => current_relations(:visible_relation).id
274     assert_response :bad_request
275
276     # try to delete without specifying a changeset
277     content "<osm><relation id='#{current_relations(:visible_relation).id}'/></osm>"
278     delete :delete, :id => current_relations(:visible_relation).id
279     assert_response :bad_request
280     assert_match(/You are missing the required changeset in the relation/, @response.body)
281
282     # try to delete with an invalid (closed) changeset
283     content update_changeset(current_relations(:visible_relation).to_xml,
284                              changesets(:normal_user_closed_change).id)
285     delete :delete, :id => current_relations(:visible_relation).id
286     assert_response :conflict
287
288     # try to delete with an invalid (non-existent) changeset
289     content update_changeset(current_relations(:visible_relation).to_xml,0)
290     delete :delete, :id => current_relations(:visible_relation).id
291     assert_response :conflict
292
293     # this won't work because the relation is in-use by another relation
294     content(relations(:used_relation).to_xml)
295     delete :delete, :id => current_relations(:used_relation).id
296     assert_response :precondition_failed, 
297        "shouldn't be able to delete a relation used in a relation (#{@response.body})"
298
299     # this should work when we provide the appropriate payload...
300     content(relations(:visible_relation).to_xml)
301     delete :delete, :id => current_relations(:visible_relation).id
302     assert_response :success
303
304     # valid delete should return the new version number, which should
305     # be greater than the old version number
306     assert @response.body.to_i > current_relations(:visible_relation).version,
307        "delete request should return a new version number for relation"
308
309     # this won't work since the relation is already deleted
310     content(relations(:invisible_relation).to_xml)
311     delete :delete, :id => current_relations(:invisible_relation).id
312     assert_response :gone
313
314     # this works now because the relation which was using this one 
315     # has been deleted.
316     content(relations(:used_relation).to_xml)
317     delete :delete, :id => current_relations(:used_relation).id
318     assert_response :success, 
319        "should be able to delete a relation used in an old relation (#{@response.body})"
320
321     # this won't work since the relation never existed
322     delete :delete, :id => 0
323     assert_response :not_found
324   end
325
326   ##
327   # when a relation's tag is modified then it should put the bounding
328   # box of all its members into the changeset.
329   def test_tag_modify_bounding_box
330     # in current fixtures, relation 5 contains nodes 3 and 5 (node 3
331     # indirectly via way 3), so the bbox should be [3,3,5,5].
332     check_changeset_modify([3,3,5,5]) do |changeset_id|
333       # add a tag to an existing relation
334       relation_xml = current_relations(:visible_relation).to_xml
335       relation_element = relation_xml.find("//osm/relation").first
336       new_tag = XML::Node.new("tag")
337       new_tag['k'] = "some_new_tag"
338       new_tag['v'] = "some_new_value"
339       relation_element << new_tag
340       
341       # update changeset ID to point to new changeset
342       update_changeset(relation_xml, changeset_id)
343       
344       # upload the change
345       content relation_xml
346       put :update, :id => current_relations(:visible_relation).id
347       assert_response :success, "can't update relation for tag/bbox test"
348     end
349   end
350
351   ##
352   # add a member to a relation and check the bounding box is only that
353   # element.
354   def test_add_member_bounding_box
355     check_changeset_modify([4,4,4,4]) do |changeset_id|
356       # add node 4 (4,4) to an existing relation
357       relation_xml = current_relations(:visible_relation).to_xml
358       relation_element = relation_xml.find("//osm/relation").first
359       new_member = XML::Node.new("member")
360       new_member['ref'] = current_nodes(:used_node_2).id.to_s
361       new_member['type'] = "node"
362       new_member['role'] = "some_role"
363       relation_element << new_member
364       
365       # update changeset ID to point to new changeset
366       update_changeset(relation_xml, changeset_id)
367       
368       # upload the change
369       content relation_xml
370       put :update, :id => current_relations(:visible_relation).id
371       assert_response :success, "can't update relation for add node/bbox test"
372     end
373   end
374   
375   ##
376   # remove a member from a relation and check the bounding box is 
377   # only that element.
378   def test_remove_member_bounding_box
379     check_changeset_modify([5,5,5,5]) do |changeset_id|
380       # remove node 5 (5,5) from an existing relation
381       relation_xml = current_relations(:visible_relation).to_xml
382       relation_xml.
383         find("//osm/relation/member[@type='node'][@ref='5']").
384         first.remove!
385       
386       # update changeset ID to point to new changeset
387       update_changeset(relation_xml, changeset_id)
388       
389       # upload the change
390       content relation_xml
391       put :update, :id => current_relations(:visible_relation).id
392       assert_response :success, "can't update relation for remove node/bbox test"
393     end
394   end
395   
396   ##
397   # check that relations are ordered
398   def test_relation_member_ordering
399     basic_authorization("test@openstreetmap.org", "test");  
400
401     doc_str = <<OSM
402 <osm>
403  <relation changeset='1'>
404   <member ref='1' type='node' role='first'/>
405   <member ref='3' type='node' role='second'/>
406   <member ref='1' type='way' role='third'/>
407   <member ref='3' type='way' role='fourth'/>
408  </relation>
409 </osm>
410 OSM
411     doc = XML::Parser.string(doc_str).parse
412
413     content doc
414     put :create
415     assert_response :success, "can't create a relation: #{@response.body}"
416     relation_id = @response.body.to_i
417
418     # get it back and check the ordering
419     get :read, :id => relation_id
420     assert_response :success, "can't read back the relation: #{@response.body}"
421     check_ordering(doc, @response.body)
422
423     # insert a member at the front
424     new_member = XML::Node.new "member"
425     new_member['ref'] = 5.to_s
426     new_member['type'] = 'node'
427     new_member['role'] = 'new first'
428     doc.find("//osm/relation").first.child.prev = new_member
429     # update the version, should be 1?
430     doc.find("//osm/relation").first['id'] = relation_id.to_s
431     doc.find("//osm/relation").first['version'] = 1.to_s
432
433     # upload the next version of the relation
434     content doc
435     put :update, :id => relation_id
436     assert_response :success, "can't update relation: #{@response.body}"
437     new_version = @response.body.to_i
438
439     # get it back again and check the ordering again
440     get :read, :id => relation_id
441     assert_response :success, "can't read back the relation: #{@response.body}"
442     check_ordering(doc, @response.body)
443   end
444
445   ## 
446   # check that relations can contain duplicate members
447   def test_relation_member_duplicates
448     basic_authorization("test@openstreetmap.org", "test");  
449
450     doc_str = <<OSM
451 <osm>
452  <relation changeset='1'>
453   <member ref='1' type='node' role='forward'/>
454   <member ref='3' type='node' role='forward'/>
455   <member ref='1' type='node' role='forward'/>
456   <member ref='3' type='node' role='forward'/>
457  </relation>
458 </osm>
459 OSM
460     doc = XML::Parser.string(doc_str).parse
461
462     content doc
463     put :create
464     assert_response :success, "can't create a relation: #{@response.body}"
465     relation_id = @response.body.to_i
466
467     # get it back and check the ordering
468     get :read, :id => relation_id
469     assert_response :success, "can't read back the relation: #{@response.body}"
470     check_ordering(doc, @response.body)
471   end
472
473   # ============================================================
474   # utility functions
475   # ============================================================
476
477   ##
478   # checks that the XML document and the string arguments have
479   # members in the same order.
480   def check_ordering(doc, xml)
481     new_doc = XML::Parser.string(xml).parse
482
483     doc_members = doc.find("//osm/relation/member").collect do |m|
484       [m['ref'].to_i, m['type'].to_sym, m['role']]
485     end
486
487     new_members = new_doc.find("//osm/relation/member").collect do |m|
488       [m['ref'].to_i, m['type'].to_sym, m['role']]
489     end
490
491     doc_members.zip(new_members).each do |d, n|
492       assert_equal d, n, "members are not equal - ordering is wrong? (#{doc}, #{xml})"
493     end
494   end
495
496   ##
497   # create a changeset and yield to the caller to set it up, then assert
498   # that the changeset bounding box is +bbox+.
499   def check_changeset_modify(bbox)
500     basic_authorization("test@openstreetmap.org", "test");  
501
502     # create a new changeset for this operation, so we are assured
503     # that the bounding box will be newly-generated.
504     changeset_id = with_controller(ChangesetController.new) do
505       content "<osm><changeset/></osm>"
506       put :create
507       assert_response :success, "couldn't create changeset for modify test"
508       @response.body.to_i
509     end
510
511     # go back to the block to do the actual modifies
512     yield changeset_id
513
514     # now download the changeset to check its bounding box
515     with_controller(ChangesetController.new) do
516       get :read, :id => changeset_id
517       assert_response :success, "can't re-read changeset for modify test"
518       assert_select "osm>changeset", 1
519       assert_select "osm>changeset[id=#{changeset_id}]", 1
520       assert_select "osm>changeset[min_lon=#{bbox[0].to_f}]", 1
521       assert_select "osm>changeset[min_lat=#{bbox[1].to_f}]", 1
522       assert_select "osm>changeset[max_lon=#{bbox[2].to_f}]", 1
523       assert_select "osm>changeset[max_lat=#{bbox[3].to_f}]", 1
524     end
525   end
526
527   ##
528   # update the changeset_id of a node element
529   def update_changeset(xml, changeset_id)
530     xml_attr_rewrite(xml, 'changeset', changeset_id)
531   end
532
533   ##
534   # update an attribute in the node element
535   def xml_attr_rewrite(xml, name, value)
536     xml.find("//osm/relation").first[name] = value.to_s
537     return xml
538   end
539
540   ##
541   # parse some xml
542   def xml_parse(xml)
543     parser = XML::Parser.new
544     parser.string = xml
545     parser.parse
546   end
547 end