4 class ChangesetsControllerTest < ActionController::TestCase
6 # test all routes which lead to this controller
9 { :path => "/api/0.6/changeset/create", :method => :put },
10 { :controller => "api/changesets", :action => "create" }
13 { :path => "/api/0.6/changeset/1/upload", :method => :post },
14 { :controller => "api/changesets", :action => "upload", :id => "1" }
17 { :path => "/api/0.6/changeset/1/download", :method => :get },
18 { :controller => "api/changesets", :action => "download", :id => "1" }
21 { :path => "/api/0.6/changeset/1/expand_bbox", :method => :post },
22 { :controller => "api/changesets", :action => "expand_bbox", :id => "1" }
25 { :path => "/api/0.6/changeset/1", :method => :get },
26 { :controller => "api/changesets", :action => "show", :id => "1" }
29 { :path => "/api/0.6/changeset/1/subscribe", :method => :post },
30 { :controller => "api/changesets", :action => "subscribe", :id => "1" }
33 { :path => "/api/0.6/changeset/1/unsubscribe", :method => :post },
34 { :controller => "api/changesets", :action => "unsubscribe", :id => "1" }
37 { :path => "/api/0.6/changeset/1", :method => :put },
38 { :controller => "api/changesets", :action => "update", :id => "1" }
41 { :path => "/api/0.6/changeset/1/close", :method => :put },
42 { :controller => "api/changesets", :action => "close", :id => "1" }
45 { :path => "/api/0.6/changesets", :method => :get },
46 { :controller => "api/changesets", :action => "query" }
50 # -----------------------
51 # Test simple changeset creation
52 # -----------------------
55 basic_authorization create(:user, :data_public => false).email, "test"
56 # Create the first user's changeset
57 xml = "<osm><changeset>" \
58 "<tag k='created_by' v='osm test suite checking changesets'/>" \
60 put :create, :body => xml
61 assert_require_public_data
63 basic_authorization create(:user).email, "test"
64 # Create the first user's changeset
65 xml = "<osm><changeset>" \
66 "<tag k='created_by' v='osm test suite checking changesets'/>" \
68 put :create, :body => xml
70 assert_response :success, "Creation of changeset did not return sucess status"
71 newid = @response.body.to_i
73 # check end time, should be an hour ahead of creation time
74 cs = Changeset.find(newid)
75 duration = cs.closed_at - cs.created_at
76 # the difference can either be a rational, or a floating point number
77 # of seconds, depending on the code path taken :-(
78 if duration.class == Rational
79 assert_equal Rational(1, 24), duration, "initial idle timeout should be an hour (#{cs.created_at} -> #{cs.closed_at})"
81 # must be number of seconds...
82 assert_equal 3600, duration.round, "initial idle timeout should be an hour (#{cs.created_at} -> #{cs.closed_at})"
85 # checks if uploader was subscribed
86 assert_equal 1, cs.subscribers.length
89 def test_create_invalid
90 basic_authorization create(:user, :data_public => false).email, "test"
91 xml = "<osm><changeset></osm>"
92 put :create, :body => xml
93 assert_require_public_data
95 ## Try the public user
96 basic_authorization create(:user).email, "test"
97 xml = "<osm><changeset></osm>"
98 put :create, :body => xml
99 assert_response :bad_request, "creating a invalid changeset should fail"
102 def test_create_invalid_no_content
103 ## First check with no auth
105 assert_response :unauthorized, "shouldn't be able to create a changeset with no auth"
107 ## Now try to with a non-public user
108 basic_authorization create(:user, :data_public => false).email, "test"
110 assert_require_public_data
112 ## Try an inactive user
113 basic_authorization create(:user, :pending).email, "test"
117 ## Now try to use a normal user
118 basic_authorization create(:user).email, "test"
120 assert_response :bad_request, "creating a changeset with no content should fail"
123 def test_create_wrong_method
124 basic_authorization create(:user).email, "test"
126 assert_response :method_not_allowed
128 assert_response :method_not_allowed
132 # check that the changeset can be shown and returns the correct
133 # document structure.
135 changeset_id = create(:changeset).id
137 get :show, :params => { :id => changeset_id }
138 assert_response :success, "cannot get first changeset"
140 assert_select "osm[version='#{Settings.api_version}'][generator='OpenStreetMap server']", 1
141 assert_select "osm>changeset[id='#{changeset_id}']", 1
142 assert_select "osm>changeset>discussion", 0
144 get :show, :params => { :id => changeset_id, :include_discussion => true }
145 assert_response :success, "cannot get first changeset with comments"
147 assert_select "osm[version='#{Settings.api_version}'][generator='OpenStreetMap server']", 1
148 assert_select "osm>changeset[id='#{changeset_id}']", 1
149 assert_select "osm>changeset>discussion", 1
150 assert_select "osm>changeset>discussion>comment", 0
152 changeset_id = create(:changeset, :closed).id
153 create_list(:changeset_comment, 3, :changeset_id => changeset_id)
155 get :show, :params => { :id => changeset_id, :include_discussion => true }
156 assert_response :success, "cannot get closed changeset with comments"
158 assert_select "osm[version='#{Settings.api_version}'][generator='OpenStreetMap server']", 1
159 assert_select "osm>changeset[id='#{changeset_id}']", 1
160 assert_select "osm>changeset>discussion", 1
161 assert_select "osm>changeset>discussion>comment", 3
165 # check that a changeset that doesn't exist returns an appropriate message
166 def test_show_not_found
167 [0, -32, 233455644, "afg", "213"].each do |id|
168 get :show, :params => { :id => id }
169 assert_response :not_found, "should get a not found"
170 rescue ActionController::UrlGenerationError => ex
171 assert_match(/No route matches/, ex.to_s)
176 # test that the user who opened a change can close it
178 private_user = create(:user, :data_public => false)
179 private_changeset = create(:changeset, :user => private_user)
181 changeset = create(:changeset, :user => user)
183 ## Try without authentication
184 put :close, :params => { :id => changeset.id }
185 assert_response :unauthorized
187 ## Try using the non-public user
188 basic_authorization private_user.email, "test"
189 put :close, :params => { :id => private_changeset.id }
190 assert_require_public_data
192 ## The try with the public user
193 basic_authorization user.email, "test"
196 put :close, :params => { :id => cs_id }
197 assert_response :success
199 # test that it really is closed now
200 cs = Changeset.find(cs_id)
201 assert_not(cs.is_open?,
202 "changeset should be closed now (#{cs.closed_at} > #{Time.now.getutc}.")
206 # test that a different user can't close another user's changeset
207 def test_close_invalid
209 changeset = create(:changeset)
211 basic_authorization user.email, "test"
213 put :close, :params => { :id => changeset.id }
214 assert_response :conflict
215 assert_equal "The user doesn't own that changeset", @response.body
219 # test that you can't close using another method
220 def test_close_method_invalid
222 changeset = create(:changeset, :user => user)
224 basic_authorization user.email, "test"
226 get :close, :params => { :id => changeset.id }
227 assert_response :method_not_allowed
229 post :close, :params => { :id => changeset.id }
230 assert_response :method_not_allowed
234 # check that you can't close a changeset that isn't found
235 def test_close_not_found
236 cs_ids = [0, -132, "123"]
238 # First try to do it with no auth
240 put :close, :params => { :id => id }
241 assert_response :unauthorized, "Shouldn't be able close the non-existant changeset #{id}, when not authorized"
242 rescue ActionController::UrlGenerationError => ex
243 assert_match(/No route matches/, ex.to_s)
247 basic_authorization create(:user).email, "test"
249 put :close, :params => { :id => id }
250 assert_response :not_found, "The changeset #{id} doesn't exist, so can't be closed"
251 rescue ActionController::UrlGenerationError => ex
252 assert_match(/No route matches/, ex.to_s)
257 # upload something simple, but valid and check that it can
259 # Also try without auth and another user.
260 def test_upload_simple_valid
261 private_user = create(:user, :data_public => false)
262 private_changeset = create(:changeset, :user => private_user)
264 changeset = create(:changeset, :user => user)
268 relation = create(:relation)
269 other_relation = create(:relation)
270 # create some tags, since we test that they are removed later
271 create(:node_tag, :node => node)
272 create(:way_tag, :way => way)
273 create(:relation_tag, :relation => relation)
276 changeset_id = changeset.id
278 # simple diff to change a node, way and relation by removing
280 diff = <<CHANGESET.strip_heredoc
283 <node id='#{node.id}' lon='0' lat='0' changeset='#{changeset_id}' version='1'/>
284 <way id='#{way.id}' changeset='#{changeset_id}' version='1'>
285 <nd ref='#{node.id}'/>
289 <relation id='#{relation.id}' changeset='#{changeset_id}' version='1'>
290 <member type='way' role='some' ref='#{way.id}'/>
291 <member type='node' role='some' ref='#{node.id}'/>
292 <member type='relation' role='some' ref='#{other_relation.id}'/>
299 post :upload, :params => { :id => changeset_id }, :body => diff
300 assert_response :unauthorized,
301 "shouldn't be able to upload a simple valid diff to changeset: #{@response.body}"
303 ## Now try with a private user
304 basic_authorization private_user.email, "test"
305 changeset_id = private_changeset.id
307 # simple diff to change a node, way and relation by removing
309 diff = <<CHANGESET.strip_heredoc
312 <node id='#{node.id}' lon='0' lat='0' changeset='#{changeset_id}' version='1'/>
313 <way id='#{way.id}' changeset='#{changeset_id}' version='1'>
314 <nd ref='#{node.id}'/>
318 <relation id='#{relation.id}' changeset='#{changeset_id}' version='1'>
319 <member type='way' role='some' ref='#{way.id}'/>
320 <member type='node' role='some' ref='#{node.id}'/>
321 <member type='relation' role='some' ref='#{other_relation.id}'/>
328 post :upload, :params => { :id => changeset_id }, :body => diff
329 assert_response :forbidden,
330 "can't upload a simple valid diff to changeset: #{@response.body}"
332 ## Now try with the public user
333 basic_authorization user.email, "test"
334 changeset_id = changeset.id
336 # simple diff to change a node, way and relation by removing
338 diff = <<CHANGESET.strip_heredoc
341 <node id='#{node.id}' lon='0' lat='0' changeset='#{changeset_id}' version='1'/>
342 <way id='#{way.id}' changeset='#{changeset_id}' version='1'>
343 <nd ref='#{node.id}'/>
347 <relation id='#{relation.id}' changeset='#{changeset_id}' version='1'>
348 <member type='way' role='some' ref='#{way.id}'/>
349 <member type='node' role='some' ref='#{node.id}'/>
350 <member type='relation' role='some' ref='#{other_relation.id}'/>
357 post :upload, :params => { :id => changeset_id }, :body => diff
358 assert_response :success,
359 "can't upload a simple valid diff to changeset: #{@response.body}"
361 # check that the changes made it into the database
362 assert_equal 0, Node.find(node.id).tags.size, "node #{node.id} should now have no tags"
363 assert_equal 0, Way.find(way.id).tags.size, "way #{way.id} should now have no tags"
364 assert_equal 0, Relation.find(relation.id).tags.size, "relation #{relation.id} should now have no tags"
368 # upload something which creates new objects using placeholders
369 def test_upload_create_valid
371 changeset = create(:changeset, :user => user)
373 way = create(:way_with_nodes, :nodes_count => 2)
374 relation = create(:relation)
376 basic_authorization user.email, "test"
378 # simple diff to create a node way and relation using placeholders
379 diff = <<CHANGESET.strip_heredoc
382 <node id='-1' lon='0' lat='0' changeset='#{changeset.id}'>
383 <tag k='foo' v='bar'/>
384 <tag k='baz' v='bat'/>
386 <way id='-1' changeset='#{changeset.id}'>
387 <nd ref='#{node.id}'/>
391 <relation id='-1' changeset='#{changeset.id}'>
392 <member type='way' role='some' ref='#{way.id}'/>
393 <member type='node' role='some' ref='#{node.id}'/>
394 <member type='relation' role='some' ref='#{relation.id}'/>
401 post :upload, :params => { :id => changeset.id }, :body => diff
402 assert_response :success,
403 "can't upload a simple valid creation to changeset: #{@response.body}"
405 # check the returned payload
406 assert_select "diffResult[version='#{Settings.api_version}'][generator='OpenStreetMap server']", 1
407 assert_select "diffResult>node", 1
408 assert_select "diffResult>way", 1
409 assert_select "diffResult>relation", 1
411 # inspect the response to find out what the new element IDs are
412 doc = XML::Parser.string(@response.body).parse
413 new_node_id = doc.find("//diffResult/node").first["new_id"].to_i
414 new_way_id = doc.find("//diffResult/way").first["new_id"].to_i
415 new_rel_id = doc.find("//diffResult/relation").first["new_id"].to_i
417 # check the old IDs are all present and negative one
418 assert_equal(-1, doc.find("//diffResult/node").first["old_id"].to_i)
419 assert_equal(-1, doc.find("//diffResult/way").first["old_id"].to_i)
420 assert_equal(-1, doc.find("//diffResult/relation").first["old_id"].to_i)
422 # check the versions are present and equal one
423 assert_equal 1, doc.find("//diffResult/node").first["new_version"].to_i
424 assert_equal 1, doc.find("//diffResult/way").first["new_version"].to_i
425 assert_equal 1, doc.find("//diffResult/relation").first["new_version"].to_i
427 # check that the changes made it into the database
428 assert_equal 2, Node.find(new_node_id).tags.size, "new node should have two tags"
429 assert_equal 0, Way.find(new_way_id).tags.size, "new way should have no tags"
430 assert_equal 0, Relation.find(new_rel_id).tags.size, "new relation should have no tags"
434 # test a complex delete where we delete elements which rely on eachother
435 # in the same transaction.
436 def test_upload_delete
437 changeset = create(:changeset)
438 super_relation = create(:relation)
439 used_relation = create(:relation)
440 used_way = create(:way)
441 used_node = create(:node)
442 create(:relation_member, :relation => super_relation, :member => used_relation)
443 create(:relation_member, :relation => super_relation, :member => used_way)
444 create(:relation_member, :relation => super_relation, :member => used_node)
446 basic_authorization changeset.user.display_name, "test"
448 diff = XML::Document.new
449 diff.root = XML::Node.new "osmChange"
450 delete = XML::Node.new "delete"
452 delete << super_relation.to_xml_node
453 delete << used_relation.to_xml_node
454 delete << used_way.to_xml_node
455 delete << used_node.to_xml_node
457 # update the changeset to one that this user owns
458 %w[node way relation].each do |type|
459 delete.find("//osmChange/delete/#{type}").each do |n|
460 n["changeset"] = changeset.id.to_s
465 post :upload, :params => { :id => changeset.id }, :body => diff.to_s
466 assert_response :success,
467 "can't upload a deletion diff to changeset: #{@response.body}"
469 # check the response is well-formed
470 assert_select "diffResult>node", 1
471 assert_select "diffResult>way", 1
472 assert_select "diffResult>relation", 2
474 # check that everything was deleted
475 assert_equal false, Node.find(used_node.id).visible
476 assert_equal false, Way.find(used_way.id).visible
477 assert_equal false, Relation.find(super_relation.id).visible
478 assert_equal false, Relation.find(used_relation.id).visible
482 # test uploading a delete with no lat/lon, as they are optional in
483 # the osmChange spec.
484 def test_upload_nolatlon_delete
486 changeset = create(:changeset)
488 basic_authorization changeset.user.display_name, "test"
489 diff = "<osmChange><delete><node id='#{node.id}' version='#{node.version}' changeset='#{changeset.id}'/></delete></osmChange>"
492 post :upload, :params => { :id => changeset.id }, :body => diff
493 assert_response :success,
494 "can't upload a deletion diff to changeset: #{@response.body}"
496 # check the response is well-formed
497 assert_select "diffResult>node", 1
499 # check that everything was deleted
500 assert_equal false, Node.find(node.id).visible
503 def test_repeated_changeset_create
505 basic_authorization create(:user).email, "test"
507 # create a temporary changeset
508 xml = "<osm><changeset>" \
509 "<tag k='created_by' v='osm test suite checking changesets'/>" \
511 assert_difference "Changeset.count", 1 do
512 put :create, :body => xml
514 assert_response :success
518 def test_upload_large_changeset
519 basic_authorization create(:user).email, "test"
522 put :create, :body => "<osm><changeset/></osm>"
523 assert_response :success, "Should be able to create a changeset: #{@response.body}"
524 changeset_id = @response.body.to_i
526 # upload some widely-spaced nodes, spiralling positive and negative
527 diff = <<CHANGESET.strip_heredoc
530 <node id='-1' lon='-20' lat='-10' changeset='#{changeset_id}'/>
531 <node id='-10' lon='20' lat='10' changeset='#{changeset_id}'/>
532 <node id='-2' lon='-40' lat='-20' changeset='#{changeset_id}'/>
533 <node id='-11' lon='40' lat='20' changeset='#{changeset_id}'/>
534 <node id='-3' lon='-60' lat='-30' changeset='#{changeset_id}'/>
535 <node id='-12' lon='60' lat='30' changeset='#{changeset_id}'/>
536 <node id='-4' lon='-80' lat='-40' changeset='#{changeset_id}'/>
537 <node id='-13' lon='80' lat='40' changeset='#{changeset_id}'/>
538 <node id='-5' lon='-100' lat='-50' changeset='#{changeset_id}'/>
539 <node id='-14' lon='100' lat='50' changeset='#{changeset_id}'/>
540 <node id='-6' lon='-120' lat='-60' changeset='#{changeset_id}'/>
541 <node id='-15' lon='120' lat='60' changeset='#{changeset_id}'/>
542 <node id='-7' lon='-140' lat='-70' changeset='#{changeset_id}'/>
543 <node id='-16' lon='140' lat='70' changeset='#{changeset_id}'/>
544 <node id='-8' lon='-160' lat='-80' changeset='#{changeset_id}'/>
545 <node id='-17' lon='160' lat='80' changeset='#{changeset_id}'/>
546 <node id='-9' lon='-179.9' lat='-89.9' changeset='#{changeset_id}'/>
547 <node id='-18' lon='179.9' lat='89.9' changeset='#{changeset_id}'/>
552 # upload it, which used to cause an error like "PGError: ERROR:
553 # integer out of range" (bug #2152). but shouldn't any more.
554 post :upload, :params => { :id => changeset_id }, :body => diff
555 assert_response :success,
556 "can't upload a spatially-large diff to changeset: #{@response.body}"
558 # check that the changeset bbox is within bounds
559 cs = Changeset.find(changeset_id)
560 assert cs.min_lon >= -180 * GeoRecord::SCALE, "Minimum longitude (#{cs.min_lon / GeoRecord::SCALE}) should be >= -180 to be valid."
561 assert cs.max_lon <= 180 * GeoRecord::SCALE, "Maximum longitude (#{cs.max_lon / GeoRecord::SCALE}) should be <= 180 to be valid."
562 assert cs.min_lat >= -90 * GeoRecord::SCALE, "Minimum latitude (#{cs.min_lat / GeoRecord::SCALE}) should be >= -90 to be valid."
563 assert cs.max_lat <= 90 * GeoRecord::SCALE, "Maximum latitude (#{cs.max_lat / GeoRecord::SCALE}) should be <= 90 to be valid."
567 # test that deleting stuff in a transaction doesn't bypass the checks
568 # to ensure that used elements are not deleted.
569 def test_upload_delete_invalid
570 changeset = create(:changeset)
571 relation = create(:relation)
572 other_relation = create(:relation)
573 used_way = create(:way)
574 used_node = create(:node)
575 create(:relation_member, :relation => relation, :member => used_way)
576 create(:relation_member, :relation => relation, :member => used_node)
578 basic_authorization changeset.user.email, "test"
580 diff = XML::Document.new
581 diff.root = XML::Node.new "osmChange"
582 delete = XML::Node.new "delete"
584 delete << other_relation.to_xml_node
585 delete << used_way.to_xml_node
586 delete << used_node.to_xml_node
588 # update the changeset to one that this user owns
589 %w[node way relation].each do |type|
590 delete.find("//osmChange/delete/#{type}").each do |n|
591 n["changeset"] = changeset.id.to_s
596 post :upload, :params => { :id => changeset.id }, :body => diff.to_s
597 assert_response :precondition_failed,
598 "shouldn't be able to upload a invalid deletion diff: #{@response.body}"
599 assert_equal "Precondition failed: Way #{used_way.id} is still used by relations #{relation.id}.", @response.body
601 # check that nothing was, in fact, deleted
602 assert_equal true, Node.find(used_node.id).visible
603 assert_equal true, Way.find(used_way.id).visible
604 assert_equal true, Relation.find(relation.id).visible
605 assert_equal true, Relation.find(other_relation.id).visible
609 # test that a conditional delete of an in use object works.
610 def test_upload_delete_if_unused
611 changeset = create(:changeset)
612 super_relation = create(:relation)
613 used_relation = create(:relation)
614 used_way = create(:way)
615 used_node = create(:node)
616 create(:relation_member, :relation => super_relation, :member => used_relation)
617 create(:relation_member, :relation => super_relation, :member => used_way)
618 create(:relation_member, :relation => super_relation, :member => used_node)
620 basic_authorization changeset.user.email, "test"
622 diff = XML::Document.new
623 diff.root = XML::Node.new "osmChange"
624 delete = XML::Node.new "delete"
626 delete["if-unused"] = ""
627 delete << used_relation.to_xml_node
628 delete << used_way.to_xml_node
629 delete << used_node.to_xml_node
631 # update the changeset to one that this user owns
632 %w[node way relation].each do |type|
633 delete.find("//osmChange/delete/#{type}").each do |n|
634 n["changeset"] = changeset.id.to_s
639 post :upload, :params => { :id => changeset.id }, :body => diff.to_s
640 assert_response :success,
641 "can't do a conditional delete of in use objects: #{@response.body}"
643 # check the returned payload
644 assert_select "diffResult[version='#{Settings.api_version}'][generator='OpenStreetMap server']", 1
645 assert_select "diffResult>node", 1
646 assert_select "diffResult>way", 1
647 assert_select "diffResult>relation", 1
650 doc = XML::Parser.string(@response.body).parse
652 # check the old IDs are all present and what we expect
653 assert_equal used_node.id, doc.find("//diffResult/node").first["old_id"].to_i
654 assert_equal used_way.id, doc.find("//diffResult/way").first["old_id"].to_i
655 assert_equal used_relation.id, doc.find("//diffResult/relation").first["old_id"].to_i
657 # check the new IDs are all present and unchanged
658 assert_equal used_node.id, doc.find("//diffResult/node").first["new_id"].to_i
659 assert_equal used_way.id, doc.find("//diffResult/way").first["new_id"].to_i
660 assert_equal used_relation.id, doc.find("//diffResult/relation").first["new_id"].to_i
662 # check the new versions are all present and unchanged
663 assert_equal used_node.version, doc.find("//diffResult/node").first["new_version"].to_i
664 assert_equal used_way.version, doc.find("//diffResult/way").first["new_version"].to_i
665 assert_equal used_relation.version, doc.find("//diffResult/relation").first["new_version"].to_i
667 # check that nothing was, in fact, deleted
668 assert_equal true, Node.find(used_node.id).visible
669 assert_equal true, Way.find(used_way.id).visible
670 assert_equal true, Relation.find(used_relation.id).visible
674 # upload an element with a really long tag value
675 def test_upload_invalid_too_long_tag
676 changeset = create(:changeset)
678 basic_authorization changeset.user.email, "test"
680 # simple diff to create a node way and relation using placeholders
681 diff = <<CHANGESET.strip_heredoc
684 <node id='-1' lon='0' lat='0' changeset='#{changeset.id}'>
685 <tag k='foo' v='#{'x' * 256}'/>
692 post :upload, :params => { :id => changeset.id }, :body => diff
693 assert_response :bad_request,
694 "shoudln't be able to upload too long a tag to changeset: #{@response.body}"
698 # upload something which creates new objects and inserts them into
699 # existing containers using placeholders.
700 def test_upload_complex
703 relation = create(:relation)
704 create(:way_node, :way => way, :node => node)
706 changeset = create(:changeset)
708 basic_authorization changeset.user.email, "test"
710 # simple diff to create a node way and relation using placeholders
711 diff = <<CHANGESET.strip_heredoc
714 <node id='-1' lon='0' lat='0' changeset='#{changeset.id}'>
715 <tag k='foo' v='bar'/>
716 <tag k='baz' v='bat'/>
720 <way id='#{way.id}' changeset='#{changeset.id}' version='1'>
722 <nd ref='#{node.id}'/>
724 <relation id='#{relation.id}' changeset='#{changeset.id}' version='1'>
725 <member type='way' role='some' ref='#{way.id}'/>
726 <member type='node' role='some' ref='-1'/>
727 <member type='relation' role='some' ref='#{relation.id}'/>
734 post :upload, :params => { :id => changeset.id }, :body => diff
735 assert_response :success,
736 "can't upload a complex diff to changeset: #{@response.body}"
738 # check the returned payload
739 assert_select "diffResult[version='#{Settings.api_version}'][generator='#{Settings.generator}']", 1
740 assert_select "diffResult>node", 1
741 assert_select "diffResult>way", 1
742 assert_select "diffResult>relation", 1
744 # inspect the response to find out what the new element IDs are
745 doc = XML::Parser.string(@response.body).parse
746 new_node_id = doc.find("//diffResult/node").first["new_id"].to_i
748 # check that the changes made it into the database
749 assert_equal 2, Node.find(new_node_id).tags.size, "new node should have two tags"
750 assert_equal [new_node_id, node.id], Way.find(way.id).nds, "way nodes should match"
751 Relation.find(relation.id).members.each do |type, id, _role|
752 assert_equal new_node_id, id, "relation should contain new node" if type == "node"
757 # create a diff which references several changesets, which should cause
758 # a rollback and none of the diff gets committed
759 def test_upload_invalid_changesets
760 changeset = create(:changeset)
761 other_changeset = create(:changeset, :user => changeset.user)
764 relation = create(:relation)
765 other_relation = create(:relation)
767 basic_authorization changeset.user.email, "test"
769 # simple diff to create a node way and relation using placeholders
770 diff = <<CHANGESET.strip_heredoc
773 <node id='#{node.id}' lon='0' lat='0' changeset='#{changeset.id}' version='1'/>
774 <way id='#{way.id}' changeset='#{changeset.id}' version='1'>
775 <nd ref='#{node.id}'/>
779 <relation id='#{relation.id}' changeset='#{changeset.id}' version='1'>
780 <member type='way' role='some' ref='#{way.id}'/>
781 <member type='node' role='some' ref='#{node.id}'/>
782 <member type='relation' role='some' ref='#{other_relation.id}'/>
786 <node id='-1' lon='0' lat='0' changeset='#{other_changeset.id}'>
787 <tag k='foo' v='bar'/>
788 <tag k='baz' v='bat'/>
795 post :upload, :params => { :id => changeset.id }, :body => diff
796 assert_response :conflict,
797 "uploading a diff with multiple changesets should have failed"
799 # check that objects are unmodified
800 assert_nodes_are_equal(node, Node.find(node.id))
801 assert_ways_are_equal(way, Way.find(way.id))
802 assert_relations_are_equal(relation, Relation.find(relation.id))
806 # upload multiple versions of the same element in the same diff.
807 def test_upload_multiple_valid
809 changeset = create(:changeset)
810 basic_authorization changeset.user.email, "test"
812 # change the location of a node multiple times, each time referencing
813 # the last version. doesn't this depend on version numbers being
815 diff = <<CHANGESET.strip_heredoc
818 <node id='#{node.id}' lon='0' lat='0' changeset='#{changeset.id}' version='1'/>
819 <node id='#{node.id}' lon='1' lat='0' changeset='#{changeset.id}' version='2'/>
820 <node id='#{node.id}' lon='1' lat='1' changeset='#{changeset.id}' version='3'/>
821 <node id='#{node.id}' lon='1' lat='2' changeset='#{changeset.id}' version='4'/>
822 <node id='#{node.id}' lon='2' lat='2' changeset='#{changeset.id}' version='5'/>
823 <node id='#{node.id}' lon='3' lat='2' changeset='#{changeset.id}' version='6'/>
824 <node id='#{node.id}' lon='3' lat='3' changeset='#{changeset.id}' version='7'/>
825 <node id='#{node.id}' lon='9' lat='9' changeset='#{changeset.id}' version='8'/>
831 post :upload, :params => { :id => changeset.id }, :body => diff
832 assert_response :success,
833 "can't upload multiple versions of an element in a diff: #{@response.body}"
835 # check the response is well-formed. its counter-intuitive, but the
836 # API will return multiple elements with the same ID and different
837 # version numbers for each change we made.
838 assert_select "diffResult>node", 8
842 # upload multiple versions of the same element in the same diff, but
843 # keep the version numbers the same.
844 def test_upload_multiple_duplicate
846 changeset = create(:changeset)
848 basic_authorization changeset.user.email, "test"
850 diff = <<CHANGESET.strip_heredoc
853 <node id='#{node.id}' lon='0' lat='0' changeset='#{changeset.id}' version='1'/>
854 <node id='#{node.id}' lon='1' lat='1' changeset='#{changeset.id}' version='1'/>
860 post :upload, :params => { :id => changeset.id }, :body => diff
861 assert_response :conflict,
862 "shouldn't be able to upload the same element twice in a diff: #{@response.body}"
866 # try to upload some elements without specifying the version
867 def test_upload_missing_version
868 changeset = create(:changeset)
870 basic_authorization changeset.user.email, "test"
872 diff = <<CHANGESET.strip_heredoc
875 <node id='1' lon='1' lat='1' changeset='#{changeset.id}'/>
881 post :upload, :params => { :id => changeset.id }, :body => diff
882 assert_response :bad_request,
883 "shouldn't be able to upload an element without version: #{@response.body}"
887 # try to upload with commands other than create, modify, or delete
888 def test_action_upload_invalid
889 changeset = create(:changeset)
891 basic_authorization changeset.user.email, "test"
893 diff = <<CHANGESET.strip_heredoc
896 <node id='1' lon='1' lat='1' changeset='#{changeset.id}' />
900 post :upload, :params => { :id => changeset.id }, :body => diff
901 assert_response :bad_request, "Shouldn't be able to upload a diff with the action ping"
902 assert_equal @response.body, "Unknown action ping, choices are create, modify, delete"
906 # upload a valid changeset which has a mixture of whitespace
907 # to check a bug reported by ivansanchez (#1565).
908 def test_upload_whitespace_valid
909 changeset = create(:changeset)
911 way = create(:way_with_nodes, :nodes_count => 2)
912 relation = create(:relation)
913 other_relation = create(:relation)
914 create(:relation_tag, :relation => relation)
916 basic_authorization changeset.user.email, "test"
918 diff = <<CHANGESET.strip_heredoc
920 <modify><node id='#{node.id}' lon='0' lat='0' changeset='#{changeset.id}'
922 <node id='#{node.id}' lon='1' lat='1' changeset='#{changeset.id}' version='2'><tag k='k' v='v'/></node></modify>
924 <relation id='#{relation.id}' changeset='#{changeset.id}' version='1'><member
925 type='way' role='some' ref='#{way.id}'/><member
926 type='node' role='some' ref='#{node.id}'/>
927 <member type='relation' role='some' ref='#{other_relation.id}'/>
929 </modify></osmChange>
933 post :upload, :params => { :id => changeset.id }, :body => diff
934 assert_response :success,
935 "can't upload a valid diff with whitespace variations to changeset: #{@response.body}"
937 # check the response is well-formed
938 assert_select "diffResult>node", 2
939 assert_select "diffResult>relation", 1
941 # check that the changes made it into the database
942 assert_equal 1, Node.find(node.id).tags.size, "node #{node.id} should now have one tag"
943 assert_equal 0, Relation.find(relation.id).tags.size, "relation #{relation.id} should now have no tags"
947 # test that a placeholder can be reused within the same upload.
948 def test_upload_reuse_placeholder_valid
949 changeset = create(:changeset)
951 basic_authorization changeset.user.email, "test"
953 diff = <<CHANGESET.strip_heredoc
956 <node id='-1' lon='0' lat='0' changeset='#{changeset.id}'>
957 <tag k="foo" v="bar"/>
961 <node id='-1' lon='1' lat='1' changeset='#{changeset.id}' version='1'/>
964 <node id='-1' lon='2' lat='2' changeset='#{changeset.id}' version='2'/>
970 post :upload, :params => { :id => changeset.id }, :body => diff
971 assert_response :success,
972 "can't upload a valid diff with re-used placeholders to changeset: #{@response.body}"
974 # check the response is well-formed
975 assert_select "diffResult>node", 3
976 assert_select "diffResult>node[old_id='-1']", 3
980 # test what happens if a diff upload re-uses placeholder IDs in an
982 def test_upload_placeholder_invalid
983 changeset = create(:changeset)
985 basic_authorization changeset.user.email, "test"
987 diff = <<CHANGESET.strip_heredoc
990 <node id='-1' lon='0' lat='0' changeset='#{changeset.id}' version='1'/>
991 <node id='-1' lon='1' lat='1' changeset='#{changeset.id}' version='1'/>
992 <node id='-1' lon='2' lat='2' changeset='#{changeset.id}' version='2'/>
998 post :upload, :params => { :id => changeset.id }, :body => diff
999 assert_response :bad_request,
1000 "shouldn't be able to re-use placeholder IDs"
1004 # test that uploading a way referencing invalid placeholders gives a
1005 # proper error, not a 500.
1006 def test_upload_placeholder_invalid_way
1007 changeset = create(:changeset)
1010 basic_authorization changeset.user.email, "test"
1012 diff = <<CHANGESET.strip_heredoc
1015 <node id="-1" lon="0" lat="0" changeset="#{changeset.id}" version="1"/>
1016 <node id="-2" lon="1" lat="1" changeset="#{changeset.id}" version="1"/>
1017 <node id="-3" lon="2" lat="2" changeset="#{changeset.id}" version="1"/>
1018 <way id="-1" changeset="#{changeset.id}" version="1">
1029 post :upload, :params => { :id => changeset.id }, :body => diff
1030 assert_response :bad_request,
1031 "shouldn't be able to use invalid placeholder IDs"
1032 assert_equal "Placeholder node not found for reference -4 in way -1", @response.body
1034 # the same again, but this time use an existing way
1035 diff = <<CHANGESET.strip_heredoc
1038 <node id="-1" lon="0" lat="0" changeset="#{changeset.id}" version="1"/>
1039 <node id="-2" lon="1" lat="1" changeset="#{changeset.id}" version="1"/>
1040 <node id="-3" lon="2" lat="2" changeset="#{changeset.id}" version="1"/>
1041 <way id="#{way.id}" changeset="#{changeset.id}" version="1">
1052 post :upload, :params => { :id => changeset.id }, :body => diff
1053 assert_response :bad_request,
1054 "shouldn't be able to use invalid placeholder IDs"
1055 assert_equal "Placeholder node not found for reference -4 in way #{way.id}", @response.body
1059 # test that uploading a relation referencing invalid placeholders gives a
1060 # proper error, not a 500.
1061 def test_upload_placeholder_invalid_relation
1062 changeset = create(:changeset)
1063 relation = create(:relation)
1065 basic_authorization changeset.user.email, "test"
1067 diff = <<CHANGESET.strip_heredoc
1070 <node id="-1" lon="0" lat="0" changeset="#{changeset.id}" version="1"/>
1071 <node id="-2" lon="1" lat="1" changeset="#{changeset.id}" version="1"/>
1072 <node id="-3" lon="2" lat="2" changeset="#{changeset.id}" version="1"/>
1073 <relation id="-1" changeset="#{changeset.id}" version="1">
1074 <member type="node" role="foo" ref="-1"/>
1075 <member type="node" role="foo" ref="-2"/>
1076 <member type="node" role="foo" ref="-3"/>
1077 <member type="node" role="foo" ref="-4"/>
1084 post :upload, :params => { :id => changeset.id }, :body => diff
1085 assert_response :bad_request,
1086 "shouldn't be able to use invalid placeholder IDs"
1087 assert_equal "Placeholder Node not found for reference -4 in relation -1.", @response.body
1089 # the same again, but this time use an existing relation
1090 diff = <<CHANGESET.strip_heredoc
1093 <node id="-1" lon="0" lat="0" changeset="#{changeset.id}" version="1"/>
1094 <node id="-2" lon="1" lat="1" changeset="#{changeset.id}" version="1"/>
1095 <node id="-3" lon="2" lat="2" changeset="#{changeset.id}" version="1"/>
1096 <relation id="#{relation.id}" changeset="#{changeset.id}" version="1">
1097 <member type="node" role="foo" ref="-1"/>
1098 <member type="node" role="foo" ref="-2"/>
1099 <member type="node" role="foo" ref="-3"/>
1100 <member type="way" role="bar" ref="-1"/>
1107 post :upload, :params => { :id => changeset.id }, :body => diff
1108 assert_response :bad_request,
1109 "shouldn't be able to use invalid placeholder IDs"
1110 assert_equal "Placeholder Way not found for reference -1 in relation #{relation.id}.", @response.body
1114 # test what happens if a diff is uploaded containing only a node
1116 def test_upload_node_move
1117 basic_authorization create(:user).email, "test"
1119 xml = "<osm><changeset>" \
1120 "<tag k='created_by' v='osm test suite checking changesets'/>" \
1121 "</changeset></osm>"
1122 put :create, :body => xml
1123 assert_response :success
1124 changeset_id = @response.body.to_i
1126 old_node = create(:node, :lat => 1, :lon => 1)
1128 diff = XML::Document.new
1129 diff.root = XML::Node.new "osmChange"
1130 modify = XML::Node.new "modify"
1131 xml_old_node = old_node.to_xml_node
1132 xml_old_node["lat"] = 2.0.to_s
1133 xml_old_node["lon"] = 2.0.to_s
1134 xml_old_node["changeset"] = changeset_id.to_s
1135 modify << xml_old_node
1139 post :upload, :params => { :id => changeset_id }, :body => diff.to_s
1140 assert_response :success,
1141 "diff should have uploaded OK"
1144 changeset = Changeset.find(changeset_id)
1145 assert_equal 1 * GeoRecord::SCALE, changeset.min_lon, "min_lon should be 1 degree"
1146 assert_equal 2 * GeoRecord::SCALE, changeset.max_lon, "max_lon should be 2 degrees"
1147 assert_equal 1 * GeoRecord::SCALE, changeset.min_lat, "min_lat should be 1 degree"
1148 assert_equal 2 * GeoRecord::SCALE, changeset.max_lat, "max_lat should be 2 degrees"
1152 # test what happens if a diff is uploaded adding a node to a way.
1153 def test_upload_way_extend
1154 basic_authorization create(:user).email, "test"
1156 xml = "<osm><changeset>" \
1157 "<tag k='created_by' v='osm test suite checking changesets'/>" \
1158 "</changeset></osm>"
1159 put :create, :body => xml
1160 assert_response :success
1161 changeset_id = @response.body.to_i
1163 old_way = create(:way)
1164 create(:way_node, :way => old_way, :node => create(:node, :lat => 1, :lon => 1))
1166 diff = XML::Document.new
1167 diff.root = XML::Node.new "osmChange"
1168 modify = XML::Node.new "modify"
1169 xml_old_way = old_way.to_xml_node
1170 nd_ref = XML::Node.new "nd"
1171 nd_ref["ref"] = create(:node, :lat => 3, :lon => 3).id.to_s
1172 xml_old_way << nd_ref
1173 xml_old_way["changeset"] = changeset_id.to_s
1174 modify << xml_old_way
1178 post :upload, :params => { :id => changeset_id }, :body => diff.to_s
1179 assert_response :success,
1180 "diff should have uploaded OK"
1183 changeset = Changeset.find(changeset_id)
1184 assert_equal 1 * GeoRecord::SCALE, changeset.min_lon, "min_lon should be 1 degree"
1185 assert_equal 3 * GeoRecord::SCALE, changeset.max_lon, "max_lon should be 3 degrees"
1186 assert_equal 1 * GeoRecord::SCALE, changeset.min_lat, "min_lat should be 1 degree"
1187 assert_equal 3 * GeoRecord::SCALE, changeset.max_lat, "max_lat should be 3 degrees"
1191 # test for more issues in #1568
1192 def test_upload_empty_invalid
1193 changeset = create(:changeset)
1195 basic_authorization changeset.user.email, "test"
1198 "<osmChange></osmChange>",
1199 "<osmChange><modify/></osmChange>",
1200 "<osmChange><modify></modify></osmChange>"].each do |diff|
1202 post :upload, :params => { :id => changeset.id }, :body => diff
1203 assert_response(:success, "should be able to upload " \
1204 "empty changeset: " + diff)
1209 # test that the X-Error-Format header works to request XML errors
1210 def test_upload_xml_errors
1211 changeset = create(:changeset)
1212 node = create(:node)
1213 create(:relation_member, :member => node)
1215 basic_authorization changeset.user.email, "test"
1217 # try and delete a node that is in use
1218 diff = XML::Document.new
1219 diff.root = XML::Node.new "osmChange"
1220 delete = XML::Node.new "delete"
1222 delete << node.to_xml_node
1226 post :upload, :params => { :id => changeset.id }, :body => diff.to_s
1227 assert_response :success,
1228 "failed to return error in XML format"
1230 # check the returned payload
1231 assert_select "osmError[version='#{Settings.api_version}'][generator='OpenStreetMap server']", 1
1232 assert_select "osmError>status", 1
1233 assert_select "osmError>message", 1
1237 # when we make some simple changes we get the same changes back from the
1239 def test_diff_download_simple
1240 node = create(:node)
1242 ## First try with a non-public user, which should get a forbidden
1243 basic_authorization create(:user, :data_public => false).email, "test"
1245 # create a temporary changeset
1246 xml = "<osm><changeset>" \
1247 "<tag k='created_by' v='osm test suite checking changesets'/>" \
1248 "</changeset></osm>"
1249 put :create, :body => xml
1250 assert_response :forbidden
1252 ## Now try with a normal user
1253 basic_authorization create(:user).email, "test"
1255 # create a temporary changeset
1256 xml = "<osm><changeset>" \
1257 "<tag k='created_by' v='osm test suite checking changesets'/>" \
1258 "</changeset></osm>"
1259 put :create, :body => xml
1260 assert_response :success
1261 changeset_id = @response.body.to_i
1264 diff = <<CHANGESET.strip_heredoc
1267 <node id='#{node.id}' lon='0' lat='0' changeset='#{changeset_id}' version='1'/>
1268 <node id='#{node.id}' lon='1' lat='0' changeset='#{changeset_id}' version='2'/>
1269 <node id='#{node.id}' lon='1' lat='1' changeset='#{changeset_id}' version='3'/>
1270 <node id='#{node.id}' lon='1' lat='2' changeset='#{changeset_id}' version='4'/>
1271 <node id='#{node.id}' lon='2' lat='2' changeset='#{changeset_id}' version='5'/>
1272 <node id='#{node.id}' lon='3' lat='2' changeset='#{changeset_id}' version='6'/>
1273 <node id='#{node.id}' lon='3' lat='3' changeset='#{changeset_id}' version='7'/>
1274 <node id='#{node.id}' lon='9' lat='9' changeset='#{changeset_id}' version='8'/>
1280 post :upload, :params => { :id => changeset_id }, :body => diff
1281 assert_response :success,
1282 "can't upload multiple versions of an element in a diff: #{@response.body}"
1284 get :download, :params => { :id => changeset_id }
1285 assert_response :success
1287 assert_select "osmChange", 1
1288 assert_select "osmChange>modify", 8
1289 assert_select "osmChange>modify>node", 8
1293 # culled this from josm to ensure that nothing in the way that josm
1294 # is formatting the request is causing it to fail.
1296 # NOTE: the error turned out to be something else completely!
1297 def test_josm_upload
1298 basic_authorization create(:user).email, "test"
1300 # create a temporary changeset
1301 xml = "<osm><changeset>" \
1302 "<tag k='created_by' v='osm test suite checking changesets'/>" \
1303 "</changeset></osm>"
1304 put :create, :body => xml
1305 assert_response :success
1306 changeset_id = @response.body.to_i
1308 diff = <<OSMFILE.strip_heredoc
1309 <osmChange version="0.6" generator="JOSM">
1310 <create version="0.6" generator="JOSM">
1311 <node id='-1' visible='true' changeset='#{changeset_id}' lat='51.49619982187321' lon='-0.18722061869438314' />
1312 <node id='-2' visible='true' changeset='#{changeset_id}' lat='51.496359883909605' lon='-0.18653093576241928' />
1313 <node id='-3' visible='true' changeset='#{changeset_id}' lat='51.49598132358285' lon='-0.18719613290981638' />
1314 <node id='-4' visible='true' changeset='#{changeset_id}' lat='51.4961591711078' lon='-0.18629015888084607' />
1315 <node id='-5' visible='true' changeset='#{changeset_id}' lat='51.49582126021711' lon='-0.18708186591517145' />
1316 <node id='-6' visible='true' changeset='#{changeset_id}' lat='51.49591018437858' lon='-0.1861432441734455' />
1317 <node id='-7' visible='true' changeset='#{changeset_id}' lat='51.49560784152179' lon='-0.18694719410005425' />
1318 <node id='-8' visible='true' changeset='#{changeset_id}' lat='51.49567389979617' lon='-0.1860289771788006' />
1319 <node id='-9' visible='true' changeset='#{changeset_id}' lat='51.49543761398892' lon='-0.186820684213126' />
1320 <way id='-10' action='modiy' visible='true' changeset='#{changeset_id}'>
1330 <tag k='highway' v='residential' />
1331 <tag k='name' v='Foobar Street' />
1338 post :upload, :params => { :id => changeset_id }, :body => diff
1339 assert_response :success,
1340 "can't upload a diff from JOSM: #{@response.body}"
1342 get :download, :params => { :id => changeset_id }
1343 assert_response :success
1345 assert_select "osmChange", 1
1346 assert_select "osmChange>create>node", 9
1347 assert_select "osmChange>create>way", 1
1348 assert_select "osmChange>create>way>nd", 9
1349 assert_select "osmChange>create>way>tag", 2
1353 # when we make some complex changes we get the same changes back from the
1355 def test_diff_download_complex
1356 node = create(:node)
1357 node2 = create(:node)
1359 basic_authorization create(:user).email, "test"
1361 # create a temporary changeset
1362 xml = "<osm><changeset>" \
1363 "<tag k='created_by' v='osm test suite checking changesets'/>" \
1364 "</changeset></osm>"
1365 put :create, :body => xml
1366 assert_response :success
1367 changeset_id = @response.body.to_i
1370 diff = <<CHANGESET.strip_heredoc
1373 <node id='#{node.id}' lon='0' lat='0' changeset='#{changeset_id}' version='1'/>
1376 <node id='-1' lon='9' lat='9' changeset='#{changeset_id}' version='0'/>
1377 <node id='-2' lon='8' lat='9' changeset='#{changeset_id}' version='0'/>
1378 <node id='-3' lon='7' lat='9' changeset='#{changeset_id}' version='0'/>
1381 <node id='#{node2.id}' lon='20' lat='15' changeset='#{changeset_id}' version='1'/>
1382 <way id='#{way.id}' changeset='#{changeset_id}' version='1'>
1383 <nd ref='#{node2.id}'/>
1393 post :upload, :params => { :id => changeset_id }, :body => diff
1394 assert_response :success,
1395 "can't upload multiple versions of an element in a diff: #{@response.body}"
1397 get :download, :params => { :id => changeset_id }
1398 assert_response :success
1400 assert_select "osmChange", 1
1401 assert_select "osmChange>create", 3
1402 assert_select "osmChange>delete", 1
1403 assert_select "osmChange>modify", 2
1404 assert_select "osmChange>create>node", 3
1405 assert_select "osmChange>delete>node", 1
1406 assert_select "osmChange>modify>node", 1
1407 assert_select "osmChange>modify>way", 1
1410 def test_changeset_download
1411 changeset = create(:changeset)
1412 node = create(:node, :with_history, :version => 1, :changeset => changeset)
1413 tag = create(:old_node_tag, :old_node => node.old_nodes.find_by(:version => 1))
1414 node2 = create(:node, :with_history, :version => 1, :changeset => changeset)
1415 _node3 = create(:node, :with_history, :deleted, :version => 1, :changeset => changeset)
1416 _relation = create(:relation, :with_history, :version => 1, :changeset => changeset)
1417 _relation2 = create(:relation, :with_history, :deleted, :version => 1, :changeset => changeset)
1419 get :download, :params => { :id => changeset.id }
1421 assert_response :success
1423 # print @response.body
1424 # FIXME: needs more assert_select tests
1425 assert_select "osmChange[version='#{Settings.api_version}'][generator='#{Settings.generator}']" do
1426 assert_select "create", :count => 5
1427 assert_select "create>node[id='#{node.id}'][visible='#{node.visible?}'][version='#{node.version}']" do
1428 assert_select "tag[k='#{tag.k}'][v='#{tag.v}']"
1430 assert_select "create>node[id='#{node2.id}']"
1435 # check that the bounding box of a changeset gets updated correctly
1436 # FIXME: This should really be moded to a integration test due to the with_controller
1437 def test_changeset_bbox
1439 create(:way_node, :way => way, :node => create(:node, :lat => 3, :lon => 3))
1441 basic_authorization create(:user).email, "test"
1443 # create a new changeset
1444 xml = "<osm><changeset/></osm>"
1445 put :create, :body => xml
1446 assert_response :success, "Creating of changeset failed."
1447 changeset_id = @response.body.to_i
1449 # add a single node to it
1450 with_controller(NodesController.new) do
1451 xml = "<osm><node lon='1' lat='2' changeset='#{changeset_id}'/></osm>"
1452 put :create, :body => xml
1453 assert_response :success, "Couldn't create node."
1456 # get the bounding box back from the changeset
1457 get :show, :params => { :id => changeset_id }
1458 assert_response :success, "Couldn't read back changeset."
1459 assert_select "osm>changeset[min_lon='1.0000000']", 1
1460 assert_select "osm>changeset[max_lon='1.0000000']", 1
1461 assert_select "osm>changeset[min_lat='2.0000000']", 1
1462 assert_select "osm>changeset[max_lat='2.0000000']", 1
1464 # add another node to it
1465 with_controller(NodesController.new) do
1466 xml = "<osm><node lon='2' lat='1' changeset='#{changeset_id}'/></osm>"
1467 put :create, :body => xml
1468 assert_response :success, "Couldn't create second node."
1471 # get the bounding box back from the changeset
1472 get :show, :params => { :id => changeset_id }
1473 assert_response :success, "Couldn't read back changeset for the second time."
1474 assert_select "osm>changeset[min_lon='1.0000000']", 1
1475 assert_select "osm>changeset[max_lon='2.0000000']", 1
1476 assert_select "osm>changeset[min_lat='1.0000000']", 1
1477 assert_select "osm>changeset[max_lat='2.0000000']", 1
1479 # add (delete) a way to it, which contains a point at (3,3)
1480 with_controller(WaysController.new) do
1481 xml = update_changeset(way.to_xml, changeset_id)
1482 put :delete, :params => { :id => way.id }, :body => xml.to_s
1483 assert_response :success, "Couldn't delete a way."
1486 # get the bounding box back from the changeset
1487 get :show, :params => { :id => changeset_id }
1488 assert_response :success, "Couldn't read back changeset for the third time."
1489 assert_select "osm>changeset[min_lon='1.0000000']", 1
1490 assert_select "osm>changeset[max_lon='3.0000000']", 1
1491 assert_select "osm>changeset[min_lat='1.0000000']", 1
1492 assert_select "osm>changeset[max_lat='3.0000000']", 1
1496 # test that the changeset :include method works as it should
1497 def test_changeset_include
1498 basic_authorization create(:user).display_name, "test"
1500 # create a new changeset
1501 put :create, :body => "<osm><changeset/></osm>"
1502 assert_response :success, "Creating of changeset failed."
1503 changeset_id = @response.body.to_i
1505 # NOTE: the include method doesn't over-expand, like inserting
1506 # a real method does. this is because we expect the client to
1507 # know what it is doing!
1508 check_after_include(changeset_id, 1, 1, [1, 1, 1, 1])
1509 check_after_include(changeset_id, 3, 3, [1, 1, 3, 3])
1510 check_after_include(changeset_id, 4, 2, [1, 1, 4, 3])
1511 check_after_include(changeset_id, 2, 2, [1, 1, 4, 3])
1512 check_after_include(changeset_id, -1, -1, [-1, -1, 4, 3])
1513 check_after_include(changeset_id, -2, 5, [-2, -1, 4, 5])
1517 # test that a not found, wrong method with the expand bbox works as expected
1518 def test_changeset_expand_bbox_error
1519 basic_authorization create(:user).display_name, "test"
1521 # create a new changeset
1522 xml = "<osm><changeset/></osm>"
1523 put :create, :body => xml
1524 assert_response :success, "Creating of changeset failed."
1525 changeset_id = @response.body.to_i
1531 xml = "<osm><node lon='#{lon}' lat='#{lat}'/></osm>"
1532 put :expand_bbox, :params => { :id => changeset_id }, :body => xml
1533 assert_response :method_not_allowed, "shouldn't be able to put a bbox expand"
1535 # Try to get the update
1536 xml = "<osm><node lon='#{lon}' lat='#{lat}'/></osm>"
1537 get :expand_bbox, :params => { :id => changeset_id }, :body => xml
1538 assert_response :method_not_allowed, "shouldn't be able to get a bbox expand"
1540 # Try to use a hopefully missing changeset
1541 xml = "<osm><node lon='#{lon}' lat='#{lat}'/></osm>"
1542 post :expand_bbox, :params => { :id => changeset_id + 13245 }, :body => xml
1543 assert_response :not_found, "shouldn't be able to do a bbox expand on a nonexistant changeset"
1547 # test the query functionality of changesets
1549 private_user = create(:user, :data_public => false)
1550 private_user_changeset = create(:changeset, :user => private_user)
1551 private_user_closed_changeset = create(:changeset, :closed, :user => private_user)
1552 user = create(:user)
1553 changeset = create(:changeset, :user => user)
1554 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))
1555 changeset2 = create(:changeset, :min_lat => 5 * GeoRecord::SCALE, :min_lon => 5 * GeoRecord::SCALE, :max_lat => 15 * GeoRecord::SCALE, :max_lon => 15 * GeoRecord::SCALE)
1556 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)
1558 get :query, :params => { :bbox => "-10,-10, 10, 10" }
1559 assert_response :success, "can't get changesets in bbox"
1560 assert_changesets [changeset2, changeset3]
1562 get :query, :params => { :bbox => "4.5,4.5,4.6,4.6" }
1563 assert_response :success, "can't get changesets in bbox"
1564 assert_changesets [changeset3]
1566 # not found when looking for changesets of non-existing users
1567 get :query, :params => { :user => User.maximum(:id) + 1 }
1568 assert_response :not_found
1569 get :query, :params => { :display_name => " " }
1570 assert_response :not_found
1572 # can't get changesets of user 1 without authenticating
1573 get :query, :params => { :user => private_user.id }
1574 assert_response :not_found, "shouldn't be able to get changesets by non-public user (ID)"
1575 get :query, :params => { :display_name => private_user.display_name }
1576 assert_response :not_found, "shouldn't be able to get changesets by non-public user (name)"
1578 # but this should work
1579 basic_authorization private_user.email, "test"
1580 get :query, :params => { :user => private_user.id }
1581 assert_response :success, "can't get changesets by user ID"
1582 assert_changesets [private_user_changeset, private_user_closed_changeset]
1584 get :query, :params => { :display_name => private_user.display_name }
1585 assert_response :success, "can't get changesets by user name"
1586 assert_changesets [private_user_changeset, private_user_closed_changeset]
1588 # check that the correct error is given when we provide both UID and name
1589 get :query, :params => { :user => private_user.id,
1590 :display_name => private_user.display_name }
1591 assert_response :bad_request, "should be a bad request to have both ID and name specified"
1593 get :query, :params => { :user => private_user.id, :open => true }
1594 assert_response :success, "can't get changesets by user and open"
1595 assert_changesets [private_user_changeset]
1597 get :query, :params => { :time => "2007-12-31" }
1598 assert_response :success, "can't get changesets by time-since"
1599 assert_changesets [private_user_changeset, private_user_closed_changeset, changeset, closed_changeset, changeset2, changeset3]
1601 get :query, :params => { :time => "2008-01-01T12:34Z" }
1602 assert_response :success, "can't get changesets by time-since with hour"
1603 assert_changesets [private_user_changeset, private_user_closed_changeset, changeset, closed_changeset, changeset2, changeset3]
1605 get :query, :params => { :time => "2007-12-31T23:59Z,2008-01-02T00:01Z" }
1606 assert_response :success, "can't get changesets by time-range"
1607 assert_changesets [closed_changeset]
1609 get :query, :params => { :open => "true" }
1610 assert_response :success, "can't get changesets by open-ness"
1611 assert_changesets [private_user_changeset, changeset, changeset2, changeset3]
1613 get :query, :params => { :closed => "true" }
1614 assert_response :success, "can't get changesets by closed-ness"
1615 assert_changesets [private_user_closed_changeset, closed_changeset]
1617 get :query, :params => { :closed => "true", :user => private_user.id }
1618 assert_response :success, "can't get changesets by closed-ness and user"
1619 assert_changesets [private_user_closed_changeset]
1621 get :query, :params => { :closed => "true", :user => user.id }
1622 assert_response :success, "can't get changesets by closed-ness and user"
1623 assert_changesets [closed_changeset]
1625 get :query, :params => { :changesets => "#{private_user_changeset.id},#{changeset.id},#{closed_changeset.id}" }
1626 assert_response :success, "can't get changesets by id (as comma-separated string)"
1627 assert_changesets [private_user_changeset, changeset, closed_changeset]
1629 get :query, :params => { :changesets => "" }
1630 assert_response :bad_request, "should be a bad request since changesets is empty"
1634 # check that errors are returned if garbage is inserted
1635 # into query strings
1636 def test_query_invalid
1639 ";drop table users;"].each do |bbox|
1640 get :query, :params => { :bbox => bbox }
1641 assert_response :bad_request, "'#{bbox}' isn't a bbox"
1646 ";drop table users;",
1648 "-,-"].each do |time|
1649 get :query, :params => { :time => time }
1650 assert_response :bad_request, "'#{time}' isn't a valid time range"
1657 get :query, :params => { :user => uid }
1658 assert_response :bad_request, "'#{uid}' isn't a valid user ID"
1663 # check updating tags on a changeset
1664 def test_changeset_update
1665 private_user = create(:user, :data_public => false)
1666 private_changeset = create(:changeset, :user => private_user)
1667 user = create(:user)
1668 changeset = create(:changeset, :user => user)
1670 ## First try with a non-public user
1671 new_changeset = create_changeset_xml(:user => private_user)
1672 new_tag = XML::Node.new "tag"
1673 new_tag["k"] = "tagtesting"
1674 new_tag["v"] = "valuetesting"
1675 new_changeset.find("//osm/changeset").first << new_tag
1677 # try without any authorization
1678 put :update, :params => { :id => private_changeset.id }, :body => new_changeset.to_s
1679 assert_response :unauthorized
1681 # try with the wrong authorization
1682 basic_authorization create(:user).email, "test"
1683 put :update, :params => { :id => private_changeset.id }, :body => new_changeset.to_s
1684 assert_response :conflict
1686 # now this should get an unauthorized
1687 basic_authorization private_user.email, "test"
1688 put :update, :params => { :id => private_changeset.id }, :body => new_changeset.to_s
1689 assert_require_public_data "user with their data non-public, shouldn't be able to edit their changeset"
1691 ## Now try with the public user
1692 new_changeset = create_changeset_xml(:id => 1)
1693 new_tag = XML::Node.new "tag"
1694 new_tag["k"] = "tagtesting"
1695 new_tag["v"] = "valuetesting"
1696 new_changeset.find("//osm/changeset").first << new_tag
1698 # try without any authorization
1699 @request.env["HTTP_AUTHORIZATION"] = nil
1700 put :update, :params => { :id => changeset.id }, :body => new_changeset.to_s
1701 assert_response :unauthorized
1703 # try with the wrong authorization
1704 basic_authorization create(:user).email, "test"
1705 put :update, :params => { :id => changeset.id }, :body => new_changeset.to_s
1706 assert_response :conflict
1708 # now this should work...
1709 basic_authorization user.email, "test"
1710 put :update, :params => { :id => changeset.id }, :body => new_changeset.to_s
1711 assert_response :success
1713 assert_select "osm>changeset[id='#{changeset.id}']", 1
1714 assert_select "osm>changeset>tag", 1
1715 assert_select "osm>changeset>tag[k='tagtesting'][v='valuetesting']", 1
1719 # check that a user different from the one who opened the changeset
1721 def test_changeset_update_invalid
1722 basic_authorization create(:user).email, "test"
1724 changeset = create(:changeset)
1725 new_changeset = create_changeset_xml(:user => changeset.user, :id => changeset.id)
1726 new_tag = XML::Node.new "tag"
1727 new_tag["k"] = "testing"
1728 new_tag["v"] = "testing"
1729 new_changeset.find("//osm/changeset").first << new_tag
1731 put :update, :params => { :id => changeset.id }, :body => new_changeset.to_s
1732 assert_response :conflict
1736 # check that a changeset can contain a certain max number of changes.
1737 ## FIXME should be changed to an integration test due to the with_controller
1738 def test_changeset_limits
1739 basic_authorization create(:user).email, "test"
1741 # open a new changeset
1742 xml = "<osm><changeset/></osm>"
1743 put :create, :body => xml
1744 assert_response :success, "can't create a new changeset"
1745 cs_id = @response.body.to_i
1747 # start the counter just short of where the changeset should finish.
1749 # alter the database to set the counter on the changeset directly,
1750 # otherwise it takes about 6 minutes to fill all of them.
1751 changeset = Changeset.find(cs_id)
1752 changeset.num_changes = Changeset::MAX_ELEMENTS - offset
1755 with_controller(NodesController.new) do
1757 xml = "<osm><node changeset='#{cs_id}' lat='0.0' lon='0.0'/></osm>"
1758 put :create, :body => xml
1759 assert_response :success, "can't create a new node"
1760 node_id = @response.body.to_i
1762 get :show, :params => { :id => node_id }
1763 assert_response :success, "can't read back new node"
1764 node_doc = XML::Parser.string(@response.body).parse
1765 node_xml = node_doc.find("//osm/node").first
1767 # loop until we fill the changeset with nodes
1769 node_xml["lat"] = rand.to_s
1770 node_xml["lon"] = rand.to_s
1771 node_xml["version"] = (i + 1).to_s
1773 put :update, :params => { :id => node_id }, :body => node_doc.to_s
1774 assert_response :success, "attempt #{i} should have succeeded"
1777 # trying again should fail
1778 node_xml["lat"] = rand.to_s
1779 node_xml["lon"] = rand.to_s
1780 node_xml["version"] = offset.to_s
1782 put :update, :params => { :id => node_id }, :body => node_doc.to_s
1783 assert_response :conflict, "final attempt should have failed"
1786 changeset = Changeset.find(cs_id)
1787 assert_equal Changeset::MAX_ELEMENTS + 1, changeset.num_changes
1789 # check that the changeset is now closed as well
1790 assert_not(changeset.is_open?,
1791 "changeset should have been auto-closed by exceeding " \
1796 # check that the changeset download for a changeset with a redacted
1797 # element in it doesn't contain that element.
1798 def test_diff_download_redacted
1799 changeset = create(:changeset)
1800 node = create(:node, :with_history, :version => 2, :changeset => changeset)
1801 node_v1 = node.old_nodes.find_by(:version => 1)
1802 node_v1.redact!(create(:redaction))
1804 get :download, :params => { :id => changeset.id }
1805 assert_response :success
1807 assert_select "osmChange", 1
1808 # this changeset contains the node in versions 1 & 2, but 1 should
1810 assert_select "osmChange node[id='#{node.id}']", 1
1811 assert_select "osmChange node[id='#{node.id}'][version='1']", 0
1815 # test subscribe success
1816 def test_subscribe_success
1817 basic_authorization create(:user).email, "test"
1818 changeset = create(:changeset, :closed)
1820 assert_difference "changeset.subscribers.count", 1 do
1821 post :subscribe, :params => { :id => changeset.id }
1823 assert_response :success
1825 # not closed changeset
1826 changeset = create(:changeset)
1827 assert_difference "changeset.subscribers.count", 1 do
1828 post :subscribe, :params => { :id => changeset.id }
1830 assert_response :success
1834 # test subscribe fail
1835 def test_subscribe_fail
1836 user = create(:user)
1839 changeset = create(:changeset, :closed)
1840 assert_no_difference "changeset.subscribers.count" do
1841 post :subscribe, :params => { :id => changeset.id }
1843 assert_response :unauthorized
1845 basic_authorization user.email, "test"
1848 assert_no_difference "changeset.subscribers.count" do
1849 post :subscribe, :params => { :id => 999111 }
1851 assert_response :not_found
1853 # trying to subscribe when already subscribed
1854 changeset = create(:changeset, :closed)
1855 changeset.subscribers.push(user)
1856 assert_no_difference "changeset.subscribers.count" do
1857 post :subscribe, :params => { :id => changeset.id }
1859 assert_response :conflict
1863 # test unsubscribe success
1864 def test_unsubscribe_success
1865 user = create(:user)
1866 basic_authorization user.email, "test"
1867 changeset = create(:changeset, :closed)
1868 changeset.subscribers.push(user)
1870 assert_difference "changeset.subscribers.count", -1 do
1871 post :unsubscribe, :params => { :id => changeset.id }
1873 assert_response :success
1875 # not closed changeset
1876 changeset = create(:changeset)
1877 changeset.subscribers.push(user)
1879 assert_difference "changeset.subscribers.count", -1 do
1880 post :unsubscribe, :params => { :id => changeset.id }
1882 assert_response :success
1886 # test unsubscribe fail
1887 def test_unsubscribe_fail
1889 changeset = create(:changeset, :closed)
1890 assert_no_difference "changeset.subscribers.count" do
1891 post :unsubscribe, :params => { :id => changeset.id }
1893 assert_response :unauthorized
1895 basic_authorization create(:user).email, "test"
1898 assert_no_difference "changeset.subscribers.count" do
1899 post :unsubscribe, :params => { :id => 999111 }
1901 assert_response :not_found
1903 # trying to unsubscribe when not subscribed
1904 changeset = create(:changeset, :closed)
1905 assert_no_difference "changeset.subscribers.count" do
1906 post :unsubscribe, :params => { :id => changeset.id }
1908 assert_response :not_found
1914 # boilerplate for checking that certain changesets exist in the
1916 def assert_changesets(changesets)
1917 assert_select "osm>changeset", changesets.size
1918 changesets.each do |changeset|
1919 assert_select "osm>changeset[id='#{changeset.id}']", 1
1924 # call the include method and assert properties of the bbox
1925 def check_after_include(changeset_id, lon, lat, bbox)
1926 xml = "<osm><node lon='#{lon}' lat='#{lat}'/></osm>"
1927 post :expand_bbox, :params => { :id => changeset_id }, :body => xml
1928 assert_response :success, "Setting include of changeset failed: #{@response.body}"
1930 # check exactly one changeset
1931 assert_select "osm>changeset", 1
1932 assert_select "osm>changeset[id='#{changeset_id}']", 1
1935 doc = XML::Parser.string(@response.body).parse
1936 changeset = doc.find("//osm/changeset").first
1937 assert_equal bbox[0], changeset["min_lon"].to_f, "min lon"
1938 assert_equal bbox[1], changeset["min_lat"].to_f, "min lat"
1939 assert_equal bbox[2], changeset["max_lon"].to_f, "max lon"
1940 assert_equal bbox[3], changeset["max_lat"].to_f, "max lat"
1944 # update the changeset_id of a way element
1945 def update_changeset(xml, changeset_id)
1946 xml_attr_rewrite(xml, "changeset", changeset_id)
1950 # update an attribute in a way element
1951 def xml_attr_rewrite(xml, name, value)
1952 xml.find("//osm/way").first[name] = value.to_s
1957 # build XML for changesets
1958 def create_changeset_xml(user: nil, id: nil)
1959 root = XML::Document.new
1960 root.root = XML::Node.new "osm"
1961 cs = XML::Node.new "changeset"
1963 cs["user"] = user.display_name
1964 cs["uid"] = user.id.to_s
1966 cs["id"] = id.to_s if id