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|
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 = <<CHANGESET.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, :params => { :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 = <<CHANGESET.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, :params => { :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 = <<CHANGESET.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, :params => { :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 = <<CHANGESET.strip_heredoc
943 <node id='1' lon='1' lat='1' changeset='#{changeset.id}'/>
950 post :upload, :params => { :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 = <<CHANGESET.strip_heredoc
965 <node id='1' lon='1' lat='1' changeset='#{changeset.id}' />
970 post :upload, :params => { :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 = <<CHANGESET.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, :params => { :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 = <<CHANGESET.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, :params => { :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 = <<CHANGESET.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, :params => { :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 = <<CHANGESET.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, :params => { :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 = <<CHANGESET.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, :params => { :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 = <<CHANGESET.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, :params => { :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 = <<CHANGESET.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, :params => { :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, :params => { :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, :params => { :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, :params => { :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, :params => { :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 = <<CHANGESET.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, :params => { :id => changeset_id }
1363 assert_response :success,
1364 "can't upload multiple versions of an element in a diff: #{@response.body}"
1366 get :download, :params => { :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, :params => { :id => changeset_id }
1422 assert_response :success,
1423 "can't upload a diff from JOSM: #{@response.body}"
1425 get :download, :params => { :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 = <<CHANGESET.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, :params => { :id => changeset_id }
1478 assert_response :success,
1479 "can't upload multiple versions of an element in a diff: #{@response.body}"
1481 get :download, :params => { :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, :params => { :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, :params => { :id => changeset_id }
1542 assert_response :success, "Couldn't read back changeset."
1543 assert_select "osm>changeset[min_lon='1.0000000']", 1
1544 assert_select "osm>changeset[max_lon='1.0000000']", 1
1545 assert_select "osm>changeset[min_lat='2.0000000']", 1
1546 assert_select "osm>changeset[max_lat='2.0000000']", 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, :params => { :id => changeset_id }
1557 assert_response :success, "Couldn't read back changeset for the second time."
1558 assert_select "osm>changeset[min_lon='1.0000000']", 1
1559 assert_select "osm>changeset[max_lon='2.0000000']", 1
1560 assert_select "osm>changeset[min_lat='1.0000000']", 1
1561 assert_select "osm>changeset[max_lat='2.0000000']", 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, :params => { :id => way.id }
1567 assert_response :success, "Couldn't delete a way."
1570 # get the bounding box back from the changeset
1571 get :read, :params => { :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.0000000']", 1
1575 assert_select "osm>changeset[max_lon='3.1000000']", 1
1576 assert_select "osm>changeset[min_lat='1.0000000']", 1
1577 assert_select "osm>changeset[max_lat='3.1000000']", 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, :params => { :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, :params => { :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, :params => { :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, :params => { :bbox => "-10,-10, 10, 10" }
1645 assert_response :success, "can't get changesets in bbox"
1646 assert_changesets [changeset2, changeset3]
1648 get :query, :params => { :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, :params => { :user => User.maximum(:id) + 1 }
1654 assert_response :not_found
1655 get :query, :params => { :display_name => " " }
1656 assert_response :not_found
1658 # can't get changesets of user 1 without authenticating
1659 get :query, :params => { :user => private_user.id }
1660 assert_response :not_found, "shouldn't be able to get changesets by non-public user (ID)"
1661 get :query, :params => { :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, :params => { :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, :params => { :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, :params => { :user => private_user.id,
1676 :display_name => private_user.display_name }
1677 assert_response :bad_request, "should be a bad request to have both ID and name specified"
1679 get :query, :params => { :user => private_user.id, :open => true }
1680 assert_response :success, "can't get changesets by user and open"
1681 assert_changesets [private_user_changeset]
1683 get :query, :params => { :time => "2007-12-31" }
1684 assert_response :success, "can't get changesets by time-since"
1685 assert_changesets [private_user_changeset, private_user_closed_changeset, changeset, closed_changeset, changeset2, changeset3]
1687 get :query, :params => { :time => "2008-01-01T12:34Z" }
1688 assert_response :success, "can't get changesets by time-since with hour"
1689 assert_changesets [private_user_changeset, private_user_closed_changeset, changeset, closed_changeset, changeset2, changeset3]
1691 get :query, :params => { :time => "2007-12-31T23:59Z,2008-01-02T00:01Z" }
1692 assert_response :success, "can't get changesets by time-range"
1693 assert_changesets [closed_changeset]
1695 get :query, :params => { :open => "true" }
1696 assert_response :success, "can't get changesets by open-ness"
1697 assert_changesets [private_user_changeset, changeset, changeset2, changeset3]
1699 get :query, :params => { :closed => "true" }
1700 assert_response :success, "can't get changesets by closed-ness"
1701 assert_changesets [private_user_closed_changeset, closed_changeset]
1703 get :query, :params => { :closed => "true", :user => private_user.id }
1704 assert_response :success, "can't get changesets by closed-ness and user"
1705 assert_changesets [private_user_closed_changeset]
1707 get :query, :params => { :closed => "true", :user => user.id }
1708 assert_response :success, "can't get changesets by closed-ness and user"
1709 assert_changesets [closed_changeset]
1711 get :query, :params => { :changesets => "#{private_user_changeset.id},#{changeset.id},#{closed_changeset.id}" }
1712 assert_response :success, "can't get changesets by id (as comma-separated string)"
1713 assert_changesets [private_user_changeset, changeset, closed_changeset]
1715 get :query, :params => { :changesets => "" }
1716 assert_response :bad_request, "should be a bad request since changesets is empty"
1720 # check that errors are returned if garbage is inserted
1721 # into query strings
1722 def test_query_invalid
1725 ";drop table users;"].each do |bbox|
1726 get :query, :params => { :bbox => bbox }
1727 assert_response :bad_request, "'#{bbox}' isn't a bbox"
1732 ";drop table users;",
1734 "-,-"].each do |time|
1735 get :query, :params => { :time => time }
1736 assert_response :bad_request, "'#{time}' isn't a valid time range"
1743 get :query, :params => { :user => uid }
1744 assert_response :bad_request, "'#{uid}' isn't a valid user ID"
1749 # check updating tags on a changeset
1750 def test_changeset_update
1751 private_user = create(:user, :data_public => false)
1752 private_changeset = create(:changeset, :user => private_user)
1753 user = create(:user)
1754 changeset = create(:changeset, :user => user)
1756 ## First try with a non-public user
1757 new_changeset = private_changeset.to_xml
1758 new_tag = XML::Node.new "tag"
1759 new_tag["k"] = "tagtesting"
1760 new_tag["v"] = "valuetesting"
1761 new_changeset.find("//osm/changeset").first << new_tag
1762 content new_changeset
1764 # try without any authorization
1765 put :update, :params => { :id => private_changeset.id }
1766 assert_response :unauthorized
1768 # try with the wrong authorization
1769 basic_authorization create(:user).email, "test"
1770 put :update, :params => { :id => private_changeset.id }
1771 assert_response :conflict
1773 # now this should get an unauthorized
1774 basic_authorization private_user.email, "test"
1775 put :update, :params => { :id => private_changeset.id }
1776 assert_require_public_data "user with their data non-public, shouldn't be able to edit their changeset"
1778 ## Now try with the public user
1779 create(:changeset_tag, :changeset => changeset)
1780 new_changeset = changeset.to_xml
1781 new_tag = XML::Node.new "tag"
1782 new_tag["k"] = "tagtesting"
1783 new_tag["v"] = "valuetesting"
1784 new_changeset.find("//osm/changeset").first << new_tag
1785 content new_changeset
1787 # try without any authorization
1788 @request.env["HTTP_AUTHORIZATION"] = nil
1789 put :update, :params => { :id => changeset.id }
1790 assert_response :unauthorized
1792 # try with the wrong authorization
1793 basic_authorization create(:user).email, "test"
1794 put :update, :params => { :id => changeset.id }
1795 assert_response :conflict
1797 # now this should work...
1798 basic_authorization user.email, "test"
1799 put :update, :params => { :id => changeset.id }
1800 assert_response :success
1802 assert_select "osm>changeset[id='#{changeset.id}']", 1
1803 assert_select "osm>changeset>tag", 2
1804 assert_select "osm>changeset>tag[k='tagtesting'][v='valuetesting']", 1
1808 # check that a user different from the one who opened the changeset
1810 def test_changeset_update_invalid
1811 basic_authorization create(:user).email, "test"
1813 changeset = create(:changeset)
1814 new_changeset = changeset.to_xml
1815 new_tag = XML::Node.new "tag"
1816 new_tag["k"] = "testing"
1817 new_tag["v"] = "testing"
1818 new_changeset.find("//osm/changeset").first << new_tag
1820 content new_changeset
1821 put :update, :params => { :id => changeset.id }
1822 assert_response :conflict
1826 # check that a changeset can contain a certain max number of changes.
1827 ## FIXME should be changed to an integration test due to the with_controller
1828 def test_changeset_limits
1829 basic_authorization create(:user).email, "test"
1831 # open a new changeset
1832 content "<osm><changeset/></osm>"
1834 assert_response :success, "can't create a new changeset"
1835 cs_id = @response.body.to_i
1837 # start the counter just short of where the changeset should finish.
1839 # alter the database to set the counter on the changeset directly,
1840 # otherwise it takes about 6 minutes to fill all of them.
1841 changeset = Changeset.find(cs_id)
1842 changeset.num_changes = Changeset::MAX_ELEMENTS - offset
1845 with_controller(NodeController.new) do
1847 content "<osm><node changeset='#{cs_id}' lat='0.0' lon='0.0'/></osm>"
1849 assert_response :success, "can't create a new node"
1850 node_id = @response.body.to_i
1852 get :read, :params => { :id => node_id }
1853 assert_response :success, "can't read back new node"
1854 node_doc = XML::Parser.string(@response.body).parse
1855 node_xml = node_doc.find("//osm/node").first
1857 # loop until we fill the changeset with nodes
1859 node_xml["lat"] = rand.to_s
1860 node_xml["lon"] = rand.to_s
1861 node_xml["version"] = (i + 1).to_s
1864 put :update, :params => { :id => node_id }
1865 assert_response :success, "attempt #{i} should have succeeded"
1868 # trying again should fail
1869 node_xml["lat"] = rand.to_s
1870 node_xml["lon"] = rand.to_s
1871 node_xml["version"] = offset.to_s
1874 put :update, :params => { :id => node_id }
1875 assert_response :conflict, "final attempt should have failed"
1878 changeset = Changeset.find(cs_id)
1879 assert_equal Changeset::MAX_ELEMENTS + 1, changeset.num_changes
1881 # check that the changeset is now closed as well
1882 assert(!changeset.is_open?,
1883 "changeset should have been auto-closed by exceeding " \
1888 # This should display the last 20 changesets closed
1890 get :list, :params => { :format => "html" }
1891 assert_response :success
1892 assert_template "history"
1893 assert_template :layout => "map"
1894 assert_select "h2", :text => "Changesets", :count => 1
1896 get :list, :params => { :format => "html", :list => "1" }, :xhr => true
1897 assert_response :success
1898 assert_template "list"
1900 check_list_result(Changeset.all)
1904 # This should display the last 20 changesets closed
1906 get :list, :params => { :format => "html" }, :xhr => true
1907 assert_response :success
1908 assert_template "history"
1909 assert_template :layout => "xhr"
1910 assert_select "h2", :text => "Changesets", :count => 1
1912 get :list, :params => { :format => "html", :list => "1" }, :xhr => true
1913 assert_response :success
1914 assert_template "list"
1916 check_list_result(Changeset.all)
1920 # This should display the last 20 changesets closed in a specific area
1922 get :list, :params => { :format => "html", :bbox => "4.5,4.5,5.5,5.5" }
1923 assert_response :success
1924 assert_template "history"
1925 assert_template :layout => "map"
1926 assert_select "h2", :text => "Changesets", :count => 1
1928 get :list, :params => { :format => "html", :bbox => "4.5,4.5,5.5,5.5", :list => "1" }, :xhr => true
1929 assert_response :success
1930 assert_template "list"
1932 check_list_result(Changeset.where("min_lon < 55000000 and max_lon > 45000000 and min_lat < 55000000 and max_lat > 45000000"))
1936 # Checks the display of the user changesets listing
1938 user = create(:user)
1939 create(:changeset, :user => user)
1940 create(:changeset, :closed, :user => user)
1942 get :list, :params => { :format => "html", :display_name => user.display_name }
1943 assert_response :success
1944 assert_template "history"
1946 get :list, :params => { :format => "html", :display_name => user.display_name, :list => "1" }, :xhr => true
1947 assert_response :success
1948 assert_template "list"
1950 check_list_result(user.changesets)
1954 # Checks the display of the user changesets listing for a private user
1955 def test_list_private_user
1956 private_user = create(:user, :data_public => false)
1957 create(:changeset, :user => private_user)
1958 create(:changeset, :closed, :user => private_user)
1960 get :list, :params => { :format => "html", :display_name => private_user.display_name }
1961 assert_response :success
1962 assert_template "history"
1964 get :list, :params => { :format => "html", :display_name => private_user.display_name, :list => "1" }, :xhr => true
1965 assert_response :success
1966 assert_template "list"
1968 check_list_result(Changeset.none)
1972 # Check the not found of the list user changesets
1973 def test_list_user_not_found
1974 get :list, :params => { :format => "html", :display_name => "Some random user" }
1975 assert_response :not_found
1976 assert_template "user/no_such_user"
1978 get :list, :params => { :format => "html", :display_name => "Some random user", :list => "1" }, :xhr => true
1979 assert_response :not_found
1980 assert_template "user/no_such_user"
1984 # Checks the display of the friends changesets listing
1985 def test_list_friends
1986 private_user = create(:user, :data_public => true)
1987 friend = create(:friend, :befriender => private_user)
1988 create(:changeset, :user => friend.befriendee)
1990 get :list, :params => { :friends => true }
1991 assert_response :redirect
1992 assert_redirected_to :controller => :user, :action => :login, :referer => friend_changesets_path
1994 session[:user] = private_user.id
1996 get :list, :params => { :friends => true }
1997 assert_response :success
1998 assert_template "history"
2000 get :list, :params => { :friends => true, :list => "1" }, :xhr => true
2001 assert_response :success
2002 assert_template "list"
2004 check_list_result(Changeset.where(:user => private_user.friend_users.identifiable))
2008 # Checks the display of the nearby user changesets listing
2009 def test_list_nearby
2010 private_user = create(:user, :data_public => false, :home_lat => 51.1, :home_lon => 1.0)
2011 user = create(:user, :home_lat => 51.0, :home_lon => 1.0)
2012 create(:changeset, :user => user)
2014 get :list, :params => { :nearby => true }
2015 assert_response :redirect
2016 assert_redirected_to :controller => :user, :action => :login, :referer => nearby_changesets_path
2018 session[:user] = private_user.id
2020 get :list, :params => { :nearby => true }
2021 assert_response :success
2022 assert_template "history"
2024 get :list, :params => { :nearby => true, :list => "1" }, :xhr => true
2025 assert_response :success
2026 assert_template "list"
2028 check_list_result(Changeset.where(:user => user.nearby))
2032 # Check that we can't request later pages of the changesets list
2033 def test_list_max_id
2034 get :list, :params => { :format => "html", :max_id => 4 }, :xhr => true
2035 assert_response :success
2036 assert_template "history"
2037 assert_template :layout => "xhr"
2038 assert_select "h2", :text => "Changesets", :count => 1
2040 get :list, :params => { :format => "html", :list => "1", :max_id => 4 }, :xhr => true
2041 assert_response :success
2042 assert_template "list"
2044 check_list_result(Changeset.where("id <= 4"))
2048 # Check that a list with a next page link works
2050 create_list(:changeset, 50)
2052 get :list, :params => { :format => "html" }
2053 assert_response :success
2055 get :list, :params => { :format => "html" }, :xhr => true
2056 assert_response :success
2060 # This should display the last 20 non-empty changesets
2062 changeset = create(:changeset, :num_changes => 1)
2063 create(:changeset_tag, :changeset => changeset)
2064 create(:changeset_tag, :changeset => changeset, :k => "website", :v => "http://example.com/")
2065 closed_changeset = create(:changeset, :closed, :num_changes => 1)
2066 _empty_changeset = create(:changeset, :num_changes => 0)
2068 get :feed, :params => { :format => :atom }
2069 assert_response :success
2070 assert_template "list"
2071 assert_equal "application/atom+xml", response.content_type
2073 check_feed_result([changeset, closed_changeset])
2077 # This should display the last 20 changesets closed in a specific area
2079 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)
2080 create(:changeset_tag, :changeset => changeset)
2081 create(:changeset_tag, :changeset => changeset, :k => "website", :v => "http://example.com/")
2082 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)
2083 _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)
2084 _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)
2086 get :feed, :params => { :format => :atom, :bbox => "4.5,4.5,5.5,5.5" }
2087 assert_response :success
2088 assert_template "list"
2089 assert_equal "application/atom+xml", response.content_type
2091 check_feed_result([changeset, closed_changeset])
2095 # Checks the display of the user changesets feed
2097 user = create(:user)
2098 changesets = create_list(:changeset, 3, :user => user, :num_changes => 4)
2099 create(:changeset_tag, :changeset => changesets[1])
2100 create(:changeset_tag, :changeset => changesets[1], :k => "website", :v => "http://example.com/")
2101 _other_changeset = create(:changeset)
2103 get :feed, :params => { :format => :atom, :display_name => user.display_name }
2105 assert_response :success
2106 assert_template "list"
2107 assert_equal "application/atom+xml", response.content_type
2109 check_feed_result(changesets)
2113 # Check the not found of the user changesets feed
2114 def test_feed_user_not_found
2115 get :feed, :params => { :format => "atom", :display_name => "Some random user" }
2116 assert_response :not_found
2120 # Check that we can't request later pages of the changesets feed
2121 def test_feed_max_id
2122 get :feed, :params => { :format => "atom", :max_id => 100 }
2123 assert_response :redirect
2124 assert_redirected_to :action => :feed
2128 # check that the changeset download for a changeset with a redacted
2129 # element in it doesn't contain that element.
2130 def test_diff_download_redacted
2131 changeset = create(:changeset)
2132 node = create(:node, :with_history, :version => 2, :changeset => changeset)
2133 node_v1 = node.old_nodes.find_by(:version => 1)
2134 node_v1.redact!(create(:redaction))
2136 get :download, :params => { :id => changeset.id }
2137 assert_response :success
2139 assert_select "osmChange", 1
2140 # this changeset contains the node in versions 1 & 2, but 1 should
2142 assert_select "osmChange node[id='#{node.id}']", 1
2143 assert_select "osmChange node[id='#{node.id}'][version='1']", 0
2147 # create comment success
2148 def test_create_comment_success
2149 user = create(:user)
2150 user2 = create(:user)
2151 private_user = create(:user, :data_public => false)
2152 suspended_user = create(:user, :suspended)
2153 deleted_user = create(:user, :deleted)
2154 private_user_closed_changeset = create(:changeset, :closed, :user => private_user)
2156 basic_authorization user.email, "test"
2158 assert_difference "ChangesetComment.count", 1 do
2159 assert_no_difference "ActionMailer::Base.deliveries.size" do
2160 post :comment, :params => { :id => private_user_closed_changeset.id, :text => "This is a comment" }
2163 assert_response :success
2165 changeset = create(:changeset, :closed, :user => private_user)
2166 changeset.subscribers.push(private_user)
2167 changeset.subscribers.push(user)
2168 changeset.subscribers.push(suspended_user)
2169 changeset.subscribers.push(deleted_user)
2171 assert_difference "ChangesetComment.count", 1 do
2172 assert_difference "ActionMailer::Base.deliveries.size", 1 do
2173 post :comment, :params => { :id => changeset.id, :text => "This is a comment" }
2176 assert_response :success
2178 email = ActionMailer::Base.deliveries.first
2179 assert_equal 1, email.to.length
2180 assert_equal "[OpenStreetMap] #{user.display_name} has commented on one of your changesets", email.subject
2181 assert_equal private_user.email, email.to.first
2183 ActionMailer::Base.deliveries.clear
2185 basic_authorization user2.email, "test"
2187 assert_difference "ChangesetComment.count", 1 do
2188 assert_difference "ActionMailer::Base.deliveries.size", 2 do
2189 post :comment, :params => { :id => changeset.id, :text => "This is a comment" }
2192 assert_response :success
2194 email = ActionMailer::Base.deliveries.find { |e| e.to.first == private_user.email }
2195 assert_not_nil email
2196 assert_equal 1, email.to.length
2197 assert_equal "[OpenStreetMap] #{user2.display_name} has commented on one of your changesets", email.subject
2199 email = ActionMailer::Base.deliveries.find { |e| e.to.first == user.email }
2200 assert_not_nil email
2201 assert_equal 1, email.to.length
2202 assert_equal "[OpenStreetMap] #{user2.display_name} has commented on a changeset you are interested in", email.subject
2204 ActionMailer::Base.deliveries.clear
2208 # create comment fail
2209 def test_create_comment_fail
2211 post :comment, :params => { :id => create(:changeset, :closed).id, :text => "This is a comment" }
2212 assert_response :unauthorized
2214 basic_authorization create(:user).email, "test"
2217 assert_no_difference "ChangesetComment.count" do
2218 post :comment, :params => { :id => 999111, :text => "This is a comment" }
2220 assert_response :not_found
2222 # not closed changeset
2223 assert_no_difference "ChangesetComment.count" do
2224 post :comment, :params => { :id => create(:changeset).id, :text => "This is a comment" }
2226 assert_response :conflict
2229 assert_no_difference "ChangesetComment.count" do
2230 post :comment, :params => { :id => create(:changeset, :closed).id }
2232 assert_response :bad_request
2235 assert_no_difference "ChangesetComment.count" do
2236 post :comment, :params => { :id => create(:changeset, :closed).id, :text => "" }
2238 assert_response :bad_request
2242 # test subscribe success
2243 def test_subscribe_success
2244 basic_authorization create(:user).email, "test"
2245 changeset = create(:changeset, :closed)
2247 assert_difference "changeset.subscribers.count", 1 do
2248 post :subscribe, :params => { :id => changeset.id }
2250 assert_response :success
2254 # test subscribe fail
2255 def test_subscribe_fail
2256 user = create(:user)
2259 changeset = create(:changeset, :closed)
2260 assert_no_difference "changeset.subscribers.count" do
2261 post :subscribe, :params => { :id => changeset.id }
2263 assert_response :unauthorized
2265 basic_authorization user.email, "test"
2268 assert_no_difference "changeset.subscribers.count" do
2269 post :subscribe, :params => { :id => 999111 }
2271 assert_response :not_found
2273 # not closed changeset
2274 changeset = create(:changeset)
2275 assert_no_difference "changeset.subscribers.count" do
2276 post :subscribe, :params => { :id => changeset.id }
2278 assert_response :conflict
2280 # trying to subscribe when already subscribed
2281 changeset = create(:changeset, :closed)
2282 changeset.subscribers.push(user)
2283 assert_no_difference "changeset.subscribers.count" do
2284 post :subscribe, :params => { :id => changeset.id }
2286 assert_response :conflict
2290 # test unsubscribe success
2291 def test_unsubscribe_success
2292 user = create(:user)
2293 basic_authorization user.email, "test"
2294 changeset = create(:changeset, :closed)
2295 changeset.subscribers.push(user)
2297 assert_difference "changeset.subscribers.count", -1 do
2298 post :unsubscribe, :params => { :id => changeset.id }
2300 assert_response :success
2304 # test unsubscribe fail
2305 def test_unsubscribe_fail
2307 changeset = create(:changeset, :closed)
2308 assert_no_difference "changeset.subscribers.count" do
2309 post :unsubscribe, :params => { :id => changeset.id }
2311 assert_response :unauthorized
2313 basic_authorization create(:user).email, "test"
2316 assert_no_difference "changeset.subscribers.count" do
2317 post :unsubscribe, :params => { :id => 999111 }
2319 assert_response :not_found
2321 # not closed changeset
2322 changeset = create(:changeset)
2323 assert_no_difference "changeset.subscribers.count" do
2324 post :unsubscribe, :params => { :id => changeset.id }
2326 assert_response :conflict
2328 # trying to unsubscribe when not subscribed
2329 changeset = create(:changeset, :closed)
2330 assert_no_difference "changeset.subscribers.count" do
2331 post :unsubscribe, :params => { :id => changeset.id }
2333 assert_response :not_found
2337 # test hide comment fail
2338 def test_hide_comment_fail
2340 comment = create(:changeset_comment)
2341 assert_equal true, comment.visible
2343 post :hide_comment, :params => { :id => comment.id }
2344 assert_response :unauthorized
2345 assert_equal true, comment.reload.visible
2347 basic_authorization create(:user).email, "test"
2350 post :hide_comment, :params => { :id => comment.id }
2351 assert_response :forbidden
2352 assert_equal true, comment.reload.visible
2354 basic_authorization create(:moderator_user).email, "test"
2357 post :hide_comment, :params => { :id => 999111 }
2358 assert_response :not_found
2359 assert_equal true, comment.reload.visible
2363 # test hide comment succes
2364 def test_hide_comment_success
2365 comment = create(:changeset_comment)
2366 assert_equal true, comment.visible
2368 basic_authorization create(:moderator_user).email, "test"
2370 post :hide_comment, :params => { :id => comment.id }
2371 assert_response :success
2372 assert_equal false, comment.reload.visible
2376 # test unhide comment fail
2377 def test_unhide_comment_fail
2379 comment = create(:changeset_comment, :visible => false)
2380 assert_equal false, comment.visible
2382 post :unhide_comment, :params => { :id => comment.id }
2383 assert_response :unauthorized
2384 assert_equal false, comment.reload.visible
2386 basic_authorization create(:user).email, "test"
2389 post :unhide_comment, :params => { :id => comment.id }
2390 assert_response :forbidden
2391 assert_equal false, comment.reload.visible
2393 basic_authorization create(:moderator_user).email, "test"
2396 post :unhide_comment, :params => { :id => 999111 }
2397 assert_response :not_found
2398 assert_equal false, comment.reload.visible
2402 # test unhide comment succes
2403 def test_unhide_comment_success
2404 comment = create(:changeset_comment, :visible => false)
2405 assert_equal false, comment.visible
2407 basic_authorization create(:moderator_user).email, "test"
2409 post :unhide_comment, :params => { :id => comment.id }
2410 assert_response :success
2411 assert_equal true, comment.reload.visible
2415 # test comments feed
2416 def test_comments_feed
2417 changeset = create(:changeset, :closed)
2418 create_list(:changeset_comment, 3, :changeset => changeset)
2420 get :comments_feed, :params => { :format => "rss" }
2421 assert_response :success
2422 assert_equal "application/rss+xml", @response.content_type
2423 assert_select "rss", :count => 1 do
2424 assert_select "channel", :count => 1 do
2425 assert_select "item", :count => 3
2429 get :comments_feed, :params => { :format => "rss", :limit => 2 }
2430 assert_response :success
2431 assert_equal "application/rss+xml", @response.content_type
2432 assert_select "rss", :count => 1 do
2433 assert_select "channel", :count => 1 do
2434 assert_select "item", :count => 2
2438 get :comments_feed, :params => { :id => changeset.id, :format => "rss" }
2439 assert_response :success
2440 assert_equal "application/rss+xml", @response.content_type
2441 assert_select "rss", :count => 1 do
2442 assert_select "channel", :count => 1 do
2443 assert_select "item", :count => 3
2449 # test comments feed
2450 def test_comments_feed_bad_limit
2451 get :comments_feed, :params => { :format => "rss", :limit => 0 }
2452 assert_response :bad_request
2454 get :comments_feed, :params => { :format => "rss", :limit => 100001 }
2455 assert_response :bad_request
2461 # boilerplate for checking that certain changesets exist in the
2463 def assert_changesets(changesets)
2464 assert_select "osm>changeset", changesets.size
2465 changesets.each do |changeset|
2466 assert_select "osm>changeset[id='#{changeset.id}']", 1
2471 # call the include method and assert properties of the bbox
2472 def check_after_include(changeset_id, lon, lat, bbox)
2473 content "<osm><node lon='#{lon}' lat='#{lat}'/></osm>"
2474 post :expand_bbox, :params => { :id => changeset_id }
2475 assert_response :success, "Setting include of changeset failed: #{@response.body}"
2477 # check exactly one changeset
2478 assert_select "osm>changeset", 1
2479 assert_select "osm>changeset[id='#{changeset_id}']", 1
2482 doc = XML::Parser.string(@response.body).parse
2483 changeset = doc.find("//osm/changeset").first
2484 assert_equal bbox[0], changeset["min_lon"].to_f, "min lon"
2485 assert_equal bbox[1], changeset["min_lat"].to_f, "min lat"
2486 assert_equal bbox[2], changeset["max_lon"].to_f, "max lon"
2487 assert_equal bbox[3], changeset["max_lat"].to_f, "max lat"
2491 # update the changeset_id of a way element
2492 def update_changeset(xml, changeset_id)
2493 xml_attr_rewrite(xml, "changeset", changeset_id)
2497 # update an attribute in a way element
2498 def xml_attr_rewrite(xml, name, value)
2499 xml.find("//osm/way").first[name] = value.to_s
2504 # check the result of a list
2505 def check_list_result(changesets)
2506 changesets = changesets.where("num_changes > 0")
2507 .order(:created_at => :desc)
2509 assert changesets.size <= 20
2511 assert_select "ol.changesets", :count => [changesets.size, 1].min do
2512 assert_select "li", :count => changesets.size
2514 changesets.each do |changeset|
2515 assert_select "li#changeset_#{changeset.id}", :count => 1
2521 # check the result of a feed
2522 def check_feed_result(changesets)
2523 assert changesets.size <= 20
2525 assert_select "feed", :count => [changesets.size, 1].min do
2526 assert_select "> title", :count => 1, :text => /^Changesets/
2527 assert_select "> entry", :count => changesets.size
2529 changesets.each do |changeset|
2530 assert_select "> entry > id", changeset_url(:id => changeset.id)