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