2 require 'changeset_controller'
4 class ChangesetControllerTest < ActionController::TestCase
6 fixtures :changeset_comments
9 # test all routes which lead to this controller
12 { :path => "/api/0.6/changeset/create", :method => :put },
13 { :controller => "changeset", :action => "create" }
16 { :path => "/api/0.6/changeset/1/upload", :method => :post },
17 { :controller => "changeset", :action => "upload", :id => "1" }
20 { :path => "/api/0.6/changeset/1/download", :method => :get },
21 { :controller => "changeset", :action => "download", :id => "1" }
24 { :path => "/api/0.6/changeset/1/expand_bbox", :method => :post },
25 { :controller => "changeset", :action => "expand_bbox", :id => "1" }
28 { :path => "/api/0.6/changeset/1", :method => :get },
29 { :controller => "changeset", :action => "read", :id => "1" }
32 { :path => "/api/0.6/changeset/1", :method => :put },
33 { :controller => "changeset", :action => "update", :id => "1" }
36 { :path => "/api/0.6/changeset/1/close", :method => :put },
37 { :controller => "changeset", :action => "close", :id => "1" }
40 { :path => "/api/0.6/changeset/1/subscribe", :method => :post },
41 { :controller => "changeset", :action => "subscribe", :id => "1" }
44 { :path => "/api/0.6/changeset/1/unsubscribe", :method => :post },
45 { :controller => "changeset", :action => "unsubscribe", :id => "1" }
48 { :path => "/api/0.6/changeset/comment/1/hide", :method => :post },
49 { :controller => "changeset", :action => "hide_comment", :id => "1" }
52 { :path => "/api/0.6/changeset/comment/1/unhide", :method => :post },
53 { :controller => "changeset", :action => "unhide_comment", :id => "1" }
56 { :path => "/api/0.6/changeset/comments/feed", :method => :get },
57 { :controller => "changeset", :action => "comments_feed", :format =>"rss" }
60 { :path => "/api/0.6/changeset/1/comments/feed", :method => :get },
61 { :controller => "changeset", :action => "comments_feed", :id => "1", :format =>"rss" }
64 { :path => "/api/0.6/changesets", :method => :get },
65 { :controller => "changeset", :action => "query" }
68 { :path => "/user/name/history", :method => :get },
69 { :controller => "changeset", :action => "list", :display_name => "name" }
72 { :path => "/user/name/history/feed", :method => :get },
73 { :controller => "changeset", :action => "feed", :display_name => "name", :format => :atom }
76 { :path => "/history/friends", :method => :get },
77 { :controller => "changeset", :action => "list", :friends => true }
80 { :path => "/history/nearby", :method => :get },
81 { :controller => "changeset", :action => "list", :nearby => true }
84 { :path => "/history", :method => :get },
85 { :controller => "changeset", :action => "list" }
88 { :path => "/history/feed", :method => :get },
89 { :controller => "changeset", :action => "feed", :format => :atom }
93 # -----------------------
94 # Test simple changeset creation
95 # -----------------------
98 basic_authorization users(:normal_user).email, "test"
99 # Create the first user's changeset
100 content "<osm><changeset>" +
101 "<tag k='created_by' v='osm test suite checking changesets'/>" +
104 assert_require_public_data
107 basic_authorization users(:public_user).email, "test"
108 # Create the first user's changeset
109 content "<osm><changeset>" +
110 "<tag k='created_by' v='osm test suite checking changesets'/>" +
114 assert_response :success, "Creation of changeset did not return sucess status"
115 newid = @response.body.to_i
117 # check end time, should be an hour ahead of creation time
118 cs = Changeset.find(newid)
119 duration = cs.closed_at - cs.created_at
120 # the difference can either be a rational, or a floating point number
121 # of seconds, depending on the code path taken :-(
122 if duration.class == Rational
123 assert_equal Rational(1,24), duration , "initial idle timeout should be an hour (#{cs.created_at} -> #{cs.closed_at})"
125 # must be number of seconds...
126 assert_equal 3600, duration.round, "initial idle timeout should be an hour (#{cs.created_at} -> #{cs.closed_at})"
129 # checks if uploader was subscribed
130 assert_equal 1, cs.subscribers.length
133 def test_create_invalid
134 basic_authorization users(:normal_user).email, "test"
135 content "<osm><changeset></osm>"
137 assert_require_public_data
139 ## Try the public user
140 basic_authorization users(:public_user).email, "test"
141 content "<osm><changeset></osm>"
143 assert_response :bad_request, "creating a invalid changeset should fail"
146 def test_create_invalid_no_content
147 ## First check with no auth
149 assert_response :unauthorized, "shouldn't be able to create a changeset with no auth"
151 ## Now try to with the non-public user
152 basic_authorization users(:normal_user).email, "test"
154 assert_require_public_data
156 ## Try the inactive user
157 basic_authorization users(:inactive_user).email, "test"
161 ## Now try to use the public user
162 basic_authorization users(:public_user).email, "test"
164 assert_response :bad_request, "creating a changeset with no content should fail"
167 def test_create_wrong_method
168 basic_authorization users(:public_user).email, "test"
170 assert_response :method_not_allowed
172 assert_response :method_not_allowed
176 # check that the changeset can be read and returns the correct
177 # document structure.
179 changeset_id = changesets(:normal_user_first_change).id
180 get :read, :id => changeset_id, :format => :xml
181 assert_response :success, "cannot get first changeset"
183 assert_select "osm[version=#{API_VERSION}][generator=\"OpenStreetMap server\"]", 1
184 assert_select "osm>changeset[id=#{changeset_id}]", 1
185 assert_select "osm>changeset>discussion", 0
187 get :read, :id => changeset_id, :format => :xml, :include_discussion => true
188 assert_select "osm>changeset>discussion", 1
192 # check that a changeset that doesn't exist returns an appropriate message
193 def test_read_not_found
194 [0, -32, 233455644, "afg", "213"].each do |id|
197 assert_response :not_found, "should get a not found"
198 rescue ActionController::UrlGenerationError => ex
199 assert_match /No route matches/, ex.to_s
205 # test that the user who opened a change can close it
207 ## Try without authentication
208 put :close, :id => changesets(:public_user_first_change).id
209 assert_response :unauthorized
212 ## Try using the non-public user
213 basic_authorization users(:normal_user).email, "test"
214 put :close, :id => changesets(:normal_user_first_change).id
215 assert_require_public_data
218 ## The try with the public user
219 basic_authorization users(:public_user).email, "test"
221 cs_id = changesets(:public_user_first_change).id
222 put :close, :id => cs_id
223 assert_response :success
225 # test that it really is closed now
226 cs = Changeset.find(cs_id)
228 "changeset should be closed now (#{cs.closed_at} > #{Time.now.getutc}.")
232 # test that a different user can't close another user's changeset
233 def test_close_invalid
234 basic_authorization users(:public_user).email, "test"
236 put :close, :id => changesets(:normal_user_first_change).id
237 assert_response :conflict
238 assert_equal "The user doesn't own that changeset", @response.body
242 # test that you can't close using another method
243 def test_close_method_invalid
244 basic_authorization users(:public_user).email, "test"
246 cs_id = changesets(:public_user_first_change).id
247 get :close, :id => cs_id
248 assert_response :method_not_allowed
250 post :close, :id => cs_id
251 assert_response :method_not_allowed
255 # check that you can't close a changeset that isn't found
256 def test_close_not_found
257 cs_ids = [0, -132, "123"]
259 # First try to do it with no auth
262 put :close, :id => id
263 assert_response :unauthorized, "Shouldn't be able close the non-existant changeset #{id}, when not authorized"
264 rescue ActionController::UrlGenerationError => ex
265 assert_match /No route matches/, ex.to_s
270 basic_authorization users(:public_user).email, "test"
273 put :close, :id => id
274 assert_response :not_found, "The changeset #{id} doesn't exist, so can't be closed"
275 rescue ActionController::UrlGenerationError => ex
276 assert_match /No route matches/, ex.to_s
282 # upload something simple, but valid and check that it can
284 # Also try without auth and another user.
285 def test_upload_simple_valid
287 changeset_id = changesets(:public_user_first_change).id
289 # simple diff to change a node, way and relation by removing
294 <node id='1' lon='0' lat='0' changeset='#{changeset_id}' version='1'/>
295 <way id='1' changeset='#{changeset_id}' version='1'>
300 <relation id='1' changeset='#{changeset_id}' version='1'>
301 <member type='way' role='some' ref='3'/>
302 <member type='node' role='some' ref='5'/>
303 <member type='relation' role='some' ref='3'/>
311 post :upload, :id => changeset_id
312 assert_response :unauthorized,
313 "shouldnn't be able to upload a simple valid diff to changeset: #{@response.body}"
317 ## Now try with a private user
318 basic_authorization users(:normal_user).email, "test"
319 changeset_id = changesets(:normal_user_first_change).id
321 # simple diff to change a node, way and relation by removing
326 <node id='1' lon='0' lat='0' changeset='#{changeset_id}' version='1'/>
327 <way id='1' changeset='#{changeset_id}' version='1'>
332 <relation id='1' changeset='#{changeset_id}' version='1'>
333 <member type='way' role='some' ref='3'/>
334 <member type='node' role='some' ref='5'/>
335 <member type='relation' role='some' ref='3'/>
343 post :upload, :id => changeset_id
344 assert_response :forbidden,
345 "can't upload a simple valid diff to changeset: #{@response.body}"
349 ## Now try with the public user
350 basic_authorization users(:public_user).email, "test"
351 changeset_id = changesets(:public_user_first_change).id
353 # simple diff to change a node, way and relation by removing
358 <node id='1' lon='0' lat='0' changeset='#{changeset_id}' version='1'/>
359 <way id='1' changeset='#{changeset_id}' version='1'>
364 <relation id='1' changeset='#{changeset_id}' version='1'>
365 <member type='way' role='some' ref='3'/>
366 <member type='node' role='some' ref='5'/>
367 <member type='relation' role='some' ref='3'/>
375 post :upload, :id => changeset_id
376 assert_response :success,
377 "can't upload a simple valid diff to changeset: #{@response.body}"
379 # check that the changes made it into the database
380 assert_equal 0, Node.find(1).tags.size, "node 1 should now have no tags"
381 assert_equal 0, Way.find(1).tags.size, "way 1 should now have no tags"
382 assert_equal 0, Relation.find(1).tags.size, "relation 1 should now have no tags"
386 # upload something which creates new objects using placeholders
387 def test_upload_create_valid
388 basic_authorization users(:public_user).email, "test"
389 cs_id = changesets(:public_user_first_change).id
391 # simple diff to create a node way and relation using placeholders
395 <node id='-1' lon='0' lat='0' changeset='#{cs_id}'>
396 <tag k='foo' v='bar'/>
397 <tag k='baz' v='bat'/>
399 <way id='-1' changeset='#{cs_id}'>
404 <relation id='-1' changeset='#{cs_id}'>
405 <member type='way' role='some' ref='3'/>
406 <member type='node' role='some' ref='5'/>
407 <member type='relation' role='some' ref='3'/>
415 post :upload, :id => cs_id
416 assert_response :success,
417 "can't upload a simple valid creation to changeset: #{@response.body}"
419 # check the returned payload
420 assert_select "diffResult[version=#{API_VERSION}][generator=\"OpenStreetMap server\"]", 1
421 assert_select "diffResult>node", 1
422 assert_select "diffresult>way", 1
423 assert_select "diffResult>relation", 1
425 # inspect the response to find out what the new element IDs are
426 doc = XML::Parser.string(@response.body).parse
427 new_node_id = doc.find("//diffResult/node").first["new_id"].to_i
428 new_way_id = doc.find("//diffResult/way").first["new_id"].to_i
429 new_rel_id = doc.find("//diffResult/relation").first["new_id"].to_i
431 # check the old IDs are all present and negative one
432 assert_equal -1, doc.find("//diffResult/node").first["old_id"].to_i
433 assert_equal -1, doc.find("//diffResult/way").first["old_id"].to_i
434 assert_equal -1, doc.find("//diffResult/relation").first["old_id"].to_i
436 # check the versions are present and equal one
437 assert_equal 1, doc.find("//diffResult/node").first["new_version"].to_i
438 assert_equal 1, doc.find("//diffResult/way").first["new_version"].to_i
439 assert_equal 1, doc.find("//diffResult/relation").first["new_version"].to_i
441 # check that the changes made it into the database
442 assert_equal 2, Node.find(new_node_id).tags.size, "new node should have two tags"
443 assert_equal 0, Way.find(new_way_id).tags.size, "new way should have no tags"
444 assert_equal 0, Relation.find(new_rel_id).tags.size, "new relation should have no tags"
448 # test a complex delete where we delete elements which rely on eachother
449 # in the same transaction.
450 def test_upload_delete
451 basic_authorization users(:public_user).display_name, "test"
453 diff = XML::Document.new
454 diff.root = XML::Node.new "osmChange"
455 delete = XML::Node.new "delete"
457 delete << current_relations(:visible_relation).to_xml_node
458 delete << current_relations(:used_relation).to_xml_node
459 delete << current_ways(:used_way).to_xml_node
460 delete << current_nodes(:node_used_by_relationship).to_xml_node
462 # update the changeset to one that this user owns
463 changeset_id = changesets(:public_user_first_change).id
464 ["node", "way", "relation"].each do |type|
465 delete.find("//osmChange/delete/#{type}").each do |n|
466 n['changeset'] = changeset_id.to_s
472 post :upload, :id => changeset_id
473 assert_response :success,
474 "can't upload a deletion diff to changeset: #{@response.body}"
476 # check the response is well-formed
477 assert_select "diffResult>node", 1
478 assert_select "diffResult>way", 1
479 assert_select "diffResult>relation", 2
481 # check that everything was deleted
482 assert_equal false, Node.find(current_nodes(:node_used_by_relationship).id).visible
483 assert_equal false, Way.find(current_ways(:used_way).id).visible
484 assert_equal false, Relation.find(current_relations(:visible_relation).id).visible
485 assert_equal false, Relation.find(current_relations(:used_relation).id).visible
489 # test uploading a delete with no lat/lon, as they are optional in
490 # the osmChange spec.
491 def test_upload_nolatlon_delete
492 basic_authorization users(:public_user).display_name, "test"
494 node = current_nodes(:public_visible_node)
495 cs = changesets(:public_user_first_change)
496 diff = "<osmChange><delete><node id='#{node.id}' version='#{node.version}' changeset='#{cs.id}'/></delete></osmChange>"
500 post :upload, :id => cs.id
501 assert_response :success,
502 "can't upload a deletion diff to changeset: #{@response.body}"
504 # check the response is well-formed
505 assert_select "diffResult>node", 1
507 # check that everything was deleted
508 assert_equal false, Node.find(node.id).visible
511 def test_repeated_changeset_create
513 basic_authorization users(:public_user).email, "test"
515 # create a temporary changeset
516 content "<osm><changeset>" +
517 "<tag k='created_by' v='osm test suite checking changesets'/>" +
519 assert_difference('Changeset.count', 1) do
522 assert_response :success
523 changeset_id = @response.body.to_i
527 def test_upload_large_changeset
528 basic_authorization users(:public_user).email, "test"
531 content "<osm><changeset/></osm>"
533 assert_response :success, "Should be able to create a changeset: #{@response.body}"
534 changeset_id = @response.body.to_i
536 # upload some widely-spaced nodes, spiralling positive and negative to cause
537 # largest bbox over-expansion possible.
541 <node id='-1' lon='-20' lat='-10' changeset='#{changeset_id}'/>
542 <node id='-10' lon='20' lat='10' changeset='#{changeset_id}'/>
543 <node id='-2' lon='-40' lat='-20' changeset='#{changeset_id}'/>
544 <node id='-11' lon='40' lat='20' changeset='#{changeset_id}'/>
545 <node id='-3' lon='-60' lat='-30' changeset='#{changeset_id}'/>
546 <node id='-12' lon='60' lat='30' changeset='#{changeset_id}'/>
547 <node id='-4' lon='-80' lat='-40' changeset='#{changeset_id}'/>
548 <node id='-13' lon='80' lat='40' changeset='#{changeset_id}'/>
549 <node id='-5' lon='-100' lat='-50' changeset='#{changeset_id}'/>
550 <node id='-14' lon='100' lat='50' changeset='#{changeset_id}'/>
551 <node id='-6' lon='-120' lat='-60' changeset='#{changeset_id}'/>
552 <node id='-15' lon='120' lat='60' changeset='#{changeset_id}'/>
553 <node id='-7' lon='-140' lat='-70' changeset='#{changeset_id}'/>
554 <node id='-16' lon='140' lat='70' changeset='#{changeset_id}'/>
555 <node id='-8' lon='-160' lat='-80' changeset='#{changeset_id}'/>
556 <node id='-17' lon='160' lat='80' changeset='#{changeset_id}'/>
557 <node id='-9' lon='-179.9' lat='-89.9' changeset='#{changeset_id}'/>
558 <node id='-18' lon='179.9' lat='89.9' changeset='#{changeset_id}'/>
563 # upload it, which used to cause an error like "PGError: ERROR:
564 # integer out of range" (bug #2152). but shouldn't any more.
566 post :upload, :id => changeset_id
567 assert_response :success,
568 "can't upload a spatially-large diff to changeset: #{@response.body}"
570 # check that the changeset bbox is within bounds
571 cs = Changeset.find(changeset_id)
572 assert cs.min_lon >= -180 * GeoRecord::SCALE, "Minimum longitude (#{cs.min_lon / GeoRecord::SCALE}) should be >= -180 to be valid."
573 assert cs.max_lon <= 180 * GeoRecord::SCALE, "Maximum longitude (#{cs.max_lon / GeoRecord::SCALE}) should be <= 180 to be valid."
574 assert cs.min_lat >= -90 * GeoRecord::SCALE, "Minimum latitude (#{cs.min_lat / GeoRecord::SCALE}) should be >= -90 to be valid."
575 assert cs.max_lat >= 90 * GeoRecord::SCALE, "Maximum latitude (#{cs.max_lat / GeoRecord::SCALE}) should be <= 90 to be valid."
579 # test that deleting stuff in a transaction doesn't bypass the checks
580 # to ensure that used elements are not deleted.
581 def test_upload_delete_invalid
582 basic_authorization users(:public_user).email, "test"
584 diff = XML::Document.new
585 diff.root = XML::Node.new "osmChange"
586 delete = XML::Node.new "delete"
588 delete << current_relations(:public_visible_relation).to_xml_node
589 delete << current_ways(:used_way).to_xml_node
590 delete << current_nodes(:node_used_by_relationship).to_xml_node
594 post :upload, :id => 2
595 assert_response :precondition_failed,
596 "shouldn't be able to upload a invalid deletion diff: #{@response.body}"
597 assert_equal "Precondition failed: Way 3 is still used by relations 1.", @response.body
599 # check that nothing was, in fact, deleted
600 assert_equal true, Node.find(current_nodes(:node_used_by_relationship).id).visible
601 assert_equal true, Way.find(current_ways(:used_way).id).visible
602 assert_equal true, Relation.find(current_relations(:visible_relation).id).visible
606 # test that a conditional delete of an in use object works.
607 def test_upload_delete_if_unused
608 basic_authorization users(:public_user).email, "test"
610 diff = XML::Document.new
611 diff.root = XML::Node.new "osmChange"
612 delete = XML::Node.new "delete"
614 delete["if-unused"] = ""
615 delete << current_relations(:public_used_relation).to_xml_node
616 delete << current_ways(:used_way).to_xml_node
617 delete << current_nodes(:node_used_by_relationship).to_xml_node
621 post :upload, :id => 2
622 assert_response :success,
623 "can't do a conditional delete of in use objects: #{@response.body}"
625 # check the returned payload
626 assert_select "diffResult[version=#{API_VERSION}][generator=\"OpenStreetMap server\"]", 1
627 assert_select "diffResult>node", 1
628 assert_select "diffresult>way", 1
629 assert_select "diffResult>relation", 1
632 doc = XML::Parser.string(@response.body).parse
634 # check the old IDs are all present and what we expect
635 assert_equal current_nodes(:node_used_by_relationship).id, doc.find("//diffResult/node").first["old_id"].to_i
636 assert_equal current_ways(:used_way).id, doc.find("//diffResult/way").first["old_id"].to_i
637 assert_equal current_relations(:public_used_relation).id, doc.find("//diffResult/relation").first["old_id"].to_i
639 # check the new IDs are all present and unchanged
640 assert_equal current_nodes(:node_used_by_relationship).id, doc.find("//diffResult/node").first["new_id"].to_i
641 assert_equal current_ways(:used_way).id, doc.find("//diffResult/way").first["new_id"].to_i
642 assert_equal current_relations(:public_used_relation).id, doc.find("//diffResult/relation").first["new_id"].to_i
644 # check the new versions are all present and unchanged
645 assert_equal current_nodes(:node_used_by_relationship).version, doc.find("//diffResult/node").first["new_version"].to_i
646 assert_equal current_ways(:used_way).version, doc.find("//diffResult/way").first["new_version"].to_i
647 assert_equal current_relations(:public_used_relation).version, doc.find("//diffResult/relation").first["new_version"].to_i
649 # check that nothing was, in fact, deleted
650 assert_equal true, Node.find(current_nodes(:node_used_by_relationship).id).visible
651 assert_equal true, Way.find(current_ways(:used_way).id).visible
652 assert_equal true, Relation.find(current_relations(:public_used_relation).id).visible
656 # upload an element with a really long tag value
657 def test_upload_invalid_too_long_tag
658 basic_authorization users(:public_user).email, "test"
659 cs_id = changesets(:public_user_first_change).id
661 # simple diff to create a node way and relation using placeholders
665 <node id='-1' lon='0' lat='0' changeset='#{cs_id}'>
666 <tag k='foo' v='#{"x"*256}'/>
674 post :upload, :id => cs_id
675 assert_response :bad_request,
676 "shoudln't be able to upload too long a tag to changeset: #{@response.body}"
681 # upload something which creates new objects and inserts them into
682 # existing containers using placeholders.
683 def test_upload_complex
684 basic_authorization users(:public_user).email, "test"
685 cs_id = changesets(:public_user_first_change).id
687 # simple diff to create a node way and relation using placeholders
691 <node id='-1' lon='0' lat='0' changeset='#{cs_id}'>
692 <tag k='foo' v='bar'/>
693 <tag k='baz' v='bat'/>
697 <way id='1' changeset='#{cs_id}' version='1'>
701 <relation id='1' changeset='#{cs_id}' version='1'>
702 <member type='way' role='some' ref='3'/>
703 <member type='node' role='some' ref='-1'/>
704 <member type='relation' role='some' ref='3'/>
712 post :upload, :id => cs_id
713 assert_response :success,
714 "can't upload a complex diff to changeset: #{@response.body}"
716 # check the returned payload
717 assert_select "diffResult[version=#{API_VERSION}][generator=\"#{GENERATOR}\"]", 1
718 assert_select "diffResult>node", 1
719 assert_select "diffResult>way", 1
720 assert_select "diffResult>relation", 1
722 # inspect the response to find out what the new element IDs are
723 doc = XML::Parser.string(@response.body).parse
724 new_node_id = doc.find("//diffResult/node").first["new_id"].to_i
726 # check that the changes made it into the database
727 assert_equal 2, Node.find(new_node_id).tags.size, "new node should have two tags"
728 assert_equal [new_node_id, 3], Way.find(1).nds, "way nodes should match"
729 Relation.find(1).members.each do |type,id,role|
731 assert_equal new_node_id, id, "relation should contain new node"
737 # create a diff which references several changesets, which should cause
738 # a rollback and none of the diff gets committed
739 def test_upload_invalid_changesets
740 basic_authorization users(:public_user).email, "test"
741 cs_id = changesets(:public_user_first_change).id
743 # simple diff to create a node way and relation using placeholders
747 <node id='1' lon='0' lat='0' changeset='#{cs_id}' version='1'/>
748 <way id='1' changeset='#{cs_id}' version='1'>
753 <relation id='1' changeset='#{cs_id}' version='1'>
754 <member type='way' role='some' ref='3'/>
755 <member type='node' role='some' ref='5'/>
756 <member type='relation' role='some' ref='3'/>
760 <node id='-1' lon='0' lat='0' changeset='4'>
761 <tag k='foo' v='bar'/>
762 <tag k='baz' v='bat'/>
767 # cache the objects before uploading them
768 node = current_nodes(:visible_node)
769 way = current_ways(:visible_way)
770 rel = current_relations(:visible_relation)
774 post :upload, :id => cs_id
775 assert_response :conflict,
776 "uploading a diff with multiple changsets should have failed"
778 # check that objects are unmodified
779 assert_nodes_are_equal(node, Node.find(1))
780 assert_ways_are_equal(way, Way.find(1))
784 # upload multiple versions of the same element in the same diff.
785 def test_upload_multiple_valid
786 basic_authorization users(:public_user).email, "test"
787 cs_id = changesets(:public_user_first_change).id
789 # change the location of a node multiple times, each time referencing
790 # the last version. doesn't this depend on version numbers being
795 <node id='1' lon='0' lat='0' changeset='#{cs_id}' version='1'/>
796 <node id='1' lon='1' lat='0' changeset='#{cs_id}' version='2'/>
797 <node id='1' lon='1' lat='1' changeset='#{cs_id}' version='3'/>
798 <node id='1' lon='1' lat='2' changeset='#{cs_id}' version='4'/>
799 <node id='1' lon='2' lat='2' changeset='#{cs_id}' version='5'/>
800 <node id='1' lon='3' lat='2' changeset='#{cs_id}' version='6'/>
801 <node id='1' lon='3' lat='3' changeset='#{cs_id}' version='7'/>
802 <node id='1' lon='9' lat='9' changeset='#{cs_id}' version='8'/>
809 post :upload, :id => cs_id
810 assert_response :success,
811 "can't upload multiple versions of an element in a diff: #{@response.body}"
813 # check the response is well-formed. its counter-intuitive, but the
814 # API will return multiple elements with the same ID and different
815 # version numbers for each change we made.
816 assert_select "diffResult>node", 8
820 # upload multiple versions of the same element in the same diff, but
821 # keep the version numbers the same.
822 def test_upload_multiple_duplicate
823 basic_authorization users(:public_user).email, "test"
824 cs_id = changesets(:public_user_first_change).id
829 <node id='1' lon='0' lat='0' changeset='#{cs_id}' version='1'/>
830 <node id='1' lon='1' lat='1' changeset='#{cs_id}' version='1'/>
837 post :upload, :id => cs_id
838 assert_response :conflict,
839 "shouldn't be able to upload the same element twice in a diff: #{@response.body}"
843 # try to upload some elements without specifying the version
844 def test_upload_missing_version
845 basic_authorization users(:public_user).email, "test"
846 cs_id = changesets(:public_user_first_change).id
851 <node id='1' lon='1' lat='1' changeset='cs_id'/>
858 post :upload, :id => cs_id
859 assert_response :bad_request,
860 "shouldn't be able to upload an element without version: #{@response.body}"
864 # try to upload with commands other than create, modify, or delete
865 def test_action_upload_invalid
866 basic_authorization users(:public_user).email, "test"
867 cs_id = changesets(:public_user_first_change).id
872 <node id='1' lon='1' lat='1' changeset='#{cs_id}' />
877 post :upload, :id => cs_id
878 assert_response :bad_request, "Shouldn't be able to upload a diff with the action ping"
879 assert_equal @response.body, "Unknown action ping, choices are create, modify, delete"
883 # upload a valid changeset which has a mixture of whitespace
884 # to check a bug reported by ivansanchez (#1565).
885 def test_upload_whitespace_valid
886 basic_authorization users(:public_user).email, "test"
887 changeset_id = changesets(:public_user_first_change).id
891 <modify><node id='1' lon='0' lat='0' changeset='#{changeset_id}'
893 <node id='1' lon='1' lat='1' changeset='#{changeset_id}' version='2'><tag k='k' v='v'/></node></modify>
895 <relation id='1' changeset='#{changeset_id}' version='1'><member
896 type='way' role='some' ref='3'/><member
897 type='node' role='some' ref='5'/>
898 <member type='relation' role='some' ref='3'/>
900 </modify></osmChange>
905 post :upload, :id => changeset_id
906 assert_response :success,
907 "can't upload a valid diff with whitespace variations to changeset: #{@response.body}"
909 # check the response is well-formed
910 assert_select "diffResult>node", 2
911 assert_select "diffResult>relation", 1
913 # check that the changes made it into the database
914 assert_equal 1, Node.find(1).tags.size, "node 1 should now have one tag"
915 assert_equal 0, Relation.find(1).tags.size, "relation 1 should now have no tags"
919 # upload a valid changeset which has a mixture of whitespace
920 # to check a bug reported by ivansanchez.
921 def test_upload_reuse_placeholder_valid
922 basic_authorization users(:public_user).email, "test"
923 changeset_id = changesets(:public_user_first_change).id
928 <node id='-1' lon='0' lat='0' changeset='#{changeset_id}'>
929 <tag k="foo" v="bar"/>
933 <node id='-1' lon='1' lat='1' changeset='#{changeset_id}' version='1'/>
936 <node id='-1' lon='2' lat='2' changeset='#{changeset_id}' version='2'/>
943 post :upload, :id => changeset_id
944 assert_response :success,
945 "can't upload a valid diff with re-used placeholders to changeset: #{@response.body}"
947 # check the response is well-formed
948 assert_select "diffResult>node", 3
949 assert_select "diffResult>node[old_id=-1]", 3
953 # test what happens if a diff upload re-uses placeholder IDs in an
955 def test_upload_placeholder_invalid
956 basic_authorization users(:public_user).email, "test"
957 changeset_id = changesets(:public_user_first_change).id
962 <node id='-1' lon='0' lat='0' changeset='#{changeset_id}' version='1'/>
963 <node id='-1' lon='1' lat='1' changeset='#{changeset_id}' version='1'/>
964 <node id='-1' lon='2' lat='2' changeset='#{changeset_id}' version='2'/>
971 post :upload, :id => changeset_id
972 assert_response :bad_request,
973 "shouldn't be able to re-use placeholder IDs"
977 # test that uploading a way referencing invalid placeholders gives a
978 # proper error, not a 500.
979 def test_upload_placeholder_invalid_way
980 basic_authorization users(:public_user).email, "test"
981 changeset_id = changesets(:public_user_first_change).id
986 <node id="-1" lon="0" lat="0" changeset="#{changeset_id}" version="1"/>
987 <node id="-2" lon="1" lat="1" changeset="#{changeset_id}" version="1"/>
988 <node id="-3" lon="2" lat="2" changeset="#{changeset_id}" version="1"/>
989 <way id="-1" changeset="#{changeset_id}" version="1">
1001 post :upload, :id => changeset_id
1002 assert_response :bad_request,
1003 "shouldn't be able to use invalid placeholder IDs"
1004 assert_equal "Placeholder node not found for reference -4 in way -1", @response.body
1006 # the same again, but this time use an existing way
1010 <node id="-1" lon="0" lat="0" changeset="#{changeset_id}" version="1"/>
1011 <node id="-2" lon="1" lat="1" changeset="#{changeset_id}" version="1"/>
1012 <node id="-3" lon="2" lat="2" changeset="#{changeset_id}" version="1"/>
1013 <way id="1" changeset="#{changeset_id}" version="1">
1025 post :upload, :id => changeset_id
1026 assert_response :bad_request,
1027 "shouldn't be able to use invalid placeholder IDs"
1028 assert_equal "Placeholder node not found for reference -4 in way 1", @response.body
1032 # test that uploading a relation referencing invalid placeholders gives a
1033 # proper error, not a 500.
1034 def test_upload_placeholder_invalid_relation
1035 basic_authorization users(:public_user).email, "test"
1036 changeset_id = changesets(:public_user_first_change).id
1041 <node id="-1" lon="0" lat="0" changeset="#{changeset_id}" version="1"/>
1042 <node id="-2" lon="1" lat="1" changeset="#{changeset_id}" version="1"/>
1043 <node id="-3" lon="2" lat="2" changeset="#{changeset_id}" version="1"/>
1044 <relation id="-1" changeset="#{changeset_id}" version="1">
1045 <member type="node" role="foo" ref="-1"/>
1046 <member type="node" role="foo" ref="-2"/>
1047 <member type="node" role="foo" ref="-3"/>
1048 <member type="node" role="foo" ref="-4"/>
1056 post :upload, :id => changeset_id
1057 assert_response :bad_request,
1058 "shouldn't be able to use invalid placeholder IDs"
1059 assert_equal "Placeholder Node not found for reference -4 in relation -1.", @response.body
1061 # the same again, but this time use an existing way
1065 <node id="-1" lon="0" lat="0" changeset="#{changeset_id}" version="1"/>
1066 <node id="-2" lon="1" lat="1" changeset="#{changeset_id}" version="1"/>
1067 <node id="-3" lon="2" lat="2" changeset="#{changeset_id}" version="1"/>
1068 <relation id="1" changeset="#{changeset_id}" version="1">
1069 <member type="node" role="foo" ref="-1"/>
1070 <member type="node" role="foo" ref="-2"/>
1071 <member type="node" role="foo" ref="-3"/>
1072 <member type="way" role="bar" ref="-1"/>
1080 post :upload, :id => changeset_id
1081 assert_response :bad_request,
1082 "shouldn't be able to use invalid placeholder IDs"
1083 assert_equal "Placeholder Way not found for reference -1 in relation 1.", @response.body
1087 # test what happens if a diff is uploaded containing only a node
1089 def test_upload_node_move
1090 basic_authorization users(:public_user).email, "test"
1092 content "<osm><changeset>" +
1093 "<tag k='created_by' v='osm test suite checking changesets'/>" +
1094 "</changeset></osm>"
1096 assert_response :success
1097 changeset_id = @response.body.to_i
1099 old_node = current_nodes(:visible_node)
1101 diff = XML::Document.new
1102 diff.root = XML::Node.new "osmChange"
1103 modify = XML::Node.new "modify"
1104 xml_old_node = old_node.to_xml_node
1105 xml_old_node["lat"] = (2.0).to_s
1106 xml_old_node["lon"] = (2.0).to_s
1107 xml_old_node["changeset"] = changeset_id.to_s
1108 modify << xml_old_node
1113 post :upload, :id => changeset_id
1114 assert_response :success,
1115 "diff should have uploaded OK"
1118 changeset = Changeset.find(changeset_id)
1119 assert_equal 1*GeoRecord::SCALE, changeset.min_lon, "min_lon should be 1 degree"
1120 assert_equal 2*GeoRecord::SCALE, changeset.max_lon, "max_lon should be 2 degrees"
1121 assert_equal 1*GeoRecord::SCALE, changeset.min_lat, "min_lat should be 1 degree"
1122 assert_equal 2*GeoRecord::SCALE, changeset.max_lat, "max_lat should be 2 degrees"
1126 # test what happens if a diff is uploaded adding a node to a way.
1127 def test_upload_way_extend
1128 basic_authorization users(:public_user).email, "test"
1130 content "<osm><changeset>" +
1131 "<tag k='created_by' v='osm test suite checking changesets'/>" +
1132 "</changeset></osm>"
1134 assert_response :success
1135 changeset_id = @response.body.to_i
1137 old_way = current_ways(:visible_way)
1139 diff = XML::Document.new
1140 diff.root = XML::Node.new "osmChange"
1141 modify = XML::Node.new "modify"
1142 xml_old_way = old_way.to_xml_node
1143 nd_ref = XML::Node.new "nd"
1144 nd_ref["ref"] = current_nodes(:visible_node).id.to_s
1145 xml_old_way << nd_ref
1146 xml_old_way["changeset"] = changeset_id.to_s
1147 modify << xml_old_way
1152 post :upload, :id => changeset_id
1153 assert_response :success,
1154 "diff should have uploaded OK"
1157 changeset = Changeset.find(changeset_id)
1158 assert_equal 1*GeoRecord::SCALE, changeset.min_lon, "min_lon should be 1 degree"
1159 assert_equal 3*GeoRecord::SCALE, changeset.max_lon, "max_lon should be 3 degrees"
1160 assert_equal 1*GeoRecord::SCALE, changeset.min_lat, "min_lat should be 1 degree"
1161 assert_equal 3*GeoRecord::SCALE, changeset.max_lat, "max_lat should be 3 degrees"
1165 # test for more issues in #1568
1166 def test_upload_empty_invalid
1167 basic_authorization users(:public_user).email, "test"
1170 "<osmChange></osmChange>",
1171 "<osmChange><modify/></osmChange>",
1172 "<osmChange><modify></modify></osmChange>"
1176 post :upload, :id => changesets(:public_user_first_change).id
1177 assert_response(:success, "should be able to upload " +
1178 "empty changeset: " + diff)
1183 # test that the X-Error-Format header works to request XML errors
1184 def test_upload_xml_errors
1185 basic_authorization users(:public_user).email, "test"
1187 # try and delete a node that is in use
1188 diff = XML::Document.new
1189 diff.root = XML::Node.new "osmChange"
1190 delete = XML::Node.new "delete"
1192 delete << current_nodes(:node_used_by_relationship).to_xml_node
1197 post :upload, :id => 2
1198 assert_response :success,
1199 "failed to return error in XML format"
1201 # check the returned payload
1202 assert_select "osmError[version=#{API_VERSION}][generator=\"OpenStreetMap server\"]", 1
1203 assert_select "osmError>status", 1
1204 assert_select "osmError>message", 1
1209 # when we make some simple changes we get the same changes back from the
1211 def test_diff_download_simple
1212 ## First try with the normal user, which should get a forbidden
1213 basic_authorization(users(:normal_user).email, "test")
1215 # create a temporary changeset
1216 content "<osm><changeset>" +
1217 "<tag k='created_by' v='osm test suite checking changesets'/>" +
1218 "</changeset></osm>"
1220 assert_response :forbidden
1224 ## Now try with the public user
1225 basic_authorization(users(:public_user).email, "test")
1227 # create a temporary changeset
1228 content "<osm><changeset>" +
1229 "<tag k='created_by' v='osm test suite checking changesets'/>" +
1230 "</changeset></osm>"
1232 assert_response :success
1233 changeset_id = @response.body.to_i
1239 <node id='1' lon='0' lat='0' changeset='#{changeset_id}' version='1'/>
1240 <node id='1' lon='1' lat='0' changeset='#{changeset_id}' version='2'/>
1241 <node id='1' lon='1' lat='1' changeset='#{changeset_id}' version='3'/>
1242 <node id='1' lon='1' lat='2' changeset='#{changeset_id}' version='4'/>
1243 <node id='1' lon='2' lat='2' changeset='#{changeset_id}' version='5'/>
1244 <node id='1' lon='3' lat='2' changeset='#{changeset_id}' version='6'/>
1245 <node id='1' lon='3' lat='3' changeset='#{changeset_id}' version='7'/>
1246 <node id='1' lon='9' lat='9' changeset='#{changeset_id}' version='8'/>
1253 post :upload, :id => changeset_id
1254 assert_response :success,
1255 "can't upload multiple versions of an element in a diff: #{@response.body}"
1257 get :download, :id => changeset_id
1258 assert_response :success
1260 assert_select "osmChange", 1
1261 assert_select "osmChange>modify", 8
1262 assert_select "osmChange>modify>node", 8
1266 # culled this from josm to ensure that nothing in the way that josm
1267 # is formatting the request is causing it to fail.
1269 # NOTE: the error turned out to be something else completely!
1270 def test_josm_upload
1271 basic_authorization(users(:public_user).email, "test")
1273 # create a temporary changeset
1274 content "<osm><changeset>" +
1275 "<tag k='created_by' v='osm test suite checking changesets'/>" +
1276 "</changeset></osm>"
1278 assert_response :success
1279 changeset_id = @response.body.to_i
1282 <osmChange version="0.6" generator="JOSM">
1283 <create version="0.6" generator="JOSM">
1284 <node id='-1' visible='true' changeset='#{changeset_id}' lat='51.49619982187321' lon='-0.18722061869438314' />
1285 <node id='-2' visible='true' changeset='#{changeset_id}' lat='51.496359883909605' lon='-0.18653093576241928' />
1286 <node id='-3' visible='true' changeset='#{changeset_id}' lat='51.49598132358285' lon='-0.18719613290981638' />
1287 <node id='-4' visible='true' changeset='#{changeset_id}' lat='51.4961591711078' lon='-0.18629015888084607' />
1288 <node id='-5' visible='true' changeset='#{changeset_id}' lat='51.49582126021711' lon='-0.18708186591517145' />
1289 <node id='-6' visible='true' changeset='#{changeset_id}' lat='51.49591018437858' lon='-0.1861432441734455' />
1290 <node id='-7' visible='true' changeset='#{changeset_id}' lat='51.49560784152179' lon='-0.18694719410005425' />
1291 <node id='-8' visible='true' changeset='#{changeset_id}' lat='51.49567389979617' lon='-0.1860289771788006' />
1292 <node id='-9' visible='true' changeset='#{changeset_id}' lat='51.49543761398892' lon='-0.186820684213126' />
1293 <way id='-10' action='modiy' visible='true' changeset='#{changeset_id}'>
1303 <tag k='highway' v='residential' />
1304 <tag k='name' v='Foobar Street' />
1312 post :upload, :id => changeset_id
1313 assert_response :success,
1314 "can't upload a diff from JOSM: #{@response.body}"
1316 get :download, :id => changeset_id
1317 assert_response :success
1319 assert_select "osmChange", 1
1320 assert_select "osmChange>create>node", 9
1321 assert_select "osmChange>create>way", 1
1322 assert_select "osmChange>create>way>nd", 9
1323 assert_select "osmChange>create>way>tag", 2
1327 # when we make some complex changes we get the same changes back from the
1329 def test_diff_download_complex
1330 basic_authorization(users(:public_user).email, "test")
1332 # create a temporary changeset
1333 content "<osm><changeset>" +
1334 "<tag k='created_by' v='osm test suite checking changesets'/>" +
1335 "</changeset></osm>"
1337 assert_response :success
1338 changeset_id = @response.body.to_i
1344 <node id='1' lon='0' lat='0' changeset='#{changeset_id}' version='1'/>
1347 <node id='-1' lon='9' lat='9' changeset='#{changeset_id}' version='0'/>
1348 <node id='-2' lon='8' lat='9' changeset='#{changeset_id}' version='0'/>
1349 <node id='-3' lon='7' lat='9' changeset='#{changeset_id}' version='0'/>
1352 <node id='3' lon='20' lat='15' changeset='#{changeset_id}' version='1'/>
1353 <way id='1' changeset='#{changeset_id}' version='1'>
1365 post :upload, :id => changeset_id
1366 assert_response :success,
1367 "can't upload multiple versions of an element in a diff: #{@response.body}"
1369 get :download, :id => changeset_id
1370 assert_response :success
1372 assert_select "osmChange", 1
1373 assert_select "osmChange>create", 3
1374 assert_select "osmChange>delete", 1
1375 assert_select "osmChange>modify", 2
1376 assert_select "osmChange>create>node", 3
1377 assert_select "osmChange>delete>node", 1
1378 assert_select "osmChange>modify>node", 1
1379 assert_select "osmChange>modify>way", 1
1382 def test_changeset_download
1383 get :download, :id => changesets(:normal_user_first_change).id
1384 assert_response :success
1386 #print @response.body
1387 # FIXME needs more assert_select tests
1388 assert_select "osmChange[version='#{API_VERSION}'][generator='#{GENERATOR}']" do
1389 assert_select "create", :count => 5
1390 assert_select "create>node[id=#{nodes(:used_node_2).node_id}][visible=#{nodes(:used_node_2).visible?}][version=#{nodes(:used_node_2).version}]" do
1391 assert_select "tag[k=#{node_tags(:t3).k}][v=#{node_tags(:t3).v}]"
1393 assert_select "create>node[id=#{nodes(:visible_node).node_id}]"
1398 # check that the bounding box of a changeset gets updated correctly
1399 ## FIXME: This should really be moded to a integration test due to the with_controller
1400 def test_changeset_bbox
1401 basic_authorization users(:public_user).email, "test"
1403 # create a new changeset
1404 content "<osm><changeset/></osm>"
1406 assert_response :success, "Creating of changeset failed."
1407 changeset_id = @response.body.to_i
1409 # add a single node to it
1410 with_controller(NodeController.new) do
1411 content "<osm><node lon='1' lat='2' changeset='#{changeset_id}'/></osm>"
1413 assert_response :success, "Couldn't create node."
1416 # get the bounding box back from the changeset
1417 get :read, :id => changeset_id, :format => :xml
1419 assert_response :success, "Couldn't read back changeset."
1420 assert_select "osm>changeset[min_lon=1.0]", 1
1421 assert_select "osm>changeset[max_lon=1.0]", 1
1422 assert_select "osm>changeset[min_lat=2.0]", 1
1423 assert_select "osm>changeset[max_lat=2.0]", 1
1425 # add another node to it
1426 with_controller(NodeController.new) do
1427 content "<osm><node lon='2' lat='1' changeset='#{changeset_id}'/></osm>"
1429 assert_response :success, "Couldn't create second node."
1432 # get the bounding box back from the changeset
1433 get :read, :id => changeset_id, :format => :xml
1434 assert_response :success, "Couldn't read back changeset for the second time."
1435 assert_select "osm>changeset[min_lon=1.0]", 1
1436 assert_select "osm>changeset[max_lon=2.0]", 1
1437 assert_select "osm>changeset[min_lat=1.0]", 1
1438 assert_select "osm>changeset[max_lat=2.0]", 1
1440 # add (delete) a way to it, which contains a point at (3,3)
1441 with_controller(WayController.new) do
1442 content update_changeset(current_ways(:visible_way).to_xml,
1444 put :delete, :id => current_ways(:visible_way).id
1445 assert_response :success, "Couldn't delete a way."
1448 # get the bounding box back from the changeset
1449 get :read, :id => changeset_id, :format => 'xml'
1450 assert_response :success, "Couldn't read back changeset for the third time."
1451 # note that the 3.1 here is because of the bbox overexpansion
1452 assert_select "osm>changeset[min_lon=1.0]", 1
1453 assert_select "osm>changeset[max_lon=3.1]", 1
1454 assert_select "osm>changeset[min_lat=1.0]", 1
1455 assert_select "osm>changeset[max_lat=3.1]", 1
1459 # test that the changeset :include method works as it should
1460 def test_changeset_include
1461 basic_authorization users(:public_user).display_name, "test"
1463 # create a new changeset
1464 content "<osm><changeset/></osm>"
1466 assert_response :success, "Creating of changeset failed."
1467 changeset_id = @response.body.to_i
1469 # NOTE: the include method doesn't over-expand, like inserting
1470 # a real method does. this is because we expect the client to
1471 # know what it is doing!
1472 check_after_include(changeset_id, 1, 1, [ 1, 1, 1, 1])
1473 check_after_include(changeset_id, 3, 3, [ 1, 1, 3, 3])
1474 check_after_include(changeset_id, 4, 2, [ 1, 1, 4, 3])
1475 check_after_include(changeset_id, 2, 2, [ 1, 1, 4, 3])
1476 check_after_include(changeset_id, -1, -1, [-1, -1, 4, 3])
1477 check_after_include(changeset_id, -2, 5, [-2, -1, 4, 5])
1481 # test that a not found, wrong method with the expand bbox works as expected
1482 def test_changeset_expand_bbox_error
1483 basic_authorization users(:public_user).display_name, "test"
1485 # create a new changeset
1486 content "<osm><changeset/></osm>"
1488 assert_response :success, "Creating of changeset failed."
1489 changeset_id = @response.body.to_i
1495 content "<osm><node lon='#{lon}' lat='#{lat}'/></osm>"
1496 put :expand_bbox, :id => changeset_id
1497 assert_response :method_not_allowed, "shouldn't be able to put a bbox expand"
1499 # Try to get the update
1500 content "<osm><node lon='#{lon}' lat='#{lat}'/></osm>"
1501 get :expand_bbox, :id => changeset_id
1502 assert_response :method_not_allowed, "shouldn't be able to get a bbox expand"
1504 # Try to use a hopefully missing changeset
1505 content "<osm><node lon='#{lon}' lat='#{lat}'/></osm>"
1506 post :expand_bbox, :id => changeset_id+13245
1507 assert_response :not_found, "shouldn't be able to do a bbox expand on a nonexistant changeset"
1512 # test the query functionality of changesets
1514 get :query, :bbox => "-10,-10, 10, 10"
1515 assert_response :success, "can't get changesets in bbox"
1516 assert_changesets [1,4,6]
1518 get :query, :bbox => "4.5,4.5,4.6,4.6"
1519 assert_response :success, "can't get changesets in bbox"
1520 assert_changesets [1]
1522 # not found when looking for changesets of non-existing users
1523 get :query, :user => User.maximum(:id) + 1
1524 assert_response :not_found
1525 get :query, :display_name => " "
1526 assert_response :not_found
1528 # can't get changesets of user 1 without authenticating
1529 get :query, :user => users(:normal_user).id
1530 assert_response :not_found, "shouldn't be able to get changesets by non-public user (ID)"
1531 get :query, :display_name => users(:normal_user).display_name
1532 assert_response :not_found, "shouldn't be able to get changesets by non-public user (name)"
1534 # but this should work
1535 basic_authorization "test@openstreetmap.org", "test"
1536 get :query, :user => users(:normal_user).id
1537 assert_response :success, "can't get changesets by user ID"
1538 assert_changesets [1,3,6]
1540 get :query, :display_name => users(:normal_user).display_name
1541 assert_response :success, "can't get changesets by user name"
1542 assert_changesets [1,3,6]
1544 # check that the correct error is given when we provide both UID and name
1545 get :query, :user => users(:normal_user).id, :display_name => users(:normal_user).display_name
1546 assert_response :bad_request, "should be a bad request to have both ID and name specified"
1548 get :query, :user => users(:normal_user).id, :open => true
1549 assert_response :success, "can't get changesets by user and open"
1550 assert_changesets [1]
1552 get :query, :time => '2007-12-31'
1553 assert_response :success, "can't get changesets by time-since"
1554 assert_changesets [1,2,4,5,6]
1556 get :query, :time => '2008-01-01T12:34Z'
1557 assert_response :success, "can't get changesets by time-since with hour"
1558 assert_changesets [1,2,4,5,6]
1560 get :query, :time => '2007-12-31T23:59Z,2008-01-01T00:01Z'
1561 assert_response :success, "can't get changesets by time-range"
1562 assert_changesets [1,5,6]
1564 get :query, :open => 'true'
1565 assert_response :success, "can't get changesets by open-ness"
1566 assert_changesets [1,2,4]
1568 get :query, :closed => 'true'
1569 assert_response :success, "can't get changesets by closed-ness"
1570 assert_changesets [3,5,6,7]
1572 get :query, :closed => 'true', :user => users(:normal_user).id
1573 assert_response :success, "can't get changesets by closed-ness and user"
1574 assert_changesets [3,6]
1576 get :query, :closed => 'true', :user => users(:public_user).id
1577 assert_response :success, "can't get changesets by closed-ness and user"
1578 assert_changesets [7]
1580 get :query, :changesets => '1,2,3'
1581 assert_response :success, "can't get changesets by id (as comma-separated string)"
1582 assert_changesets [1,2,3]
1584 get :query, :changesets => ''
1585 assert_response :bad_request, "should be a bad request since changesets is empty"
1589 # check that errors are returned if garbage is inserted
1590 # into query strings
1591 def test_query_invalid
1594 ";drop table users;"
1596 get :query, :bbox => bbox
1597 assert_response :bad_request, "'#{bbox}' isn't a bbox"
1602 ";drop table users;",
1606 get :query, :time => time
1607 assert_response :bad_request, "'#{time}' isn't a valid time range"
1615 get :query, :user => uid
1616 assert_response :bad_request, "'#{uid}' isn't a valid user ID"
1621 # check updating tags on a changeset
1622 def test_changeset_update
1623 ## First try with the non-public user
1624 changeset = changesets(:normal_user_first_change)
1625 new_changeset = changeset.to_xml
1626 new_tag = XML::Node.new "tag"
1627 new_tag['k'] = "tagtesting"
1628 new_tag['v'] = "valuetesting"
1629 new_changeset.find("//osm/changeset").first << new_tag
1630 content new_changeset
1632 # try without any authorization
1633 put :update, :id => changeset.id
1634 assert_response :unauthorized
1636 # try with the wrong authorization
1637 basic_authorization users(:public_user).email, "test"
1638 put :update, :id => changeset.id
1639 assert_response :conflict
1641 # now this should get an unauthorized
1642 basic_authorization users(:normal_user).email, "test"
1643 put :update, :id => changeset.id
1644 assert_require_public_data "user with their data non-public, shouldn't be able to edit their changeset"
1647 ## Now try with the public user
1648 changeset = changesets(:public_user_first_change)
1649 new_changeset = changeset.to_xml
1650 new_tag = XML::Node.new "tag"
1651 new_tag['k'] = "tagtesting"
1652 new_tag['v'] = "valuetesting"
1653 new_changeset.find("//osm/changeset").first << new_tag
1654 content new_changeset
1656 # try without any authorization
1657 @request.env["HTTP_AUTHORIZATION"] = nil
1658 put :update, :id => changeset.id
1659 assert_response :unauthorized
1661 # try with the wrong authorization
1662 basic_authorization users(:second_public_user).email, "test"
1663 put :update, :id => changeset.id
1664 assert_response :conflict
1666 # now this should work...
1667 basic_authorization users(:public_user).email, "test"
1668 put :update, :id => changeset.id
1669 assert_response :success
1671 assert_select "osm>changeset[id=#{changeset.id}]", 1
1672 assert_select "osm>changeset>tag", 2
1673 assert_select "osm>changeset>tag[k=tagtesting][v=valuetesting]", 1
1677 # check that a user different from the one who opened the changeset
1679 def test_changeset_update_invalid
1680 basic_authorization users(:public_user).email, "test"
1682 changeset = changesets(:normal_user_first_change)
1683 new_changeset = changeset.to_xml
1684 new_tag = XML::Node.new "tag"
1685 new_tag['k'] = "testing"
1686 new_tag['v'] = "testing"
1687 new_changeset.find("//osm/changeset").first << new_tag
1689 content new_changeset
1690 put :update, :id => changeset.id
1691 assert_response :conflict
1695 # check that a changeset can contain a certain max number of changes.
1696 ## FIXME should be changed to an integration test due to the with_controller
1697 def test_changeset_limits
1698 basic_authorization users(:public_user).email, "test"
1700 # open a new changeset
1701 content "<osm><changeset/></osm>"
1703 assert_response :success, "can't create a new changeset"
1704 cs_id = @response.body.to_i
1706 # start the counter just short of where the changeset should finish.
1708 # alter the database to set the counter on the changeset directly,
1709 # otherwise it takes about 6 minutes to fill all of them.
1710 changeset = Changeset.find(cs_id)
1711 changeset.num_changes = Changeset::MAX_ELEMENTS - offset
1714 with_controller(NodeController.new) do
1716 content "<osm><node changeset='#{cs_id}' lat='0.0' lon='0.0'/></osm>"
1718 assert_response :success, "can't create a new node"
1719 node_id = @response.body.to_i
1721 get :read, :id => node_id
1722 assert_response :success, "can't read back new node"
1723 node_doc = XML::Parser.string(@response.body).parse
1724 node_xml = node_doc.find("//osm/node").first
1726 # loop until we fill the changeset with nodes
1728 node_xml['lat'] = rand.to_s
1729 node_xml['lon'] = rand.to_s
1730 node_xml['version'] = (i+1).to_s
1733 put :update, :id => node_id
1734 assert_response :success, "attempt #{i} should have succeeded"
1737 # trying again should fail
1738 node_xml['lat'] = rand.to_s
1739 node_xml['lon'] = rand.to_s
1740 node_xml['version'] = offset.to_s
1743 put :update, :id => node_id
1744 assert_response :conflict, "final attempt should have failed"
1747 changeset = Changeset.find(cs_id)
1748 assert_equal Changeset::MAX_ELEMENTS + 1, changeset.num_changes
1750 # check that the changeset is now closed as well
1751 assert(!changeset.is_open?,
1752 "changeset should have been auto-closed by exceeding " +
1757 # This should display the last 20 changesets closed.
1759 get :list, {:format => "html"}
1760 assert_response :success
1761 assert_template "history"
1762 assert_template :layout => "map"
1763 assert_select "h2", :text => "Changesets", :count => 1
1765 get :list, {:format => "html", :list => '1', :bbox => '-180,-90,90,180'}
1766 assert_response :success
1767 assert_template "list"
1769 changesets = Changeset.
1770 where("num_changes > 0 and min_lon is not null").
1771 order(:created_at => :desc).
1773 assert changesets.size <= 20
1775 # Now check that all 20 (or however many were returned) changesets are in the html
1776 assert_select "li", :count => changesets.size
1777 changesets.each do |changeset|
1778 # FIXME this test needs rewriting - test for table contents
1783 # This should display the last 20 changesets closed.
1785 xhr :get, :list, {:format => "html"}
1786 assert_response :success
1787 assert_template "history"
1788 assert_template :layout => "xhr"
1789 assert_select "h2", :text => "Changesets", :count => 1
1791 get :list, {:format => "html", :list => '1', :bbox => '-180,-90,90,180'}
1792 assert_response :success
1793 assert_template "list"
1795 changesets = Changeset.
1796 where("num_changes > 0 and min_lon is not null").
1797 order(:created_at => :desc).
1799 assert changesets.size <= 20
1801 # Now check that all 20 (or however many were returned) changesets are in the html
1802 assert_select "li", :count => changesets.size
1803 changesets.each do |changeset|
1804 # FIXME this test needs rewriting - test for table contents
1809 # Checks the display of the user changesets listing
1811 user = users(:public_user)
1812 get :list, {:format => "html", :display_name => user.display_name}
1813 assert_response :success
1814 assert_template "history"
1815 ## FIXME need to add more checks to see which if edits are actually shown if your data is public
1819 # Check the not found of the list user changesets
1820 def test_list_user_not_found
1821 get :list, {:format => "html", :display_name => "Some random user"}
1822 assert_response :not_found
1823 assert_template 'user/no_such_user'
1827 # This should display the last 20 changesets closed.
1829 changesets = Changeset.where("num_changes > 0").order(:created_at => :desc).limit(20)
1830 assert changesets.size <= 20
1831 get :feed, {:format => "atom"}
1832 assert_response :success
1833 assert_template "list"
1834 # Now check that all 20 (or however many were returned) changesets are in the html
1835 assert_select "feed", :count => 1
1836 assert_select "entry", :count => changesets.size
1837 changesets.each do |changeset|
1838 # FIXME this test needs rewriting - test for feed contents
1843 # Checks the display of the user changesets feed
1845 user = users(:public_user)
1846 get :feed, {:format => "atom", :display_name => user.display_name}
1847 assert_response :success
1848 assert_template "list"
1849 assert_equal "application/atom+xml", response.content_type
1850 ## FIXME need to add more checks to see which if edits are actually shown if your data is public
1854 # Check the not found of the user changesets feed
1855 def test_feed_user_not_found
1856 get :feed, {:format => "atom", :display_name => "Some random user"}
1857 assert_response :not_found
1861 # check that the changeset download for a changeset with a redacted
1862 # element in it doesn't contain that element.
1863 def test_diff_download_redacted
1864 changeset_id = changesets(:public_user_first_change).id
1866 get :download, :id => changeset_id
1867 assert_response :success
1869 assert_select "osmChange", 1
1870 # this changeset contains node 17 in versions 1 & 2, but 1 should
1872 assert_select "osmChange node[id=17]", 1
1873 assert_select "osmChange node[id=17][version=1]", 0
1877 # create comment success
1878 def test_create_comment_success
1879 basic_authorization(users(:public_user).email, 'test')
1881 assert_difference('ChangesetComment.count') do
1882 post :comment, { :id => changesets(:normal_user_closed_change).id, :text => 'This is a comment', :format => :xml }
1884 assert_response :success
1888 # create comment fail
1889 def test_create_comment_fail
1891 post :comment, { :id => changesets(:normal_user_closed_change).id, :text => 'This is a comment' }
1892 assert_response :unauthorized
1894 basic_authorization(users(:public_user).email, 'test')
1897 assert_no_difference('ChangesetComment.count') do
1898 post :comment, { :id => 999111, :text => 'This is a comment' }
1900 assert_response :not_found
1902 # not closed changeset
1903 assert_no_difference('ChangesetComment.count') do
1904 post :comment, { :id => changesets(:normal_user_first_change).id, :text => 'This is a comment' }
1906 assert_response :conflict
1909 assert_no_difference('ChangesetComment.count') do
1910 post :comment, { :id => changesets(:normal_user_closed_change).id }
1912 assert_response :bad_request
1915 assert_no_difference('ChangesetComment.count') do
1916 post :comment, { :id => changesets(:normal_user_closed_change).id, :text => '' }
1918 assert_response :bad_request
1922 # test subscribe success
1923 def test_subscribe_success
1924 basic_authorization(users(:public_user).email, 'test')
1925 changeset = changesets(:normal_user_closed_change)
1927 assert_difference('changeset.subscribers.count') do
1928 post :subscribe, { :id => changeset.id, :format => :xml }
1930 assert_response :success
1934 # test subscribe fail
1935 def test_subscribe_fail
1936 changeset = changesets(:normal_user_closed_change)
1937 assert_no_difference('changeset.subscribers.count') do
1938 post :subscribe, { :id => changeset.id, :format => :xml }
1940 assert_response :unauthorized
1942 basic_authorization(users(:public_user).email, 'test')
1944 assert_no_difference('changeset.subscribers.count') do
1945 post :subscribe, { :id => 999111 }
1947 assert_response :not_found
1949 changeset = changesets(:normal_user_first_change)
1950 assert_no_difference('changeset.subscribers.count') do
1951 post :subscribe, { :id => changeset.id }
1953 assert_response :conflict
1956 changeset = changesets(:normal_user_closed_change)
1957 post :subscribe, { :id => changeset.id, :format => :xml }
1958 assert_response :success
1960 # trying to subsrcirbe one more time
1961 assert_no_difference('changeset.subscribers.count') do
1962 post :subscribe, { :id => changeset.id, :format => :xml }
1964 assert_response :conflict
1968 # test unsubscribe success
1969 def test_unsubscribe_success
1970 basic_authorization(users(:public_user).email, 'test')
1971 changeset = changesets(:normal_user_closed_change)
1972 post :subscribe, { :id => changeset.id, :format => :xml }
1974 assert_difference('changeset.subscribers.count', -1) do
1975 post :unsubscribe, { :id => changeset.id, :format => :xml }
1977 assert_response :success
1981 # test unsubscribe fail
1982 def test_unsubscribe_fail
1983 changeset = changesets(:normal_user_closed_change)
1984 assert_no_difference('changeset.subscribers.count') do
1985 post :unsubscribe, { :id => changeset.id }
1987 assert_response :unauthorized
1989 basic_authorization(users(:public_user).email, 'test')
1991 assert_no_difference('changeset.subscribers.count', -1) do
1992 post :unsubscribe, { :id => 999111 }
1994 assert_response :not_found
1996 changeset = changesets(:normal_user_first_change)
1997 assert_no_difference('changeset.subscribers.count', -1) do
1998 post :unsubscribe, { :id => changeset.id }
2000 assert_response :conflict
2002 # trying to unsubscribe when not subscribed
2003 changeset = changesets(:normal_user_closed_change)
2004 assert_no_difference('changeset.subscribers.count') do
2005 post :unsubscribe, { :id => changeset.id }
2007 assert_response :not_found
2011 # test hide comment fail
2012 def test_hide_comment_fail
2013 comment = changeset_comments(:normal_comment_1)
2014 assert('comment.visible') do
2015 post :hide_comment, { :id => comment.id, :format => "xml" }
2016 assert_response :unauthorized
2020 basic_authorization(users(:public_user).email, 'test')
2022 assert('comment.visible') do
2023 post :hide_comment, { :id => comment.id, :format => "xml" }
2024 assert_response :forbidden
2027 basic_authorization(users(:moderator_user).email, 'test')
2029 post :hide_comment, { :id => 999111, :format => "xml" }
2030 assert_response :not_found
2034 # test hide comment succes
2035 def test_hide_comment_success
2036 comment = changeset_comments(:normal_comment_1)
2038 basic_authorization(users(:moderator_user).email, 'test')
2040 assert('!comment.visible') do
2041 post :hide_comment, { :id => comment.id, :format => "xml" }
2043 assert_response :success
2047 # test unhide comment fail
2048 def test_unhide_comment_fail
2049 comment = changeset_comments(:normal_comment_1)
2050 assert('comment.visible') do
2051 post :unhide_comment, { :id => comment.id, :format => "xml" }
2052 assert_response :unauthorized
2056 basic_authorization(users(:public_user).email, 'test')
2058 assert('comment.visible') do
2059 post :unhide_comment, { :id => comment.id, :format => "xml" }
2060 assert_response :forbidden
2063 basic_authorization(users(:moderator_user).email, 'test')
2065 post :unhide_comment, { :id => 999111, :format => "xml" }
2066 assert_response :not_found
2070 # test unhide comment succes
2071 def test_unhide_comment_success
2072 comment = changeset_comments(:normal_comment_1)
2074 basic_authorization(users(:moderator_user).email, 'test')
2076 assert('!comment.visible') do
2077 post :unhide_comment, { :id => comment.id, :format => "xml" }
2079 assert_response :success
2083 # test comments feed
2084 def test_comments_feed
2085 get :comments_feed, {:format => "rss"}
2086 assert_response :success
2087 assert_equal "application/rss+xml", @response.content_type
2088 assert_select "rss", :count => 1 do
2089 assert_select "channel", :count => 1 do
2090 assert_select "item", :count => 3
2094 get :comments_feed, { :id => changesets(:normal_user_closed_change), :format => "rss"}
2095 assert_response :success
2096 assert_equal "application/rss+xml", @response.content_type
2097 assert_select "rss", :count => 1 do
2098 assert_select "channel", :count => 1 do
2099 assert_select "item", :count => 3
2104 #------------------------------------------------------------
2106 #------------------------------------------------------------
2109 # boilerplate for checking that certain changesets exist in the
2111 def assert_changesets(ids)
2112 assert_select "osm>changeset", ids.size
2114 assert_select "osm>changeset[id=#{id}]", 1
2119 # call the include method and assert properties of the bbox
2120 def check_after_include(changeset_id, lon, lat, bbox)
2121 content "<osm><node lon='#{lon}' lat='#{lat}'/></osm>"
2122 post :expand_bbox, :id => changeset_id
2123 assert_response :success, "Setting include of changeset failed: #{@response.body}"
2125 # check exactly one changeset
2126 assert_select "osm>changeset", 1
2127 assert_select "osm>changeset[id=#{changeset_id}]", 1
2130 doc = XML::Parser.string(@response.body).parse
2131 changeset = doc.find("//osm/changeset").first
2132 assert_equal bbox[0], changeset['min_lon'].to_f, "min lon"
2133 assert_equal bbox[1], changeset['min_lat'].to_f, "min lat"
2134 assert_equal bbox[2], changeset['max_lon'].to_f, "max lon"
2135 assert_equal bbox[3], changeset['max_lat'].to_f, "max lat"
2139 # update the changeset_id of a way element
2140 def update_changeset(xml, changeset_id)
2141 xml_attr_rewrite(xml, 'changeset', changeset_id)
2145 # update an attribute in a way element
2146 def xml_attr_rewrite(xml, name, value)
2147 xml.find("//osm/way").first[name] = value.to_s