2 require "changeset_controller"
4 class ChangesetControllerTest < ActionController::TestCase
6 # test all routes which lead to this controller
9 { :path => "/api/0.6/changeset/create", :method => :put },
10 { :controller => "changeset", :action => "create" }
13 { :path => "/api/0.6/changeset/1/upload", :method => :post },
14 { :controller => "changeset", :action => "upload", :id => "1" }
17 { :path => "/api/0.6/changeset/1/download", :method => :get },
18 { :controller => "changeset", :action => "download", :id => "1" }
21 { :path => "/api/0.6/changeset/1/expand_bbox", :method => :post },
22 { :controller => "changeset", :action => "expand_bbox", :id => "1" }
25 { :path => "/api/0.6/changeset/1", :method => :get },
26 { :controller => "changeset", :action => "read", :id => "1" }
29 { :path => "/api/0.6/changeset/1/subscribe", :method => :post },
30 { :controller => "changeset", :action => "subscribe", :id => "1" }
33 { :path => "/api/0.6/changeset/1/unsubscribe", :method => :post },
34 { :controller => "changeset", :action => "unsubscribe", :id => "1" }
37 { :path => "/api/0.6/changeset/1", :method => :put },
38 { :controller => "changeset", :action => "update", :id => "1" }
41 { :path => "/api/0.6/changeset/1/close", :method => :put },
42 { :controller => "changeset", :action => "close", :id => "1" }
45 { :path => "/api/0.6/changeset/1/comment", :method => :post },
46 { :controller => "changeset", :action => "comment", :id => "1" }
49 { :path => "/api/0.6/changeset/comment/1/hide", :method => :post },
50 { :controller => "changeset", :action => "hide_comment", :id => "1" }
53 { :path => "/api/0.6/changeset/comment/1/unhide", :method => :post },
54 { :controller => "changeset", :action => "unhide_comment", :id => "1" }
57 { :path => "/api/0.6/changesets", :method => :get },
58 { :controller => "changeset", :action => "query" }
61 { :path => "/changeset/1/comments/feed", :method => :get },
62 { :controller => "changeset", :action => "comments_feed", :id => "1", :format => "rss" }
65 { :path => "/user/name/history", :method => :get },
66 { :controller => "changeset", :action => "list", :display_name => "name" }
69 { :path => "/user/name/history/feed", :method => :get },
70 { :controller => "changeset", :action => "feed", :display_name => "name", :format => :atom }
73 { :path => "/history/friends", :method => :get },
74 { :controller => "changeset", :action => "list", :friends => true, :format => :html }
77 { :path => "/history/nearby", :method => :get },
78 { :controller => "changeset", :action => "list", :nearby => true, :format => :html }
81 { :path => "/history", :method => :get },
82 { :controller => "changeset", :action => "list" }
85 { :path => "/history/feed", :method => :get },
86 { :controller => "changeset", :action => "feed", :format => :atom }
89 { :path => "/history/comments/feed", :method => :get },
90 { :controller => "changeset", :action => "comments_feed", :format => "rss" }
94 # -----------------------
95 # Test simple changeset creation
96 # -----------------------
99 basic_authorization create(:user, :data_public => false).email, "test"
100 # Create the first user's changeset
101 content "<osm><changeset>" +
102 "<tag k='created_by' v='osm test suite checking changesets'/>" +
105 assert_require_public_data
107 basic_authorization create(: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 create(:user, :data_public => false).email, "test"
135 content "<osm><changeset></osm>"
137 assert_require_public_data
139 ## Try the public user
140 basic_authorization create(: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 a non-public user
152 basic_authorization create(:user, :data_public => false).email, "test"
154 assert_require_public_data
156 ## Try an inactive user
157 basic_authorization create(:user, :pending).email, "test"
161 ## Now try to use a normal user
162 basic_authorization create(: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 create(: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 = create(:changeset).id
181 get :read, :id => changeset_id
182 assert_response :success, "cannot get first changeset"
184 assert_select "osm[version='#{API_VERSION}'][generator='OpenStreetMap server']", 1
185 assert_select "osm>changeset[id='#{changeset_id}']", 1
186 assert_select "osm>changeset>discussion", 0
188 get :read, :id => changeset_id, :include_discussion => true
189 assert_response :success, "cannot get first changeset with comments"
191 assert_select "osm[version='#{API_VERSION}'][generator='OpenStreetMap server']", 1
192 assert_select "osm>changeset[id='#{changeset_id}']", 1
193 assert_select "osm>changeset>discussion", 1
194 assert_select "osm>changeset>discussion>comment", 0
196 changeset_id = create(:changeset, :closed).id
197 create_list(:changeset_comment, 3, :changeset_id => changeset_id)
199 get :read, :id => changeset_id, :include_discussion => true
200 assert_response :success, "cannot get closed changeset with comments"
202 assert_select "osm[version='#{API_VERSION}'][generator='OpenStreetMap server']", 1
203 assert_select "osm>changeset[id='#{changeset_id}']", 1
204 assert_select "osm>changeset>discussion", 1
205 assert_select "osm>changeset>discussion>comment", 3
209 # check that a changeset that doesn't exist returns an appropriate message
210 def test_read_not_found
211 [0, -32, 233455644, "afg", "213"].each do |id|
214 assert_response :not_found, "should get a not found"
215 rescue ActionController::UrlGenerationError => ex
216 assert_match /No route matches/, ex.to_s
222 # test that the user who opened a change can close it
224 private_user = create(:user, :data_public => false)
225 private_changeset = create(:changeset, :user => private_user)
227 changeset = create(:changeset, :user => user)
229 ## Try without authentication
230 put :close, :id => changeset.id
231 assert_response :unauthorized
233 ## Try using the non-public user
234 basic_authorization private_user.email, "test"
235 put :close, :id => private_changeset.id
236 assert_require_public_data
238 ## The try with the public user
239 basic_authorization user.email, "test"
242 put :close, :id => cs_id
243 assert_response :success
245 # test that it really is closed now
246 cs = Changeset.find(cs_id)
248 "changeset should be closed now (#{cs.closed_at} > #{Time.now.getutc}.")
252 # test that a different user can't close another user's changeset
253 def test_close_invalid
255 changeset = create(:changeset)
257 basic_authorization user.email, "test"
259 put :close, :id => changeset.id
260 assert_response :conflict
261 assert_equal "The user doesn't own that changeset", @response.body
265 # test that you can't close using another method
266 def test_close_method_invalid
268 changeset = create(:changeset, :user => user)
270 basic_authorization user.email, "test"
272 get :close, :id => changeset.id
273 assert_response :method_not_allowed
275 post :close, :id => changeset.id
276 assert_response :method_not_allowed
280 # check that you can't close a changeset that isn't found
281 def test_close_not_found
282 cs_ids = [0, -132, "123"]
284 # First try to do it with no auth
287 put :close, :id => id
288 assert_response :unauthorized, "Shouldn't be able close the non-existant changeset #{id}, when not authorized"
289 rescue ActionController::UrlGenerationError => ex
290 assert_match /No route matches/, ex.to_s
295 basic_authorization create(:user).email, "test"
298 put :close, :id => id
299 assert_response :not_found, "The changeset #{id} doesn't exist, so can't be closed"
300 rescue ActionController::UrlGenerationError => ex
301 assert_match /No route matches/, ex.to_s
307 # upload something simple, but valid and check that it can
309 # Also try without auth and another user.
310 def test_upload_simple_valid
311 private_user = create(:user, :data_public => false)
312 private_changeset = create(:changeset, :user => private_user)
314 changeset = create(:changeset, :user => user)
318 relation = create(:relation)
319 other_relation = create(:relation)
320 # create some tags, since we test that they are removed later
321 create(:node_tag, :node => node)
322 create(:way_tag, :way => way)
323 create(:relation_tag, :relation => relation)
326 changeset_id = changeset.id
328 # simple diff to change a node, way and relation by removing
333 <node id='#{node.id}' lon='0' lat='0' changeset='#{changeset_id}' version='1'/>
334 <way id='#{way.id}' changeset='#{changeset_id}' version='1'>
335 <nd ref='#{node.id}'/>
339 <relation id='#{relation.id}' changeset='#{changeset_id}' version='1'>
340 <member type='way' role='some' ref='#{way.id}'/>
341 <member type='node' role='some' ref='#{node.id}'/>
342 <member type='relation' role='some' ref='#{other_relation.id}'/>
350 post :upload, :id => changeset_id
351 assert_response :unauthorized,
352 "shouldn't be able to upload a simple valid diff to changeset: #{@response.body}"
354 ## Now try with a private user
355 basic_authorization private_user.email, "test"
356 changeset_id = private_changeset.id
358 # simple diff to change a node, way and relation by removing
363 <node id='#{node.id}' lon='0' lat='0' changeset='#{changeset_id}' version='1'/>
364 <way id='#{way.id}' changeset='#{changeset_id}' version='1'>
365 <nd ref='#{node.id}'/>
369 <relation id='#{relation.id}' changeset='#{changeset_id}' version='1'>
370 <member type='way' role='some' ref='#{way.id}'/>
371 <member type='node' role='some' ref='#{node.id}'/>
372 <member type='relation' role='some' ref='#{other_relation.id}'/>
380 post :upload, :id => changeset_id
381 assert_response :forbidden,
382 "can't upload a simple valid diff to changeset: #{@response.body}"
384 ## Now try with the public user
385 basic_authorization user.email, "test"
386 changeset_id = changeset.id
388 # simple diff to change a node, way and relation by removing
393 <node id='#{node.id}' lon='0' lat='0' changeset='#{changeset_id}' version='1'/>
394 <way id='#{way.id}' changeset='#{changeset_id}' version='1'>
395 <nd ref='#{node.id}'/>
399 <relation id='#{relation.id}' changeset='#{changeset_id}' version='1'>
400 <member type='way' role='some' ref='#{way.id}'/>
401 <member type='node' role='some' ref='#{node.id}'/>
402 <member type='relation' role='some' ref='#{other_relation.id}'/>
410 post :upload, :id => changeset_id
411 assert_response :success,
412 "can't upload a simple valid diff to changeset: #{@response.body}"
414 # check that the changes made it into the database
415 assert_equal 0, Node.find(node.id).tags.size, "node #{node.id} should now have no tags"
416 assert_equal 0, Way.find(way.id).tags.size, "way #{way.id} should now have no tags"
417 assert_equal 0, Relation.find(relation.id).tags.size, "relation #{relation.id} should now have no tags"
421 # upload something which creates new objects using placeholders
422 def test_upload_create_valid
424 changeset = create(:changeset, :user => user)
426 way = create(:way_with_nodes, :nodes_count => 2)
427 relation = create(:relation)
429 basic_authorization user.email, "test"
431 # simple diff to create a node way and relation using placeholders
435 <node id='-1' lon='0' lat='0' changeset='#{changeset.id}'>
436 <tag k='foo' v='bar'/>
437 <tag k='baz' v='bat'/>
439 <way id='-1' changeset='#{changeset.id}'>
440 <nd ref='#{node.id}'/>
444 <relation id='-1' changeset='#{changeset.id}'>
445 <member type='way' role='some' ref='#{way.id}'/>
446 <member type='node' role='some' ref='#{node.id}'/>
447 <member type='relation' role='some' ref='#{relation.id}'/>
455 post :upload, :id => changeset.id
456 assert_response :success,
457 "can't upload a simple valid creation to changeset: #{@response.body}"
459 # check the returned payload
460 assert_select "diffResult[version='#{API_VERSION}'][generator='OpenStreetMap server']", 1
461 assert_select "diffResult>node", 1
462 assert_select "diffResult>way", 1
463 assert_select "diffResult>relation", 1
465 # inspect the response to find out what the new element IDs are
466 doc = XML::Parser.string(@response.body).parse
467 new_node_id = doc.find("//diffResult/node").first["new_id"].to_i
468 new_way_id = doc.find("//diffResult/way").first["new_id"].to_i
469 new_rel_id = doc.find("//diffResult/relation").first["new_id"].to_i
471 # check the old IDs are all present and negative one
472 assert_equal -1, doc.find("//diffResult/node").first["old_id"].to_i
473 assert_equal -1, doc.find("//diffResult/way").first["old_id"].to_i
474 assert_equal -1, doc.find("//diffResult/relation").first["old_id"].to_i
476 # check the versions are present and equal one
477 assert_equal 1, doc.find("//diffResult/node").first["new_version"].to_i
478 assert_equal 1, doc.find("//diffResult/way").first["new_version"].to_i
479 assert_equal 1, doc.find("//diffResult/relation").first["new_version"].to_i
481 # check that the changes made it into the database
482 assert_equal 2, Node.find(new_node_id).tags.size, "new node should have two tags"
483 assert_equal 0, Way.find(new_way_id).tags.size, "new way should have no tags"
484 assert_equal 0, Relation.find(new_rel_id).tags.size, "new relation should have no tags"
488 # test a complex delete where we delete elements which rely on eachother
489 # in the same transaction.
490 def test_upload_delete
491 changeset = create(:changeset)
492 super_relation = create(:relation)
493 used_relation = create(:relation)
494 used_way = create(:way)
495 used_node = create(:node)
496 create(:relation_member, :relation => super_relation, :member => used_relation)
497 create(:relation_member, :relation => super_relation, :member => used_way)
498 create(:relation_member, :relation => super_relation, :member => used_node)
500 basic_authorization changeset.user.display_name, "test"
502 diff = XML::Document.new
503 diff.root = XML::Node.new "osmChange"
504 delete = XML::Node.new "delete"
506 delete << super_relation.to_xml_node
507 delete << used_relation.to_xml_node
508 delete << used_way.to_xml_node
509 delete << used_node.to_xml_node
511 # update the changeset to one that this user owns
512 %w(node way relation).each do |type|
513 delete.find("//osmChange/delete/#{type}").each do |n|
514 n["changeset"] = changeset.id.to_s
520 post :upload, :id => changeset.id
521 assert_response :success,
522 "can't upload a deletion diff to changeset: #{@response.body}"
524 # check the response is well-formed
525 assert_select "diffResult>node", 1
526 assert_select "diffResult>way", 1
527 assert_select "diffResult>relation", 2
529 # check that everything was deleted
530 assert_equal false, Node.find(used_node.id).visible
531 assert_equal false, Way.find(used_way.id).visible
532 assert_equal false, Relation.find(super_relation.id).visible
533 assert_equal false, Relation.find(used_relation.id).visible
537 # test uploading a delete with no lat/lon, as they are optional in
538 # the osmChange spec.
539 def test_upload_nolatlon_delete
541 changeset = create(:changeset)
543 basic_authorization changeset.user.display_name, "test"
544 diff = "<osmChange><delete><node id='#{node.id}' version='#{node.version}' changeset='#{changeset.id}'/></delete></osmChange>"
548 post :upload, :id => changeset.id
549 assert_response :success,
550 "can't upload a deletion diff to changeset: #{@response.body}"
552 # check the response is well-formed
553 assert_select "diffResult>node", 1
555 # check that everything was deleted
556 assert_equal false, Node.find(node.id).visible
559 def test_repeated_changeset_create
561 basic_authorization create(:user).email, "test"
563 # create a temporary changeset
564 content "<osm><changeset>" +
565 "<tag k='created_by' v='osm test suite checking changesets'/>" +
567 assert_difference "Changeset.count", 1 do
570 assert_response :success
574 def test_upload_large_changeset
575 basic_authorization create(:user).email, "test"
578 content "<osm><changeset/></osm>"
580 assert_response :success, "Should be able to create a changeset: #{@response.body}"
581 changeset_id = @response.body.to_i
583 # upload some widely-spaced nodes, spiralling positive and negative to cause
584 # largest bbox over-expansion possible.
588 <node id='-1' lon='-20' lat='-10' changeset='#{changeset_id}'/>
589 <node id='-10' lon='20' lat='10' changeset='#{changeset_id}'/>
590 <node id='-2' lon='-40' lat='-20' changeset='#{changeset_id}'/>
591 <node id='-11' lon='40' lat='20' changeset='#{changeset_id}'/>
592 <node id='-3' lon='-60' lat='-30' changeset='#{changeset_id}'/>
593 <node id='-12' lon='60' lat='30' changeset='#{changeset_id}'/>
594 <node id='-4' lon='-80' lat='-40' changeset='#{changeset_id}'/>
595 <node id='-13' lon='80' lat='40' changeset='#{changeset_id}'/>
596 <node id='-5' lon='-100' lat='-50' changeset='#{changeset_id}'/>
597 <node id='-14' lon='100' lat='50' changeset='#{changeset_id}'/>
598 <node id='-6' lon='-120' lat='-60' changeset='#{changeset_id}'/>
599 <node id='-15' lon='120' lat='60' changeset='#{changeset_id}'/>
600 <node id='-7' lon='-140' lat='-70' changeset='#{changeset_id}'/>
601 <node id='-16' lon='140' lat='70' changeset='#{changeset_id}'/>
602 <node id='-8' lon='-160' lat='-80' changeset='#{changeset_id}'/>
603 <node id='-17' lon='160' lat='80' changeset='#{changeset_id}'/>
604 <node id='-9' lon='-179.9' lat='-89.9' changeset='#{changeset_id}'/>
605 <node id='-18' lon='179.9' lat='89.9' changeset='#{changeset_id}'/>
610 # upload it, which used to cause an error like "PGError: ERROR:
611 # integer out of range" (bug #2152). but shouldn't any more.
613 post :upload, :id => changeset_id
614 assert_response :success,
615 "can't upload a spatially-large diff to changeset: #{@response.body}"
617 # check that the changeset bbox is within bounds
618 cs = Changeset.find(changeset_id)
619 assert cs.min_lon >= -180 * GeoRecord::SCALE, "Minimum longitude (#{cs.min_lon / GeoRecord::SCALE}) should be >= -180 to be valid."
620 assert cs.max_lon <= 180 * GeoRecord::SCALE, "Maximum longitude (#{cs.max_lon / GeoRecord::SCALE}) should be <= 180 to be valid."
621 assert cs.min_lat >= -90 * GeoRecord::SCALE, "Minimum latitude (#{cs.min_lat / GeoRecord::SCALE}) should be >= -90 to be valid."
622 assert cs.max_lat >= 90 * GeoRecord::SCALE, "Maximum latitude (#{cs.max_lat / GeoRecord::SCALE}) should be <= 90 to be valid."
626 # test that deleting stuff in a transaction doesn't bypass the checks
627 # to ensure that used elements are not deleted.
628 def test_upload_delete_invalid
629 changeset = create(:changeset)
630 relation = create(:relation)
631 other_relation = create(:relation)
632 used_way = create(:way)
633 used_node = create(:node)
634 create(:relation_member, :relation => relation, :member => used_way)
635 create(:relation_member, :relation => relation, :member => used_node)
637 basic_authorization changeset.user.email, "test"
639 diff = XML::Document.new
640 diff.root = XML::Node.new "osmChange"
641 delete = XML::Node.new "delete"
643 delete << other_relation.to_xml_node
644 delete << used_way.to_xml_node
645 delete << used_node.to_xml_node
647 # update the changeset to one that this user owns
648 %w(node way relation).each do |type|
649 delete.find("//osmChange/delete/#{type}").each do |n|
650 n["changeset"] = changeset.id.to_s
656 post :upload, :id => changeset.id
657 assert_response :precondition_failed,
658 "shouldn't be able to upload a invalid deletion diff: #{@response.body}"
659 assert_equal "Precondition failed: Way #{used_way.id} is still used by relations #{relation.id}.", @response.body
661 # check that nothing was, in fact, deleted
662 assert_equal true, Node.find(used_node.id).visible
663 assert_equal true, Way.find(used_way.id).visible
664 assert_equal true, Relation.find(relation.id).visible
665 assert_equal true, Relation.find(other_relation.id).visible
669 # test that a conditional delete of an in use object works.
670 def test_upload_delete_if_unused
671 changeset = create(:changeset)
672 super_relation = create(:relation)
673 used_relation = create(:relation)
674 used_way = create(:way)
675 used_node = create(:node)
676 create(:relation_member, :relation => super_relation, :member => used_relation)
677 create(:relation_member, :relation => super_relation, :member => used_way)
678 create(:relation_member, :relation => super_relation, :member => used_node)
680 basic_authorization changeset.user.email, "test"
682 diff = XML::Document.new
683 diff.root = XML::Node.new "osmChange"
684 delete = XML::Node.new "delete"
686 delete["if-unused"] = ""
687 delete << used_relation.to_xml_node
688 delete << used_way.to_xml_node
689 delete << used_node.to_xml_node
691 # update the changeset to one that this user owns
692 %w(node way relation).each do |type|
693 delete.find("//osmChange/delete/#{type}").each do |n|
694 n["changeset"] = changeset.id.to_s
700 post :upload, :id => changeset.id
701 assert_response :success,
702 "can't do a conditional delete of in use objects: #{@response.body}"
704 # check the returned payload
705 assert_select "diffResult[version='#{API_VERSION}'][generator='OpenStreetMap server']", 1
706 assert_select "diffResult>node", 1
707 assert_select "diffResult>way", 1
708 assert_select "diffResult>relation", 1
711 doc = XML::Parser.string(@response.body).parse
713 # check the old IDs are all present and what we expect
714 assert_equal used_node.id, doc.find("//diffResult/node").first["old_id"].to_i
715 assert_equal used_way.id, doc.find("//diffResult/way").first["old_id"].to_i
716 assert_equal used_relation.id, doc.find("//diffResult/relation").first["old_id"].to_i
718 # check the new IDs are all present and unchanged
719 assert_equal used_node.id, doc.find("//diffResult/node").first["new_id"].to_i
720 assert_equal used_way.id, doc.find("//diffResult/way").first["new_id"].to_i
721 assert_equal used_relation.id, doc.find("//diffResult/relation").first["new_id"].to_i
723 # check the new versions are all present and unchanged
724 assert_equal used_node.version, doc.find("//diffResult/node").first["new_version"].to_i
725 assert_equal used_way.version, doc.find("//diffResult/way").first["new_version"].to_i
726 assert_equal used_relation.version, doc.find("//diffResult/relation").first["new_version"].to_i
728 # check that nothing was, in fact, deleted
729 assert_equal true, Node.find(used_node.id).visible
730 assert_equal true, Way.find(used_way.id).visible
731 assert_equal true, Relation.find(used_relation.id).visible
735 # upload an element with a really long tag value
736 def test_upload_invalid_too_long_tag
737 changeset = create(:changeset)
739 basic_authorization changeset.user.email, "test"
741 # simple diff to create a node way and relation using placeholders
745 <node id='-1' lon='0' lat='0' changeset='#{changeset.id}'>
746 <tag k='foo' v='#{'x' * 256}'/>
754 post :upload, :id => changeset.id
755 assert_response :bad_request,
756 "shoudln't be able to upload too long a tag to changeset: #{@response.body}"
760 # upload something which creates new objects and inserts them into
761 # existing containers using placeholders.
762 def test_upload_complex
765 relation = create(:relation)
766 create(:way_node, :way => way, :node => node)
768 changeset = create(:changeset)
770 basic_authorization changeset.user.email, "test"
772 # simple diff to create a node way and relation using placeholders
776 <node id='-1' lon='0' lat='0' changeset='#{changeset.id}'>
777 <tag k='foo' v='bar'/>
778 <tag k='baz' v='bat'/>
782 <way id='#{way.id}' changeset='#{changeset.id}' version='1'>
784 <nd ref='#{node.id}'/>
786 <relation id='#{relation.id}' changeset='#{changeset.id}' version='1'>
787 <member type='way' role='some' ref='#{way.id}'/>
788 <member type='node' role='some' ref='-1'/>
789 <member type='relation' role='some' ref='#{relation.id}'/>
797 post :upload, :id => changeset.id
798 assert_response :success,
799 "can't upload a complex diff to changeset: #{@response.body}"
801 # check the returned payload
802 assert_select "diffResult[version='#{API_VERSION}'][generator='#{GENERATOR}']", 1
803 assert_select "diffResult>node", 1
804 assert_select "diffResult>way", 1
805 assert_select "diffResult>relation", 1
807 # inspect the response to find out what the new element IDs are
808 doc = XML::Parser.string(@response.body).parse
809 new_node_id = doc.find("//diffResult/node").first["new_id"].to_i
811 # check that the changes made it into the database
812 assert_equal 2, Node.find(new_node_id).tags.size, "new node should have two tags"
813 assert_equal [new_node_id, node.id], Way.find(way.id).nds, "way nodes should match"
814 Relation.find(relation.id).members.each do |type, id, _role|
816 assert_equal new_node_id, id, "relation should contain new node"
822 # create a diff which references several changesets, which should cause
823 # a rollback and none of the diff gets committed
824 def test_upload_invalid_changesets
825 changeset = create(:changeset)
826 other_changeset = create(:changeset, :user => changeset.user)
829 relation = create(:relation)
830 other_relation = create(:relation)
832 basic_authorization changeset.user.email, "test"
834 # simple diff to create a node way and relation using placeholders
838 <node id='#{node.id}' lon='0' lat='0' changeset='#{changeset.id}' version='1'/>
839 <way id='#{way.id}' changeset='#{changeset.id}' version='1'>
840 <nd ref='#{node.id}'/>
844 <relation id='#{relation.id}' changeset='#{changeset.id}' version='1'>
845 <member type='way' role='some' ref='#{way.id}'/>
846 <member type='node' role='some' ref='#{node.id}'/>
847 <member type='relation' role='some' ref='#{other_relation.id}'/>
851 <node id='-1' lon='0' lat='0' changeset='#{other_changeset.id}'>
852 <tag k='foo' v='bar'/>
853 <tag k='baz' v='bat'/>
861 post :upload, :id => changeset.id
862 assert_response :conflict,
863 "uploading a diff with multiple changesets should have failed"
865 # check that objects are unmodified
866 assert_nodes_are_equal(node, Node.find(node.id))
867 assert_ways_are_equal(way, Way.find(way.id))
868 assert_relations_are_equal(relation, Relation.find(relation.id))
872 # upload multiple versions of the same element in the same diff.
873 def test_upload_multiple_valid
875 changeset = create(:changeset)
876 basic_authorization changeset.user.email, "test"
878 # change the location of a node multiple times, each time referencing
879 # the last version. doesn't this depend on version numbers being
884 <node id='#{node.id}' lon='0' lat='0' changeset='#{changeset.id}' version='1'/>
885 <node id='#{node.id}' lon='1' lat='0' changeset='#{changeset.id}' version='2'/>
886 <node id='#{node.id}' lon='1' lat='1' changeset='#{changeset.id}' version='3'/>
887 <node id='#{node.id}' lon='1' lat='2' changeset='#{changeset.id}' version='4'/>
888 <node id='#{node.id}' lon='2' lat='2' changeset='#{changeset.id}' version='5'/>
889 <node id='#{node.id}' lon='3' lat='2' changeset='#{changeset.id}' version='6'/>
890 <node id='#{node.id}' lon='3' lat='3' changeset='#{changeset.id}' version='7'/>
891 <node id='#{node.id}' lon='9' lat='9' changeset='#{changeset.id}' version='8'/>
898 post :upload, :id => changeset.id
899 assert_response :success,
900 "can't upload multiple versions of an element in a diff: #{@response.body}"
902 # check the response is well-formed. its counter-intuitive, but the
903 # API will return multiple elements with the same ID and different
904 # version numbers for each change we made.
905 assert_select "diffResult>node", 8
909 # upload multiple versions of the same element in the same diff, but
910 # keep the version numbers the same.
911 def test_upload_multiple_duplicate
913 changeset = create(:changeset)
915 basic_authorization changeset.user.email, "test"
920 <node id='#{node.id}' lon='0' lat='0' changeset='#{changeset.id}' version='1'/>
921 <node id='#{node.id}' lon='1' lat='1' changeset='#{changeset.id}' version='1'/>
928 post :upload, :id => changeset.id
929 assert_response :conflict,
930 "shouldn't be able to upload the same element twice in a diff: #{@response.body}"
934 # try to upload some elements without specifying the version
935 def test_upload_missing_version
936 changeset = create(:changeset)
938 basic_authorization changeset.user.email, "test"
943 <node id='1' lon='1' lat='1' changeset='#{changeset.id}'/>
950 post :upload, :id => changeset.id
951 assert_response :bad_request,
952 "shouldn't be able to upload an element without version: #{@response.body}"
956 # try to upload with commands other than create, modify, or delete
957 def test_action_upload_invalid
958 changeset = create(:changeset)
960 basic_authorization changeset.user.email, "test"
965 <node id='1' lon='1' lat='1' changeset='#{changeset.id}' />
970 post :upload, :id => changeset.id
971 assert_response :bad_request, "Shouldn't be able to upload a diff with the action ping"
972 assert_equal @response.body, "Unknown action ping, choices are create, modify, delete"
976 # upload a valid changeset which has a mixture of whitespace
977 # to check a bug reported by ivansanchez (#1565).
978 def test_upload_whitespace_valid
979 changeset = create(:changeset)
981 way = create(:way_with_nodes, :nodes_count => 2)
982 relation = create(:relation)
983 other_relation = create(:relation)
984 create(:relation_tag, :relation => relation)
986 basic_authorization changeset.user.email, "test"
990 <modify><node id='#{node.id}' lon='0' lat='0' changeset='#{changeset.id}'
992 <node id='#{node.id}' lon='1' lat='1' changeset='#{changeset.id}' version='2'><tag k='k' v='v'/></node></modify>
994 <relation id='#{relation.id}' changeset='#{changeset.id}' version='1'><member
995 type='way' role='some' ref='#{way.id}'/><member
996 type='node' role='some' ref='#{node.id}'/>
997 <member type='relation' role='some' ref='#{other_relation.id}'/>
999 </modify></osmChange>
1004 post :upload, :id => changeset.id
1005 assert_response :success,
1006 "can't upload a valid diff with whitespace variations to changeset: #{@response.body}"
1008 # check the response is well-formed
1009 assert_select "diffResult>node", 2
1010 assert_select "diffResult>relation", 1
1012 # check that the changes made it into the database
1013 assert_equal 1, Node.find(node.id).tags.size, "node #{node.id} should now have one tag"
1014 assert_equal 0, Relation.find(relation.id).tags.size, "relation #{relation.id} should now have no tags"
1018 # test that a placeholder can be reused within the same upload.
1019 def test_upload_reuse_placeholder_valid
1020 changeset = create(:changeset)
1022 basic_authorization changeset.user.email, "test"
1027 <node id='-1' lon='0' lat='0' changeset='#{changeset.id}'>
1028 <tag k="foo" v="bar"/>
1032 <node id='-1' lon='1' lat='1' changeset='#{changeset.id}' version='1'/>
1035 <node id='-1' lon='2' lat='2' changeset='#{changeset.id}' version='2'/>
1042 post :upload, :id => changeset.id
1043 assert_response :success,
1044 "can't upload a valid diff with re-used placeholders to changeset: #{@response.body}"
1046 # check the response is well-formed
1047 assert_select "diffResult>node", 3
1048 assert_select "diffResult>node[old_id='-1']", 3
1052 # test what happens if a diff upload re-uses placeholder IDs in an
1054 def test_upload_placeholder_invalid
1055 changeset = create(:changeset)
1057 basic_authorization changeset.user.email, "test"
1062 <node id='-1' lon='0' lat='0' changeset='#{changeset.id}' version='1'/>
1063 <node id='-1' lon='1' lat='1' changeset='#{changeset.id}' version='1'/>
1064 <node id='-1' lon='2' lat='2' changeset='#{changeset.id}' version='2'/>
1071 post :upload, :id => changeset.id
1072 assert_response :bad_request,
1073 "shouldn't be able to re-use placeholder IDs"
1077 # test that uploading a way referencing invalid placeholders gives a
1078 # proper error, not a 500.
1079 def test_upload_placeholder_invalid_way
1080 changeset = create(:changeset)
1083 basic_authorization changeset.user.email, "test"
1088 <node id="-1" lon="0" lat="0" changeset="#{changeset.id}" version="1"/>
1089 <node id="-2" lon="1" lat="1" changeset="#{changeset.id}" version="1"/>
1090 <node id="-3" lon="2" lat="2" changeset="#{changeset.id}" version="1"/>
1091 <way id="-1" changeset="#{changeset.id}" version="1">
1103 post :upload, :id => changeset.id
1104 assert_response :bad_request,
1105 "shouldn't be able to use invalid placeholder IDs"
1106 assert_equal "Placeholder node not found for reference -4 in way -1", @response.body
1108 # the same again, but this time use an existing way
1112 <node id="-1" lon="0" lat="0" changeset="#{changeset.id}" version="1"/>
1113 <node id="-2" lon="1" lat="1" changeset="#{changeset.id}" version="1"/>
1114 <node id="-3" lon="2" lat="2" changeset="#{changeset.id}" version="1"/>
1115 <way id="#{way.id}" changeset="#{changeset.id}" version="1">
1127 post :upload, :id => changeset.id
1128 assert_response :bad_request,
1129 "shouldn't be able to use invalid placeholder IDs"
1130 assert_equal "Placeholder node not found for reference -4 in way #{way.id}", @response.body
1134 # test that uploading a relation referencing invalid placeholders gives a
1135 # proper error, not a 500.
1136 def test_upload_placeholder_invalid_relation
1137 changeset = create(:changeset)
1138 relation = create(:relation)
1140 basic_authorization changeset.user.email, "test"
1145 <node id="-1" lon="0" lat="0" changeset="#{changeset.id}" version="1"/>
1146 <node id="-2" lon="1" lat="1" changeset="#{changeset.id}" version="1"/>
1147 <node id="-3" lon="2" lat="2" changeset="#{changeset.id}" version="1"/>
1148 <relation id="-1" changeset="#{changeset.id}" version="1">
1149 <member type="node" role="foo" ref="-1"/>
1150 <member type="node" role="foo" ref="-2"/>
1151 <member type="node" role="foo" ref="-3"/>
1152 <member type="node" role="foo" ref="-4"/>
1160 post :upload, :id => changeset.id
1161 assert_response :bad_request,
1162 "shouldn't be able to use invalid placeholder IDs"
1163 assert_equal "Placeholder Node not found for reference -4 in relation -1.", @response.body
1165 # the same again, but this time use an existing relation
1169 <node id="-1" lon="0" lat="0" changeset="#{changeset.id}" version="1"/>
1170 <node id="-2" lon="1" lat="1" changeset="#{changeset.id}" version="1"/>
1171 <node id="-3" lon="2" lat="2" changeset="#{changeset.id}" version="1"/>
1172 <relation id="#{relation.id}" changeset="#{changeset.id}" version="1">
1173 <member type="node" role="foo" ref="-1"/>
1174 <member type="node" role="foo" ref="-2"/>
1175 <member type="node" role="foo" ref="-3"/>
1176 <member type="way" role="bar" ref="-1"/>
1184 post :upload, :id => changeset.id
1185 assert_response :bad_request,
1186 "shouldn't be able to use invalid placeholder IDs"
1187 assert_equal "Placeholder Way not found for reference -1 in relation #{relation.id}.", @response.body
1191 # test what happens if a diff is uploaded containing only a node
1193 def test_upload_node_move
1194 basic_authorization create(:user).email, "test"
1196 content "<osm><changeset>" +
1197 "<tag k='created_by' v='osm test suite checking changesets'/>" +
1198 "</changeset></osm>"
1200 assert_response :success
1201 changeset_id = @response.body.to_i
1203 old_node = create(:node, :lat => 1, :lon => 1)
1205 diff = XML::Document.new
1206 diff.root = XML::Node.new "osmChange"
1207 modify = XML::Node.new "modify"
1208 xml_old_node = old_node.to_xml_node
1209 xml_old_node["lat"] = 2.0.to_s
1210 xml_old_node["lon"] = 2.0.to_s
1211 xml_old_node["changeset"] = changeset_id.to_s
1212 modify << xml_old_node
1217 post :upload, :id => changeset_id
1218 assert_response :success,
1219 "diff should have uploaded OK"
1222 changeset = Changeset.find(changeset_id)
1223 assert_equal 1 * GeoRecord::SCALE, changeset.min_lon, "min_lon should be 1 degree"
1224 assert_equal 2 * GeoRecord::SCALE, changeset.max_lon, "max_lon should be 2 degrees"
1225 assert_equal 1 * GeoRecord::SCALE, changeset.min_lat, "min_lat should be 1 degree"
1226 assert_equal 2 * GeoRecord::SCALE, changeset.max_lat, "max_lat should be 2 degrees"
1230 # test what happens if a diff is uploaded adding a node to a way.
1231 def test_upload_way_extend
1232 basic_authorization create(:user).email, "test"
1234 content "<osm><changeset>" +
1235 "<tag k='created_by' v='osm test suite checking changesets'/>" +
1236 "</changeset></osm>"
1238 assert_response :success
1239 changeset_id = @response.body.to_i
1241 old_way = create(:way)
1242 create(:way_node, :way => old_way, :node => create(:node, :lat => 1, :lon => 1))
1244 diff = XML::Document.new
1245 diff.root = XML::Node.new "osmChange"
1246 modify = XML::Node.new "modify"
1247 xml_old_way = old_way.to_xml_node
1248 nd_ref = XML::Node.new "nd"
1249 nd_ref["ref"] = create(:node, :lat => 3, :lon => 3).id.to_s
1250 xml_old_way << nd_ref
1251 xml_old_way["changeset"] = changeset_id.to_s
1252 modify << xml_old_way
1257 post :upload, :id => changeset_id
1258 assert_response :success,
1259 "diff should have uploaded OK"
1262 changeset = Changeset.find(changeset_id)
1263 assert_equal 1 * GeoRecord::SCALE, changeset.min_lon, "min_lon should be 1 degree"
1264 assert_equal 3 * GeoRecord::SCALE, changeset.max_lon, "max_lon should be 3 degrees"
1265 assert_equal 1 * GeoRecord::SCALE, changeset.min_lat, "min_lat should be 1 degree"
1266 assert_equal 3 * GeoRecord::SCALE, changeset.max_lat, "max_lat should be 3 degrees"
1270 # test for more issues in #1568
1271 def test_upload_empty_invalid
1272 changeset = create(:changeset)
1274 basic_authorization changeset.user.email, "test"
1277 "<osmChange></osmChange>",
1278 "<osmChange><modify/></osmChange>",
1279 "<osmChange><modify></modify></osmChange>"].each do |diff|
1282 post :upload, :id => changeset.id
1283 assert_response(:success, "should be able to upload " +
1284 "empty changeset: " + diff)
1289 # test that the X-Error-Format header works to request XML errors
1290 def test_upload_xml_errors
1291 changeset = create(:changeset)
1292 node = create(:node)
1293 create(:relation_member, :member => node)
1295 basic_authorization changeset.user.email, "test"
1297 # try and delete a node that is in use
1298 diff = XML::Document.new
1299 diff.root = XML::Node.new "osmChange"
1300 delete = XML::Node.new "delete"
1302 delete << node.to_xml_node
1307 post :upload, :id => changeset.id
1308 assert_response :success,
1309 "failed to return error in XML format"
1311 # check the returned payload
1312 assert_select "osmError[version='#{API_VERSION}'][generator='OpenStreetMap server']", 1
1313 assert_select "osmError>status", 1
1314 assert_select "osmError>message", 1
1318 # when we make some simple changes we get the same changes back from the
1320 def test_diff_download_simple
1321 node = create(:node)
1323 ## First try with a non-public user, which should get a forbidden
1324 basic_authorization(create(:user, :data_public => false).email, "test")
1326 # create a temporary changeset
1327 content "<osm><changeset>" +
1328 "<tag k='created_by' v='osm test suite checking changesets'/>" +
1329 "</changeset></osm>"
1331 assert_response :forbidden
1333 ## Now try with a normal user
1334 basic_authorization(create(:user).email, "test")
1336 # create a temporary changeset
1337 content "<osm><changeset>" +
1338 "<tag k='created_by' v='osm test suite checking changesets'/>" +
1339 "</changeset></osm>"
1341 assert_response :success
1342 changeset_id = @response.body.to_i
1348 <node id='#{node.id}' lon='0' lat='0' changeset='#{changeset_id}' version='1'/>
1349 <node id='#{node.id}' lon='1' lat='0' changeset='#{changeset_id}' version='2'/>
1350 <node id='#{node.id}' lon='1' lat='1' changeset='#{changeset_id}' version='3'/>
1351 <node id='#{node.id}' lon='1' lat='2' changeset='#{changeset_id}' version='4'/>
1352 <node id='#{node.id}' lon='2' lat='2' changeset='#{changeset_id}' version='5'/>
1353 <node id='#{node.id}' lon='3' lat='2' changeset='#{changeset_id}' version='6'/>
1354 <node id='#{node.id}' lon='3' lat='3' changeset='#{changeset_id}' version='7'/>
1355 <node id='#{node.id}' lon='9' lat='9' changeset='#{changeset_id}' version='8'/>
1362 post :upload, :id => changeset_id
1363 assert_response :success,
1364 "can't upload multiple versions of an element in a diff: #{@response.body}"
1366 get :download, :id => changeset_id
1367 assert_response :success
1369 assert_select "osmChange", 1
1370 assert_select "osmChange>modify", 8
1371 assert_select "osmChange>modify>node", 8
1375 # culled this from josm to ensure that nothing in the way that josm
1376 # is formatting the request is causing it to fail.
1378 # NOTE: the error turned out to be something else completely!
1379 def test_josm_upload
1380 basic_authorization(create(:user).email, "test")
1382 # create a temporary changeset
1383 content "<osm><changeset>" +
1384 "<tag k='created_by' v='osm test suite checking changesets'/>" +
1385 "</changeset></osm>"
1387 assert_response :success
1388 changeset_id = @response.body.to_i
1391 <osmChange version="0.6" generator="JOSM">
1392 <create version="0.6" generator="JOSM">
1393 <node id='-1' visible='true' changeset='#{changeset_id}' lat='51.49619982187321' lon='-0.18722061869438314' />
1394 <node id='-2' visible='true' changeset='#{changeset_id}' lat='51.496359883909605' lon='-0.18653093576241928' />
1395 <node id='-3' visible='true' changeset='#{changeset_id}' lat='51.49598132358285' lon='-0.18719613290981638' />
1396 <node id='-4' visible='true' changeset='#{changeset_id}' lat='51.4961591711078' lon='-0.18629015888084607' />
1397 <node id='-5' visible='true' changeset='#{changeset_id}' lat='51.49582126021711' lon='-0.18708186591517145' />
1398 <node id='-6' visible='true' changeset='#{changeset_id}' lat='51.49591018437858' lon='-0.1861432441734455' />
1399 <node id='-7' visible='true' changeset='#{changeset_id}' lat='51.49560784152179' lon='-0.18694719410005425' />
1400 <node id='-8' visible='true' changeset='#{changeset_id}' lat='51.49567389979617' lon='-0.1860289771788006' />
1401 <node id='-9' visible='true' changeset='#{changeset_id}' lat='51.49543761398892' lon='-0.186820684213126' />
1402 <way id='-10' action='modiy' visible='true' changeset='#{changeset_id}'>
1412 <tag k='highway' v='residential' />
1413 <tag k='name' v='Foobar Street' />
1421 post :upload, :id => changeset_id
1422 assert_response :success,
1423 "can't upload a diff from JOSM: #{@response.body}"
1425 get :download, :id => changeset_id
1426 assert_response :success
1428 assert_select "osmChange", 1
1429 assert_select "osmChange>create>node", 9
1430 assert_select "osmChange>create>way", 1
1431 assert_select "osmChange>create>way>nd", 9
1432 assert_select "osmChange>create>way>tag", 2
1436 # when we make some complex changes we get the same changes back from the
1438 def test_diff_download_complex
1439 node = create(:node)
1440 node2 = create(:node)
1442 basic_authorization(create(:user).email, "test")
1444 # create a temporary changeset
1445 content "<osm><changeset>" +
1446 "<tag k='created_by' v='osm test suite checking changesets'/>" +
1447 "</changeset></osm>"
1449 assert_response :success
1450 changeset_id = @response.body.to_i
1456 <node id='#{node.id}' lon='0' lat='0' changeset='#{changeset_id}' version='1'/>
1459 <node id='-1' lon='9' lat='9' changeset='#{changeset_id}' version='0'/>
1460 <node id='-2' lon='8' lat='9' changeset='#{changeset_id}' version='0'/>
1461 <node id='-3' lon='7' lat='9' changeset='#{changeset_id}' version='0'/>
1464 <node id='#{node2.id}' lon='20' lat='15' changeset='#{changeset_id}' version='1'/>
1465 <way id='#{way.id}' changeset='#{changeset_id}' version='1'>
1466 <nd ref='#{node2.id}'/>
1477 post :upload, :id => changeset_id
1478 assert_response :success,
1479 "can't upload multiple versions of an element in a diff: #{@response.body}"
1481 get :download, :id => changeset_id
1482 assert_response :success
1484 assert_select "osmChange", 1
1485 assert_select "osmChange>create", 3
1486 assert_select "osmChange>delete", 1
1487 assert_select "osmChange>modify", 2
1488 assert_select "osmChange>create>node", 3
1489 assert_select "osmChange>delete>node", 1
1490 assert_select "osmChange>modify>node", 1
1491 assert_select "osmChange>modify>way", 1
1494 def test_changeset_download
1495 changeset = create(:changeset)
1496 node = create(:node, :with_history, :version => 1, :changeset => changeset)
1497 tag = create(:old_node_tag, :old_node => node.old_nodes.find_by(:version => 1))
1498 node2 = create(:node, :with_history, :version => 1, :changeset => changeset)
1499 _node3 = create(:node, :with_history, :deleted, :version => 1, :changeset => changeset)
1500 _relation = create(:relation, :with_history, :version => 1, :changeset => changeset)
1501 _relation2 = create(:relation, :with_history, :deleted, :version => 1, :changeset => changeset)
1503 get :download, :id => changeset.id
1505 assert_response :success
1507 # print @response.body
1508 # FIXME: needs more assert_select tests
1509 assert_select "osmChange[version='#{API_VERSION}'][generator='#{GENERATOR}']" do
1510 assert_select "create", :count => 5
1511 assert_select "create>node[id='#{node.id}'][visible='#{node.visible?}'][version='#{node.version}']" do
1512 assert_select "tag[k='#{tag.k}'][v='#{tag.v}']"
1514 assert_select "create>node[id='#{node2.id}']"
1519 # check that the bounding box of a changeset gets updated correctly
1520 # FIXME: This should really be moded to a integration test due to the with_controller
1521 def test_changeset_bbox
1523 create(:way_node, :way => way, :node => create(:node, :lat => 3, :lon => 3))
1525 basic_authorization create(:user).email, "test"
1527 # create a new changeset
1528 content "<osm><changeset/></osm>"
1530 assert_response :success, "Creating of changeset failed."
1531 changeset_id = @response.body.to_i
1533 # add a single node to it
1534 with_controller(NodeController.new) do
1535 content "<osm><node lon='1' lat='2' changeset='#{changeset_id}'/></osm>"
1537 assert_response :success, "Couldn't create node."
1540 # get the bounding box back from the changeset
1541 get :read, :id => changeset_id
1542 assert_response :success, "Couldn't read back changeset."
1543 assert_select "osm>changeset[min_lon='1.0']", 1
1544 assert_select "osm>changeset[max_lon='1.0']", 1
1545 assert_select "osm>changeset[min_lat='2.0']", 1
1546 assert_select "osm>changeset[max_lat='2.0']", 1
1548 # add another node to it
1549 with_controller(NodeController.new) do
1550 content "<osm><node lon='2' lat='1' changeset='#{changeset_id}'/></osm>"
1552 assert_response :success, "Couldn't create second node."
1555 # get the bounding box back from the changeset
1556 get :read, :id => changeset_id
1557 assert_response :success, "Couldn't read back changeset for the second time."
1558 assert_select "osm>changeset[min_lon='1.0']", 1
1559 assert_select "osm>changeset[max_lon='2.0']", 1
1560 assert_select "osm>changeset[min_lat='1.0']", 1
1561 assert_select "osm>changeset[max_lat='2.0']", 1
1563 # add (delete) a way to it, which contains a point at (3,3)
1564 with_controller(WayController.new) do
1565 content update_changeset(way.to_xml, changeset_id)
1566 put :delete, :id => way.id
1567 assert_response :success, "Couldn't delete a way."
1570 # get the bounding box back from the changeset
1571 get :read, :id => changeset_id
1572 assert_response :success, "Couldn't read back changeset for the third time."
1573 # note that the 3.1 here is because of the bbox overexpansion
1574 assert_select "osm>changeset[min_lon='1.0']", 1
1575 assert_select "osm>changeset[max_lon='3.1']", 1
1576 assert_select "osm>changeset[min_lat='1.0']", 1
1577 assert_select "osm>changeset[max_lat='3.1']", 1
1581 # test that the changeset :include method works as it should
1582 def test_changeset_include
1583 basic_authorization create(:user).display_name, "test"
1585 # create a new changeset
1586 content "<osm><changeset/></osm>"
1588 assert_response :success, "Creating of changeset failed."
1589 changeset_id = @response.body.to_i
1591 # NOTE: the include method doesn't over-expand, like inserting
1592 # a real method does. this is because we expect the client to
1593 # know what it is doing!
1594 check_after_include(changeset_id, 1, 1, [1, 1, 1, 1])
1595 check_after_include(changeset_id, 3, 3, [1, 1, 3, 3])
1596 check_after_include(changeset_id, 4, 2, [1, 1, 4, 3])
1597 check_after_include(changeset_id, 2, 2, [1, 1, 4, 3])
1598 check_after_include(changeset_id, -1, -1, [-1, -1, 4, 3])
1599 check_after_include(changeset_id, -2, 5, [-2, -1, 4, 5])
1603 # test that a not found, wrong method with the expand bbox works as expected
1604 def test_changeset_expand_bbox_error
1605 basic_authorization create(:user).display_name, "test"
1607 # create a new changeset
1608 content "<osm><changeset/></osm>"
1610 assert_response :success, "Creating of changeset failed."
1611 changeset_id = @response.body.to_i
1617 content "<osm><node lon='#{lon}' lat='#{lat}'/></osm>"
1618 put :expand_bbox, :id => changeset_id
1619 assert_response :method_not_allowed, "shouldn't be able to put a bbox expand"
1621 # Try to get the update
1622 content "<osm><node lon='#{lon}' lat='#{lat}'/></osm>"
1623 get :expand_bbox, :id => changeset_id
1624 assert_response :method_not_allowed, "shouldn't be able to get a bbox expand"
1626 # Try to use a hopefully missing changeset
1627 content "<osm><node lon='#{lon}' lat='#{lat}'/></osm>"
1628 post :expand_bbox, :id => changeset_id + 13245
1629 assert_response :not_found, "shouldn't be able to do a bbox expand on a nonexistant changeset"
1633 # test the query functionality of changesets
1635 private_user = create(:user, :data_public => false)
1636 private_user_changeset = create(:changeset, :user => private_user)
1637 private_user_closed_changeset = create(:changeset, :closed, :user => private_user)
1638 user = create(:user)
1639 changeset = create(:changeset, :user => user)
1640 closed_changeset = create(:changeset, :closed, :user => user, :created_at => Time.utc(2008, 1, 1, 0, 0, 0), :closed_at => Time.utc(2008, 1, 2, 0, 0, 0))
1641 changeset2 = create(:changeset, :min_lat => 5 * GeoRecord::SCALE, :min_lon => 5 * GeoRecord::SCALE, :max_lat => 15 * GeoRecord::SCALE, :max_lon => 15 * GeoRecord::SCALE)
1642 changeset3 = create(:changeset, :min_lat => 4.5 * GeoRecord::SCALE, :min_lon => 4.5 * GeoRecord::SCALE, :max_lat => 5 * GeoRecord::SCALE, :max_lon => 5 * GeoRecord::SCALE)
1644 get :query, :bbox => "-10,-10, 10, 10"
1645 assert_response :success, "can't get changesets in bbox"
1646 assert_changesets [changeset2.id, changeset3.id]
1648 get :query, :bbox => "4.5,4.5,4.6,4.6"
1649 assert_response :success, "can't get changesets in bbox"
1650 assert_changesets [changeset3.id]
1652 # not found when looking for changesets of non-existing users
1653 get :query, :user => User.maximum(:id) + 1
1654 assert_response :not_found
1655 get :query, :display_name => " "
1656 assert_response :not_found
1658 # can't get changesets of user 1 without authenticating
1659 get :query, :user => private_user.id
1660 assert_response :not_found, "shouldn't be able to get changesets by non-public user (ID)"
1661 get :query, :display_name => private_user.display_name
1662 assert_response :not_found, "shouldn't be able to get changesets by non-public user (name)"
1664 # but this should work
1665 basic_authorization private_user.email, "test"
1666 get :query, :user => private_user.id
1667 assert_response :success, "can't get changesets by user ID"
1668 assert_changesets [private_user_changeset.id, private_user_closed_changeset.id]
1670 get :query, :display_name => private_user.display_name
1671 assert_response :success, "can't get changesets by user name"
1672 assert_changesets [private_user_changeset.id, private_user_closed_changeset.id]
1674 # check that the correct error is given when we provide both UID and name
1675 get :query, :user => private_user.id, :display_name => private_user.display_name
1676 assert_response :bad_request, "should be a bad request to have both ID and name specified"
1678 get :query, :user => private_user.id, :open => true
1679 assert_response :success, "can't get changesets by user and open"
1680 assert_changesets [private_user_changeset.id]
1682 get :query, :time => "2007-12-31"
1683 assert_response :success, "can't get changesets by time-since"
1684 assert_changesets [private_user_changeset.id, private_user_closed_changeset.id, changeset.id, closed_changeset.id, changeset2.id, changeset3.id]
1686 get :query, :time => "2008-01-01T12:34Z"
1687 assert_response :success, "can't get changesets by time-since with hour"
1688 assert_changesets [private_user_changeset.id, private_user_closed_changeset.id, changeset.id, closed_changeset.id, changeset2.id, changeset3.id]
1690 get :query, :time => "2007-12-31T23:59Z,2008-01-02T00:01Z"
1691 assert_response :success, "can't get changesets by time-range"
1692 assert_changesets [closed_changeset.id]
1694 get :query, :open => "true"
1695 assert_response :success, "can't get changesets by open-ness"
1696 assert_changesets [private_user_changeset.id, changeset.id, changeset2.id, changeset3.id]
1698 get :query, :closed => "true"
1699 assert_response :success, "can't get changesets by closed-ness"
1700 assert_changesets [private_user_closed_changeset.id, closed_changeset.id]
1702 get :query, :closed => "true", :user => private_user.id
1703 assert_response :success, "can't get changesets by closed-ness and user"
1704 assert_changesets [private_user_closed_changeset.id]
1706 get :query, :closed => "true", :user => user.id
1707 assert_response :success, "can't get changesets by closed-ness and user"
1708 assert_changesets [closed_changeset.id]
1710 get :query, :changesets => "#{private_user_changeset.id},#{changeset.id},#{closed_changeset.id}"
1711 assert_response :success, "can't get changesets by id (as comma-separated string)"
1712 assert_changesets [private_user_changeset.id, changeset.id, closed_changeset.id]
1714 get :query, :changesets => ""
1715 assert_response :bad_request, "should be a bad request since changesets is empty"
1719 # check that errors are returned if garbage is inserted
1720 # into query strings
1721 def test_query_invalid
1724 ";drop table users;"].each do |bbox|
1725 get :query, :bbox => bbox
1726 assert_response :bad_request, "'#{bbox}' isn't a bbox"
1731 ";drop table users;",
1733 "-,-"].each do |time|
1734 get :query, :time => time
1735 assert_response :bad_request, "'#{time}' isn't a valid time range"
1742 get :query, :user => uid
1743 assert_response :bad_request, "'#{uid}' isn't a valid user ID"
1748 # check updating tags on a changeset
1749 def test_changeset_update
1750 private_user = create(:user, :data_public => false)
1751 private_changeset = create(:changeset, :user => private_user)
1752 user = create(:user)
1753 changeset = create(:changeset, :user => user)
1755 ## First try with a non-public user
1756 new_changeset = private_changeset.to_xml
1757 new_tag = XML::Node.new "tag"
1758 new_tag["k"] = "tagtesting"
1759 new_tag["v"] = "valuetesting"
1760 new_changeset.find("//osm/changeset").first << new_tag
1761 content new_changeset
1763 # try without any authorization
1764 put :update, :id => private_changeset.id
1765 assert_response :unauthorized
1767 # try with the wrong authorization
1768 basic_authorization create(:user).email, "test"
1769 put :update, :id => private_changeset.id
1770 assert_response :conflict
1772 # now this should get an unauthorized
1773 basic_authorization private_user.email, "test"
1774 put :update, :id => private_changeset.id
1775 assert_require_public_data "user with their data non-public, shouldn't be able to edit their changeset"
1777 ## Now try with the public user
1778 create(:changeset_tag, :changeset => changeset)
1779 new_changeset = changeset.to_xml
1780 new_tag = XML::Node.new "tag"
1781 new_tag["k"] = "tagtesting"
1782 new_tag["v"] = "valuetesting"
1783 new_changeset.find("//osm/changeset").first << new_tag
1784 content new_changeset
1786 # try without any authorization
1787 @request.env["HTTP_AUTHORIZATION"] = nil
1788 put :update, :id => changeset.id
1789 assert_response :unauthorized
1791 # try with the wrong authorization
1792 basic_authorization create(:user).email, "test"
1793 put :update, :id => changeset.id
1794 assert_response :conflict
1796 # now this should work...
1797 basic_authorization user.email, "test"
1798 put :update, :id => changeset.id
1799 assert_response :success
1801 assert_select "osm>changeset[id='#{changeset.id}']", 1
1802 assert_select "osm>changeset>tag", 2
1803 assert_select "osm>changeset>tag[k='tagtesting'][v='valuetesting']", 1
1807 # check that a user different from the one who opened the changeset
1809 def test_changeset_update_invalid
1810 basic_authorization create(:user).email, "test"
1812 changeset = create(:changeset)
1813 new_changeset = changeset.to_xml
1814 new_tag = XML::Node.new "tag"
1815 new_tag["k"] = "testing"
1816 new_tag["v"] = "testing"
1817 new_changeset.find("//osm/changeset").first << new_tag
1819 content new_changeset
1820 put :update, :id => changeset.id
1821 assert_response :conflict
1825 # check that a changeset can contain a certain max number of changes.
1826 ## FIXME should be changed to an integration test due to the with_controller
1827 def test_changeset_limits
1828 basic_authorization create(:user).email, "test"
1830 # open a new changeset
1831 content "<osm><changeset/></osm>"
1833 assert_response :success, "can't create a new changeset"
1834 cs_id = @response.body.to_i
1836 # start the counter just short of where the changeset should finish.
1838 # alter the database to set the counter on the changeset directly,
1839 # otherwise it takes about 6 minutes to fill all of them.
1840 changeset = Changeset.find(cs_id)
1841 changeset.num_changes = Changeset::MAX_ELEMENTS - offset
1844 with_controller(NodeController.new) do
1846 content "<osm><node changeset='#{cs_id}' lat='0.0' lon='0.0'/></osm>"
1848 assert_response :success, "can't create a new node"
1849 node_id = @response.body.to_i
1851 get :read, :id => node_id
1852 assert_response :success, "can't read back new node"
1853 node_doc = XML::Parser.string(@response.body).parse
1854 node_xml = node_doc.find("//osm/node").first
1856 # loop until we fill the changeset with nodes
1858 node_xml["lat"] = rand.to_s
1859 node_xml["lon"] = rand.to_s
1860 node_xml["version"] = (i + 1).to_s
1863 put :update, :id => node_id
1864 assert_response :success, "attempt #{i} should have succeeded"
1867 # trying again should fail
1868 node_xml["lat"] = rand.to_s
1869 node_xml["lon"] = rand.to_s
1870 node_xml["version"] = offset.to_s
1873 put :update, :id => node_id
1874 assert_response :conflict, "final attempt should have failed"
1877 changeset = Changeset.find(cs_id)
1878 assert_equal Changeset::MAX_ELEMENTS + 1, changeset.num_changes
1880 # check that the changeset is now closed as well
1881 assert(!changeset.is_open?,
1882 "changeset should have been auto-closed by exceeding " +
1887 # This should display the last 20 changesets closed
1889 get :list, :format => "html"
1890 assert_response :success
1891 assert_template "history"
1892 assert_template :layout => "map"
1893 assert_select "h2", :text => "Changesets", :count => 1
1895 xhr :get, :list, :format => "html", :list => "1"
1896 assert_response :success
1897 assert_template "list"
1899 check_list_result(Changeset.all)
1903 # This should display the last 20 changesets closed
1905 xhr :get, :list, :format => "html"
1906 assert_response :success
1907 assert_template "history"
1908 assert_template :layout => "xhr"
1909 assert_select "h2", :text => "Changesets", :count => 1
1911 xhr :get, :list, :format => "html", :list => "1"
1912 assert_response :success
1913 assert_template "list"
1915 check_list_result(Changeset.all)
1919 # This should display the last 20 changesets closed in a specific area
1921 get :list, :format => "html", :bbox => "4.5,4.5,5.5,5.5"
1922 assert_response :success
1923 assert_template "history"
1924 assert_template :layout => "map"
1925 assert_select "h2", :text => "Changesets", :count => 1
1927 xhr :get, :list, :format => "html", :bbox => "4.5,4.5,5.5,5.5", :list => "1"
1928 assert_response :success
1929 assert_template "list"
1931 check_list_result(Changeset.where("min_lon < 55000000 and max_lon > 45000000 and min_lat < 55000000 and max_lat > 45000000"))
1935 # Checks the display of the user changesets listing
1937 user = create(:user)
1938 create(:changeset, :user => user)
1939 create(:changeset, :closed, :user => user)
1941 get :list, :format => "html", :display_name => user.display_name
1942 assert_response :success
1943 assert_template "history"
1945 xhr :get, :list, :format => "html", :display_name => user.display_name, :list => "1"
1946 assert_response :success
1947 assert_template "list"
1949 check_list_result(user.changesets)
1953 # Checks the display of the user changesets listing for a private user
1954 def test_list_private_user
1955 private_user = create(:user, :data_public => false)
1956 create(:changeset, :user => private_user)
1957 create(:changeset, :closed, :user => private_user)
1959 get :list, :format => "html", :display_name => private_user.display_name
1960 assert_response :success
1961 assert_template "history"
1963 xhr :get, :list, :format => "html", :display_name => private_user.display_name, :list => "1"
1964 assert_response :success
1965 assert_template "list"
1967 check_list_result(Changeset.none)
1971 # Check the not found of the list user changesets
1972 def test_list_user_not_found
1973 get :list, :format => "html", :display_name => "Some random user"
1974 assert_response :not_found
1975 assert_template "user/no_such_user"
1977 xhr :get, :list, :format => "html", :display_name => "Some random user", :list => "1"
1978 assert_response :not_found
1979 assert_template "user/no_such_user"
1983 # Checks the display of the friends changesets listing
1984 def test_list_friends
1985 private_user = create(:user, :data_public => true)
1986 friend = create(:friend, :befriender => private_user)
1987 create(:changeset, :user => friend.befriendee)
1989 get :list, :friends => true
1990 assert_response :redirect
1991 assert_redirected_to :controller => :user, :action => :login, :referer => friend_changesets_path
1993 session[:user] = private_user.id
1995 get :list, :friends => true
1996 assert_response :success
1997 assert_template "history"
1999 xhr :get, :list, :friends => true, :list => "1"
2000 assert_response :success
2001 assert_template "list"
2003 check_list_result(Changeset.where(:user => private_user.friend_users.identifiable))
2007 # Checks the display of the nearby user changesets listing
2008 def test_list_nearby
2009 private_user = create(:user, :data_public => false, :home_lat => 51.1, :home_lon => 1.0)
2010 user = create(:user, :home_lat => 51.0, :home_lon => 1.0)
2011 create(:changeset, :user => user)
2013 get :list, :nearby => true
2014 assert_response :redirect
2015 assert_redirected_to :controller => :user, :action => :login, :referer => nearby_changesets_path
2017 session[:user] = private_user.id
2019 get :list, :nearby => true
2020 assert_response :success
2021 assert_template "history"
2023 xhr :get, :list, :nearby => true, :list => "1"
2024 assert_response :success
2025 assert_template "list"
2027 check_list_result(Changeset.where(:user => user.nearby))
2031 # Check that we can't request later pages of the changesets list
2032 def test_list_max_id
2033 xhr :get, :list, :format => "html", :max_id => 4
2034 assert_response :success
2035 assert_template "history"
2036 assert_template :layout => "xhr"
2037 assert_select "h2", :text => "Changesets", :count => 1
2039 xhr :get, :list, :format => "html", :list => "1", :max_id => 4
2040 assert_response :success
2041 assert_template "list"
2043 check_list_result(Changeset.where("id <= 4"))
2047 # This should display the last 20 changesets closed
2049 changeset = create(:changeset, :num_changes => 1)
2050 _empty_changeset = create(:changeset, :num_changes => 0)
2052 get :feed, :format => :atom
2053 assert_response :success
2054 assert_template "list"
2055 assert_equal "application/atom+xml", response.content_type
2057 check_feed_result([changeset])
2061 # This should display the last 20 changesets closed in a specific area
2063 changeset = create(:changeset, :num_changes => 1, :min_lat => 5 * GeoRecord::SCALE, :min_lon => 5 * GeoRecord::SCALE, :max_lat => 5 * GeoRecord::SCALE, :max_lon => 5 * GeoRecord::SCALE)
2064 closed_changeset = create(:changeset, :closed, :num_changes => 1, :min_lat => 5 * GeoRecord::SCALE, :min_lon => 5 * GeoRecord::SCALE, :max_lat => 5 * GeoRecord::SCALE, :max_lon => 5 * GeoRecord::SCALE)
2065 _elsewhere_changeset = create(:changeset, :num_changes => 1, :min_lat => -5 * GeoRecord::SCALE, :min_lon => -5 * GeoRecord::SCALE, :max_lat => -5 * GeoRecord::SCALE, :max_lon => -5 * GeoRecord::SCALE)
2066 _empty_changeset = create(:changeset, :num_changes => 0, :min_lat => -5 * GeoRecord::SCALE, :min_lon => -5 * GeoRecord::SCALE, :max_lat => -5 * GeoRecord::SCALE, :max_lon => -5 * GeoRecord::SCALE)
2068 get :feed, :format => :atom, :bbox => "4.5,4.5,5.5,5.5"
2069 assert_response :success
2070 assert_template "list"
2071 assert_equal "application/atom+xml", response.content_type
2073 check_feed_result([changeset, closed_changeset])
2077 # Checks the display of the user changesets feed
2079 user = create(:user)
2080 changesets = create_list(:changeset, 3, :user => user, :num_changes => 4)
2081 _other_changeset = create(:changeset)
2083 get :feed, :format => :atom, :display_name => user.display_name
2084 assert_response :success
2085 assert_template "list"
2086 assert_equal "application/atom+xml", response.content_type
2088 check_feed_result(changesets)
2092 # Check the not found of the user changesets feed
2093 def test_feed_user_not_found
2094 get :feed, :format => "atom", :display_name => "Some random user"
2095 assert_response :not_found
2099 # Check that we can't request later pages of the changesets feed
2100 def test_feed_max_id
2101 get :feed, :format => "atom", :max_id => 100
2102 assert_response :redirect
2103 assert_redirected_to :action => :feed
2107 # check that the changeset download for a changeset with a redacted
2108 # element in it doesn't contain that element.
2109 def test_diff_download_redacted
2110 changeset = create(:changeset)
2111 node = create(:node, :with_history, :version => 2, :changeset => changeset)
2112 node_v1 = node.old_nodes.find_by(:version => 1)
2113 node_v1.redact!(create(:redaction))
2115 get :download, :id => changeset.id
2116 assert_response :success
2118 assert_select "osmChange", 1
2119 # this changeset contains the node in versions 1 & 2, but 1 should
2121 assert_select "osmChange node[id='#{node.id}']", 1
2122 assert_select "osmChange node[id='#{node.id}'][version='1']", 0
2126 # create comment success
2127 def test_create_comment_success
2128 user = create(:user)
2129 user2 = create(:user)
2130 private_user = create(:user, :data_public => false)
2131 suspended_user = create(:user, :suspended)
2132 deleted_user = create(:user, :deleted)
2133 private_user_closed_changeset = create(:changeset, :closed, :user => private_user)
2135 basic_authorization(user.email, "test")
2137 assert_difference "ChangesetComment.count", 1 do
2138 assert_no_difference "ActionMailer::Base.deliveries.size" do
2139 post :comment, :id => private_user_closed_changeset.id, :text => "This is a comment"
2142 assert_response :success
2144 changeset = create(:changeset, :closed, :user => private_user)
2145 changeset.subscribers.push(private_user)
2146 changeset.subscribers.push(user)
2147 changeset.subscribers.push(suspended_user)
2148 changeset.subscribers.push(deleted_user)
2150 assert_difference "ChangesetComment.count", 1 do
2151 assert_difference "ActionMailer::Base.deliveries.size", 1 do
2152 post :comment, :id => changeset.id, :text => "This is a comment"
2155 assert_response :success
2157 email = ActionMailer::Base.deliveries.first
2158 assert_equal 1, email.to.length
2159 assert_equal "[OpenStreetMap] #{user.display_name} has commented on one of your changesets", email.subject
2160 assert_equal private_user.email, email.to.first
2162 ActionMailer::Base.deliveries.clear
2164 basic_authorization(user2.email, "test")
2166 assert_difference "ChangesetComment.count", 1 do
2167 assert_difference "ActionMailer::Base.deliveries.size", 2 do
2168 post :comment, :id => changeset.id, :text => "This is a comment"
2171 assert_response :success
2173 email = ActionMailer::Base.deliveries.find { |e| e.to.first == private_user.email }
2174 assert_not_nil email
2175 assert_equal 1, email.to.length
2176 assert_equal "[OpenStreetMap] #{user2.display_name} has commented on one of your changesets", email.subject
2178 email = ActionMailer::Base.deliveries.find { |e| e.to.first == user.email }
2179 assert_not_nil email
2180 assert_equal 1, email.to.length
2181 assert_equal "[OpenStreetMap] #{user2.display_name} has commented on a changeset you are interested in", email.subject
2183 ActionMailer::Base.deliveries.clear
2187 # create comment fail
2188 def test_create_comment_fail
2190 post :comment, :id => create(:changeset, :closed).id, :text => "This is a comment"
2191 assert_response :unauthorized
2193 basic_authorization(create(:user).email, "test")
2196 assert_no_difference "ChangesetComment.count" do
2197 post :comment, :id => 999111, :text => "This is a comment"
2199 assert_response :not_found
2201 # not closed changeset
2202 assert_no_difference "ChangesetComment.count" do
2203 post :comment, :id => create(:changeset).id, :text => "This is a comment"
2205 assert_response :conflict
2208 assert_no_difference "ChangesetComment.count" do
2209 post :comment, :id => create(:changeset, :closed).id
2211 assert_response :bad_request
2214 assert_no_difference "ChangesetComment.count" do
2215 post :comment, :id => create(:changeset, :closed).id, :text => ""
2217 assert_response :bad_request
2221 # test subscribe success
2222 def test_subscribe_success
2223 basic_authorization(create(:user).email, "test")
2224 changeset = create(:changeset, :closed)
2226 assert_difference "changeset.subscribers.count", 1 do
2227 post :subscribe, :id => changeset.id
2229 assert_response :success
2233 # test subscribe fail
2234 def test_subscribe_fail
2235 user = create(:user)
2238 changeset = create(:changeset, :closed)
2239 assert_no_difference "changeset.subscribers.count" do
2240 post :subscribe, :id => changeset.id
2242 assert_response :unauthorized
2244 basic_authorization(user.email, "test")
2247 assert_no_difference "changeset.subscribers.count" do
2248 post :subscribe, :id => 999111
2250 assert_response :not_found
2252 # not closed changeset
2253 changeset = create(:changeset)
2254 assert_no_difference "changeset.subscribers.count" do
2255 post :subscribe, :id => changeset.id
2257 assert_response :conflict
2259 # trying to subscribe when already subscribed
2260 changeset = create(:changeset, :closed)
2261 changeset.subscribers.push(user)
2262 assert_no_difference "changeset.subscribers.count" do
2263 post :subscribe, :id => changeset.id
2265 assert_response :conflict
2269 # test unsubscribe success
2270 def test_unsubscribe_success
2271 user = create(:user)
2272 basic_authorization(user.email, "test")
2273 changeset = create(:changeset, :closed)
2274 changeset.subscribers.push(user)
2276 assert_difference "changeset.subscribers.count", -1 do
2277 post :unsubscribe, :id => changeset.id
2279 assert_response :success
2283 # test unsubscribe fail
2284 def test_unsubscribe_fail
2286 changeset = create(:changeset, :closed)
2287 assert_no_difference "changeset.subscribers.count" do
2288 post :unsubscribe, :id => changeset.id
2290 assert_response :unauthorized
2292 basic_authorization(create(:user).email, "test")
2295 assert_no_difference "changeset.subscribers.count" do
2296 post :unsubscribe, :id => 999111
2298 assert_response :not_found
2300 # not closed changeset
2301 changeset = create(:changeset)
2302 assert_no_difference "changeset.subscribers.count" do
2303 post :unsubscribe, :id => changeset.id
2305 assert_response :conflict
2307 # trying to unsubscribe when not subscribed
2308 changeset = create(:changeset, :closed)
2309 assert_no_difference "changeset.subscribers.count" do
2310 post :unsubscribe, :id => changeset.id
2312 assert_response :not_found
2316 # test hide comment fail
2317 def test_hide_comment_fail
2319 comment = create(:changeset_comment)
2320 assert_equal true, comment.visible
2322 post :hide_comment, :id => comment.id
2323 assert_response :unauthorized
2324 assert_equal true, comment.reload.visible
2326 basic_authorization(create(:user).email, "test")
2329 post :hide_comment, :id => comment.id
2330 assert_response :forbidden
2331 assert_equal true, comment.reload.visible
2333 basic_authorization(create(:moderator_user).email, "test")
2336 post :hide_comment, :id => 999111
2337 assert_response :not_found
2338 assert_equal true, comment.reload.visible
2342 # test hide comment succes
2343 def test_hide_comment_success
2344 comment = create(:changeset_comment)
2345 assert_equal true, comment.visible
2347 basic_authorization(create(:moderator_user).email, "test")
2349 post :hide_comment, :id => comment.id
2350 assert_response :success
2351 assert_equal false, comment.reload.visible
2355 # test unhide comment fail
2356 def test_unhide_comment_fail
2358 comment = create(:changeset_comment, :visible => false)
2359 assert_equal false, comment.visible
2361 post :unhide_comment, :id => comment.id
2362 assert_response :unauthorized
2363 assert_equal false, comment.reload.visible
2365 basic_authorization(create(:user).email, "test")
2368 post :unhide_comment, :id => comment.id
2369 assert_response :forbidden
2370 assert_equal false, comment.reload.visible
2372 basic_authorization(create(:moderator_user).email, "test")
2375 post :unhide_comment, :id => 999111
2376 assert_response :not_found
2377 assert_equal false, comment.reload.visible
2381 # test unhide comment succes
2382 def test_unhide_comment_success
2383 comment = create(:changeset_comment, :visible => false)
2384 assert_equal false, comment.visible
2386 basic_authorization(create(:moderator_user).email, "test")
2388 post :unhide_comment, :id => comment.id
2389 assert_response :success
2390 assert_equal true, comment.reload.visible
2394 # test comments feed
2395 def test_comments_feed
2396 changeset = create(:changeset, :closed)
2397 create_list(:changeset_comment, 3, :changeset => changeset)
2399 get :comments_feed, :format => "rss"
2400 assert_response :success
2401 assert_equal "application/rss+xml", @response.content_type
2402 assert_select "rss", :count => 1 do
2403 assert_select "channel", :count => 1 do
2404 assert_select "item", :count => 3
2408 get :comments_feed, :format => "rss", :limit => 2
2409 assert_response :success
2410 assert_equal "application/rss+xml", @response.content_type
2411 assert_select "rss", :count => 1 do
2412 assert_select "channel", :count => 1 do
2413 assert_select "item", :count => 2
2417 get :comments_feed, :id => changeset.id, :format => "rss"
2418 assert_response :success
2419 assert_equal "application/rss+xml", @response.content_type
2420 assert_select "rss", :count => 1 do
2421 assert_select "channel", :count => 1 do
2422 assert_select "item", :count => 3
2428 # test comments feed
2429 def test_comments_feed_bad_limit
2430 get :comments_feed, :format => "rss", :limit => 0
2431 assert_response :bad_request
2433 get :comments_feed, :format => "rss", :limit => 100001
2434 assert_response :bad_request
2440 # boilerplate for checking that certain changesets exist in the
2442 def assert_changesets(ids)
2443 assert_select "osm>changeset", ids.size
2445 assert_select "osm>changeset[id='#{id}']", 1
2450 # call the include method and assert properties of the bbox
2451 def check_after_include(changeset_id, lon, lat, bbox)
2452 content "<osm><node lon='#{lon}' lat='#{lat}'/></osm>"
2453 post :expand_bbox, :id => changeset_id
2454 assert_response :success, "Setting include of changeset failed: #{@response.body}"
2456 # check exactly one changeset
2457 assert_select "osm>changeset", 1
2458 assert_select "osm>changeset[id='#{changeset_id}']", 1
2461 doc = XML::Parser.string(@response.body).parse
2462 changeset = doc.find("//osm/changeset").first
2463 assert_equal bbox[0], changeset["min_lon"].to_f, "min lon"
2464 assert_equal bbox[1], changeset["min_lat"].to_f, "min lat"
2465 assert_equal bbox[2], changeset["max_lon"].to_f, "max lon"
2466 assert_equal bbox[3], changeset["max_lat"].to_f, "max lat"
2470 # update the changeset_id of a way element
2471 def update_changeset(xml, changeset_id)
2472 xml_attr_rewrite(xml, "changeset", changeset_id)
2476 # update an attribute in a way element
2477 def xml_attr_rewrite(xml, name, value)
2478 xml.find("//osm/way").first[name] = value.to_s
2483 # check the result of a list
2484 def check_list_result(changesets)
2485 changesets = changesets.where("num_changes > 0")
2486 .order(:created_at => :desc)
2488 assert changesets.size <= 20
2490 assert_select "ol.changesets", :count => [changesets.size, 1].min do
2491 assert_select "li", :count => changesets.size
2493 changesets.each do |changeset|
2494 assert_select "li#changeset_#{changeset.id}", :count => 1
2500 # check the result of a feed
2501 def check_feed_result(changesets)
2502 assert changesets.size <= 20
2504 assert_select "feed", :count => [changesets.size, 1].min do
2505 assert_select "> title", :count => 1, :text => /^Changesets/
2506 assert_select "> entry", :count => changesets.size
2508 changesets.each do |changeset|
2509 assert_select "> entry > id", changeset_url(:id => changeset.id)