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, :params => { :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, :params => { :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, :params => { :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|
213 get :read, :params => { :id => 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, :params => { :id => changeset.id }
231 assert_response :unauthorized
233 ## Try using the non-public user
234 basic_authorization private_user.email, "test"
235 put :close, :params => { :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, :params => { :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, :params => { :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, :params => { :id => changeset.id }
273 assert_response :method_not_allowed
275 post :close, :params => { :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, :params => { :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, :params => { :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 = <<CHANGESET.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, :params => { :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 = <<CHANGESET.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, :params => { :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 = <<CHANGESET.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, :params => { :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 = <<CHANGESET.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, :params => { :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, :params => { :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, :params => { :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 = <<CHANGESET.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, :params => { :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, :params => { :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, :params => { :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 = <<CHANGESET.strip_heredoc
745 <node id='-1' lon='0' lat='0' changeset='#{changeset.id}'>
746 <tag k='foo' v='#{'x' * 256}'/>
754 post :upload, :params => { :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 = <<CHANGESET.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, :params => { :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|
815 assert_equal new_node_id, id, "relation should contain new node" if type == "node"
820 # create a diff which references several changesets, which should cause
821 # a rollback and none of the diff gets committed
822 def test_upload_invalid_changesets
823 changeset = create(:changeset)
824 other_changeset = create(:changeset, :user => changeset.user)
827 relation = create(:relation)
828 other_relation = create(:relation)
830 basic_authorization changeset.user.email, "test"
832 # simple diff to create a node way and relation using placeholders
833 diff = <<CHANGESET.strip_heredoc
836 <node id='#{node.id}' lon='0' lat='0' changeset='#{changeset.id}' version='1'/>
837 <way id='#{way.id}' changeset='#{changeset.id}' version='1'>
838 <nd ref='#{node.id}'/>
842 <relation id='#{relation.id}' changeset='#{changeset.id}' version='1'>
843 <member type='way' role='some' ref='#{way.id}'/>
844 <member type='node' role='some' ref='#{node.id}'/>
845 <member type='relation' role='some' ref='#{other_relation.id}'/>
849 <node id='-1' lon='0' lat='0' changeset='#{other_changeset.id}'>
850 <tag k='foo' v='bar'/>
851 <tag k='baz' v='bat'/>
859 post :upload, :params => { :id => changeset.id }
860 assert_response :conflict,
861 "uploading a diff with multiple changesets should have failed"
863 # check that objects are unmodified
864 assert_nodes_are_equal(node, Node.find(node.id))
865 assert_ways_are_equal(way, Way.find(way.id))
866 assert_relations_are_equal(relation, Relation.find(relation.id))
870 # upload multiple versions of the same element in the same diff.
871 def test_upload_multiple_valid
873 changeset = create(:changeset)
874 basic_authorization changeset.user.email, "test"
876 # change the location of a node multiple times, each time referencing
877 # the last version. doesn't this depend on version numbers being
879 diff = <<CHANGESET.strip_heredoc
882 <node id='#{node.id}' lon='0' lat='0' changeset='#{changeset.id}' version='1'/>
883 <node id='#{node.id}' lon='1' lat='0' changeset='#{changeset.id}' version='2'/>
884 <node id='#{node.id}' lon='1' lat='1' changeset='#{changeset.id}' version='3'/>
885 <node id='#{node.id}' lon='1' lat='2' changeset='#{changeset.id}' version='4'/>
886 <node id='#{node.id}' lon='2' lat='2' changeset='#{changeset.id}' version='5'/>
887 <node id='#{node.id}' lon='3' lat='2' changeset='#{changeset.id}' version='6'/>
888 <node id='#{node.id}' lon='3' lat='3' changeset='#{changeset.id}' version='7'/>
889 <node id='#{node.id}' lon='9' lat='9' changeset='#{changeset.id}' version='8'/>
896 post :upload, :params => { :id => changeset.id }
897 assert_response :success,
898 "can't upload multiple versions of an element in a diff: #{@response.body}"
900 # check the response is well-formed. its counter-intuitive, but the
901 # API will return multiple elements with the same ID and different
902 # version numbers for each change we made.
903 assert_select "diffResult>node", 8
907 # upload multiple versions of the same element in the same diff, but
908 # keep the version numbers the same.
909 def test_upload_multiple_duplicate
911 changeset = create(:changeset)
913 basic_authorization changeset.user.email, "test"
915 diff = <<CHANGESET.strip_heredoc
918 <node id='#{node.id}' lon='0' lat='0' changeset='#{changeset.id}' version='1'/>
919 <node id='#{node.id}' lon='1' lat='1' changeset='#{changeset.id}' version='1'/>
926 post :upload, :params => { :id => changeset.id }
927 assert_response :conflict,
928 "shouldn't be able to upload the same element twice in a diff: #{@response.body}"
932 # try to upload some elements without specifying the version
933 def test_upload_missing_version
934 changeset = create(:changeset)
936 basic_authorization changeset.user.email, "test"
938 diff = <<CHANGESET.strip_heredoc
941 <node id='1' lon='1' lat='1' changeset='#{changeset.id}'/>
948 post :upload, :params => { :id => changeset.id }
949 assert_response :bad_request,
950 "shouldn't be able to upload an element without version: #{@response.body}"
954 # try to upload with commands other than create, modify, or delete
955 def test_action_upload_invalid
956 changeset = create(:changeset)
958 basic_authorization changeset.user.email, "test"
960 diff = <<CHANGESET.strip_heredoc
963 <node id='1' lon='1' lat='1' changeset='#{changeset.id}' />
968 post :upload, :params => { :id => changeset.id }
969 assert_response :bad_request, "Shouldn't be able to upload a diff with the action ping"
970 assert_equal @response.body, "Unknown action ping, choices are create, modify, delete"
974 # upload a valid changeset which has a mixture of whitespace
975 # to check a bug reported by ivansanchez (#1565).
976 def test_upload_whitespace_valid
977 changeset = create(:changeset)
979 way = create(:way_with_nodes, :nodes_count => 2)
980 relation = create(:relation)
981 other_relation = create(:relation)
982 create(:relation_tag, :relation => relation)
984 basic_authorization changeset.user.email, "test"
986 diff = <<CHANGESET.strip_heredoc
988 <modify><node id='#{node.id}' lon='0' lat='0' changeset='#{changeset.id}'
990 <node id='#{node.id}' lon='1' lat='1' changeset='#{changeset.id}' version='2'><tag k='k' v='v'/></node></modify>
992 <relation id='#{relation.id}' changeset='#{changeset.id}' version='1'><member
993 type='way' role='some' ref='#{way.id}'/><member
994 type='node' role='some' ref='#{node.id}'/>
995 <member type='relation' role='some' ref='#{other_relation.id}'/>
997 </modify></osmChange>
1002 post :upload, :params => { :id => changeset.id }
1003 assert_response :success,
1004 "can't upload a valid diff with whitespace variations to changeset: #{@response.body}"
1006 # check the response is well-formed
1007 assert_select "diffResult>node", 2
1008 assert_select "diffResult>relation", 1
1010 # check that the changes made it into the database
1011 assert_equal 1, Node.find(node.id).tags.size, "node #{node.id} should now have one tag"
1012 assert_equal 0, Relation.find(relation.id).tags.size, "relation #{relation.id} should now have no tags"
1016 # test that a placeholder can be reused within the same upload.
1017 def test_upload_reuse_placeholder_valid
1018 changeset = create(:changeset)
1020 basic_authorization changeset.user.email, "test"
1022 diff = <<CHANGESET.strip_heredoc
1025 <node id='-1' lon='0' lat='0' changeset='#{changeset.id}'>
1026 <tag k="foo" v="bar"/>
1030 <node id='-1' lon='1' lat='1' changeset='#{changeset.id}' version='1'/>
1033 <node id='-1' lon='2' lat='2' changeset='#{changeset.id}' version='2'/>
1040 post :upload, :params => { :id => changeset.id }
1041 assert_response :success,
1042 "can't upload a valid diff with re-used placeholders to changeset: #{@response.body}"
1044 # check the response is well-formed
1045 assert_select "diffResult>node", 3
1046 assert_select "diffResult>node[old_id='-1']", 3
1050 # test what happens if a diff upload re-uses placeholder IDs in an
1052 def test_upload_placeholder_invalid
1053 changeset = create(:changeset)
1055 basic_authorization changeset.user.email, "test"
1057 diff = <<CHANGESET.strip_heredoc
1060 <node id='-1' lon='0' lat='0' changeset='#{changeset.id}' version='1'/>
1061 <node id='-1' lon='1' lat='1' changeset='#{changeset.id}' version='1'/>
1062 <node id='-1' lon='2' lat='2' changeset='#{changeset.id}' version='2'/>
1069 post :upload, :params => { :id => changeset.id }
1070 assert_response :bad_request,
1071 "shouldn't be able to re-use placeholder IDs"
1075 # test that uploading a way referencing invalid placeholders gives a
1076 # proper error, not a 500.
1077 def test_upload_placeholder_invalid_way
1078 changeset = create(:changeset)
1081 basic_authorization changeset.user.email, "test"
1083 diff = <<CHANGESET.strip_heredoc
1086 <node id="-1" lon="0" lat="0" changeset="#{changeset.id}" version="1"/>
1087 <node id="-2" lon="1" lat="1" changeset="#{changeset.id}" version="1"/>
1088 <node id="-3" lon="2" lat="2" changeset="#{changeset.id}" version="1"/>
1089 <way id="-1" changeset="#{changeset.id}" version="1">
1101 post :upload, :params => { :id => changeset.id }
1102 assert_response :bad_request,
1103 "shouldn't be able to use invalid placeholder IDs"
1104 assert_equal "Placeholder node not found for reference -4 in way -1", @response.body
1106 # the same again, but this time use an existing way
1107 diff = <<CHANGESET.strip_heredoc
1110 <node id="-1" lon="0" lat="0" changeset="#{changeset.id}" version="1"/>
1111 <node id="-2" lon="1" lat="1" changeset="#{changeset.id}" version="1"/>
1112 <node id="-3" lon="2" lat="2" changeset="#{changeset.id}" version="1"/>
1113 <way id="#{way.id}" changeset="#{changeset.id}" version="1">
1125 post :upload, :params => { :id => changeset.id }
1126 assert_response :bad_request,
1127 "shouldn't be able to use invalid placeholder IDs"
1128 assert_equal "Placeholder node not found for reference -4 in way #{way.id}", @response.body
1132 # test that uploading a relation referencing invalid placeholders gives a
1133 # proper error, not a 500.
1134 def test_upload_placeholder_invalid_relation
1135 changeset = create(:changeset)
1136 relation = create(:relation)
1138 basic_authorization changeset.user.email, "test"
1140 diff = <<CHANGESET.strip_heredoc
1143 <node id="-1" lon="0" lat="0" changeset="#{changeset.id}" version="1"/>
1144 <node id="-2" lon="1" lat="1" changeset="#{changeset.id}" version="1"/>
1145 <node id="-3" lon="2" lat="2" changeset="#{changeset.id}" version="1"/>
1146 <relation id="-1" changeset="#{changeset.id}" version="1">
1147 <member type="node" role="foo" ref="-1"/>
1148 <member type="node" role="foo" ref="-2"/>
1149 <member type="node" role="foo" ref="-3"/>
1150 <member type="node" role="foo" ref="-4"/>
1158 post :upload, :params => { :id => changeset.id }
1159 assert_response :bad_request,
1160 "shouldn't be able to use invalid placeholder IDs"
1161 assert_equal "Placeholder Node not found for reference -4 in relation -1.", @response.body
1163 # the same again, but this time use an existing relation
1164 diff = <<CHANGESET.strip_heredoc
1167 <node id="-1" lon="0" lat="0" changeset="#{changeset.id}" version="1"/>
1168 <node id="-2" lon="1" lat="1" changeset="#{changeset.id}" version="1"/>
1169 <node id="-3" lon="2" lat="2" changeset="#{changeset.id}" version="1"/>
1170 <relation id="#{relation.id}" changeset="#{changeset.id}" version="1">
1171 <member type="node" role="foo" ref="-1"/>
1172 <member type="node" role="foo" ref="-2"/>
1173 <member type="node" role="foo" ref="-3"/>
1174 <member type="way" role="bar" ref="-1"/>
1182 post :upload, :params => { :id => changeset.id }
1183 assert_response :bad_request,
1184 "shouldn't be able to use invalid placeholder IDs"
1185 assert_equal "Placeholder Way not found for reference -1 in relation #{relation.id}.", @response.body
1189 # test what happens if a diff is uploaded containing only a node
1191 def test_upload_node_move
1192 basic_authorization create(:user).email, "test"
1194 content "<osm><changeset>" \
1195 "<tag k='created_by' v='osm test suite checking changesets'/>" \
1196 "</changeset></osm>"
1198 assert_response :success
1199 changeset_id = @response.body.to_i
1201 old_node = create(:node, :lat => 1, :lon => 1)
1203 diff = XML::Document.new
1204 diff.root = XML::Node.new "osmChange"
1205 modify = XML::Node.new "modify"
1206 xml_old_node = old_node.to_xml_node
1207 xml_old_node["lat"] = 2.0.to_s
1208 xml_old_node["lon"] = 2.0.to_s
1209 xml_old_node["changeset"] = changeset_id.to_s
1210 modify << xml_old_node
1215 post :upload, :params => { :id => changeset_id }
1216 assert_response :success,
1217 "diff should have uploaded OK"
1220 changeset = Changeset.find(changeset_id)
1221 assert_equal 1 * GeoRecord::SCALE, changeset.min_lon, "min_lon should be 1 degree"
1222 assert_equal 2 * GeoRecord::SCALE, changeset.max_lon, "max_lon should be 2 degrees"
1223 assert_equal 1 * GeoRecord::SCALE, changeset.min_lat, "min_lat should be 1 degree"
1224 assert_equal 2 * GeoRecord::SCALE, changeset.max_lat, "max_lat should be 2 degrees"
1228 # test what happens if a diff is uploaded adding a node to a way.
1229 def test_upload_way_extend
1230 basic_authorization create(:user).email, "test"
1232 content "<osm><changeset>" \
1233 "<tag k='created_by' v='osm test suite checking changesets'/>" \
1234 "</changeset></osm>"
1236 assert_response :success
1237 changeset_id = @response.body.to_i
1239 old_way = create(:way)
1240 create(:way_node, :way => old_way, :node => create(:node, :lat => 1, :lon => 1))
1242 diff = XML::Document.new
1243 diff.root = XML::Node.new "osmChange"
1244 modify = XML::Node.new "modify"
1245 xml_old_way = old_way.to_xml_node
1246 nd_ref = XML::Node.new "nd"
1247 nd_ref["ref"] = create(:node, :lat => 3, :lon => 3).id.to_s
1248 xml_old_way << nd_ref
1249 xml_old_way["changeset"] = changeset_id.to_s
1250 modify << xml_old_way
1255 post :upload, :params => { :id => changeset_id }
1256 assert_response :success,
1257 "diff should have uploaded OK"
1260 changeset = Changeset.find(changeset_id)
1261 assert_equal 1 * GeoRecord::SCALE, changeset.min_lon, "min_lon should be 1 degree"
1262 assert_equal 3 * GeoRecord::SCALE, changeset.max_lon, "max_lon should be 3 degrees"
1263 assert_equal 1 * GeoRecord::SCALE, changeset.min_lat, "min_lat should be 1 degree"
1264 assert_equal 3 * GeoRecord::SCALE, changeset.max_lat, "max_lat should be 3 degrees"
1268 # test for more issues in #1568
1269 def test_upload_empty_invalid
1270 changeset = create(:changeset)
1272 basic_authorization changeset.user.email, "test"
1275 "<osmChange></osmChange>",
1276 "<osmChange><modify/></osmChange>",
1277 "<osmChange><modify></modify></osmChange>"].each do |diff|
1280 post :upload, :params => { :id => changeset.id }
1281 assert_response(:success, "should be able to upload " \
1282 "empty changeset: " + diff)
1287 # test that the X-Error-Format header works to request XML errors
1288 def test_upload_xml_errors
1289 changeset = create(:changeset)
1290 node = create(:node)
1291 create(:relation_member, :member => node)
1293 basic_authorization changeset.user.email, "test"
1295 # try and delete a node that is in use
1296 diff = XML::Document.new
1297 diff.root = XML::Node.new "osmChange"
1298 delete = XML::Node.new "delete"
1300 delete << node.to_xml_node
1305 post :upload, :params => { :id => changeset.id }
1306 assert_response :success,
1307 "failed to return error in XML format"
1309 # check the returned payload
1310 assert_select "osmError[version='#{API_VERSION}'][generator='OpenStreetMap server']", 1
1311 assert_select "osmError>status", 1
1312 assert_select "osmError>message", 1
1316 # when we make some simple changes we get the same changes back from the
1318 def test_diff_download_simple
1319 node = create(:node)
1321 ## First try with a non-public user, which should get a forbidden
1322 basic_authorization create(:user, :data_public => false).email, "test"
1324 # create a temporary changeset
1325 content "<osm><changeset>" \
1326 "<tag k='created_by' v='osm test suite checking changesets'/>" \
1327 "</changeset></osm>"
1329 assert_response :forbidden
1331 ## Now try with a normal user
1332 basic_authorization create(:user).email, "test"
1334 # create a temporary changeset
1335 content "<osm><changeset>" \
1336 "<tag k='created_by' v='osm test suite checking changesets'/>" \
1337 "</changeset></osm>"
1339 assert_response :success
1340 changeset_id = @response.body.to_i
1343 diff = <<CHANGESET.strip_heredoc
1346 <node id='#{node.id}' lon='0' lat='0' changeset='#{changeset_id}' version='1'/>
1347 <node id='#{node.id}' lon='1' lat='0' changeset='#{changeset_id}' version='2'/>
1348 <node id='#{node.id}' lon='1' lat='1' changeset='#{changeset_id}' version='3'/>
1349 <node id='#{node.id}' lon='1' lat='2' changeset='#{changeset_id}' version='4'/>
1350 <node id='#{node.id}' lon='2' lat='2' changeset='#{changeset_id}' version='5'/>
1351 <node id='#{node.id}' lon='3' lat='2' changeset='#{changeset_id}' version='6'/>
1352 <node id='#{node.id}' lon='3' lat='3' changeset='#{changeset_id}' version='7'/>
1353 <node id='#{node.id}' lon='9' lat='9' changeset='#{changeset_id}' version='8'/>
1360 post :upload, :params => { :id => changeset_id }
1361 assert_response :success,
1362 "can't upload multiple versions of an element in a diff: #{@response.body}"
1364 get :download, :params => { :id => changeset_id }
1365 assert_response :success
1367 assert_select "osmChange", 1
1368 assert_select "osmChange>modify", 8
1369 assert_select "osmChange>modify>node", 8
1373 # culled this from josm to ensure that nothing in the way that josm
1374 # is formatting the request is causing it to fail.
1376 # NOTE: the error turned out to be something else completely!
1377 def test_josm_upload
1378 basic_authorization create(:user).email, "test"
1380 # create a temporary changeset
1381 content "<osm><changeset>" \
1382 "<tag k='created_by' v='osm test suite checking changesets'/>" \
1383 "</changeset></osm>"
1385 assert_response :success
1386 changeset_id = @response.body.to_i
1388 diff = <<OSMFILE.strip_heredoc
1389 <osmChange version="0.6" generator="JOSM">
1390 <create version="0.6" generator="JOSM">
1391 <node id='-1' visible='true' changeset='#{changeset_id}' lat='51.49619982187321' lon='-0.18722061869438314' />
1392 <node id='-2' visible='true' changeset='#{changeset_id}' lat='51.496359883909605' lon='-0.18653093576241928' />
1393 <node id='-3' visible='true' changeset='#{changeset_id}' lat='51.49598132358285' lon='-0.18719613290981638' />
1394 <node id='-4' visible='true' changeset='#{changeset_id}' lat='51.4961591711078' lon='-0.18629015888084607' />
1395 <node id='-5' visible='true' changeset='#{changeset_id}' lat='51.49582126021711' lon='-0.18708186591517145' />
1396 <node id='-6' visible='true' changeset='#{changeset_id}' lat='51.49591018437858' lon='-0.1861432441734455' />
1397 <node id='-7' visible='true' changeset='#{changeset_id}' lat='51.49560784152179' lon='-0.18694719410005425' />
1398 <node id='-8' visible='true' changeset='#{changeset_id}' lat='51.49567389979617' lon='-0.1860289771788006' />
1399 <node id='-9' visible='true' changeset='#{changeset_id}' lat='51.49543761398892' lon='-0.186820684213126' />
1400 <way id='-10' action='modiy' visible='true' changeset='#{changeset_id}'>
1410 <tag k='highway' v='residential' />
1411 <tag k='name' v='Foobar Street' />
1419 post :upload, :params => { :id => changeset_id }
1420 assert_response :success,
1421 "can't upload a diff from JOSM: #{@response.body}"
1423 get :download, :params => { :id => changeset_id }
1424 assert_response :success
1426 assert_select "osmChange", 1
1427 assert_select "osmChange>create>node", 9
1428 assert_select "osmChange>create>way", 1
1429 assert_select "osmChange>create>way>nd", 9
1430 assert_select "osmChange>create>way>tag", 2
1434 # when we make some complex changes we get the same changes back from the
1436 def test_diff_download_complex
1437 node = create(:node)
1438 node2 = create(:node)
1440 basic_authorization create(:user).email, "test"
1442 # create a temporary changeset
1443 content "<osm><changeset>" \
1444 "<tag k='created_by' v='osm test suite checking changesets'/>" \
1445 "</changeset></osm>"
1447 assert_response :success
1448 changeset_id = @response.body.to_i
1451 diff = <<CHANGESET.strip_heredoc
1454 <node id='#{node.id}' lon='0' lat='0' changeset='#{changeset_id}' version='1'/>
1457 <node id='-1' lon='9' lat='9' changeset='#{changeset_id}' version='0'/>
1458 <node id='-2' lon='8' lat='9' changeset='#{changeset_id}' version='0'/>
1459 <node id='-3' lon='7' lat='9' changeset='#{changeset_id}' version='0'/>
1462 <node id='#{node2.id}' lon='20' lat='15' changeset='#{changeset_id}' version='1'/>
1463 <way id='#{way.id}' changeset='#{changeset_id}' version='1'>
1464 <nd ref='#{node2.id}'/>
1475 post :upload, :params => { :id => changeset_id }
1476 assert_response :success,
1477 "can't upload multiple versions of an element in a diff: #{@response.body}"
1479 get :download, :params => { :id => changeset_id }
1480 assert_response :success
1482 assert_select "osmChange", 1
1483 assert_select "osmChange>create", 3
1484 assert_select "osmChange>delete", 1
1485 assert_select "osmChange>modify", 2
1486 assert_select "osmChange>create>node", 3
1487 assert_select "osmChange>delete>node", 1
1488 assert_select "osmChange>modify>node", 1
1489 assert_select "osmChange>modify>way", 1
1492 def test_changeset_download
1493 changeset = create(:changeset)
1494 node = create(:node, :with_history, :version => 1, :changeset => changeset)
1495 tag = create(:old_node_tag, :old_node => node.old_nodes.find_by(:version => 1))
1496 node2 = create(:node, :with_history, :version => 1, :changeset => changeset)
1497 _node3 = create(:node, :with_history, :deleted, :version => 1, :changeset => changeset)
1498 _relation = create(:relation, :with_history, :version => 1, :changeset => changeset)
1499 _relation2 = create(:relation, :with_history, :deleted, :version => 1, :changeset => changeset)
1501 get :download, :params => { :id => changeset.id }
1503 assert_response :success
1505 # print @response.body
1506 # FIXME: needs more assert_select tests
1507 assert_select "osmChange[version='#{API_VERSION}'][generator='#{GENERATOR}']" do
1508 assert_select "create", :count => 5
1509 assert_select "create>node[id='#{node.id}'][visible='#{node.visible?}'][version='#{node.version}']" do
1510 assert_select "tag[k='#{tag.k}'][v='#{tag.v}']"
1512 assert_select "create>node[id='#{node2.id}']"
1517 # check that the bounding box of a changeset gets updated correctly
1518 # FIXME: This should really be moded to a integration test due to the with_controller
1519 def test_changeset_bbox
1521 create(:way_node, :way => way, :node => create(:node, :lat => 3, :lon => 3))
1523 basic_authorization create(:user).email, "test"
1525 # create a new changeset
1526 content "<osm><changeset/></osm>"
1528 assert_response :success, "Creating of changeset failed."
1529 changeset_id = @response.body.to_i
1531 # add a single node to it
1532 with_controller(NodeController.new) do
1533 content "<osm><node lon='1' lat='2' changeset='#{changeset_id}'/></osm>"
1535 assert_response :success, "Couldn't create node."
1538 # get the bounding box back from the changeset
1539 get :read, :params => { :id => changeset_id }
1540 assert_response :success, "Couldn't read back changeset."
1541 assert_select "osm>changeset[min_lon='1.0000000']", 1
1542 assert_select "osm>changeset[max_lon='1.0000000']", 1
1543 assert_select "osm>changeset[min_lat='2.0000000']", 1
1544 assert_select "osm>changeset[max_lat='2.0000000']", 1
1546 # add another node to it
1547 with_controller(NodeController.new) do
1548 content "<osm><node lon='2' lat='1' changeset='#{changeset_id}'/></osm>"
1550 assert_response :success, "Couldn't create second node."
1553 # get the bounding box back from the changeset
1554 get :read, :params => { :id => changeset_id }
1555 assert_response :success, "Couldn't read back changeset for the second time."
1556 assert_select "osm>changeset[min_lon='1.0000000']", 1
1557 assert_select "osm>changeset[max_lon='2.0000000']", 1
1558 assert_select "osm>changeset[min_lat='1.0000000']", 1
1559 assert_select "osm>changeset[max_lat='2.0000000']", 1
1561 # add (delete) a way to it, which contains a point at (3,3)
1562 with_controller(WayController.new) do
1563 content update_changeset(way.to_xml, changeset_id)
1564 put :delete, :params => { :id => way.id }
1565 assert_response :success, "Couldn't delete a way."
1568 # get the bounding box back from the changeset
1569 get :read, :params => { :id => changeset_id }
1570 assert_response :success, "Couldn't read back changeset for the third time."
1571 # note that the 3.1 here is because of the bbox overexpansion
1572 assert_select "osm>changeset[min_lon='1.0000000']", 1
1573 assert_select "osm>changeset[max_lon='3.1000000']", 1
1574 assert_select "osm>changeset[min_lat='1.0000000']", 1
1575 assert_select "osm>changeset[max_lat='3.1000000']", 1
1579 # test that the changeset :include method works as it should
1580 def test_changeset_include
1581 basic_authorization create(:user).display_name, "test"
1583 # create a new changeset
1584 content "<osm><changeset/></osm>"
1586 assert_response :success, "Creating of changeset failed."
1587 changeset_id = @response.body.to_i
1589 # NOTE: the include method doesn't over-expand, like inserting
1590 # a real method does. this is because we expect the client to
1591 # know what it is doing!
1592 check_after_include(changeset_id, 1, 1, [1, 1, 1, 1])
1593 check_after_include(changeset_id, 3, 3, [1, 1, 3, 3])
1594 check_after_include(changeset_id, 4, 2, [1, 1, 4, 3])
1595 check_after_include(changeset_id, 2, 2, [1, 1, 4, 3])
1596 check_after_include(changeset_id, -1, -1, [-1, -1, 4, 3])
1597 check_after_include(changeset_id, -2, 5, [-2, -1, 4, 5])
1601 # test that a not found, wrong method with the expand bbox works as expected
1602 def test_changeset_expand_bbox_error
1603 basic_authorization create(:user).display_name, "test"
1605 # create a new changeset
1606 content "<osm><changeset/></osm>"
1608 assert_response :success, "Creating of changeset failed."
1609 changeset_id = @response.body.to_i
1615 content "<osm><node lon='#{lon}' lat='#{lat}'/></osm>"
1616 put :expand_bbox, :params => { :id => changeset_id }
1617 assert_response :method_not_allowed, "shouldn't be able to put a bbox expand"
1619 # Try to get the update
1620 content "<osm><node lon='#{lon}' lat='#{lat}'/></osm>"
1621 get :expand_bbox, :params => { :id => changeset_id }
1622 assert_response :method_not_allowed, "shouldn't be able to get a bbox expand"
1624 # Try to use a hopefully missing changeset
1625 content "<osm><node lon='#{lon}' lat='#{lat}'/></osm>"
1626 post :expand_bbox, :params => { :id => changeset_id + 13245 }
1627 assert_response :not_found, "shouldn't be able to do a bbox expand on a nonexistant changeset"
1631 # test the query functionality of changesets
1633 private_user = create(:user, :data_public => false)
1634 private_user_changeset = create(:changeset, :user => private_user)
1635 private_user_closed_changeset = create(:changeset, :closed, :user => private_user)
1636 user = create(:user)
1637 changeset = create(:changeset, :user => user)
1638 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))
1639 changeset2 = create(:changeset, :min_lat => 5 * GeoRecord::SCALE, :min_lon => 5 * GeoRecord::SCALE, :max_lat => 15 * GeoRecord::SCALE, :max_lon => 15 * GeoRecord::SCALE)
1640 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)
1642 get :query, :params => { :bbox => "-10,-10, 10, 10" }
1643 assert_response :success, "can't get changesets in bbox"
1644 assert_changesets [changeset2, changeset3]
1646 get :query, :params => { :bbox => "4.5,4.5,4.6,4.6" }
1647 assert_response :success, "can't get changesets in bbox"
1648 assert_changesets [changeset3]
1650 # not found when looking for changesets of non-existing users
1651 get :query, :params => { :user => User.maximum(:id) + 1 }
1652 assert_response :not_found
1653 get :query, :params => { :display_name => " " }
1654 assert_response :not_found
1656 # can't get changesets of user 1 without authenticating
1657 get :query, :params => { :user => private_user.id }
1658 assert_response :not_found, "shouldn't be able to get changesets by non-public user (ID)"
1659 get :query, :params => { :display_name => private_user.display_name }
1660 assert_response :not_found, "shouldn't be able to get changesets by non-public user (name)"
1662 # but this should work
1663 basic_authorization private_user.email, "test"
1664 get :query, :params => { :user => private_user.id }
1665 assert_response :success, "can't get changesets by user ID"
1666 assert_changesets [private_user_changeset, private_user_closed_changeset]
1668 get :query, :params => { :display_name => private_user.display_name }
1669 assert_response :success, "can't get changesets by user name"
1670 assert_changesets [private_user_changeset, private_user_closed_changeset]
1672 # check that the correct error is given when we provide both UID and name
1673 get :query, :params => { :user => private_user.id,
1674 :display_name => private_user.display_name }
1675 assert_response :bad_request, "should be a bad request to have both ID and name specified"
1677 get :query, :params => { :user => private_user.id, :open => true }
1678 assert_response :success, "can't get changesets by user and open"
1679 assert_changesets [private_user_changeset]
1681 get :query, :params => { :time => "2007-12-31" }
1682 assert_response :success, "can't get changesets by time-since"
1683 assert_changesets [private_user_changeset, private_user_closed_changeset, changeset, closed_changeset, changeset2, changeset3]
1685 get :query, :params => { :time => "2008-01-01T12:34Z" }
1686 assert_response :success, "can't get changesets by time-since with hour"
1687 assert_changesets [private_user_changeset, private_user_closed_changeset, changeset, closed_changeset, changeset2, changeset3]
1689 get :query, :params => { :time => "2007-12-31T23:59Z,2008-01-02T00:01Z" }
1690 assert_response :success, "can't get changesets by time-range"
1691 assert_changesets [closed_changeset]
1693 get :query, :params => { :open => "true" }
1694 assert_response :success, "can't get changesets by open-ness"
1695 assert_changesets [private_user_changeset, changeset, changeset2, changeset3]
1697 get :query, :params => { :closed => "true" }
1698 assert_response :success, "can't get changesets by closed-ness"
1699 assert_changesets [private_user_closed_changeset, closed_changeset]
1701 get :query, :params => { :closed => "true", :user => private_user.id }
1702 assert_response :success, "can't get changesets by closed-ness and user"
1703 assert_changesets [private_user_closed_changeset]
1705 get :query, :params => { :closed => "true", :user => user.id }
1706 assert_response :success, "can't get changesets by closed-ness and user"
1707 assert_changesets [closed_changeset]
1709 get :query, :params => { :changesets => "#{private_user_changeset.id},#{changeset.id},#{closed_changeset.id}" }
1710 assert_response :success, "can't get changesets by id (as comma-separated string)"
1711 assert_changesets [private_user_changeset, changeset, closed_changeset]
1713 get :query, :params => { :changesets => "" }
1714 assert_response :bad_request, "should be a bad request since changesets is empty"
1718 # check that errors are returned if garbage is inserted
1719 # into query strings
1720 def test_query_invalid
1723 ";drop table users;"].each do |bbox|
1724 get :query, :params => { :bbox => bbox }
1725 assert_response :bad_request, "'#{bbox}' isn't a bbox"
1730 ";drop table users;",
1732 "-,-"].each do |time|
1733 get :query, :params => { :time => time }
1734 assert_response :bad_request, "'#{time}' isn't a valid time range"
1741 get :query, :params => { :user => uid }
1742 assert_response :bad_request, "'#{uid}' isn't a valid user ID"
1747 # check updating tags on a changeset
1748 def test_changeset_update
1749 private_user = create(:user, :data_public => false)
1750 private_changeset = create(:changeset, :user => private_user)
1751 user = create(:user)
1752 changeset = create(:changeset, :user => user)
1754 ## First try with a non-public user
1755 new_changeset = private_changeset.to_xml
1756 new_tag = XML::Node.new "tag"
1757 new_tag["k"] = "tagtesting"
1758 new_tag["v"] = "valuetesting"
1759 new_changeset.find("//osm/changeset").first << new_tag
1760 content new_changeset
1762 # try without any authorization
1763 put :update, :params => { :id => private_changeset.id }
1764 assert_response :unauthorized
1766 # try with the wrong authorization
1767 basic_authorization create(:user).email, "test"
1768 put :update, :params => { :id => private_changeset.id }
1769 assert_response :conflict
1771 # now this should get an unauthorized
1772 basic_authorization private_user.email, "test"
1773 put :update, :params => { :id => private_changeset.id }
1774 assert_require_public_data "user with their data non-public, shouldn't be able to edit their changeset"
1776 ## Now try with the public user
1777 create(:changeset_tag, :changeset => changeset)
1778 new_changeset = changeset.to_xml
1779 new_tag = XML::Node.new "tag"
1780 new_tag["k"] = "tagtesting"
1781 new_tag["v"] = "valuetesting"
1782 new_changeset.find("//osm/changeset").first << new_tag
1783 content new_changeset
1785 # try without any authorization
1786 @request.env["HTTP_AUTHORIZATION"] = nil
1787 put :update, :params => { :id => changeset.id }
1788 assert_response :unauthorized
1790 # try with the wrong authorization
1791 basic_authorization create(:user).email, "test"
1792 put :update, :params => { :id => changeset.id }
1793 assert_response :conflict
1795 # now this should work...
1796 basic_authorization user.email, "test"
1797 put :update, :params => { :id => changeset.id }
1798 assert_response :success
1800 assert_select "osm>changeset[id='#{changeset.id}']", 1
1801 assert_select "osm>changeset>tag", 2
1802 assert_select "osm>changeset>tag[k='tagtesting'][v='valuetesting']", 1
1806 # check that a user different from the one who opened the changeset
1808 def test_changeset_update_invalid
1809 basic_authorization create(:user).email, "test"
1811 changeset = create(:changeset)
1812 new_changeset = changeset.to_xml
1813 new_tag = XML::Node.new "tag"
1814 new_tag["k"] = "testing"
1815 new_tag["v"] = "testing"
1816 new_changeset.find("//osm/changeset").first << new_tag
1818 content new_changeset
1819 put :update, :params => { :id => changeset.id }
1820 assert_response :conflict
1824 # check that a changeset can contain a certain max number of changes.
1825 ## FIXME should be changed to an integration test due to the with_controller
1826 def test_changeset_limits
1827 basic_authorization create(:user).email, "test"
1829 # open a new changeset
1830 content "<osm><changeset/></osm>"
1832 assert_response :success, "can't create a new changeset"
1833 cs_id = @response.body.to_i
1835 # start the counter just short of where the changeset should finish.
1837 # alter the database to set the counter on the changeset directly,
1838 # otherwise it takes about 6 minutes to fill all of them.
1839 changeset = Changeset.find(cs_id)
1840 changeset.num_changes = Changeset::MAX_ELEMENTS - offset
1843 with_controller(NodeController.new) do
1845 content "<osm><node changeset='#{cs_id}' lat='0.0' lon='0.0'/></osm>"
1847 assert_response :success, "can't create a new node"
1848 node_id = @response.body.to_i
1850 get :read, :params => { :id => node_id }
1851 assert_response :success, "can't read back new node"
1852 node_doc = XML::Parser.string(@response.body).parse
1853 node_xml = node_doc.find("//osm/node").first
1855 # loop until we fill the changeset with nodes
1857 node_xml["lat"] = rand.to_s
1858 node_xml["lon"] = rand.to_s
1859 node_xml["version"] = (i + 1).to_s
1862 put :update, :params => { :id => node_id }
1863 assert_response :success, "attempt #{i} should have succeeded"
1866 # trying again should fail
1867 node_xml["lat"] = rand.to_s
1868 node_xml["lon"] = rand.to_s
1869 node_xml["version"] = offset.to_s
1872 put :update, :params => { :id => node_id }
1873 assert_response :conflict, "final attempt should have failed"
1876 changeset = Changeset.find(cs_id)
1877 assert_equal Changeset::MAX_ELEMENTS + 1, changeset.num_changes
1879 # check that the changeset is now closed as well
1880 assert(!changeset.is_open?,
1881 "changeset should have been auto-closed by exceeding " \
1886 # This should display the last 20 changesets closed
1888 get :list, :params => { :format => "html" }
1889 assert_response :success
1890 assert_template "history"
1891 assert_template :layout => "map"
1892 assert_select "h2", :text => "Changesets", :count => 1
1894 get :list, :params => { :format => "html", :list => "1" }, :xhr => true
1895 assert_response :success
1896 assert_template "list"
1898 check_list_result(Changeset.all)
1902 # This should display the last 20 changesets closed
1904 get :list, :params => { :format => "html" }, :xhr => true
1905 assert_response :success
1906 assert_template "history"
1907 assert_template :layout => "xhr"
1908 assert_select "h2", :text => "Changesets", :count => 1
1910 get :list, :params => { :format => "html", :list => "1" }, :xhr => true
1911 assert_response :success
1912 assert_template "list"
1914 check_list_result(Changeset.all)
1918 # This should display the last 20 changesets closed in a specific area
1920 get :list, :params => { :format => "html", :bbox => "4.5,4.5,5.5,5.5" }
1921 assert_response :success
1922 assert_template "history"
1923 assert_template :layout => "map"
1924 assert_select "h2", :text => "Changesets", :count => 1
1926 get :list, :params => { :format => "html", :bbox => "4.5,4.5,5.5,5.5", :list => "1" }, :xhr => true
1927 assert_response :success
1928 assert_template "list"
1930 check_list_result(Changeset.where("min_lon < 55000000 and max_lon > 45000000 and min_lat < 55000000 and max_lat > 45000000"))
1934 # Checks the display of the user changesets listing
1936 user = create(:user)
1937 create(:changeset, :user => user)
1938 create(:changeset, :closed, :user => user)
1940 get :list, :params => { :format => "html", :display_name => user.display_name }
1941 assert_response :success
1942 assert_template "history"
1944 get :list, :params => { :format => "html", :display_name => user.display_name, :list => "1" }, :xhr => true
1945 assert_response :success
1946 assert_template "list"
1948 check_list_result(user.changesets)
1952 # Checks the display of the user changesets listing for a private user
1953 def test_list_private_user
1954 private_user = create(:user, :data_public => false)
1955 create(:changeset, :user => private_user)
1956 create(:changeset, :closed, :user => private_user)
1958 get :list, :params => { :format => "html", :display_name => private_user.display_name }
1959 assert_response :success
1960 assert_template "history"
1962 get :list, :params => { :format => "html", :display_name => private_user.display_name, :list => "1" }, :xhr => true
1963 assert_response :success
1964 assert_template "list"
1966 check_list_result(Changeset.none)
1970 # Check the not found of the list user changesets
1971 def test_list_user_not_found
1972 get :list, :params => { :format => "html", :display_name => "Some random user" }
1973 assert_response :not_found
1974 assert_template "user/no_such_user"
1976 get :list, :params => { :format => "html", :display_name => "Some random user", :list => "1" }, :xhr => true
1977 assert_response :not_found
1978 assert_template "user/no_such_user"
1982 # Checks the display of the friends changesets listing
1983 def test_list_friends
1984 private_user = create(:user, :data_public => true)
1985 friend = create(:friend, :befriender => private_user)
1986 create(:changeset, :user => friend.befriendee)
1988 get :list, :params => { :friends => true }
1989 assert_response :redirect
1990 assert_redirected_to :controller => :user, :action => :login, :referer => friend_changesets_path
1992 session[:user] = private_user.id
1994 get :list, :params => { :friends => true }
1995 assert_response :success
1996 assert_template "history"
1998 get :list, :params => { :friends => true, :list => "1" }, :xhr => true
1999 assert_response :success
2000 assert_template "list"
2002 check_list_result(Changeset.where(:user => private_user.friend_users.identifiable))
2006 # Checks the display of the nearby user changesets listing
2007 def test_list_nearby
2008 private_user = create(:user, :data_public => false, :home_lat => 51.1, :home_lon => 1.0)
2009 user = create(:user, :home_lat => 51.0, :home_lon => 1.0)
2010 create(:changeset, :user => user)
2012 get :list, :params => { :nearby => true }
2013 assert_response :redirect
2014 assert_redirected_to :controller => :user, :action => :login, :referer => nearby_changesets_path
2016 session[:user] = private_user.id
2018 get :list, :params => { :nearby => true }
2019 assert_response :success
2020 assert_template "history"
2022 get :list, :params => { :nearby => true, :list => "1" }, :xhr => true
2023 assert_response :success
2024 assert_template "list"
2026 check_list_result(Changeset.where(:user => user.nearby))
2030 # Check that we can't request later pages of the changesets list
2031 def test_list_max_id
2032 get :list, :params => { :format => "html", :max_id => 4 }, :xhr => true
2033 assert_response :success
2034 assert_template "history"
2035 assert_template :layout => "xhr"
2036 assert_select "h2", :text => "Changesets", :count => 1
2038 get :list, :params => { :format => "html", :list => "1", :max_id => 4 }, :xhr => true
2039 assert_response :success
2040 assert_template "list"
2042 check_list_result(Changeset.where("id <= 4"))
2046 # Check that a list with a next page link works
2048 create_list(:changeset, 50)
2050 get :list, :params => { :format => "html" }
2051 assert_response :success
2053 get :list, :params => { :format => "html" }, :xhr => true
2054 assert_response :success
2058 # This should display the last 20 non-empty changesets
2060 changeset = create(:changeset, :num_changes => 1)
2061 create(:changeset_tag, :changeset => changeset)
2062 create(:changeset_tag, :changeset => changeset, :k => "website", :v => "http://example.com/")
2063 closed_changeset = create(:changeset, :closed, :num_changes => 1)
2064 _empty_changeset = create(:changeset, :num_changes => 0)
2066 get :feed, :params => { :format => :atom }
2067 assert_response :success
2068 assert_template "list"
2069 assert_equal "application/atom+xml", response.content_type
2071 check_feed_result([changeset, closed_changeset])
2075 # This should display the last 20 changesets closed in a specific area
2077 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)
2078 create(:changeset_tag, :changeset => changeset)
2079 create(:changeset_tag, :changeset => changeset, :k => "website", :v => "http://example.com/")
2080 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)
2081 _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)
2082 _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)
2084 get :feed, :params => { :format => :atom, :bbox => "4.5,4.5,5.5,5.5" }
2085 assert_response :success
2086 assert_template "list"
2087 assert_equal "application/atom+xml", response.content_type
2089 check_feed_result([changeset, closed_changeset])
2093 # Checks the display of the user changesets feed
2095 user = create(:user)
2096 changesets = create_list(:changeset, 3, :user => user, :num_changes => 4)
2097 create(:changeset_tag, :changeset => changesets[1])
2098 create(:changeset_tag, :changeset => changesets[1], :k => "website", :v => "http://example.com/")
2099 _other_changeset = create(:changeset)
2101 get :feed, :params => { :format => :atom, :display_name => user.display_name }
2103 assert_response :success
2104 assert_template "list"
2105 assert_equal "application/atom+xml", response.content_type
2107 check_feed_result(changesets)
2111 # Check the not found of the user changesets feed
2112 def test_feed_user_not_found
2113 get :feed, :params => { :format => "atom", :display_name => "Some random user" }
2114 assert_response :not_found
2118 # Check that we can't request later pages of the changesets feed
2119 def test_feed_max_id
2120 get :feed, :params => { :format => "atom", :max_id => 100 }
2121 assert_response :redirect
2122 assert_redirected_to :action => :feed
2126 # check that the changeset download for a changeset with a redacted
2127 # element in it doesn't contain that element.
2128 def test_diff_download_redacted
2129 changeset = create(:changeset)
2130 node = create(:node, :with_history, :version => 2, :changeset => changeset)
2131 node_v1 = node.old_nodes.find_by(:version => 1)
2132 node_v1.redact!(create(:redaction))
2134 get :download, :params => { :id => changeset.id }
2135 assert_response :success
2137 assert_select "osmChange", 1
2138 # this changeset contains the node in versions 1 & 2, but 1 should
2140 assert_select "osmChange node[id='#{node.id}']", 1
2141 assert_select "osmChange node[id='#{node.id}'][version='1']", 0
2145 # create comment success
2146 def test_create_comment_success
2147 user = create(:user)
2148 user2 = create(:user)
2149 private_user = create(:user, :data_public => false)
2150 suspended_user = create(:user, :suspended)
2151 deleted_user = create(:user, :deleted)
2152 private_user_closed_changeset = create(:changeset, :closed, :user => private_user)
2154 basic_authorization user.email, "test"
2156 assert_difference "ChangesetComment.count", 1 do
2157 assert_no_difference "ActionMailer::Base.deliveries.size" do
2158 post :comment, :params => { :id => private_user_closed_changeset.id, :text => "This is a comment" }
2161 assert_response :success
2163 changeset = create(:changeset, :closed, :user => private_user)
2164 changeset.subscribers.push(private_user)
2165 changeset.subscribers.push(user)
2166 changeset.subscribers.push(suspended_user)
2167 changeset.subscribers.push(deleted_user)
2169 assert_difference "ChangesetComment.count", 1 do
2170 assert_difference "ActionMailer::Base.deliveries.size", 1 do
2171 post :comment, :params => { :id => changeset.id, :text => "This is a comment" }
2174 assert_response :success
2176 email = ActionMailer::Base.deliveries.first
2177 assert_equal 1, email.to.length
2178 assert_equal "[OpenStreetMap] #{user.display_name} has commented on one of your changesets", email.subject
2179 assert_equal private_user.email, email.to.first
2181 ActionMailer::Base.deliveries.clear
2183 basic_authorization user2.email, "test"
2185 assert_difference "ChangesetComment.count", 1 do
2186 assert_difference "ActionMailer::Base.deliveries.size", 2 do
2187 post :comment, :params => { :id => changeset.id, :text => "This is a comment" }
2190 assert_response :success
2192 email = ActionMailer::Base.deliveries.find { |e| e.to.first == private_user.email }
2193 assert_not_nil email
2194 assert_equal 1, email.to.length
2195 assert_equal "[OpenStreetMap] #{user2.display_name} has commented on one of your changesets", email.subject
2197 email = ActionMailer::Base.deliveries.find { |e| e.to.first == user.email }
2198 assert_not_nil email
2199 assert_equal 1, email.to.length
2200 assert_equal "[OpenStreetMap] #{user2.display_name} has commented on a changeset you are interested in", email.subject
2202 ActionMailer::Base.deliveries.clear
2206 # create comment fail
2207 def test_create_comment_fail
2209 post :comment, :params => { :id => create(:changeset, :closed).id, :text => "This is a comment" }
2210 assert_response :unauthorized
2212 basic_authorization create(:user).email, "test"
2215 assert_no_difference "ChangesetComment.count" do
2216 post :comment, :params => { :id => 999111, :text => "This is a comment" }
2218 assert_response :not_found
2220 # not closed changeset
2221 assert_no_difference "ChangesetComment.count" do
2222 post :comment, :params => { :id => create(:changeset).id, :text => "This is a comment" }
2224 assert_response :conflict
2227 assert_no_difference "ChangesetComment.count" do
2228 post :comment, :params => { :id => create(:changeset, :closed).id }
2230 assert_response :bad_request
2233 assert_no_difference "ChangesetComment.count" do
2234 post :comment, :params => { :id => create(:changeset, :closed).id, :text => "" }
2236 assert_response :bad_request
2240 # test subscribe success
2241 def test_subscribe_success
2242 basic_authorization create(:user).email, "test"
2243 changeset = create(:changeset, :closed)
2245 assert_difference "changeset.subscribers.count", 1 do
2246 post :subscribe, :params => { :id => changeset.id }
2248 assert_response :success
2252 # test subscribe fail
2253 def test_subscribe_fail
2254 user = create(:user)
2257 changeset = create(:changeset, :closed)
2258 assert_no_difference "changeset.subscribers.count" do
2259 post :subscribe, :params => { :id => changeset.id }
2261 assert_response :unauthorized
2263 basic_authorization user.email, "test"
2266 assert_no_difference "changeset.subscribers.count" do
2267 post :subscribe, :params => { :id => 999111 }
2269 assert_response :not_found
2271 # not closed changeset
2272 changeset = create(:changeset)
2273 assert_no_difference "changeset.subscribers.count" do
2274 post :subscribe, :params => { :id => changeset.id }
2276 assert_response :conflict
2278 # trying to subscribe when already subscribed
2279 changeset = create(:changeset, :closed)
2280 changeset.subscribers.push(user)
2281 assert_no_difference "changeset.subscribers.count" do
2282 post :subscribe, :params => { :id => changeset.id }
2284 assert_response :conflict
2288 # test unsubscribe success
2289 def test_unsubscribe_success
2290 user = create(:user)
2291 basic_authorization user.email, "test"
2292 changeset = create(:changeset, :closed)
2293 changeset.subscribers.push(user)
2295 assert_difference "changeset.subscribers.count", -1 do
2296 post :unsubscribe, :params => { :id => changeset.id }
2298 assert_response :success
2302 # test unsubscribe fail
2303 def test_unsubscribe_fail
2305 changeset = create(:changeset, :closed)
2306 assert_no_difference "changeset.subscribers.count" do
2307 post :unsubscribe, :params => { :id => changeset.id }
2309 assert_response :unauthorized
2311 basic_authorization create(:user).email, "test"
2314 assert_no_difference "changeset.subscribers.count" do
2315 post :unsubscribe, :params => { :id => 999111 }
2317 assert_response :not_found
2319 # not closed changeset
2320 changeset = create(:changeset)
2321 assert_no_difference "changeset.subscribers.count" do
2322 post :unsubscribe, :params => { :id => changeset.id }
2324 assert_response :conflict
2326 # trying to unsubscribe when not subscribed
2327 changeset = create(:changeset, :closed)
2328 assert_no_difference "changeset.subscribers.count" do
2329 post :unsubscribe, :params => { :id => changeset.id }
2331 assert_response :not_found
2335 # test hide comment fail
2336 def test_hide_comment_fail
2338 comment = create(:changeset_comment)
2339 assert_equal true, comment.visible
2341 post :hide_comment, :params => { :id => comment.id }
2342 assert_response :unauthorized
2343 assert_equal true, comment.reload.visible
2345 basic_authorization create(:user).email, "test"
2348 post :hide_comment, :params => { :id => comment.id }
2349 assert_response :forbidden
2350 assert_equal true, comment.reload.visible
2352 basic_authorization create(:moderator_user).email, "test"
2355 post :hide_comment, :params => { :id => 999111 }
2356 assert_response :not_found
2357 assert_equal true, comment.reload.visible
2361 # test hide comment succes
2362 def test_hide_comment_success
2363 comment = create(:changeset_comment)
2364 assert_equal true, comment.visible
2366 basic_authorization create(:moderator_user).email, "test"
2368 post :hide_comment, :params => { :id => comment.id }
2369 assert_response :success
2370 assert_equal false, comment.reload.visible
2374 # test unhide comment fail
2375 def test_unhide_comment_fail
2377 comment = create(:changeset_comment, :visible => false)
2378 assert_equal false, comment.visible
2380 post :unhide_comment, :params => { :id => comment.id }
2381 assert_response :unauthorized
2382 assert_equal false, comment.reload.visible
2384 basic_authorization create(:user).email, "test"
2387 post :unhide_comment, :params => { :id => comment.id }
2388 assert_response :forbidden
2389 assert_equal false, comment.reload.visible
2391 basic_authorization create(:moderator_user).email, "test"
2394 post :unhide_comment, :params => { :id => 999111 }
2395 assert_response :not_found
2396 assert_equal false, comment.reload.visible
2400 # test unhide comment succes
2401 def test_unhide_comment_success
2402 comment = create(:changeset_comment, :visible => false)
2403 assert_equal false, comment.visible
2405 basic_authorization create(:moderator_user).email, "test"
2407 post :unhide_comment, :params => { :id => comment.id }
2408 assert_response :success
2409 assert_equal true, comment.reload.visible
2413 # test comments feed
2414 def test_comments_feed
2415 changeset = create(:changeset, :closed)
2416 create_list(:changeset_comment, 3, :changeset => changeset)
2418 get :comments_feed, :params => { :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
2427 get :comments_feed, :params => { :format => "rss", :limit => 2 }
2428 assert_response :success
2429 assert_equal "application/rss+xml", @response.content_type
2430 assert_select "rss", :count => 1 do
2431 assert_select "channel", :count => 1 do
2432 assert_select "item", :count => 2
2436 get :comments_feed, :params => { :id => changeset.id, :format => "rss" }
2437 assert_response :success
2438 assert_equal "application/rss+xml", @response.content_type
2439 assert_select "rss", :count => 1 do
2440 assert_select "channel", :count => 1 do
2441 assert_select "item", :count => 3
2447 # test comments feed
2448 def test_comments_feed_bad_limit
2449 get :comments_feed, :params => { :format => "rss", :limit => 0 }
2450 assert_response :bad_request
2452 get :comments_feed, :params => { :format => "rss", :limit => 100001 }
2453 assert_response :bad_request
2459 # boilerplate for checking that certain changesets exist in the
2461 def assert_changesets(changesets)
2462 assert_select "osm>changeset", changesets.size
2463 changesets.each do |changeset|
2464 assert_select "osm>changeset[id='#{changeset.id}']", 1
2469 # call the include method and assert properties of the bbox
2470 def check_after_include(changeset_id, lon, lat, bbox)
2471 content "<osm><node lon='#{lon}' lat='#{lat}'/></osm>"
2472 post :expand_bbox, :params => { :id => changeset_id }
2473 assert_response :success, "Setting include of changeset failed: #{@response.body}"
2475 # check exactly one changeset
2476 assert_select "osm>changeset", 1
2477 assert_select "osm>changeset[id='#{changeset_id}']", 1
2480 doc = XML::Parser.string(@response.body).parse
2481 changeset = doc.find("//osm/changeset").first
2482 assert_equal bbox[0], changeset["min_lon"].to_f, "min lon"
2483 assert_equal bbox[1], changeset["min_lat"].to_f, "min lat"
2484 assert_equal bbox[2], changeset["max_lon"].to_f, "max lon"
2485 assert_equal bbox[3], changeset["max_lat"].to_f, "max lat"
2489 # update the changeset_id of a way element
2490 def update_changeset(xml, changeset_id)
2491 xml_attr_rewrite(xml, "changeset", changeset_id)
2495 # update an attribute in a way element
2496 def xml_attr_rewrite(xml, name, value)
2497 xml.find("//osm/way").first[name] = value.to_s
2502 # check the result of a list
2503 def check_list_result(changesets)
2504 changesets = changesets.where("num_changes > 0")
2505 .order(:created_at => :desc)
2507 assert changesets.size <= 20
2509 assert_select "ol.changesets", :count => [changesets.size, 1].min do
2510 assert_select "li", :count => changesets.size
2512 changesets.each do |changeset|
2513 assert_select "li#changeset_#{changeset.id}", :count => 1
2519 # check the result of a feed
2520 def check_feed_result(changesets)
2521 assert changesets.size <= 20
2523 assert_select "feed", :count => [changesets.size, 1].min do
2524 assert_select "> title", :count => 1, :text => /^Changesets/
2525 assert_select "> entry", :count => changesets.size
2527 changesets.each do |changeset|
2528 assert_select "> entry > id", changeset_url(:id => changeset.id)