]> git.openstreetmap.org Git - rails.git/blob - test/controllers/relation_controller_test.rb
Hide note comments made by deleted users
[rails.git] / test / controllers / relation_controller_test.rb
1 require "test_helper"
2 require "relation_controller"
3
4 class RelationControllerTest < ActionController::TestCase
5   ##
6   # test all routes which lead to this controller
7   def test_routes
8     assert_routing(
9       { :path => "/api/0.6/relation/create", :method => :put },
10       { :controller => "relation", :action => "create" }
11     )
12     assert_routing(
13       { :path => "/api/0.6/relation/1/full", :method => :get },
14       { :controller => "relation", :action => "full", :id => "1" }
15     )
16     assert_routing(
17       { :path => "/api/0.6/relation/1", :method => :get },
18       { :controller => "relation", :action => "read", :id => "1" }
19     )
20     assert_routing(
21       { :path => "/api/0.6/relation/1", :method => :put },
22       { :controller => "relation", :action => "update", :id => "1" }
23     )
24     assert_routing(
25       { :path => "/api/0.6/relation/1", :method => :delete },
26       { :controller => "relation", :action => "delete", :id => "1" }
27     )
28     assert_routing(
29       { :path => "/api/0.6/relations", :method => :get },
30       { :controller => "relation", :action => "relations" }
31     )
32
33     assert_routing(
34       { :path => "/api/0.6/node/1/relations", :method => :get },
35       { :controller => "relation", :action => "relations_for_node", :id => "1" }
36     )
37     assert_routing(
38       { :path => "/api/0.6/way/1/relations", :method => :get },
39       { :controller => "relation", :action => "relations_for_way", :id => "1" }
40     )
41     assert_routing(
42       { :path => "/api/0.6/relation/1/relations", :method => :get },
43       { :controller => "relation", :action => "relations_for_relation", :id => "1" }
44     )
45   end
46
47   # -------------------------------------
48   # Test reading relations.
49   # -------------------------------------
50
51   def test_read
52     # check that a visible relation is returned properly
53     get :read, :params => { :id => create(:relation).id }
54     assert_response :success
55
56     # check that an invisible relation is not returned
57     get :read, :params => { :id => create(:relation, :deleted).id }
58     assert_response :gone
59
60     # check chat a non-existent relation is not returned
61     get :read, :params => { :id => 0 }
62     assert_response :not_found
63   end
64
65   ##
66   # check that all relations containing a particular node, and no extra
67   # relations, are returned from the relations_for_node call.
68   def test_relations_for_node
69     node = create(:node)
70     # should include relations with that node as a member
71     relation_with_node = create(:relation_member, :member => node).relation
72     # should ignore relations without that node as a member
73     _relation_without_node = create(:relation_member).relation
74     # should ignore relations with the node involved indirectly, via a way
75     way = create(:way_node, :node => node).way
76     _relation_with_way = create(:relation_member, :member => way).relation
77     # should ignore relations with the node involved indirectly, via a relation
78     second_relation = create(:relation_member, :member => node).relation
79     _super_relation = create(:relation_member, :member => second_relation).relation
80     # should combine multiple relation_member references into just one relation entry
81     create(:relation_member, :member => node, :relation => relation_with_node, :sequence_id => 2)
82     # should not include deleted relations
83     deleted_relation = create(:relation, :deleted)
84     create(:relation_member, :member => node, :relation => deleted_relation)
85
86     check_relations_for_element(:relations_for_node, "node",
87                                 node.id,
88                                 [relation_with_node, second_relation])
89   end
90
91   def test_relations_for_way
92     way = create(:way)
93     # should include relations with that way as a member
94     relation_with_way = create(:relation_member, :member => way).relation
95     # should ignore relations without that way as a member
96     _relation_without_way = create(:relation_member).relation
97     # should ignore relations with the way involved indirectly, via a relation
98     second_relation = create(:relation_member, :member => way).relation
99     _super_relation = create(:relation_member, :member => second_relation).relation
100     # should combine multiple relation_member references into just one relation entry
101     create(:relation_member, :member => way, :relation => relation_with_way, :sequence_id => 2)
102     # should not include deleted relations
103     deleted_relation = create(:relation, :deleted)
104     create(:relation_member, :member => way, :relation => deleted_relation)
105
106     check_relations_for_element(:relations_for_way, "way",
107                                 way.id,
108                                 [relation_with_way, second_relation])
109   end
110
111   def test_relations_for_relation
112     relation = create(:relation)
113     # should include relations with that relation as a member
114     relation_with_relation = create(:relation_member, :member => relation).relation
115     # should ignore any relation without that relation as a member
116     _relation_without_relation = create(:relation_member).relation
117     # should ignore relations with the relation involved indirectly, via a relation
118     second_relation = create(:relation_member, :member => relation).relation
119     _super_relation = create(:relation_member, :member => second_relation).relation
120     # should combine multiple relation_member references into just one relation entry
121     create(:relation_member, :member => relation, :relation => relation_with_relation, :sequence_id => 2)
122     # should not include deleted relations
123     deleted_relation = create(:relation, :deleted)
124     create(:relation_member, :member => relation, :relation => deleted_relation)
125     check_relations_for_element(:relations_for_relation, "relation",
126                                 relation.id,
127                                 [relation_with_relation, second_relation])
128   end
129
130   def check_relations_for_element(method, type, id, expected_relations)
131     # check the "relations for relation" mode
132     get method, :params => { :id => id }
133     assert_response :success
134
135     # count one osm element
136     assert_select "osm[version='#{API_VERSION}'][generator='OpenStreetMap server']", 1
137
138     # we should have only the expected number of relations
139     assert_select "osm>relation", expected_relations.size
140
141     # and each of them should contain the element we originally searched for
142     expected_relations.each do |relation|
143       # The relation should appear once, but the element could appear multiple times
144       assert_select "osm>relation[id='#{relation.id}']", 1
145       assert_select "osm>relation[id='#{relation.id}']>member[type='#{type}'][ref='#{id}']"
146     end
147   end
148
149   def test_full
150     # check the "full" mode
151     get :full, :params => { :id => 999999 }
152     assert_response :not_found
153
154     get :full, :params => { :id => create(:relation, :deleted).id }
155     assert_response :gone
156
157     get :full, :params => { :id => create(:relation).id }
158     assert_response :success
159     # FIXME: check whether this contains the stuff we want!
160   end
161
162   ##
163   # test fetching multiple relations
164   def test_relations
165     relation1 = create(:relation)
166     relation2 = create(:relation, :deleted)
167     relation3 = create(:relation, :with_history, :version => 2)
168     relation4 = create(:relation, :with_history, :version => 2)
169     relation4.old_relations.find_by(:version => 1).redact!(create(:redaction))
170
171     # check error when no parameter provided
172     get :relations
173     assert_response :bad_request
174
175     # check error when no parameter value provided
176     get :relations, :params => { :relations => "" }
177     assert_response :bad_request
178
179     # test a working call
180     get :relations, :params => { :relations => "#{relation1.id},#{relation2.id},#{relation3.id},#{relation4.id}" }
181     assert_response :success
182     assert_select "osm" do
183       assert_select "relation", :count => 4
184       assert_select "relation[id='#{relation1.id}'][visible='true']", :count => 1
185       assert_select "relation[id='#{relation2.id}'][visible='false']", :count => 1
186       assert_select "relation[id='#{relation3.id}'][visible='true']", :count => 1
187       assert_select "relation[id='#{relation4.id}'][visible='true']", :count => 1
188     end
189
190     # check error when a non-existent relation is included
191     get :relations, :params => { :relations => "#{relation1.id},#{relation2.id},#{relation3.id},#{relation4.id},0" }
192     assert_response :not_found
193   end
194
195   # -------------------------------------
196   # Test simple relation creation.
197   # -------------------------------------
198
199   def test_create
200     private_user = create(:user, :data_public => false)
201     private_changeset = create(:changeset, :user => private_user)
202     user = create(:user)
203     changeset = create(:changeset, :user => user)
204     node = create(:node)
205     way = create(:way_with_nodes, :nodes_count => 2)
206
207     basic_authorization private_user.email, "test"
208
209     # create an relation without members
210     content "<osm><relation changeset='#{private_changeset.id}'><tag k='test' v='yes' /></relation></osm>"
211     put :create
212     # hope for forbidden, due to user
213     assert_response :forbidden,
214                     "relation upload should have failed with forbidden"
215
216     ###
217     # create an relation with a node as member
218     # This time try with a role attribute in the relation
219     content "<osm><relation changeset='#{private_changeset.id}'>" \
220             "<member  ref='#{node.id}' type='node' role='some'/>" \
221             "<tag k='test' v='yes' /></relation></osm>"
222     put :create
223     # hope for forbidden due to user
224     assert_response :forbidden,
225                     "relation upload did not return forbidden status"
226
227     ###
228     # create an relation with a node as member, this time test that we don't
229     # need a role attribute to be included
230     content "<osm><relation changeset='#{private_changeset.id}'>" \
231             "<member  ref='#{node.id}' type='node'/>" + "<tag k='test' v='yes' /></relation></osm>"
232     put :create
233     # hope for forbidden due to user
234     assert_response :forbidden,
235                     "relation upload did not return forbidden status"
236
237     ###
238     # create an relation with a way and a node as members
239     content "<osm><relation changeset='#{private_changeset.id}'>" \
240             "<member type='node' ref='#{node.id}' role='some'/>" \
241             "<member type='way' ref='#{way.id}' role='other'/>" \
242             "<tag k='test' v='yes' /></relation></osm>"
243     put :create
244     # hope for forbidden, due to user
245     assert_response :forbidden,
246                     "relation upload did not return success status"
247
248     ## Now try with the public user
249     basic_authorization user.email, "test"
250
251     # create an relation without members
252     content "<osm><relation changeset='#{changeset.id}'><tag k='test' v='yes' /></relation></osm>"
253     put :create
254     # hope for success
255     assert_response :success,
256                     "relation upload did not return success status"
257     # read id of created relation and search for it
258     relationid = @response.body
259     checkrelation = Relation.find(relationid)
260     assert_not_nil checkrelation,
261                    "uploaded relation not found in data base after upload"
262     # compare values
263     assert_equal checkrelation.members.length, 0,
264                  "saved relation contains members but should not"
265     assert_equal checkrelation.tags.length, 1,
266                  "saved relation does not contain exactly one tag"
267     assert_equal changeset.id, checkrelation.changeset.id,
268                  "saved relation does not belong in the changeset it was assigned to"
269     assert_equal user.id, checkrelation.changeset.user_id,
270                  "saved relation does not belong to user that created it"
271     assert_equal true, checkrelation.visible,
272                  "saved relation is not visible"
273     # ok the relation is there but can we also retrieve it?
274     get :read, :params => { :id => relationid }
275     assert_response :success
276
277     ###
278     # create an relation with a node as member
279     # This time try with a role attribute in the relation
280     content "<osm><relation changeset='#{changeset.id}'>" \
281             "<member  ref='#{node.id}' type='node' role='some'/>" \
282             "<tag k='test' v='yes' /></relation></osm>"
283     put :create
284     # hope for success
285     assert_response :success,
286                     "relation upload did not return success status"
287     # read id of created relation and search for it
288     relationid = @response.body
289     checkrelation = Relation.find(relationid)
290     assert_not_nil checkrelation,
291                    "uploaded relation not found in data base after upload"
292     # compare values
293     assert_equal checkrelation.members.length, 1,
294                  "saved relation does not contain exactly one member"
295     assert_equal checkrelation.tags.length, 1,
296                  "saved relation does not contain exactly one tag"
297     assert_equal changeset.id, checkrelation.changeset.id,
298                  "saved relation does not belong in the changeset it was assigned to"
299     assert_equal user.id, checkrelation.changeset.user_id,
300                  "saved relation does not belong to user that created it"
301     assert_equal true, checkrelation.visible,
302                  "saved relation is not visible"
303     # ok the relation is there but can we also retrieve it?
304
305     get :read, :params => { :id => relationid }
306     assert_response :success
307
308     ###
309     # create an relation with a node as member, this time test that we don't
310     # need a role attribute to be included
311     content "<osm><relation changeset='#{changeset.id}'>" \
312             "<member  ref='#{node.id}' type='node'/>" + "<tag k='test' v='yes' /></relation></osm>"
313     put :create
314     # hope for success
315     assert_response :success,
316                     "relation upload did not return success status"
317     # read id of created relation and search for it
318     relationid = @response.body
319     checkrelation = Relation.find(relationid)
320     assert_not_nil checkrelation,
321                    "uploaded relation not found in data base after upload"
322     # compare values
323     assert_equal checkrelation.members.length, 1,
324                  "saved relation does not contain exactly one member"
325     assert_equal checkrelation.tags.length, 1,
326                  "saved relation does not contain exactly one tag"
327     assert_equal changeset.id, checkrelation.changeset.id,
328                  "saved relation does not belong in the changeset it was assigned to"
329     assert_equal user.id, checkrelation.changeset.user_id,
330                  "saved relation does not belong to user that created it"
331     assert_equal true, checkrelation.visible,
332                  "saved relation is not visible"
333     # ok the relation is there but can we also retrieve it?
334
335     get :read, :params => { :id => relationid }
336     assert_response :success
337
338     ###
339     # create an relation with a way and a node as members
340     content "<osm><relation changeset='#{changeset.id}'>" \
341             "<member type='node' ref='#{node.id}' role='some'/>" \
342             "<member type='way' ref='#{way.id}' role='other'/>" \
343             "<tag k='test' v='yes' /></relation></osm>"
344     put :create
345     # hope for success
346     assert_response :success,
347                     "relation upload did not return success status"
348     # read id of created relation and search for it
349     relationid = @response.body
350     checkrelation = Relation.find(relationid)
351     assert_not_nil checkrelation,
352                    "uploaded relation not found in data base after upload"
353     # compare values
354     assert_equal checkrelation.members.length, 2,
355                  "saved relation does not have exactly two members"
356     assert_equal checkrelation.tags.length, 1,
357                  "saved relation does not contain exactly one tag"
358     assert_equal changeset.id, checkrelation.changeset.id,
359                  "saved relation does not belong in the changeset it was assigned to"
360     assert_equal user.id, checkrelation.changeset.user_id,
361                  "saved relation does not belong to user that created it"
362     assert_equal true, checkrelation.visible,
363                  "saved relation is not visible"
364     # ok the relation is there but can we also retrieve it?
365     get :read, :params => { :id => relationid }
366     assert_response :success
367   end
368
369   # ------------------------------------
370   # Test updating relations
371   # ------------------------------------
372
373   ##
374   # test that, when tags are updated on a relation, the correct things
375   # happen to the correct tables and the API gives sensible results.
376   # this is to test a case that gregory marler noticed and posted to
377   # josm-dev.
378   ## FIXME Move this to an integration test
379   def test_update_relation_tags
380     user = create(:user)
381     changeset = create(:changeset, :user => user)
382     relation = create(:relation)
383     create_list(:relation_tag, 4, :relation => relation)
384
385     basic_authorization user.email, "test"
386
387     with_relation(relation.id) do |rel|
388       # alter one of the tags
389       tag = rel.find("//osm/relation/tag").first
390       tag["v"] = "some changed value"
391       update_changeset(rel, changeset.id)
392
393       # check that the downloaded tags are the same as the uploaded tags...
394       new_version = with_update(rel) do |new_rel|
395         assert_tags_equal rel, new_rel
396       end
397
398       # check the original one in the current_* table again
399       with_relation(relation.id) { |r| assert_tags_equal rel, r }
400
401       # now check the version in the history
402       with_relation(relation.id, new_version) { |r| assert_tags_equal rel, r }
403     end
404   end
405
406   ##
407   # test that, when tags are updated on a relation when using the diff
408   # upload function, the correct things happen to the correct tables
409   # and the API gives sensible results. this is to test a case that
410   # gregory marler noticed and posted to josm-dev.
411   def test_update_relation_tags_via_upload
412     user = create(:user)
413     changeset = create(:changeset, :user => user)
414     relation = create(:relation)
415     create_list(:relation_tag, 4, :relation => relation)
416
417     basic_authorization user.email, "test"
418
419     with_relation(relation.id) do |rel|
420       # alter one of the tags
421       tag = rel.find("//osm/relation/tag").first
422       tag["v"] = "some changed value"
423       update_changeset(rel, changeset.id)
424
425       # check that the downloaded tags are the same as the uploaded tags...
426       new_version = with_update_diff(rel) do |new_rel|
427         assert_tags_equal rel, new_rel
428       end
429
430       # check the original one in the current_* table again
431       with_relation(relation.id) { |r| assert_tags_equal rel, r }
432
433       # now check the version in the history
434       with_relation(relation.id, new_version) { |r| assert_tags_equal rel, r }
435     end
436   end
437
438   def test_update_wrong_id
439     user = create(:user)
440     changeset = create(:changeset, :user => user)
441     relation = create(:relation)
442     other_relation = create(:relation)
443
444     basic_authorization user.email, "test"
445     with_relation(relation.id) do |rel|
446       update_changeset(rel, changeset.id)
447       content rel
448       put :update, :params => { :id => other_relation.id }
449       assert_response :bad_request
450     end
451   end
452
453   # -------------------------------------
454   # Test creating some invalid relations.
455   # -------------------------------------
456
457   def test_create_invalid
458     user = create(:user)
459     changeset = create(:changeset, :user => user)
460
461     basic_authorization user.email, "test"
462
463     # create a relation with non-existing node as member
464     content "<osm><relation changeset='#{changeset.id}'>" \
465             "<member type='node' ref='0'/><tag k='test' v='yes' />" \
466             "</relation></osm>"
467     put :create
468     # expect failure
469     assert_response :precondition_failed,
470                     "relation upload with invalid node did not return 'precondition failed'"
471     assert_equal "Precondition failed: Relation with id  cannot be saved due to Node with id 0", @response.body
472   end
473
474   # -------------------------------------
475   # Test creating a relation, with some invalid XML
476   # -------------------------------------
477   def test_create_invalid_xml
478     user = create(:user)
479     changeset = create(:changeset, :user => user)
480     node = create(:node)
481
482     basic_authorization user.email, "test"
483
484     # create some xml that should return an error
485     content "<osm><relation changeset='#{changeset.id}'>" \
486             "<member type='type' ref='#{node.id}' role=''/>" \
487             "<tag k='tester' v='yep'/></relation></osm>"
488     put :create
489     # expect failure
490     assert_response :bad_request
491     assert_match(/Cannot parse valid relation from xml string/, @response.body)
492     assert_match(/The type is not allowed only, /, @response.body)
493   end
494
495   # -------------------------------------
496   # Test deleting relations.
497   # -------------------------------------
498
499   def test_delete
500     private_user = create(:user, :data_public => false)
501     private_user_closed_changeset = create(:changeset, :closed, :user => private_user)
502     user = create(:user)
503     closed_changeset = create(:changeset, :closed, :user => user)
504     changeset = create(:changeset, :user => user)
505     relation = create(:relation)
506     used_relation = create(:relation)
507     super_relation = create(:relation_member, :member => used_relation).relation
508     deleted_relation = create(:relation, :deleted)
509     multi_tag_relation = create(:relation)
510     create_list(:relation_tag, 4, :relation => multi_tag_relation)
511
512     ## First try to delete relation without auth
513     delete :delete, :params => { :id => relation.id }
514     assert_response :unauthorized
515
516     ## Then try with the private user, to make sure that you get a forbidden
517     basic_authorization private_user.email, "test"
518
519     # this shouldn't work, as we should need the payload...
520     delete :delete, :params => { :id => relation.id }
521     assert_response :forbidden
522
523     # try to delete without specifying a changeset
524     content "<osm><relation id='#{relation.id}'/></osm>"
525     delete :delete, :params => { :id => relation.id }
526     assert_response :forbidden
527
528     # try to delete with an invalid (closed) changeset
529     content update_changeset(relation.to_xml,
530                              private_user_closed_changeset.id)
531     delete :delete, :params => { :id => relation.id }
532     assert_response :forbidden
533
534     # try to delete with an invalid (non-existent) changeset
535     content update_changeset(relation.to_xml, 0)
536     delete :delete, :params => { :id => relation.id }
537     assert_response :forbidden
538
539     # this won't work because the relation is in-use by another relation
540     content(used_relation.to_xml)
541     delete :delete, :params => { :id => used_relation.id }
542     assert_response :forbidden
543
544     # this should work when we provide the appropriate payload...
545     content(relation.to_xml)
546     delete :delete, :params => { :id => relation.id }
547     assert_response :forbidden
548
549     # this won't work since the relation is already deleted
550     content(deleted_relation.to_xml)
551     delete :delete, :params => { :id => deleted_relation.id }
552     assert_response :forbidden
553
554     # this won't work since the relation never existed
555     delete :delete, :params => { :id => 0 }
556     assert_response :forbidden
557
558     ## now set auth for the public user
559     basic_authorization user.email, "test"
560
561     # this shouldn't work, as we should need the payload...
562     delete :delete, :params => { :id => relation.id }
563     assert_response :bad_request
564
565     # try to delete without specifying a changeset
566     content "<osm><relation id='#{relation.id}' version='#{relation.version}' /></osm>"
567     delete :delete, :params => { :id => relation.id }
568     assert_response :bad_request
569     assert_match(/Changeset id is missing/, @response.body)
570
571     # try to delete with an invalid (closed) changeset
572     content update_changeset(relation.to_xml,
573                              closed_changeset.id)
574     delete :delete, :params => { :id => relation.id }
575     assert_response :conflict
576
577     # try to delete with an invalid (non-existent) changeset
578     content update_changeset(relation.to_xml, 0)
579     delete :delete, :params => { :id => relation.id }
580     assert_response :conflict
581
582     # this won't work because the relation is in a changeset owned by someone else
583     content update_changeset(relation.to_xml, create(:changeset).id)
584     delete :delete, :params => { :id => relation.id }
585     assert_response :conflict,
586                     "shouldn't be able to delete a relation in a changeset owned by someone else (#{@response.body})"
587
588     # this won't work because the relation in the payload is different to that passed
589     content update_changeset(relation.to_xml, changeset.id)
590     delete :delete, :params => { :id => create(:relation).id }
591     assert_response :bad_request, "shouldn't be able to delete a relation when payload is different to the url"
592
593     # this won't work because the relation is in-use by another relation
594     content update_changeset(used_relation.to_xml, changeset.id)
595     delete :delete, :params => { :id => used_relation.id }
596     assert_response :precondition_failed,
597                     "shouldn't be able to delete a relation used in a relation (#{@response.body})"
598     assert_equal "Precondition failed: The relation #{used_relation.id} is used in relation #{super_relation.id}.", @response.body
599
600     # this should work when we provide the appropriate payload...
601     content update_changeset(multi_tag_relation.to_xml, changeset.id)
602     delete :delete, :params => { :id => multi_tag_relation.id }
603     assert_response :success
604
605     # valid delete should return the new version number, which should
606     # be greater than the old version number
607     assert @response.body.to_i > multi_tag_relation.version,
608            "delete request should return a new version number for relation"
609
610     # this won't work since the relation is already deleted
611     content update_changeset(deleted_relation.to_xml, changeset.id)
612     delete :delete, :params => { :id => deleted_relation.id }
613     assert_response :gone
614
615     # Public visible relation needs to be deleted
616     content update_changeset(super_relation.to_xml, changeset.id)
617     delete :delete, :params => { :id => super_relation.id }
618     assert_response :success
619
620     # this works now because the relation which was using this one
621     # has been deleted.
622     content update_changeset(used_relation.to_xml, changeset.id)
623     delete :delete, :params => { :id => used_relation.id }
624     assert_response :success,
625                     "should be able to delete a relation used in an old relation (#{@response.body})"
626
627     # this won't work since the relation never existed
628     delete :delete, :params => { :id => 0 }
629     assert_response :not_found
630   end
631
632   ##
633   # when a relation's tag is modified then it should put the bounding
634   # box of all its members into the changeset.
635   def test_tag_modify_bounding_box
636     relation = create(:relation)
637     node1 = create(:node, :lat => 3, :lon => 3)
638     node2 = create(:node, :lat => 5, :lon => 5)
639     way = create(:way)
640     create(:way_node, :way => way, :node => node1)
641     create(:relation_member, :relation => relation, :member => way)
642     create(:relation_member, :relation => relation, :member => node2)
643     # the relation contains nodes1 and node2 (node1
644     # indirectly via the way), so the bbox should be [3,3,5,5].
645     check_changeset_modify(BoundingBox.new(3, 3, 5, 5)) do |changeset_id|
646       # add a tag to an existing relation
647       relation_xml = relation.to_xml
648       relation_element = relation_xml.find("//osm/relation").first
649       new_tag = XML::Node.new("tag")
650       new_tag["k"] = "some_new_tag"
651       new_tag["v"] = "some_new_value"
652       relation_element << new_tag
653
654       # update changeset ID to point to new changeset
655       update_changeset(relation_xml, changeset_id)
656
657       # upload the change
658       content relation_xml
659       put :update, :params => { :id => relation.id }
660       assert_response :success, "can't update relation for tag/bbox test"
661     end
662   end
663
664   ##
665   # add a member to a relation and check the bounding box is only that
666   # element.
667   def test_add_member_bounding_box
668     relation = create(:relation)
669     node1 = create(:node, :lat => 4, :lon => 4)
670     node2 = create(:node, :lat => 7, :lon => 7)
671     way1 = create(:way)
672     create(:way_node, :way => way1, :node => create(:node, :lat => 8, :lon => 8))
673     way2 = create(:way)
674     create(:way_node, :way => way2, :node => create(:node, :lat => 9, :lon => 9), :sequence_id => 1)
675     create(:way_node, :way => way2, :node => create(:node, :lat => 10, :lon => 10), :sequence_id => 2)
676
677     [node1, node2, way1, way2].each do |element|
678       bbox = element.bbox.to_unscaled
679       check_changeset_modify(bbox) do |changeset_id|
680         relation_xml = Relation.find(relation.id).to_xml
681         relation_element = relation_xml.find("//osm/relation").first
682         new_member = XML::Node.new("member")
683         new_member["ref"] = element.id.to_s
684         new_member["type"] = element.class.to_s.downcase
685         new_member["role"] = "some_role"
686         relation_element << new_member
687
688         # update changeset ID to point to new changeset
689         update_changeset(relation_xml, changeset_id)
690
691         # upload the change
692         content relation_xml
693         put :update, :params => { :id => relation.id }
694         assert_response :success, "can't update relation for add #{element.class}/bbox test: #{@response.body}"
695
696         # get it back and check the ordering
697         get :read, :params => { :id => relation.id }
698         assert_response :success, "can't read back the relation: #{@response.body}"
699         check_ordering(relation_xml, @response.body)
700       end
701     end
702   end
703
704   ##
705   # remove a member from a relation and check the bounding box is
706   # only that element.
707   def test_remove_member_bounding_box
708     relation = create(:relation)
709     node1 = create(:node, :lat => 3, :lon => 3)
710     node2 = create(:node, :lat => 5, :lon => 5)
711     create(:relation_member, :relation => relation, :member => node1)
712     create(:relation_member, :relation => relation, :member => node2)
713
714     check_changeset_modify(BoundingBox.new(5, 5, 5, 5)) do |changeset_id|
715       # remove node 5 (5,5) from an existing relation
716       relation_xml = relation.to_xml
717       relation_xml
718         .find("//osm/relation/member[@type='node'][@ref='#{node2.id}']")
719         .first.remove!
720
721       # update changeset ID to point to new changeset
722       update_changeset(relation_xml, changeset_id)
723
724       # upload the change
725       content relation_xml
726       put :update, :params => { :id => relation.id }
727       assert_response :success, "can't update relation for remove node/bbox test"
728     end
729   end
730
731   ##
732   # check that relations are ordered
733   def test_relation_member_ordering
734     user = create(:user)
735     changeset = create(:changeset, :user => user)
736     node1 = create(:node)
737     node2 = create(:node)
738     node3 = create(:node)
739     way1 = create(:way_with_nodes, :nodes_count => 2)
740     way2 = create(:way_with_nodes, :nodes_count => 2)
741
742     basic_authorization user.email, "test"
743
744     doc_str = <<OSM.strip_heredoc
745       <osm>
746        <relation changeset='#{changeset.id}'>
747         <member ref='#{node1.id}' type='node' role='first'/>
748         <member ref='#{node2.id}' type='node' role='second'/>
749         <member ref='#{way1.id}' type='way' role='third'/>
750         <member ref='#{way2.id}' type='way' role='fourth'/>
751        </relation>
752       </osm>
753 OSM
754     doc = XML::Parser.string(doc_str).parse
755
756     content doc
757     put :create
758     assert_response :success, "can't create a relation: #{@response.body}"
759     relation_id = @response.body.to_i
760
761     # get it back and check the ordering
762     get :read, :params => { :id => relation_id }
763     assert_response :success, "can't read back the relation: #{@response.body}"
764     check_ordering(doc, @response.body)
765
766     # insert a member at the front
767     new_member = XML::Node.new "member"
768     new_member["ref"] = node3.id.to_s
769     new_member["type"] = "node"
770     new_member["role"] = "new first"
771     doc.find("//osm/relation").first.child.prev = new_member
772     # update the version, should be 1?
773     doc.find("//osm/relation").first["id"] = relation_id.to_s
774     doc.find("//osm/relation").first["version"] = 1.to_s
775
776     # upload the next version of the relation
777     content doc
778     put :update, :params => { :id => relation_id }
779     assert_response :success, "can't update relation: #{@response.body}"
780     assert_equal 2, @response.body.to_i
781
782     # get it back again and check the ordering again
783     get :read, :params => { :id => relation_id }
784     assert_response :success, "can't read back the relation: #{@response.body}"
785     check_ordering(doc, @response.body)
786
787     # check the ordering in the history tables:
788     with_controller(OldRelationController.new) do
789       get :version, :params => { :id => relation_id, :version => 2 }
790       assert_response :success, "can't read back version 2 of the relation #{relation_id}"
791       check_ordering(doc, @response.body)
792     end
793   end
794
795   ##
796   # check that relations can contain duplicate members
797   def test_relation_member_duplicates
798     private_user = create(:user, :data_public => false)
799     user = create(:user)
800     changeset = create(:changeset, :user => user)
801     node1 = create(:node)
802     node2 = create(:node)
803
804     doc_str = <<OSM.strip_heredoc
805       <osm>
806        <relation changeset='#{changeset.id}'>
807         <member ref='#{node1.id}' type='node' role='forward'/>
808         <member ref='#{node2.id}' type='node' role='forward'/>
809         <member ref='#{node1.id}' type='node' role='forward'/>
810         <member ref='#{node2.id}' type='node' role='forward'/>
811        </relation>
812       </osm>
813 OSM
814     doc = XML::Parser.string(doc_str).parse
815
816     ## First try with the private user
817     basic_authorization private_user.email, "test"
818
819     content doc
820     put :create
821     assert_response :forbidden
822
823     ## Now try with the public user
824     basic_authorization user.email, "test"
825
826     content doc
827     put :create
828     assert_response :success, "can't create a relation: #{@response.body}"
829     relation_id = @response.body.to_i
830
831     # get it back and check the ordering
832     get :read, :params => { :id => relation_id }
833     assert_response :success, "can't read back the relation: #{relation_id}"
834     check_ordering(doc, @response.body)
835   end
836
837   ##
838   # test that the ordering of elements in the history is the same as in current.
839   def test_history_ordering
840     user = create(:user)
841     changeset = create(:changeset, :user => user)
842     node1 = create(:node)
843     node2 = create(:node)
844     node3 = create(:node)
845     node4 = create(:node)
846
847     doc_str = <<OSM.strip_heredoc
848       <osm>
849        <relation changeset='#{changeset.id}'>
850         <member ref='#{node1.id}' type='node' role='forward'/>
851         <member ref='#{node4.id}' type='node' role='forward'/>
852         <member ref='#{node3.id}' type='node' role='forward'/>
853         <member ref='#{node2.id}' type='node' role='forward'/>
854        </relation>
855       </osm>
856 OSM
857     doc = XML::Parser.string(doc_str).parse
858     basic_authorization user.email, "test"
859
860     content doc
861     put :create
862     assert_response :success, "can't create a relation: #{@response.body}"
863     relation_id = @response.body.to_i
864
865     # check the ordering in the current tables:
866     get :read, :params => { :id => relation_id }
867     assert_response :success, "can't read back the relation: #{@response.body}"
868     check_ordering(doc, @response.body)
869
870     # check the ordering in the history tables:
871     with_controller(OldRelationController.new) do
872       get :version, :params => { :id => relation_id, :version => 1 }
873       assert_response :success, "can't read back version 1 of the relation: #{@response.body}"
874       check_ordering(doc, @response.body)
875     end
876   end
877
878   ##
879   # remove all the members from a relation. the result is pretty useless, but
880   # still technically valid.
881   def test_remove_all_members
882     relation = create(:relation)
883     node1 = create(:node, :lat => 3, :lon => 3)
884     node2 = create(:node, :lat => 5, :lon => 5)
885     way = create(:way)
886     create(:way_node, :way => way, :node => node1)
887     create(:relation_member, :relation => relation, :member => way)
888     create(:relation_member, :relation => relation, :member => node2)
889
890     check_changeset_modify(BoundingBox.new(3, 3, 5, 5)) do |changeset_id|
891       relation_xml = relation.to_xml
892       relation_xml
893         .find("//osm/relation/member")
894         .each(&:remove!)
895
896       # update changeset ID to point to new changeset
897       update_changeset(relation_xml, changeset_id)
898
899       # upload the change
900       content relation_xml
901       put :update, :params => { :id => relation.id }
902       assert_response :success, "can't update relation for remove all members test"
903       checkrelation = Relation.find(relation.id)
904       assert_not_nil(checkrelation,
905                      "uploaded relation not found in database after upload")
906       assert_equal(0, checkrelation.members.length,
907                    "relation contains members but they should have all been deleted")
908     end
909   end
910
911   # ============================================================
912   # utility functions
913   # ============================================================
914
915   ##
916   # checks that the XML document and the string arguments have
917   # members in the same order.
918   def check_ordering(doc, xml)
919     new_doc = XML::Parser.string(xml).parse
920
921     doc_members = doc.find("//osm/relation/member").collect do |m|
922       [m["ref"].to_i, m["type"].to_sym, m["role"]]
923     end
924
925     new_members = new_doc.find("//osm/relation/member").collect do |m|
926       [m["ref"].to_i, m["type"].to_sym, m["role"]]
927     end
928
929     doc_members.zip(new_members).each do |d, n|
930       assert_equal d, n, "members are not equal - ordering is wrong? (#{doc}, #{xml})"
931     end
932   end
933
934   ##
935   # create a changeset and yield to the caller to set it up, then assert
936   # that the changeset bounding box is +bbox+.
937   def check_changeset_modify(bbox)
938     ## First test with the private user to check that you get a forbidden
939     basic_authorization create(:user, :data_public => false).email, "test"
940
941     # create a new changeset for this operation, so we are assured
942     # that the bounding box will be newly-generated.
943     changeset_id = with_controller(ChangesetController.new) do
944       content "<osm><changeset/></osm>"
945       put :create
946       assert_response :forbidden, "shouldn't be able to create changeset for modify test, as should get forbidden"
947     end
948
949     ## Now do the whole thing with the public user
950     basic_authorization create(:user).email, "test"
951
952     # create a new changeset for this operation, so we are assured
953     # that the bounding box will be newly-generated.
954     changeset_id = with_controller(ChangesetController.new) do
955       content "<osm><changeset/></osm>"
956       put :create
957       assert_response :success, "couldn't create changeset for modify test"
958       @response.body.to_i
959     end
960
961     # go back to the block to do the actual modifies
962     yield changeset_id
963
964     # now download the changeset to check its bounding box
965     with_controller(ChangesetController.new) do
966       get :read, :params => { :id => changeset_id }
967       assert_response :success, "can't re-read changeset for modify test"
968       assert_select "osm>changeset", 1, "Changeset element doesn't exist in #{@response.body}"
969       assert_select "osm>changeset[id='#{changeset_id}']", 1, "Changeset id=#{changeset_id} doesn't exist in #{@response.body}"
970       assert_select "osm>changeset[min_lon='#{format('%.7f', bbox.min_lon)}']", 1, "Changeset min_lon wrong in #{@response.body}"
971       assert_select "osm>changeset[min_lat='#{format('%.7f', bbox.min_lat)}']", 1, "Changeset min_lat wrong in #{@response.body}"
972       assert_select "osm>changeset[max_lon='#{format('%.7f', bbox.max_lon)}']", 1, "Changeset max_lon wrong in #{@response.body}"
973       assert_select "osm>changeset[max_lat='#{format('%.7f', bbox.max_lat)}']", 1, "Changeset max_lat wrong in #{@response.body}"
974     end
975   end
976
977   ##
978   # yields the relation with the given +id+ (and optional +version+
979   # to read from the history tables) into the block. the parsed XML
980   # doc is returned.
981   def with_relation(id, ver = nil)
982     if ver.nil?
983       get :read, :params => { :id => id }
984     else
985       with_controller(OldRelationController.new) do
986         get :version, :params => { :id => id, :version => ver }
987       end
988     end
989     assert_response :success
990     yield xml_parse(@response.body)
991   end
992
993   ##
994   # updates the relation (XML) +rel+ and
995   # yields the new version of that relation into the block.
996   # the parsed XML doc is retured.
997   def with_update(rel)
998     rel_id = rel.find("//osm/relation").first["id"].to_i
999     content rel
1000     put :update, :params => { :id => rel_id }
1001     assert_response :success, "can't update relation: #{@response.body}"
1002     version = @response.body.to_i
1003
1004     # now get the new version
1005     get :read, :params => { :id => rel_id }
1006     assert_response :success
1007     new_rel = xml_parse(@response.body)
1008
1009     yield new_rel
1010
1011     version
1012   end
1013
1014   ##
1015   # updates the relation (XML) +rel+ via the diff-upload API and
1016   # yields the new version of that relation into the block.
1017   # the parsed XML doc is retured.
1018   def with_update_diff(rel)
1019     rel_id = rel.find("//osm/relation").first["id"].to_i
1020     cs_id = rel.find("//osm/relation").first["changeset"].to_i
1021     version = nil
1022
1023     with_controller(ChangesetController.new) do
1024       doc = OSM::API.new.get_xml_doc
1025       change = XML::Node.new "osmChange"
1026       doc.root = change
1027       modify = XML::Node.new "modify"
1028       change << modify
1029       modify << doc.import(rel.find("//osm/relation").first)
1030
1031       content doc.to_s
1032       post :upload, :params => { :id => cs_id }
1033       assert_response :success, "can't upload diff relation: #{@response.body}"
1034       version = xml_parse(@response.body).find("//diffResult/relation").first["new_version"].to_i
1035     end
1036
1037     # now get the new version
1038     get :read, :params => { :id => rel_id }
1039     assert_response :success
1040     new_rel = xml_parse(@response.body)
1041
1042     yield new_rel
1043
1044     version
1045   end
1046
1047   ##
1048   # returns a k->v hash of tags from an xml doc
1049   def get_tags_as_hash(a)
1050     a.find("//osm/relation/tag").sort_by { |v| v["k"] }.each_with_object({}) do |v, h|
1051       h[v["k"]] = v["v"]
1052     end
1053   end
1054
1055   ##
1056   # assert that all tags on relation documents +a+ and +b+
1057   # are equal
1058   def assert_tags_equal(a, b)
1059     # turn the XML doc into tags hashes
1060     a_tags = get_tags_as_hash(a)
1061     b_tags = get_tags_as_hash(b)
1062
1063     assert_equal a_tags.keys, b_tags.keys, "Tag keys should be identical."
1064     a_tags.each do |k, v|
1065       assert_equal v, b_tags[k],
1066                    "Tags which were not altered should be the same. " \
1067                    "#{a_tags.inspect} != #{b_tags.inspect}"
1068     end
1069   end
1070
1071   ##
1072   # update the changeset_id of a node element
1073   def update_changeset(xml, changeset_id)
1074     xml_attr_rewrite(xml, "changeset", changeset_id)
1075   end
1076
1077   ##
1078   # update an attribute in the node element
1079   def xml_attr_rewrite(xml, name, value)
1080     xml.find("//osm/relation").first[name] = value.to_s
1081     xml
1082   end
1083
1084   ##
1085   # parse some xml
1086   def xml_parse(xml)
1087     parser = XML::Parser.string(xml)
1088     parser.parse
1089   end
1090 end