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 users(:normal_user).email, "test"
13 # Create the first user's changeset
14 content "<osm><changeset>" +
15 "<tag k='created_by' v='osm test suite checking changesets'/>" +
18 assert_require_public_data
21 basic_authorization users(:public_user).email, "test"
22 # Create the first user's changeset
23 content "<osm><changeset>" +
24 "<tag k='created_by' v='osm test suite checking changesets'/>" +
28 assert_response :success, "Creation of changeset did not return sucess status"
29 newid = @response.body.to_i
31 # check end time, should be an hour ahead of creation time
32 cs = Changeset.find(newid)
33 duration = cs.closed_at - cs.created_at
34 # the difference can either be a rational, or a floating point number
35 # of seconds, depending on the code path taken :-(
36 if duration.class == Rational
37 assert_equal Rational(1,24), duration , "initial idle timeout should be an hour (#{cs.created_at} -> #{cs.closed_at})"
39 # must be number of seconds...
40 assert_equal 3600, duration.round, "initial idle timeout should be an hour (#{cs.created_at} -> #{cs.closed_at})"
44 def test_create_invalid
45 basic_authorization users(:normal_user).email, "test"
46 content "<osm><changeset></osm>"
48 assert_require_public_data
50 ## Try the public user
51 basic_authorization users(:public_user).email, "test"
52 content "<osm><changeset></osm>"
54 assert_response :bad_request, "creating a invalid changeset should fail"
57 def test_create_invalid_no_content
58 ## First check with no auth
60 assert_response :unauthorized, "shouldn't be able to create a changeset with no auth"
62 ## Now try to with the non-public user
63 basic_authorization users(:normal_user).email, "test"
65 assert_require_public_data
67 ## Try the inactive user
68 basic_authorization users(:inactive_user).email, "test"
72 ## Now try to use the public user
73 basic_authorization users(:public_user).email, "test"
75 assert_response :bad_request, "creating a changeset with no content should fail"
78 def test_create_wrong_method
79 basic_authorization users(:public_user).email, "test"
81 assert_response :method_not_allowed
83 assert_response :method_not_allowed
87 # check that the changeset can be read and returns the correct
90 changeset_id = changesets(:normal_user_first_change).id
91 get :read, :id => changeset_id
92 assert_response :success, "cannot get first changeset"
94 assert_select "osm[version=#{API_VERSION}][generator=\"OpenStreetMap server\"]", 1
95 assert_select "osm>changeset[id=#{changeset_id}]", 1
99 # test that the user who opened a change can close it
101 ## Try without authentication
102 put :close, :id => changesets(:public_user_first_change).id
103 assert_response :unauthorized
106 ## Try using the non-public user
107 basic_authorization users(:normal_user).email, "test"
108 put :close, :id => changesets(:normal_user_first_change).id
109 assert_require_public_data
112 ## The try with the public user
113 basic_authorization users(:public_user).email, "test"
115 cs_id = changesets(:public_user_first_change).id
116 put :close, :id => cs_id
117 assert_response :success
119 # test that it really is closed now
120 cs = Changeset.find(cs_id)
122 "changeset should be closed now (#{cs.closed_at} > #{Time.now}.")
126 # test that a different user can't close another user's changeset
127 def test_close_invalid
128 basic_authorization users(:public_user).email, "test"
130 put :close, :id => changesets(:normal_user_first_change).id
131 assert_response :conflict
132 assert_equal "The user doesn't own that changeset", @response.body
136 # upload something simple, but valid and check that it can
138 # Also try without auth and another user.
139 def test_upload_simple_valid
141 changeset_id = changesets(:public_user_first_change).id
143 # simple diff to change a node, way and relation by removing
148 <node id='1' lon='0' lat='0' changeset='#{changeset_id}' version='1'/>
149 <way id='1' changeset='#{changeset_id}' version='1'>
154 <relation id='1' changeset='#{changeset_id}' version='1'>
155 <member type='way' role='some' ref='3'/>
156 <member type='node' role='some' ref='5'/>
157 <member type='relation' role='some' ref='3'/>
165 post :upload, :id => changeset_id
166 assert_response :unauthorized,
167 "shouldnn't be able to upload a simple valid diff to changeset: #{@response.body}"
171 ## Now try with a private user
172 basic_authorization users(:normal_user).email, "test"
173 changeset_id = changesets(:normal_user_first_change).id
175 # simple diff to change a node, way and relation by removing
180 <node id='1' lon='0' lat='0' changeset='#{changeset_id}' version='1'/>
181 <way id='1' changeset='#{changeset_id}' version='1'>
186 <relation id='1' changeset='#{changeset_id}' version='1'>
187 <member type='way' role='some' ref='3'/>
188 <member type='node' role='some' ref='5'/>
189 <member type='relation' role='some' ref='3'/>
197 post :upload, :id => changeset_id
198 assert_response :forbidden,
199 "can't upload a simple valid diff to changeset: #{@response.body}"
203 ## Now try with the public user
204 basic_authorization users(:public_user).email, "test"
205 changeset_id = changesets(:public_user_first_change).id
207 # simple diff to change a node, way and relation by removing
212 <node id='1' lon='0' lat='0' changeset='#{changeset_id}' version='1'/>
213 <way id='1' changeset='#{changeset_id}' version='1'>
218 <relation id='1' changeset='#{changeset_id}' version='1'>
219 <member type='way' role='some' ref='3'/>
220 <member type='node' role='some' ref='5'/>
221 <member type='relation' role='some' ref='3'/>
229 post :upload, :id => changeset_id
230 assert_response :success,
231 "can't upload a simple valid diff to changeset: #{@response.body}"
233 # check that the changes made it into the database
234 assert_equal 0, Node.find(1).tags.size, "node 1 should now have no tags"
235 assert_equal 0, Way.find(1).tags.size, "way 1 should now have no tags"
236 assert_equal 0, Relation.find(1).tags.size, "relation 1 should now have no tags"
240 # upload something which creates new objects using placeholders
241 def test_upload_create_valid
242 basic_authorization users(:public_user).email, "test"
243 cs_id = changesets(:public_user_first_change).id
245 # simple diff to create a node way and relation using placeholders
249 <node id='-1' lon='0' lat='0' changeset='#{cs_id}'>
250 <tag k='foo' v='bar'/>
251 <tag k='baz' v='bat'/>
253 <way id='-1' changeset='#{cs_id}'>
258 <relation id='-1' changeset='#{cs_id}'>
259 <member type='way' role='some' ref='3'/>
260 <member type='node' role='some' ref='5'/>
261 <member type='relation' role='some' ref='3'/>
269 post :upload, :id => cs_id
270 assert_response :success,
271 "can't upload a simple valid creation to changeset: #{@response.body}"
273 # check the returned payload
274 assert_select "diffResult[version=#{API_VERSION}][generator=\"OpenStreetMap server\"]", 1
275 assert_select "diffResult>node", 1
276 assert_select "diffresult>way", 1
277 assert_select "diffResult>relation", 1
279 # inspect the response to find out what the new element IDs are
280 doc = XML::Parser.string(@response.body).parse
281 new_node_id = doc.find("//diffResult/node").first["new_id"].to_i
282 new_way_id = doc.find("//diffResult/way").first["new_id"].to_i
283 new_rel_id = doc.find("//diffResult/relation").first["new_id"].to_i
285 # check the old IDs are all present and negative one
286 assert_equal -1, doc.find("//diffResult/node").first["old_id"].to_i
287 assert_equal -1, doc.find("//diffResult/way").first["old_id"].to_i
288 assert_equal -1, doc.find("//diffResult/relation").first["old_id"].to_i
290 # check the versions are present and equal one
291 assert_equal 1, doc.find("//diffResult/node").first["new_version"].to_i
292 assert_equal 1, doc.find("//diffResult/way").first["new_version"].to_i
293 assert_equal 1, doc.find("//diffResult/relation").first["new_version"].to_i
295 # check that the changes made it into the database
296 assert_equal 2, Node.find(new_node_id).tags.size, "new node should have two tags"
297 assert_equal 0, Way.find(new_way_id).tags.size, "new way should have no tags"
298 assert_equal 0, Relation.find(new_rel_id).tags.size, "new relation should have no tags"
302 # test a complex delete where we delete elements which rely on eachother
303 # in the same transaction.
304 def test_upload_delete
305 basic_authorization users(:public_user).display_name, "test"
307 diff = XML::Document.new
308 diff.root = XML::Node.new "osmChange"
309 delete = XML::Node.new "delete"
311 delete << current_relations(:visible_relation).to_xml_node
312 delete << current_relations(:used_relation).to_xml_node
313 delete << current_ways(:used_way).to_xml_node
314 delete << current_nodes(:node_used_by_relationship).to_xml_node
316 # update the changeset to one that this user owns
317 changeset_id = changesets(:public_user_first_change).id
318 ["node", "way", "relation"].each do |type|
319 delete.find("//osmChange/delete/#{type}").each do |n|
320 n['changeset'] = changeset_id.to_s
326 post :upload, :id => changeset_id
327 assert_response :success,
328 "can't upload a deletion diff to changeset: #{@response.body}"
330 # check the response is well-formed
331 assert_select "diffResult>node", 1
332 assert_select "diffResult>way", 1
333 assert_select "diffResult>relation", 2
335 # check that everything was deleted
336 assert_equal false, Node.find(current_nodes(:node_used_by_relationship).id).visible
337 assert_equal false, Way.find(current_ways(:used_way).id).visible
338 assert_equal false, Relation.find(current_relations(:visible_relation).id).visible
339 assert_equal false, Relation.find(current_relations(:used_relation).id).visible
343 # test uploading a delete with no lat/lon, as they are optional in
344 # the osmChange spec.
345 def test_upload_nolatlon_delete
346 basic_authorization users(:public_user).display_name, "test"
348 node = current_nodes(:public_visible_node)
349 cs = changesets(:public_user_first_change)
350 diff = "<osmChange><delete><node id='#{node.id}' version='#{node.version}' changeset='#{cs.id}'/></delete></osmChange>"
354 post :upload, :id => cs.id
355 assert_response :success,
356 "can't upload a deletion diff to changeset: #{@response.body}"
358 # check the response is well-formed
359 assert_select "diffResult>node", 1
361 # check that everything was deleted
362 assert_equal false, Node.find(node.id).visible
365 def test_repeated_changeset_create
367 basic_authorization users(:public_user).email, "test"
369 # create a temporary changeset
370 content "<osm><changeset>" +
371 "<tag k='created_by' v='osm test suite checking changesets'/>" +
373 assert_difference('Changeset.count', 1) do
376 assert_response :success
377 changeset_id = @response.body.to_i
382 # test that deleting stuff in a transaction doesn't bypass the checks
383 # to ensure that used elements are not deleted.
384 def test_upload_delete_invalid
385 basic_authorization users(:public_user).email, "test"
387 diff = XML::Document.new
388 diff.root = XML::Node.new "osmChange"
389 delete = XML::Node.new "delete"
391 delete << current_relations(:public_visible_relation).to_xml_node
392 delete << current_ways(:used_way).to_xml_node
393 delete << current_nodes(:node_used_by_relationship).to_xml_node
397 post :upload, :id => 2
398 assert_response :precondition_failed,
399 "shouldn't be able to upload a invalid deletion diff: #{@response.body}"
400 assert_equal "Precondition failed: Way 3 still used by relation 1.", @response.body
402 # check that nothing was, in fact, deleted
403 assert_equal true, Node.find(current_nodes(:node_used_by_relationship).id).visible
404 assert_equal true, Way.find(current_ways(:used_way).id).visible
405 assert_equal true, Relation.find(current_relations(:visible_relation).id).visible
409 # upload something which creates new objects and inserts them into
410 # existing containers using placeholders.
411 def test_upload_complex
412 basic_authorization users(:public_user).email, "test"
413 cs_id = changesets(:public_user_first_change).id
415 # simple diff to create a node way and relation using placeholders
419 <node id='-1' lon='0' lat='0' changeset='#{cs_id}'>
420 <tag k='foo' v='bar'/>
421 <tag k='baz' v='bat'/>
425 <way id='1' changeset='#{cs_id}' version='1'>
429 <relation id='1' changeset='#{cs_id}' version='1'>
430 <member type='way' role='some' ref='3'/>
431 <member type='node' role='some' ref='-1'/>
432 <member type='relation' role='some' ref='3'/>
440 post :upload, :id => cs_id
441 assert_response :success,
442 "can't upload a complex diff to changeset: #{@response.body}"
444 # check the returned payload
445 assert_select "diffResult[version=#{API_VERSION}][generator=\"#{GENERATOR}\"]", 1
446 assert_select "diffResult>node", 1
447 assert_select "diffResult>way", 1
448 assert_select "diffResult>relation", 1
450 # inspect the response to find out what the new element IDs are
451 doc = XML::Parser.string(@response.body).parse
452 new_node_id = doc.find("//diffResult/node").first["new_id"].to_i
454 # check that the changes made it into the database
455 assert_equal 2, Node.find(new_node_id).tags.size, "new node should have two tags"
456 assert_equal [new_node_id, 3], Way.find(1).nds, "way nodes should match"
457 Relation.find(1).members.each do |type,id,role|
459 assert_equal new_node_id, id, "relation should contain new node"
465 # create a diff which references several changesets, which should cause
466 # a rollback and none of the diff gets committed
467 def test_upload_invalid_changesets
468 basic_authorization users(:public_user).email, "test"
469 cs_id = changesets(:public_user_first_change).id
471 # simple diff to create a node way and relation using placeholders
475 <node id='1' lon='0' lat='0' changeset='#{cs_id}' version='1'/>
476 <way id='1' changeset='#{cs_id}' version='1'>
481 <relation id='1' changeset='#{cs_id}' version='1'>
482 <member type='way' role='some' ref='3'/>
483 <member type='node' role='some' ref='5'/>
484 <member type='relation' role='some' ref='3'/>
488 <node id='-1' lon='0' lat='0' changeset='4'>
489 <tag k='foo' v='bar'/>
490 <tag k='baz' v='bat'/>
495 # cache the objects before uploading them
496 node = current_nodes(:visible_node)
497 way = current_ways(:visible_way)
498 rel = current_relations(:visible_relation)
502 post :upload, :id => cs_id
503 assert_response :conflict,
504 "uploading a diff with multiple changsets should have failed"
506 # check that objects are unmodified
507 assert_nodes_are_equal(node, Node.find(1))
508 assert_ways_are_equal(way, Way.find(1))
512 # upload multiple versions of the same element in the same diff.
513 def test_upload_multiple_valid
514 basic_authorization users(:public_user).email, "test"
515 cs_id = changesets(:public_user_first_change).id
517 # change the location of a node multiple times, each time referencing
518 # the last version. doesn't this depend on version numbers being
523 <node id='1' lon='0' lat='0' changeset='#{cs_id}' version='1'/>
524 <node id='1' lon='1' lat='0' changeset='#{cs_id}' version='2'/>
525 <node id='1' lon='1' lat='1' changeset='#{cs_id}' version='3'/>
526 <node id='1' lon='1' lat='2' changeset='#{cs_id}' version='4'/>
527 <node id='1' lon='2' lat='2' changeset='#{cs_id}' version='5'/>
528 <node id='1' lon='3' lat='2' changeset='#{cs_id}' version='6'/>
529 <node id='1' lon='3' lat='3' changeset='#{cs_id}' version='7'/>
530 <node id='1' lon='9' lat='9' changeset='#{cs_id}' version='8'/>
537 post :upload, :id => cs_id
538 assert_response :success,
539 "can't upload multiple versions of an element in a diff: #{@response.body}"
541 # check the response is well-formed. its counter-intuitive, but the
542 # API will return multiple elements with the same ID and different
543 # version numbers for each change we made.
544 assert_select "diffResult>node", 8
548 # upload multiple versions of the same element in the same diff, but
549 # keep the version numbers the same.
550 def test_upload_multiple_duplicate
551 basic_authorization users(:public_user).email, "test"
552 cs_id = changesets(:public_user_first_change).id
557 <node id='1' lon='0' lat='0' changeset='#{cs_id}' version='1'/>
558 <node id='1' lon='1' lat='1' changeset='#{cs_id}' version='1'/>
565 post :upload, :id => cs_id
566 assert_response :conflict,
567 "shouldn't be able to upload the same element twice in a diff: #{@response.body}"
571 # try to upload some elements without specifying the version
572 def test_upload_missing_version
573 basic_authorization users(:public_user).email, "test"
574 cs_id = changesets(:public_user_first_change).id
579 <node id='1' lon='1' lat='1' changeset='cs_id'/>
586 post :upload, :id => cs_id
587 assert_response :bad_request,
588 "shouldn't be able to upload an element without version: #{@response.body}"
592 # try to upload with commands other than create, modify, or delete
593 def test_action_upload_invalid
594 basic_authorization users(:public_user).email, "test"
595 cs_id = changesets(:public_user_first_change).id
600 <node id='1' lon='1' lat='1' changeset='#{cs_id}' />
605 post :upload, :id => cs_id
606 assert_response :bad_request, "Shouldn't be able to upload a diff with the action ping"
607 assert_equal @response.body, "Unknown action ping, choices are create, modify, delete."
611 # upload a valid changeset which has a mixture of whitespace
612 # to check a bug reported by ivansanchez (#1565).
613 def test_upload_whitespace_valid
614 basic_authorization users(:public_user).email, "test"
615 changeset_id = changesets(:public_user_first_change).id
619 <modify><node id='1' lon='0' lat='0' changeset='#{changeset_id}'
621 <node id='1' lon='1' lat='1' changeset='#{changeset_id}' version='2'><tag k='k' v='v'/></node></modify>
623 <relation id='1' changeset='#{changeset_id}' version='1'><member
624 type='way' role='some' ref='3'/><member
625 type='node' role='some' ref='5'/>
626 <member type='relation' role='some' ref='3'/>
628 </modify></osmChange>
633 post :upload, :id => changeset_id
634 assert_response :success,
635 "can't upload a valid diff with whitespace variations to changeset: #{@response.body}"
637 # check the response is well-formed
638 assert_select "diffResult>node", 2
639 assert_select "diffResult>relation", 1
641 # check that the changes made it into the database
642 assert_equal 1, Node.find(1).tags.size, "node 1 should now have one tag"
643 assert_equal 0, Relation.find(1).tags.size, "relation 1 should now have no tags"
647 # upload a valid changeset which has a mixture of whitespace
648 # to check a bug reported by ivansanchez.
649 def test_upload_reuse_placeholder_valid
650 basic_authorization users(:public_user).email, "test"
651 changeset_id = changesets(:public_user_first_change).id
656 <node id='-1' lon='0' lat='0' changeset='#{changeset_id}'>
657 <tag k="foo" v="bar"/>
661 <node id='-1' lon='1' lat='1' changeset='#{changeset_id}' version='1'/>
664 <node id='-1' lon='2' lat='2' changeset='#{changeset_id}' version='2'/>
671 post :upload, :id => changeset_id
672 assert_response :success,
673 "can't upload a valid diff with re-used placeholders to changeset: #{@response.body}"
675 # check the response is well-formed
676 assert_select "diffResult>node", 3
677 assert_select "diffResult>node[old_id=-1]", 3
681 # test what happens if a diff upload re-uses placeholder IDs in an
683 def test_upload_placeholder_invalid
684 basic_authorization users(:public_user).email, "test"
685 changeset_id = changesets(:public_user_first_change).id
690 <node id='-1' lon='0' lat='0' changeset='#{changeset_id}' version='1'/>
691 <node id='-1' lon='1' lat='1' changeset='#{changeset_id}' version='1'/>
692 <node id='-1' lon='2' lat='2' changeset='#{changeset_id}' version='2'/>
699 post :upload, :id => changeset_id
700 assert_response :bad_request,
701 "shouldn't be able to re-use placeholder IDs"
705 # test that uploading a way referencing invalid placeholders gives a
706 # proper error, not a 500.
707 def test_upload_placeholder_invalid_way
708 basic_authorization users(:public_user).email, "test"
709 changeset_id = changesets(:public_user_first_change).id
714 <node id="-1" lon="0" lat="0" changeset="#{changeset_id}" version="1"/>
715 <node id="-2" lon="1" lat="1" changeset="#{changeset_id}" version="1"/>
716 <node id="-3" lon="2" lat="2" changeset="#{changeset_id}" version="1"/>
717 <way id="-1" changeset="#{changeset_id}" version="1">
729 post :upload, :id => changeset_id
730 assert_response :bad_request,
731 "shouldn't be able to use invalid placeholder IDs"
732 assert_equal "Placeholder node not found for reference -4 in way -1", @response.body
734 # the same again, but this time use an existing way
738 <node id="-1" lon="0" lat="0" changeset="#{changeset_id}" version="1"/>
739 <node id="-2" lon="1" lat="1" changeset="#{changeset_id}" version="1"/>
740 <node id="-3" lon="2" lat="2" changeset="#{changeset_id}" version="1"/>
741 <way id="1" changeset="#{changeset_id}" version="1">
753 post :upload, :id => changeset_id
754 assert_response :bad_request,
755 "shouldn't be able to use invalid placeholder IDs"
756 assert_equal "Placeholder node not found for reference -4 in way 1", @response.body
760 # test that uploading a relation referencing invalid placeholders gives a
761 # proper error, not a 500.
762 def test_upload_placeholder_invalid_relation
763 basic_authorization users(:public_user).email, "test"
764 changeset_id = changesets(:public_user_first_change).id
769 <node id="-1" lon="0" lat="0" changeset="#{changeset_id}" version="1"/>
770 <node id="-2" lon="1" lat="1" changeset="#{changeset_id}" version="1"/>
771 <node id="-3" lon="2" lat="2" changeset="#{changeset_id}" version="1"/>
772 <relation id="-1" changeset="#{changeset_id}" version="1">
773 <member type="node" role="foo" ref="-1"/>
774 <member type="node" role="foo" ref="-2"/>
775 <member type="node" role="foo" ref="-3"/>
776 <member type="node" role="foo" ref="-4"/>
784 post :upload, :id => changeset_id
785 assert_response :bad_request,
786 "shouldn't be able to use invalid placeholder IDs"
787 assert_equal "Placeholder Node not found for reference -4 in relation -1.", @response.body
789 # the same again, but this time use an existing way
793 <node id="-1" lon="0" lat="0" changeset="#{changeset_id}" version="1"/>
794 <node id="-2" lon="1" lat="1" changeset="#{changeset_id}" version="1"/>
795 <node id="-3" lon="2" lat="2" changeset="#{changeset_id}" version="1"/>
796 <relation id="1" changeset="#{changeset_id}" version="1">
797 <member type="node" role="foo" ref="-1"/>
798 <member type="node" role="foo" ref="-2"/>
799 <member type="node" role="foo" ref="-3"/>
800 <member type="way" role="bar" ref="-1"/>
808 post :upload, :id => changeset_id
809 assert_response :bad_request,
810 "shouldn't be able to use invalid placeholder IDs"
811 assert_equal "Placeholder Way not found for reference -1 in relation 1.", @response.body
815 # test what happens if a diff is uploaded containing only a node
817 def test_upload_node_move
818 basic_authorization users(:public_user).email, "test"
820 content "<osm><changeset>" +
821 "<tag k='created_by' v='osm test suite checking changesets'/>" +
824 assert_response :success
825 changeset_id = @response.body.to_i
827 old_node = current_nodes(:visible_node)
829 diff = XML::Document.new
830 diff.root = XML::Node.new "osmChange"
831 modify = XML::Node.new "modify"
832 xml_old_node = old_node.to_xml_node
833 xml_old_node["lat"] = (2.0).to_s
834 xml_old_node["lon"] = (2.0).to_s
835 xml_old_node["changeset"] = changeset_id.to_s
836 modify << xml_old_node
841 post :upload, :id => changeset_id
842 assert_response :success,
843 "diff should have uploaded OK"
846 changeset = Changeset.find(changeset_id)
847 assert_equal 1*SCALE, changeset.min_lon, "min_lon should be 1 degree"
848 assert_equal 2*SCALE, changeset.max_lon, "max_lon should be 2 degrees"
849 assert_equal 1*SCALE, changeset.min_lat, "min_lat should be 1 degree"
850 assert_equal 2*SCALE, changeset.max_lat, "max_lat should be 2 degrees"
854 # test what happens if a diff is uploaded adding a node to a way.
855 def test_upload_way_extend
856 basic_authorization users(:public_user).email, "test"
858 content "<osm><changeset>" +
859 "<tag k='created_by' v='osm test suite checking changesets'/>" +
862 assert_response :success
863 changeset_id = @response.body.to_i
865 old_way = current_ways(:visible_way)
867 diff = XML::Document.new
868 diff.root = XML::Node.new "osmChange"
869 modify = XML::Node.new "modify"
870 xml_old_way = old_way.to_xml_node
871 nd_ref = XML::Node.new "nd"
872 nd_ref["ref"] = current_nodes(:visible_node).id.to_s
873 xml_old_way << nd_ref
874 xml_old_way["changeset"] = changeset_id.to_s
875 modify << xml_old_way
880 post :upload, :id => changeset_id
881 assert_response :success,
882 "diff should have uploaded OK"
885 changeset = Changeset.find(changeset_id)
886 assert_equal 1*SCALE, changeset.min_lon, "min_lon should be 1 degree"
887 assert_equal 3*SCALE, changeset.max_lon, "max_lon should be 3 degrees"
888 assert_equal 1*SCALE, changeset.min_lat, "min_lat should be 1 degree"
889 assert_equal 3*SCALE, changeset.max_lat, "max_lat should be 3 degrees"
893 # test for more issues in #1568
894 def test_upload_empty_invalid
895 basic_authorization users(:public_user).email, "test"
898 "<osmChange></osmChange>",
899 "<osmChange><modify/></osmChange>",
900 "<osmChange><modify></modify></osmChange>"
904 post :upload, :id => changesets(:public_user_first_change).id
905 assert_response(:success, "should be able to upload " +
906 "empty changeset: " + diff)
911 # when we make some simple changes we get the same changes back from the
913 def test_diff_download_simple
914 ## First try with the normal user, which should get a forbidden
915 basic_authorization(users(:normal_user).email, "test")
917 # create a temporary changeset
918 content "<osm><changeset>" +
919 "<tag k='created_by' v='osm test suite checking changesets'/>" +
922 assert_response :forbidden
926 ## Now try with the public user
927 basic_authorization(users(:public_user).email, "test")
929 # create a temporary changeset
930 content "<osm><changeset>" +
931 "<tag k='created_by' v='osm test suite checking changesets'/>" +
934 assert_response :success
935 changeset_id = @response.body.to_i
941 <node id='1' lon='0' lat='0' changeset='#{changeset_id}' version='1'/>
942 <node id='1' lon='1' lat='0' changeset='#{changeset_id}' version='2'/>
943 <node id='1' lon='1' lat='1' changeset='#{changeset_id}' version='3'/>
944 <node id='1' lon='1' lat='2' changeset='#{changeset_id}' version='4'/>
945 <node id='1' lon='2' lat='2' changeset='#{changeset_id}' version='5'/>
946 <node id='1' lon='3' lat='2' changeset='#{changeset_id}' version='6'/>
947 <node id='1' lon='3' lat='3' changeset='#{changeset_id}' version='7'/>
948 <node id='1' lon='9' lat='9' changeset='#{changeset_id}' version='8'/>
955 post :upload, :id => changeset_id
956 assert_response :success,
957 "can't upload multiple versions of an element in a diff: #{@response.body}"
959 get :download, :id => changeset_id
960 assert_response :success
962 assert_select "osmChange", 1
963 assert_select "osmChange>modify", 8
964 assert_select "osmChange>modify>node", 8
968 # culled this from josm to ensure that nothing in the way that josm
969 # is formatting the request is causing it to fail.
971 # NOTE: the error turned out to be something else completely!
973 basic_authorization(users(:public_user).email, "test")
975 # create a temporary changeset
976 content "<osm><changeset>" +
977 "<tag k='created_by' v='osm test suite checking changesets'/>" +
980 assert_response :success
981 changeset_id = @response.body.to_i
984 <osmChange version="0.6" generator="JOSM">
985 <create version="0.6" generator="JOSM">
986 <node id='-1' visible='true' changeset='#{changeset_id}' lat='51.49619982187321' lon='-0.18722061869438314' />
987 <node id='-2' visible='true' changeset='#{changeset_id}' lat='51.496359883909605' lon='-0.18653093576241928' />
988 <node id='-3' visible='true' changeset='#{changeset_id}' lat='51.49598132358285' lon='-0.18719613290981638' />
989 <node id='-4' visible='true' changeset='#{changeset_id}' lat='51.4961591711078' lon='-0.18629015888084607' />
990 <node id='-5' visible='true' changeset='#{changeset_id}' lat='51.49582126021711' lon='-0.18708186591517145' />
991 <node id='-6' visible='true' changeset='#{changeset_id}' lat='51.49591018437858' lon='-0.1861432441734455' />
992 <node id='-7' visible='true' changeset='#{changeset_id}' lat='51.49560784152179' lon='-0.18694719410005425' />
993 <node id='-8' visible='true' changeset='#{changeset_id}' lat='51.49567389979617' lon='-0.1860289771788006' />
994 <node id='-9' visible='true' changeset='#{changeset_id}' lat='51.49543761398892' lon='-0.186820684213126' />
995 <way id='-10' action='modiy' visible='true' changeset='#{changeset_id}'>
1005 <tag k='highway' v='residential' />
1006 <tag k='name' v='Foobar Street' />
1014 post :upload, :id => changeset_id
1015 assert_response :success,
1016 "can't upload a diff from JOSM: #{@response.body}"
1018 get :download, :id => changeset_id
1019 assert_response :success
1021 assert_select "osmChange", 1
1022 assert_select "osmChange>create>node", 9
1023 assert_select "osmChange>create>way", 1
1024 assert_select "osmChange>create>way>nd", 9
1025 assert_select "osmChange>create>way>tag", 2
1029 # when we make some complex changes we get the same changes back from the
1031 def test_diff_download_complex
1032 basic_authorization(users(:public_user).email, "test")
1034 # create a temporary changeset
1035 content "<osm><changeset>" +
1036 "<tag k='created_by' v='osm test suite checking changesets'/>" +
1037 "</changeset></osm>"
1039 assert_response :success
1040 changeset_id = @response.body.to_i
1046 <node id='1' lon='0' lat='0' changeset='#{changeset_id}' version='1'/>
1049 <node id='-1' lon='9' lat='9' changeset='#{changeset_id}' version='0'/>
1050 <node id='-2' lon='8' lat='9' changeset='#{changeset_id}' version='0'/>
1051 <node id='-3' lon='7' lat='9' changeset='#{changeset_id}' version='0'/>
1054 <node id='3' lon='20' lat='15' changeset='#{changeset_id}' version='1'/>
1055 <way id='1' changeset='#{changeset_id}' version='1'>
1067 post :upload, :id => changeset_id
1068 assert_response :success,
1069 "can't upload multiple versions of an element in a diff: #{@response.body}"
1071 get :download, :id => changeset_id
1072 assert_response :success
1074 assert_select "osmChange", 1
1075 assert_select "osmChange>create", 3
1076 assert_select "osmChange>delete", 1
1077 assert_select "osmChange>modify", 2
1078 assert_select "osmChange>create>node", 3
1079 assert_select "osmChange>delete>node", 1
1080 assert_select "osmChange>modify>node", 1
1081 assert_select "osmChange>modify>way", 1
1085 # check that the bounding box of a changeset gets updated correctly
1086 ## FIXME: This should really be moded to a integration test due to the with_controller
1087 def test_changeset_bbox
1088 basic_authorization users(:public_user).email, "test"
1090 # create a new changeset
1091 content "<osm><changeset/></osm>"
1093 assert_response :success, "Creating of changeset failed."
1094 changeset_id = @response.body.to_i
1096 # add a single node to it
1097 with_controller(NodeController.new) do
1098 content "<osm><node lon='1' lat='2' changeset='#{changeset_id}'/></osm>"
1100 assert_response :success, "Couldn't create node."
1103 # get the bounding box back from the changeset
1104 get :read, :id => changeset_id
1105 assert_response :success, "Couldn't read back changeset."
1106 assert_select "osm>changeset[min_lon=1.0]", 1
1107 assert_select "osm>changeset[max_lon=1.0]", 1
1108 assert_select "osm>changeset[min_lat=2.0]", 1
1109 assert_select "osm>changeset[max_lat=2.0]", 1
1111 # add another node to it
1112 with_controller(NodeController.new) do
1113 content "<osm><node lon='2' lat='1' changeset='#{changeset_id}'/></osm>"
1115 assert_response :success, "Couldn't create second node."
1118 # get the bounding box back from the changeset
1119 get :read, :id => changeset_id
1120 assert_response :success, "Couldn't read back changeset for the second time."
1121 assert_select "osm>changeset[min_lon=1.0]", 1
1122 assert_select "osm>changeset[max_lon=2.0]", 1
1123 assert_select "osm>changeset[min_lat=1.0]", 1
1124 assert_select "osm>changeset[max_lat=2.0]", 1
1126 # add (delete) a way to it, which contains a point at (3,3)
1127 with_controller(WayController.new) do
1128 content update_changeset(current_ways(:visible_way).to_xml,
1130 put :delete, :id => current_ways(:visible_way).id
1131 assert_response :success, "Couldn't delete a way."
1134 # get the bounding box back from the changeset
1135 get :read, :id => changeset_id
1136 assert_response :success, "Couldn't read back changeset for the third time."
1137 # note that the 3.1 here is because of the bbox overexpansion
1138 assert_select "osm>changeset[min_lon=1.0]", 1
1139 assert_select "osm>changeset[max_lon=3.1]", 1
1140 assert_select "osm>changeset[min_lat=1.0]", 1
1141 assert_select "osm>changeset[max_lat=3.1]", 1
1145 # test that the changeset :include method works as it should
1146 def test_changeset_include
1147 basic_authorization users(:public_user).display_name, "test"
1149 # create a new changeset
1150 content "<osm><changeset/></osm>"
1152 assert_response :success, "Creating of changeset failed."
1153 changeset_id = @response.body.to_i
1155 # NOTE: the include method doesn't over-expand, like inserting
1156 # a real method does. this is because we expect the client to
1157 # know what it is doing!
1158 check_after_include(changeset_id, 1, 1, [ 1, 1, 1, 1])
1159 check_after_include(changeset_id, 3, 3, [ 1, 1, 3, 3])
1160 check_after_include(changeset_id, 4, 2, [ 1, 1, 4, 3])
1161 check_after_include(changeset_id, 2, 2, [ 1, 1, 4, 3])
1162 check_after_include(changeset_id, -1, -1, [-1, -1, 4, 3])
1163 check_after_include(changeset_id, -2, 5, [-2, -1, 4, 5])
1167 # test the query functionality of changesets
1169 get :query, :bbox => "-10,-10, 10, 10"
1170 assert_response :success, "can't get changesets in bbox"
1171 assert_changesets [1,4,6]
1173 get :query, :bbox => "4.5,4.5,4.6,4.6"
1174 assert_response :success, "can't get changesets in bbox"
1175 assert_changesets [1]
1177 # can't get changesets of user 1 without authenticating
1178 get :query, :user => users(:normal_user).id
1179 assert_response :not_found, "shouldn't be able to get changesets by non-public user"
1181 # but this should work
1182 basic_authorization "test@openstreetmap.org", "test"
1183 get :query, :user => users(:normal_user).id
1184 assert_response :success, "can't get changesets by user"
1185 assert_changesets [1,3,6]
1187 get :query, :user => users(:normal_user).id, :open => true
1188 assert_response :success, "can't get changesets by user and open"
1189 assert_changesets [1]
1191 get :query, :time => '2007-12-31'
1192 assert_response :success, "can't get changesets by time-since"
1193 assert_changesets [1,2,4,5,6]
1195 get :query, :time => '2008-01-01T12:34Z'
1196 assert_response :success, "can't get changesets by time-since with hour"
1197 assert_changesets [1,2,4,5,6]
1199 get :query, :time => '2007-12-31T23:59Z,2008-01-01T00:01Z'
1200 assert_response :success, "can't get changesets by time-range"
1201 assert_changesets [1,4,5,6]
1203 get :query, :open => 'true'
1204 assert_response :success, "can't get changesets by open-ness"
1205 assert_changesets [1,2,4]
1209 # check that errors are returned if garbage is inserted
1210 # into query strings
1211 def test_query_invalid
1214 ";drop table users;"
1216 get :query, :bbox => bbox
1217 assert_response :bad_request, "'#{bbox}' isn't a bbox"
1222 ";drop table users;",
1226 get :query, :time => time
1227 assert_response :bad_request, "'#{time}' isn't a valid time range"
1235 get :query, :user => uid
1236 assert_response :bad_request, "'#{uid}' isn't a valid user ID"
1241 # check updating tags on a changeset
1242 def test_changeset_update
1243 ## First try with the non-public user
1244 changeset = changesets(:normal_user_first_change)
1245 new_changeset = changeset.to_xml
1246 new_tag = XML::Node.new "tag"
1247 new_tag['k'] = "tagtesting"
1248 new_tag['v'] = "valuetesting"
1249 new_changeset.find("//osm/changeset").first << new_tag
1250 content new_changeset
1252 # try without any authorization
1253 put :update, :id => changeset.id
1254 assert_response :unauthorized
1256 # try with the wrong authorization
1257 basic_authorization users(:public_user).email, "test"
1258 put :update, :id => changeset.id
1259 assert_response :conflict
1261 # now this should get an unauthorized
1262 basic_authorization users(:normal_user).email, "test"
1263 put :update, :id => changeset.id
1264 assert_require_public_data "user with their data non-public, shouldn't be able to edit their changeset"
1267 ## Now try with the public user
1268 changeset = changesets(:public_user_first_change)
1269 new_changeset = changeset.to_xml
1270 new_tag = XML::Node.new "tag"
1271 new_tag['k'] = "tagtesting"
1272 new_tag['v'] = "valuetesting"
1273 new_changeset.find("//osm/changeset").first << new_tag
1274 content new_changeset
1276 # try without any authorization
1277 @request.env["HTTP_AUTHORIZATION"] = nil
1278 put :update, :id => changeset.id
1279 assert_response :unauthorized
1281 # try with the wrong authorization
1282 basic_authorization users(:second_public_user).email, "test"
1283 put :update, :id => changeset.id
1284 assert_response :conflict
1286 # now this should work...
1287 basic_authorization users(:public_user).email, "test"
1288 put :update, :id => changeset.id
1289 assert_response :success
1291 assert_select "osm>changeset[id=#{changeset.id}]", 1
1292 assert_select "osm>changeset>tag", 2
1293 assert_select "osm>changeset>tag[k=tagtesting][v=valuetesting]", 1
1297 # check that a user different from the one who opened the changeset
1299 def test_changeset_update_invalid
1300 basic_authorization users(:public_user).email, "test"
1302 changeset = changesets(:normal_user_first_change)
1303 new_changeset = changeset.to_xml
1304 new_tag = XML::Node.new "tag"
1305 new_tag['k'] = "testing"
1306 new_tag['v'] = "testing"
1307 new_changeset.find("//osm/changeset").first << new_tag
1309 content new_changeset
1310 put :update, :id => changeset.id
1311 assert_response :conflict
1315 # check that a changeset can contain a certain max number of changes.
1316 ## FIXME should be changed to an integration test due to the with_controller
1317 def test_changeset_limits
1318 basic_authorization users(:public_user).email, "test"
1320 # open a new changeset
1321 content "<osm><changeset/></osm>"
1323 assert_response :success, "can't create a new changeset"
1324 cs_id = @response.body.to_i
1326 # start the counter just short of where the changeset should finish.
1328 # alter the database to set the counter on the changeset directly,
1329 # otherwise it takes about 6 minutes to fill all of them.
1330 changeset = Changeset.find(cs_id)
1331 changeset.num_changes = Changeset::MAX_ELEMENTS - offset
1334 with_controller(NodeController.new) do
1336 content "<osm><node changeset='#{cs_id}' lat='0.0' lon='0.0'/></osm>"
1338 assert_response :success, "can't create a new node"
1339 node_id = @response.body.to_i
1341 get :read, :id => node_id
1342 assert_response :success, "can't read back new node"
1343 node_doc = XML::Parser.string(@response.body).parse
1344 node_xml = node_doc.find("//osm/node").first
1346 # loop until we fill the changeset with nodes
1348 node_xml['lat'] = rand.to_s
1349 node_xml['lon'] = rand.to_s
1350 node_xml['version'] = (i+1).to_s
1353 put :update, :id => node_id
1354 assert_response :success, "attempt #{i} should have succeeded"
1357 # trying again should fail
1358 node_xml['lat'] = rand.to_s
1359 node_xml['lon'] = rand.to_s
1360 node_xml['version'] = offset.to_s
1363 put :update, :id => node_id
1364 assert_response :conflict, "final attempt should have failed"
1367 changeset = Changeset.find(cs_id)
1368 assert_equal Changeset::MAX_ELEMENTS + 1, changeset.num_changes
1370 # check that the changeset is now closed as well
1371 assert(!changeset.is_open?,
1372 "changeset should have been auto-closed by exceeding " +
1376 # This should display the last 20 changesets closed.
1378 @changesets = Changeset.find(:all, :order => "created_at DESC", :conditions => ['min_lat IS NOT NULL'], :limit=> 20)
1379 assert @changesets.size <= 20
1381 assert_response :success
1382 assert_template "list"
1383 # Now check that all 20 (or however many were returned) changesets are in the html
1384 assert_select "h1", :text => "Recent Changes", :count => 1
1385 assert_select "table[id='keyvalue'] tr", :count => @changesets.size + 1
1386 @changesets.each do |changeset|
1387 # FIXME this test needs rewriting - test for table contents
1391 #------------------------------------------------------------
1393 #------------------------------------------------------------
1396 # boilerplate for checking that certain changesets exist in the
1398 def assert_changesets(ids)
1399 assert_select "osm>changeset", ids.size
1401 assert_select "osm>changeset[id=#{id}]", 1
1406 # call the include method and assert properties of the bbox
1407 def check_after_include(changeset_id, lon, lat, bbox)
1408 content "<osm><node lon='#{lon}' lat='#{lat}'/></osm>"
1409 post :expand_bbox, :id => changeset_id
1410 assert_response :success, "Setting include of changeset failed: #{@response.body}"
1412 # check exactly one changeset
1413 assert_select "osm>changeset", 1
1414 assert_select "osm>changeset[id=#{changeset_id}]", 1
1417 doc = XML::Parser.string(@response.body).parse
1418 changeset = doc.find("//osm/changeset").first
1419 assert_equal bbox[0], changeset['min_lon'].to_f, "min lon"
1420 assert_equal bbox[1], changeset['min_lat'].to_f, "min lat"
1421 assert_equal bbox[2], changeset['max_lon'].to_f, "max lon"
1422 assert_equal bbox[3], changeset['max_lat'].to_f, "max lat"
1426 # update the changeset_id of a way element
1427 def update_changeset(xml, changeset_id)
1428 xml_attr_rewrite(xml, 'changeset', changeset_id)
1432 # update an attribute in a way element
1433 def xml_attr_rewrite(xml, name, value)
1434 xml.find("//osm/way").first[name] = value.to_s