2 require "changeset_controller"
4 class ChangesetControllerTest < ActionController::TestCase
8 # test all routes which lead to this controller
11 { :path => "/api/0.6/changeset/create", :method => :put },
12 { :controller => "changeset", :action => "create" }
15 { :path => "/api/0.6/changeset/1/upload", :method => :post },
16 { :controller => "changeset", :action => "upload", :id => "1" }
19 { :path => "/api/0.6/changeset/1/download", :method => :get },
20 { :controller => "changeset", :action => "download", :id => "1" }
23 { :path => "/api/0.6/changeset/1/expand_bbox", :method => :post },
24 { :controller => "changeset", :action => "expand_bbox", :id => "1" }
27 { :path => "/api/0.6/changeset/1", :method => :get },
28 { :controller => "changeset", :action => "read", :id => "1" }
31 { :path => "/api/0.6/changeset/1/subscribe", :method => :post },
32 { :controller => "changeset", :action => "subscribe", :id => "1" }
35 { :path => "/api/0.6/changeset/1/unsubscribe", :method => :post },
36 { :controller => "changeset", :action => "unsubscribe", :id => "1" }
39 { :path => "/api/0.6/changeset/1", :method => :put },
40 { :controller => "changeset", :action => "update", :id => "1" }
43 { :path => "/api/0.6/changeset/1/close", :method => :put },
44 { :controller => "changeset", :action => "close", :id => "1" }
47 { :path => "/api/0.6/changeset/1/comment", :method => :post },
48 { :controller => "changeset", :action => "comment", :id => "1" }
51 { :path => "/api/0.6/changeset/comment/1/hide", :method => :post },
52 { :controller => "changeset", :action => "hide_comment", :id => "1" }
55 { :path => "/api/0.6/changeset/comment/1/unhide", :method => :post },
56 { :controller => "changeset", :action => "unhide_comment", :id => "1" }
59 { :path => "/api/0.6/changesets", :method => :get },
60 { :controller => "changeset", :action => "query" }
63 { :path => "/changeset/1/comments/feed", :method => :get },
64 { :controller => "changeset", :action => "comments_feed", :id => "1", :format => "rss" }
67 { :path => "/user/name/history", :method => :get },
68 { :controller => "changeset", :action => "list", :display_name => "name" }
71 { :path => "/user/name/history/feed", :method => :get },
72 { :controller => "changeset", :action => "feed", :display_name => "name", :format => :atom }
75 { :path => "/history/friends", :method => :get },
76 { :controller => "changeset", :action => "list", :friends => true, :format => :html }
79 { :path => "/history/nearby", :method => :get },
80 { :controller => "changeset", :action => "list", :nearby => true, :format => :html }
83 { :path => "/history", :method => :get },
84 { :controller => "changeset", :action => "list" }
87 { :path => "/history/feed", :method => :get },
88 { :controller => "changeset", :action => "feed", :format => :atom }
91 { :path => "/history/comments/feed", :method => :get },
92 { :controller => "changeset", :action => "comments_feed", :format => "rss" }
96 # -----------------------
97 # Test simple changeset creation
98 # -----------------------
101 basic_authorization create(:user, :data_public => false).email, "test"
102 # Create the first user's changeset
103 content "<osm><changeset>" +
104 "<tag k='created_by' v='osm test suite checking changesets'/>" +
107 assert_require_public_data
109 basic_authorization create(:user).email, "test"
110 # Create the first user's changeset
111 content "<osm><changeset>" +
112 "<tag k='created_by' v='osm test suite checking changesets'/>" +
116 assert_response :success, "Creation of changeset did not return sucess status"
117 newid = @response.body.to_i
119 # check end time, should be an hour ahead of creation time
120 cs = Changeset.find(newid)
121 duration = cs.closed_at - cs.created_at
122 # the difference can either be a rational, or a floating point number
123 # of seconds, depending on the code path taken :-(
124 if duration.class == Rational
125 assert_equal Rational(1, 24), duration, "initial idle timeout should be an hour (#{cs.created_at} -> #{cs.closed_at})"
127 # must be number of seconds...
128 assert_equal 3600, duration.round, "initial idle timeout should be an hour (#{cs.created_at} -> #{cs.closed_at})"
131 # checks if uploader was subscribed
132 assert_equal 1, cs.subscribers.length
135 def test_create_invalid
136 basic_authorization create(:user, :data_public => false).email, "test"
137 content "<osm><changeset></osm>"
139 assert_require_public_data
141 ## Try the public user
142 basic_authorization create(:user).email, "test"
143 content "<osm><changeset></osm>"
145 assert_response :bad_request, "creating a invalid changeset should fail"
148 def test_create_invalid_no_content
149 ## First check with no auth
151 assert_response :unauthorized, "shouldn't be able to create a changeset with no auth"
153 ## Now try to with a non-public user
154 basic_authorization create(:user, :data_public => false).email, "test"
156 assert_require_public_data
158 ## Try an inactive user
159 basic_authorization create(:user, :pending).email, "test"
163 ## Now try to use a normal user
164 basic_authorization create(:user).email, "test"
166 assert_response :bad_request, "creating a changeset with no content should fail"
169 def test_create_wrong_method
170 basic_authorization create(:user).email, "test"
172 assert_response :method_not_allowed
174 assert_response :method_not_allowed
178 # check that the changeset can be read and returns the correct
179 # document structure.
181 changeset_id = create(:changeset).id
183 get :read, :id => changeset_id
184 assert_response :success, "cannot get first changeset"
186 assert_select "osm[version='#{API_VERSION}'][generator='OpenStreetMap server']", 1
187 assert_select "osm>changeset[id='#{changeset_id}']", 1
188 assert_select "osm>changeset>discussion", 0
190 get :read, :id => changeset_id, :include_discussion => true
191 assert_response :success, "cannot get first changeset with comments"
193 assert_select "osm[version='#{API_VERSION}'][generator='OpenStreetMap server']", 1
194 assert_select "osm>changeset[id='#{changeset_id}']", 1
195 assert_select "osm>changeset>discussion", 1
196 assert_select "osm>changeset>discussion>comment", 0
198 changeset_id = create(:changeset, :closed).id
199 create_list(:changeset_comment, 3, :changeset_id => changeset_id)
201 get :read, :id => changeset_id, :include_discussion => true
202 assert_response :success, "cannot get closed changeset with comments"
204 assert_select "osm[version='#{API_VERSION}'][generator='OpenStreetMap server']", 1
205 assert_select "osm>changeset[id='#{changeset_id}']", 1
206 assert_select "osm>changeset>discussion", 1
207 assert_select "osm>changeset>discussion>comment", 3
211 # check that a changeset that doesn't exist returns an appropriate message
212 def test_read_not_found
213 [0, -32, 233455644, "afg", "213"].each do |id|
216 assert_response :not_found, "should get a not found"
217 rescue ActionController::UrlGenerationError => ex
218 assert_match /No route matches/, ex.to_s
224 # test that the user who opened a change can close it
226 private_user = create(:user, :data_public => false)
227 private_changeset = create(:changeset, :user => private_user)
229 changeset = create(:changeset, :user => user)
231 ## Try without authentication
232 put :close, :id => changeset.id
233 assert_response :unauthorized
235 ## Try using the non-public user
236 basic_authorization private_user.email, "test"
237 put :close, :id => private_changeset.id
238 assert_require_public_data
240 ## The try with the public user
241 basic_authorization user.email, "test"
244 put :close, :id => cs_id
245 assert_response :success
247 # test that it really is closed now
248 cs = Changeset.find(cs_id)
250 "changeset should be closed now (#{cs.closed_at} > #{Time.now.getutc}.")
254 # test that a different user can't close another user's changeset
255 def test_close_invalid
257 changeset = create(:changeset)
259 basic_authorization user.email, "test"
261 put :close, :id => changeset.id
262 assert_response :conflict
263 assert_equal "The user doesn't own that changeset", @response.body
267 # test that you can't close using another method
268 def test_close_method_invalid
270 changeset = create(:changeset, :user => user)
272 basic_authorization user.email, "test"
274 get :close, :id => changeset.id
275 assert_response :method_not_allowed
277 post :close, :id => changeset.id
278 assert_response :method_not_allowed
282 # check that you can't close a changeset that isn't found
283 def test_close_not_found
284 cs_ids = [0, -132, "123"]
286 # First try to do it with no auth
289 put :close, :id => id
290 assert_response :unauthorized, "Shouldn't be able close the non-existant changeset #{id}, when not authorized"
291 rescue ActionController::UrlGenerationError => ex
292 assert_match /No route matches/, ex.to_s
297 basic_authorization create(:user).email, "test"
300 put :close, :id => id
301 assert_response :not_found, "The changeset #{id} doesn't exist, so can't be closed"
302 rescue ActionController::UrlGenerationError => ex
303 assert_match /No route matches/, ex.to_s
309 # upload something simple, but valid and check that it can
311 # Also try without auth and another user.
312 def test_upload_simple_valid
313 private_user = create(:user, :data_public => false)
314 private_changeset = create(:changeset, :user => private_user)
316 changeset = create(:changeset, :user => user)
320 relation = create(:relation)
321 other_relation = create(:relation)
322 # create some tags, since we test that they are removed later
323 create(:node_tag, :node => node)
324 create(:way_tag, :way => way)
325 create(:relation_tag, :relation => relation)
328 changeset_id = changeset.id
330 # simple diff to change a node, way and relation by removing
335 <node id='#{node.id}' lon='0' lat='0' changeset='#{changeset_id}' version='1'/>
336 <way id='#{way.id}' changeset='#{changeset_id}' version='1'>
337 <nd ref='#{node.id}'/>
341 <relation id='#{relation.id}' changeset='#{changeset_id}' version='1'>
342 <member type='way' role='some' ref='#{way.id}'/>
343 <member type='node' role='some' ref='#{node.id}'/>
344 <member type='relation' role='some' ref='#{other_relation.id}'/>
352 post :upload, :id => changeset_id
353 assert_response :unauthorized,
354 "shouldn't be able to upload a simple valid diff to changeset: #{@response.body}"
356 ## Now try with a private user
357 basic_authorization private_user.email, "test"
358 changeset_id = private_changeset.id
360 # simple diff to change a node, way and relation by removing
365 <node id='#{node.id}' lon='0' lat='0' changeset='#{changeset_id}' version='1'/>
366 <way id='#{way.id}' changeset='#{changeset_id}' version='1'>
367 <nd ref='#{node.id}'/>
371 <relation id='#{relation.id}' changeset='#{changeset_id}' version='1'>
372 <member type='way' role='some' ref='#{way.id}'/>
373 <member type='node' role='some' ref='#{node.id}'/>
374 <member type='relation' role='some' ref='#{other_relation.id}'/>
382 post :upload, :id => changeset_id
383 assert_response :forbidden,
384 "can't upload a simple valid diff to changeset: #{@response.body}"
386 ## Now try with the public user
387 basic_authorization user.email, "test"
388 changeset_id = changeset.id
390 # simple diff to change a node, way and relation by removing
395 <node id='#{node.id}' lon='0' lat='0' changeset='#{changeset_id}' version='1'/>
396 <way id='#{way.id}' changeset='#{changeset_id}' version='1'>
397 <nd ref='#{node.id}'/>
401 <relation id='#{relation.id}' changeset='#{changeset_id}' version='1'>
402 <member type='way' role='some' ref='#{way.id}'/>
403 <member type='node' role='some' ref='#{node.id}'/>
404 <member type='relation' role='some' ref='#{other_relation.id}'/>
412 post :upload, :id => changeset_id
413 assert_response :success,
414 "can't upload a simple valid diff to changeset: #{@response.body}"
416 # check that the changes made it into the database
417 assert_equal 0, Node.find(node.id).tags.size, "node #{node.id} should now have no tags"
418 assert_equal 0, Way.find(way.id).tags.size, "way #{way.id} should now have no tags"
419 assert_equal 0, Relation.find(relation.id).tags.size, "relation #{relation.id} should now have no tags"
423 # upload something which creates new objects using placeholders
424 def test_upload_create_valid
426 changeset = create(:changeset, :user => user)
428 basic_authorization user.email, "test"
430 # simple diff to create a node way and relation using placeholders
434 <node id='-1' lon='0' lat='0' changeset='#{changeset.id}'>
435 <tag k='foo' v='bar'/>
436 <tag k='baz' v='bat'/>
438 <way id='-1' changeset='#{changeset.id}'>
443 <relation id='-1' changeset='#{changeset.id}'>
444 <member type='way' role='some' ref='3'/>
445 <member type='node' role='some' ref='5'/>
446 <member type='relation' role='some' ref='3'/>
454 post :upload, :id => changeset.id
455 assert_response :success,
456 "can't upload a simple valid creation to changeset: #{@response.body}"
458 # check the returned payload
459 assert_select "diffResult[version='#{API_VERSION}'][generator='OpenStreetMap server']", 1
460 assert_select "diffResult>node", 1
461 assert_select "diffResult>way", 1
462 assert_select "diffResult>relation", 1
464 # inspect the response to find out what the new element IDs are
465 doc = XML::Parser.string(@response.body).parse
466 new_node_id = doc.find("//diffResult/node").first["new_id"].to_i
467 new_way_id = doc.find("//diffResult/way").first["new_id"].to_i
468 new_rel_id = doc.find("//diffResult/relation").first["new_id"].to_i
470 # check the old IDs are all present and negative one
471 assert_equal -1, doc.find("//diffResult/node").first["old_id"].to_i
472 assert_equal -1, doc.find("//diffResult/way").first["old_id"].to_i
473 assert_equal -1, doc.find("//diffResult/relation").first["old_id"].to_i
475 # check the versions are present and equal one
476 assert_equal 1, doc.find("//diffResult/node").first["new_version"].to_i
477 assert_equal 1, doc.find("//diffResult/way").first["new_version"].to_i
478 assert_equal 1, doc.find("//diffResult/relation").first["new_version"].to_i
480 # check that the changes made it into the database
481 assert_equal 2, Node.find(new_node_id).tags.size, "new node should have two tags"
482 assert_equal 0, Way.find(new_way_id).tags.size, "new way should have no tags"
483 assert_equal 0, Relation.find(new_rel_id).tags.size, "new relation should have no tags"
487 # test a complex delete where we delete elements which rely on eachother
488 # in the same transaction.
489 def test_upload_delete
490 changeset = create(:changeset)
491 super_relation = create(:relation)
492 used_relation = create(:relation)
493 used_way = create(:way)
494 used_node = create(:node)
495 create(:relation_member, :relation => super_relation, :member => used_relation)
496 create(:relation_member, :relation => super_relation, :member => used_way)
497 create(:relation_member, :relation => super_relation, :member => used_node)
499 basic_authorization changeset.user.display_name, "test"
501 diff = XML::Document.new
502 diff.root = XML::Node.new "osmChange"
503 delete = XML::Node.new "delete"
505 delete << super_relation.to_xml_node
506 delete << used_relation.to_xml_node
507 delete << used_way.to_xml_node
508 delete << used_node.to_xml_node
510 # update the changeset to one that this user owns
511 %w(node way relation).each do |type|
512 delete.find("//osmChange/delete/#{type}").each do |n|
513 n["changeset"] = changeset.id.to_s
519 post :upload, :id => changeset.id
520 assert_response :success,
521 "can't upload a deletion diff to changeset: #{@response.body}"
523 # check the response is well-formed
524 assert_select "diffResult>node", 1
525 assert_select "diffResult>way", 1
526 assert_select "diffResult>relation", 2
528 # check that everything was deleted
529 assert_equal false, Node.find(used_node.id).visible
530 assert_equal false, Way.find(used_way.id).visible
531 assert_equal false, Relation.find(super_relation.id).visible
532 assert_equal false, Relation.find(used_relation.id).visible
536 # test uploading a delete with no lat/lon, as they are optional in
537 # the osmChange spec.
538 def test_upload_nolatlon_delete
540 changeset = create(:changeset)
542 basic_authorization changeset.user.display_name, "test"
543 diff = "<osmChange><delete><node id='#{node.id}' version='#{node.version}' changeset='#{changeset.id}'/></delete></osmChange>"
547 post :upload, :id => changeset.id
548 assert_response :success,
549 "can't upload a deletion diff to changeset: #{@response.body}"
551 # check the response is well-formed
552 assert_select "diffResult>node", 1
554 # check that everything was deleted
555 assert_equal false, Node.find(node.id).visible
558 def test_repeated_changeset_create
560 basic_authorization create(:user).email, "test"
562 # create a temporary changeset
563 content "<osm><changeset>" +
564 "<tag k='created_by' v='osm test suite checking changesets'/>" +
566 assert_difference "Changeset.count", 1 do
569 assert_response :success
573 def test_upload_large_changeset
574 basic_authorization create(:user).email, "test"
577 content "<osm><changeset/></osm>"
579 assert_response :success, "Should be able to create a changeset: #{@response.body}"
580 changeset_id = @response.body.to_i
582 # upload some widely-spaced nodes, spiralling positive and negative to cause
583 # largest bbox over-expansion possible.
587 <node id='-1' lon='-20' lat='-10' changeset='#{changeset_id}'/>
588 <node id='-10' lon='20' lat='10' changeset='#{changeset_id}'/>
589 <node id='-2' lon='-40' lat='-20' changeset='#{changeset_id}'/>
590 <node id='-11' lon='40' lat='20' changeset='#{changeset_id}'/>
591 <node id='-3' lon='-60' lat='-30' changeset='#{changeset_id}'/>
592 <node id='-12' lon='60' lat='30' changeset='#{changeset_id}'/>
593 <node id='-4' lon='-80' lat='-40' changeset='#{changeset_id}'/>
594 <node id='-13' lon='80' lat='40' changeset='#{changeset_id}'/>
595 <node id='-5' lon='-100' lat='-50' changeset='#{changeset_id}'/>
596 <node id='-14' lon='100' lat='50' changeset='#{changeset_id}'/>
597 <node id='-6' lon='-120' lat='-60' changeset='#{changeset_id}'/>
598 <node id='-15' lon='120' lat='60' changeset='#{changeset_id}'/>
599 <node id='-7' lon='-140' lat='-70' changeset='#{changeset_id}'/>
600 <node id='-16' lon='140' lat='70' changeset='#{changeset_id}'/>
601 <node id='-8' lon='-160' lat='-80' changeset='#{changeset_id}'/>
602 <node id='-17' lon='160' lat='80' changeset='#{changeset_id}'/>
603 <node id='-9' lon='-179.9' lat='-89.9' changeset='#{changeset_id}'/>
604 <node id='-18' lon='179.9' lat='89.9' changeset='#{changeset_id}'/>
609 # upload it, which used to cause an error like "PGError: ERROR:
610 # integer out of range" (bug #2152). but shouldn't any more.
612 post :upload, :id => changeset_id
613 assert_response :success,
614 "can't upload a spatially-large diff to changeset: #{@response.body}"
616 # check that the changeset bbox is within bounds
617 cs = Changeset.find(changeset_id)
618 assert cs.min_lon >= -180 * GeoRecord::SCALE, "Minimum longitude (#{cs.min_lon / GeoRecord::SCALE}) should be >= -180 to be valid."
619 assert cs.max_lon <= 180 * GeoRecord::SCALE, "Maximum longitude (#{cs.max_lon / GeoRecord::SCALE}) should be <= 180 to be valid."
620 assert cs.min_lat >= -90 * GeoRecord::SCALE, "Minimum latitude (#{cs.min_lat / GeoRecord::SCALE}) should be >= -90 to be valid."
621 assert cs.max_lat >= 90 * GeoRecord::SCALE, "Maximum latitude (#{cs.max_lat / GeoRecord::SCALE}) should be <= 90 to be valid."
625 # test that deleting stuff in a transaction doesn't bypass the checks
626 # to ensure that used elements are not deleted.
627 def test_upload_delete_invalid
628 changeset = create(:changeset)
629 relation = create(:relation)
630 other_relation = create(:relation)
631 used_way = create(:way)
632 used_node = create(:node)
633 create(:relation_member, :relation => relation, :member => used_way)
634 create(:relation_member, :relation => relation, :member => used_node)
636 basic_authorization changeset.user.email, "test"
638 diff = XML::Document.new
639 diff.root = XML::Node.new "osmChange"
640 delete = XML::Node.new "delete"
642 delete << other_relation.to_xml_node
643 delete << used_way.to_xml_node
644 delete << used_node.to_xml_node
646 # update the changeset to one that this user owns
647 %w(node way relation).each do |type|
648 delete.find("//osmChange/delete/#{type}").each do |n|
649 n["changeset"] = changeset.id.to_s
655 post :upload, :id => changeset.id
656 assert_response :precondition_failed,
657 "shouldn't be able to upload a invalid deletion diff: #{@response.body}"
658 assert_equal "Precondition failed: Way #{used_way.id} is still used by relations #{relation.id}.", @response.body
660 # check that nothing was, in fact, deleted
661 assert_equal true, Node.find(used_node.id).visible
662 assert_equal true, Way.find(used_way.id).visible
663 assert_equal true, Relation.find(relation.id).visible
664 assert_equal true, Relation.find(other_relation.id).visible
668 # test that a conditional delete of an in use object works.
669 def test_upload_delete_if_unused
670 changeset = create(:changeset)
671 super_relation = create(:relation)
672 used_relation = create(:relation)
673 used_way = create(:way)
674 used_node = create(:node)
675 create(:relation_member, :relation => super_relation, :member => used_relation)
676 create(:relation_member, :relation => super_relation, :member => used_way)
677 create(:relation_member, :relation => super_relation, :member => used_node)
679 basic_authorization changeset.user.email, "test"
681 diff = XML::Document.new
682 diff.root = XML::Node.new "osmChange"
683 delete = XML::Node.new "delete"
685 delete["if-unused"] = ""
686 delete << used_relation.to_xml_node
687 delete << used_way.to_xml_node
688 delete << used_node.to_xml_node
690 # update the changeset to one that this user owns
691 %w(node way relation).each do |type|
692 delete.find("//osmChange/delete/#{type}").each do |n|
693 n["changeset"] = changeset.id.to_s
699 post :upload, :id => changeset.id
700 assert_response :success,
701 "can't do a conditional delete of in use objects: #{@response.body}"
703 # check the returned payload
704 assert_select "diffResult[version='#{API_VERSION}'][generator='OpenStreetMap server']", 1
705 assert_select "diffResult>node", 1
706 assert_select "diffResult>way", 1
707 assert_select "diffResult>relation", 1
710 doc = XML::Parser.string(@response.body).parse
712 # check the old IDs are all present and what we expect
713 assert_equal used_node.id, doc.find("//diffResult/node").first["old_id"].to_i
714 assert_equal used_way.id, doc.find("//diffResult/way").first["old_id"].to_i
715 assert_equal used_relation.id, doc.find("//diffResult/relation").first["old_id"].to_i
717 # check the new IDs are all present and unchanged
718 assert_equal used_node.id, doc.find("//diffResult/node").first["new_id"].to_i
719 assert_equal used_way.id, doc.find("//diffResult/way").first["new_id"].to_i
720 assert_equal used_relation.id, doc.find("//diffResult/relation").first["new_id"].to_i
722 # check the new versions are all present and unchanged
723 assert_equal used_node.version, doc.find("//diffResult/node").first["new_version"].to_i
724 assert_equal used_way.version, doc.find("//diffResult/way").first["new_version"].to_i
725 assert_equal used_relation.version, doc.find("//diffResult/relation").first["new_version"].to_i
727 # check that nothing was, in fact, deleted
728 assert_equal true, Node.find(used_node.id).visible
729 assert_equal true, Way.find(used_way.id).visible
730 assert_equal true, Relation.find(used_relation.id).visible
734 # upload an element with a really long tag value
735 def test_upload_invalid_too_long_tag
736 changeset = create(:changeset)
738 basic_authorization changeset.user.email, "test"
740 # simple diff to create a node way and relation using placeholders
744 <node id='-1' lon='0' lat='0' changeset='#{changeset.id}'>
745 <tag k='foo' v='#{'x' * 256}'/>
753 post :upload, :id => changeset.id
754 assert_response :bad_request,
755 "shoudln't be able to upload too long a tag to changeset: #{@response.body}"
759 # upload something which creates new objects and inserts them into
760 # existing containers using placeholders.
761 def test_upload_complex
764 relation = create(:relation)
765 create(:way_node, :way => way, :node => node)
767 changeset = create(:changeset)
769 basic_authorization changeset.user.email, "test"
771 # simple diff to create a node way and relation using placeholders
775 <node id='-1' lon='0' lat='0' changeset='#{changeset.id}'>
776 <tag k='foo' v='bar'/>
777 <tag k='baz' v='bat'/>
781 <way id='#{way.id}' changeset='#{changeset.id}' version='1'>
783 <nd ref='#{node.id}'/>
785 <relation id='#{relation.id}' changeset='#{changeset.id}' version='1'>
786 <member type='way' role='some' ref='#{way.id}'/>
787 <member type='node' role='some' ref='-1'/>
788 <member type='relation' role='some' ref='#{relation.id}'/>
796 post :upload, :id => changeset.id
797 assert_response :success,
798 "can't upload a complex diff to changeset: #{@response.body}"
800 # check the returned payload
801 assert_select "diffResult[version='#{API_VERSION}'][generator='#{GENERATOR}']", 1
802 assert_select "diffResult>node", 1
803 assert_select "diffResult>way", 1
804 assert_select "diffResult>relation", 1
806 # inspect the response to find out what the new element IDs are
807 doc = XML::Parser.string(@response.body).parse
808 new_node_id = doc.find("//diffResult/node").first["new_id"].to_i
810 # check that the changes made it into the database
811 assert_equal 2, Node.find(new_node_id).tags.size, "new node should have two tags"
812 assert_equal [new_node_id, node.id], Way.find(way.id).nds, "way nodes should match"
813 Relation.find(relation.id).members.each do |type, id, _role|
815 assert_equal new_node_id, id, "relation should contain new node"
821 # create a diff which references several changesets, which should cause
822 # a rollback and none of the diff gets committed
823 def test_upload_invalid_changesets
824 changeset = create(:changeset)
825 other_changeset = create(:changeset, :user => changeset.user)
828 relation = create(:relation)
829 other_relation = create(:relation)
831 basic_authorization changeset.user.email, "test"
833 # simple diff to create a node way and relation using placeholders
837 <node id='#{node.id}' lon='0' lat='0' changeset='#{changeset.id}' version='1'/>
838 <way id='#{way.id}' changeset='#{changeset.id}' version='1'>
839 <nd ref='#{node.id}'/>
843 <relation id='#{relation.id}' changeset='#{changeset.id}' version='1'>
844 <member type='way' role='some' ref='#{way.id}'/>
845 <member type='node' role='some' ref='#{node.id}'/>
846 <member type='relation' role='some' ref='#{other_relation.id}'/>
850 <node id='-1' lon='0' lat='0' changeset='#{other_changeset.id}'>
851 <tag k='foo' v='bar'/>
852 <tag k='baz' v='bat'/>
860 post :upload, :id => changeset.id
861 assert_response :conflict,
862 "uploading a diff with multiple changesets should have failed"
864 # check that objects are unmodified
865 assert_nodes_are_equal(node, Node.find(node.id))
866 assert_ways_are_equal(way, Way.find(way.id))
867 assert_relations_are_equal(relation, Relation.find(relation.id))
871 # upload multiple versions of the same element in the same diff.
872 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
882 <node id='1' lon='0' lat='0' changeset='#{changeset.id}' version='1'/>
883 <node id='1' lon='1' lat='0' changeset='#{changeset.id}' version='2'/>
884 <node id='1' lon='1' lat='1' changeset='#{changeset.id}' version='3'/>
885 <node id='1' lon='1' lat='2' changeset='#{changeset.id}' version='4'/>
886 <node id='1' lon='2' lat='2' changeset='#{changeset.id}' version='5'/>
887 <node id='1' lon='3' lat='2' changeset='#{changeset.id}' version='6'/>
888 <node id='1' lon='3' lat='3' changeset='#{changeset.id}' version='7'/>
889 <node id='1' lon='9' lat='9' changeset='#{changeset.id}' version='8'/>
896 post :upload, :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
910 changeset = create(:changeset)
912 basic_authorization changeset.user.email, "test"
917 <node id='1' lon='0' lat='0' changeset='#{changeset.id}' version='1'/>
918 <node id='1' lon='1' lat='1' changeset='#{changeset.id}' version='1'/>
925 post :upload, :id => changeset.id
926 assert_response :conflict,
927 "shouldn't be able to upload the same element twice in a diff: #{@response.body}"
931 # try to upload some elements without specifying the version
932 def test_upload_missing_version
933 changeset = create(:changeset)
935 basic_authorization changeset.user.email, "test"
940 <node id='1' lon='1' lat='1' changeset='#{changeset.id}'/>
947 post :upload, :id => changeset.id
948 assert_response :bad_request,
949 "shouldn't be able to upload an element without version: #{@response.body}"
953 # try to upload with commands other than create, modify, or delete
954 def test_action_upload_invalid
955 changeset = create(:changeset)
957 basic_authorization changeset.user.email, "test"
962 <node id='1' lon='1' lat='1' changeset='#{changeset.id}' />
967 post :upload, :id => changeset.id
968 assert_response :bad_request, "Shouldn't be able to upload a diff with the action ping"
969 assert_equal @response.body, "Unknown action ping, choices are create, modify, delete"
973 # upload a valid changeset which has a mixture of whitespace
974 # to check a bug reported by ivansanchez (#1565).
975 def test_upload_whitespace_valid
976 changeset = create(:changeset)
978 way = create(:way_with_nodes, :nodes_count => 2)
979 relation = create(:relation)
980 other_relation = create(:relation)
981 create(:relation_tag, :relation => relation)
983 basic_authorization changeset.user.email, "test"
987 <modify><node id='#{node.id}' lon='0' lat='0' changeset='#{changeset.id}'
989 <node id='#{node.id}' lon='1' lat='1' changeset='#{changeset.id}' version='2'><tag k='k' v='v'/></node></modify>
991 <relation id='#{relation.id}' changeset='#{changeset.id}' version='1'><member
992 type='way' role='some' ref='#{way.id}'/><member
993 type='node' role='some' ref='#{node.id}'/>
994 <member type='relation' role='some' ref='#{other_relation.id}'/>
996 </modify></osmChange>
1001 post :upload, :id => changeset.id
1002 assert_response :success,
1003 "can't upload a valid diff with whitespace variations to changeset: #{@response.body}"
1005 # check the response is well-formed
1006 assert_select "diffResult>node", 2
1007 assert_select "diffResult>relation", 1
1009 # check that the changes made it into the database
1010 assert_equal 1, Node.find(node.id).tags.size, "node #{node.id} should now have one tag"
1011 assert_equal 0, Relation.find(relation.id).tags.size, "relation #{relation.id} should now have no tags"
1015 # test that a placeholder can be reused within the same upload.
1016 def test_upload_reuse_placeholder_valid
1017 changeset = create(:changeset)
1019 basic_authorization changeset.user.email, "test"
1024 <node id='-1' lon='0' lat='0' changeset='#{changeset.id}'>
1025 <tag k="foo" v="bar"/>
1029 <node id='-1' lon='1' lat='1' changeset='#{changeset.id}' version='1'/>
1032 <node id='-1' lon='2' lat='2' changeset='#{changeset.id}' version='2'/>
1039 post :upload, :id => changeset.id
1040 assert_response :success,
1041 "can't upload a valid diff with re-used placeholders to changeset: #{@response.body}"
1043 # check the response is well-formed
1044 assert_select "diffResult>node", 3
1045 assert_select "diffResult>node[old_id='-1']", 3
1049 # test what happens if a diff upload re-uses placeholder IDs in an
1051 def test_upload_placeholder_invalid
1052 changeset = create(:changeset)
1054 basic_authorization changeset.user.email, "test"
1059 <node id='-1' lon='0' lat='0' changeset='#{changeset.id}' version='1'/>
1060 <node id='-1' lon='1' lat='1' changeset='#{changeset.id}' version='1'/>
1061 <node id='-1' lon='2' lat='2' changeset='#{changeset.id}' version='2'/>
1068 post :upload, :id => changeset.id
1069 assert_response :bad_request,
1070 "shouldn't be able to re-use placeholder IDs"
1074 # test that uploading a way referencing invalid placeholders gives a
1075 # proper error, not a 500.
1076 def test_upload_placeholder_invalid_way
1077 changeset = create(:changeset)
1080 basic_authorization changeset.user.email, "test"
1085 <node id="-1" lon="0" lat="0" changeset="#{changeset.id}" version="1"/>
1086 <node id="-2" lon="1" lat="1" changeset="#{changeset.id}" version="1"/>
1087 <node id="-3" lon="2" lat="2" changeset="#{changeset.id}" version="1"/>
1088 <way id="-1" changeset="#{changeset.id}" version="1">
1100 post :upload, :id => changeset.id
1101 assert_response :bad_request,
1102 "shouldn't be able to use invalid placeholder IDs"
1103 assert_equal "Placeholder node not found for reference -4 in way -1", @response.body
1105 # the same again, but this time use an existing way
1109 <node id="-1" lon="0" lat="0" changeset="#{changeset.id}" version="1"/>
1110 <node id="-2" lon="1" lat="1" changeset="#{changeset.id}" version="1"/>
1111 <node id="-3" lon="2" lat="2" changeset="#{changeset.id}" version="1"/>
1112 <way id="#{way.id}" changeset="#{changeset.id}" version="1">
1124 post :upload, :id => changeset.id
1125 assert_response :bad_request,
1126 "shouldn't be able to use invalid placeholder IDs"
1127 assert_equal "Placeholder node not found for reference -4 in way #{way.id}", @response.body
1131 # test that uploading a relation referencing invalid placeholders gives a
1132 # proper error, not a 500.
1133 def test_upload_placeholder_invalid_relation
1134 changeset = create(:changeset)
1135 relation = create(:relation)
1137 basic_authorization changeset.user.email, "test"
1142 <node id="-1" lon="0" lat="0" changeset="#{changeset.id}" version="1"/>
1143 <node id="-2" lon="1" lat="1" changeset="#{changeset.id}" version="1"/>
1144 <node id="-3" lon="2" lat="2" changeset="#{changeset.id}" version="1"/>
1145 <relation id="-1" changeset="#{changeset.id}" version="1">
1146 <member type="node" role="foo" ref="-1"/>
1147 <member type="node" role="foo" ref="-2"/>
1148 <member type="node" role="foo" ref="-3"/>
1149 <member type="node" role="foo" ref="-4"/>
1157 post :upload, :id => changeset.id
1158 assert_response :bad_request,
1159 "shouldn't be able to use invalid placeholder IDs"
1160 assert_equal "Placeholder Node not found for reference -4 in relation -1.", @response.body
1162 # the same again, but this time use an existing relation
1166 <node id="-1" lon="0" lat="0" changeset="#{changeset.id}" version="1"/>
1167 <node id="-2" lon="1" lat="1" changeset="#{changeset.id}" version="1"/>
1168 <node id="-3" lon="2" lat="2" changeset="#{changeset.id}" version="1"/>
1169 <relation id="#{relation.id}" changeset="#{changeset.id}" version="1">
1170 <member type="node" role="foo" ref="-1"/>
1171 <member type="node" role="foo" ref="-2"/>
1172 <member type="node" role="foo" ref="-3"/>
1173 <member type="way" role="bar" ref="-1"/>
1181 post :upload, :id => changeset.id
1182 assert_response :bad_request,
1183 "shouldn't be able to use invalid placeholder IDs"
1184 assert_equal "Placeholder Way not found for reference -1 in relation #{relation.id}.", @response.body
1188 # test what happens if a diff is uploaded containing only a node
1190 def test_upload_node_move
1191 basic_authorization create(:user).email, "test"
1193 content "<osm><changeset>" +
1194 "<tag k='created_by' v='osm test suite checking changesets'/>" +
1195 "</changeset></osm>"
1197 assert_response :success
1198 changeset_id = @response.body.to_i
1200 old_node = create(:node, :lat => 1, :lon => 1)
1202 diff = XML::Document.new
1203 diff.root = XML::Node.new "osmChange"
1204 modify = XML::Node.new "modify"
1205 xml_old_node = old_node.to_xml_node
1206 xml_old_node["lat"] = 2.0.to_s
1207 xml_old_node["lon"] = 2.0.to_s
1208 xml_old_node["changeset"] = changeset_id.to_s
1209 modify << xml_old_node
1214 post :upload, :id => changeset_id
1215 assert_response :success,
1216 "diff should have uploaded OK"
1219 changeset = Changeset.find(changeset_id)
1220 assert_equal 1 * GeoRecord::SCALE, changeset.min_lon, "min_lon should be 1 degree"
1221 assert_equal 2 * GeoRecord::SCALE, changeset.max_lon, "max_lon should be 2 degrees"
1222 assert_equal 1 * GeoRecord::SCALE, changeset.min_lat, "min_lat should be 1 degree"
1223 assert_equal 2 * GeoRecord::SCALE, changeset.max_lat, "max_lat should be 2 degrees"
1227 # test what happens if a diff is uploaded adding a node to a way.
1228 def test_upload_way_extend
1229 basic_authorization create(:user).email, "test"
1231 content "<osm><changeset>" +
1232 "<tag k='created_by' v='osm test suite checking changesets'/>" +
1233 "</changeset></osm>"
1235 assert_response :success
1236 changeset_id = @response.body.to_i
1238 old_way = create(:way)
1239 create(:way_node, :way => old_way, :node => create(:node, :lat => 1, :lon => 1))
1241 diff = XML::Document.new
1242 diff.root = XML::Node.new "osmChange"
1243 modify = XML::Node.new "modify"
1244 xml_old_way = old_way.to_xml_node
1245 nd_ref = XML::Node.new "nd"
1246 nd_ref["ref"] = create(:node, :lat => 3, :lon => 3).id.to_s
1247 xml_old_way << nd_ref
1248 xml_old_way["changeset"] = changeset_id.to_s
1249 modify << xml_old_way
1254 post :upload, :id => changeset_id
1255 assert_response :success,
1256 "diff should have uploaded OK"
1259 changeset = Changeset.find(changeset_id)
1260 assert_equal 1 * GeoRecord::SCALE, changeset.min_lon, "min_lon should be 1 degree"
1261 assert_equal 3 * GeoRecord::SCALE, changeset.max_lon, "max_lon should be 3 degrees"
1262 assert_equal 1 * GeoRecord::SCALE, changeset.min_lat, "min_lat should be 1 degree"
1263 assert_equal 3 * GeoRecord::SCALE, changeset.max_lat, "max_lat should be 3 degrees"
1267 # test for more issues in #1568
1268 def test_upload_empty_invalid
1269 changeset = create(:changeset)
1271 basic_authorization changeset.user.email, "test"
1274 "<osmChange></osmChange>",
1275 "<osmChange><modify/></osmChange>",
1276 "<osmChange><modify></modify></osmChange>"].each do |diff|
1279 post :upload, :id => changeset.id
1280 assert_response(:success, "should be able to upload " +
1281 "empty changeset: " + diff)
1286 # test that the X-Error-Format header works to request XML errors
1287 def test_upload_xml_errors
1288 changeset = create(:changeset)
1289 node = create(:node)
1290 create(:relation_member, :member => node)
1292 basic_authorization changeset.user.email, "test"
1294 # try and delete a node that is in use
1295 diff = XML::Document.new
1296 diff.root = XML::Node.new "osmChange"
1297 delete = XML::Node.new "delete"
1299 delete << node.to_xml_node
1304 post :upload, :id => changeset.id
1305 assert_response :success,
1306 "failed to return error in XML format"
1308 # check the returned payload
1309 assert_select "osmError[version='#{API_VERSION}'][generator='OpenStreetMap server']", 1
1310 assert_select "osmError>status", 1
1311 assert_select "osmError>message", 1
1315 # when we make some simple changes we get the same changes back from the
1317 def test_diff_download_simple
1318 ## First try with a non-public user, which should get a forbidden
1319 basic_authorization(create(:user, :data_public => false).email, "test")
1321 # create a temporary changeset
1322 content "<osm><changeset>" +
1323 "<tag k='created_by' v='osm test suite checking changesets'/>" +
1324 "</changeset></osm>"
1326 assert_response :forbidden
1328 ## Now try with a normal user
1329 basic_authorization(create(:user).email, "test")
1331 # create a temporary changeset
1332 content "<osm><changeset>" +
1333 "<tag k='created_by' v='osm test suite checking changesets'/>" +
1334 "</changeset></osm>"
1336 assert_response :success
1337 changeset_id = @response.body.to_i
1343 <node id='1' lon='0' lat='0' changeset='#{changeset_id}' version='1'/>
1344 <node id='1' lon='1' lat='0' changeset='#{changeset_id}' version='2'/>
1345 <node id='1' lon='1' lat='1' changeset='#{changeset_id}' version='3'/>
1346 <node id='1' lon='1' lat='2' changeset='#{changeset_id}' version='4'/>
1347 <node id='1' lon='2' lat='2' changeset='#{changeset_id}' version='5'/>
1348 <node id='1' lon='3' lat='2' changeset='#{changeset_id}' version='6'/>
1349 <node id='1' lon='3' lat='3' changeset='#{changeset_id}' version='7'/>
1350 <node id='1' lon='9' lat='9' changeset='#{changeset_id}' version='8'/>
1357 post :upload, :id => changeset_id
1358 assert_response :success,
1359 "can't upload multiple versions of an element in a diff: #{@response.body}"
1361 get :download, :id => changeset_id
1362 assert_response :success
1364 assert_select "osmChange", 1
1365 assert_select "osmChange>modify", 8
1366 assert_select "osmChange>modify>node", 8
1370 # culled this from josm to ensure that nothing in the way that josm
1371 # is formatting the request is causing it to fail.
1373 # NOTE: the error turned out to be something else completely!
1374 def test_josm_upload
1375 basic_authorization(create(:user).email, "test")
1377 # create a temporary changeset
1378 content "<osm><changeset>" +
1379 "<tag k='created_by' v='osm test suite checking changesets'/>" +
1380 "</changeset></osm>"
1382 assert_response :success
1383 changeset_id = @response.body.to_i
1386 <osmChange version="0.6" generator="JOSM">
1387 <create version="0.6" generator="JOSM">
1388 <node id='-1' visible='true' changeset='#{changeset_id}' lat='51.49619982187321' lon='-0.18722061869438314' />
1389 <node id='-2' visible='true' changeset='#{changeset_id}' lat='51.496359883909605' lon='-0.18653093576241928' />
1390 <node id='-3' visible='true' changeset='#{changeset_id}' lat='51.49598132358285' lon='-0.18719613290981638' />
1391 <node id='-4' visible='true' changeset='#{changeset_id}' lat='51.4961591711078' lon='-0.18629015888084607' />
1392 <node id='-5' visible='true' changeset='#{changeset_id}' lat='51.49582126021711' lon='-0.18708186591517145' />
1393 <node id='-6' visible='true' changeset='#{changeset_id}' lat='51.49591018437858' lon='-0.1861432441734455' />
1394 <node id='-7' visible='true' changeset='#{changeset_id}' lat='51.49560784152179' lon='-0.18694719410005425' />
1395 <node id='-8' visible='true' changeset='#{changeset_id}' lat='51.49567389979617' lon='-0.1860289771788006' />
1396 <node id='-9' visible='true' changeset='#{changeset_id}' lat='51.49543761398892' lon='-0.186820684213126' />
1397 <way id='-10' action='modiy' visible='true' changeset='#{changeset_id}'>
1407 <tag k='highway' v='residential' />
1408 <tag k='name' v='Foobar Street' />
1416 post :upload, :id => changeset_id
1417 assert_response :success,
1418 "can't upload a diff from JOSM: #{@response.body}"
1420 get :download, :id => changeset_id
1421 assert_response :success
1423 assert_select "osmChange", 1
1424 assert_select "osmChange>create>node", 9
1425 assert_select "osmChange>create>way", 1
1426 assert_select "osmChange>create>way>nd", 9
1427 assert_select "osmChange>create>way>tag", 2
1431 # when we make some complex changes we get the same changes back from the
1433 def test_diff_download_complex
1434 basic_authorization(create(:user).email, "test")
1436 # create a temporary changeset
1437 content "<osm><changeset>" +
1438 "<tag k='created_by' v='osm test suite checking changesets'/>" +
1439 "</changeset></osm>"
1441 assert_response :success
1442 changeset_id = @response.body.to_i
1448 <node id='1' lon='0' lat='0' changeset='#{changeset_id}' version='1'/>
1451 <node id='-1' lon='9' lat='9' changeset='#{changeset_id}' version='0'/>
1452 <node id='-2' lon='8' lat='9' changeset='#{changeset_id}' version='0'/>
1453 <node id='-3' lon='7' lat='9' changeset='#{changeset_id}' version='0'/>
1456 <node id='3' lon='20' lat='15' changeset='#{changeset_id}' version='1'/>
1457 <way id='1' changeset='#{changeset_id}' version='1'>
1469 post :upload, :id => changeset_id
1470 assert_response :success,
1471 "can't upload multiple versions of an element in a diff: #{@response.body}"
1473 get :download, :id => changeset_id
1474 assert_response :success
1476 assert_select "osmChange", 1
1477 assert_select "osmChange>create", 3
1478 assert_select "osmChange>delete", 1
1479 assert_select "osmChange>modify", 2
1480 assert_select "osmChange>create>node", 3
1481 assert_select "osmChange>delete>node", 1
1482 assert_select "osmChange>modify>node", 1
1483 assert_select "osmChange>modify>way", 1
1486 def test_changeset_download
1487 changeset = create(:changeset)
1488 node = create(:node, :with_history, :version => 1, :changeset => changeset)
1489 tag = create(:old_node_tag, :old_node => node.old_nodes.find_by(:version => 1))
1490 node2 = create(:node, :with_history, :version => 1, :changeset => changeset)
1491 _node3 = create(:node, :with_history, :deleted, :version => 1, :changeset => changeset)
1492 _relation = create(:relation, :with_history, :version => 1, :changeset => changeset)
1493 _relation2 = create(:relation, :with_history, :deleted, :version => 1, :changeset => changeset)
1495 get :download, :id => changeset.id
1497 assert_response :success
1499 # print @response.body
1500 # FIXME: needs more assert_select tests
1501 assert_select "osmChange[version='#{API_VERSION}'][generator='#{GENERATOR}']" do
1502 assert_select "create", :count => 5
1503 assert_select "create>node[id='#{node.id}'][visible='#{node.visible?}'][version='#{node.version}']" do
1504 assert_select "tag[k='#{tag.k}'][v='#{tag.v}']"
1506 assert_select "create>node[id='#{node2.id}']"
1511 # check that the bounding box of a changeset gets updated correctly
1512 # FIXME: This should really be moded to a integration test due to the with_controller
1513 def test_changeset_bbox
1515 create(:way_node, :way => way, :node => create(:node, :lat => 3, :lon => 3))
1517 basic_authorization create(:user).email, "test"
1519 # create a new changeset
1520 content "<osm><changeset/></osm>"
1522 assert_response :success, "Creating of changeset failed."
1523 changeset_id = @response.body.to_i
1525 # add a single node to it
1526 with_controller(NodeController.new) do
1527 content "<osm><node lon='1' lat='2' changeset='#{changeset_id}'/></osm>"
1529 assert_response :success, "Couldn't create node."
1532 # get the bounding box back from the changeset
1533 get :read, :id => changeset_id
1534 assert_response :success, "Couldn't read back changeset."
1535 assert_select "osm>changeset[min_lon='1.0']", 1
1536 assert_select "osm>changeset[max_lon='1.0']", 1
1537 assert_select "osm>changeset[min_lat='2.0']", 1
1538 assert_select "osm>changeset[max_lat='2.0']", 1
1540 # add another node to it
1541 with_controller(NodeController.new) do
1542 content "<osm><node lon='2' lat='1' changeset='#{changeset_id}'/></osm>"
1544 assert_response :success, "Couldn't create second node."
1547 # get the bounding box back from the changeset
1548 get :read, :id => changeset_id
1549 assert_response :success, "Couldn't read back changeset for the second time."
1550 assert_select "osm>changeset[min_lon='1.0']", 1
1551 assert_select "osm>changeset[max_lon='2.0']", 1
1552 assert_select "osm>changeset[min_lat='1.0']", 1
1553 assert_select "osm>changeset[max_lat='2.0']", 1
1555 # add (delete) a way to it, which contains a point at (3,3)
1556 with_controller(WayController.new) do
1557 content update_changeset(way.to_xml, changeset_id)
1558 put :delete, :id => way.id
1559 assert_response :success, "Couldn't delete a way."
1562 # get the bounding box back from the changeset
1563 get :read, :id => changeset_id
1564 assert_response :success, "Couldn't read back changeset for the third time."
1565 # note that the 3.1 here is because of the bbox overexpansion
1566 assert_select "osm>changeset[min_lon='1.0']", 1
1567 assert_select "osm>changeset[max_lon='3.1']", 1
1568 assert_select "osm>changeset[min_lat='1.0']", 1
1569 assert_select "osm>changeset[max_lat='3.1']", 1
1573 # test that the changeset :include method works as it should
1574 def test_changeset_include
1575 basic_authorization create(:user).display_name, "test"
1577 # create a new changeset
1578 content "<osm><changeset/></osm>"
1580 assert_response :success, "Creating of changeset failed."
1581 changeset_id = @response.body.to_i
1583 # NOTE: the include method doesn't over-expand, like inserting
1584 # a real method does. this is because we expect the client to
1585 # know what it is doing!
1586 check_after_include(changeset_id, 1, 1, [1, 1, 1, 1])
1587 check_after_include(changeset_id, 3, 3, [1, 1, 3, 3])
1588 check_after_include(changeset_id, 4, 2, [1, 1, 4, 3])
1589 check_after_include(changeset_id, 2, 2, [1, 1, 4, 3])
1590 check_after_include(changeset_id, -1, -1, [-1, -1, 4, 3])
1591 check_after_include(changeset_id, -2, 5, [-2, -1, 4, 5])
1595 # test that a not found, wrong method with the expand bbox works as expected
1596 def test_changeset_expand_bbox_error
1597 basic_authorization create(:user).display_name, "test"
1599 # create a new changeset
1600 content "<osm><changeset/></osm>"
1602 assert_response :success, "Creating of changeset failed."
1603 changeset_id = @response.body.to_i
1609 content "<osm><node lon='#{lon}' lat='#{lat}'/></osm>"
1610 put :expand_bbox, :id => changeset_id
1611 assert_response :method_not_allowed, "shouldn't be able to put a bbox expand"
1613 # Try to get the update
1614 content "<osm><node lon='#{lon}' lat='#{lat}'/></osm>"
1615 get :expand_bbox, :id => changeset_id
1616 assert_response :method_not_allowed, "shouldn't be able to get a bbox expand"
1618 # Try to use a hopefully missing changeset
1619 content "<osm><node lon='#{lon}' lat='#{lat}'/></osm>"
1620 post :expand_bbox, :id => changeset_id + 13245
1621 assert_response :not_found, "shouldn't be able to do a bbox expand on a nonexistant changeset"
1625 # test the query functionality of changesets
1627 private_user = create(:user, :data_public => false)
1628 private_user_changeset = create(:changeset, :user => private_user)
1629 private_user_closed_changeset = create(:changeset, :closed, :user => private_user)
1630 user = create(:user)
1631 changeset = create(:changeset, :user => user)
1632 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))
1634 get :query, :bbox => "-10,-10, 10, 10"
1635 assert_response :success, "can't get changesets in bbox"
1636 assert_changesets [1, 4, 6]
1638 get :query, :bbox => "4.5,4.5,4.6,4.6"
1639 assert_response :success, "can't get changesets in bbox"
1640 assert_changesets [1]
1642 # not found when looking for changesets of non-existing users
1643 get :query, :user => User.maximum(:id) + 1
1644 assert_response :not_found
1645 get :query, :display_name => " "
1646 assert_response :not_found
1648 # can't get changesets of user 1 without authenticating
1649 get :query, :user => private_user.id
1650 assert_response :not_found, "shouldn't be able to get changesets by non-public user (ID)"
1651 get :query, :display_name => private_user.display_name
1652 assert_response :not_found, "shouldn't be able to get changesets by non-public user (name)"
1654 # but this should work
1655 basic_authorization private_user.email, "test"
1656 get :query, :user => private_user.id
1657 assert_response :success, "can't get changesets by user ID"
1658 assert_changesets [private_user_changeset.id, private_user_closed_changeset.id]
1660 get :query, :display_name => private_user.display_name
1661 assert_response :success, "can't get changesets by user name"
1662 assert_changesets [private_user_changeset.id, private_user_closed_changeset.id]
1664 # check that the correct error is given when we provide both UID and name
1665 get :query, :user => private_user.id, :display_name => private_user.display_name
1666 assert_response :bad_request, "should be a bad request to have both ID and name specified"
1668 get :query, :user => private_user.id, :open => true
1669 assert_response :success, "can't get changesets by user and open"
1670 assert_changesets [private_user_changeset.id]
1672 get :query, :time => "2007-12-31"
1673 assert_response :success, "can't get changesets by time-since"
1674 assert_changesets [1, 2, 4, 5, 6, private_user_changeset.id, private_user_closed_changeset.id, changeset.id, closed_changeset.id]
1676 get :query, :time => "2008-01-01T12:34Z"
1677 assert_response :success, "can't get changesets by time-since with hour"
1678 assert_changesets [1, 2, 4, 5, 6, private_user_changeset.id, private_user_closed_changeset.id, changeset.id, closed_changeset.id]
1680 get :query, :time => "2007-12-31T23:59Z,2008-01-02T00:01Z"
1681 assert_response :success, "can't get changesets by time-range"
1682 assert_changesets [1, 5, 6, closed_changeset.id]
1684 get :query, :open => "true"
1685 assert_response :success, "can't get changesets by open-ness"
1686 assert_changesets [1, 2, 4, private_user_changeset.id, changeset.id]
1688 get :query, :closed => "true"
1689 assert_response :success, "can't get changesets by closed-ness"
1690 assert_changesets [3, 5, 6, 7, 8, 9, private_user_closed_changeset.id, closed_changeset.id]
1692 get :query, :closed => "true", :user => private_user.id
1693 assert_response :success, "can't get changesets by closed-ness and user"
1694 assert_changesets [private_user_closed_changeset.id]
1696 get :query, :closed => "true", :user => user.id
1697 assert_response :success, "can't get changesets by closed-ness and user"
1698 assert_changesets [closed_changeset.id]
1700 get :query, :changesets => "#{private_user_changeset.id},#{changeset.id},#{closed_changeset.id}"
1701 assert_response :success, "can't get changesets by id (as comma-separated string)"
1702 assert_changesets [private_user_changeset.id, changeset.id, closed_changeset.id]
1704 get :query, :changesets => ""
1705 assert_response :bad_request, "should be a bad request since changesets is empty"
1709 # check that errors are returned if garbage is inserted
1710 # into query strings
1711 def test_query_invalid
1714 ";drop table users;"].each do |bbox|
1715 get :query, :bbox => bbox
1716 assert_response :bad_request, "'#{bbox}' isn't a bbox"
1721 ";drop table users;",
1723 "-,-"].each do |time|
1724 get :query, :time => time
1725 assert_response :bad_request, "'#{time}' isn't a valid time range"
1732 get :query, :user => uid
1733 assert_response :bad_request, "'#{uid}' isn't a valid user ID"
1738 # check updating tags on a changeset
1739 def test_changeset_update
1740 private_user = create(:user, :data_public => false)
1741 private_changeset = create(:changeset, :user => private_user)
1742 user = create(:user)
1743 changeset = create(:changeset, :user => user)
1745 ## First try with a non-public user
1746 new_changeset = private_changeset.to_xml
1747 new_tag = XML::Node.new "tag"
1748 new_tag["k"] = "tagtesting"
1749 new_tag["v"] = "valuetesting"
1750 new_changeset.find("//osm/changeset").first << new_tag
1751 content new_changeset
1753 # try without any authorization
1754 put :update, :id => private_changeset.id
1755 assert_response :unauthorized
1757 # try with the wrong authorization
1758 basic_authorization create(:user).email, "test"
1759 put :update, :id => private_changeset.id
1760 assert_response :conflict
1762 # now this should get an unauthorized
1763 basic_authorization private_user.email, "test"
1764 put :update, :id => private_changeset.id
1765 assert_require_public_data "user with their data non-public, shouldn't be able to edit their changeset"
1767 ## Now try with the public user
1768 create(:changeset_tag, :changeset => changeset)
1769 new_changeset = changeset.to_xml
1770 new_tag = XML::Node.new "tag"
1771 new_tag["k"] = "tagtesting"
1772 new_tag["v"] = "valuetesting"
1773 new_changeset.find("//osm/changeset").first << new_tag
1774 content new_changeset
1776 # try without any authorization
1777 @request.env["HTTP_AUTHORIZATION"] = nil
1778 put :update, :id => changeset.id
1779 assert_response :unauthorized
1781 # try with the wrong authorization
1782 basic_authorization create(:user).email, "test"
1783 put :update, :id => changeset.id
1784 assert_response :conflict
1786 # now this should work...
1787 basic_authorization user.email, "test"
1788 put :update, :id => changeset.id
1789 assert_response :success
1791 assert_select "osm>changeset[id='#{changeset.id}']", 1
1792 assert_select "osm>changeset>tag", 2
1793 assert_select "osm>changeset>tag[k='tagtesting'][v='valuetesting']", 1
1797 # check that a user different from the one who opened the changeset
1799 def test_changeset_update_invalid
1800 basic_authorization create(:user).email, "test"
1802 changeset = create(:changeset)
1803 new_changeset = changeset.to_xml
1804 new_tag = XML::Node.new "tag"
1805 new_tag["k"] = "testing"
1806 new_tag["v"] = "testing"
1807 new_changeset.find("//osm/changeset").first << new_tag
1809 content new_changeset
1810 put :update, :id => changeset.id
1811 assert_response :conflict
1815 # check that a changeset can contain a certain max number of changes.
1816 ## FIXME should be changed to an integration test due to the with_controller
1817 def test_changeset_limits
1818 basic_authorization create(:user).email, "test"
1820 # open a new changeset
1821 content "<osm><changeset/></osm>"
1823 assert_response :success, "can't create a new changeset"
1824 cs_id = @response.body.to_i
1826 # start the counter just short of where the changeset should finish.
1828 # alter the database to set the counter on the changeset directly,
1829 # otherwise it takes about 6 minutes to fill all of them.
1830 changeset = Changeset.find(cs_id)
1831 changeset.num_changes = Changeset::MAX_ELEMENTS - offset
1834 with_controller(NodeController.new) do
1836 content "<osm><node changeset='#{cs_id}' lat='0.0' lon='0.0'/></osm>"
1838 assert_response :success, "can't create a new node"
1839 node_id = @response.body.to_i
1841 get :read, :id => node_id
1842 assert_response :success, "can't read back new node"
1843 node_doc = XML::Parser.string(@response.body).parse
1844 node_xml = node_doc.find("//osm/node").first
1846 # loop until we fill the changeset with nodes
1848 node_xml["lat"] = rand.to_s
1849 node_xml["lon"] = rand.to_s
1850 node_xml["version"] = (i + 1).to_s
1853 put :update, :id => node_id
1854 assert_response :success, "attempt #{i} should have succeeded"
1857 # trying again should fail
1858 node_xml["lat"] = rand.to_s
1859 node_xml["lon"] = rand.to_s
1860 node_xml["version"] = offset.to_s
1863 put :update, :id => node_id
1864 assert_response :conflict, "final attempt should have failed"
1867 changeset = Changeset.find(cs_id)
1868 assert_equal Changeset::MAX_ELEMENTS + 1, changeset.num_changes
1870 # check that the changeset is now closed as well
1871 assert(!changeset.is_open?,
1872 "changeset should have been auto-closed by exceeding " +
1877 # This should display the last 20 changesets closed
1879 get :list, :format => "html"
1880 assert_response :success
1881 assert_template "history"
1882 assert_template :layout => "map"
1883 assert_select "h2", :text => "Changesets", :count => 1
1885 xhr :get, :list, :format => "html", :list => "1"
1886 assert_response :success
1887 assert_template "list"
1889 check_list_result(Changeset.all)
1893 # This should display the last 20 changesets closed
1895 xhr :get, :list, :format => "html"
1896 assert_response :success
1897 assert_template "history"
1898 assert_template :layout => "xhr"
1899 assert_select "h2", :text => "Changesets", :count => 1
1901 xhr :get, :list, :format => "html", :list => "1"
1902 assert_response :success
1903 assert_template "list"
1905 check_list_result(Changeset.all)
1909 # This should display the last 20 changesets closed in a specific area
1911 get :list, :format => "html", :bbox => "4.5,4.5,5.5,5.5"
1912 assert_response :success
1913 assert_template "history"
1914 assert_template :layout => "map"
1915 assert_select "h2", :text => "Changesets", :count => 1
1917 xhr :get, :list, :format => "html", :bbox => "4.5,4.5,5.5,5.5", :list => "1"
1918 assert_response :success
1919 assert_template "list"
1921 check_list_result(Changeset.where("min_lon < 55000000 and max_lon > 45000000 and min_lat < 55000000 and max_lat > 45000000"))
1925 # Checks the display of the user changesets listing
1927 user = create(:user)
1928 create(:changeset, :user => user)
1929 create(:changeset, :closed, :user => user)
1931 get :list, :format => "html", :display_name => user.display_name
1932 assert_response :success
1933 assert_template "history"
1935 xhr :get, :list, :format => "html", :display_name => user.display_name, :list => "1"
1936 assert_response :success
1937 assert_template "list"
1939 check_list_result(user.changesets)
1943 # Checks the display of the user changesets listing for a private user
1944 def test_list_private_user
1945 private_user = create(:user, :data_public => false)
1946 create(:changeset, :user => private_user)
1947 create(:changeset, :closed, :user => private_user)
1949 get :list, :format => "html", :display_name => private_user.display_name
1950 assert_response :success
1951 assert_template "history"
1953 xhr :get, :list, :format => "html", :display_name => private_user.display_name, :list => "1"
1954 assert_response :success
1955 assert_template "list"
1957 check_list_result(Changeset.none)
1961 # Check the not found of the list user changesets
1962 def test_list_user_not_found
1963 get :list, :format => "html", :display_name => "Some random user"
1964 assert_response :not_found
1965 assert_template "user/no_such_user"
1967 xhr :get, :list, :format => "html", :display_name => "Some random user", :list => "1"
1968 assert_response :not_found
1969 assert_template "user/no_such_user"
1973 # Checks the display of the friends changesets listing
1974 def test_list_friends
1975 private_user = create(:user, :data_public => true)
1976 friend = create(:friend, :befriender => private_user)
1977 create(:changeset, :user => friend.befriendee)
1979 get :list, :friends => true
1980 assert_response :redirect
1981 assert_redirected_to :controller => :user, :action => :login, :referer => friend_changesets_path
1983 session[:user] = private_user.id
1985 get :list, :friends => true
1986 assert_response :success
1987 assert_template "history"
1989 xhr :get, :list, :friends => true, :list => "1"
1990 assert_response :success
1991 assert_template "list"
1993 check_list_result(Changeset.where(:user => private_user.friend_users.identifiable))
1997 # Checks the display of the nearby user changesets listing
1998 def test_list_nearby
1999 private_user = create(:user, :data_public => false, :home_lat => 51.1, :home_lon => 1.0)
2000 user = create(:user, :home_lat => 51.0, :home_lon => 1.0)
2001 create(:changeset, :user => user)
2003 get :list, :nearby => true
2004 assert_response :redirect
2005 assert_redirected_to :controller => :user, :action => :login, :referer => nearby_changesets_path
2007 session[:user] = private_user.id
2009 get :list, :nearby => true
2010 assert_response :success
2011 assert_template "history"
2013 xhr :get, :list, :nearby => true, :list => "1"
2014 assert_response :success
2015 assert_template "list"
2017 check_list_result(Changeset.where(:user => user.nearby))
2021 # Check that we can't request later pages of the changesets list
2022 def test_list_max_id
2023 xhr :get, :list, :format => "html", :max_id => 4
2024 assert_response :success
2025 assert_template "history"
2026 assert_template :layout => "xhr"
2027 assert_select "h2", :text => "Changesets", :count => 1
2029 xhr :get, :list, :format => "html", :list => "1", :max_id => 4
2030 assert_response :success
2031 assert_template "list"
2033 check_list_result(Changeset.where("id <= 4"))
2037 # This should display the last 20 changesets closed
2039 get :feed, :format => :atom
2040 assert_response :success
2041 assert_template "list"
2042 assert_equal "application/atom+xml", response.content_type
2044 check_feed_result(Changeset.all)
2048 # This should display the last 20 changesets closed in a specific area
2050 get :feed, :format => :atom, :bbox => "4.5,4.5,5.5,5.5"
2051 assert_response :success
2052 assert_template "list"
2053 assert_equal "application/atom+xml", response.content_type
2055 check_feed_result(Changeset.where("min_lon < 55000000 and max_lon > 45000000 and min_lat < 55000000 and max_lat > 45000000"))
2059 # Checks the display of the user changesets feed
2061 user = create(:user)
2062 create_list(:changeset, 3, :user => user, :num_changes => 4)
2064 get :feed, :format => :atom, :display_name => user.display_name
2065 assert_response :success
2066 assert_template "list"
2067 assert_equal "application/atom+xml", response.content_type
2069 check_feed_result(user.changesets)
2073 # Check the not found of the user changesets feed
2074 def test_feed_user_not_found
2075 get :feed, :format => "atom", :display_name => "Some random user"
2076 assert_response :not_found
2080 # Check that we can't request later pages of the changesets feed
2081 def test_feed_max_id
2082 get :feed, :format => "atom", :max_id => 100
2083 assert_response :redirect
2084 assert_redirected_to :action => :feed
2088 # check that the changeset download for a changeset with a redacted
2089 # element in it doesn't contain that element.
2090 def test_diff_download_redacted
2091 changeset = create(:changeset)
2092 node = create(:node, :with_history, :version => 2, :changeset => changeset)
2093 node_v1 = node.old_nodes.find_by(:version => 1)
2094 node_v1.redact!(create(:redaction))
2096 get :download, :id => changeset.id
2097 assert_response :success
2099 assert_select "osmChange", 1
2100 # this changeset contains the node in versions 1 & 2, but 1 should
2102 assert_select "osmChange node[id='#{node.id}']", 1
2103 assert_select "osmChange node[id='#{node.id}'][version='1']", 0
2107 # create comment success
2108 def test_create_comment_success
2109 user = create(:user)
2110 user2 = create(:user)
2111 private_user = create(:user, :data_public => false)
2112 suspended_user = create(:user, :suspended)
2113 deleted_user = create(:user, :deleted)
2114 private_user_closed_changeset = create(:changeset, :closed, :user => private_user)
2116 basic_authorization(user.email, "test")
2118 assert_difference "ChangesetComment.count", 1 do
2119 assert_no_difference "ActionMailer::Base.deliveries.size" do
2120 post :comment, :id => private_user_closed_changeset.id, :text => "This is a comment"
2123 assert_response :success
2125 changeset = create(:changeset, :closed, :user => private_user)
2126 changeset.subscribers.push(private_user)
2127 changeset.subscribers.push(user)
2128 changeset.subscribers.push(suspended_user)
2129 changeset.subscribers.push(deleted_user)
2131 assert_difference "ChangesetComment.count", 1 do
2132 assert_difference "ActionMailer::Base.deliveries.size", 1 do
2133 post :comment, :id => changeset.id, :text => "This is a comment"
2136 assert_response :success
2138 email = ActionMailer::Base.deliveries.first
2139 assert_equal 1, email.to.length
2140 assert_equal "[OpenStreetMap] #{user.display_name} has commented on one of your changesets", email.subject
2141 assert_equal private_user.email, email.to.first
2143 ActionMailer::Base.deliveries.clear
2145 basic_authorization(user2.email, "test")
2147 assert_difference "ChangesetComment.count", 1 do
2148 assert_difference "ActionMailer::Base.deliveries.size", 2 do
2149 post :comment, :id => changeset.id, :text => "This is a comment"
2152 assert_response :success
2154 email = ActionMailer::Base.deliveries.find { |e| e.to.first == private_user.email }
2155 assert_not_nil email
2156 assert_equal 1, email.to.length
2157 assert_equal "[OpenStreetMap] #{user2.display_name} has commented on one of your changesets", email.subject
2159 email = ActionMailer::Base.deliveries.find { |e| e.to.first == user.email }
2160 assert_not_nil email
2161 assert_equal 1, email.to.length
2162 assert_equal "[OpenStreetMap] #{user2.display_name} has commented on a changeset you are interested in", email.subject
2164 ActionMailer::Base.deliveries.clear
2168 # create comment fail
2169 def test_create_comment_fail
2171 post :comment, :id => create(:changeset, :closed).id, :text => "This is a comment"
2172 assert_response :unauthorized
2174 basic_authorization(create(:user).email, "test")
2177 assert_no_difference "ChangesetComment.count" do
2178 post :comment, :id => 999111, :text => "This is a comment"
2180 assert_response :not_found
2182 # not closed changeset
2183 assert_no_difference "ChangesetComment.count" do
2184 post :comment, :id => create(:changeset).id, :text => "This is a comment"
2186 assert_response :conflict
2189 assert_no_difference "ChangesetComment.count" do
2190 post :comment, :id => create(:changeset, :closed).id
2192 assert_response :bad_request
2195 assert_no_difference "ChangesetComment.count" do
2196 post :comment, :id => create(:changeset, :closed).id, :text => ""
2198 assert_response :bad_request
2202 # test subscribe success
2203 def test_subscribe_success
2204 basic_authorization(create(:user).email, "test")
2205 changeset = create(:changeset, :closed)
2207 assert_difference "changeset.subscribers.count", 1 do
2208 post :subscribe, :id => changeset.id
2210 assert_response :success
2214 # test subscribe fail
2215 def test_subscribe_fail
2216 user = create(:user)
2219 changeset = create(:changeset, :closed)
2220 assert_no_difference "changeset.subscribers.count" do
2221 post :subscribe, :id => changeset.id
2223 assert_response :unauthorized
2225 basic_authorization(user.email, "test")
2228 assert_no_difference "changeset.subscribers.count" do
2229 post :subscribe, :id => 999111
2231 assert_response :not_found
2233 # not closed changeset
2234 changeset = create(:changeset)
2235 assert_no_difference "changeset.subscribers.count" do
2236 post :subscribe, :id => changeset.id
2238 assert_response :conflict
2240 # trying to subscribe when already subscribed
2241 changeset = create(:changeset, :closed)
2242 changeset.subscribers.push(user)
2243 assert_no_difference "changeset.subscribers.count" do
2244 post :subscribe, :id => changeset.id
2246 assert_response :conflict
2250 # test unsubscribe success
2251 def test_unsubscribe_success
2252 user = create(:user)
2253 basic_authorization(user.email, "test")
2254 changeset = create(:changeset, :closed)
2255 changeset.subscribers.push(user)
2257 assert_difference "changeset.subscribers.count", -1 do
2258 post :unsubscribe, :id => changeset.id
2260 assert_response :success
2264 # test unsubscribe fail
2265 def test_unsubscribe_fail
2267 changeset = create(:changeset, :closed)
2268 assert_no_difference "changeset.subscribers.count" do
2269 post :unsubscribe, :id => changeset.id
2271 assert_response :unauthorized
2273 basic_authorization(create(:user).email, "test")
2276 assert_no_difference "changeset.subscribers.count" do
2277 post :unsubscribe, :id => 999111
2279 assert_response :not_found
2281 # not closed changeset
2282 changeset = create(:changeset)
2283 assert_no_difference "changeset.subscribers.count" do
2284 post :unsubscribe, :id => changeset.id
2286 assert_response :conflict
2288 # trying to unsubscribe when not subscribed
2289 changeset = create(:changeset, :closed)
2290 assert_no_difference "changeset.subscribers.count" do
2291 post :unsubscribe, :id => changeset.id
2293 assert_response :not_found
2297 # test hide comment fail
2298 def test_hide_comment_fail
2300 comment = create(:changeset_comment)
2301 assert_equal true, comment.visible
2303 post :hide_comment, :id => comment.id
2304 assert_response :unauthorized
2305 assert_equal true, comment.reload.visible
2307 basic_authorization(create(:user).email, "test")
2310 post :hide_comment, :id => comment.id
2311 assert_response :forbidden
2312 assert_equal true, comment.reload.visible
2314 basic_authorization(create(:moderator_user).email, "test")
2317 post :hide_comment, :id => 999111
2318 assert_response :not_found
2319 assert_equal true, comment.reload.visible
2323 # test hide comment succes
2324 def test_hide_comment_success
2325 comment = create(:changeset_comment)
2326 assert_equal true, comment.visible
2328 basic_authorization(create(:moderator_user).email, "test")
2330 post :hide_comment, :id => comment.id
2331 assert_response :success
2332 assert_equal false, comment.reload.visible
2336 # test unhide comment fail
2337 def test_unhide_comment_fail
2339 comment = create(:changeset_comment, :visible => false)
2340 assert_equal false, comment.visible
2342 post :unhide_comment, :id => comment.id
2343 assert_response :unauthorized
2344 assert_equal false, comment.reload.visible
2346 basic_authorization(create(:user).email, "test")
2349 post :unhide_comment, :id => comment.id
2350 assert_response :forbidden
2351 assert_equal false, comment.reload.visible
2353 basic_authorization(create(:moderator_user).email, "test")
2356 post :unhide_comment, :id => 999111
2357 assert_response :not_found
2358 assert_equal false, comment.reload.visible
2362 # test unhide comment succes
2363 def test_unhide_comment_success
2364 comment = create(:changeset_comment, :visible => false)
2365 assert_equal false, comment.visible
2367 basic_authorization(create(:moderator_user).email, "test")
2369 post :unhide_comment, :id => comment.id
2370 assert_response :success
2371 assert_equal true, comment.reload.visible
2375 # test comments feed
2376 def test_comments_feed
2377 changeset = create(:changeset, :closed)
2378 create_list(:changeset_comment, 3, :changeset => changeset)
2380 get :comments_feed, :format => "rss"
2381 assert_response :success
2382 assert_equal "application/rss+xml", @response.content_type
2383 assert_select "rss", :count => 1 do
2384 assert_select "channel", :count => 1 do
2385 assert_select "item", :count => 3
2389 get :comments_feed, :format => "rss", :limit => 2
2390 assert_response :success
2391 assert_equal "application/rss+xml", @response.content_type
2392 assert_select "rss", :count => 1 do
2393 assert_select "channel", :count => 1 do
2394 assert_select "item", :count => 2
2398 get :comments_feed, :id => changeset.id, :format => "rss"
2399 assert_response :success
2400 assert_equal "application/rss+xml", @response.content_type
2401 assert_select "rss", :count => 1 do
2402 assert_select "channel", :count => 1 do
2403 assert_select "item", :count => 3
2409 # test comments feed
2410 def test_comments_feed_bad_limit
2411 get :comments_feed, :format => "rss", :limit => 0
2412 assert_response :bad_request
2414 get :comments_feed, :format => "rss", :limit => 100001
2415 assert_response :bad_request
2421 # boilerplate for checking that certain changesets exist in the
2423 def assert_changesets(ids)
2424 assert_select "osm>changeset", ids.size
2426 assert_select "osm>changeset[id='#{id}']", 1
2431 # call the include method and assert properties of the bbox
2432 def check_after_include(changeset_id, lon, lat, bbox)
2433 content "<osm><node lon='#{lon}' lat='#{lat}'/></osm>"
2434 post :expand_bbox, :id => changeset_id
2435 assert_response :success, "Setting include of changeset failed: #{@response.body}"
2437 # check exactly one changeset
2438 assert_select "osm>changeset", 1
2439 assert_select "osm>changeset[id='#{changeset_id}']", 1
2442 doc = XML::Parser.string(@response.body).parse
2443 changeset = doc.find("//osm/changeset").first
2444 assert_equal bbox[0], changeset["min_lon"].to_f, "min lon"
2445 assert_equal bbox[1], changeset["min_lat"].to_f, "min lat"
2446 assert_equal bbox[2], changeset["max_lon"].to_f, "max lon"
2447 assert_equal bbox[3], changeset["max_lat"].to_f, "max lat"
2451 # update the changeset_id of a way element
2452 def update_changeset(xml, changeset_id)
2453 xml_attr_rewrite(xml, "changeset", changeset_id)
2457 # update an attribute in a way element
2458 def xml_attr_rewrite(xml, name, value)
2459 xml.find("//osm/way").first[name] = value.to_s
2464 # check the result of a list
2465 def check_list_result(changesets)
2466 changesets = changesets.where("num_changes > 0")
2467 .order(:created_at => :desc)
2469 assert changesets.size <= 20
2471 assert_select "ol.changesets", :count => [changesets.size, 1].min do
2472 assert_select "li", :count => changesets.size
2474 changesets.each do |changeset|
2475 assert_select "li#changeset_#{changeset.id}", :count => 1
2481 # check the result of a feed
2482 def check_feed_result(changesets)
2483 changesets = changesets.where("num_changes > 0")
2484 .order(:created_at => :desc)
2486 assert changesets.size <= 20
2488 assert_select "feed", :count => [changesets.size, 1].min do
2489 assert_select "> title", :count => 1, :text => /^Changesets/
2490 assert_select "> entry", :count => changesets.size
2492 changesets.each do |changeset|
2493 assert_select "> entry > id", changeset_url(:id => changeset.id)