1 require File.dirname(__FILE__) + '/../test_helper'
2 require 'changeset_controller'
4 class ChangesetControllerTest < ActionController::TestCase
7 # -----------------------
8 # Test simple changeset creation
9 # -----------------------
12 basic_authorization "test@openstreetmap.org", "test"
14 # Create the first user's changeset
15 content "<osm><changeset>" +
16 "<tag k='created_by' v='osm test suite checking changesets'/>" +
20 assert_response :success, "Creation of changeset did not return sucess status"
21 newid = @response.body.to_i
23 # check end time, should be an hour ahead of creation time
24 cs = Changeset.find(newid)
25 duration = cs.closed_at - cs.created_at
26 # the difference can either be a rational, or a floating point number
27 # of seconds, depending on the code path taken :-(
28 if duration.class == Rational
29 assert_equal Rational(1,24), duration , "initial idle timeout should be an hour (#{cs.created_at} -> #{cs.closed_at})"
31 # must be number of seconds...
32 assert_equal 3600, duration.round, "initial idle timeout should be an hour (#{cs.created_at} -> #{cs.closed_at})"
36 def test_create_invalid
37 basic_authorization "test@openstreetmap.org", "test"
38 content "<osm><changeset></osm>"
40 assert_response :bad_request, "creating a invalid changeset should fail"
43 def test_create_invalid_no_content
44 basic_authorization "test@openstreetmap.org", "test"
46 assert_response :bad_request, "creating a changeset with no content should fail"
49 def test_create_wrong_method
50 basic_authorization "test@openstreetmap.org", "test"
52 assert_response :method_not_allowed
56 # check that the changeset can be read and returns the correct
59 changeset_id = changesets(:normal_user_first_change).id
60 get :read, :id => changeset_id
61 assert_response :success, "cannot get first changeset"
63 assert_select "osm[version=#{API_VERSION}][generator=\"OpenStreetMap server\"]", 1
64 assert_select "osm>changeset[id=#{changeset_id}]", 1
68 # test that the user who opened a change can close it
70 basic_authorization "test@openstreetmap.org", "test"
72 cs_id = changesets(:normal_user_first_change).id
73 put :close, :id => cs_id
74 assert_response :success
76 # test that it really is closed now
77 cs = Changeset.find(cs_id)
79 "changeset should be closed now (#{cs.closed_at} > #{Time.now}.")
83 # test that a different user can't close another user's changeset
84 def test_close_invalid
85 basic_authorization "test@example.com", "test"
87 put :close, :id => changesets(:normal_user_first_change).id
88 assert_response :conflict
89 assert_equal "The user doesn't own that changeset", @response.body
93 # upload something simple, but valid and check that it can
95 def test_upload_simple_valid
96 basic_authorization "test@openstreetmap.org", "test"
98 # simple diff to change a node, way and relation by removing
103 <node id='1' lon='0' lat='0' changeset='1' version='1'/>
104 <way id='1' changeset='1' version='1'>
109 <relation id='1' changeset='1' version='1'>
110 <member type='way' role='some' ref='3'/>
111 <member type='node' role='some' ref='5'/>
112 <member type='relation' role='some' ref='3'/>
120 post :upload, :id => 1
121 assert_response :success,
122 "can't upload a simple valid diff to changeset: #{@response.body}"
124 # check that the changes made it into the database
125 assert_equal 0, Node.find(1).tags.size, "node 1 should now have no tags"
126 assert_equal 0, Way.find(1).tags.size, "way 1 should now have no tags"
127 assert_equal 0, Relation.find(1).tags.size, "relation 1 should now have no tags"
131 # upload something which creates new objects using placeholders
132 def test_upload_create_valid
133 basic_authorization "test@openstreetmap.org", "test"
135 # simple diff to create a node way and relation using placeholders
139 <node id='-1' lon='0' lat='0' changeset='1'>
140 <tag k='foo' v='bar'/>
141 <tag k='baz' v='bat'/>
143 <way id='-1' changeset='1'>
148 <relation id='-1' changeset='1'>
149 <member type='way' role='some' ref='3'/>
150 <member type='node' role='some' ref='5'/>
151 <member type='relation' role='some' ref='3'/>
159 post :upload, :id => 1
160 assert_response :success,
161 "can't upload a simple valid creation to changeset: #{@response.body}"
163 # check the returned payload
164 assert_select "diffResult[version=#{API_VERSION}][generator=\"OpenStreetMap server\"]", 1
165 assert_select "diffResult>node", 1
166 assert_select "diffresult>way", 1
167 assert_select "diffResult>relation", 1
169 # inspect the response to find out what the new element IDs are
170 doc = XML::Parser.string(@response.body).parse
171 new_node_id = doc.find("//diffResult/node").first["new_id"].to_i
172 new_way_id = doc.find("//diffResult/way").first["new_id"].to_i
173 new_rel_id = doc.find("//diffResult/relation").first["new_id"].to_i
175 # check the old IDs are all present and negative one
176 assert_equal -1, doc.find("//diffResult/node").first["old_id"].to_i
177 assert_equal -1, doc.find("//diffResult/way").first["old_id"].to_i
178 assert_equal -1, doc.find("//diffResult/relation").first["old_id"].to_i
180 # check the versions are present and equal one
181 assert_equal 1, doc.find("//diffResult/node").first["new_version"].to_i
182 assert_equal 1, doc.find("//diffResult/way").first["new_version"].to_i
183 assert_equal 1, doc.find("//diffResult/relation").first["new_version"].to_i
185 # check that the changes made it into the database
186 assert_equal 2, Node.find(new_node_id).tags.size, "new node should have two tags"
187 assert_equal 0, Way.find(new_way_id).tags.size, "new way should have no tags"
188 assert_equal 0, Relation.find(new_rel_id).tags.size, "new relation should have no tags"
192 # test a complex delete where we delete elements which rely on eachother
193 # in the same transaction.
194 def test_upload_delete
195 basic_authorization "test@openstreetmap.org", "test"
197 diff = XML::Document.new
198 diff.root = XML::Node.new "osmChange"
199 delete = XML::Node.new "delete"
201 delete << current_relations(:visible_relation).to_xml_node
202 delete << current_relations(:used_relation).to_xml_node
203 delete << current_ways(:used_way).to_xml_node
204 delete << current_nodes(:node_used_by_relationship).to_xml_node
208 post :upload, :id => 1
209 assert_response :success,
210 "can't upload a deletion diff to changeset: #{@response.body}"
212 # check the response is well-formed
213 assert_select "diffResult>node", 1
214 assert_select "diffResult>way", 1
215 assert_select "diffResult>relation", 2
217 # check that everything was deleted
218 assert_equal false, Node.find(current_nodes(:node_used_by_relationship).id).visible
219 assert_equal false, Way.find(current_ways(:used_way).id).visible
220 assert_equal false, Relation.find(current_relations(:visible_relation).id).visible
221 assert_equal false, Relation.find(current_relations(:used_relation).id).visible
225 # test uploading a delete with no lat/lon, as they are optional in
226 # the osmChange spec.
227 def test_upload_nolatlon_delete
228 basic_authorization "test@openstreetmap.org", "test"
230 node = current_nodes(:visible_node)
231 cs = changesets(:normal_user_first_change)
232 diff = "<osmChange><delete><node id='#{node.id}' version='#{node.version}' changeset='#{cs.id}'/></delete></osmChange>"
236 post :upload, :id => cs.id
237 assert_response :success,
238 "can't upload a deletion diff to changeset: #{@response.body}"
240 # check the response is well-formed
241 assert_select "diffResult>node", 1
243 # check that everything was deleted
244 assert_equal false, Node.find(node.id).visible
247 def test_repeated_changeset_create
249 basic_authorization "test@openstreetmap.org", "test"
251 # create a temporary changeset
252 content "<osm><changeset>" +
253 "<tag k='created_by' v='osm test suite checking changesets'/>" +
255 assert_difference('Changeset.count', 1) do
258 assert_response :success
259 changeset_id = @response.body.to_i
264 # test that deleting stuff in a transaction doesn't bypass the checks
265 # to ensure that used elements are not deleted.
266 def test_upload_delete_invalid
267 basic_authorization "test@openstreetmap.org", "test"
269 diff = XML::Document.new
270 diff.root = XML::Node.new "osmChange"
271 delete = XML::Node.new "delete"
273 delete << current_relations(:visible_relation).to_xml_node
274 delete << current_ways(:used_way).to_xml_node
275 delete << current_nodes(:node_used_by_relationship).to_xml_node
279 post :upload, :id => 1
280 assert_response :precondition_failed,
281 "shouldn't be able to upload a invalid deletion diff: #{@response.body}"
283 # check that nothing was, in fact, deleted
284 assert_equal true, Node.find(current_nodes(:node_used_by_relationship).id).visible
285 assert_equal true, Way.find(current_ways(:used_way).id).visible
286 assert_equal true, Relation.find(current_relations(:visible_relation).id).visible
290 # upload something which creates new objects and inserts them into
291 # existing containers using placeholders.
292 def test_upload_complex
293 basic_authorization "test@openstreetmap.org", "test"
295 # simple diff to create a node way and relation using placeholders
299 <node id='-1' lon='0' lat='0' changeset='1'>
300 <tag k='foo' v='bar'/>
301 <tag k='baz' v='bat'/>
305 <way id='1' changeset='1' version='1'>
309 <relation id='1' changeset='1' version='1'>
310 <member type='way' role='some' ref='3'/>
311 <member type='node' role='some' ref='-1'/>
312 <member type='relation' role='some' ref='3'/>
320 post :upload, :id => 1
321 assert_response :success,
322 "can't upload a complex diff to changeset: #{@response.body}"
324 # check the returned payload
325 assert_select "diffResult[version=#{API_VERSION}][generator=\"#{GENERATOR}\"]", 1
326 assert_select "diffResult>node", 1
327 assert_select "diffResult>way", 1
328 assert_select "diffResult>relation", 1
330 # inspect the response to find out what the new element IDs are
331 doc = XML::Parser.string(@response.body).parse
332 new_node_id = doc.find("//diffResult/node").first["new_id"].to_i
334 # check that the changes made it into the database
335 assert_equal 2, Node.find(new_node_id).tags.size, "new node should have two tags"
336 assert_equal [new_node_id, 3], Way.find(1).nds, "way nodes should match"
337 Relation.find(1).members.each do |type,id,role|
339 assert_equal new_node_id, id, "relation should contain new node"
345 # create a diff which references several changesets, which should cause
346 # a rollback and none of the diff gets committed
347 def test_upload_invalid_changesets
348 basic_authorization "test@openstreetmap.org", "test"
350 # simple diff to create a node way and relation using placeholders
354 <node id='1' lon='0' lat='0' changeset='1' version='1'/>
355 <way id='1' changeset='1' version='1'>
360 <relation id='1' changeset='1' version='1'>
361 <member type='way' role='some' ref='3'/>
362 <member type='node' role='some' ref='5'/>
363 <member type='relation' role='some' ref='3'/>
367 <node id='-1' lon='0' lat='0' changeset='4'>
368 <tag k='foo' v='bar'/>
369 <tag k='baz' v='bat'/>
374 # cache the objects before uploading them
375 node = current_nodes(:visible_node)
376 way = current_ways(:visible_way)
377 rel = current_relations(:visible_relation)
381 post :upload, :id => 1
382 assert_response :conflict,
383 "uploading a diff with multiple changsets should have failed"
385 # check that objects are unmodified
386 assert_nodes_are_equal(node, Node.find(1))
387 assert_ways_are_equal(way, Way.find(1))
391 # upload multiple versions of the same element in the same diff.
392 def test_upload_multiple_valid
393 basic_authorization "test@openstreetmap.org", "test"
395 # change the location of a node multiple times, each time referencing
396 # the last version. doesn't this depend on version numbers being
401 <node id='1' lon='0' lat='0' changeset='1' version='1'/>
402 <node id='1' lon='1' lat='0' changeset='1' version='2'/>
403 <node id='1' lon='1' lat='1' changeset='1' version='3'/>
404 <node id='1' lon='1' lat='2' changeset='1' version='4'/>
405 <node id='1' lon='2' lat='2' changeset='1' version='5'/>
406 <node id='1' lon='3' lat='2' changeset='1' version='6'/>
407 <node id='1' lon='3' lat='3' changeset='1' version='7'/>
408 <node id='1' lon='9' lat='9' changeset='1' version='8'/>
415 post :upload, :id => 1
416 assert_response :success,
417 "can't upload multiple versions of an element in a diff: #{@response.body}"
419 # check the response is well-formed. its counter-intuitive, but the
420 # API will return multiple elements with the same ID and different
421 # version numbers for each change we made.
422 assert_select "diffResult>node", 8
426 # upload multiple versions of the same element in the same diff, but
427 # keep the version numbers the same.
428 def test_upload_multiple_duplicate
429 basic_authorization "test@openstreetmap.org", "test"
434 <node id='1' lon='0' lat='0' changeset='1' version='1'/>
435 <node id='1' lon='1' lat='1' changeset='1' version='1'/>
442 post :upload, :id => 1
443 assert_response :conflict,
444 "shouldn't be able to upload the same element twice in a diff: #{@response.body}"
448 # try to upload some elements without specifying the version
449 def test_upload_missing_version
450 basic_authorization "test@openstreetmap.org", "test"
455 <node id='1' lon='1' lat='1' changeset='1'/>
462 post :upload, :id => 1
463 assert_response :bad_request,
464 "shouldn't be able to upload an element without version: #{@response.body}"
468 # try to upload with commands other than create, modify, or delete
469 def test_action_upload_invalid
470 basic_authorization "test@openstreetmap.org", "test"
475 <node id='1' lon='1' lat='1' changeset='1' />
480 post :upload, :id => 1
481 assert_response :bad_request, "Shouldn't be able to upload a diff with the action ping"
482 assert_equal @response.body, "Unknown action ping, choices are create, modify, delete."
486 # upload a valid changeset which has a mixture of whitespace
487 # to check a bug reported by ivansanchez (#1565).
488 def test_upload_whitespace_valid
489 basic_authorization "test@openstreetmap.org", "test"
493 <modify><node id='1' lon='0' lat='0' changeset='1'
495 <node id='1' lon='1' lat='1' changeset='1' version='2'><tag k='k' v='v'/></node></modify>
497 <relation id='1' changeset='1' version='1'><member
498 type='way' role='some' ref='3'/><member
499 type='node' role='some' ref='5'/>
500 <member type='relation' role='some' ref='3'/>
502 </modify></osmChange>
507 post :upload, :id => 1
508 assert_response :success,
509 "can't upload a valid diff with whitespace variations to changeset: #{@response.body}"
511 # check the response is well-formed
512 assert_select "diffResult>node", 2
513 assert_select "diffResult>relation", 1
515 # check that the changes made it into the database
516 assert_equal 1, Node.find(1).tags.size, "node 1 should now have one tag"
517 assert_equal 0, Relation.find(1).tags.size, "relation 1 should now have no tags"
521 # upload a valid changeset which has a mixture of whitespace
522 # to check a bug reported by ivansanchez.
523 def test_upload_reuse_placeholder_valid
524 basic_authorization "test@openstreetmap.org", "test"
529 <node id='-1' lon='0' lat='0' changeset='1'>
530 <tag k="foo" v="bar"/>
534 <node id='-1' lon='1' lat='1' changeset='1' version='1'/>
537 <node id='-1' lon='2' lat='2' changeset='1' version='2'/>
544 post :upload, :id => 1
545 assert_response :success,
546 "can't upload a valid diff with re-used placeholders to changeset: #{@response.body}"
548 # check the response is well-formed
549 assert_select "diffResult>node", 3
550 assert_select "diffResult>node[old_id=-1]", 3
554 # test what happens if a diff upload re-uses placeholder IDs in an
556 def test_upload_placeholder_invalid
557 basic_authorization "test@openstreetmap.org", "test"
562 <node id='-1' lon='0' lat='0' changeset='1' version='1'/>
563 <node id='-1' lon='1' lat='1' changeset='1' version='1'/>
564 <node id='-1' lon='2' lat='2' changeset='1' version='2'/>
571 post :upload, :id => 1
572 assert_response :bad_request,
573 "shouldn't be able to re-use placeholder IDs"
577 # test that uploading a way referencing invalid placeholders gives a
578 # proper error, not a 500.
579 def test_upload_placeholder_invalid_way
580 basic_authorization "test@example.com", "test"
585 <node id="-1" lon="0" lat="0" changeset="2" version="1"/>
586 <node id="-2" lon="1" lat="1" changeset="2" version="1"/>
587 <node id="-3" lon="2" lat="2" changeset="2" version="1"/>
588 <way id="-1" changeset="2" version="1">
600 post :upload, :id => 2
601 assert_response :bad_request,
602 "shouldn't be able to use invalid placeholder IDs"
603 assert_equal "Placeholder node not found for reference -4 in way -1", @response.body
605 # the same again, but this time use an existing way
609 <node id="-1" lon="0" lat="0" changeset="2" version="1"/>
610 <node id="-2" lon="1" lat="1" changeset="2" version="1"/>
611 <node id="-3" lon="2" lat="2" changeset="2" version="1"/>
612 <way id="1" changeset="2" version="1">
624 post :upload, :id => 2
625 assert_response :bad_request,
626 "shouldn't be able to use invalid placeholder IDs"
627 assert_equal "Placeholder node not found for reference -4 in way 1", @response.body
631 # test that uploading a relation referencing invalid placeholders gives a
632 # proper error, not a 500.
633 def test_upload_placeholder_invalid_relation
634 basic_authorization "test@example.com", "test"
639 <node id="-1" lon="0" lat="0" changeset="2" version="1"/>
640 <node id="-2" lon="1" lat="1" changeset="2" version="1"/>
641 <node id="-3" lon="2" lat="2" changeset="2" version="1"/>
642 <relation id="-1" changeset="2" version="1">
643 <member type="node" role="foo" ref="-1"/>
644 <member type="node" role="foo" ref="-2"/>
645 <member type="node" role="foo" ref="-3"/>
646 <member type="node" role="foo" ref="-4"/>
654 post :upload, :id => 2
655 assert_response :bad_request,
656 "shouldn't be able to use invalid placeholder IDs"
657 assert_equal "Placeholder Node not found for reference -4 in relation -1.", @response.body
659 # the same again, but this time use an existing way
663 <node id="-1" lon="0" lat="0" changeset="2" version="1"/>
664 <node id="-2" lon="1" lat="1" changeset="2" version="1"/>
665 <node id="-3" lon="2" lat="2" changeset="2" version="1"/>
666 <relation id="1" changeset="2" version="1">
667 <member type="node" role="foo" ref="-1"/>
668 <member type="node" role="foo" ref="-2"/>
669 <member type="node" role="foo" ref="-3"/>
670 <member type="way" role="bar" ref="-1"/>
678 post :upload, :id => 2
679 assert_response :bad_request,
680 "shouldn't be able to use invalid placeholder IDs"
681 assert_equal "Placeholder Way not found for reference -1 in relation 1.", @response.body
685 # test what happens if a diff is uploaded containing only a node
687 def test_upload_node_move
688 basic_authorization "test@openstreetmap.org", "test"
690 content "<osm><changeset>" +
691 "<tag k='created_by' v='osm test suite checking changesets'/>" +
694 assert_response :success
695 changeset_id = @response.body.to_i
697 old_node = current_nodes(:visible_node)
699 diff = XML::Document.new
700 diff.root = XML::Node.new "osmChange"
701 modify = XML::Node.new "modify"
702 xml_old_node = old_node.to_xml_node
703 xml_old_node["lat"] = (2.0).to_s
704 xml_old_node["lon"] = (2.0).to_s
705 xml_old_node["changeset"] = changeset_id.to_s
706 modify << xml_old_node
711 post :upload, :id => changeset_id
712 assert_response :success,
713 "diff should have uploaded OK"
716 changeset = Changeset.find(changeset_id)
717 assert_equal 1*SCALE, changeset.min_lon, "min_lon should be 1 degree"
718 assert_equal 2*SCALE, changeset.max_lon, "max_lon should be 2 degrees"
719 assert_equal 1*SCALE, changeset.min_lat, "min_lat should be 1 degree"
720 assert_equal 2*SCALE, changeset.max_lat, "max_lat should be 2 degrees"
724 # test what happens if a diff is uploaded adding a node to a way.
725 def test_upload_way_extend
726 basic_authorization "test@openstreetmap.org", "test"
728 content "<osm><changeset>" +
729 "<tag k='created_by' v='osm test suite checking changesets'/>" +
732 assert_response :success
733 changeset_id = @response.body.to_i
735 old_way = current_ways(:visible_way)
737 diff = XML::Document.new
738 diff.root = XML::Node.new "osmChange"
739 modify = XML::Node.new "modify"
740 xml_old_way = old_way.to_xml_node
741 nd_ref = XML::Node.new "nd"
742 nd_ref["ref"] = current_nodes(:visible_node).id.to_s
743 xml_old_way << nd_ref
744 xml_old_way["changeset"] = changeset_id.to_s
745 modify << xml_old_way
750 post :upload, :id => changeset_id
751 assert_response :success,
752 "diff should have uploaded OK"
755 changeset = Changeset.find(changeset_id)
756 assert_equal 1*SCALE, changeset.min_lon, "min_lon should be 1 degree"
757 assert_equal 3*SCALE, changeset.max_lon, "max_lon should be 3 degrees"
758 assert_equal 1*SCALE, changeset.min_lat, "min_lat should be 1 degree"
759 assert_equal 3*SCALE, changeset.max_lat, "max_lat should be 3 degrees"
763 # test for more issues in #1568
764 def test_upload_empty_invalid
765 basic_authorization "test@openstreetmap.org", "test"
768 "<osmChange></osmChange>",
769 "<osmChange><modify/></osmChange>",
770 "<osmChange><modify></modify></osmChange>"
774 post :upload, :id => 1
775 assert_response(:success, "should be able to upload " +
776 "empty changeset: " + diff)
781 # when we make some simple changes we get the same changes back from the
783 def test_diff_download_simple
784 basic_authorization(users(:normal_user).email, "test")
786 # create a temporary changeset
787 content "<osm><changeset>" +
788 "<tag k='created_by' v='osm test suite checking changesets'/>" +
791 assert_response :success
792 changeset_id = @response.body.to_i
798 <node id='1' lon='0' lat='0' changeset='#{changeset_id}' version='1'/>
799 <node id='1' lon='1' lat='0' changeset='#{changeset_id}' version='2'/>
800 <node id='1' lon='1' lat='1' changeset='#{changeset_id}' version='3'/>
801 <node id='1' lon='1' lat='2' changeset='#{changeset_id}' version='4'/>
802 <node id='1' lon='2' lat='2' changeset='#{changeset_id}' version='5'/>
803 <node id='1' lon='3' lat='2' changeset='#{changeset_id}' version='6'/>
804 <node id='1' lon='3' lat='3' changeset='#{changeset_id}' version='7'/>
805 <node id='1' lon='9' lat='9' changeset='#{changeset_id}' version='8'/>
812 post :upload, :id => changeset_id
813 assert_response :success,
814 "can't upload multiple versions of an element in a diff: #{@response.body}"
816 get :download, :id => changeset_id
817 assert_response :success
819 assert_select "osmChange", 1
820 assert_select "osmChange>modify", 8
821 assert_select "osmChange>modify>node", 8
825 # culled this from josm to ensure that nothing in the way that josm
826 # is formatting the request is causing it to fail.
828 # NOTE: the error turned out to be something else completely!
830 basic_authorization(users(:normal_user).email, "test")
832 # create a temporary changeset
833 content "<osm><changeset>" +
834 "<tag k='created_by' v='osm test suite checking changesets'/>" +
837 assert_response :success
838 changeset_id = @response.body.to_i
841 <osmChange version="0.6" generator="JOSM">
842 <create version="0.6" generator="JOSM">
843 <node id='-1' visible='true' changeset='#{changeset_id}' lat='51.49619982187321' lon='-0.18722061869438314' />
844 <node id='-2' visible='true' changeset='#{changeset_id}' lat='51.496359883909605' lon='-0.18653093576241928' />
845 <node id='-3' visible='true' changeset='#{changeset_id}' lat='51.49598132358285' lon='-0.18719613290981638' />
846 <node id='-4' visible='true' changeset='#{changeset_id}' lat='51.4961591711078' lon='-0.18629015888084607' />
847 <node id='-5' visible='true' changeset='#{changeset_id}' lat='51.49582126021711' lon='-0.18708186591517145' />
848 <node id='-6' visible='true' changeset='#{changeset_id}' lat='51.49591018437858' lon='-0.1861432441734455' />
849 <node id='-7' visible='true' changeset='#{changeset_id}' lat='51.49560784152179' lon='-0.18694719410005425' />
850 <node id='-8' visible='true' changeset='#{changeset_id}' lat='51.49567389979617' lon='-0.1860289771788006' />
851 <node id='-9' visible='true' changeset='#{changeset_id}' lat='51.49543761398892' lon='-0.186820684213126' />
852 <way id='-10' action='modiy' visible='true' changeset='#{changeset_id}'>
862 <tag k='highway' v='residential' />
863 <tag k='name' v='Foobar Street' />
871 post :upload, :id => changeset_id
872 assert_response :success,
873 "can't upload a diff from JOSM: #{@response.body}"
875 get :download, :id => changeset_id
876 assert_response :success
878 assert_select "osmChange", 1
879 assert_select "osmChange>create>node", 9
880 assert_select "osmChange>create>way", 1
881 assert_select "osmChange>create>way>nd", 9
882 assert_select "osmChange>create>way>tag", 2
886 # when we make some complex changes we get the same changes back from the
888 def test_diff_download_complex
889 basic_authorization(users(:normal_user).email, "test")
891 # create a temporary changeset
892 content "<osm><changeset>" +
893 "<tag k='created_by' v='osm test suite checking changesets'/>" +
896 assert_response :success
897 changeset_id = @response.body.to_i
903 <node id='1' lon='0' lat='0' changeset='#{changeset_id}' version='1'/>
906 <node id='-1' lon='9' lat='9' changeset='#{changeset_id}' version='0'/>
907 <node id='-2' lon='8' lat='9' changeset='#{changeset_id}' version='0'/>
908 <node id='-3' lon='7' lat='9' changeset='#{changeset_id}' version='0'/>
911 <node id='3' lon='20' lat='15' changeset='#{changeset_id}' version='1'/>
912 <way id='1' changeset='#{changeset_id}' version='1'>
924 post :upload, :id => changeset_id
925 assert_response :success,
926 "can't upload multiple versions of an element in a diff: #{@response.body}"
928 get :download, :id => changeset_id
929 assert_response :success
931 assert_select "osmChange", 1
932 assert_select "osmChange>create", 3
933 assert_select "osmChange>delete", 1
934 assert_select "osmChange>modify", 2
935 assert_select "osmChange>create>node", 3
936 assert_select "osmChange>delete>node", 1
937 assert_select "osmChange>modify>node", 1
938 assert_select "osmChange>modify>way", 1
942 # check that the bounding box of a changeset gets updated correctly
943 def test_changeset_bbox
944 basic_authorization "test@openstreetmap.org", "test"
946 # create a new changeset
947 content "<osm><changeset/></osm>"
949 assert_response :success, "Creating of changeset failed."
950 changeset_id = @response.body.to_i
952 # add a single node to it
953 with_controller(NodeController.new) do
954 content "<osm><node lon='1' lat='2' changeset='#{changeset_id}'/></osm>"
956 assert_response :success, "Couldn't create node."
959 # get the bounding box back from the changeset
960 get :read, :id => changeset_id
961 assert_response :success, "Couldn't read back changeset."
962 assert_select "osm>changeset[min_lon=1.0]", 1
963 assert_select "osm>changeset[max_lon=1.0]", 1
964 assert_select "osm>changeset[min_lat=2.0]", 1
965 assert_select "osm>changeset[max_lat=2.0]", 1
967 # add another node to it
968 with_controller(NodeController.new) do
969 content "<osm><node lon='2' lat='1' changeset='#{changeset_id}'/></osm>"
971 assert_response :success, "Couldn't create second node."
974 # get the bounding box back from the changeset
975 get :read, :id => changeset_id
976 assert_response :success, "Couldn't read back changeset for the second time."
977 assert_select "osm>changeset[min_lon=1.0]", 1
978 assert_select "osm>changeset[max_lon=2.0]", 1
979 assert_select "osm>changeset[min_lat=1.0]", 1
980 assert_select "osm>changeset[max_lat=2.0]", 1
982 # add (delete) a way to it, which contains a point at (3,3)
983 with_controller(WayController.new) do
984 content update_changeset(current_ways(:visible_way).to_xml,
986 put :delete, :id => current_ways(:visible_way).id
987 assert_response :success, "Couldn't delete a way."
990 # get the bounding box back from the changeset
991 get :read, :id => changeset_id
992 assert_response :success, "Couldn't read back changeset for the third time."
993 # note that the 3.1 here is because of the bbox overexpansion
994 assert_select "osm>changeset[min_lon=1.0]", 1
995 assert_select "osm>changeset[max_lon=3.1]", 1
996 assert_select "osm>changeset[min_lat=1.0]", 1
997 assert_select "osm>changeset[max_lat=3.1]", 1
1001 # test that the changeset :include method works as it should
1002 def test_changeset_include
1003 basic_authorization "test@openstreetmap.org", "test"
1005 # create a new changeset
1006 content "<osm><changeset/></osm>"
1008 assert_response :success, "Creating of changeset failed."
1009 changeset_id = @response.body.to_i
1011 # NOTE: the include method doesn't over-expand, like inserting
1012 # a real method does. this is because we expect the client to
1013 # know what it is doing!
1014 check_after_include(changeset_id, 1, 1, [ 1, 1, 1, 1])
1015 check_after_include(changeset_id, 3, 3, [ 1, 1, 3, 3])
1016 check_after_include(changeset_id, 4, 2, [ 1, 1, 4, 3])
1017 check_after_include(changeset_id, 2, 2, [ 1, 1, 4, 3])
1018 check_after_include(changeset_id, -1, -1, [-1, -1, 4, 3])
1019 check_after_include(changeset_id, -2, 5, [-2, -1, 4, 5])
1023 # test the query functionality of changesets
1025 get :query, :bbox => "-10,-10, 10, 10"
1026 assert_response :success, "can't get changesets in bbox"
1027 assert_changesets [1,4,6]
1029 get :query, :bbox => "4.5,4.5,4.6,4.6"
1030 assert_response :success, "can't get changesets in bbox"
1031 assert_changesets [1]
1033 # can't get changesets of user 1 without authenticating
1034 get :query, :user => users(:normal_user).id
1035 assert_response :not_found, "shouldn't be able to get changesets by non-public user"
1037 # but this should work
1038 basic_authorization "test@openstreetmap.org", "test"
1039 get :query, :user => users(:normal_user).id
1040 assert_response :success, "can't get changesets by user"
1041 assert_changesets [1,3,4,6]
1043 get :query, :user => users(:normal_user).id, :open => true
1044 assert_response :success, "can't get changesets by user and open"
1045 assert_changesets [1,4]
1047 get :query, :time => '2007-12-31'
1048 assert_response :success, "can't get changesets by time-since"
1049 assert_changesets [1,2,4,5,6]
1051 get :query, :time => '2008-01-01T12:34Z'
1052 assert_response :success, "can't get changesets by time-since with hour"
1053 assert_changesets [1,2,4,5,6]
1055 get :query, :time => '2007-12-31T23:59Z,2008-01-01T00:01Z'
1056 assert_response :success, "can't get changesets by time-range"
1057 assert_changesets [1,4,5,6]
1059 get :query, :open => 'true'
1060 assert_response :success, "can't get changesets by open-ness"
1061 assert_changesets [1,2,4]
1065 # check that errors are returned if garbage is inserted
1066 # into query strings
1067 def test_query_invalid
1070 ";drop table users;"
1072 get :query, :bbox => bbox
1073 assert_response :bad_request, "'#{bbox}' isn't a bbox"
1078 ";drop table users;",
1082 get :query, :time => time
1083 assert_response :bad_request, "'#{time}' isn't a valid time range"
1091 get :query, :user => uid
1092 assert_response :bad_request, "'#{uid}' isn't a valid user ID"
1097 # check updating tags on a changeset
1098 def test_changeset_update
1099 changeset = changesets(:normal_user_first_change)
1100 new_changeset = changeset.to_xml
1101 new_tag = XML::Node.new "tag"
1102 new_tag['k'] = "tagtesting"
1103 new_tag['v'] = "valuetesting"
1104 new_changeset.find("//osm/changeset").first << new_tag
1105 content new_changeset
1107 # try without any authorization
1108 put :update, :id => changeset.id
1109 assert_response :unauthorized
1111 # try with the wrong authorization
1112 basic_authorization "test@example.com", "test"
1113 put :update, :id => changeset.id
1114 assert_response :conflict
1116 # now this should work...
1117 basic_authorization "test@openstreetmap.org", "test"
1118 put :update, :id => changeset.id
1119 assert_response :success
1121 assert_select "osm>changeset[id=#{changeset.id}]", 1
1122 assert_select "osm>changeset>tag", 2
1123 assert_select "osm>changeset>tag[k=tagtesting][v=valuetesting]", 1
1127 # check that a user different from the one who opened the changeset
1129 def test_changeset_update_invalid
1130 basic_authorization "test@example.com", "test"
1132 changeset = changesets(:normal_user_first_change)
1133 new_changeset = changeset.to_xml
1134 new_tag = XML::Node.new "tag"
1135 new_tag['k'] = "testing"
1136 new_tag['v'] = "testing"
1137 new_changeset.find("//osm/changeset").first << new_tag
1139 content new_changeset
1140 put :update, :id => changeset.id
1141 assert_response :conflict
1145 # check that a changeset can contain a certain max number of changes.
1146 def test_changeset_limits
1147 basic_authorization "test@openstreetmap.org", "test"
1149 # open a new changeset
1150 content "<osm><changeset/></osm>"
1152 assert_response :success, "can't create a new changeset"
1153 cs_id = @response.body.to_i
1155 # start the counter just short of where the changeset should finish.
1157 # alter the database to set the counter on the changeset directly,
1158 # otherwise it takes about 6 minutes to fill all of them.
1159 changeset = Changeset.find(cs_id)
1160 changeset.num_changes = Changeset::MAX_ELEMENTS - offset
1163 with_controller(NodeController.new) do
1165 content "<osm><node changeset='#{cs_id}' lat='0.0' lon='0.0'/></osm>"
1167 assert_response :success, "can't create a new node"
1168 node_id = @response.body.to_i
1170 get :read, :id => node_id
1171 assert_response :success, "can't read back new node"
1172 node_doc = XML::Parser.string(@response.body).parse
1173 node_xml = node_doc.find("//osm/node").first
1175 # loop until we fill the changeset with nodes
1177 node_xml['lat'] = rand.to_s
1178 node_xml['lon'] = rand.to_s
1179 node_xml['version'] = (i+1).to_s
1182 put :update, :id => node_id
1183 assert_response :success, "attempt #{i} should have succeeded"
1186 # trying again should fail
1187 node_xml['lat'] = rand.to_s
1188 node_xml['lon'] = rand.to_s
1189 node_xml['version'] = offset.to_s
1192 put :update, :id => node_id
1193 assert_response :conflict, "final attempt should have failed"
1196 changeset = Changeset.find(cs_id)
1197 assert_equal Changeset::MAX_ELEMENTS + 1, changeset.num_changes
1199 # check that the changeset is now closed as well
1200 assert(!changeset.is_open?,
1201 "changeset should have been auto-closed by exceeding " +
1205 # This should display the last 20 changesets closed.
1207 @changesets = Changeset.find(:all, :order => "created_at DESC", :conditions => ['min_lat IS NOT NULL'], :limit=> 20)
1208 assert @changesets.size <= 20
1210 assert_response :success
1211 assert_template "list"
1212 # Now check that all 20 (or however many were returned) changesets are in the html
1213 assert_select "h1", :text => "Recent Changes", :count => 1
1214 assert_select "table[id='keyvalue'] tr", :count => @changesets.size + 1
1215 @changesets.each do |changeset|
1216 # FIXME this test needs rewriting - test for table contents
1220 #------------------------------------------------------------
1222 #------------------------------------------------------------
1225 # boilerplate for checking that certain changesets exist in the
1227 def assert_changesets(ids)
1228 assert_select "osm>changeset", ids.size
1230 assert_select "osm>changeset[id=#{id}]", 1
1235 # call the include method and assert properties of the bbox
1236 def check_after_include(changeset_id, lon, lat, bbox)
1237 content "<osm><node lon='#{lon}' lat='#{lat}'/></osm>"
1238 post :expand_bbox, :id => changeset_id
1239 assert_response :success, "Setting include of changeset failed: #{@response.body}"
1241 # check exactly one changeset
1242 assert_select "osm>changeset", 1
1243 assert_select "osm>changeset[id=#{changeset_id}]", 1
1246 doc = XML::Parser.string(@response.body).parse
1247 changeset = doc.find("//osm/changeset").first
1248 assert_equal bbox[0], changeset['min_lon'].to_f, "min lon"
1249 assert_equal bbox[1], changeset['min_lat'].to_f, "min lat"
1250 assert_equal bbox[2], changeset['max_lon'].to_f, "max lon"
1251 assert_equal bbox[3], changeset['max_lat'].to_f, "max lat"
1255 # update the changeset_id of a way element
1256 def update_changeset(xml, changeset_id)
1257 xml_attr_rewrite(xml, 'changeset', changeset_id)
1261 # update an attribute in a way element
1262 def xml_attr_rewrite(xml, name, value)
1263 xml.find("//osm/way").first[name] = value.to_s