4 class RelationsControllerTest < ActionDispatch::IntegrationTest
6 # test all routes which lead to this controller
9 { :path => "/api/0.6/relation/create", :method => :put },
10 { :controller => "api/relations", :action => "create" }
13 { :path => "/api/0.6/relation/1/full", :method => :get },
14 { :controller => "api/relations", :action => "full", :id => "1" }
17 { :path => "/api/0.6/relation/1/full.json", :method => :get },
18 { :controller => "api/relations", :action => "full", :id => "1", :format => "json" }
21 { :path => "/api/0.6/relation/1", :method => :get },
22 { :controller => "api/relations", :action => "show", :id => "1" }
25 { :path => "/api/0.6/relation/1.json", :method => :get },
26 { :controller => "api/relations", :action => "show", :id => "1", :format => "json" }
29 { :path => "/api/0.6/relation/1", :method => :put },
30 { :controller => "api/relations", :action => "update", :id => "1" }
33 { :path => "/api/0.6/relation/1", :method => :delete },
34 { :controller => "api/relations", :action => "delete", :id => "1" }
37 { :path => "/api/0.6/relations", :method => :get },
38 { :controller => "api/relations", :action => "index" }
41 { :path => "/api/0.6/relations.json", :method => :get },
42 { :controller => "api/relations", :action => "index", :format => "json" }
46 { :path => "/api/0.6/node/1/relations", :method => :get },
47 { :controller => "api/relations", :action => "relations_for_node", :id => "1" }
50 { :path => "/api/0.6/way/1/relations", :method => :get },
51 { :controller => "api/relations", :action => "relations_for_way", :id => "1" }
54 { :path => "/api/0.6/relation/1/relations", :method => :get },
55 { :controller => "api/relations", :action => "relations_for_relation", :id => "1" }
58 { :path => "/api/0.6/node/1/relations.json", :method => :get },
59 { :controller => "api/relations", :action => "relations_for_node", :id => "1", :format => "json" }
62 { :path => "/api/0.6/way/1/relations.json", :method => :get },
63 { :controller => "api/relations", :action => "relations_for_way", :id => "1", :format => "json" }
66 { :path => "/api/0.6/relation/1/relations.json", :method => :get },
67 { :controller => "api/relations", :action => "relations_for_relation", :id => "1", :format => "json" }
71 # -------------------------------------
72 # Test showing relations.
73 # -------------------------------------
76 # check that a visible relation is returned properly
77 get api_relation_path(create(:relation))
78 assert_response :success
80 # check that an invisible relation is not returned
81 get api_relation_path(create(:relation, :deleted))
84 # check chat a non-existent relation is not returned
85 get api_relation_path(:id => 0)
86 assert_response :not_found
90 # check that all relations containing a particular node, and no extra
91 # relations, are returned from the relations_for_node call.
92 def test_relations_for_node
94 # should include relations with that node as a member
95 relation_with_node = create(:relation_member, :member => node).relation
96 # should ignore relations without that node as a member
97 _relation_without_node = create(:relation_member).relation
98 # should ignore relations with the node involved indirectly, via a way
99 way = create(:way_node, :node => node).way
100 _relation_with_way = create(:relation_member, :member => way).relation
101 # should ignore relations with the node involved indirectly, via a relation
102 second_relation = create(:relation_member, :member => node).relation
103 _super_relation = create(:relation_member, :member => second_relation).relation
104 # should combine multiple relation_member references into just one relation entry
105 create(:relation_member, :member => node, :relation => relation_with_node, :sequence_id => 2)
106 # should not include deleted relations
107 deleted_relation = create(:relation, :deleted)
108 create(:relation_member, :member => node, :relation => deleted_relation)
110 check_relations_for_element(node_relations_path(node), "node",
112 [relation_with_node, second_relation])
115 def test_relations_for_way
117 # should include relations with that way as a member
118 relation_with_way = create(:relation_member, :member => way).relation
119 # should ignore relations without that way as a member
120 _relation_without_way = create(:relation_member).relation
121 # should ignore relations with the way involved indirectly, via a relation
122 second_relation = create(:relation_member, :member => way).relation
123 _super_relation = create(:relation_member, :member => second_relation).relation
124 # should combine multiple relation_member references into just one relation entry
125 create(:relation_member, :member => way, :relation => relation_with_way, :sequence_id => 2)
126 # should not include deleted relations
127 deleted_relation = create(:relation, :deleted)
128 create(:relation_member, :member => way, :relation => deleted_relation)
130 check_relations_for_element(way_relations_path(way), "way",
132 [relation_with_way, second_relation])
135 def test_relations_for_relation
136 relation = create(:relation)
137 # should include relations with that relation as a member
138 relation_with_relation = create(:relation_member, :member => relation).relation
139 # should ignore any relation without that relation as a member
140 _relation_without_relation = create(:relation_member).relation
141 # should ignore relations with the relation involved indirectly, via a relation
142 second_relation = create(:relation_member, :member => relation).relation
143 _super_relation = create(:relation_member, :member => second_relation).relation
144 # should combine multiple relation_member references into just one relation entry
145 create(:relation_member, :member => relation, :relation => relation_with_relation, :sequence_id => 2)
146 # should not include deleted relations
147 deleted_relation = create(:relation, :deleted)
148 create(:relation_member, :member => relation, :relation => deleted_relation)
149 check_relations_for_element(relation_relations_path(relation), "relation",
151 [relation_with_relation, second_relation])
155 # check the "full" mode
156 get relation_full_path(:id => 999999)
157 assert_response :not_found
159 get relation_full_path(:id => create(:relation, :deleted).id)
160 assert_response :gone
162 get relation_full_path(:id => create(:relation).id)
163 assert_response :success
164 # FIXME: check whether this contains the stuff we want!
168 # test fetching multiple relations
170 relation1 = create(:relation)
171 relation2 = create(:relation, :deleted)
172 relation3 = create(:relation, :with_history, :version => 2)
173 relation4 = create(:relation, :with_history, :version => 2)
174 relation4.old_relations.find_by(:version => 1).redact!(create(:redaction))
176 # check error when no parameter provided
178 assert_response :bad_request
180 # check error when no parameter value provided
181 get relations_path, :params => { :relations => "" }
182 assert_response :bad_request
184 # test a working call
185 get relations_path, :params => { :relations => "#{relation1.id},#{relation2.id},#{relation3.id},#{relation4.id}" }
186 assert_response :success
187 assert_select "osm" do
188 assert_select "relation", :count => 4
189 assert_select "relation[id='#{relation1.id}'][visible='true']", :count => 1
190 assert_select "relation[id='#{relation2.id}'][visible='false']", :count => 1
191 assert_select "relation[id='#{relation3.id}'][visible='true']", :count => 1
192 assert_select "relation[id='#{relation4.id}'][visible='true']", :count => 1
195 # test a working call with json format
196 get relations_path, :params => { :relations => "#{relation1.id},#{relation2.id},#{relation3.id},#{relation4.id}", :format => "json" }
198 js = ActiveSupport::JSON.decode(@response.body)
200 assert_equal 4, js["elements"].count
201 assert_equal 4, (js["elements"].count { |a| a["type"] == "relation" })
202 assert_equal 1, (js["elements"].count { |a| a["id"] == relation1.id && a["visible"].nil? })
203 assert_equal 1, (js["elements"].count { |a| a["id"] == relation2.id && a["visible"] == false })
204 assert_equal 1, (js["elements"].count { |a| a["id"] == relation3.id && a["visible"].nil? })
205 assert_equal 1, (js["elements"].count { |a| a["id"] == relation4.id && a["visible"].nil? })
207 # check error when a non-existent relation is included
208 get relations_path, :params => { :relations => "#{relation1.id},#{relation2.id},#{relation3.id},#{relation4.id},0" }
209 assert_response :not_found
212 # -------------------------------------
213 # Test simple relation creation.
214 # -------------------------------------
217 private_user = create(:user, :data_public => false)
218 private_changeset = create(:changeset, :user => private_user)
220 changeset = create(:changeset, :user => user)
222 way = create(:way_with_nodes, :nodes_count => 2)
224 auth_header = basic_authorization_header private_user.email, "test"
226 # create an relation without members
227 xml = "<osm><relation changeset='#{private_changeset.id}'><tag k='test' v='yes' /></relation></osm>"
228 put relation_create_path, :params => xml, :headers => auth_header
229 # hope for forbidden, due to user
230 assert_response :forbidden,
231 "relation upload should have failed with forbidden"
234 # create an relation with a node as member
235 # This time try with a role attribute in the relation
236 xml = "<osm><relation changeset='#{private_changeset.id}'>" \
237 "<member ref='#{node.id}' type='node' role='some'/>" \
238 "<tag k='test' v='yes' /></relation></osm>"
239 put relation_create_path, :params => xml, :headers => auth_header
240 # hope for forbidden due to user
241 assert_response :forbidden,
242 "relation upload did not return forbidden status"
245 # create an relation with a node as member, this time test that we don't
246 # need a role attribute to be included
247 xml = "<osm><relation changeset='#{private_changeset.id}'>" \
248 "<member ref='#{node.id}' type='node'/><tag k='test' v='yes' /></relation></osm>"
249 put relation_create_path, :params => xml, :headers => auth_header
250 # hope for forbidden due to user
251 assert_response :forbidden,
252 "relation upload did not return forbidden status"
255 # create an relation with a way and a node as members
256 xml = "<osm><relation changeset='#{private_changeset.id}'>" \
257 "<member type='node' ref='#{node.id}' role='some'/>" \
258 "<member type='way' ref='#{way.id}' role='other'/>" \
259 "<tag k='test' v='yes' /></relation></osm>"
260 put relation_create_path, :params => xml, :headers => auth_header
261 # hope for forbidden, due to user
262 assert_response :forbidden,
263 "relation upload did not return success status"
265 ## Now try with the public user
266 auth_header = basic_authorization_header user.email, "test"
268 # create an relation without members
269 xml = "<osm><relation changeset='#{changeset.id}'><tag k='test' v='yes' /></relation></osm>"
270 put relation_create_path, :params => xml, :headers => auth_header
272 assert_response :success,
273 "relation upload did not return success status"
274 # read id of created relation and search for it
275 relationid = @response.body
276 checkrelation = Relation.find(relationid)
277 assert_not_nil checkrelation,
278 "uploaded relation not found in data base after upload"
280 assert_equal(0, checkrelation.members.length, "saved relation contains members but should not")
281 assert_equal(1, checkrelation.tags.length, "saved relation does not contain exactly one tag")
282 assert_equal changeset.id, checkrelation.changeset.id,
283 "saved relation does not belong in the changeset it was assigned to"
284 assert_equal user.id, checkrelation.changeset.user_id,
285 "saved relation does not belong to user that created it"
286 assert checkrelation.visible,
287 "saved relation is not visible"
288 # ok the relation is there but can we also retrieve it?
289 get api_relation_path(:id => relationid)
290 assert_response :success
293 # create an relation with a node as member
294 # This time try with a role attribute in the relation
295 xml = "<osm><relation changeset='#{changeset.id}'>" \
296 "<member ref='#{node.id}' type='node' role='some'/>" \
297 "<tag k='test' v='yes' /></relation></osm>"
298 put relation_create_path, :params => xml, :headers => auth_header
300 assert_response :success,
301 "relation upload did not return success status"
302 # read id of created relation and search for it
303 relationid = @response.body
304 checkrelation = Relation.find(relationid)
305 assert_not_nil checkrelation,
306 "uploaded relation not found in data base after upload"
308 assert_equal(1, checkrelation.members.length, "saved relation does not contain exactly one member")
309 assert_equal(1, checkrelation.tags.length, "saved relation does not contain exactly one tag")
310 assert_equal changeset.id, checkrelation.changeset.id,
311 "saved relation does not belong in the changeset it was assigned to"
312 assert_equal user.id, checkrelation.changeset.user_id,
313 "saved relation does not belong to user that created it"
314 assert checkrelation.visible,
315 "saved relation is not visible"
316 # ok the relation is there but can we also retrieve it?
318 get api_relation_path(:id => relationid)
319 assert_response :success
322 # create an relation with a node as member, this time test that we don't
323 # need a role attribute to be included
324 xml = "<osm><relation changeset='#{changeset.id}'>" \
325 "<member ref='#{node.id}' type='node'/><tag k='test' v='yes' /></relation></osm>"
326 put relation_create_path, :params => xml, :headers => auth_header
328 assert_response :success,
329 "relation upload did not return success status"
330 # read id of created relation and search for it
331 relationid = @response.body
332 checkrelation = Relation.find(relationid)
333 assert_not_nil checkrelation,
334 "uploaded relation not found in data base after upload"
336 assert_equal(1, checkrelation.members.length, "saved relation does not contain exactly one member")
337 assert_equal(1, checkrelation.tags.length, "saved relation does not contain exactly one tag")
338 assert_equal changeset.id, checkrelation.changeset.id,
339 "saved relation does not belong in the changeset it was assigned to"
340 assert_equal user.id, checkrelation.changeset.user_id,
341 "saved relation does not belong to user that created it"
342 assert checkrelation.visible,
343 "saved relation is not visible"
344 # ok the relation is there but can we also retrieve it?
346 get api_relation_path(:id => relationid)
347 assert_response :success
350 # create an relation with a way and a node as members
351 xml = "<osm><relation changeset='#{changeset.id}'>" \
352 "<member type='node' ref='#{node.id}' role='some'/>" \
353 "<member type='way' ref='#{way.id}' role='other'/>" \
354 "<tag k='test' v='yes' /></relation></osm>"
355 put relation_create_path, :params => xml, :headers => auth_header
357 assert_response :success,
358 "relation upload did not return success status"
359 # read id of created relation and search for it
360 relationid = @response.body
361 checkrelation = Relation.find(relationid)
362 assert_not_nil checkrelation,
363 "uploaded relation not found in data base after upload"
365 assert_equal(2, checkrelation.members.length, "saved relation does not have exactly two members")
366 assert_equal(1, checkrelation.tags.length, "saved relation does not contain exactly one tag")
367 assert_equal changeset.id, checkrelation.changeset.id,
368 "saved relation does not belong in the changeset it was assigned to"
369 assert_equal user.id, checkrelation.changeset.user_id,
370 "saved relation does not belong to user that created it"
371 assert checkrelation.visible,
372 "saved relation is not visible"
373 # ok the relation is there but can we also retrieve it?
374 get api_relation_path(:id => relationid)
375 assert_response :success
378 # ------------------------------------
379 # Test updating relations
380 # ------------------------------------
383 # test that, when tags are updated on a relation, the correct things
384 # happen to the correct tables and the API gives sensible results.
385 # this is to test a case that gregory marler noticed and posted to
387 ## FIXME Move this to an integration test
388 def test_update_relation_tags
390 changeset = create(:changeset, :user => user)
391 relation = create(:relation)
392 create_list(:relation_tag, 4, :relation => relation)
394 auth_header = basic_authorization_header user.email, "test"
396 with_relation(relation.id) do |rel|
397 # alter one of the tags
398 tag = rel.find("//osm/relation/tag").first
399 tag["v"] = "some changed value"
400 update_changeset(rel, changeset.id)
402 # check that the downloaded tags are the same as the uploaded tags...
403 new_version = with_update(rel, auth_header) do |new_rel|
404 assert_tags_equal rel, new_rel
407 # check the original one in the current_* table again
408 with_relation(relation.id) { |r| assert_tags_equal rel, r }
410 # now check the version in the history
411 with_relation(relation.id, new_version) { |r| assert_tags_equal rel, r }
416 # test that, when tags are updated on a relation when using the diff
417 # upload function, the correct things happen to the correct tables
418 # and the API gives sensible results. this is to test a case that
419 # gregory marler noticed and posted to josm-dev.
420 def test_update_relation_tags_via_upload
422 changeset = create(:changeset, :user => user)
423 relation = create(:relation)
424 create_list(:relation_tag, 4, :relation => relation)
426 auth_header = basic_authorization_header user.email, "test"
428 with_relation(relation.id) do |rel|
429 # alter one of the tags
430 tag = rel.find("//osm/relation/tag").first
431 tag["v"] = "some changed value"
432 update_changeset(rel, changeset.id)
434 # check that the downloaded tags are the same as the uploaded tags...
435 new_version = with_update_diff(rel, auth_header) do |new_rel|
436 assert_tags_equal rel, new_rel
439 # check the original one in the current_* table again
440 with_relation(relation.id) { |r| assert_tags_equal rel, r }
442 # now check the version in the history
443 with_relation(relation.id, new_version) { |r| assert_tags_equal rel, r }
447 def test_update_wrong_id
449 changeset = create(:changeset, :user => user)
450 relation = create(:relation)
451 other_relation = create(:relation)
453 auth_header = basic_authorization_header user.email, "test"
454 with_relation(relation.id) do |rel|
455 update_changeset(rel, changeset.id)
456 put api_relation_path(:id => other_relation.id), :params => rel.to_s, :headers => auth_header
457 assert_response :bad_request
461 # -------------------------------------
462 # Test creating some invalid relations.
463 # -------------------------------------
465 def test_create_invalid
467 changeset = create(:changeset, :user => user)
469 auth_header = basic_authorization_header user.email, "test"
471 # create a relation with non-existing node as member
472 xml = "<osm><relation changeset='#{changeset.id}'>" \
473 "<member type='node' ref='0'/><tag k='test' v='yes' />" \
475 put relation_create_path, :params => xml, :headers => auth_header
477 assert_response :precondition_failed,
478 "relation upload with invalid node did not return 'precondition failed'"
479 assert_equal "Precondition failed: Relation with id cannot be saved due to Node with id 0", @response.body
482 # -------------------------------------
483 # Test creating a relation, with some invalid XML
484 # -------------------------------------
485 def test_create_invalid_xml
487 changeset = create(:changeset, :user => user)
490 auth_header = basic_authorization_header user.email, "test"
492 # create some xml that should return an error
493 xml = "<osm><relation changeset='#{changeset.id}'>" \
494 "<member type='type' ref='#{node.id}' role=''/>" \
495 "<tag k='tester' v='yep'/></relation></osm>"
496 put relation_create_path, :params => xml, :headers => auth_header
498 assert_response :bad_request
499 assert_match(/Cannot parse valid relation from xml string/, @response.body)
500 assert_match(/The type is not allowed only, /, @response.body)
503 # -------------------------------------
504 # Test deleting relations.
505 # -------------------------------------
508 private_user = create(:user, :data_public => false)
509 private_user_closed_changeset = create(:changeset, :closed, :user => private_user)
511 closed_changeset = create(:changeset, :closed, :user => user)
512 changeset = create(:changeset, :user => user)
513 relation = create(:relation)
514 used_relation = create(:relation)
515 super_relation = create(:relation_member, :member => used_relation).relation
516 deleted_relation = create(:relation, :deleted)
517 multi_tag_relation = create(:relation)
518 create_list(:relation_tag, 4, :relation => multi_tag_relation)
520 ## First try to delete relation without auth
521 delete api_relation_path(relation)
522 assert_response :unauthorized
524 ## Then try with the private user, to make sure that you get a forbidden
525 auth_header = basic_authorization_header private_user.email, "test"
527 # this shouldn't work, as we should need the payload...
528 delete api_relation_path(relation), :headers => auth_header
529 assert_response :forbidden
531 # try to delete without specifying a changeset
532 xml = "<osm><relation id='#{relation.id}'/></osm>"
533 delete api_relation_path(relation), :params => xml.to_s, :headers => auth_header
534 assert_response :forbidden
536 # try to delete with an invalid (closed) changeset
537 xml = update_changeset(xml_for_relation(relation),
538 private_user_closed_changeset.id)
539 delete api_relation_path(relation), :params => xml.to_s, :headers => auth_header
540 assert_response :forbidden
542 # try to delete with an invalid (non-existent) changeset
543 xml = update_changeset(xml_for_relation(relation), 0)
544 delete api_relation_path(relation), :params => xml.to_s, :headers => auth_header
545 assert_response :forbidden
547 # this won't work because the relation is in-use by another relation
548 xml = xml_for_relation(used_relation)
549 delete api_relation_path(used_relation), :params => xml.to_s, :headers => auth_header
550 assert_response :forbidden
552 # this should work when we provide the appropriate payload...
553 xml = xml_for_relation(relation)
554 delete api_relation_path(relation), :params => xml.to_s, :headers => auth_header
555 assert_response :forbidden
557 # this won't work since the relation is already deleted
558 xml = xml_for_relation(deleted_relation)
559 delete api_relation_path(deleted_relation), :params => xml.to_s, :headers => auth_header
560 assert_response :forbidden
562 # this won't work since the relation never existed
563 delete api_relation_path(:id => 0), :headers => auth_header
564 assert_response :forbidden
566 ## now set auth for the public user
567 auth_header = basic_authorization_header user.email, "test"
569 # this shouldn't work, as we should need the payload...
570 delete api_relation_path(relation), :headers => auth_header
571 assert_response :bad_request
573 # try to delete without specifying a changeset
574 xml = "<osm><relation id='#{relation.id}' version='#{relation.version}' /></osm>"
575 delete api_relation_path(relation), :params => xml.to_s, :headers => auth_header
576 assert_response :bad_request
577 assert_match(/Changeset id is missing/, @response.body)
579 # try to delete with an invalid (closed) changeset
580 xml = update_changeset(xml_for_relation(relation),
582 delete api_relation_path(relation), :params => xml.to_s, :headers => auth_header
583 assert_response :conflict
585 # try to delete with an invalid (non-existent) changeset
586 xml = update_changeset(xml_for_relation(relation), 0)
587 delete api_relation_path(relation), :params => xml.to_s, :headers => auth_header
588 assert_response :conflict
590 # this won't work because the relation is in a changeset owned by someone else
591 xml = update_changeset(xml_for_relation(relation), create(:changeset).id)
592 delete api_relation_path(relation), :params => xml.to_s, :headers => auth_header
593 assert_response :conflict,
594 "shouldn't be able to delete a relation in a changeset owned by someone else (#{@response.body})"
596 # this won't work because the relation in the payload is different to that passed
597 xml = update_changeset(xml_for_relation(relation), changeset.id)
598 delete api_relation_path(create(:relation)), :params => xml.to_s, :headers => auth_header
599 assert_response :bad_request, "shouldn't be able to delete a relation when payload is different to the url"
601 # this won't work because the relation is in-use by another relation
602 xml = update_changeset(xml_for_relation(used_relation), changeset.id)
603 delete api_relation_path(used_relation), :params => xml.to_s, :headers => auth_header
604 assert_response :precondition_failed,
605 "shouldn't be able to delete a relation used in a relation (#{@response.body})"
606 assert_equal "Precondition failed: The relation #{used_relation.id} is used in relation #{super_relation.id}.", @response.body
608 # this should work when we provide the appropriate payload...
609 xml = update_changeset(xml_for_relation(multi_tag_relation), changeset.id)
610 delete api_relation_path(multi_tag_relation), :params => xml.to_s, :headers => auth_header
611 assert_response :success
613 # valid delete should return the new version number, which should
614 # be greater than the old version number
615 assert @response.body.to_i > multi_tag_relation.version,
616 "delete request should return a new version number for relation"
618 # this won't work since the relation is already deleted
619 xml = update_changeset(xml_for_relation(deleted_relation), changeset.id)
620 delete api_relation_path(deleted_relation), :params => xml.to_s, :headers => auth_header
621 assert_response :gone
623 # Public visible relation needs to be deleted
624 xml = update_changeset(xml_for_relation(super_relation), changeset.id)
625 delete api_relation_path(super_relation), :params => xml.to_s, :headers => auth_header
626 assert_response :success
628 # this works now because the relation which was using this one
630 xml = update_changeset(xml_for_relation(used_relation), changeset.id)
631 delete api_relation_path(used_relation), :params => xml.to_s, :headers => auth_header
632 assert_response :success,
633 "should be able to delete a relation used in an old relation (#{@response.body})"
635 # this won't work since the relation never existed
636 delete api_relation_path(:id => 0), :headers => auth_header
637 assert_response :not_found
641 # when a relation's tag is modified then it should put the bounding
642 # box of all its members into the changeset.
643 def test_tag_modify_bounding_box
644 relation = create(:relation)
645 node1 = create(:node, :lat => 3, :lon => 3)
646 node2 = create(:node, :lat => 5, :lon => 5)
648 create(:way_node, :way => way, :node => node1)
649 create(:relation_member, :relation => relation, :member => way)
650 create(:relation_member, :relation => relation, :member => node2)
651 # the relation contains nodes1 and node2 (node1
652 # indirectly via the way), so the bbox should be [3,3,5,5].
653 check_changeset_modify(BoundingBox.new(3, 3, 5, 5)) do |changeset_id, auth_header|
654 # add a tag to an existing relation
655 relation_xml = xml_for_relation(relation)
656 relation_element = relation_xml.find("//osm/relation").first
657 new_tag = XML::Node.new("tag")
658 new_tag["k"] = "some_new_tag"
659 new_tag["v"] = "some_new_value"
660 relation_element << new_tag
662 # update changeset ID to point to new changeset
663 update_changeset(relation_xml, changeset_id)
666 put api_relation_path(relation), :params => relation_xml.to_s, :headers => auth_header
667 assert_response :success, "can't update relation for tag/bbox test"
672 # add a member to a relation and check the bounding box is only that
674 def test_add_member_bounding_box
675 relation = create(:relation)
676 node1 = create(:node, :lat => 4, :lon => 4)
677 node2 = create(:node, :lat => 7, :lon => 7)
679 create(:way_node, :way => way1, :node => create(:node, :lat => 8, :lon => 8))
681 create(:way_node, :way => way2, :node => create(:node, :lat => 9, :lon => 9), :sequence_id => 1)
682 create(:way_node, :way => way2, :node => create(:node, :lat => 10, :lon => 10), :sequence_id => 2)
684 [node1, node2, way1, way2].each do |element|
685 bbox = element.bbox.to_unscaled
686 check_changeset_modify(bbox) do |changeset_id, auth_header|
687 relation_xml = xml_for_relation(Relation.find(relation.id))
688 relation_element = relation_xml.find("//osm/relation").first
689 new_member = XML::Node.new("member")
690 new_member["ref"] = element.id.to_s
691 new_member["type"] = element.class.to_s.downcase
692 new_member["role"] = "some_role"
693 relation_element << new_member
695 # update changeset ID to point to new changeset
696 update_changeset(relation_xml, changeset_id)
699 put api_relation_path(:id => relation.id), :params => relation_xml.to_s, :headers => auth_header
700 assert_response :success, "can't update relation for add #{element.class}/bbox test: #{@response.body}"
702 # get it back and check the ordering
703 get api_relation_path(relation)
704 assert_response :success, "can't read back the relation: #{@response.body}"
705 check_ordering(relation_xml, @response.body)
711 # remove a member from a relation and check the bounding box is
713 def test_remove_member_bounding_box
714 relation = create(:relation)
715 node1 = create(:node, :lat => 3, :lon => 3)
716 node2 = create(:node, :lat => 5, :lon => 5)
717 create(:relation_member, :relation => relation, :member => node1)
718 create(:relation_member, :relation => relation, :member => node2)
720 check_changeset_modify(BoundingBox.new(5, 5, 5, 5)) do |changeset_id, auth_header|
721 # remove node 5 (5,5) from an existing relation
722 relation_xml = xml_for_relation(relation)
724 .find("//osm/relation/member[@type='node'][@ref='#{node2.id}']")
727 # update changeset ID to point to new changeset
728 update_changeset(relation_xml, changeset_id)
731 put api_relation_path(relation), :params => relation_xml.to_s, :headers => auth_header
732 assert_response :success, "can't update relation for remove node/bbox test"
737 # check that relations are ordered
738 def test_relation_member_ordering
740 changeset = create(:changeset, :user => user)
741 node1 = create(:node)
742 node2 = create(:node)
743 node3 = create(:node)
744 way1 = create(:way_with_nodes, :nodes_count => 2)
745 way2 = create(:way_with_nodes, :nodes_count => 2)
747 auth_header = basic_authorization_header user.email, "test"
751 <relation changeset='#{changeset.id}'>
752 <member ref='#{node1.id}' type='node' role='first'/>
753 <member ref='#{node2.id}' type='node' role='second'/>
754 <member ref='#{way1.id}' type='way' role='third'/>
755 <member ref='#{way2.id}' type='way' role='fourth'/>
759 doc = XML::Parser.string(doc_str).parse
761 put relation_create_path, :params => doc.to_s, :headers => auth_header
762 assert_response :success, "can't create a relation: #{@response.body}"
763 relation_id = @response.body.to_i
765 # get it back and check the ordering
766 get api_relation_path(:id => relation_id)
767 assert_response :success, "can't read back the relation: #{@response.body}"
768 check_ordering(doc, @response.body)
770 # insert a member at the front
771 new_member = XML::Node.new "member"
772 new_member["ref"] = node3.id.to_s
773 new_member["type"] = "node"
774 new_member["role"] = "new first"
775 doc.find("//osm/relation").first.child.prev = new_member
776 # update the version, should be 1?
777 doc.find("//osm/relation").first["id"] = relation_id.to_s
778 doc.find("//osm/relation").first["version"] = 1.to_s
780 # upload the next version of the relation
781 put api_relation_path(:id => relation_id), :params => doc.to_s, :headers => auth_header
782 assert_response :success, "can't update relation: #{@response.body}"
783 assert_equal 2, @response.body.to_i
785 # get it back again and check the ordering again
786 get api_relation_path(:id => relation_id)
787 assert_response :success, "can't read back the relation: #{@response.body}"
788 check_ordering(doc, @response.body)
790 # check the ordering in the history tables:
791 with_controller(OldRelationsController.new) do
792 get relation_version_path(:id => relation_id, :version => 2)
793 assert_response :success, "can't read back version 2 of the relation #{relation_id}"
794 check_ordering(doc, @response.body)
799 # check that relations can contain duplicate members
800 def test_relation_member_duplicates
801 private_user = create(:user, :data_public => false)
803 changeset = create(:changeset, :user => user)
804 node1 = create(:node)
805 node2 = create(:node)
809 <relation changeset='#{changeset.id}'>
810 <member ref='#{node1.id}' type='node' role='forward'/>
811 <member ref='#{node2.id}' type='node' role='forward'/>
812 <member ref='#{node1.id}' type='node' role='forward'/>
813 <member ref='#{node2.id}' type='node' role='forward'/>
817 doc = XML::Parser.string(doc_str).parse
819 ## First try with the private user
820 auth_header = basic_authorization_header private_user.email, "test"
822 put relation_create_path, :params => doc.to_s, :headers => auth_header
823 assert_response :forbidden
825 ## Now try with the public user
826 auth_header = basic_authorization_header user.email, "test"
828 put relation_create_path, :params => doc.to_s, :headers => auth_header
829 assert_response :success, "can't create a relation: #{@response.body}"
830 relation_id = @response.body.to_i
832 # get it back and check the ordering
833 get api_relation_path(:id => relation_id)
834 assert_response :success, "can't read back the relation: #{relation_id}"
835 check_ordering(doc, @response.body)
839 # test that the ordering of elements in the history is the same as in current.
840 def test_history_ordering
842 changeset = create(:changeset, :user => user)
843 node1 = create(:node)
844 node2 = create(:node)
845 node3 = create(:node)
846 node4 = create(:node)
850 <relation changeset='#{changeset.id}'>
851 <member ref='#{node1.id}' type='node' role='forward'/>
852 <member ref='#{node4.id}' type='node' role='forward'/>
853 <member ref='#{node3.id}' type='node' role='forward'/>
854 <member ref='#{node2.id}' type='node' role='forward'/>
858 doc = XML::Parser.string(doc_str).parse
859 auth_header = basic_authorization_header user.email, "test"
861 put relation_create_path, :params => doc.to_s, :headers => auth_header
862 assert_response :success, "can't create a relation: #{@response.body}"
863 relation_id = @response.body.to_i
865 # check the ordering in the current tables:
866 get api_relation_path(:id => relation_id)
867 assert_response :success, "can't read back the relation: #{@response.body}"
868 check_ordering(doc, @response.body)
870 # check the ordering in the history tables:
871 with_controller(OldRelationsController.new) do
872 get relation_version_path(:id => relation_id, :version => 1)
873 assert_response :success, "can't read back version 1 of the relation: #{@response.body}"
874 check_ordering(doc, @response.body)
879 # remove all the members from a relation. the result is pretty useless, but
880 # still technically valid.
881 def test_remove_all_members
882 relation = create(:relation)
883 node1 = create(:node, :lat => 3, :lon => 3)
884 node2 = create(:node, :lat => 5, :lon => 5)
886 create(:way_node, :way => way, :node => node1)
887 create(:relation_member, :relation => relation, :member => way)
888 create(:relation_member, :relation => relation, :member => node2)
890 check_changeset_modify(BoundingBox.new(3, 3, 5, 5)) do |changeset_id, auth_header|
891 relation_xml = xml_for_relation(relation)
893 .find("//osm/relation/member")
896 # update changeset ID to point to new changeset
897 update_changeset(relation_xml, changeset_id)
900 put api_relation_path(relation), :params => relation_xml.to_s, :headers => auth_header
901 assert_response :success, "can't update relation for remove all members test"
902 checkrelation = Relation.find(relation.id)
903 assert_not_nil(checkrelation,
904 "uploaded relation not found in database after upload")
905 assert_equal(0, checkrelation.members.length,
906 "relation contains members but they should have all been deleted")
912 def check_relations_for_element(path, type, id, expected_relations)
913 # check the "relations for relation" mode
915 assert_response :success
917 # count one osm element
918 assert_select "osm[version='#{Settings.api_version}'][generator='OpenStreetMap server']", 1
920 # we should have only the expected number of relations
921 assert_select "osm>relation", expected_relations.size
923 # and each of them should contain the element we originally searched for
924 expected_relations.each do |relation|
925 # The relation should appear once, but the element could appear multiple times
926 assert_select "osm>relation[id='#{relation.id}']", 1
927 assert_select "osm>relation[id='#{relation.id}']>member[type='#{type}'][ref='#{id}']"
932 # checks that the XML document and the string arguments have
933 # members in the same order.
934 def check_ordering(doc, xml)
935 new_doc = XML::Parser.string(xml).parse
937 doc_members = doc.find("//osm/relation/member").collect do |m|
938 [m["ref"].to_i, m["type"].to_sym, m["role"]]
941 new_members = new_doc.find("//osm/relation/member").collect do |m|
942 [m["ref"].to_i, m["type"].to_sym, m["role"]]
945 doc_members.zip(new_members).each do |d, n|
946 assert_equal d, n, "members are not equal - ordering is wrong? (#{doc}, #{xml})"
951 # create a changeset and yield to the caller to set it up, then assert
952 # that the changeset bounding box is +bbox+.
953 def check_changeset_modify(bbox)
954 ## First test with the private user to check that you get a forbidden
955 auth_header = basic_authorization_header create(:user, :data_public => false).email, "test"
957 # create a new changeset for this operation, so we are assured
958 # that the bounding box will be newly-generated.
959 changeset_id = with_controller(Api::ChangesetsController.new) do
960 xml = "<osm><changeset/></osm>"
961 put changeset_create_path, :params => xml, :headers => auth_header
962 assert_response :forbidden, "shouldn't be able to create changeset for modify test, as should get forbidden"
965 ## Now do the whole thing with the public user
966 auth_header = basic_authorization_header create(:user).email, "test"
968 # create a new changeset for this operation, so we are assured
969 # that the bounding box will be newly-generated.
970 changeset_id = with_controller(Api::ChangesetsController.new) do
971 xml = "<osm><changeset/></osm>"
972 put changeset_create_path, :params => xml, :headers => auth_header
973 assert_response :success, "couldn't create changeset for modify test"
977 # go back to the block to do the actual modifies
978 yield changeset_id, auth_header
980 # now download the changeset to check its bounding box
981 with_controller(Api::ChangesetsController.new) do
982 get changeset_show_path(:id => changeset_id)
983 assert_response :success, "can't re-read changeset for modify test"
984 assert_select "osm>changeset", 1, "Changeset element doesn't exist in #{@response.body}"
985 assert_select "osm>changeset[id='#{changeset_id}']", 1, "Changeset id=#{changeset_id} doesn't exist in #{@response.body}"
986 assert_select "osm>changeset[min_lon='#{format('%<lon>.7f', :lon => bbox.min_lon)}']", 1, "Changeset min_lon wrong in #{@response.body}"
987 assert_select "osm>changeset[min_lat='#{format('%<lat>.7f', :lat => bbox.min_lat)}']", 1, "Changeset min_lat wrong in #{@response.body}"
988 assert_select "osm>changeset[max_lon='#{format('%<lon>.7f', :lon => bbox.max_lon)}']", 1, "Changeset max_lon wrong in #{@response.body}"
989 assert_select "osm>changeset[max_lat='#{format('%<lat>.7f', :lat => bbox.max_lat)}']", 1, "Changeset max_lat wrong in #{@response.body}"
994 # yields the relation with the given +id+ (and optional +version+
995 # to read from the history tables) into the block. the parsed XML
997 def with_relation(id, ver = nil)
999 get api_relation_path(:id => id)
1001 with_controller(OldRelationsController.new) do
1002 get relation_version_path(:id => id, :version => ver)
1005 assert_response :success
1006 yield xml_parse(@response.body)
1010 # updates the relation (XML) +rel+ and
1011 # yields the new version of that relation into the block.
1012 # the parsed XML doc is returned.
1013 def with_update(rel, headers)
1014 rel_id = rel.find("//osm/relation").first["id"].to_i
1015 put api_relation_path(:id => rel_id), :params => rel.to_s, :headers => headers
1016 assert_response :success, "can't update relation: #{@response.body}"
1017 version = @response.body.to_i
1019 # now get the new version
1020 get api_relation_path(:id => rel_id)
1021 assert_response :success
1022 new_rel = xml_parse(@response.body)
1030 # updates the relation (XML) +rel+ via the diff-upload API and
1031 # yields the new version of that relation into the block.
1032 # the parsed XML doc is returned.
1033 def with_update_diff(rel, headers)
1034 rel_id = rel.find("//osm/relation").first["id"].to_i
1035 cs_id = rel.find("//osm/relation").first["changeset"].to_i
1038 with_controller(Api::ChangesetsController.new) do
1039 doc = OSM::API.new.get_xml_doc
1040 change = XML::Node.new "osmChange"
1042 modify = XML::Node.new "modify"
1044 modify << doc.import(rel.find("//osm/relation").first)
1046 post changeset_upload_path(:id => cs_id), :params => doc.to_s, :headers => headers
1047 assert_response :success, "can't upload diff relation: #{@response.body}"
1048 version = xml_parse(@response.body).find("//diffResult/relation").first["new_version"].to_i
1051 # now get the new version
1052 get api_relation_path(:id => rel_id)
1053 assert_response :success
1054 new_rel = xml_parse(@response.body)
1062 # returns a k->v hash of tags from an xml doc
1063 def get_tags_as_hash(a)
1064 a.find("//osm/relation/tag").sort_by { |v| v["k"] }.each_with_object({}) do |v, h|
1070 # assert that all tags on relation documents +a+ and +b+
1072 def assert_tags_equal(a, b)
1073 # turn the XML doc into tags hashes
1074 a_tags = get_tags_as_hash(a)
1075 b_tags = get_tags_as_hash(b)
1077 assert_equal a_tags.keys, b_tags.keys, "Tag keys should be identical."
1078 a_tags.each do |k, v|
1079 assert_equal v, b_tags[k],
1080 "Tags which were not altered should be the same. " \
1081 "#{a_tags.inspect} != #{b_tags.inspect}"
1086 # update the changeset_id of a node element
1087 def update_changeset(xml, changeset_id)
1088 xml_attr_rewrite(xml, "changeset", changeset_id)
1092 # update an attribute in the node element
1093 def xml_attr_rewrite(xml, name, value)
1094 xml.find("//osm/relation").first[name] = value.to_s
1101 parser = XML::Parser.string(xml)