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/changesets", :method => :get },
46 { :controller => "changeset", :action => "query" }
49 { :path => "/user/name/history", :method => :get },
50 { :controller => "changeset", :action => "index", :display_name => "name" }
53 { :path => "/user/name/history/feed", :method => :get },
54 { :controller => "changeset", :action => "feed", :display_name => "name", :format => :atom }
57 { :path => "/history/friends", :method => :get },
58 { :controller => "changeset", :action => "index", :friends => true, :format => :html }
61 { :path => "/history/nearby", :method => :get },
62 { :controller => "changeset", :action => "index", :nearby => true, :format => :html }
65 { :path => "/history", :method => :get },
66 { :controller => "changeset", :action => "index" }
69 { :path => "/history/feed", :method => :get },
70 { :controller => "changeset", :action => "feed", :format => :atom }
74 # -----------------------
75 # Test simple changeset creation
76 # -----------------------
79 basic_authorization create(:user, :data_public => false).email, "test"
80 # Create the first user's changeset
81 content "<osm><changeset>" \
82 "<tag k='created_by' v='osm test suite checking changesets'/>" \
85 assert_require_public_data
87 basic_authorization create(:user).email, "test"
88 # Create the first user's changeset
89 content "<osm><changeset>" \
90 "<tag k='created_by' v='osm test suite checking changesets'/>" \
94 assert_response :success, "Creation of changeset did not return sucess status"
95 newid = @response.body.to_i
97 # check end time, should be an hour ahead of creation time
98 cs = Changeset.find(newid)
99 duration = cs.closed_at - cs.created_at
100 # the difference can either be a rational, or a floating point number
101 # of seconds, depending on the code path taken :-(
102 if duration.class == Rational
103 assert_equal Rational(1, 24), duration, "initial idle timeout should be an hour (#{cs.created_at} -> #{cs.closed_at})"
105 # must be number of seconds...
106 assert_equal 3600, duration.round, "initial idle timeout should be an hour (#{cs.created_at} -> #{cs.closed_at})"
109 # checks if uploader was subscribed
110 assert_equal 1, cs.subscribers.length
113 def test_create_invalid
114 basic_authorization create(:user, :data_public => false).email, "test"
115 content "<osm><changeset></osm>"
117 assert_require_public_data
119 ## Try the public user
120 basic_authorization create(:user).email, "test"
121 content "<osm><changeset></osm>"
123 assert_response :bad_request, "creating a invalid changeset should fail"
126 def test_create_invalid_no_content
127 ## First check with no auth
129 assert_response :unauthorized, "shouldn't be able to create a changeset with no auth"
131 ## Now try to with a non-public user
132 basic_authorization create(:user, :data_public => false).email, "test"
134 assert_require_public_data
136 ## Try an inactive user
137 basic_authorization create(:user, :pending).email, "test"
141 ## Now try to use a normal user
142 basic_authorization create(:user).email, "test"
144 assert_response :bad_request, "creating a changeset with no content should fail"
147 def test_create_wrong_method
148 basic_authorization create(:user).email, "test"
150 assert_response :method_not_allowed
152 assert_response :method_not_allowed
156 # check that the changeset can be read and returns the correct
157 # document structure.
159 changeset_id = create(:changeset).id
161 get :read, :params => { :id => changeset_id }
162 assert_response :success, "cannot get first changeset"
164 assert_select "osm[version='#{API_VERSION}'][generator='OpenStreetMap server']", 1
165 assert_select "osm>changeset[id='#{changeset_id}']", 1
166 assert_select "osm>changeset>discussion", 0
168 get :read, :params => { :id => changeset_id, :include_discussion => true }
169 assert_response :success, "cannot get first changeset with comments"
171 assert_select "osm[version='#{API_VERSION}'][generator='OpenStreetMap server']", 1
172 assert_select "osm>changeset[id='#{changeset_id}']", 1
173 assert_select "osm>changeset>discussion", 1
174 assert_select "osm>changeset>discussion>comment", 0
176 changeset_id = create(:changeset, :closed).id
177 create_list(:changeset_comment, 3, :changeset_id => changeset_id)
179 get :read, :params => { :id => changeset_id, :include_discussion => true }
180 assert_response :success, "cannot get closed changeset with comments"
182 assert_select "osm[version='#{API_VERSION}'][generator='OpenStreetMap server']", 1
183 assert_select "osm>changeset[id='#{changeset_id}']", 1
184 assert_select "osm>changeset>discussion", 1
185 assert_select "osm>changeset>discussion>comment", 3
189 # check that a changeset that doesn't exist returns an appropriate message
190 def test_read_not_found
191 [0, -32, 233455644, "afg", "213"].each do |id|
193 get :read, :params => { :id => id }
194 assert_response :not_found, "should get a not found"
195 rescue ActionController::UrlGenerationError => ex
196 assert_match(/No route matches/, ex.to_s)
202 # test that the user who opened a change can close it
204 private_user = create(:user, :data_public => false)
205 private_changeset = create(:changeset, :user => private_user)
207 changeset = create(:changeset, :user => user)
209 ## Try without authentication
210 put :close, :params => { :id => changeset.id }
211 assert_response :unauthorized
213 ## Try using the non-public user
214 basic_authorization private_user.email, "test"
215 put :close, :params => { :id => private_changeset.id }
216 assert_require_public_data
218 ## The try with the public user
219 basic_authorization user.email, "test"
222 put :close, :params => { :id => cs_id }
223 assert_response :success
225 # test that it really is closed now
226 cs = Changeset.find(cs_id)
227 assert_not(cs.is_open?,
228 "changeset should be closed now (#{cs.closed_at} > #{Time.now.getutc}.")
232 # test that a different user can't close another user's changeset
233 def test_close_invalid
235 changeset = create(:changeset)
237 basic_authorization user.email, "test"
239 put :close, :params => { :id => changeset.id }
240 assert_response :conflict
241 assert_equal "The user doesn't own that changeset", @response.body
245 # test that you can't close using another method
246 def test_close_method_invalid
248 changeset = create(:changeset, :user => user)
250 basic_authorization user.email, "test"
252 get :close, :params => { :id => changeset.id }
253 assert_response :method_not_allowed
255 post :close, :params => { :id => changeset.id }
256 assert_response :method_not_allowed
260 # check that you can't close a changeset that isn't found
261 def test_close_not_found
262 cs_ids = [0, -132, "123"]
264 # First try to do it with no auth
267 put :close, :params => { :id => id }
268 assert_response :unauthorized, "Shouldn't be able close the non-existant changeset #{id}, when not authorized"
269 rescue ActionController::UrlGenerationError => ex
270 assert_match(/No route matches/, ex.to_s)
275 basic_authorization create(:user).email, "test"
278 put :close, :params => { :id => id }
279 assert_response :not_found, "The changeset #{id} doesn't exist, so can't be closed"
280 rescue ActionController::UrlGenerationError => ex
281 assert_match(/No route matches/, ex.to_s)
287 # upload something simple, but valid and check that it can
289 # Also try without auth and another user.
290 def test_upload_simple_valid
291 private_user = create(:user, :data_public => false)
292 private_changeset = create(:changeset, :user => private_user)
294 changeset = create(:changeset, :user => user)
298 relation = create(:relation)
299 other_relation = create(:relation)
300 # create some tags, since we test that they are removed later
301 create(:node_tag, :node => node)
302 create(:way_tag, :way => way)
303 create(:relation_tag, :relation => relation)
306 changeset_id = changeset.id
308 # simple diff to change a node, way and relation by removing
310 diff = <<CHANGESET.strip_heredoc
313 <node id='#{node.id}' lon='0' lat='0' changeset='#{changeset_id}' version='1'/>
314 <way id='#{way.id}' changeset='#{changeset_id}' version='1'>
315 <nd ref='#{node.id}'/>
319 <relation id='#{relation.id}' changeset='#{changeset_id}' version='1'>
320 <member type='way' role='some' ref='#{way.id}'/>
321 <member type='node' role='some' ref='#{node.id}'/>
322 <member type='relation' role='some' ref='#{other_relation.id}'/>
330 post :upload, :params => { :id => changeset_id }
331 assert_response :unauthorized,
332 "shouldn't be able to upload a simple valid diff to changeset: #{@response.body}"
334 ## Now try with a private user
335 basic_authorization private_user.email, "test"
336 changeset_id = private_changeset.id
338 # simple diff to change a node, way and relation by removing
340 diff = <<CHANGESET.strip_heredoc
343 <node id='#{node.id}' lon='0' lat='0' changeset='#{changeset_id}' version='1'/>
344 <way id='#{way.id}' changeset='#{changeset_id}' version='1'>
345 <nd ref='#{node.id}'/>
349 <relation id='#{relation.id}' changeset='#{changeset_id}' version='1'>
350 <member type='way' role='some' ref='#{way.id}'/>
351 <member type='node' role='some' ref='#{node.id}'/>
352 <member type='relation' role='some' ref='#{other_relation.id}'/>
360 post :upload, :params => { :id => changeset_id }
361 assert_response :forbidden,
362 "can't upload a simple valid diff to changeset: #{@response.body}"
364 ## Now try with the public user
365 basic_authorization user.email, "test"
366 changeset_id = changeset.id
368 # simple diff to change a node, way and relation by removing
370 diff = <<CHANGESET.strip_heredoc
373 <node id='#{node.id}' lon='0' lat='0' changeset='#{changeset_id}' version='1'/>
374 <way id='#{way.id}' changeset='#{changeset_id}' version='1'>
375 <nd ref='#{node.id}'/>
379 <relation id='#{relation.id}' changeset='#{changeset_id}' version='1'>
380 <member type='way' role='some' ref='#{way.id}'/>
381 <member type='node' role='some' ref='#{node.id}'/>
382 <member type='relation' role='some' ref='#{other_relation.id}'/>
390 post :upload, :params => { :id => changeset_id }
391 assert_response :success,
392 "can't upload a simple valid diff to changeset: #{@response.body}"
394 # check that the changes made it into the database
395 assert_equal 0, Node.find(node.id).tags.size, "node #{node.id} should now have no tags"
396 assert_equal 0, Way.find(way.id).tags.size, "way #{way.id} should now have no tags"
397 assert_equal 0, Relation.find(relation.id).tags.size, "relation #{relation.id} should now have no tags"
401 # upload something which creates new objects using placeholders
402 def test_upload_create_valid
404 changeset = create(:changeset, :user => user)
406 way = create(:way_with_nodes, :nodes_count => 2)
407 relation = create(:relation)
409 basic_authorization user.email, "test"
411 # simple diff to create a node way and relation using placeholders
412 diff = <<CHANGESET.strip_heredoc
415 <node id='-1' lon='0' lat='0' changeset='#{changeset.id}'>
416 <tag k='foo' v='bar'/>
417 <tag k='baz' v='bat'/>
419 <way id='-1' changeset='#{changeset.id}'>
420 <nd ref='#{node.id}'/>
424 <relation id='-1' changeset='#{changeset.id}'>
425 <member type='way' role='some' ref='#{way.id}'/>
426 <member type='node' role='some' ref='#{node.id}'/>
427 <member type='relation' role='some' ref='#{relation.id}'/>
435 post :upload, :params => { :id => changeset.id }
436 assert_response :success,
437 "can't upload a simple valid creation to changeset: #{@response.body}"
439 # check the returned payload
440 assert_select "diffResult[version='#{API_VERSION}'][generator='OpenStreetMap server']", 1
441 assert_select "diffResult>node", 1
442 assert_select "diffResult>way", 1
443 assert_select "diffResult>relation", 1
445 # inspect the response to find out what the new element IDs are
446 doc = XML::Parser.string(@response.body).parse
447 new_node_id = doc.find("//diffResult/node").first["new_id"].to_i
448 new_way_id = doc.find("//diffResult/way").first["new_id"].to_i
449 new_rel_id = doc.find("//diffResult/relation").first["new_id"].to_i
451 # check the old IDs are all present and negative one
452 assert_equal(-1, doc.find("//diffResult/node").first["old_id"].to_i)
453 assert_equal(-1, doc.find("//diffResult/way").first["old_id"].to_i)
454 assert_equal(-1, doc.find("//diffResult/relation").first["old_id"].to_i)
456 # check the versions are present and equal one
457 assert_equal 1, doc.find("//diffResult/node").first["new_version"].to_i
458 assert_equal 1, doc.find("//diffResult/way").first["new_version"].to_i
459 assert_equal 1, doc.find("//diffResult/relation").first["new_version"].to_i
461 # check that the changes made it into the database
462 assert_equal 2, Node.find(new_node_id).tags.size, "new node should have two tags"
463 assert_equal 0, Way.find(new_way_id).tags.size, "new way should have no tags"
464 assert_equal 0, Relation.find(new_rel_id).tags.size, "new relation should have no tags"
468 # test a complex delete where we delete elements which rely on eachother
469 # in the same transaction.
470 def test_upload_delete
471 changeset = create(:changeset)
472 super_relation = create(:relation)
473 used_relation = create(:relation)
474 used_way = create(:way)
475 used_node = create(:node)
476 create(:relation_member, :relation => super_relation, :member => used_relation)
477 create(:relation_member, :relation => super_relation, :member => used_way)
478 create(:relation_member, :relation => super_relation, :member => used_node)
480 basic_authorization changeset.user.display_name, "test"
482 diff = XML::Document.new
483 diff.root = XML::Node.new "osmChange"
484 delete = XML::Node.new "delete"
486 delete << super_relation.to_xml_node
487 delete << used_relation.to_xml_node
488 delete << used_way.to_xml_node
489 delete << used_node.to_xml_node
491 # update the changeset to one that this user owns
492 %w[node way relation].each do |type|
493 delete.find("//osmChange/delete/#{type}").each do |n|
494 n["changeset"] = changeset.id.to_s
500 post :upload, :params => { :id => changeset.id }
501 assert_response :success,
502 "can't upload a deletion diff to changeset: #{@response.body}"
504 # check the response is well-formed
505 assert_select "diffResult>node", 1
506 assert_select "diffResult>way", 1
507 assert_select "diffResult>relation", 2
509 # check that everything was deleted
510 assert_equal false, Node.find(used_node.id).visible
511 assert_equal false, Way.find(used_way.id).visible
512 assert_equal false, Relation.find(super_relation.id).visible
513 assert_equal false, Relation.find(used_relation.id).visible
517 # test uploading a delete with no lat/lon, as they are optional in
518 # the osmChange spec.
519 def test_upload_nolatlon_delete
521 changeset = create(:changeset)
523 basic_authorization changeset.user.display_name, "test"
524 diff = "<osmChange><delete><node id='#{node.id}' version='#{node.version}' changeset='#{changeset.id}'/></delete></osmChange>"
528 post :upload, :params => { :id => changeset.id }
529 assert_response :success,
530 "can't upload a deletion diff to changeset: #{@response.body}"
532 # check the response is well-formed
533 assert_select "diffResult>node", 1
535 # check that everything was deleted
536 assert_equal false, Node.find(node.id).visible
539 def test_repeated_changeset_create
541 basic_authorization create(:user).email, "test"
543 # create a temporary changeset
544 content "<osm><changeset>" \
545 "<tag k='created_by' v='osm test suite checking changesets'/>" \
547 assert_difference "Changeset.count", 1 do
550 assert_response :success
554 def test_upload_large_changeset
555 basic_authorization create(:user).email, "test"
558 content "<osm><changeset/></osm>"
560 assert_response :success, "Should be able to create a changeset: #{@response.body}"
561 changeset_id = @response.body.to_i
563 # upload some widely-spaced nodes, spiralling positive and negative
564 diff = <<CHANGESET.strip_heredoc
567 <node id='-1' lon='-20' lat='-10' changeset='#{changeset_id}'/>
568 <node id='-10' lon='20' lat='10' changeset='#{changeset_id}'/>
569 <node id='-2' lon='-40' lat='-20' changeset='#{changeset_id}'/>
570 <node id='-11' lon='40' lat='20' changeset='#{changeset_id}'/>
571 <node id='-3' lon='-60' lat='-30' changeset='#{changeset_id}'/>
572 <node id='-12' lon='60' lat='30' changeset='#{changeset_id}'/>
573 <node id='-4' lon='-80' lat='-40' changeset='#{changeset_id}'/>
574 <node id='-13' lon='80' lat='40' changeset='#{changeset_id}'/>
575 <node id='-5' lon='-100' lat='-50' changeset='#{changeset_id}'/>
576 <node id='-14' lon='100' lat='50' changeset='#{changeset_id}'/>
577 <node id='-6' lon='-120' lat='-60' changeset='#{changeset_id}'/>
578 <node id='-15' lon='120' lat='60' changeset='#{changeset_id}'/>
579 <node id='-7' lon='-140' lat='-70' changeset='#{changeset_id}'/>
580 <node id='-16' lon='140' lat='70' changeset='#{changeset_id}'/>
581 <node id='-8' lon='-160' lat='-80' changeset='#{changeset_id}'/>
582 <node id='-17' lon='160' lat='80' changeset='#{changeset_id}'/>
583 <node id='-9' lon='-179.9' lat='-89.9' changeset='#{changeset_id}'/>
584 <node id='-18' lon='179.9' lat='89.9' changeset='#{changeset_id}'/>
589 # upload it, which used to cause an error like "PGError: ERROR:
590 # integer out of range" (bug #2152). but shouldn't any more.
592 post :upload, :params => { :id => changeset_id }
593 assert_response :success,
594 "can't upload a spatially-large diff to changeset: #{@response.body}"
596 # check that the changeset bbox is within bounds
597 cs = Changeset.find(changeset_id)
598 assert cs.min_lon >= -180 * GeoRecord::SCALE, "Minimum longitude (#{cs.min_lon / GeoRecord::SCALE}) should be >= -180 to be valid."
599 assert cs.max_lon <= 180 * GeoRecord::SCALE, "Maximum longitude (#{cs.max_lon / GeoRecord::SCALE}) should be <= 180 to be valid."
600 assert cs.min_lat >= -90 * GeoRecord::SCALE, "Minimum latitude (#{cs.min_lat / GeoRecord::SCALE}) should be >= -90 to be valid."
601 assert cs.max_lat <= 90 * GeoRecord::SCALE, "Maximum latitude (#{cs.max_lat / GeoRecord::SCALE}) should be <= 90 to be valid."
605 # test that deleting stuff in a transaction doesn't bypass the checks
606 # to ensure that used elements are not deleted.
607 def test_upload_delete_invalid
608 changeset = create(:changeset)
609 relation = create(:relation)
610 other_relation = create(:relation)
611 used_way = create(:way)
612 used_node = create(:node)
613 create(:relation_member, :relation => relation, :member => used_way)
614 create(:relation_member, :relation => relation, :member => used_node)
616 basic_authorization changeset.user.email, "test"
618 diff = XML::Document.new
619 diff.root = XML::Node.new "osmChange"
620 delete = XML::Node.new "delete"
622 delete << other_relation.to_xml_node
623 delete << used_way.to_xml_node
624 delete << used_node.to_xml_node
626 # update the changeset to one that this user owns
627 %w[node way relation].each do |type|
628 delete.find("//osmChange/delete/#{type}").each do |n|
629 n["changeset"] = changeset.id.to_s
635 post :upload, :params => { :id => changeset.id }
636 assert_response :precondition_failed,
637 "shouldn't be able to upload a invalid deletion diff: #{@response.body}"
638 assert_equal "Precondition failed: Way #{used_way.id} is still used by relations #{relation.id}.", @response.body
640 # check that nothing was, in fact, deleted
641 assert_equal true, Node.find(used_node.id).visible
642 assert_equal true, Way.find(used_way.id).visible
643 assert_equal true, Relation.find(relation.id).visible
644 assert_equal true, Relation.find(other_relation.id).visible
648 # test that a conditional delete of an in use object works.
649 def test_upload_delete_if_unused
650 changeset = create(:changeset)
651 super_relation = create(:relation)
652 used_relation = create(:relation)
653 used_way = create(:way)
654 used_node = create(:node)
655 create(:relation_member, :relation => super_relation, :member => used_relation)
656 create(:relation_member, :relation => super_relation, :member => used_way)
657 create(:relation_member, :relation => super_relation, :member => used_node)
659 basic_authorization changeset.user.email, "test"
661 diff = XML::Document.new
662 diff.root = XML::Node.new "osmChange"
663 delete = XML::Node.new "delete"
665 delete["if-unused"] = ""
666 delete << used_relation.to_xml_node
667 delete << used_way.to_xml_node
668 delete << used_node.to_xml_node
670 # update the changeset to one that this user owns
671 %w[node way relation].each do |type|
672 delete.find("//osmChange/delete/#{type}").each do |n|
673 n["changeset"] = changeset.id.to_s
679 post :upload, :params => { :id => changeset.id }
680 assert_response :success,
681 "can't do a conditional delete of in use objects: #{@response.body}"
683 # check the returned payload
684 assert_select "diffResult[version='#{API_VERSION}'][generator='OpenStreetMap server']", 1
685 assert_select "diffResult>node", 1
686 assert_select "diffResult>way", 1
687 assert_select "diffResult>relation", 1
690 doc = XML::Parser.string(@response.body).parse
692 # check the old IDs are all present and what we expect
693 assert_equal used_node.id, doc.find("//diffResult/node").first["old_id"].to_i
694 assert_equal used_way.id, doc.find("//diffResult/way").first["old_id"].to_i
695 assert_equal used_relation.id, doc.find("//diffResult/relation").first["old_id"].to_i
697 # check the new IDs are all present and unchanged
698 assert_equal used_node.id, doc.find("//diffResult/node").first["new_id"].to_i
699 assert_equal used_way.id, doc.find("//diffResult/way").first["new_id"].to_i
700 assert_equal used_relation.id, doc.find("//diffResult/relation").first["new_id"].to_i
702 # check the new versions are all present and unchanged
703 assert_equal used_node.version, doc.find("//diffResult/node").first["new_version"].to_i
704 assert_equal used_way.version, doc.find("//diffResult/way").first["new_version"].to_i
705 assert_equal used_relation.version, doc.find("//diffResult/relation").first["new_version"].to_i
707 # check that nothing was, in fact, deleted
708 assert_equal true, Node.find(used_node.id).visible
709 assert_equal true, Way.find(used_way.id).visible
710 assert_equal true, Relation.find(used_relation.id).visible
714 # upload an element with a really long tag value
715 def test_upload_invalid_too_long_tag
716 changeset = create(:changeset)
718 basic_authorization changeset.user.email, "test"
720 # simple diff to create a node way and relation using placeholders
721 diff = <<CHANGESET.strip_heredoc
724 <node id='-1' lon='0' lat='0' changeset='#{changeset.id}'>
725 <tag k='foo' v='#{'x' * 256}'/>
733 post :upload, :params => { :id => changeset.id }
734 assert_response :bad_request,
735 "shoudln't be able to upload too long a tag to changeset: #{@response.body}"
739 # upload something which creates new objects and inserts them into
740 # existing containers using placeholders.
741 def test_upload_complex
744 relation = create(:relation)
745 create(:way_node, :way => way, :node => node)
747 changeset = create(:changeset)
749 basic_authorization changeset.user.email, "test"
751 # simple diff to create a node way and relation using placeholders
752 diff = <<CHANGESET.strip_heredoc
755 <node id='-1' lon='0' lat='0' changeset='#{changeset.id}'>
756 <tag k='foo' v='bar'/>
757 <tag k='baz' v='bat'/>
761 <way id='#{way.id}' changeset='#{changeset.id}' version='1'>
763 <nd ref='#{node.id}'/>
765 <relation id='#{relation.id}' changeset='#{changeset.id}' version='1'>
766 <member type='way' role='some' ref='#{way.id}'/>
767 <member type='node' role='some' ref='-1'/>
768 <member type='relation' role='some' ref='#{relation.id}'/>
776 post :upload, :params => { :id => changeset.id }
777 assert_response :success,
778 "can't upload a complex diff to changeset: #{@response.body}"
780 # check the returned payload
781 assert_select "diffResult[version='#{API_VERSION}'][generator='#{GENERATOR}']", 1
782 assert_select "diffResult>node", 1
783 assert_select "diffResult>way", 1
784 assert_select "diffResult>relation", 1
786 # inspect the response to find out what the new element IDs are
787 doc = XML::Parser.string(@response.body).parse
788 new_node_id = doc.find("//diffResult/node").first["new_id"].to_i
790 # check that the changes made it into the database
791 assert_equal 2, Node.find(new_node_id).tags.size, "new node should have two tags"
792 assert_equal [new_node_id, node.id], Way.find(way.id).nds, "way nodes should match"
793 Relation.find(relation.id).members.each do |type, id, _role|
794 assert_equal new_node_id, id, "relation should contain new node" if type == "node"
799 # create a diff which references several changesets, which should cause
800 # a rollback and none of the diff gets committed
801 def test_upload_invalid_changesets
802 changeset = create(:changeset)
803 other_changeset = create(:changeset, :user => changeset.user)
806 relation = create(:relation)
807 other_relation = create(:relation)
809 basic_authorization changeset.user.email, "test"
811 # simple diff to create a node way and relation using placeholders
812 diff = <<CHANGESET.strip_heredoc
815 <node id='#{node.id}' lon='0' lat='0' changeset='#{changeset.id}' version='1'/>
816 <way id='#{way.id}' changeset='#{changeset.id}' version='1'>
817 <nd ref='#{node.id}'/>
821 <relation id='#{relation.id}' changeset='#{changeset.id}' version='1'>
822 <member type='way' role='some' ref='#{way.id}'/>
823 <member type='node' role='some' ref='#{node.id}'/>
824 <member type='relation' role='some' ref='#{other_relation.id}'/>
828 <node id='-1' lon='0' lat='0' changeset='#{other_changeset.id}'>
829 <tag k='foo' v='bar'/>
830 <tag k='baz' v='bat'/>
838 post :upload, :params => { :id => changeset.id }
839 assert_response :conflict,
840 "uploading a diff with multiple changesets should have failed"
842 # check that objects are unmodified
843 assert_nodes_are_equal(node, Node.find(node.id))
844 assert_ways_are_equal(way, Way.find(way.id))
845 assert_relations_are_equal(relation, Relation.find(relation.id))
849 # upload multiple versions of the same element in the same diff.
850 def test_upload_multiple_valid
852 changeset = create(:changeset)
853 basic_authorization changeset.user.email, "test"
855 # change the location of a node multiple times, each time referencing
856 # the last version. doesn't this depend on version numbers being
858 diff = <<CHANGESET.strip_heredoc
861 <node id='#{node.id}' lon='0' lat='0' changeset='#{changeset.id}' version='1'/>
862 <node id='#{node.id}' lon='1' lat='0' changeset='#{changeset.id}' version='2'/>
863 <node id='#{node.id}' lon='1' lat='1' changeset='#{changeset.id}' version='3'/>
864 <node id='#{node.id}' lon='1' lat='2' changeset='#{changeset.id}' version='4'/>
865 <node id='#{node.id}' lon='2' lat='2' changeset='#{changeset.id}' version='5'/>
866 <node id='#{node.id}' lon='3' lat='2' changeset='#{changeset.id}' version='6'/>
867 <node id='#{node.id}' lon='3' lat='3' changeset='#{changeset.id}' version='7'/>
868 <node id='#{node.id}' lon='9' lat='9' changeset='#{changeset.id}' version='8'/>
875 post :upload, :params => { :id => changeset.id }
876 assert_response :success,
877 "can't upload multiple versions of an element in a diff: #{@response.body}"
879 # check the response is well-formed. its counter-intuitive, but the
880 # API will return multiple elements with the same ID and different
881 # version numbers for each change we made.
882 assert_select "diffResult>node", 8
886 # upload multiple versions of the same element in the same diff, but
887 # keep the version numbers the same.
888 def test_upload_multiple_duplicate
890 changeset = create(:changeset)
892 basic_authorization changeset.user.email, "test"
894 diff = <<CHANGESET.strip_heredoc
897 <node id='#{node.id}' lon='0' lat='0' changeset='#{changeset.id}' version='1'/>
898 <node id='#{node.id}' lon='1' lat='1' changeset='#{changeset.id}' version='1'/>
905 post :upload, :params => { :id => changeset.id }
906 assert_response :conflict,
907 "shouldn't be able to upload the same element twice in a diff: #{@response.body}"
911 # try to upload some elements without specifying the version
912 def test_upload_missing_version
913 changeset = create(:changeset)
915 basic_authorization changeset.user.email, "test"
917 diff = <<CHANGESET.strip_heredoc
920 <node id='1' lon='1' lat='1' changeset='#{changeset.id}'/>
927 post :upload, :params => { :id => changeset.id }
928 assert_response :bad_request,
929 "shouldn't be able to upload an element without version: #{@response.body}"
933 # try to upload with commands other than create, modify, or delete
934 def test_action_upload_invalid
935 changeset = create(:changeset)
937 basic_authorization changeset.user.email, "test"
939 diff = <<CHANGESET.strip_heredoc
942 <node id='1' lon='1' lat='1' changeset='#{changeset.id}' />
947 post :upload, :params => { :id => changeset.id }
948 assert_response :bad_request, "Shouldn't be able to upload a diff with the action ping"
949 assert_equal @response.body, "Unknown action ping, choices are create, modify, delete"
953 # upload a valid changeset which has a mixture of whitespace
954 # to check a bug reported by ivansanchez (#1565).
955 def test_upload_whitespace_valid
956 changeset = create(:changeset)
958 way = create(:way_with_nodes, :nodes_count => 2)
959 relation = create(:relation)
960 other_relation = create(:relation)
961 create(:relation_tag, :relation => relation)
963 basic_authorization changeset.user.email, "test"
965 diff = <<CHANGESET.strip_heredoc
967 <modify><node id='#{node.id}' lon='0' lat='0' changeset='#{changeset.id}'
969 <node id='#{node.id}' lon='1' lat='1' changeset='#{changeset.id}' version='2'><tag k='k' v='v'/></node></modify>
971 <relation id='#{relation.id}' changeset='#{changeset.id}' version='1'><member
972 type='way' role='some' ref='#{way.id}'/><member
973 type='node' role='some' ref='#{node.id}'/>
974 <member type='relation' role='some' ref='#{other_relation.id}'/>
976 </modify></osmChange>
981 post :upload, :params => { :id => changeset.id }
982 assert_response :success,
983 "can't upload a valid diff with whitespace variations to changeset: #{@response.body}"
985 # check the response is well-formed
986 assert_select "diffResult>node", 2
987 assert_select "diffResult>relation", 1
989 # check that the changes made it into the database
990 assert_equal 1, Node.find(node.id).tags.size, "node #{node.id} should now have one tag"
991 assert_equal 0, Relation.find(relation.id).tags.size, "relation #{relation.id} should now have no tags"
995 # test that a placeholder can be reused within the same upload.
996 def test_upload_reuse_placeholder_valid
997 changeset = create(:changeset)
999 basic_authorization changeset.user.email, "test"
1001 diff = <<CHANGESET.strip_heredoc
1004 <node id='-1' lon='0' lat='0' changeset='#{changeset.id}'>
1005 <tag k="foo" v="bar"/>
1009 <node id='-1' lon='1' lat='1' changeset='#{changeset.id}' version='1'/>
1012 <node id='-1' lon='2' lat='2' changeset='#{changeset.id}' version='2'/>
1019 post :upload, :params => { :id => changeset.id }
1020 assert_response :success,
1021 "can't upload a valid diff with re-used placeholders to changeset: #{@response.body}"
1023 # check the response is well-formed
1024 assert_select "diffResult>node", 3
1025 assert_select "diffResult>node[old_id='-1']", 3
1029 # test what happens if a diff upload re-uses placeholder IDs in an
1031 def test_upload_placeholder_invalid
1032 changeset = create(:changeset)
1034 basic_authorization changeset.user.email, "test"
1036 diff = <<CHANGESET.strip_heredoc
1039 <node id='-1' lon='0' lat='0' changeset='#{changeset.id}' version='1'/>
1040 <node id='-1' lon='1' lat='1' changeset='#{changeset.id}' version='1'/>
1041 <node id='-1' lon='2' lat='2' changeset='#{changeset.id}' version='2'/>
1048 post :upload, :params => { :id => changeset.id }
1049 assert_response :bad_request,
1050 "shouldn't be able to re-use placeholder IDs"
1054 # test that uploading a way referencing invalid placeholders gives a
1055 # proper error, not a 500.
1056 def test_upload_placeholder_invalid_way
1057 changeset = create(:changeset)
1060 basic_authorization changeset.user.email, "test"
1062 diff = <<CHANGESET.strip_heredoc
1065 <node id="-1" lon="0" lat="0" changeset="#{changeset.id}" version="1"/>
1066 <node id="-2" lon="1" lat="1" changeset="#{changeset.id}" version="1"/>
1067 <node id="-3" lon="2" lat="2" changeset="#{changeset.id}" version="1"/>
1068 <way id="-1" changeset="#{changeset.id}" version="1">
1080 post :upload, :params => { :id => changeset.id }
1081 assert_response :bad_request,
1082 "shouldn't be able to use invalid placeholder IDs"
1083 assert_equal "Placeholder node not found for reference -4 in way -1", @response.body
1085 # the same again, but this time use an existing way
1086 diff = <<CHANGESET.strip_heredoc
1089 <node id="-1" lon="0" lat="0" changeset="#{changeset.id}" version="1"/>
1090 <node id="-2" lon="1" lat="1" changeset="#{changeset.id}" version="1"/>
1091 <node id="-3" lon="2" lat="2" changeset="#{changeset.id}" version="1"/>
1092 <way id="#{way.id}" changeset="#{changeset.id}" version="1">
1104 post :upload, :params => { :id => changeset.id }
1105 assert_response :bad_request,
1106 "shouldn't be able to use invalid placeholder IDs"
1107 assert_equal "Placeholder node not found for reference -4 in way #{way.id}", @response.body
1111 # test that uploading a relation referencing invalid placeholders gives a
1112 # proper error, not a 500.
1113 def test_upload_placeholder_invalid_relation
1114 changeset = create(:changeset)
1115 relation = create(:relation)
1117 basic_authorization changeset.user.email, "test"
1119 diff = <<CHANGESET.strip_heredoc
1122 <node id="-1" lon="0" lat="0" changeset="#{changeset.id}" version="1"/>
1123 <node id="-2" lon="1" lat="1" changeset="#{changeset.id}" version="1"/>
1124 <node id="-3" lon="2" lat="2" changeset="#{changeset.id}" version="1"/>
1125 <relation id="-1" changeset="#{changeset.id}" version="1">
1126 <member type="node" role="foo" ref="-1"/>
1127 <member type="node" role="foo" ref="-2"/>
1128 <member type="node" role="foo" ref="-3"/>
1129 <member type="node" role="foo" ref="-4"/>
1137 post :upload, :params => { :id => changeset.id }
1138 assert_response :bad_request,
1139 "shouldn't be able to use invalid placeholder IDs"
1140 assert_equal "Placeholder Node not found for reference -4 in relation -1.", @response.body
1142 # the same again, but this time use an existing relation
1143 diff = <<CHANGESET.strip_heredoc
1146 <node id="-1" lon="0" lat="0" changeset="#{changeset.id}" version="1"/>
1147 <node id="-2" lon="1" lat="1" changeset="#{changeset.id}" version="1"/>
1148 <node id="-3" lon="2" lat="2" changeset="#{changeset.id}" version="1"/>
1149 <relation id="#{relation.id}" changeset="#{changeset.id}" version="1">
1150 <member type="node" role="foo" ref="-1"/>
1151 <member type="node" role="foo" ref="-2"/>
1152 <member type="node" role="foo" ref="-3"/>
1153 <member type="way" role="bar" ref="-1"/>
1161 post :upload, :params => { :id => changeset.id }
1162 assert_response :bad_request,
1163 "shouldn't be able to use invalid placeholder IDs"
1164 assert_equal "Placeholder Way not found for reference -1 in relation #{relation.id}.", @response.body
1168 # test what happens if a diff is uploaded containing only a node
1170 def test_upload_node_move
1171 basic_authorization create(:user).email, "test"
1173 content "<osm><changeset>" \
1174 "<tag k='created_by' v='osm test suite checking changesets'/>" \
1175 "</changeset></osm>"
1177 assert_response :success
1178 changeset_id = @response.body.to_i
1180 old_node = create(:node, :lat => 1, :lon => 1)
1182 diff = XML::Document.new
1183 diff.root = XML::Node.new "osmChange"
1184 modify = XML::Node.new "modify"
1185 xml_old_node = old_node.to_xml_node
1186 xml_old_node["lat"] = 2.0.to_s
1187 xml_old_node["lon"] = 2.0.to_s
1188 xml_old_node["changeset"] = changeset_id.to_s
1189 modify << xml_old_node
1194 post :upload, :params => { :id => changeset_id }
1195 assert_response :success,
1196 "diff should have uploaded OK"
1199 changeset = Changeset.find(changeset_id)
1200 assert_equal 1 * GeoRecord::SCALE, changeset.min_lon, "min_lon should be 1 degree"
1201 assert_equal 2 * GeoRecord::SCALE, changeset.max_lon, "max_lon should be 2 degrees"
1202 assert_equal 1 * GeoRecord::SCALE, changeset.min_lat, "min_lat should be 1 degree"
1203 assert_equal 2 * GeoRecord::SCALE, changeset.max_lat, "max_lat should be 2 degrees"
1207 # test what happens if a diff is uploaded adding a node to a way.
1208 def test_upload_way_extend
1209 basic_authorization create(:user).email, "test"
1211 content "<osm><changeset>" \
1212 "<tag k='created_by' v='osm test suite checking changesets'/>" \
1213 "</changeset></osm>"
1215 assert_response :success
1216 changeset_id = @response.body.to_i
1218 old_way = create(:way)
1219 create(:way_node, :way => old_way, :node => create(:node, :lat => 1, :lon => 1))
1221 diff = XML::Document.new
1222 diff.root = XML::Node.new "osmChange"
1223 modify = XML::Node.new "modify"
1224 xml_old_way = old_way.to_xml_node
1225 nd_ref = XML::Node.new "nd"
1226 nd_ref["ref"] = create(:node, :lat => 3, :lon => 3).id.to_s
1227 xml_old_way << nd_ref
1228 xml_old_way["changeset"] = changeset_id.to_s
1229 modify << xml_old_way
1234 post :upload, :params => { :id => changeset_id }
1235 assert_response :success,
1236 "diff should have uploaded OK"
1239 changeset = Changeset.find(changeset_id)
1240 assert_equal 1 * GeoRecord::SCALE, changeset.min_lon, "min_lon should be 1 degree"
1241 assert_equal 3 * GeoRecord::SCALE, changeset.max_lon, "max_lon should be 3 degrees"
1242 assert_equal 1 * GeoRecord::SCALE, changeset.min_lat, "min_lat should be 1 degree"
1243 assert_equal 3 * GeoRecord::SCALE, changeset.max_lat, "max_lat should be 3 degrees"
1247 # test for more issues in #1568
1248 def test_upload_empty_invalid
1249 changeset = create(:changeset)
1251 basic_authorization changeset.user.email, "test"
1254 "<osmChange></osmChange>",
1255 "<osmChange><modify/></osmChange>",
1256 "<osmChange><modify></modify></osmChange>"].each do |diff|
1259 post :upload, :params => { :id => changeset.id }
1260 assert_response(:success, "should be able to upload " \
1261 "empty changeset: " + diff)
1266 # test that the X-Error-Format header works to request XML errors
1267 def test_upload_xml_errors
1268 changeset = create(:changeset)
1269 node = create(:node)
1270 create(:relation_member, :member => node)
1272 basic_authorization changeset.user.email, "test"
1274 # try and delete a node that is in use
1275 diff = XML::Document.new
1276 diff.root = XML::Node.new "osmChange"
1277 delete = XML::Node.new "delete"
1279 delete << node.to_xml_node
1284 post :upload, :params => { :id => changeset.id }
1285 assert_response :success,
1286 "failed to return error in XML format"
1288 # check the returned payload
1289 assert_select "osmError[version='#{API_VERSION}'][generator='OpenStreetMap server']", 1
1290 assert_select "osmError>status", 1
1291 assert_select "osmError>message", 1
1295 # when we make some simple changes we get the same changes back from the
1297 def test_diff_download_simple
1298 node = create(:node)
1300 ## First try with a non-public user, which should get a forbidden
1301 basic_authorization create(:user, :data_public => false).email, "test"
1303 # create a temporary changeset
1304 content "<osm><changeset>" \
1305 "<tag k='created_by' v='osm test suite checking changesets'/>" \
1306 "</changeset></osm>"
1308 assert_response :forbidden
1310 ## Now try with a normal user
1311 basic_authorization create(:user).email, "test"
1313 # create a temporary changeset
1314 content "<osm><changeset>" \
1315 "<tag k='created_by' v='osm test suite checking changesets'/>" \
1316 "</changeset></osm>"
1318 assert_response :success
1319 changeset_id = @response.body.to_i
1322 diff = <<CHANGESET.strip_heredoc
1325 <node id='#{node.id}' lon='0' lat='0' changeset='#{changeset_id}' version='1'/>
1326 <node id='#{node.id}' lon='1' lat='0' changeset='#{changeset_id}' version='2'/>
1327 <node id='#{node.id}' lon='1' lat='1' changeset='#{changeset_id}' version='3'/>
1328 <node id='#{node.id}' lon='1' lat='2' changeset='#{changeset_id}' version='4'/>
1329 <node id='#{node.id}' lon='2' lat='2' changeset='#{changeset_id}' version='5'/>
1330 <node id='#{node.id}' lon='3' lat='2' changeset='#{changeset_id}' version='6'/>
1331 <node id='#{node.id}' lon='3' lat='3' changeset='#{changeset_id}' version='7'/>
1332 <node id='#{node.id}' lon='9' lat='9' changeset='#{changeset_id}' version='8'/>
1339 post :upload, :params => { :id => changeset_id }
1340 assert_response :success,
1341 "can't upload multiple versions of an element in a diff: #{@response.body}"
1343 get :download, :params => { :id => changeset_id }
1344 assert_response :success
1346 assert_select "osmChange", 1
1347 assert_select "osmChange>modify", 8
1348 assert_select "osmChange>modify>node", 8
1352 # culled this from josm to ensure that nothing in the way that josm
1353 # is formatting the request is causing it to fail.
1355 # NOTE: the error turned out to be something else completely!
1356 def test_josm_upload
1357 basic_authorization create(:user).email, "test"
1359 # create a temporary changeset
1360 content "<osm><changeset>" \
1361 "<tag k='created_by' v='osm test suite checking changesets'/>" \
1362 "</changeset></osm>"
1364 assert_response :success
1365 changeset_id = @response.body.to_i
1367 diff = <<OSMFILE.strip_heredoc
1368 <osmChange version="0.6" generator="JOSM">
1369 <create version="0.6" generator="JOSM">
1370 <node id='-1' visible='true' changeset='#{changeset_id}' lat='51.49619982187321' lon='-0.18722061869438314' />
1371 <node id='-2' visible='true' changeset='#{changeset_id}' lat='51.496359883909605' lon='-0.18653093576241928' />
1372 <node id='-3' visible='true' changeset='#{changeset_id}' lat='51.49598132358285' lon='-0.18719613290981638' />
1373 <node id='-4' visible='true' changeset='#{changeset_id}' lat='51.4961591711078' lon='-0.18629015888084607' />
1374 <node id='-5' visible='true' changeset='#{changeset_id}' lat='51.49582126021711' lon='-0.18708186591517145' />
1375 <node id='-6' visible='true' changeset='#{changeset_id}' lat='51.49591018437858' lon='-0.1861432441734455' />
1376 <node id='-7' visible='true' changeset='#{changeset_id}' lat='51.49560784152179' lon='-0.18694719410005425' />
1377 <node id='-8' visible='true' changeset='#{changeset_id}' lat='51.49567389979617' lon='-0.1860289771788006' />
1378 <node id='-9' visible='true' changeset='#{changeset_id}' lat='51.49543761398892' lon='-0.186820684213126' />
1379 <way id='-10' action='modiy' visible='true' changeset='#{changeset_id}'>
1389 <tag k='highway' v='residential' />
1390 <tag k='name' v='Foobar Street' />
1398 post :upload, :params => { :id => changeset_id }
1399 assert_response :success,
1400 "can't upload a diff from JOSM: #{@response.body}"
1402 get :download, :params => { :id => changeset_id }
1403 assert_response :success
1405 assert_select "osmChange", 1
1406 assert_select "osmChange>create>node", 9
1407 assert_select "osmChange>create>way", 1
1408 assert_select "osmChange>create>way>nd", 9
1409 assert_select "osmChange>create>way>tag", 2
1413 # when we make some complex changes we get the same changes back from the
1415 def test_diff_download_complex
1416 node = create(:node)
1417 node2 = create(:node)
1419 basic_authorization create(:user).email, "test"
1421 # create a temporary changeset
1422 content "<osm><changeset>" \
1423 "<tag k='created_by' v='osm test suite checking changesets'/>" \
1424 "</changeset></osm>"
1426 assert_response :success
1427 changeset_id = @response.body.to_i
1430 diff = <<CHANGESET.strip_heredoc
1433 <node id='#{node.id}' lon='0' lat='0' changeset='#{changeset_id}' version='1'/>
1436 <node id='-1' lon='9' lat='9' changeset='#{changeset_id}' version='0'/>
1437 <node id='-2' lon='8' lat='9' changeset='#{changeset_id}' version='0'/>
1438 <node id='-3' lon='7' lat='9' changeset='#{changeset_id}' version='0'/>
1441 <node id='#{node2.id}' lon='20' lat='15' changeset='#{changeset_id}' version='1'/>
1442 <way id='#{way.id}' changeset='#{changeset_id}' version='1'>
1443 <nd ref='#{node2.id}'/>
1454 post :upload, :params => { :id => changeset_id }
1455 assert_response :success,
1456 "can't upload multiple versions of an element in a diff: #{@response.body}"
1458 get :download, :params => { :id => changeset_id }
1459 assert_response :success
1461 assert_select "osmChange", 1
1462 assert_select "osmChange>create", 3
1463 assert_select "osmChange>delete", 1
1464 assert_select "osmChange>modify", 2
1465 assert_select "osmChange>create>node", 3
1466 assert_select "osmChange>delete>node", 1
1467 assert_select "osmChange>modify>node", 1
1468 assert_select "osmChange>modify>way", 1
1471 def test_changeset_download
1472 changeset = create(:changeset)
1473 node = create(:node, :with_history, :version => 1, :changeset => changeset)
1474 tag = create(:old_node_tag, :old_node => node.old_nodes.find_by(:version => 1))
1475 node2 = create(:node, :with_history, :version => 1, :changeset => changeset)
1476 _node3 = create(:node, :with_history, :deleted, :version => 1, :changeset => changeset)
1477 _relation = create(:relation, :with_history, :version => 1, :changeset => changeset)
1478 _relation2 = create(:relation, :with_history, :deleted, :version => 1, :changeset => changeset)
1480 get :download, :params => { :id => changeset.id }
1482 assert_response :success
1484 # print @response.body
1485 # FIXME: needs more assert_select tests
1486 assert_select "osmChange[version='#{API_VERSION}'][generator='#{GENERATOR}']" do
1487 assert_select "create", :count => 5
1488 assert_select "create>node[id='#{node.id}'][visible='#{node.visible?}'][version='#{node.version}']" do
1489 assert_select "tag[k='#{tag.k}'][v='#{tag.v}']"
1491 assert_select "create>node[id='#{node2.id}']"
1496 # check that the bounding box of a changeset gets updated correctly
1497 # FIXME: This should really be moded to a integration test due to the with_controller
1498 def test_changeset_bbox
1500 create(:way_node, :way => way, :node => create(:node, :lat => 3, :lon => 3))
1502 basic_authorization create(:user).email, "test"
1504 # create a new changeset
1505 content "<osm><changeset/></osm>"
1507 assert_response :success, "Creating of changeset failed."
1508 changeset_id = @response.body.to_i
1510 # add a single node to it
1511 with_controller(NodeController.new) do
1512 content "<osm><node lon='1' lat='2' changeset='#{changeset_id}'/></osm>"
1514 assert_response :success, "Couldn't create node."
1517 # get the bounding box back from the changeset
1518 get :read, :params => { :id => changeset_id }
1519 assert_response :success, "Couldn't read back changeset."
1520 assert_select "osm>changeset[min_lon='1.0000000']", 1
1521 assert_select "osm>changeset[max_lon='1.0000000']", 1
1522 assert_select "osm>changeset[min_lat='2.0000000']", 1
1523 assert_select "osm>changeset[max_lat='2.0000000']", 1
1525 # add another node to it
1526 with_controller(NodeController.new) do
1527 content "<osm><node lon='2' lat='1' changeset='#{changeset_id}'/></osm>"
1529 assert_response :success, "Couldn't create second node."
1532 # get the bounding box back from the changeset
1533 get :read, :params => { :id => changeset_id }
1534 assert_response :success, "Couldn't read back changeset for the second time."
1535 assert_select "osm>changeset[min_lon='1.0000000']", 1
1536 assert_select "osm>changeset[max_lon='2.0000000']", 1
1537 assert_select "osm>changeset[min_lat='1.0000000']", 1
1538 assert_select "osm>changeset[max_lat='2.0000000']", 1
1540 # add (delete) a way to it, which contains a point at (3,3)
1541 with_controller(WayController.new) do
1542 content update_changeset(way.to_xml, changeset_id)
1543 put :delete, :params => { :id => way.id }
1544 assert_response :success, "Couldn't delete a way."
1547 # get the bounding box back from the changeset
1548 get :read, :params => { :id => changeset_id }
1549 assert_response :success, "Couldn't read back changeset for the third time."
1550 assert_select "osm>changeset[min_lon='1.0000000']", 1
1551 assert_select "osm>changeset[max_lon='3.0000000']", 1
1552 assert_select "osm>changeset[min_lat='1.0000000']", 1
1553 assert_select "osm>changeset[max_lat='3.0000000']", 1
1557 # test that the changeset :include method works as it should
1558 def test_changeset_include
1559 basic_authorization create(:user).display_name, "test"
1561 # create a new changeset
1562 content "<osm><changeset/></osm>"
1564 assert_response :success, "Creating of changeset failed."
1565 changeset_id = @response.body.to_i
1567 # NOTE: the include method doesn't over-expand, like inserting
1568 # a real method does. this is because we expect the client to
1569 # know what it is doing!
1570 check_after_include(changeset_id, 1, 1, [1, 1, 1, 1])
1571 check_after_include(changeset_id, 3, 3, [1, 1, 3, 3])
1572 check_after_include(changeset_id, 4, 2, [1, 1, 4, 3])
1573 check_after_include(changeset_id, 2, 2, [1, 1, 4, 3])
1574 check_after_include(changeset_id, -1, -1, [-1, -1, 4, 3])
1575 check_after_include(changeset_id, -2, 5, [-2, -1, 4, 5])
1579 # test that a not found, wrong method with the expand bbox works as expected
1580 def test_changeset_expand_bbox_error
1581 basic_authorization create(:user).display_name, "test"
1583 # create a new changeset
1584 content "<osm><changeset/></osm>"
1586 assert_response :success, "Creating of changeset failed."
1587 changeset_id = @response.body.to_i
1593 content "<osm><node lon='#{lon}' lat='#{lat}'/></osm>"
1594 put :expand_bbox, :params => { :id => changeset_id }
1595 assert_response :method_not_allowed, "shouldn't be able to put a bbox expand"
1597 # Try to get the update
1598 content "<osm><node lon='#{lon}' lat='#{lat}'/></osm>"
1599 get :expand_bbox, :params => { :id => changeset_id }
1600 assert_response :method_not_allowed, "shouldn't be able to get a bbox expand"
1602 # Try to use a hopefully missing changeset
1603 content "<osm><node lon='#{lon}' lat='#{lat}'/></osm>"
1604 post :expand_bbox, :params => { :id => changeset_id + 13245 }
1605 assert_response :not_found, "shouldn't be able to do a bbox expand on a nonexistant changeset"
1609 # test the query functionality of changesets
1611 private_user = create(:user, :data_public => false)
1612 private_user_changeset = create(:changeset, :user => private_user)
1613 private_user_closed_changeset = create(:changeset, :closed, :user => private_user)
1614 user = create(:user)
1615 changeset = create(:changeset, :user => user)
1616 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))
1617 changeset2 = create(:changeset, :min_lat => 5 * GeoRecord::SCALE, :min_lon => 5 * GeoRecord::SCALE, :max_lat => 15 * GeoRecord::SCALE, :max_lon => 15 * GeoRecord::SCALE)
1618 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)
1620 get :query, :params => { :bbox => "-10,-10, 10, 10" }
1621 assert_response :success, "can't get changesets in bbox"
1622 assert_changesets [changeset2, changeset3]
1624 get :query, :params => { :bbox => "4.5,4.5,4.6,4.6" }
1625 assert_response :success, "can't get changesets in bbox"
1626 assert_changesets [changeset3]
1628 # not found when looking for changesets of non-existing users
1629 get :query, :params => { :user => User.maximum(:id) + 1 }
1630 assert_response :not_found
1631 get :query, :params => { :display_name => " " }
1632 assert_response :not_found
1634 # can't get changesets of user 1 without authenticating
1635 get :query, :params => { :user => private_user.id }
1636 assert_response :not_found, "shouldn't be able to get changesets by non-public user (ID)"
1637 get :query, :params => { :display_name => private_user.display_name }
1638 assert_response :not_found, "shouldn't be able to get changesets by non-public user (name)"
1640 # but this should work
1641 basic_authorization private_user.email, "test"
1642 get :query, :params => { :user => private_user.id }
1643 assert_response :success, "can't get changesets by user ID"
1644 assert_changesets [private_user_changeset, private_user_closed_changeset]
1646 get :query, :params => { :display_name => private_user.display_name }
1647 assert_response :success, "can't get changesets by user name"
1648 assert_changesets [private_user_changeset, private_user_closed_changeset]
1650 # check that the correct error is given when we provide both UID and name
1651 get :query, :params => { :user => private_user.id,
1652 :display_name => private_user.display_name }
1653 assert_response :bad_request, "should be a bad request to have both ID and name specified"
1655 get :query, :params => { :user => private_user.id, :open => true }
1656 assert_response :success, "can't get changesets by user and open"
1657 assert_changesets [private_user_changeset]
1659 get :query, :params => { :time => "2007-12-31" }
1660 assert_response :success, "can't get changesets by time-since"
1661 assert_changesets [private_user_changeset, private_user_closed_changeset, changeset, closed_changeset, changeset2, changeset3]
1663 get :query, :params => { :time => "2008-01-01T12:34Z" }
1664 assert_response :success, "can't get changesets by time-since with hour"
1665 assert_changesets [private_user_changeset, private_user_closed_changeset, changeset, closed_changeset, changeset2, changeset3]
1667 get :query, :params => { :time => "2007-12-31T23:59Z,2008-01-02T00:01Z" }
1668 assert_response :success, "can't get changesets by time-range"
1669 assert_changesets [closed_changeset]
1671 get :query, :params => { :open => "true" }
1672 assert_response :success, "can't get changesets by open-ness"
1673 assert_changesets [private_user_changeset, changeset, changeset2, changeset3]
1675 get :query, :params => { :closed => "true" }
1676 assert_response :success, "can't get changesets by closed-ness"
1677 assert_changesets [private_user_closed_changeset, closed_changeset]
1679 get :query, :params => { :closed => "true", :user => private_user.id }
1680 assert_response :success, "can't get changesets by closed-ness and user"
1681 assert_changesets [private_user_closed_changeset]
1683 get :query, :params => { :closed => "true", :user => user.id }
1684 assert_response :success, "can't get changesets by closed-ness and user"
1685 assert_changesets [closed_changeset]
1687 get :query, :params => { :changesets => "#{private_user_changeset.id},#{changeset.id},#{closed_changeset.id}" }
1688 assert_response :success, "can't get changesets by id (as comma-separated string)"
1689 assert_changesets [private_user_changeset, changeset, closed_changeset]
1691 get :query, :params => { :changesets => "" }
1692 assert_response :bad_request, "should be a bad request since changesets is empty"
1696 # check that errors are returned if garbage is inserted
1697 # into query strings
1698 def test_query_invalid
1701 ";drop table users;"].each do |bbox|
1702 get :query, :params => { :bbox => bbox }
1703 assert_response :bad_request, "'#{bbox}' isn't a bbox"
1708 ";drop table users;",
1710 "-,-"].each do |time|
1711 get :query, :params => { :time => time }
1712 assert_response :bad_request, "'#{time}' isn't a valid time range"
1719 get :query, :params => { :user => uid }
1720 assert_response :bad_request, "'#{uid}' isn't a valid user ID"
1725 # check updating tags on a changeset
1726 def test_changeset_update
1727 private_user = create(:user, :data_public => false)
1728 private_changeset = create(:changeset, :user => private_user)
1729 user = create(:user)
1730 changeset = create(:changeset, :user => user)
1732 ## First try with a non-public user
1733 new_changeset = private_changeset.to_xml
1734 new_tag = XML::Node.new "tag"
1735 new_tag["k"] = "tagtesting"
1736 new_tag["v"] = "valuetesting"
1737 new_changeset.find("//osm/changeset").first << new_tag
1738 content new_changeset
1740 # try without any authorization
1741 put :update, :params => { :id => private_changeset.id }
1742 assert_response :unauthorized
1744 # try with the wrong authorization
1745 basic_authorization create(:user).email, "test"
1746 put :update, :params => { :id => private_changeset.id }
1747 assert_response :conflict
1749 # now this should get an unauthorized
1750 basic_authorization private_user.email, "test"
1751 put :update, :params => { :id => private_changeset.id }
1752 assert_require_public_data "user with their data non-public, shouldn't be able to edit their changeset"
1754 ## Now try with the public user
1755 create(:changeset_tag, :changeset => changeset)
1756 new_changeset = changeset.to_xml
1757 new_tag = XML::Node.new "tag"
1758 new_tag["k"] = "tagtesting"
1759 new_tag["v"] = "valuetesting"
1760 new_changeset.find("//osm/changeset").first << new_tag
1761 content new_changeset
1763 # try without any authorization
1764 @request.env["HTTP_AUTHORIZATION"] = nil
1765 put :update, :params => { :id => changeset.id }
1766 assert_response :unauthorized
1768 # try with the wrong authorization
1769 basic_authorization create(:user).email, "test"
1770 put :update, :params => { :id => changeset.id }
1771 assert_response :conflict
1773 # now this should work...
1774 basic_authorization user.email, "test"
1775 put :update, :params => { :id => changeset.id }
1776 assert_response :success
1778 assert_select "osm>changeset[id='#{changeset.id}']", 1
1779 assert_select "osm>changeset>tag", 2
1780 assert_select "osm>changeset>tag[k='tagtesting'][v='valuetesting']", 1
1784 # check that a user different from the one who opened the changeset
1786 def test_changeset_update_invalid
1787 basic_authorization create(:user).email, "test"
1789 changeset = create(:changeset)
1790 new_changeset = changeset.to_xml
1791 new_tag = XML::Node.new "tag"
1792 new_tag["k"] = "testing"
1793 new_tag["v"] = "testing"
1794 new_changeset.find("//osm/changeset").first << new_tag
1796 content new_changeset
1797 put :update, :params => { :id => changeset.id }
1798 assert_response :conflict
1802 # check that a changeset can contain a certain max number of changes.
1803 ## FIXME should be changed to an integration test due to the with_controller
1804 def test_changeset_limits
1805 basic_authorization create(:user).email, "test"
1807 # open a new changeset
1808 content "<osm><changeset/></osm>"
1810 assert_response :success, "can't create a new changeset"
1811 cs_id = @response.body.to_i
1813 # start the counter just short of where the changeset should finish.
1815 # alter the database to set the counter on the changeset directly,
1816 # otherwise it takes about 6 minutes to fill all of them.
1817 changeset = Changeset.find(cs_id)
1818 changeset.num_changes = Changeset::MAX_ELEMENTS - offset
1821 with_controller(NodeController.new) do
1823 content "<osm><node changeset='#{cs_id}' lat='0.0' lon='0.0'/></osm>"
1825 assert_response :success, "can't create a new node"
1826 node_id = @response.body.to_i
1828 get :read, :params => { :id => node_id }
1829 assert_response :success, "can't read back new node"
1830 node_doc = XML::Parser.string(@response.body).parse
1831 node_xml = node_doc.find("//osm/node").first
1833 # loop until we fill the changeset with nodes
1835 node_xml["lat"] = rand.to_s
1836 node_xml["lon"] = rand.to_s
1837 node_xml["version"] = (i + 1).to_s
1840 put :update, :params => { :id => node_id }
1841 assert_response :success, "attempt #{i} should have succeeded"
1844 # trying again should fail
1845 node_xml["lat"] = rand.to_s
1846 node_xml["lon"] = rand.to_s
1847 node_xml["version"] = offset.to_s
1850 put :update, :params => { :id => node_id }
1851 assert_response :conflict, "final attempt should have failed"
1854 changeset = Changeset.find(cs_id)
1855 assert_equal Changeset::MAX_ELEMENTS + 1, changeset.num_changes
1857 # check that the changeset is now closed as well
1858 assert_not(changeset.is_open?,
1859 "changeset should have been auto-closed by exceeding " \
1864 # This should display the last 20 changesets closed
1866 get :index, :params => { :format => "html" }
1867 assert_response :success
1868 assert_template "history"
1869 assert_template :layout => "map"
1870 assert_select "h2", :text => "Changesets", :count => 1
1872 get :index, :params => { :format => "html", :list => "1" }, :xhr => true
1873 assert_response :success
1874 assert_template "index"
1876 check_index_result(Changeset.all)
1880 # This should display the last 20 changesets closed
1882 get :index, :params => { :format => "html" }, :xhr => true
1883 assert_response :success
1884 assert_template "history"
1885 assert_template :layout => "xhr"
1886 assert_select "h2", :text => "Changesets", :count => 1
1888 get :index, :params => { :format => "html", :list => "1" }, :xhr => true
1889 assert_response :success
1890 assert_template "index"
1892 check_index_result(Changeset.all)
1896 # This should display the last 20 changesets closed in a specific area
1898 get :index, :params => { :format => "html", :bbox => "4.5,4.5,5.5,5.5" }
1899 assert_response :success
1900 assert_template "history"
1901 assert_template :layout => "map"
1902 assert_select "h2", :text => "Changesets", :count => 1
1904 get :index, :params => { :format => "html", :bbox => "4.5,4.5,5.5,5.5", :list => "1" }, :xhr => true
1905 assert_response :success
1906 assert_template "index"
1908 check_index_result(Changeset.where("min_lon < 55000000 and max_lon > 45000000 and min_lat < 55000000 and max_lat > 45000000"))
1912 # Checks the display of the user changesets listing
1914 user = create(:user)
1915 create(:changeset, :user => user)
1916 create(:changeset, :closed, :user => user)
1918 get :index, :params => { :format => "html", :display_name => user.display_name }
1919 assert_response :success
1920 assert_template "history"
1922 get :index, :params => { :format => "html", :display_name => user.display_name, :list => "1" }, :xhr => true
1923 assert_response :success
1924 assert_template "index"
1926 check_index_result(user.changesets)
1930 # Checks the display of the user changesets listing for a private user
1931 def test_index_private_user
1932 private_user = create(:user, :data_public => false)
1933 create(:changeset, :user => private_user)
1934 create(:changeset, :closed, :user => private_user)
1936 get :index, :params => { :format => "html", :display_name => private_user.display_name }
1937 assert_response :success
1938 assert_template "history"
1940 get :index, :params => { :format => "html", :display_name => private_user.display_name, :list => "1" }, :xhr => true
1941 assert_response :success
1942 assert_template "index"
1944 check_index_result(Changeset.none)
1948 # Check the not found of the index user changesets
1949 def test_index_user_not_found
1950 get :index, :params => { :format => "html", :display_name => "Some random user" }
1951 assert_response :not_found
1952 assert_template "users/no_such_user"
1954 get :index, :params => { :format => "html", :display_name => "Some random user", :list => "1" }, :xhr => true
1955 assert_response :not_found
1956 assert_template "users/no_such_user"
1960 # Checks the display of the friends changesets listing
1961 def test_index_friends
1962 private_user = create(:user, :data_public => true)
1963 friend = create(:friend, :befriender => private_user)
1964 create(:changeset, :user => friend.befriendee)
1966 get :index, :params => { :friends => true }
1967 assert_response :redirect
1968 assert_redirected_to :controller => :users, :action => :login, :referer => friend_changesets_path
1970 session[:user] = private_user.id
1972 get :index, :params => { :friends => true }
1973 assert_response :success
1974 assert_template "history"
1976 get :index, :params => { :friends => true, :list => "1" }, :xhr => true
1977 assert_response :success
1978 assert_template "index"
1980 check_index_result(Changeset.where(:user => private_user.friend_users.identifiable))
1984 # Checks the display of the nearby user changesets listing
1985 def test_index_nearby
1986 private_user = create(:user, :data_public => false, :home_lat => 51.1, :home_lon => 1.0)
1987 user = create(:user, :home_lat => 51.0, :home_lon => 1.0)
1988 create(:changeset, :user => user)
1990 get :index, :params => { :nearby => true }
1991 assert_response :redirect
1992 assert_redirected_to :controller => :users, :action => :login, :referer => nearby_changesets_path
1994 session[:user] = private_user.id
1996 get :index, :params => { :nearby => true }
1997 assert_response :success
1998 assert_template "history"
2000 get :index, :params => { :nearby => true, :list => "1" }, :xhr => true
2001 assert_response :success
2002 assert_template "index"
2004 check_index_result(Changeset.where(:user => user.nearby))
2008 # Check that we can't request later pages of the changesets index
2009 def test_index_max_id
2010 get :index, :params => { :format => "html", :max_id => 4 }, :xhr => true
2011 assert_response :success
2012 assert_template "history"
2013 assert_template :layout => "xhr"
2014 assert_select "h2", :text => "Changesets", :count => 1
2016 get :index, :params => { :format => "html", :list => "1", :max_id => 4 }, :xhr => true
2017 assert_response :success
2018 assert_template "index"
2020 check_index_result(Changeset.where("id <= 4"))
2024 # Check that a list with a next page link works
2026 create_list(:changeset, 50)
2028 get :index, :params => { :format => "html" }
2029 assert_response :success
2031 get :index, :params => { :format => "html" }, :xhr => true
2032 assert_response :success
2036 # This should display the last 20 non-empty changesets
2038 changeset = create(:changeset, :num_changes => 1)
2039 create(:changeset_tag, :changeset => changeset)
2040 create(:changeset_tag, :changeset => changeset, :k => "website", :v => "http://example.com/")
2041 closed_changeset = create(:changeset, :closed, :num_changes => 1)
2042 _empty_changeset = create(:changeset, :num_changes => 0)
2044 get :feed, :params => { :format => :atom }
2045 assert_response :success
2046 assert_template "index"
2047 assert_equal "application/atom+xml", response.content_type
2049 check_feed_result([changeset, closed_changeset])
2053 # This should display the last 20 changesets closed in a specific area
2055 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)
2056 create(:changeset_tag, :changeset => changeset)
2057 create(:changeset_tag, :changeset => changeset, :k => "website", :v => "http://example.com/")
2058 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)
2059 _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)
2060 _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)
2062 get :feed, :params => { :format => :atom, :bbox => "4.5,4.5,5.5,5.5" }
2063 assert_response :success
2064 assert_template "index"
2065 assert_equal "application/atom+xml", response.content_type
2067 check_feed_result([changeset, closed_changeset])
2071 # Checks the display of the user changesets feed
2073 user = create(:user)
2074 changesets = create_list(:changeset, 3, :user => user, :num_changes => 4)
2075 create(:changeset_tag, :changeset => changesets[1])
2076 create(:changeset_tag, :changeset => changesets[1], :k => "website", :v => "http://example.com/")
2077 _other_changeset = create(:changeset)
2079 get :feed, :params => { :format => :atom, :display_name => user.display_name }
2081 assert_response :success
2082 assert_template "index"
2083 assert_equal "application/atom+xml", response.content_type
2085 check_feed_result(changesets)
2089 # Check the not found of the user changesets feed
2090 def test_feed_user_not_found
2091 get :feed, :params => { :format => "atom", :display_name => "Some random user" }
2092 assert_response :not_found
2096 # Check that we can't request later pages of the changesets feed
2097 def test_feed_max_id
2098 get :feed, :params => { :format => "atom", :max_id => 100 }
2099 assert_response :redirect
2100 assert_redirected_to :action => :feed
2104 # check that the changeset download for a changeset with a redacted
2105 # element in it doesn't contain that element.
2106 def test_diff_download_redacted
2107 changeset = create(:changeset)
2108 node = create(:node, :with_history, :version => 2, :changeset => changeset)
2109 node_v1 = node.old_nodes.find_by(:version => 1)
2110 node_v1.redact!(create(:redaction))
2112 get :download, :params => { :id => changeset.id }
2113 assert_response :success
2115 assert_select "osmChange", 1
2116 # this changeset contains the node in versions 1 & 2, but 1 should
2118 assert_select "osmChange node[id='#{node.id}']", 1
2119 assert_select "osmChange node[id='#{node.id}'][version='1']", 0
2123 # test subscribe success
2124 def test_subscribe_success
2125 basic_authorization create(:user).email, "test"
2126 changeset = create(:changeset, :closed)
2128 assert_difference "changeset.subscribers.count", 1 do
2129 post :subscribe, :params => { :id => changeset.id }
2131 assert_response :success
2133 # not closed changeset
2134 changeset = create(:changeset)
2135 assert_difference "changeset.subscribers.count", 1 do
2136 post :subscribe, :params => { :id => changeset.id }
2138 assert_response :success
2142 # test subscribe fail
2143 def test_subscribe_fail
2144 user = create(:user)
2147 changeset = create(:changeset, :closed)
2148 assert_no_difference "changeset.subscribers.count" do
2149 post :subscribe, :params => { :id => changeset.id }
2151 assert_response :unauthorized
2153 basic_authorization user.email, "test"
2156 assert_no_difference "changeset.subscribers.count" do
2157 post :subscribe, :params => { :id => 999111 }
2159 assert_response :not_found
2161 # trying to subscribe when already subscribed
2162 changeset = create(:changeset, :closed)
2163 changeset.subscribers.push(user)
2164 assert_no_difference "changeset.subscribers.count" do
2165 post :subscribe, :params => { :id => changeset.id }
2167 assert_response :conflict
2171 # test unsubscribe success
2172 def test_unsubscribe_success
2173 user = create(:user)
2174 basic_authorization user.email, "test"
2175 changeset = create(:changeset, :closed)
2176 changeset.subscribers.push(user)
2178 assert_difference "changeset.subscribers.count", -1 do
2179 post :unsubscribe, :params => { :id => changeset.id }
2181 assert_response :success
2183 # not closed changeset
2184 changeset = create(:changeset)
2185 changeset.subscribers.push(user)
2187 assert_difference "changeset.subscribers.count", -1 do
2188 post :unsubscribe, :params => { :id => changeset.id }
2190 assert_response :success
2194 # test unsubscribe fail
2195 def test_unsubscribe_fail
2197 changeset = create(:changeset, :closed)
2198 assert_no_difference "changeset.subscribers.count" do
2199 post :unsubscribe, :params => { :id => changeset.id }
2201 assert_response :unauthorized
2203 basic_authorization create(:user).email, "test"
2206 assert_no_difference "changeset.subscribers.count" do
2207 post :unsubscribe, :params => { :id => 999111 }
2209 assert_response :not_found
2211 # trying to unsubscribe when not subscribed
2212 changeset = create(:changeset, :closed)
2213 assert_no_difference "changeset.subscribers.count" do
2214 post :unsubscribe, :params => { :id => changeset.id }
2216 assert_response :not_found
2222 # boilerplate for checking that certain changesets exist in the
2224 def assert_changesets(changesets)
2225 assert_select "osm>changeset", changesets.size
2226 changesets.each do |changeset|
2227 assert_select "osm>changeset[id='#{changeset.id}']", 1
2232 # call the include method and assert properties of the bbox
2233 def check_after_include(changeset_id, lon, lat, bbox)
2234 content "<osm><node lon='#{lon}' lat='#{lat}'/></osm>"
2235 post :expand_bbox, :params => { :id => changeset_id }
2236 assert_response :success, "Setting include of changeset failed: #{@response.body}"
2238 # check exactly one changeset
2239 assert_select "osm>changeset", 1
2240 assert_select "osm>changeset[id='#{changeset_id}']", 1
2243 doc = XML::Parser.string(@response.body).parse
2244 changeset = doc.find("//osm/changeset").first
2245 assert_equal bbox[0], changeset["min_lon"].to_f, "min lon"
2246 assert_equal bbox[1], changeset["min_lat"].to_f, "min lat"
2247 assert_equal bbox[2], changeset["max_lon"].to_f, "max lon"
2248 assert_equal bbox[3], changeset["max_lat"].to_f, "max lat"
2252 # update the changeset_id of a way element
2253 def update_changeset(xml, changeset_id)
2254 xml_attr_rewrite(xml, "changeset", changeset_id)
2258 # update an attribute in a way element
2259 def xml_attr_rewrite(xml, name, value)
2260 xml.find("//osm/way").first[name] = value.to_s
2265 # check the result of a index
2266 def check_index_result(changesets)
2267 changesets = changesets.where("num_changes > 0")
2268 .order(:created_at => :desc)
2270 assert changesets.size <= 20
2272 assert_select "ol.changesets", :count => [changesets.size, 1].min do
2273 assert_select "li", :count => changesets.size
2275 changesets.each do |changeset|
2276 assert_select "li#changeset_#{changeset.id}", :count => 1
2282 # check the result of a feed
2283 def check_feed_result(changesets)
2284 assert changesets.size <= 20
2286 assert_select "feed", :count => [changesets.size, 1].min do
2287 assert_select "> title", :count => 1, :text => /^Changesets/
2288 assert_select "> entry", :count => changesets.size
2290 changesets.each do |changeset|
2291 assert_select "> entry > id", changeset_url(:id => changeset.id)