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
330 diff = <<EOF.strip_heredoc
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
360 diff = <<EOF.strip_heredoc
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
390 diff = <<EOF.strip_heredoc
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
432 diff = <<EOF.strip_heredoc
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.
585 diff = <<EOF.strip_heredoc
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
742 diff = <<EOF.strip_heredoc
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
773 diff = <<EOF.strip_heredoc
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
835 diff = <<EOF.strip_heredoc
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
881 diff = <<EOF.strip_heredoc
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"
917 diff = <<EOF.strip_heredoc
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"
940 diff = <<EOF.strip_heredoc
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"
962 diff = <<EOF.strip_heredoc
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"
988 diff = <<EOF.strip_heredoc
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"
1024 diff = <<EOF.strip_heredoc
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"
1059 diff = <<EOF.strip_heredoc
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"
1085 diff = <<EOF.strip_heredoc
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
1109 diff = <<EOF.strip_heredoc
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"
1142 diff = <<EOF.strip_heredoc
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
1166 diff = <<EOF.strip_heredoc
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
1345 diff = <<EOF.strip_heredoc
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
1390 diff = <<OSMFILE.strip_heredoc
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
1453 diff = <<EOF.strip_heredoc
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, changeset3]
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]
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, private_user_closed_changeset]
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, private_user_closed_changeset]
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]
1682 get :query, :time => "2007-12-31"
1683 assert_response :success, "can't get changesets by time-since"
1684 assert_changesets [private_user_changeset, private_user_closed_changeset, changeset, closed_changeset, changeset2, changeset3]
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, private_user_closed_changeset, changeset, closed_changeset, changeset2, changeset3]
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]
1694 get :query, :open => "true"
1695 assert_response :success, "can't get changesets by open-ness"
1696 assert_changesets [private_user_changeset, changeset, changeset2, changeset3]
1698 get :query, :closed => "true"
1699 assert_response :success, "can't get changesets by closed-ness"
1700 assert_changesets [private_user_closed_changeset, closed_changeset]
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]
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]
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, changeset, closed_changeset]
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 non-empty changesets
2049 changeset = create(:changeset, :num_changes => 1)
2050 closed_changeset = create(:changeset, :closed, :num_changes => 1)
2051 _empty_changeset = create(:changeset, :num_changes => 0)
2053 get :feed, :format => :atom
2054 assert_response :success
2055 assert_template "list"
2056 assert_equal "application/atom+xml", response.content_type
2058 check_feed_result([changeset, closed_changeset])
2062 # This should display the last 20 changesets closed in a specific area
2064 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)
2065 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)
2066 _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)
2067 _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)
2069 get :feed, :format => :atom, :bbox => "4.5,4.5,5.5,5.5"
2070 assert_response :success
2071 assert_template "list"
2072 assert_equal "application/atom+xml", response.content_type
2074 check_feed_result([changeset, closed_changeset])
2078 # Checks the display of the user changesets feed
2080 user = create(:user)
2081 changesets = create_list(:changeset, 3, :user => user, :num_changes => 4)
2082 _other_changeset = create(:changeset)
2084 get :feed, :format => :atom, :display_name => user.display_name
2085 assert_response :success
2086 assert_template "list"
2087 assert_equal "application/atom+xml", response.content_type
2089 check_feed_result(changesets)
2093 # Check the not found of the user changesets feed
2094 def test_feed_user_not_found
2095 get :feed, :format => "atom", :display_name => "Some random user"
2096 assert_response :not_found
2100 # Check that we can't request later pages of the changesets feed
2101 def test_feed_max_id
2102 get :feed, :format => "atom", :max_id => 100
2103 assert_response :redirect
2104 assert_redirected_to :action => :feed
2108 # check that the changeset download for a changeset with a redacted
2109 # element in it doesn't contain that element.
2110 def test_diff_download_redacted
2111 changeset = create(:changeset)
2112 node = create(:node, :with_history, :version => 2, :changeset => changeset)
2113 node_v1 = node.old_nodes.find_by(:version => 1)
2114 node_v1.redact!(create(:redaction))
2116 get :download, :id => changeset.id
2117 assert_response :success
2119 assert_select "osmChange", 1
2120 # this changeset contains the node in versions 1 & 2, but 1 should
2122 assert_select "osmChange node[id='#{node.id}']", 1
2123 assert_select "osmChange node[id='#{node.id}'][version='1']", 0
2127 # create comment success
2128 def test_create_comment_success
2129 user = create(:user)
2130 user2 = create(:user)
2131 private_user = create(:user, :data_public => false)
2132 suspended_user = create(:user, :suspended)
2133 deleted_user = create(:user, :deleted)
2134 private_user_closed_changeset = create(:changeset, :closed, :user => private_user)
2136 basic_authorization(user.email, "test")
2138 assert_difference "ChangesetComment.count", 1 do
2139 assert_no_difference "ActionMailer::Base.deliveries.size" do
2140 post :comment, :id => private_user_closed_changeset.id, :text => "This is a comment"
2143 assert_response :success
2145 changeset = create(:changeset, :closed, :user => private_user)
2146 changeset.subscribers.push(private_user)
2147 changeset.subscribers.push(user)
2148 changeset.subscribers.push(suspended_user)
2149 changeset.subscribers.push(deleted_user)
2151 assert_difference "ChangesetComment.count", 1 do
2152 assert_difference "ActionMailer::Base.deliveries.size", 1 do
2153 post :comment, :id => changeset.id, :text => "This is a comment"
2156 assert_response :success
2158 email = ActionMailer::Base.deliveries.first
2159 assert_equal 1, email.to.length
2160 assert_equal "[OpenStreetMap] #{user.display_name} has commented on one of your changesets", email.subject
2161 assert_equal private_user.email, email.to.first
2163 ActionMailer::Base.deliveries.clear
2165 basic_authorization(user2.email, "test")
2167 assert_difference "ChangesetComment.count", 1 do
2168 assert_difference "ActionMailer::Base.deliveries.size", 2 do
2169 post :comment, :id => changeset.id, :text => "This is a comment"
2172 assert_response :success
2174 email = ActionMailer::Base.deliveries.find { |e| e.to.first == private_user.email }
2175 assert_not_nil email
2176 assert_equal 1, email.to.length
2177 assert_equal "[OpenStreetMap] #{user2.display_name} has commented on one of your changesets", email.subject
2179 email = ActionMailer::Base.deliveries.find { |e| e.to.first == user.email }
2180 assert_not_nil email
2181 assert_equal 1, email.to.length
2182 assert_equal "[OpenStreetMap] #{user2.display_name} has commented on a changeset you are interested in", email.subject
2184 ActionMailer::Base.deliveries.clear
2188 # create comment fail
2189 def test_create_comment_fail
2191 post :comment, :id => create(:changeset, :closed).id, :text => "This is a comment"
2192 assert_response :unauthorized
2194 basic_authorization(create(:user).email, "test")
2197 assert_no_difference "ChangesetComment.count" do
2198 post :comment, :id => 999111, :text => "This is a comment"
2200 assert_response :not_found
2202 # not closed changeset
2203 assert_no_difference "ChangesetComment.count" do
2204 post :comment, :id => create(:changeset).id, :text => "This is a comment"
2206 assert_response :conflict
2209 assert_no_difference "ChangesetComment.count" do
2210 post :comment, :id => create(:changeset, :closed).id
2212 assert_response :bad_request
2215 assert_no_difference "ChangesetComment.count" do
2216 post :comment, :id => create(:changeset, :closed).id, :text => ""
2218 assert_response :bad_request
2222 # test subscribe success
2223 def test_subscribe_success
2224 basic_authorization(create(:user).email, "test")
2225 changeset = create(:changeset, :closed)
2227 assert_difference "changeset.subscribers.count", 1 do
2228 post :subscribe, :id => changeset.id
2230 assert_response :success
2234 # test subscribe fail
2235 def test_subscribe_fail
2236 user = create(:user)
2239 changeset = create(:changeset, :closed)
2240 assert_no_difference "changeset.subscribers.count" do
2241 post :subscribe, :id => changeset.id
2243 assert_response :unauthorized
2245 basic_authorization(user.email, "test")
2248 assert_no_difference "changeset.subscribers.count" do
2249 post :subscribe, :id => 999111
2251 assert_response :not_found
2253 # not closed changeset
2254 changeset = create(:changeset)
2255 assert_no_difference "changeset.subscribers.count" do
2256 post :subscribe, :id => changeset.id
2258 assert_response :conflict
2260 # trying to subscribe when already subscribed
2261 changeset = create(:changeset, :closed)
2262 changeset.subscribers.push(user)
2263 assert_no_difference "changeset.subscribers.count" do
2264 post :subscribe, :id => changeset.id
2266 assert_response :conflict
2270 # test unsubscribe success
2271 def test_unsubscribe_success
2272 user = create(:user)
2273 basic_authorization(user.email, "test")
2274 changeset = create(:changeset, :closed)
2275 changeset.subscribers.push(user)
2277 assert_difference "changeset.subscribers.count", -1 do
2278 post :unsubscribe, :id => changeset.id
2280 assert_response :success
2284 # test unsubscribe fail
2285 def test_unsubscribe_fail
2287 changeset = create(:changeset, :closed)
2288 assert_no_difference "changeset.subscribers.count" do
2289 post :unsubscribe, :id => changeset.id
2291 assert_response :unauthorized
2293 basic_authorization(create(:user).email, "test")
2296 assert_no_difference "changeset.subscribers.count" do
2297 post :unsubscribe, :id => 999111
2299 assert_response :not_found
2301 # not closed changeset
2302 changeset = create(:changeset)
2303 assert_no_difference "changeset.subscribers.count" do
2304 post :unsubscribe, :id => changeset.id
2306 assert_response :conflict
2308 # trying to unsubscribe when not subscribed
2309 changeset = create(:changeset, :closed)
2310 assert_no_difference "changeset.subscribers.count" do
2311 post :unsubscribe, :id => changeset.id
2313 assert_response :not_found
2317 # test hide comment fail
2318 def test_hide_comment_fail
2320 comment = create(:changeset_comment)
2321 assert_equal true, comment.visible
2323 post :hide_comment, :id => comment.id
2324 assert_response :unauthorized
2325 assert_equal true, comment.reload.visible
2327 basic_authorization(create(:user).email, "test")
2330 post :hide_comment, :id => comment.id
2331 assert_response :forbidden
2332 assert_equal true, comment.reload.visible
2334 basic_authorization(create(:moderator_user).email, "test")
2337 post :hide_comment, :id => 999111
2338 assert_response :not_found
2339 assert_equal true, comment.reload.visible
2343 # test hide comment succes
2344 def test_hide_comment_success
2345 comment = create(:changeset_comment)
2346 assert_equal true, comment.visible
2348 basic_authorization(create(:moderator_user).email, "test")
2350 post :hide_comment, :id => comment.id
2351 assert_response :success
2352 assert_equal false, comment.reload.visible
2356 # test unhide comment fail
2357 def test_unhide_comment_fail
2359 comment = create(:changeset_comment, :visible => false)
2360 assert_equal false, comment.visible
2362 post :unhide_comment, :id => comment.id
2363 assert_response :unauthorized
2364 assert_equal false, comment.reload.visible
2366 basic_authorization(create(:user).email, "test")
2369 post :unhide_comment, :id => comment.id
2370 assert_response :forbidden
2371 assert_equal false, comment.reload.visible
2373 basic_authorization(create(:moderator_user).email, "test")
2376 post :unhide_comment, :id => 999111
2377 assert_response :not_found
2378 assert_equal false, comment.reload.visible
2382 # test unhide comment succes
2383 def test_unhide_comment_success
2384 comment = create(:changeset_comment, :visible => false)
2385 assert_equal false, comment.visible
2387 basic_authorization(create(:moderator_user).email, "test")
2389 post :unhide_comment, :id => comment.id
2390 assert_response :success
2391 assert_equal true, comment.reload.visible
2395 # test comments feed
2396 def test_comments_feed
2397 changeset = create(:changeset, :closed)
2398 create_list(:changeset_comment, 3, :changeset => changeset)
2400 get :comments_feed, :format => "rss"
2401 assert_response :success
2402 assert_equal "application/rss+xml", @response.content_type
2403 assert_select "rss", :count => 1 do
2404 assert_select "channel", :count => 1 do
2405 assert_select "item", :count => 3
2409 get :comments_feed, :format => "rss", :limit => 2
2410 assert_response :success
2411 assert_equal "application/rss+xml", @response.content_type
2412 assert_select "rss", :count => 1 do
2413 assert_select "channel", :count => 1 do
2414 assert_select "item", :count => 2
2418 get :comments_feed, :id => changeset.id, :format => "rss"
2419 assert_response :success
2420 assert_equal "application/rss+xml", @response.content_type
2421 assert_select "rss", :count => 1 do
2422 assert_select "channel", :count => 1 do
2423 assert_select "item", :count => 3
2429 # test comments feed
2430 def test_comments_feed_bad_limit
2431 get :comments_feed, :format => "rss", :limit => 0
2432 assert_response :bad_request
2434 get :comments_feed, :format => "rss", :limit => 100001
2435 assert_response :bad_request
2441 # boilerplate for checking that certain changesets exist in the
2443 def assert_changesets(changesets)
2444 assert_select "osm>changeset", changesets.size
2445 changesets.each do |changeset|
2446 assert_select "osm>changeset[id='#{changeset.id}']", 1
2451 # call the include method and assert properties of the bbox
2452 def check_after_include(changeset_id, lon, lat, bbox)
2453 content "<osm><node lon='#{lon}' lat='#{lat}'/></osm>"
2454 post :expand_bbox, :id => changeset_id
2455 assert_response :success, "Setting include of changeset failed: #{@response.body}"
2457 # check exactly one changeset
2458 assert_select "osm>changeset", 1
2459 assert_select "osm>changeset[id='#{changeset_id}']", 1
2462 doc = XML::Parser.string(@response.body).parse
2463 changeset = doc.find("//osm/changeset").first
2464 assert_equal bbox[0], changeset["min_lon"].to_f, "min lon"
2465 assert_equal bbox[1], changeset["min_lat"].to_f, "min lat"
2466 assert_equal bbox[2], changeset["max_lon"].to_f, "max lon"
2467 assert_equal bbox[3], changeset["max_lat"].to_f, "max lat"
2471 # update the changeset_id of a way element
2472 def update_changeset(xml, changeset_id)
2473 xml_attr_rewrite(xml, "changeset", changeset_id)
2477 # update an attribute in a way element
2478 def xml_attr_rewrite(xml, name, value)
2479 xml.find("//osm/way").first[name] = value.to_s
2484 # check the result of a list
2485 def check_list_result(changesets)
2486 changesets = changesets.where("num_changes > 0")
2487 .order(:created_at => :desc)
2489 assert changesets.size <= 20
2491 assert_select "ol.changesets", :count => [changesets.size, 1].min do
2492 assert_select "li", :count => changesets.size
2494 changesets.each do |changeset|
2495 assert_select "li#changeset_#{changeset.id}", :count => 1
2501 # check the result of a feed
2502 def check_feed_result(changesets)
2503 assert changesets.size <= 20
2505 assert_select "feed", :count => [changesets.size, 1].min do
2506 assert_select "> title", :count => 1, :text => /^Changesets/
2507 assert_select "> entry", :count => changesets.size
2509 changesets.each do |changeset|
2510 assert_select "> entry > id", changeset_url(:id => changeset.id)