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