]> git.openstreetmap.org Git - rails.git/blob - test/controllers/api/relations_controller_test.rb
Merge remote-tracking branch 'upstream/pull/1926'
[rails.git] / test / controllers / api / relations_controller_test.rb
1 require "test_helper"
2
3 module Api
4   class RelationsControllerTest < 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 => "api/relations", :action => "create" }
11       )
12       assert_routing(
13         { :path => "/api/0.6/relation/1/full", :method => :get },
14         { :controller => "api/relations", :action => "full", :id => "1" }
15       )
16       assert_routing(
17         { :path => "/api/0.6/relation/1", :method => :get },
18         { :controller => "api/relations", :action => "show", :id => "1" }
19       )
20       assert_routing(
21         { :path => "/api/0.6/relation/1", :method => :put },
22         { :controller => "api/relations", :action => "update", :id => "1" }
23       )
24       assert_routing(
25         { :path => "/api/0.6/relation/1", :method => :delete },
26         { :controller => "api/relations", :action => "delete", :id => "1" }
27       )
28       assert_routing(
29         { :path => "/api/0.6/relations", :method => :get },
30         { :controller => "api/relations", :action => "index" }
31       )
32
33       assert_routing(
34         { :path => "/api/0.6/node/1/relations", :method => :get },
35         { :controller => "api/relations", :action => "relations_for_node", :id => "1" }
36       )
37       assert_routing(
38         { :path => "/api/0.6/way/1/relations", :method => :get },
39         { :controller => "api/relations", :action => "relations_for_way", :id => "1" }
40       )
41       assert_routing(
42         { :path => "/api/0.6/relation/1/relations", :method => :get },
43         { :controller => "api/relations", :action => "relations_for_relation", :id => "1" }
44       )
45     end
46
47     # -------------------------------------
48     # Test showing relations.
49     # -------------------------------------
50
51     def test_show
52       # check that a visible relation is returned properly
53       get :show, :params => { :id => create(:relation).id }
54       assert_response :success
55
56       # check that an invisible relation is not returned
57       get :show, :params => { :id => create(:relation, :deleted).id }
58       assert_response :gone
59
60       # check chat a non-existent relation is not returned
61       get :show, :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='#{Settings.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_index
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 :index
173       assert_response :bad_request
174
175       # check error when no parameter value provided
176       get :index, :params => { :relations => "" }
177       assert_response :bad_request
178
179       # test a working call
180       get :index, :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 :index, :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       xml = "<osm><relation changeset='#{private_changeset.id}'><tag k='test' v='yes' /></relation></osm>"
211       put :create, :body => xml
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       xml = "<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, :body => xml
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       xml = "<osm><relation changeset='#{private_changeset.id}'>" \
231             "<member  ref='#{node.id}' type='node'/>" + "<tag k='test' v='yes' /></relation></osm>"
232       put :create, :body => xml
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       xml = "<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, :body => xml
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       xml = "<osm><relation changeset='#{changeset.id}'><tag k='test' v='yes' /></relation></osm>"
253       put :create, :body => xml
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 :show, :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       xml = "<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, :body => xml
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 :show, :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       xml = "<osm><relation changeset='#{changeset.id}'>" \
312             "<member  ref='#{node.id}' type='node'/>" + "<tag k='test' v='yes' /></relation></osm>"
313       put :create, :body => xml
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 :show, :params => { :id => relationid }
336       assert_response :success
337
338       ###
339       # create an relation with a way and a node as members
340       xml = "<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, :body => xml
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 :show, :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         put :update, :params => { :id => other_relation.id }, :body => rel.to_s
448         assert_response :bad_request
449       end
450     end
451
452     # -------------------------------------
453     # Test creating some invalid relations.
454     # -------------------------------------
455
456     def test_create_invalid
457       user = create(:user)
458       changeset = create(:changeset, :user => user)
459
460       basic_authorization user.email, "test"
461
462       # create a relation with non-existing node as member
463       xml = "<osm><relation changeset='#{changeset.id}'>" \
464             "<member type='node' ref='0'/><tag k='test' v='yes' />" \
465             "</relation></osm>"
466       put :create, :body => xml
467       # expect failure
468       assert_response :precondition_failed,
469                       "relation upload with invalid node did not return 'precondition failed'"
470       assert_equal "Precondition failed: Relation with id  cannot be saved due to Node with id 0", @response.body
471     end
472
473     # -------------------------------------
474     # Test creating a relation, with some invalid XML
475     # -------------------------------------
476     def test_create_invalid_xml
477       user = create(:user)
478       changeset = create(:changeset, :user => user)
479       node = create(:node)
480
481       basic_authorization user.email, "test"
482
483       # create some xml that should return an error
484       xml = "<osm><relation changeset='#{changeset.id}'>" \
485             "<member type='type' ref='#{node.id}' role=''/>" \
486             "<tag k='tester' v='yep'/></relation></osm>"
487       put :create, :body => xml
488       # expect failure
489       assert_response :bad_request
490       assert_match(/Cannot parse valid relation from xml string/, @response.body)
491       assert_match(/The type is not allowed only, /, @response.body)
492     end
493
494     # -------------------------------------
495     # Test deleting relations.
496     # -------------------------------------
497
498     def test_delete
499       private_user = create(:user, :data_public => false)
500       private_user_closed_changeset = create(:changeset, :closed, :user => private_user)
501       user = create(:user)
502       closed_changeset = create(:changeset, :closed, :user => user)
503       changeset = create(:changeset, :user => user)
504       relation = create(:relation)
505       used_relation = create(:relation)
506       super_relation = create(:relation_member, :member => used_relation).relation
507       deleted_relation = create(:relation, :deleted)
508       multi_tag_relation = create(:relation)
509       create_list(:relation_tag, 4, :relation => multi_tag_relation)
510
511       ## First try to delete relation without auth
512       delete :delete, :params => { :id => relation.id }
513       assert_response :unauthorized
514
515       ## Then try with the private user, to make sure that you get a forbidden
516       basic_authorization private_user.email, "test"
517
518       # this shouldn't work, as we should need the payload...
519       delete :delete, :params => { :id => relation.id }
520       assert_response :forbidden
521
522       # try to delete without specifying a changeset
523       xml = "<osm><relation id='#{relation.id}'/></osm>"
524       delete :delete, :params => { :id => relation.id }, :body => xml.to_s
525       assert_response :forbidden
526
527       # try to delete with an invalid (closed) changeset
528       xml = update_changeset(relation.to_xml,
529                              private_user_closed_changeset.id)
530       delete :delete, :params => { :id => relation.id }, :body => xml.to_s
531       assert_response :forbidden
532
533       # try to delete with an invalid (non-existent) changeset
534       xml = update_changeset(relation.to_xml, 0)
535       delete :delete, :params => { :id => relation.id }, :body => xml.to_s
536       assert_response :forbidden
537
538       # this won't work because the relation is in-use by another relation
539       xml = used_relation.to_xml
540       delete :delete, :params => { :id => used_relation.id }, :body => xml.to_s
541       assert_response :forbidden
542
543       # this should work when we provide the appropriate payload...
544       xml = relation.to_xml
545       delete :delete, :params => { :id => relation.id }, :body => xml.to_s
546       assert_response :forbidden
547
548       # this won't work since the relation is already deleted
549       xml = deleted_relation.to_xml
550       delete :delete, :params => { :id => deleted_relation.id }, :body => xml.to_s
551       assert_response :forbidden
552
553       # this won't work since the relation never existed
554       delete :delete, :params => { :id => 0 }
555       assert_response :forbidden
556
557       ## now set auth for the public user
558       basic_authorization user.email, "test"
559
560       # this shouldn't work, as we should need the payload...
561       delete :delete, :params => { :id => relation.id }
562       assert_response :bad_request
563
564       # try to delete without specifying a changeset
565       xml = "<osm><relation id='#{relation.id}' version='#{relation.version}' /></osm>"
566       delete :delete, :params => { :id => relation.id }, :body => xml.to_s
567       assert_response :bad_request
568       assert_match(/Changeset id is missing/, @response.body)
569
570       # try to delete with an invalid (closed) changeset
571       xml = update_changeset(relation.to_xml,
572                              closed_changeset.id)
573       delete :delete, :params => { :id => relation.id }, :body => xml.to_s
574       assert_response :conflict
575
576       # try to delete with an invalid (non-existent) changeset
577       xml = update_changeset(relation.to_xml, 0)
578       delete :delete, :params => { :id => relation.id }, :body => xml.to_s
579       assert_response :conflict
580
581       # this won't work because the relation is in a changeset owned by someone else
582       xml = update_changeset(relation.to_xml, create(:changeset).id)
583       delete :delete, :params => { :id => relation.id }, :body => xml.to_s
584       assert_response :conflict,
585                       "shouldn't be able to delete a relation in a changeset owned by someone else (#{@response.body})"
586
587       # this won't work because the relation in the payload is different to that passed
588       xml = update_changeset(relation.to_xml, changeset.id)
589       delete :delete, :params => { :id => create(:relation).id }, :body => xml.to_s
590       assert_response :bad_request, "shouldn't be able to delete a relation when payload is different to the url"
591
592       # this won't work because the relation is in-use by another relation
593       xml = update_changeset(used_relation.to_xml, changeset.id)
594       delete :delete, :params => { :id => used_relation.id }, :body => xml.to_s
595       assert_response :precondition_failed,
596                       "shouldn't be able to delete a relation used in a relation (#{@response.body})"
597       assert_equal "Precondition failed: The relation #{used_relation.id} is used in relation #{super_relation.id}.", @response.body
598
599       # this should work when we provide the appropriate payload...
600       xml = update_changeset(multi_tag_relation.to_xml, changeset.id)
601       delete :delete, :params => { :id => multi_tag_relation.id }, :body => xml.to_s
602       assert_response :success
603
604       # valid delete should return the new version number, which should
605       # be greater than the old version number
606       assert @response.body.to_i > multi_tag_relation.version,
607              "delete request should return a new version number for relation"
608
609       # this won't work since the relation is already deleted
610       xml = update_changeset(deleted_relation.to_xml, changeset.id)
611       delete :delete, :params => { :id => deleted_relation.id }, :body => xml.to_s
612       assert_response :gone
613
614       # Public visible relation needs to be deleted
615       xml = update_changeset(super_relation.to_xml, changeset.id)
616       delete :delete, :params => { :id => super_relation.id }, :body => xml.to_s
617       assert_response :success
618
619       # this works now because the relation which was using this one
620       # has been deleted.
621       xml = update_changeset(used_relation.to_xml, changeset.id)
622       delete :delete, :params => { :id => used_relation.id }, :body => xml.to_s
623       assert_response :success,
624                       "should be able to delete a relation used in an old relation (#{@response.body})"
625
626       # this won't work since the relation never existed
627       delete :delete, :params => { :id => 0 }
628       assert_response :not_found
629     end
630
631     ##
632     # when a relation's tag is modified then it should put the bounding
633     # box of all its members into the changeset.
634     def test_tag_modify_bounding_box
635       relation = create(:relation)
636       node1 = create(:node, :lat => 3, :lon => 3)
637       node2 = create(:node, :lat => 5, :lon => 5)
638       way = create(:way)
639       create(:way_node, :way => way, :node => node1)
640       create(:relation_member, :relation => relation, :member => way)
641       create(:relation_member, :relation => relation, :member => node2)
642       # the relation contains nodes1 and node2 (node1
643       # indirectly via the way), so the bbox should be [3,3,5,5].
644       check_changeset_modify(BoundingBox.new(3, 3, 5, 5)) do |changeset_id|
645         # add a tag to an existing relation
646         relation_xml = relation.to_xml
647         relation_element = relation_xml.find("//osm/relation").first
648         new_tag = XML::Node.new("tag")
649         new_tag["k"] = "some_new_tag"
650         new_tag["v"] = "some_new_value"
651         relation_element << new_tag
652
653         # update changeset ID to point to new changeset
654         update_changeset(relation_xml, changeset_id)
655
656         # upload the change
657         put :update, :params => { :id => relation.id }, :body => relation_xml.to_s
658         assert_response :success, "can't update relation for tag/bbox test"
659       end
660     end
661
662     ##
663     # add a member to a relation and check the bounding box is only that
664     # element.
665     def test_add_member_bounding_box
666       relation = create(:relation)
667       node1 = create(:node, :lat => 4, :lon => 4)
668       node2 = create(:node, :lat => 7, :lon => 7)
669       way1 = create(:way)
670       create(:way_node, :way => way1, :node => create(:node, :lat => 8, :lon => 8))
671       way2 = create(:way)
672       create(:way_node, :way => way2, :node => create(:node, :lat => 9, :lon => 9), :sequence_id => 1)
673       create(:way_node, :way => way2, :node => create(:node, :lat => 10, :lon => 10), :sequence_id => 2)
674
675       [node1, node2, way1, way2].each do |element|
676         bbox = element.bbox.to_unscaled
677         check_changeset_modify(bbox) do |changeset_id|
678           relation_xml = Relation.find(relation.id).to_xml
679           relation_element = relation_xml.find("//osm/relation").first
680           new_member = XML::Node.new("member")
681           new_member["ref"] = element.id.to_s
682           new_member["type"] = element.class.to_s.downcase
683           new_member["role"] = "some_role"
684           relation_element << new_member
685
686           # update changeset ID to point to new changeset
687           update_changeset(relation_xml, changeset_id)
688
689           # upload the change
690           put :update, :params => { :id => relation.id }, :body => relation_xml.to_s
691           assert_response :success, "can't update relation for add #{element.class}/bbox test: #{@response.body}"
692
693           # get it back and check the ordering
694           get :show, :params => { :id => relation.id }
695           assert_response :success, "can't read back the relation: #{@response.body}"
696           check_ordering(relation_xml, @response.body)
697         end
698       end
699     end
700
701     ##
702     # remove a member from a relation and check the bounding box is
703     # only that element.
704     def test_remove_member_bounding_box
705       relation = create(:relation)
706       node1 = create(:node, :lat => 3, :lon => 3)
707       node2 = create(:node, :lat => 5, :lon => 5)
708       create(:relation_member, :relation => relation, :member => node1)
709       create(:relation_member, :relation => relation, :member => node2)
710
711       check_changeset_modify(BoundingBox.new(5, 5, 5, 5)) do |changeset_id|
712         # remove node 5 (5,5) from an existing relation
713         relation_xml = relation.to_xml
714         relation_xml
715           .find("//osm/relation/member[@type='node'][@ref='#{node2.id}']")
716           .first.remove!
717
718         # update changeset ID to point to new changeset
719         update_changeset(relation_xml, changeset_id)
720
721         # upload the change
722         put :update, :params => { :id => relation.id }, :body => relation_xml.to_s
723         assert_response :success, "can't update relation for remove node/bbox test"
724       end
725     end
726
727     ##
728     # check that relations are ordered
729     def test_relation_member_ordering
730       user = create(:user)
731       changeset = create(:changeset, :user => user)
732       node1 = create(:node)
733       node2 = create(:node)
734       node3 = create(:node)
735       way1 = create(:way_with_nodes, :nodes_count => 2)
736       way2 = create(:way_with_nodes, :nodes_count => 2)
737
738       basic_authorization user.email, "test"
739
740       doc_str = <<OSM.strip_heredoc
741         <osm>
742          <relation changeset='#{changeset.id}'>
743           <member ref='#{node1.id}' type='node' role='first'/>
744           <member ref='#{node2.id}' type='node' role='second'/>
745           <member ref='#{way1.id}' type='way' role='third'/>
746           <member ref='#{way2.id}' type='way' role='fourth'/>
747          </relation>
748         </osm>
749 OSM
750       doc = XML::Parser.string(doc_str).parse
751
752       put :create, :body => doc.to_s
753       assert_response :success, "can't create a relation: #{@response.body}"
754       relation_id = @response.body.to_i
755
756       # get it back and check the ordering
757       get :show, :params => { :id => relation_id }
758       assert_response :success, "can't read back the relation: #{@response.body}"
759       check_ordering(doc, @response.body)
760
761       # insert a member at the front
762       new_member = XML::Node.new "member"
763       new_member["ref"] = node3.id.to_s
764       new_member["type"] = "node"
765       new_member["role"] = "new first"
766       doc.find("//osm/relation").first.child.prev = new_member
767       # update the version, should be 1?
768       doc.find("//osm/relation").first["id"] = relation_id.to_s
769       doc.find("//osm/relation").first["version"] = 1.to_s
770
771       # upload the next version of the relation
772       put :update, :params => { :id => relation_id }, :body => doc.to_s
773       assert_response :success, "can't update relation: #{@response.body}"
774       assert_equal 2, @response.body.to_i
775
776       # get it back again and check the ordering again
777       get :show, :params => { :id => relation_id }
778       assert_response :success, "can't read back the relation: #{@response.body}"
779       check_ordering(doc, @response.body)
780
781       # check the ordering in the history tables:
782       with_controller(OldRelationsController.new) do
783         get :version, :params => { :id => relation_id, :version => 2 }
784         assert_response :success, "can't read back version 2 of the relation #{relation_id}"
785         check_ordering(doc, @response.body)
786       end
787     end
788
789     ##
790     # check that relations can contain duplicate members
791     def test_relation_member_duplicates
792       private_user = create(:user, :data_public => false)
793       user = create(:user)
794       changeset = create(:changeset, :user => user)
795       node1 = create(:node)
796       node2 = create(:node)
797
798       doc_str = <<OSM.strip_heredoc
799         <osm>
800          <relation changeset='#{changeset.id}'>
801           <member ref='#{node1.id}' type='node' role='forward'/>
802           <member ref='#{node2.id}' type='node' role='forward'/>
803           <member ref='#{node1.id}' type='node' role='forward'/>
804           <member ref='#{node2.id}' type='node' role='forward'/>
805          </relation>
806         </osm>
807 OSM
808       doc = XML::Parser.string(doc_str).parse
809
810       ## First try with the private user
811       basic_authorization private_user.email, "test"
812
813       put :create, :body => doc.to_s
814       assert_response :forbidden
815
816       ## Now try with the public user
817       basic_authorization user.email, "test"
818
819       put :create, :body => doc.to_s
820       assert_response :success, "can't create a relation: #{@response.body}"
821       relation_id = @response.body.to_i
822
823       # get it back and check the ordering
824       get :show, :params => { :id => relation_id }
825       assert_response :success, "can't read back the relation: #{relation_id}"
826       check_ordering(doc, @response.body)
827     end
828
829     ##
830     # test that the ordering of elements in the history is the same as in current.
831     def test_history_ordering
832       user = create(:user)
833       changeset = create(:changeset, :user => user)
834       node1 = create(:node)
835       node2 = create(:node)
836       node3 = create(:node)
837       node4 = create(:node)
838
839       doc_str = <<OSM.strip_heredoc
840         <osm>
841          <relation changeset='#{changeset.id}'>
842           <member ref='#{node1.id}' type='node' role='forward'/>
843           <member ref='#{node4.id}' type='node' role='forward'/>
844           <member ref='#{node3.id}' type='node' role='forward'/>
845           <member ref='#{node2.id}' type='node' role='forward'/>
846          </relation>
847         </osm>
848 OSM
849       doc = XML::Parser.string(doc_str).parse
850       basic_authorization user.email, "test"
851
852       put :create, :body => doc.to_s
853       assert_response :success, "can't create a relation: #{@response.body}"
854       relation_id = @response.body.to_i
855
856       # check the ordering in the current tables:
857       get :show, :params => { :id => relation_id }
858       assert_response :success, "can't read back the relation: #{@response.body}"
859       check_ordering(doc, @response.body)
860
861       # check the ordering in the history tables:
862       with_controller(OldRelationsController.new) do
863         get :version, :params => { :id => relation_id, :version => 1 }
864         assert_response :success, "can't read back version 1 of the relation: #{@response.body}"
865         check_ordering(doc, @response.body)
866       end
867     end
868
869     ##
870     # remove all the members from a relation. the result is pretty useless, but
871     # still technically valid.
872     def test_remove_all_members
873       relation = create(:relation)
874       node1 = create(:node, :lat => 3, :lon => 3)
875       node2 = create(:node, :lat => 5, :lon => 5)
876       way = create(:way)
877       create(:way_node, :way => way, :node => node1)
878       create(:relation_member, :relation => relation, :member => way)
879       create(:relation_member, :relation => relation, :member => node2)
880
881       check_changeset_modify(BoundingBox.new(3, 3, 5, 5)) do |changeset_id|
882         relation_xml = relation.to_xml
883         relation_xml
884           .find("//osm/relation/member")
885           .each(&:remove!)
886
887         # update changeset ID to point to new changeset
888         update_changeset(relation_xml, changeset_id)
889
890         # upload the change
891         put :update, :params => { :id => relation.id }, :body => relation_xml.to_s
892         assert_response :success, "can't update relation for remove all members test"
893         checkrelation = Relation.find(relation.id)
894         assert_not_nil(checkrelation,
895                        "uploaded relation not found in database after upload")
896         assert_equal(0, checkrelation.members.length,
897                      "relation contains members but they should have all been deleted")
898       end
899     end
900
901     # ============================================================
902     # utility functions
903     # ============================================================
904
905     ##
906     # checks that the XML document and the string arguments have
907     # members in the same order.
908     def check_ordering(doc, xml)
909       new_doc = XML::Parser.string(xml).parse
910
911       doc_members = doc.find("//osm/relation/member").collect do |m|
912         [m["ref"].to_i, m["type"].to_sym, m["role"]]
913       end
914
915       new_members = new_doc.find("//osm/relation/member").collect do |m|
916         [m["ref"].to_i, m["type"].to_sym, m["role"]]
917       end
918
919       doc_members.zip(new_members).each do |d, n|
920         assert_equal d, n, "members are not equal - ordering is wrong? (#{doc}, #{xml})"
921       end
922     end
923
924     ##
925     # create a changeset and yield to the caller to set it up, then assert
926     # that the changeset bounding box is +bbox+.
927     def check_changeset_modify(bbox)
928       ## First test with the private user to check that you get a forbidden
929       basic_authorization create(:user, :data_public => false).email, "test"
930
931       # create a new changeset for this operation, so we are assured
932       # that the bounding box will be newly-generated.
933       changeset_id = with_controller(Api::ChangesetsController.new) do
934         xml = "<osm><changeset/></osm>"
935         put :create, :body => xml
936         assert_response :forbidden, "shouldn't be able to create changeset for modify test, as should get forbidden"
937       end
938
939       ## Now do the whole thing with the public user
940       basic_authorization create(:user).email, "test"
941
942       # create a new changeset for this operation, so we are assured
943       # that the bounding box will be newly-generated.
944       changeset_id = with_controller(Api::ChangesetsController.new) do
945         xml = "<osm><changeset/></osm>"
946         put :create, :body => xml
947         assert_response :success, "couldn't create changeset for modify test"
948         @response.body.to_i
949       end
950
951       # go back to the block to do the actual modifies
952       yield changeset_id
953
954       # now download the changeset to check its bounding box
955       with_controller(Api::ChangesetsController.new) do
956         get :show, :params => { :id => changeset_id }
957         assert_response :success, "can't re-read changeset for modify test"
958         assert_select "osm>changeset", 1, "Changeset element doesn't exist in #{@response.body}"
959         assert_select "osm>changeset[id='#{changeset_id}']", 1, "Changeset id=#{changeset_id} doesn't exist in #{@response.body}"
960         assert_select "osm>changeset[min_lon='#{format('%.7f', bbox.min_lon)}']", 1, "Changeset min_lon wrong in #{@response.body}"
961         assert_select "osm>changeset[min_lat='#{format('%.7f', bbox.min_lat)}']", 1, "Changeset min_lat wrong in #{@response.body}"
962         assert_select "osm>changeset[max_lon='#{format('%.7f', bbox.max_lon)}']", 1, "Changeset max_lon wrong in #{@response.body}"
963         assert_select "osm>changeset[max_lat='#{format('%.7f', bbox.max_lat)}']", 1, "Changeset max_lat wrong in #{@response.body}"
964       end
965     end
966
967     ##
968     # yields the relation with the given +id+ (and optional +version+
969     # to read from the history tables) into the block. the parsed XML
970     # doc is returned.
971     def with_relation(id, ver = nil)
972       if ver.nil?
973         get :show, :params => { :id => id }
974       else
975         with_controller(OldRelationsController.new) do
976           get :version, :params => { :id => id, :version => ver }
977         end
978       end
979       assert_response :success
980       yield xml_parse(@response.body)
981     end
982
983     ##
984     # updates the relation (XML) +rel+ and
985     # yields the new version of that relation into the block.
986     # the parsed XML doc is retured.
987     def with_update(rel)
988       rel_id = rel.find("//osm/relation").first["id"].to_i
989       put :update, :params => { :id => rel_id }, :body => rel.to_s
990       assert_response :success, "can't update relation: #{@response.body}"
991       version = @response.body.to_i
992
993       # now get the new version
994       get :show, :params => { :id => rel_id }
995       assert_response :success
996       new_rel = xml_parse(@response.body)
997
998       yield new_rel
999
1000       version
1001     end
1002
1003     ##
1004     # updates the relation (XML) +rel+ via the diff-upload API and
1005     # yields the new version of that relation into the block.
1006     # the parsed XML doc is retured.
1007     def with_update_diff(rel)
1008       rel_id = rel.find("//osm/relation").first["id"].to_i
1009       cs_id = rel.find("//osm/relation").first["changeset"].to_i
1010       version = nil
1011
1012       with_controller(Api::ChangesetsController.new) do
1013         doc = OSM::API.new.get_xml_doc
1014         change = XML::Node.new "osmChange"
1015         doc.root = change
1016         modify = XML::Node.new "modify"
1017         change << modify
1018         modify << doc.import(rel.find("//osm/relation").first)
1019
1020         post :upload, :params => { :id => cs_id }, :body => doc.to_s
1021         assert_response :success, "can't upload diff relation: #{@response.body}"
1022         version = xml_parse(@response.body).find("//diffResult/relation").first["new_version"].to_i
1023       end
1024
1025       # now get the new version
1026       get :show, :params => { :id => rel_id }
1027       assert_response :success
1028       new_rel = xml_parse(@response.body)
1029
1030       yield new_rel
1031
1032       version
1033     end
1034
1035     ##
1036     # returns a k->v hash of tags from an xml doc
1037     def get_tags_as_hash(a)
1038       a.find("//osm/relation/tag").sort_by { |v| v["k"] }.each_with_object({}) do |v, h|
1039         h[v["k"]] = v["v"]
1040       end
1041     end
1042
1043     ##
1044     # assert that all tags on relation documents +a+ and +b+
1045     # are equal
1046     def assert_tags_equal(a, b)
1047       # turn the XML doc into tags hashes
1048       a_tags = get_tags_as_hash(a)
1049       b_tags = get_tags_as_hash(b)
1050
1051       assert_equal a_tags.keys, b_tags.keys, "Tag keys should be identical."
1052       a_tags.each do |k, v|
1053         assert_equal v, b_tags[k],
1054                      "Tags which were not altered should be the same. " \
1055                      "#{a_tags.inspect} != #{b_tags.inspect}"
1056       end
1057     end
1058
1059     ##
1060     # update the changeset_id of a node element
1061     def update_changeset(xml, changeset_id)
1062       xml_attr_rewrite(xml, "changeset", changeset_id)
1063     end
1064
1065     ##
1066     # update an attribute in the node element
1067     def xml_attr_rewrite(xml, name, value)
1068       xml.find("//osm/relation").first[name] = value.to_s
1069       xml
1070     end
1071
1072     ##
1073     # parse some xml
1074     def xml_parse(xml)
1075       parser = XML::Parser.string(xml)
1076       parser.parse
1077     end
1078   end
1079 end