]> git.openstreetmap.org Git - rails.git/blob - test/functional/relation_controller_test.rb
Migration to add close-time to changesets. This replaces the boolean 'open' attribute...
[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     # create an relation with a node as member
121     nid = current_nodes(:used_node_1).id
122     content "<osm><relation changeset='#{changeset_id}'>" +
123       "<member type='node' ref='#{nid}' role='some'/>" +
124       "<tag k='test' v='yes' /></relation></osm>"
125     put :create
126     # hope for success
127     assert_response :success, 
128         "relation upload did not return success status"
129     # read id of created relation and search for it
130     relationid = @response.body
131     checkrelation = Relation.find(relationid)
132     assert_not_nil checkrelation, 
133         "uploaded relation not found in data base after upload"
134     # compare values
135     assert_equal checkrelation.members.length, 1, 
136         "saved relation does not contain exactly one member"
137     assert_equal checkrelation.tags.length, 1, 
138         "saved relation does not contain exactly one tag"
139     assert_equal changeset_id, checkrelation.changeset.id,
140         "saved relation does not belong in the changeset it was assigned to"
141     assert_equal users(:normal_user).id, checkrelation.changeset.user_id, 
142         "saved relation does not belong to user that created it"
143     assert_equal true, checkrelation.visible, 
144         "saved relation is not visible"
145     # ok the relation is there but can we also retrieve it?
146     
147     get :read, :id => relationid
148     assert_response :success
149
150     # create an relation with a way and a node as members
151     nid = current_nodes(:used_node_1).id
152     wid = current_ways(:used_way).id
153     content "<osm><relation changeset='#{changeset_id}'>" +
154       "<member type='node' ref='#{nid}' role='some'/>" +
155       "<member type='way' ref='#{wid}' role='other'/>" +
156       "<tag k='test' v='yes' /></relation></osm>"
157     put :create
158     # hope for success
159     assert_response :success, 
160         "relation upload did not return success status"
161     # read id of created relation and search for it
162     relationid = @response.body
163     checkrelation = Relation.find(relationid)
164     assert_not_nil checkrelation, 
165         "uploaded relation not found in data base after upload"
166     # compare values
167     assert_equal checkrelation.members.length, 2, 
168         "saved relation does not have exactly two members"
169     assert_equal checkrelation.tags.length, 1, 
170         "saved relation does not contain exactly one tag"
171     assert_equal changeset_id, checkrelation.changeset.id,
172         "saved relation does not belong in the changeset it was assigned to"
173     assert_equal users(:normal_user).id, checkrelation.changeset.user_id, 
174         "saved relation does not belong to user that created it"
175     assert_equal true, checkrelation.visible, 
176         "saved relation is not visible"
177     # ok the relation is there but can we also retrieve it?
178     get :read, :id => relationid
179     assert_response :success
180
181   end
182
183   # -------------------------------------
184   # Test creating some invalid relations.
185   # -------------------------------------
186
187   def test_create_invalid
188     basic_authorization "test@openstreetmap.org", "test"
189
190     # put the relation in a dummy fixture changset
191     changeset_id = changesets(:normal_user_first_change).id
192
193     # create a relation with non-existing node as member
194     content "<osm><relation changeset='#{changeset_id}'>" +
195       "<member type='node' ref='0'/><tag k='test' v='yes' />" +
196       "</relation></osm>"
197     put :create
198     # expect failure
199     assert_response :precondition_failed, 
200         "relation upload with invalid node did not return 'precondition failed'"
201   end
202
203   # -------------------------------------
204   # Test deleting relations.
205   # -------------------------------------
206   
207   def test_delete
208     # first try to delete relation without auth
209     delete :delete, :id => current_relations(:visible_relation).id
210     assert_response :unauthorized
211
212     # now set auth
213     basic_authorization("test@openstreetmap.org", "test");  
214
215     # this shouldn't work, as we should need the payload...
216     delete :delete, :id => current_relations(:visible_relation).id
217     assert_response :bad_request
218
219     # try to delete without specifying a changeset
220     content "<osm><relation id='#{current_relations(:visible_relation).id}'/></osm>"
221     delete :delete, :id => current_relations(:visible_relation).id
222     assert_response :conflict
223
224     # try to delete with an invalid (closed) changeset
225     content update_changeset(current_relations(:visible_relation).to_xml,
226                              changesets(:normal_user_closed_change).id)
227     delete :delete, :id => current_relations(:visible_relation).id
228     assert_response :conflict
229
230     # try to delete with an invalid (non-existent) changeset
231     content update_changeset(current_relations(:visible_relation).to_xml,0)
232     delete :delete, :id => current_relations(:visible_relation).id
233     assert_response :conflict
234
235     # this won't work because the relation is in-use by another relation
236     content(relations(:used_relation).to_xml)
237     delete :delete, :id => current_relations(:used_relation).id
238     assert_response :precondition_failed, 
239        "shouldn't be able to delete a relation used in a relation (#{@response.body})"
240
241     # this should work when we provide the appropriate payload...
242     content(relations(:visible_relation).to_xml)
243     delete :delete, :id => current_relations(:visible_relation).id
244     assert_response :success
245
246     # valid delete should return the new version number, which should
247     # be greater than the old version number
248     assert @response.body.to_i > current_relations(:visible_relation).version,
249        "delete request should return a new version number for relation"
250
251     # this won't work since the relation is already deleted
252     content(relations(:invisible_relation).to_xml)
253     delete :delete, :id => current_relations(:invisible_relation).id
254     assert_response :gone
255
256     # this works now because the relation which was using this one 
257     # has been deleted.
258     content(relations(:used_relation).to_xml)
259     delete :delete, :id => current_relations(:used_relation).id
260     assert_response :success, 
261        "should be able to delete a relation used in an old relation (#{@response.body})"
262
263     # this won't work since the relation never existed
264     delete :delete, :id => 0
265     assert_response :not_found
266   end
267
268   ##
269   # when a relation's tag is modified then it should put the bounding
270   # box of all its members into the changeset.
271   def test_tag_modify_bounding_box
272     # in current fixtures, relation 5 contains nodes 3 and 5 (node 3
273     # indirectly via way 3), so the bbox should be [3,3,5,5].
274     check_changeset_modify([3,3,5,5]) do |changeset_id|
275       # add a tag to an existing relation
276       relation_xml = current_relations(:visible_relation).to_xml
277       relation_element = relation_xml.find("//osm/relation").first
278       new_tag = XML::Node.new("tag")
279       new_tag['k'] = "some_new_tag"
280       new_tag['v'] = "some_new_value"
281       relation_element << new_tag
282       
283       # update changeset ID to point to new changeset
284       update_changeset(relation_xml, changeset_id)
285       
286       # upload the change
287       content relation_xml
288       put :update, :id => current_relations(:visible_relation).id
289       assert_response :success, "can't update relation for tag/bbox test"
290     end
291   end
292
293   ##
294   # add a member to a relation and check the bounding box is only that
295   # element.
296   def test_add_member_bounding_box
297     check_changeset_modify([4,4,4,4]) do |changeset_id|
298       # add node 4 (4,4) to an existing relation
299       relation_xml = current_relations(:visible_relation).to_xml
300       relation_element = relation_xml.find("//osm/relation").first
301       new_member = XML::Node.new("member")
302       new_member['ref'] = current_nodes(:used_node_2).id.to_s
303       new_member['type'] = "node"
304       new_member['role'] = "some_role"
305       relation_element << new_member
306       
307       # update changeset ID to point to new changeset
308       update_changeset(relation_xml, changeset_id)
309       
310       # upload the change
311       content relation_xml
312       put :update, :id => current_relations(:visible_relation).id
313       assert_response :success, "can't update relation for add node/bbox test"
314     end
315   end
316   
317   ##
318   # remove a member from a relation and check the bounding box is 
319   # only that element.
320   def test_remove_member_bounding_box
321     check_changeset_modify([5,5,5,5]) do |changeset_id|
322       # remove node 5 (5,5) from an existing relation
323       relation_xml = current_relations(:visible_relation).to_xml
324       relation_xml.
325         find("//osm/relation/member[@type='node'][@ref='5']").
326         first.remove!
327       
328       # update changeset ID to point to new changeset
329       update_changeset(relation_xml, changeset_id)
330       
331       # upload the change
332       content relation_xml
333       put :update, :id => current_relations(:visible_relation).id
334       assert_response :success, "can't update relation for remove node/bbox test"
335     end
336   end
337   
338   ##
339   # check that relations are ordered
340   def test_relation_member_ordering
341     basic_authorization("test@openstreetmap.org", "test");  
342
343     doc_str = <<OSM
344 <osm>
345  <relation changeset='1'>
346   <member ref='1' type='node' role='first'/>
347   <member ref='3' type='node' role='second'/>
348   <member ref='1' type='way' role='third'/>
349   <member ref='3' type='way' role='fourth'/>
350  </relation>
351 </osm>
352 OSM
353     doc = XML::Parser.string(doc_str).parse
354
355     content doc
356     put :create
357     assert_response :success, "can't create a relation: #{@response.body}"
358     relation_id = @response.body.to_i
359
360     # get it back and check the ordering
361     get :read, :id => relation_id
362     assert_response :success, "can't read back the relation: #{@response.body}"
363     check_ordering(doc, @response.body)
364
365     # insert a member at the front
366     new_member = XML::Node.new "member"
367     new_member['ref'] = 5.to_s
368     new_member['type'] = 'node'
369     new_member['role'] = 'new first'
370     doc.find("//osm/relation").first.child.prev = new_member
371     # update the version, should be 1?
372     doc.find("//osm/relation").first['id'] = relation_id.to_s
373     doc.find("//osm/relation").first['version'] = 1.to_s
374
375     # upload the next version of the relation
376     content doc
377     put :update, :id => relation_id
378     assert_response :success, "can't update relation: #{@response.body}"
379     new_version = @response.body.to_i
380
381     # get it back again and check the ordering again
382     get :read, :id => relation_id
383     assert_response :success, "can't read back the relation: #{@response.body}"
384     check_ordering(doc, @response.body)
385   end
386
387   ## 
388   # check that relations can contain duplicate members
389   def test_relation_member_duplicates
390     basic_authorization("test@openstreetmap.org", "test");  
391
392     doc_str = <<OSM
393 <osm>
394  <relation changeset='1'>
395   <member ref='1' type='node' role='forward'/>
396   <member ref='3' type='node' role='forward'/>
397   <member ref='1' type='node' role='forward'/>
398   <member ref='3' type='node' role='forward'/>
399  </relation>
400 </osm>
401 OSM
402     doc = XML::Parser.string(doc_str).parse
403
404     content doc
405     put :create
406     assert_response :success, "can't create a relation: #{@response.body}"
407     relation_id = @response.body.to_i
408
409     # get it back and check the ordering
410     get :read, :id => relation_id
411     assert_response :success, "can't read back the relation: #{@response.body}"
412     check_ordering(doc, @response.body)
413   end
414
415   # ============================================================
416   # utility functions
417   # ============================================================
418
419   ##
420   # checks that the XML document and the string arguments have
421   # members in the same order.
422   def check_ordering(doc, xml)
423     new_doc = XML::Parser.string(xml).parse
424
425     doc_members = doc.find("//osm/relation/member").collect do |m|
426       [m['ref'].to_i, m['type'].to_sym, m['role']]
427     end
428
429     new_members = new_doc.find("//osm/relation/member").collect do |m|
430       [m['ref'].to_i, m['type'].to_sym, m['role']]
431     end
432
433     doc_members.zip(new_members).each do |d, n|
434       assert_equal d, n, "members are not equal - ordering is wrong? (#{doc}, #{xml})"
435     end
436   end
437
438   ##
439   # create a changeset and yield to the caller to set it up, then assert
440   # that the changeset bounding box is +bbox+.
441   def check_changeset_modify(bbox)
442     basic_authorization("test@openstreetmap.org", "test");  
443
444     # create a new changeset for this operation, so we are assured
445     # that the bounding box will be newly-generated.
446     changeset_id = with_controller(ChangesetController.new) do
447       content "<osm><changeset/></osm>"
448       put :create
449       assert_response :success, "couldn't create changeset for modify test"
450       @response.body.to_i
451     end
452
453     # go back to the block to do the actual modifies
454     yield changeset_id
455
456     # now download the changeset to check its bounding box
457     with_controller(ChangesetController.new) do
458       get :read, :id => changeset_id
459       assert_response :success, "can't re-read changeset for modify test"
460       assert_select "osm>changeset", 1
461       assert_select "osm>changeset[id=#{changeset_id}]", 1
462       assert_select "osm>changeset[min_lon=#{bbox[0].to_f}]", 1
463       assert_select "osm>changeset[min_lat=#{bbox[1].to_f}]", 1
464       assert_select "osm>changeset[max_lon=#{bbox[2].to_f}]", 1
465       assert_select "osm>changeset[max_lat=#{bbox[3].to_f}]", 1
466     end
467   end
468
469   ##
470   # update the changeset_id of a node element
471   def update_changeset(xml, changeset_id)
472     xml_attr_rewrite(xml, 'changeset', changeset_id)
473   end
474
475   ##
476   # update an attribute in the node element
477   def xml_attr_rewrite(xml, name, value)
478     xml.find("//osm/relation").first[name] = value.to_s
479     return xml
480   end
481
482   ##
483   # parse some xml
484   def xml_parse(xml)
485     parser = XML::Parser.new
486     parser.string = xml
487     parser.parse
488   end
489 end