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(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)
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)
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)
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(:relations => "")
182 assert_response :bad_request
184 # test a working call
185 get relations_path(: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(: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(: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(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(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(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(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(other_relation), :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(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_operator @response.body.to_i, :>, multi_tag_relation.version, "delete request should return a new version number for relation"
617 # this won't work since the relation is already deleted
618 xml = update_changeset(xml_for_relation(deleted_relation), changeset.id)
619 delete api_relation_path(deleted_relation), :params => xml.to_s, :headers => auth_header
620 assert_response :gone
622 # Public visible relation needs to be deleted
623 xml = update_changeset(xml_for_relation(super_relation), changeset.id)
624 delete api_relation_path(super_relation), :params => xml.to_s, :headers => auth_header
625 assert_response :success
627 # this works now because the relation which was using this one
629 xml = update_changeset(xml_for_relation(used_relation), changeset.id)
630 delete api_relation_path(used_relation), :params => xml.to_s, :headers => auth_header
631 assert_response :success,
632 "should be able to delete a relation used in an old relation (#{@response.body})"
634 # this won't work since the relation never existed
635 delete api_relation_path(0), :headers => auth_header
636 assert_response :not_found
640 # when a relation's tag is modified then it should put the bounding
641 # box of all its members into the changeset.
642 def test_tag_modify_bounding_box
643 relation = create(:relation)
644 node1 = create(:node, :lat => 0.3, :lon => 0.3)
645 node2 = create(:node, :lat => 0.5, :lon => 0.5)
647 create(:way_node, :way => way, :node => node1)
648 create(:relation_member, :relation => relation, :member => way)
649 create(:relation_member, :relation => relation, :member => node2)
650 # the relation contains nodes1 and node2 (node1
651 # indirectly via the way), so the bbox should be [0.3,0.3,0.5,0.5].
652 check_changeset_modify(BoundingBox.new(0.3, 0.3, 0.5, 0.5)) do |changeset_id, auth_header|
653 # add a tag to an existing relation
654 relation_xml = xml_for_relation(relation)
655 relation_element = relation_xml.find("//osm/relation").first
656 new_tag = XML::Node.new("tag")
657 new_tag["k"] = "some_new_tag"
658 new_tag["v"] = "some_new_value"
659 relation_element << new_tag
661 # update changeset ID to point to new changeset
662 update_changeset(relation_xml, changeset_id)
665 put api_relation_path(relation), :params => relation_xml.to_s, :headers => auth_header
666 assert_response :success, "can't update relation for tag/bbox test"
671 # add a member to a relation and check the bounding box is only that
673 def test_add_member_bounding_box
674 relation = create(:relation)
675 node1 = create(:node, :lat => 4, :lon => 4)
676 node2 = create(:node, :lat => 7, :lon => 7)
678 create(:way_node, :way => way1, :node => create(:node, :lat => 8, :lon => 8))
680 create(:way_node, :way => way2, :node => create(:node, :lat => 9, :lon => 9), :sequence_id => 1)
681 create(:way_node, :way => way2, :node => create(:node, :lat => 10, :lon => 10), :sequence_id => 2)
683 [node1, node2, way1, way2].each do |element|
684 bbox = element.bbox.to_unscaled
685 check_changeset_modify(bbox) do |changeset_id, auth_header|
686 relation_xml = xml_for_relation(Relation.find(relation.id))
687 relation_element = relation_xml.find("//osm/relation").first
688 new_member = XML::Node.new("member")
689 new_member["ref"] = element.id.to_s
690 new_member["type"] = element.class.to_s.downcase
691 new_member["role"] = "some_role"
692 relation_element << new_member
694 # update changeset ID to point to new changeset
695 update_changeset(relation_xml, changeset_id)
698 put api_relation_path(relation), :params => relation_xml.to_s, :headers => auth_header
699 assert_response :success, "can't update relation for add #{element.class}/bbox test: #{@response.body}"
701 # get it back and check the ordering
702 get api_relation_path(relation)
703 assert_response :success, "can't read back the relation: #{@response.body}"
704 check_ordering(relation_xml, @response.body)
710 # remove a member from a relation and check the bounding box is
712 def test_remove_member_bounding_box
713 relation = create(:relation)
714 node1 = create(:node, :lat => 3, :lon => 3)
715 node2 = create(:node, :lat => 5, :lon => 5)
716 create(:relation_member, :relation => relation, :member => node1)
717 create(:relation_member, :relation => relation, :member => node2)
719 check_changeset_modify(BoundingBox.new(5, 5, 5, 5)) do |changeset_id, auth_header|
720 # remove node 5 (5,5) from an existing relation
721 relation_xml = xml_for_relation(relation)
723 .find("//osm/relation/member[@type='node'][@ref='#{node2.id}']")
726 # update changeset ID to point to new changeset
727 update_changeset(relation_xml, changeset_id)
730 put api_relation_path(relation), :params => relation_xml.to_s, :headers => auth_header
731 assert_response :success, "can't update relation for remove node/bbox test"
736 # check that relations are ordered
737 def test_relation_member_ordering
739 changeset = create(:changeset, :user => user)
740 node1 = create(:node)
741 node2 = create(:node)
742 node3 = create(:node)
743 way1 = create(:way_with_nodes, :nodes_count => 2)
744 way2 = create(:way_with_nodes, :nodes_count => 2)
746 auth_header = basic_authorization_header user.email, "test"
750 <relation changeset='#{changeset.id}'>
751 <member ref='#{node1.id}' type='node' role='first'/>
752 <member ref='#{node2.id}' type='node' role='second'/>
753 <member ref='#{way1.id}' type='way' role='third'/>
754 <member ref='#{way2.id}' type='way' role='fourth'/>
758 doc = XML::Parser.string(doc_str).parse
760 put relation_create_path, :params => doc.to_s, :headers => auth_header
761 assert_response :success, "can't create a relation: #{@response.body}"
762 relation_id = @response.body.to_i
764 # get it back and check the ordering
765 get api_relation_path(relation_id)
766 assert_response :success, "can't read back the relation: #{@response.body}"
767 check_ordering(doc, @response.body)
769 # insert a member at the front
770 new_member = XML::Node.new "member"
771 new_member["ref"] = node3.id.to_s
772 new_member["type"] = "node"
773 new_member["role"] = "new first"
774 doc.find("//osm/relation").first.child.prev = new_member
775 # update the version, should be 1?
776 doc.find("//osm/relation").first["id"] = relation_id.to_s
777 doc.find("//osm/relation").first["version"] = 1.to_s
779 # upload the next version of the relation
780 put api_relation_path(relation_id), :params => doc.to_s, :headers => auth_header
781 assert_response :success, "can't update relation: #{@response.body}"
782 assert_equal 2, @response.body.to_i
784 # get it back again and check the ordering again
785 get api_relation_path(relation_id)
786 assert_response :success, "can't read back the relation: #{@response.body}"
787 check_ordering(doc, @response.body)
789 # check the ordering in the history tables:
790 with_controller(OldRelationsController.new) do
791 get api_old_relation_path(relation_id, 2)
792 assert_response :success, "can't read back version 2 of the relation #{relation_id}"
793 check_ordering(doc, @response.body)
798 # check that relations can contain duplicate members
799 def test_relation_member_duplicates
800 private_user = create(:user, :data_public => false)
802 changeset = create(:changeset, :user => user)
803 node1 = create(:node)
804 node2 = create(:node)
808 <relation changeset='#{changeset.id}'>
809 <member ref='#{node1.id}' type='node' role='forward'/>
810 <member ref='#{node2.id}' type='node' role='forward'/>
811 <member ref='#{node1.id}' type='node' role='forward'/>
812 <member ref='#{node2.id}' type='node' role='forward'/>
816 doc = XML::Parser.string(doc_str).parse
818 ## First try with the private user
819 auth_header = basic_authorization_header private_user.email, "test"
821 put relation_create_path, :params => doc.to_s, :headers => auth_header
822 assert_response :forbidden
824 ## Now try with the public user
825 auth_header = basic_authorization_header user.email, "test"
827 put relation_create_path, :params => doc.to_s, :headers => auth_header
828 assert_response :success, "can't create a relation: #{@response.body}"
829 relation_id = @response.body.to_i
831 # get it back and check the ordering
832 get api_relation_path(relation_id)
833 assert_response :success, "can't read back the relation: #{relation_id}"
834 check_ordering(doc, @response.body)
838 # test that the ordering of elements in the history is the same as in current.
839 def test_history_ordering
841 changeset = create(:changeset, :user => user)
842 node1 = create(:node)
843 node2 = create(:node)
844 node3 = create(:node)
845 node4 = create(:node)
849 <relation changeset='#{changeset.id}'>
850 <member ref='#{node1.id}' type='node' role='forward'/>
851 <member ref='#{node4.id}' type='node' role='forward'/>
852 <member ref='#{node3.id}' type='node' role='forward'/>
853 <member ref='#{node2.id}' type='node' role='forward'/>
857 doc = XML::Parser.string(doc_str).parse
858 auth_header = basic_authorization_header user.email, "test"
860 put relation_create_path, :params => doc.to_s, :headers => auth_header
861 assert_response :success, "can't create a relation: #{@response.body}"
862 relation_id = @response.body.to_i
864 # check the ordering in the current tables:
865 get api_relation_path(relation_id)
866 assert_response :success, "can't read back the relation: #{@response.body}"
867 check_ordering(doc, @response.body)
869 # check the ordering in the history tables:
870 with_controller(OldRelationsController.new) do
871 get api_old_relation_path(relation_id, 1)
872 assert_response :success, "can't read back version 1 of the relation: #{@response.body}"
873 check_ordering(doc, @response.body)
878 # remove all the members from a relation. the result is pretty useless, but
879 # still technically valid.
880 def test_remove_all_members
881 relation = create(:relation)
882 node1 = create(:node, :lat => 0.3, :lon => 0.3)
883 node2 = create(:node, :lat => 0.5, :lon => 0.5)
885 create(:way_node, :way => way, :node => node1)
886 create(:relation_member, :relation => relation, :member => way)
887 create(:relation_member, :relation => relation, :member => node2)
889 check_changeset_modify(BoundingBox.new(0.3, 0.3, 0.5, 0.5)) do |changeset_id, auth_header|
890 relation_xml = xml_for_relation(relation)
892 .find("//osm/relation/member")
895 # update changeset ID to point to new changeset
896 update_changeset(relation_xml, changeset_id)
899 put api_relation_path(relation), :params => relation_xml.to_s, :headers => auth_header
900 assert_response :success, "can't update relation for remove all members test"
901 checkrelation = Relation.find(relation.id)
902 assert_not_nil(checkrelation,
903 "uploaded relation not found in database after upload")
904 assert_equal(0, checkrelation.members.length,
905 "relation contains members but they should have all been deleted")
910 # test initial rate limit
911 def test_initial_rate_limit
916 node1 = create(:node)
917 node2 = create(:node)
919 # create a changeset that puts us near the initial rate limit
920 changeset = create(:changeset, :user => user,
921 :created_at => Time.now.utc - 5.minutes,
922 :num_changes => Settings.initial_changes_per_hour - 1)
924 # create authentication header
925 auth_header = basic_authorization_header user.email, "test"
927 # try creating a relation
928 xml = "<osm><relation changeset='#{changeset.id}'>" \
929 "<member ref='#{node1.id}' type='node' role='some'/>" \
930 "<member ref='#{node2.id}' type='node' role='some'/>" \
931 "<tag k='test' v='yes' /></relation></osm>"
932 put relation_create_path, :params => xml, :headers => auth_header
933 assert_response :success, "relation create did not return success status"
935 # get the id of the relation we created
936 relationid = @response.body
938 # try updating the relation, which should be rate limited
939 xml = "<osm><relation id='#{relationid}' version='1' changeset='#{changeset.id}'>" \
940 "<member ref='#{node2.id}' type='node' role='some'/>" \
941 "<member ref='#{node1.id}' type='node' role='some'/>" \
942 "<tag k='test' v='yes' /></relation></osm>"
943 put api_relation_path(relationid), :params => xml, :headers => auth_header
944 assert_response :too_many_requests, "relation update did not hit rate limit"
946 # try deleting the relation, which should be rate limited
947 xml = "<osm><relation id='#{relationid}' version='2' changeset='#{changeset.id}'/></osm>"
948 delete api_relation_path(relationid), :params => xml, :headers => auth_header
949 assert_response :too_many_requests, "relation delete did not hit rate limit"
951 # try creating a relation, which should be rate limited
952 xml = "<osm><relation changeset='#{changeset.id}'>" \
953 "<member ref='#{node1.id}' type='node' role='some'/>" \
954 "<member ref='#{node2.id}' type='node' role='some'/>" \
955 "<tag k='test' v='yes' /></relation></osm>"
956 put relation_create_path, :params => xml, :headers => auth_header
957 assert_response :too_many_requests, "relation create did not hit rate limit"
961 # test maximum rate limit
962 def test_maximum_rate_limit
967 node1 = create(:node)
968 node2 = create(:node)
970 # create a changeset to establish our initial edit time
971 changeset = create(:changeset, :user => user,
972 :created_at => Time.now.utc - 28.days)
974 # create changeset to put us near the maximum rate limit
975 total_changes = Settings.max_changes_per_hour - 1
976 while total_changes.positive?
977 changes = [total_changes, Changeset::MAX_ELEMENTS].min
978 changeset = create(:changeset, :user => user,
979 :created_at => Time.now.utc - 5.minutes,
980 :num_changes => changes)
981 total_changes -= changes
984 # create authentication header
985 auth_header = basic_authorization_header user.email, "test"
987 # try creating a relation
988 xml = "<osm><relation changeset='#{changeset.id}'>" \
989 "<member ref='#{node1.id}' type='node' role='some'/>" \
990 "<member ref='#{node2.id}' type='node' role='some'/>" \
991 "<tag k='test' v='yes' /></relation></osm>"
992 put relation_create_path, :params => xml, :headers => auth_header
993 assert_response :success, "relation create did not return success status"
995 # get the id of the relation we created
996 relationid = @response.body
998 # try updating the relation, which should be rate limited
999 xml = "<osm><relation id='#{relationid}' version='1' changeset='#{changeset.id}'>" \
1000 "<member ref='#{node2.id}' type='node' role='some'/>" \
1001 "<member ref='#{node1.id}' type='node' role='some'/>" \
1002 "<tag k='test' v='yes' /></relation></osm>"
1003 put api_relation_path(relationid), :params => xml, :headers => auth_header
1004 assert_response :too_many_requests, "relation update did not hit rate limit"
1006 # try deleting the relation, which should be rate limited
1007 xml = "<osm><relation id='#{relationid}' version='2' changeset='#{changeset.id}'/></osm>"
1008 delete api_relation_path(relationid), :params => xml, :headers => auth_header
1009 assert_response :too_many_requests, "relation delete did not hit rate limit"
1011 # try creating a relation, which should be rate limited
1012 xml = "<osm><relation changeset='#{changeset.id}'>" \
1013 "<member ref='#{node1.id}' type='node' role='some'/>" \
1014 "<member ref='#{node2.id}' type='node' role='some'/>" \
1015 "<tag k='test' v='yes' /></relation></osm>"
1016 put relation_create_path, :params => xml, :headers => auth_header
1017 assert_response :too_many_requests, "relation create did not hit rate limit"
1022 def check_relations_for_element(path, type, id, expected_relations)
1023 # check the "relations for relation" mode
1025 assert_response :success
1027 # count one osm element
1028 assert_select "osm[version='#{Settings.api_version}'][generator='#{Settings.generator}']", 1
1030 # we should have only the expected number of relations
1031 assert_select "osm>relation", expected_relations.size
1033 # and each of them should contain the element we originally searched for
1034 expected_relations.each do |relation|
1035 # The relation should appear once, but the element could appear multiple times
1036 assert_select "osm>relation[id='#{relation.id}']", 1
1037 assert_select "osm>relation[id='#{relation.id}']>member[type='#{type}'][ref='#{id}']"
1042 # checks that the XML document and the string arguments have
1043 # members in the same order.
1044 def check_ordering(doc, xml)
1045 new_doc = XML::Parser.string(xml).parse
1047 doc_members = doc.find("//osm/relation/member").collect do |m|
1048 [m["ref"].to_i, m["type"].to_sym, m["role"]]
1051 new_members = new_doc.find("//osm/relation/member").collect do |m|
1052 [m["ref"].to_i, m["type"].to_sym, m["role"]]
1055 doc_members.zip(new_members).each do |d, n|
1056 assert_equal d, n, "members are not equal - ordering is wrong? (#{doc}, #{xml})"
1061 # create a changeset and yield to the caller to set it up, then assert
1062 # that the changeset bounding box is +bbox+.
1063 def check_changeset_modify(bbox)
1064 ## First test with the private user to check that you get a forbidden
1065 auth_header = basic_authorization_header create(:user, :data_public => false).email, "test"
1067 # create a new changeset for this operation, so we are assured
1068 # that the bounding box will be newly-generated.
1069 changeset_id = with_controller(Api::ChangesetsController.new) do
1070 xml = "<osm><changeset/></osm>"
1071 put changeset_create_path, :params => xml, :headers => auth_header
1072 assert_response :forbidden, "shouldn't be able to create changeset for modify test, as should get forbidden"
1075 ## Now do the whole thing with the public user
1076 auth_header = basic_authorization_header create(:user).email, "test"
1078 # create a new changeset for this operation, so we are assured
1079 # that the bounding box will be newly-generated.
1080 changeset_id = with_controller(Api::ChangesetsController.new) do
1081 xml = "<osm><changeset/></osm>"
1082 put changeset_create_path, :params => xml, :headers => auth_header
1083 assert_response :success, "couldn't create changeset for modify test"
1087 # go back to the block to do the actual modifies
1088 yield changeset_id, auth_header
1090 # now download the changeset to check its bounding box
1091 with_controller(Api::ChangesetsController.new) do
1092 get changeset_show_path(changeset_id)
1093 assert_response :success, "can't re-read changeset for modify test"
1094 assert_select "osm>changeset", 1, "Changeset element doesn't exist in #{@response.body}"
1095 assert_select "osm>changeset[id='#{changeset_id}']", 1, "Changeset id=#{changeset_id} doesn't exist in #{@response.body}"
1096 assert_select "osm>changeset[min_lon='#{format('%<lon>.7f', :lon => bbox.min_lon)}']", 1, "Changeset min_lon wrong in #{@response.body}"
1097 assert_select "osm>changeset[min_lat='#{format('%<lat>.7f', :lat => bbox.min_lat)}']", 1, "Changeset min_lat wrong in #{@response.body}"
1098 assert_select "osm>changeset[max_lon='#{format('%<lon>.7f', :lon => bbox.max_lon)}']", 1, "Changeset max_lon wrong in #{@response.body}"
1099 assert_select "osm>changeset[max_lat='#{format('%<lat>.7f', :lat => bbox.max_lat)}']", 1, "Changeset max_lat wrong in #{@response.body}"
1104 # yields the relation with the given +id+ (and optional +version+
1105 # to read from the history tables) into the block. the parsed XML
1107 def with_relation(id, ver = nil)
1109 get api_relation_path(id)
1111 with_controller(OldRelationsController.new) do
1112 get api_old_relation_path(id, ver)
1115 assert_response :success
1116 yield xml_parse(@response.body)
1120 # updates the relation (XML) +rel+ and
1121 # yields the new version of that relation into the block.
1122 # the parsed XML doc is returned.
1123 def with_update(rel, headers)
1124 rel_id = rel.find("//osm/relation").first["id"].to_i
1125 put api_relation_path(rel_id), :params => rel.to_s, :headers => headers
1126 assert_response :success, "can't update relation: #{@response.body}"
1127 version = @response.body.to_i
1129 # now get the new version
1130 get api_relation_path(rel_id)
1131 assert_response :success
1132 new_rel = xml_parse(@response.body)
1140 # updates the relation (XML) +rel+ via the diff-upload API and
1141 # yields the new version of that relation into the block.
1142 # the parsed XML doc is returned.
1143 def with_update_diff(rel, headers)
1144 rel_id = rel.find("//osm/relation").first["id"].to_i
1145 cs_id = rel.find("//osm/relation").first["changeset"].to_i
1148 with_controller(Api::ChangesetsController.new) do
1149 doc = OSM::API.new.xml_doc
1150 change = XML::Node.new "osmChange"
1152 modify = XML::Node.new "modify"
1154 modify << doc.import(rel.find("//osm/relation").first)
1156 post changeset_upload_path(cs_id), :params => doc.to_s, :headers => headers
1157 assert_response :success, "can't upload diff relation: #{@response.body}"
1158 version = xml_parse(@response.body).find("//diffResult/relation").first["new_version"].to_i
1161 # now get the new version
1162 get api_relation_path(rel_id)
1163 assert_response :success
1164 new_rel = xml_parse(@response.body)
1172 # returns a k->v hash of tags from an xml doc
1173 def get_tags_as_hash(a)
1174 a.find("//osm/relation/tag").sort_by { |v| v["k"] }.each_with_object({}) do |v, h|
1180 # assert that all tags on relation documents +a+ and +b+
1182 def assert_tags_equal(a, b)
1183 # turn the XML doc into tags hashes
1184 a_tags = get_tags_as_hash(a)
1185 b_tags = get_tags_as_hash(b)
1187 assert_equal a_tags.keys, b_tags.keys, "Tag keys should be identical."
1188 a_tags.each do |k, v|
1189 assert_equal v, b_tags[k],
1190 "Tags which were not altered should be the same. " \
1191 "#{a_tags.inspect} != #{b_tags.inspect}"
1196 # update the changeset_id of a node element
1197 def update_changeset(xml, changeset_id)
1198 xml_attr_rewrite(xml, "changeset", changeset_id)
1202 # update an attribute in the node element
1203 def xml_attr_rewrite(xml, name, value)
1204 xml.find("//osm/relation").first[name] = value.to_s
1211 parser = XML::Parser.string(xml)