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