2 require 'changeset_controller'
4 class ChangesetControllerTest < ActionController::TestCase
6 fixtures :changeset_comments, :changesets_subscribers
9 # test all routes which lead to this controller
12 { :path => "/api/0.6/changeset/create", :method => :put },
13 { :controller => "changeset", :action => "create" }
16 { :path => "/api/0.6/changeset/1/upload", :method => :post },
17 { :controller => "changeset", :action => "upload", :id => "1" }
20 { :path => "/api/0.6/changeset/1/download", :method => :get },
21 { :controller => "changeset", :action => "download", :id => "1" }
24 { :path => "/api/0.6/changeset/1/expand_bbox", :method => :post },
25 { :controller => "changeset", :action => "expand_bbox", :id => "1" }
28 { :path => "/api/0.6/changeset/1", :method => :get },
29 { :controller => "changeset", :action => "read", :id => "1" }
32 { :path => "/api/0.6/changeset/1/subscribe", :method => :post },
33 { :controller => "changeset", :action => "subscribe", :id => "1" }
36 { :path => "/api/0.6/changeset/1/unsubscribe", :method => :post },
37 { :controller => "changeset", :action => "unsubscribe", :id => "1" }
40 { :path => "/api/0.6/changeset/1", :method => :put },
41 { :controller => "changeset", :action => "update", :id => "1" }
44 { :path => "/api/0.6/changeset/1/close", :method => :put },
45 { :controller => "changeset", :action => "close", :id => "1" }
48 { :path => "/api/0.6/changeset/1/comment", :method => :post },
49 { :controller => "changeset", :action => "comment", :id => "1" }
52 { :path => "/api/0.6/changeset/comment/1/hide", :method => :post },
53 { :controller => "changeset", :action => "hide_comment", :id => "1" }
56 { :path => "/api/0.6/changeset/comment/1/unhide", :method => :post },
57 { :controller => "changeset", :action => "unhide_comment", :id => "1" }
60 { :path => "/api/0.6/changesets", :method => :get },
61 { :controller => "changeset", :action => "query" }
64 { :path => "/changeset/1/comments/feed", :method => :get },
65 { :controller => "changeset", :action => "comments_feed", :id => "1", :format => "rss" }
68 { :path => "/user/name/history", :method => :get },
69 { :controller => "changeset", :action => "list", :display_name => "name" }
72 { :path => "/user/name/history/feed", :method => :get },
73 { :controller => "changeset", :action => "feed", :display_name => "name", :format => :atom }
76 { :path => "/history/friends", :method => :get },
77 { :controller => "changeset", :action => "list", :friends => true }
80 { :path => "/history/nearby", :method => :get },
81 { :controller => "changeset", :action => "list", :nearby => true }
84 { :path => "/history", :method => :get },
85 { :controller => "changeset", :action => "list" }
88 { :path => "/history/feed", :method => :get },
89 { :controller => "changeset", :action => "feed", :format => :atom }
92 { :path => "/history/comments/feed", :method => :get },
93 { :controller => "changeset", :action => "comments_feed", :format => "rss" }
97 # -----------------------
98 # Test simple changeset creation
99 # -----------------------
102 basic_authorization users(:normal_user).email, "test"
103 # Create the first user's changeset
104 content "<osm><changeset>" +
105 "<tag k='created_by' v='osm test suite checking changesets'/>" +
108 assert_require_public_data
110 basic_authorization users(:public_user).email, "test"
111 # Create the first user's changeset
112 content "<osm><changeset>" +
113 "<tag k='created_by' v='osm test suite checking changesets'/>" +
117 assert_response :success, "Creation of changeset did not return sucess status"
118 newid = @response.body.to_i
120 # check end time, should be an hour ahead of creation time
121 cs = Changeset.find(newid)
122 duration = cs.closed_at - cs.created_at
123 # the difference can either be a rational, or a floating point number
124 # of seconds, depending on the code path taken :-(
125 if duration.class == Rational
126 assert_equal Rational(1, 24), duration, "initial idle timeout should be an hour (#{cs.created_at} -> #{cs.closed_at})"
128 # must be number of seconds...
129 assert_equal 3600, duration.round, "initial idle timeout should be an hour (#{cs.created_at} -> #{cs.closed_at})"
132 # checks if uploader was subscribed
133 assert_equal 1, cs.subscribers.length
136 def test_create_invalid
137 basic_authorization users(:normal_user).email, "test"
138 content "<osm><changeset></osm>"
140 assert_require_public_data
142 ## Try the public user
143 basic_authorization users(:public_user).email, "test"
144 content "<osm><changeset></osm>"
146 assert_response :bad_request, "creating a invalid changeset should fail"
149 def test_create_invalid_no_content
150 ## First check with no auth
152 assert_response :unauthorized, "shouldn't be able to create a changeset with no auth"
154 ## Now try to with the non-public user
155 basic_authorization users(:normal_user).email, "test"
157 assert_require_public_data
159 ## Try the inactive user
160 basic_authorization users(:inactive_user).email, "test"
164 ## Now try to use the public user
165 basic_authorization users(:public_user).email, "test"
167 assert_response :bad_request, "creating a changeset with no content should fail"
170 def test_create_wrong_method
171 basic_authorization users(:public_user).email, "test"
173 assert_response :method_not_allowed
175 assert_response :method_not_allowed
179 # check that the changeset can be read and returns the correct
180 # document structure.
182 changeset_id = changesets(:normal_user_first_change).id
183 get :read, :id => changeset_id
184 assert_response :success, "cannot get first changeset"
186 assert_select "osm[version='#{API_VERSION}'][generator='OpenStreetMap server']", 1
187 assert_select "osm>changeset[id='#{changeset_id}']", 1
188 assert_select "osm>changeset>discussion", 0
190 get :read, :id => changeset_id, :include_discussion => true
191 assert_response :success, "cannot get first changeset with comments"
193 assert_select "osm[version='#{API_VERSION}'][generator='OpenStreetMap server']", 1
194 assert_select "osm>changeset[id='#{changeset_id}']", 1
195 assert_select "osm>changeset>discussion", 1
199 # check that a changeset that doesn't exist returns an appropriate message
200 def test_read_not_found
201 [0, -32, 233455644, "afg", "213"].each do |id|
204 assert_response :not_found, "should get a not found"
205 rescue ActionController::UrlGenerationError => ex
206 assert_match /No route matches/, ex.to_s
212 # test that the user who opened a change can close it
214 ## Try without authentication
215 put :close, :id => changesets(:public_user_first_change).id
216 assert_response :unauthorized
218 ## Try using the non-public user
219 basic_authorization users(:normal_user).email, "test"
220 put :close, :id => changesets(:normal_user_first_change).id
221 assert_require_public_data
223 ## The try with the public user
224 basic_authorization users(:public_user).email, "test"
226 cs_id = changesets(:public_user_first_change).id
227 put :close, :id => cs_id
228 assert_response :success
230 # test that it really is closed now
231 cs = Changeset.find(cs_id)
233 "changeset should be closed now (#{cs.closed_at} > #{Time.now.getutc}.")
237 # test that a different user can't close another user's changeset
238 def test_close_invalid
239 basic_authorization users(:public_user).email, "test"
241 put :close, :id => changesets(:normal_user_first_change).id
242 assert_response :conflict
243 assert_equal "The user doesn't own that changeset", @response.body
247 # test that you can't close using another method
248 def test_close_method_invalid
249 basic_authorization users(:public_user).email, "test"
251 cs_id = changesets(:public_user_first_change).id
252 get :close, :id => cs_id
253 assert_response :method_not_allowed
255 post :close, :id => cs_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, :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 users(:public_user).email, "test"
278 put :close, :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
292 changeset_id = changesets(:public_user_first_change).id
294 # simple diff to change a node, way and relation by removing
299 <node id='1' lon='0' lat='0' changeset='#{changeset_id}' version='1'/>
300 <way id='1' changeset='#{changeset_id}' version='1'>
305 <relation id='1' changeset='#{changeset_id}' version='1'>
306 <member type='way' role='some' ref='3'/>
307 <member type='node' role='some' ref='5'/>
308 <member type='relation' role='some' ref='3'/>
316 post :upload, :id => changeset_id
317 assert_response :unauthorized,
318 "shouldnn't be able to upload a simple valid diff to changeset: #{@response.body}"
320 ## Now try with a private user
321 basic_authorization users(:normal_user).email, "test"
322 changeset_id = changesets(:normal_user_first_change).id
324 # simple diff to change a node, way and relation by removing
329 <node id='1' lon='0' lat='0' changeset='#{changeset_id}' version='1'/>
330 <way id='1' changeset='#{changeset_id}' version='1'>
335 <relation id='1' changeset='#{changeset_id}' version='1'>
336 <member type='way' role='some' ref='3'/>
337 <member type='node' role='some' ref='5'/>
338 <member type='relation' role='some' ref='3'/>
346 post :upload, :id => changeset_id
347 assert_response :forbidden,
348 "can't upload a simple valid diff to changeset: #{@response.body}"
350 ## Now try with the public user
351 basic_authorization users(:public_user).email, "test"
352 changeset_id = changesets(:public_user_first_change).id
354 # simple diff to change a node, way and relation by removing
359 <node id='1' lon='0' lat='0' changeset='#{changeset_id}' version='1'/>
360 <way id='1' changeset='#{changeset_id}' version='1'>
365 <relation id='1' changeset='#{changeset_id}' version='1'>
366 <member type='way' role='some' ref='3'/>
367 <member type='node' role='some' ref='5'/>
368 <member type='relation' role='some' ref='3'/>
376 post :upload, :id => changeset_id
377 assert_response :success,
378 "can't upload a simple valid diff to changeset: #{@response.body}"
380 # check that the changes made it into the database
381 assert_equal 0, Node.find(1).tags.size, "node 1 should now have no tags"
382 assert_equal 0, Way.find(1).tags.size, "way 1 should now have no tags"
383 assert_equal 0, Relation.find(1).tags.size, "relation 1 should now have no tags"
387 # upload something which creates new objects using placeholders
388 def test_upload_create_valid
389 basic_authorization users(:public_user).email, "test"
390 cs_id = changesets(:public_user_first_change).id
392 # simple diff to create a node way and relation using placeholders
396 <node id='-1' lon='0' lat='0' changeset='#{cs_id}'>
397 <tag k='foo' v='bar'/>
398 <tag k='baz' v='bat'/>
400 <way id='-1' changeset='#{cs_id}'>
405 <relation id='-1' changeset='#{cs_id}'>
406 <member type='way' role='some' ref='3'/>
407 <member type='node' role='some' ref='5'/>
408 <member type='relation' role='some' ref='3'/>
416 post :upload, :id => cs_id
417 assert_response :success,
418 "can't upload a simple valid creation to changeset: #{@response.body}"
420 # check the returned payload
421 assert_select "diffResult[version='#{API_VERSION}'][generator='OpenStreetMap server']", 1
422 assert_select "diffResult>node", 1
423 assert_select "diffResult>way", 1
424 assert_select "diffResult>relation", 1
426 # inspect the response to find out what the new element IDs are
427 doc = XML::Parser.string(@response.body).parse
428 new_node_id = doc.find("//diffResult/node").first["new_id"].to_i
429 new_way_id = doc.find("//diffResult/way").first["new_id"].to_i
430 new_rel_id = doc.find("//diffResult/relation").first["new_id"].to_i
432 # check the old IDs are all present and negative one
433 assert_equal -1, doc.find("//diffResult/node").first["old_id"].to_i
434 assert_equal -1, doc.find("//diffResult/way").first["old_id"].to_i
435 assert_equal -1, doc.find("//diffResult/relation").first["old_id"].to_i
437 # check the versions are present and equal one
438 assert_equal 1, doc.find("//diffResult/node").first["new_version"].to_i
439 assert_equal 1, doc.find("//diffResult/way").first["new_version"].to_i
440 assert_equal 1, doc.find("//diffResult/relation").first["new_version"].to_i
442 # check that the changes made it into the database
443 assert_equal 2, Node.find(new_node_id).tags.size, "new node should have two tags"
444 assert_equal 0, Way.find(new_way_id).tags.size, "new way should have no tags"
445 assert_equal 0, Relation.find(new_rel_id).tags.size, "new relation should have no tags"
449 # test a complex delete where we delete elements which rely on eachother
450 # in the same transaction.
451 def test_upload_delete
452 basic_authorization users(:public_user).display_name, "test"
454 diff = XML::Document.new
455 diff.root = XML::Node.new "osmChange"
456 delete = XML::Node.new "delete"
458 delete << current_relations(:visible_relation).to_xml_node
459 delete << current_relations(:used_relation).to_xml_node
460 delete << current_ways(:used_way).to_xml_node
461 delete << current_nodes(:node_used_by_relationship).to_xml_node
463 # update the changeset to one that this user owns
464 changeset_id = changesets(:public_user_first_change).id
465 %w(node way relation).each do |type|
466 delete.find("//osmChange/delete/#{type}").each do |n|
467 n['changeset'] = changeset_id.to_s
473 post :upload, :id => changeset_id
474 assert_response :success,
475 "can't upload a deletion diff to changeset: #{@response.body}"
477 # check the response is well-formed
478 assert_select "diffResult>node", 1
479 assert_select "diffResult>way", 1
480 assert_select "diffResult>relation", 2
482 # check that everything was deleted
483 assert_equal false, Node.find(current_nodes(:node_used_by_relationship).id).visible
484 assert_equal false, Way.find(current_ways(:used_way).id).visible
485 assert_equal false, Relation.find(current_relations(:visible_relation).id).visible
486 assert_equal false, Relation.find(current_relations(:used_relation).id).visible
490 # test uploading a delete with no lat/lon, as they are optional in
491 # the osmChange spec.
492 def test_upload_nolatlon_delete
493 basic_authorization users(:public_user).display_name, "test"
495 node = current_nodes(:public_visible_node)
496 cs = changesets(:public_user_first_change)
497 diff = "<osmChange><delete><node id='#{node.id}' version='#{node.version}' changeset='#{cs.id}'/></delete></osmChange>"
501 post :upload, :id => cs.id
502 assert_response :success,
503 "can't upload a deletion diff to changeset: #{@response.body}"
505 # check the response is well-formed
506 assert_select "diffResult>node", 1
508 # check that everything was deleted
509 assert_equal false, Node.find(node.id).visible
512 def test_repeated_changeset_create
514 basic_authorization users(:public_user).email, "test"
516 # create a temporary changeset
517 content "<osm><changeset>" +
518 "<tag k='created_by' v='osm test suite checking changesets'/>" +
520 assert_difference('Changeset.count', 1) do
523 assert_response :success
527 def test_upload_large_changeset
528 basic_authorization users(:public_user).email, "test"
531 content "<osm><changeset/></osm>"
533 assert_response :success, "Should be able to create a changeset: #{@response.body}"
534 changeset_id = @response.body.to_i
536 # upload some widely-spaced nodes, spiralling positive and negative to cause
537 # largest bbox over-expansion possible.
541 <node id='-1' lon='-20' lat='-10' changeset='#{changeset_id}'/>
542 <node id='-10' lon='20' lat='10' changeset='#{changeset_id}'/>
543 <node id='-2' lon='-40' lat='-20' changeset='#{changeset_id}'/>
544 <node id='-11' lon='40' lat='20' changeset='#{changeset_id}'/>
545 <node id='-3' lon='-60' lat='-30' changeset='#{changeset_id}'/>
546 <node id='-12' lon='60' lat='30' changeset='#{changeset_id}'/>
547 <node id='-4' lon='-80' lat='-40' changeset='#{changeset_id}'/>
548 <node id='-13' lon='80' lat='40' changeset='#{changeset_id}'/>
549 <node id='-5' lon='-100' lat='-50' changeset='#{changeset_id}'/>
550 <node id='-14' lon='100' lat='50' changeset='#{changeset_id}'/>
551 <node id='-6' lon='-120' lat='-60' changeset='#{changeset_id}'/>
552 <node id='-15' lon='120' lat='60' changeset='#{changeset_id}'/>
553 <node id='-7' lon='-140' lat='-70' changeset='#{changeset_id}'/>
554 <node id='-16' lon='140' lat='70' changeset='#{changeset_id}'/>
555 <node id='-8' lon='-160' lat='-80' changeset='#{changeset_id}'/>
556 <node id='-17' lon='160' lat='80' changeset='#{changeset_id}'/>
557 <node id='-9' lon='-179.9' lat='-89.9' changeset='#{changeset_id}'/>
558 <node id='-18' lon='179.9' lat='89.9' changeset='#{changeset_id}'/>
563 # upload it, which used to cause an error like "PGError: ERROR:
564 # integer out of range" (bug #2152). but shouldn't any more.
566 post :upload, :id => changeset_id
567 assert_response :success,
568 "can't upload a spatially-large diff to changeset: #{@response.body}"
570 # check that the changeset bbox is within bounds
571 cs = Changeset.find(changeset_id)
572 assert cs.min_lon >= -180 * GeoRecord::SCALE, "Minimum longitude (#{cs.min_lon / GeoRecord::SCALE}) should be >= -180 to be valid."
573 assert cs.max_lon <= 180 * GeoRecord::SCALE, "Maximum longitude (#{cs.max_lon / GeoRecord::SCALE}) should be <= 180 to be valid."
574 assert cs.min_lat >= -90 * GeoRecord::SCALE, "Minimum latitude (#{cs.min_lat / GeoRecord::SCALE}) should be >= -90 to be valid."
575 assert cs.max_lat >= 90 * GeoRecord::SCALE, "Maximum latitude (#{cs.max_lat / GeoRecord::SCALE}) should be <= 90 to be valid."
579 # test that deleting stuff in a transaction doesn't bypass the checks
580 # to ensure that used elements are not deleted.
581 def test_upload_delete_invalid
582 basic_authorization users(:public_user).email, "test"
584 diff = XML::Document.new
585 diff.root = XML::Node.new "osmChange"
586 delete = XML::Node.new "delete"
588 delete << current_relations(:public_visible_relation).to_xml_node
589 delete << current_ways(:used_way).to_xml_node
590 delete << current_nodes(:node_used_by_relationship).to_xml_node
594 post :upload, :id => 2
595 assert_response :precondition_failed,
596 "shouldn't be able to upload a invalid deletion diff: #{@response.body}"
597 assert_equal "Precondition failed: Way 3 is still used by relations 1.", @response.body
599 # check that nothing was, in fact, deleted
600 assert_equal true, Node.find(current_nodes(:node_used_by_relationship).id).visible
601 assert_equal true, Way.find(current_ways(:used_way).id).visible
602 assert_equal true, Relation.find(current_relations(:visible_relation).id).visible
606 # test that a conditional delete of an in use object works.
607 def test_upload_delete_if_unused
608 basic_authorization users(:public_user).email, "test"
610 diff = XML::Document.new
611 diff.root = XML::Node.new "osmChange"
612 delete = XML::Node.new "delete"
614 delete["if-unused"] = ""
615 delete << current_relations(:public_used_relation).to_xml_node
616 delete << current_ways(:used_way).to_xml_node
617 delete << current_nodes(:node_used_by_relationship).to_xml_node
621 post :upload, :id => 2
622 assert_response :success,
623 "can't do a conditional delete of in use objects: #{@response.body}"
625 # check the returned payload
626 assert_select "diffResult[version='#{API_VERSION}'][generator='OpenStreetMap server']", 1
627 assert_select "diffResult>node", 1
628 assert_select "diffResult>way", 1
629 assert_select "diffResult>relation", 1
632 doc = XML::Parser.string(@response.body).parse
634 # check the old IDs are all present and what we expect
635 assert_equal current_nodes(:node_used_by_relationship).id, doc.find("//diffResult/node").first["old_id"].to_i
636 assert_equal current_ways(:used_way).id, doc.find("//diffResult/way").first["old_id"].to_i
637 assert_equal current_relations(:public_used_relation).id, doc.find("//diffResult/relation").first["old_id"].to_i
639 # check the new IDs are all present and unchanged
640 assert_equal current_nodes(:node_used_by_relationship).id, doc.find("//diffResult/node").first["new_id"].to_i
641 assert_equal current_ways(:used_way).id, doc.find("//diffResult/way").first["new_id"].to_i
642 assert_equal current_relations(:public_used_relation).id, doc.find("//diffResult/relation").first["new_id"].to_i
644 # check the new versions are all present and unchanged
645 assert_equal current_nodes(:node_used_by_relationship).version, doc.find("//diffResult/node").first["new_version"].to_i
646 assert_equal current_ways(:used_way).version, doc.find("//diffResult/way").first["new_version"].to_i
647 assert_equal current_relations(:public_used_relation).version, doc.find("//diffResult/relation").first["new_version"].to_i
649 # check that nothing was, in fact, deleted
650 assert_equal true, Node.find(current_nodes(:node_used_by_relationship).id).visible
651 assert_equal true, Way.find(current_ways(:used_way).id).visible
652 assert_equal true, Relation.find(current_relations(:public_used_relation).id).visible
656 # upload an element with a really long tag value
657 def test_upload_invalid_too_long_tag
658 basic_authorization users(:public_user).email, "test"
659 cs_id = changesets(:public_user_first_change).id
661 # simple diff to create a node way and relation using placeholders
665 <node id='-1' lon='0' lat='0' changeset='#{cs_id}'>
666 <tag k='foo' v='#{"x" * 256}'/>
674 post :upload, :id => cs_id
675 assert_response :bad_request,
676 "shoudln't be able to upload too long a tag to changeset: #{@response.body}"
680 # upload something which creates new objects and inserts them into
681 # existing containers using placeholders.
682 def test_upload_complex
683 basic_authorization users(:public_user).email, "test"
684 cs_id = changesets(:public_user_first_change).id
686 # simple diff to create a node way and relation using placeholders
690 <node id='-1' lon='0' lat='0' changeset='#{cs_id}'>
691 <tag k='foo' v='bar'/>
692 <tag k='baz' v='bat'/>
696 <way id='1' changeset='#{cs_id}' version='1'>
700 <relation id='1' changeset='#{cs_id}' version='1'>
701 <member type='way' role='some' ref='3'/>
702 <member type='node' role='some' ref='-1'/>
703 <member type='relation' role='some' ref='3'/>
711 post :upload, :id => cs_id
712 assert_response :success,
713 "can't upload a complex diff to changeset: #{@response.body}"
715 # check the returned payload
716 assert_select "diffResult[version='#{API_VERSION}'][generator='#{GENERATOR}']", 1
717 assert_select "diffResult>node", 1
718 assert_select "diffResult>way", 1
719 assert_select "diffResult>relation", 1
721 # inspect the response to find out what the new element IDs are
722 doc = XML::Parser.string(@response.body).parse
723 new_node_id = doc.find("//diffResult/node").first["new_id"].to_i
725 # check that the changes made it into the database
726 assert_equal 2, Node.find(new_node_id).tags.size, "new node should have two tags"
727 assert_equal [new_node_id, 3], Way.find(1).nds, "way nodes should match"
728 Relation.find(1).members.each do |type, id, _role|
730 assert_equal new_node_id, id, "relation should contain new node"
736 # create a diff which references several changesets, which should cause
737 # a rollback and none of the diff gets committed
738 def test_upload_invalid_changesets
739 basic_authorization users(:public_user).email, "test"
740 cs_id = changesets(:public_user_first_change).id
742 # simple diff to create a node way and relation using placeholders
746 <node id='1' lon='0' lat='0' changeset='#{cs_id}' version='1'/>
747 <way id='1' changeset='#{cs_id}' version='1'>
752 <relation id='1' changeset='#{cs_id}' version='1'>
753 <member type='way' role='some' ref='3'/>
754 <member type='node' role='some' ref='5'/>
755 <member type='relation' role='some' ref='3'/>
759 <node id='-1' lon='0' lat='0' changeset='4'>
760 <tag k='foo' v='bar'/>
761 <tag k='baz' v='bat'/>
766 # cache the objects before uploading them
767 node = current_nodes(:visible_node)
768 way = current_ways(:visible_way)
769 rel = current_relations(:visible_relation)
773 post :upload, :id => cs_id
774 assert_response :conflict,
775 "uploading a diff with multiple changsets should have failed"
777 # check that objects are unmodified
778 assert_nodes_are_equal(node, Node.find(1))
779 assert_ways_are_equal(way, Way.find(1))
780 assert_relations_are_equal(rel, Relation.find(1))
784 # upload multiple versions of the same element in the same diff.
785 def test_upload_multiple_valid
786 basic_authorization users(:public_user).email, "test"
787 cs_id = changesets(:public_user_first_change).id
789 # change the location of a node multiple times, each time referencing
790 # the last version. doesn't this depend on version numbers being
795 <node id='1' lon='0' lat='0' changeset='#{cs_id}' version='1'/>
796 <node id='1' lon='1' lat='0' changeset='#{cs_id}' version='2'/>
797 <node id='1' lon='1' lat='1' changeset='#{cs_id}' version='3'/>
798 <node id='1' lon='1' lat='2' changeset='#{cs_id}' version='4'/>
799 <node id='1' lon='2' lat='2' changeset='#{cs_id}' version='5'/>
800 <node id='1' lon='3' lat='2' changeset='#{cs_id}' version='6'/>
801 <node id='1' lon='3' lat='3' changeset='#{cs_id}' version='7'/>
802 <node id='1' lon='9' lat='9' changeset='#{cs_id}' version='8'/>
809 post :upload, :id => cs_id
810 assert_response :success,
811 "can't upload multiple versions of an element in a diff: #{@response.body}"
813 # check the response is well-formed. its counter-intuitive, but the
814 # API will return multiple elements with the same ID and different
815 # version numbers for each change we made.
816 assert_select "diffResult>node", 8
820 # upload multiple versions of the same element in the same diff, but
821 # keep the version numbers the same.
822 def test_upload_multiple_duplicate
823 basic_authorization users(:public_user).email, "test"
824 cs_id = changesets(:public_user_first_change).id
829 <node id='1' lon='0' lat='0' changeset='#{cs_id}' version='1'/>
830 <node id='1' lon='1' lat='1' changeset='#{cs_id}' version='1'/>
837 post :upload, :id => cs_id
838 assert_response :conflict,
839 "shouldn't be able to upload the same element twice in a diff: #{@response.body}"
843 # try to upload some elements without specifying the version
844 def test_upload_missing_version
845 basic_authorization users(:public_user).email, "test"
846 cs_id = changesets(:public_user_first_change).id
851 <node id='1' lon='1' lat='1' changeset='cs_id'/>
858 post :upload, :id => cs_id
859 assert_response :bad_request,
860 "shouldn't be able to upload an element without version: #{@response.body}"
864 # try to upload with commands other than create, modify, or delete
865 def test_action_upload_invalid
866 basic_authorization users(:public_user).email, "test"
867 cs_id = changesets(:public_user_first_change).id
872 <node id='1' lon='1' lat='1' changeset='#{cs_id}' />
877 post :upload, :id => cs_id
878 assert_response :bad_request, "Shouldn't be able to upload a diff with the action ping"
879 assert_equal @response.body, "Unknown action ping, choices are create, modify, delete"
883 # upload a valid changeset which has a mixture of whitespace
884 # to check a bug reported by ivansanchez (#1565).
885 def test_upload_whitespace_valid
886 basic_authorization users(:public_user).email, "test"
887 changeset_id = changesets(:public_user_first_change).id
891 <modify><node id='1' lon='0' lat='0' changeset='#{changeset_id}'
893 <node id='1' lon='1' lat='1' changeset='#{changeset_id}' version='2'><tag k='k' v='v'/></node></modify>
895 <relation id='1' changeset='#{changeset_id}' version='1'><member
896 type='way' role='some' ref='3'/><member
897 type='node' role='some' ref='5'/>
898 <member type='relation' role='some' ref='3'/>
900 </modify></osmChange>
905 post :upload, :id => changeset_id
906 assert_response :success,
907 "can't upload a valid diff with whitespace variations to changeset: #{@response.body}"
909 # check the response is well-formed
910 assert_select "diffResult>node", 2
911 assert_select "diffResult>relation", 1
913 # check that the changes made it into the database
914 assert_equal 1, Node.find(1).tags.size, "node 1 should now have one tag"
915 assert_equal 0, Relation.find(1).tags.size, "relation 1 should now have no tags"
919 # upload a valid changeset which has a mixture of whitespace
920 # to check a bug reported by ivansanchez.
921 def test_upload_reuse_placeholder_valid
922 basic_authorization users(:public_user).email, "test"
923 changeset_id = changesets(:public_user_first_change).id
928 <node id='-1' lon='0' lat='0' changeset='#{changeset_id}'>
929 <tag k="foo" v="bar"/>
933 <node id='-1' lon='1' lat='1' changeset='#{changeset_id}' version='1'/>
936 <node id='-1' lon='2' lat='2' changeset='#{changeset_id}' version='2'/>
943 post :upload, :id => changeset_id
944 assert_response :success,
945 "can't upload a valid diff with re-used placeholders to changeset: #{@response.body}"
947 # check the response is well-formed
948 assert_select "diffResult>node", 3
949 assert_select "diffResult>node[old_id='-1']", 3
953 # test what happens if a diff upload re-uses placeholder IDs in an
955 def test_upload_placeholder_invalid
956 basic_authorization users(:public_user).email, "test"
957 changeset_id = changesets(:public_user_first_change).id
962 <node id='-1' lon='0' lat='0' changeset='#{changeset_id}' version='1'/>
963 <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'/>
971 post :upload, :id => changeset_id
972 assert_response :bad_request,
973 "shouldn't be able to re-use placeholder IDs"
977 # test that uploading a way referencing invalid placeholders gives a
978 # proper error, not a 500.
979 def test_upload_placeholder_invalid_way
980 basic_authorization users(:public_user).email, "test"
981 changeset_id = changesets(:public_user_first_change).id
986 <node id="-1" lon="0" lat="0" changeset="#{changeset_id}" version="1"/>
987 <node id="-2" lon="1" lat="1" changeset="#{changeset_id}" version="1"/>
988 <node id="-3" lon="2" lat="2" changeset="#{changeset_id}" version="1"/>
989 <way id="-1" changeset="#{changeset_id}" version="1">
1001 post :upload, :id => changeset_id
1002 assert_response :bad_request,
1003 "shouldn't be able to use invalid placeholder IDs"
1004 assert_equal "Placeholder node not found for reference -4 in way -1", @response.body
1006 # the same again, but this time use an existing way
1010 <node id="-1" lon="0" lat="0" changeset="#{changeset_id}" version="1"/>
1011 <node id="-2" lon="1" lat="1" changeset="#{changeset_id}" version="1"/>
1012 <node id="-3" lon="2" lat="2" changeset="#{changeset_id}" version="1"/>
1013 <way id="1" changeset="#{changeset_id}" version="1">
1025 post :upload, :id => changeset_id
1026 assert_response :bad_request,
1027 "shouldn't be able to use invalid placeholder IDs"
1028 assert_equal "Placeholder node not found for reference -4 in way 1", @response.body
1032 # test that uploading a relation referencing invalid placeholders gives a
1033 # proper error, not a 500.
1034 def test_upload_placeholder_invalid_relation
1035 basic_authorization users(:public_user).email, "test"
1036 changeset_id = changesets(:public_user_first_change).id
1041 <node id="-1" lon="0" lat="0" changeset="#{changeset_id}" version="1"/>
1042 <node id="-2" lon="1" lat="1" changeset="#{changeset_id}" version="1"/>
1043 <node id="-3" lon="2" lat="2" changeset="#{changeset_id}" version="1"/>
1044 <relation id="-1" changeset="#{changeset_id}" version="1">
1045 <member type="node" role="foo" ref="-1"/>
1046 <member type="node" role="foo" ref="-2"/>
1047 <member type="node" role="foo" ref="-3"/>
1048 <member type="node" role="foo" ref="-4"/>
1056 post :upload, :id => changeset_id
1057 assert_response :bad_request,
1058 "shouldn't be able to use invalid placeholder IDs"
1059 assert_equal "Placeholder Node not found for reference -4 in relation -1.", @response.body
1061 # the same again, but this time use an existing way
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 <relation id="1" changeset="#{changeset_id}" version="1">
1069 <member type="node" role="foo" ref="-1"/>
1070 <member type="node" role="foo" ref="-2"/>
1071 <member type="node" role="foo" ref="-3"/>
1072 <member type="way" role="bar" ref="-1"/>
1080 post :upload, :id => changeset_id
1081 assert_response :bad_request,
1082 "shouldn't be able to use invalid placeholder IDs"
1083 assert_equal "Placeholder Way not found for reference -1 in relation 1.", @response.body
1087 # test what happens if a diff is uploaded containing only a node
1089 def test_upload_node_move
1090 basic_authorization users(:public_user).email, "test"
1092 content "<osm><changeset>" +
1093 "<tag k='created_by' v='osm test suite checking changesets'/>" +
1094 "</changeset></osm>"
1096 assert_response :success
1097 changeset_id = @response.body.to_i
1099 old_node = current_nodes(:visible_node)
1101 diff = XML::Document.new
1102 diff.root = XML::Node.new "osmChange"
1103 modify = XML::Node.new "modify"
1104 xml_old_node = old_node.to_xml_node
1105 xml_old_node["lat"] = (2.0).to_s
1106 xml_old_node["lon"] = (2.0).to_s
1107 xml_old_node["changeset"] = changeset_id.to_s
1108 modify << xml_old_node
1113 post :upload, :id => changeset_id
1114 assert_response :success,
1115 "diff should have uploaded OK"
1118 changeset = Changeset.find(changeset_id)
1119 assert_equal 1 * GeoRecord::SCALE, changeset.min_lon, "min_lon should be 1 degree"
1120 assert_equal 2 * GeoRecord::SCALE, changeset.max_lon, "max_lon should be 2 degrees"
1121 assert_equal 1 * GeoRecord::SCALE, changeset.min_lat, "min_lat should be 1 degree"
1122 assert_equal 2 * GeoRecord::SCALE, changeset.max_lat, "max_lat should be 2 degrees"
1126 # test what happens if a diff is uploaded adding a node to a way.
1127 def test_upload_way_extend
1128 basic_authorization users(:public_user).email, "test"
1130 content "<osm><changeset>" +
1131 "<tag k='created_by' v='osm test suite checking changesets'/>" +
1132 "</changeset></osm>"
1134 assert_response :success
1135 changeset_id = @response.body.to_i
1137 old_way = current_ways(:visible_way)
1139 diff = XML::Document.new
1140 diff.root = XML::Node.new "osmChange"
1141 modify = XML::Node.new "modify"
1142 xml_old_way = old_way.to_xml_node
1143 nd_ref = XML::Node.new "nd"
1144 nd_ref["ref"] = current_nodes(:visible_node).id.to_s
1145 xml_old_way << nd_ref
1146 xml_old_way["changeset"] = changeset_id.to_s
1147 modify << xml_old_way
1152 post :upload, :id => changeset_id
1153 assert_response :success,
1154 "diff should have uploaded OK"
1157 changeset = Changeset.find(changeset_id)
1158 assert_equal 1 * GeoRecord::SCALE, changeset.min_lon, "min_lon should be 1 degree"
1159 assert_equal 3 * GeoRecord::SCALE, changeset.max_lon, "max_lon should be 3 degrees"
1160 assert_equal 1 * GeoRecord::SCALE, changeset.min_lat, "min_lat should be 1 degree"
1161 assert_equal 3 * GeoRecord::SCALE, changeset.max_lat, "max_lat should be 3 degrees"
1165 # test for more issues in #1568
1166 def test_upload_empty_invalid
1167 basic_authorization users(:public_user).email, "test"
1170 "<osmChange></osmChange>",
1171 "<osmChange><modify/></osmChange>",
1172 "<osmChange><modify></modify></osmChange>"
1176 post :upload, :id => changesets(:public_user_first_change).id
1177 assert_response(:success, "should be able to upload " +
1178 "empty changeset: " + diff)
1183 # test that the X-Error-Format header works to request XML errors
1184 def test_upload_xml_errors
1185 basic_authorization users(:public_user).email, "test"
1187 # try and delete a node that is in use
1188 diff = XML::Document.new
1189 diff.root = XML::Node.new "osmChange"
1190 delete = XML::Node.new "delete"
1192 delete << current_nodes(:node_used_by_relationship).to_xml_node
1197 post :upload, :id => 2
1198 assert_response :success,
1199 "failed to return error in XML format"
1201 # check the returned payload
1202 assert_select "osmError[version='#{API_VERSION}'][generator='OpenStreetMap server']", 1
1203 assert_select "osmError>status", 1
1204 assert_select "osmError>message", 1
1208 # when we make some simple changes we get the same changes back from the
1210 def test_diff_download_simple
1211 ## First try with the normal user, which should get a forbidden
1212 basic_authorization(users(:normal_user).email, "test")
1214 # create a temporary changeset
1215 content "<osm><changeset>" +
1216 "<tag k='created_by' v='osm test suite checking changesets'/>" +
1217 "</changeset></osm>"
1219 assert_response :forbidden
1221 ## Now try with the public user
1222 basic_authorization(users(:public_user).email, "test")
1224 # create a temporary changeset
1225 content "<osm><changeset>" +
1226 "<tag k='created_by' v='osm test suite checking changesets'/>" +
1227 "</changeset></osm>"
1229 assert_response :success
1230 changeset_id = @response.body.to_i
1236 <node id='1' lon='0' lat='0' changeset='#{changeset_id}' version='1'/>
1237 <node id='1' lon='1' lat='0' changeset='#{changeset_id}' version='2'/>
1238 <node id='1' lon='1' lat='1' changeset='#{changeset_id}' version='3'/>
1239 <node id='1' lon='1' lat='2' changeset='#{changeset_id}' version='4'/>
1240 <node id='1' lon='2' lat='2' changeset='#{changeset_id}' version='5'/>
1241 <node id='1' lon='3' lat='2' changeset='#{changeset_id}' version='6'/>
1242 <node id='1' lon='3' lat='3' changeset='#{changeset_id}' version='7'/>
1243 <node id='1' lon='9' lat='9' changeset='#{changeset_id}' version='8'/>
1250 post :upload, :id => changeset_id
1251 assert_response :success,
1252 "can't upload multiple versions of an element in a diff: #{@response.body}"
1254 get :download, :id => changeset_id
1255 assert_response :success
1257 assert_select "osmChange", 1
1258 assert_select "osmChange>modify", 8
1259 assert_select "osmChange>modify>node", 8
1263 # culled this from josm to ensure that nothing in the way that josm
1264 # is formatting the request is causing it to fail.
1266 # NOTE: the error turned out to be something else completely!
1267 def test_josm_upload
1268 basic_authorization(users(:public_user).email, "test")
1270 # create a temporary changeset
1271 content "<osm><changeset>" +
1272 "<tag k='created_by' v='osm test suite checking changesets'/>" +
1273 "</changeset></osm>"
1275 assert_response :success
1276 changeset_id = @response.body.to_i
1279 <osmChange version="0.6" generator="JOSM">
1280 <create version="0.6" generator="JOSM">
1281 <node id='-1' visible='true' changeset='#{changeset_id}' lat='51.49619982187321' lon='-0.18722061869438314' />
1282 <node id='-2' visible='true' changeset='#{changeset_id}' lat='51.496359883909605' lon='-0.18653093576241928' />
1283 <node id='-3' visible='true' changeset='#{changeset_id}' lat='51.49598132358285' lon='-0.18719613290981638' />
1284 <node id='-4' visible='true' changeset='#{changeset_id}' lat='51.4961591711078' lon='-0.18629015888084607' />
1285 <node id='-5' visible='true' changeset='#{changeset_id}' lat='51.49582126021711' lon='-0.18708186591517145' />
1286 <node id='-6' visible='true' changeset='#{changeset_id}' lat='51.49591018437858' lon='-0.1861432441734455' />
1287 <node id='-7' visible='true' changeset='#{changeset_id}' lat='51.49560784152179' lon='-0.18694719410005425' />
1288 <node id='-8' visible='true' changeset='#{changeset_id}' lat='51.49567389979617' lon='-0.1860289771788006' />
1289 <node id='-9' visible='true' changeset='#{changeset_id}' lat='51.49543761398892' lon='-0.186820684213126' />
1290 <way id='-10' action='modiy' visible='true' changeset='#{changeset_id}'>
1300 <tag k='highway' v='residential' />
1301 <tag k='name' v='Foobar Street' />
1309 post :upload, :id => changeset_id
1310 assert_response :success,
1311 "can't upload a diff from JOSM: #{@response.body}"
1313 get :download, :id => changeset_id
1314 assert_response :success
1316 assert_select "osmChange", 1
1317 assert_select "osmChange>create>node", 9
1318 assert_select "osmChange>create>way", 1
1319 assert_select "osmChange>create>way>nd", 9
1320 assert_select "osmChange>create>way>tag", 2
1324 # when we make some complex changes we get the same changes back from the
1326 def test_diff_download_complex
1327 basic_authorization(users(:public_user).email, "test")
1329 # create a temporary changeset
1330 content "<osm><changeset>" +
1331 "<tag k='created_by' v='osm test suite checking changesets'/>" +
1332 "</changeset></osm>"
1334 assert_response :success
1335 changeset_id = @response.body.to_i
1341 <node id='1' lon='0' lat='0' changeset='#{changeset_id}' version='1'/>
1344 <node id='-1' lon='9' lat='9' changeset='#{changeset_id}' version='0'/>
1345 <node id='-2' lon='8' lat='9' changeset='#{changeset_id}' version='0'/>
1346 <node id='-3' lon='7' lat='9' changeset='#{changeset_id}' version='0'/>
1349 <node id='3' lon='20' lat='15' changeset='#{changeset_id}' version='1'/>
1350 <way id='1' changeset='#{changeset_id}' version='1'>
1362 post :upload, :id => changeset_id
1363 assert_response :success,
1364 "can't upload multiple versions of an element in a diff: #{@response.body}"
1366 get :download, :id => changeset_id
1367 assert_response :success
1369 assert_select "osmChange", 1
1370 assert_select "osmChange>create", 3
1371 assert_select "osmChange>delete", 1
1372 assert_select "osmChange>modify", 2
1373 assert_select "osmChange>create>node", 3
1374 assert_select "osmChange>delete>node", 1
1375 assert_select "osmChange>modify>node", 1
1376 assert_select "osmChange>modify>way", 1
1379 def test_changeset_download
1380 get :download, :id => changesets(:normal_user_first_change).id
1381 assert_response :success
1383 # print @response.body
1384 # FIXME needs more assert_select tests
1385 assert_select "osmChange[version='#{API_VERSION}'][generator='#{GENERATOR}']" do
1386 assert_select "create", :count => 5
1387 assert_select "create>node[id='#{nodes(:used_node_2).node_id}'][visible='#{nodes(:used_node_2).visible?}'][version='#{nodes(:used_node_2).version}']" do
1388 assert_select "tag[k='#{node_tags(:t3).k}'][v='#{node_tags(:t3).v}']"
1390 assert_select "create>node[id='#{nodes(:visible_node).node_id}']"
1395 # check that the bounding box of a changeset gets updated correctly
1396 ## FIXME: This should really be moded to a integration test due to the with_controller
1397 def test_changeset_bbox
1398 basic_authorization users(:public_user).email, "test"
1400 # create a new changeset
1401 content "<osm><changeset/></osm>"
1403 assert_response :success, "Creating of changeset failed."
1404 changeset_id = @response.body.to_i
1406 # add a single node to it
1407 with_controller(NodeController.new) do
1408 content "<osm><node lon='1' lat='2' changeset='#{changeset_id}'/></osm>"
1410 assert_response :success, "Couldn't create node."
1413 # get the bounding box back from the changeset
1414 get :read, :id => changeset_id
1415 assert_response :success, "Couldn't read back changeset."
1416 assert_select "osm>changeset[min_lon='1.0']", 1
1417 assert_select "osm>changeset[max_lon='1.0']", 1
1418 assert_select "osm>changeset[min_lat='2.0']", 1
1419 assert_select "osm>changeset[max_lat='2.0']", 1
1421 # add another node to it
1422 with_controller(NodeController.new) do
1423 content "<osm><node lon='2' lat='1' changeset='#{changeset_id}'/></osm>"
1425 assert_response :success, "Couldn't create second node."
1428 # get the bounding box back from the changeset
1429 get :read, :id => changeset_id
1430 assert_response :success, "Couldn't read back changeset for the second time."
1431 assert_select "osm>changeset[min_lon='1.0']", 1
1432 assert_select "osm>changeset[max_lon='2.0']", 1
1433 assert_select "osm>changeset[min_lat='1.0']", 1
1434 assert_select "osm>changeset[max_lat='2.0']", 1
1436 # add (delete) a way to it, which contains a point at (3,3)
1437 with_controller(WayController.new) do
1438 content update_changeset(current_ways(:visible_way).to_xml,
1440 put :delete, :id => current_ways(:visible_way).id
1441 assert_response :success, "Couldn't delete a way."
1444 # get the bounding box back from the changeset
1445 get :read, :id => changeset_id
1446 assert_response :success, "Couldn't read back changeset for the third time."
1447 # note that the 3.1 here is because of the bbox overexpansion
1448 assert_select "osm>changeset[min_lon='1.0']", 1
1449 assert_select "osm>changeset[max_lon='3.1']", 1
1450 assert_select "osm>changeset[min_lat='1.0']", 1
1451 assert_select "osm>changeset[max_lat='3.1']", 1
1455 # test that the changeset :include method works as it should
1456 def test_changeset_include
1457 basic_authorization users(:public_user).display_name, "test"
1459 # create a new changeset
1460 content "<osm><changeset/></osm>"
1462 assert_response :success, "Creating of changeset failed."
1463 changeset_id = @response.body.to_i
1465 # NOTE: the include method doesn't over-expand, like inserting
1466 # a real method does. this is because we expect the client to
1467 # know what it is doing!
1468 check_after_include(changeset_id, 1, 1, [1, 1, 1, 1])
1469 check_after_include(changeset_id, 3, 3, [1, 1, 3, 3])
1470 check_after_include(changeset_id, 4, 2, [1, 1, 4, 3])
1471 check_after_include(changeset_id, 2, 2, [1, 1, 4, 3])
1472 check_after_include(changeset_id, -1, -1, [-1, -1, 4, 3])
1473 check_after_include(changeset_id, -2, 5, [-2, -1, 4, 5])
1477 # test that a not found, wrong method with the expand bbox works as expected
1478 def test_changeset_expand_bbox_error
1479 basic_authorization users(:public_user).display_name, "test"
1481 # create a new changeset
1482 content "<osm><changeset/></osm>"
1484 assert_response :success, "Creating of changeset failed."
1485 changeset_id = @response.body.to_i
1491 content "<osm><node lon='#{lon}' lat='#{lat}'/></osm>"
1492 put :expand_bbox, :id => changeset_id
1493 assert_response :method_not_allowed, "shouldn't be able to put a bbox expand"
1495 # Try to get the update
1496 content "<osm><node lon='#{lon}' lat='#{lat}'/></osm>"
1497 get :expand_bbox, :id => changeset_id
1498 assert_response :method_not_allowed, "shouldn't be able to get a bbox expand"
1500 # Try to use a hopefully missing changeset
1501 content "<osm><node lon='#{lon}' lat='#{lat}'/></osm>"
1502 post :expand_bbox, :id => changeset_id + 13245
1503 assert_response :not_found, "shouldn't be able to do a bbox expand on a nonexistant changeset"
1507 # test the query functionality of changesets
1509 get :query, :bbox => "-10,-10, 10, 10"
1510 assert_response :success, "can't get changesets in bbox"
1511 assert_changesets [1, 4, 6]
1513 get :query, :bbox => "4.5,4.5,4.6,4.6"
1514 assert_response :success, "can't get changesets in bbox"
1515 assert_changesets [1]
1517 # not found when looking for changesets of non-existing users
1518 get :query, :user => User.maximum(:id) + 1
1519 assert_response :not_found
1520 get :query, :display_name => " "
1521 assert_response :not_found
1523 # can't get changesets of user 1 without authenticating
1524 get :query, :user => users(:normal_user).id
1525 assert_response :not_found, "shouldn't be able to get changesets by non-public user (ID)"
1526 get :query, :display_name => users(:normal_user).display_name
1527 assert_response :not_found, "shouldn't be able to get changesets by non-public user (name)"
1529 # but this should work
1530 basic_authorization "test@openstreetmap.org", "test"
1531 get :query, :user => users(:normal_user).id
1532 assert_response :success, "can't get changesets by user ID"
1533 assert_changesets [1, 3, 6, 8]
1535 get :query, :display_name => users(:normal_user).display_name
1536 assert_response :success, "can't get changesets by user name"
1537 assert_changesets [1, 3, 6, 8]
1539 # check that the correct error is given when we provide both UID and name
1540 get :query, :user => users(:normal_user).id, :display_name => users(:normal_user).display_name
1541 assert_response :bad_request, "should be a bad request to have both ID and name specified"
1543 get :query, :user => users(:normal_user).id, :open => true
1544 assert_response :success, "can't get changesets by user and open"
1545 assert_changesets [1]
1547 get :query, :time => '2007-12-31'
1548 assert_response :success, "can't get changesets by time-since"
1549 assert_changesets [1, 2, 4, 5, 6]
1551 get :query, :time => '2008-01-01T12:34Z'
1552 assert_response :success, "can't get changesets by time-since with hour"
1553 assert_changesets [1, 2, 4, 5, 6]
1555 get :query, :time => '2007-12-31T23:59Z,2008-01-01T00:01Z'
1556 assert_response :success, "can't get changesets by time-range"
1557 assert_changesets [1, 5, 6]
1559 get :query, :open => 'true'
1560 assert_response :success, "can't get changesets by open-ness"
1561 assert_changesets [1, 2, 4]
1563 get :query, :closed => 'true'
1564 assert_response :success, "can't get changesets by closed-ness"
1565 assert_changesets [3, 5, 6, 7, 8]
1567 get :query, :closed => 'true', :user => users(:normal_user).id
1568 assert_response :success, "can't get changesets by closed-ness and user"
1569 assert_changesets [3, 6, 8]
1571 get :query, :closed => 'true', :user => users(:public_user).id
1572 assert_response :success, "can't get changesets by closed-ness and user"
1573 assert_changesets [7]
1575 get :query, :changesets => '1,2,3'
1576 assert_response :success, "can't get changesets by id (as comma-separated string)"
1577 assert_changesets [1, 2, 3]
1579 get :query, :changesets => ''
1580 assert_response :bad_request, "should be a bad request since changesets is empty"
1584 # check that errors are returned if garbage is inserted
1585 # into query strings
1586 def test_query_invalid
1589 ";drop table users;"
1591 get :query, :bbox => bbox
1592 assert_response :bad_request, "'#{bbox}' isn't a bbox"
1597 ";drop table users;",
1601 get :query, :time => time
1602 assert_response :bad_request, "'#{time}' isn't a valid time range"
1610 get :query, :user => uid
1611 assert_response :bad_request, "'#{uid}' isn't a valid user ID"
1616 # check updating tags on a changeset
1617 def test_changeset_update
1618 ## First try with the non-public user
1619 changeset = changesets(:normal_user_first_change)
1620 new_changeset = changeset.to_xml
1621 new_tag = XML::Node.new "tag"
1622 new_tag['k'] = "tagtesting"
1623 new_tag['v'] = "valuetesting"
1624 new_changeset.find("//osm/changeset").first << new_tag
1625 content new_changeset
1627 # try without any authorization
1628 put :update, :id => changeset.id
1629 assert_response :unauthorized
1631 # try with the wrong authorization
1632 basic_authorization users(:public_user).email, "test"
1633 put :update, :id => changeset.id
1634 assert_response :conflict
1636 # now this should get an unauthorized
1637 basic_authorization users(:normal_user).email, "test"
1638 put :update, :id => changeset.id
1639 assert_require_public_data "user with their data non-public, shouldn't be able to edit their changeset"
1641 ## Now try with the public user
1642 changeset = changesets(:public_user_first_change)
1643 new_changeset = changeset.to_xml
1644 new_tag = XML::Node.new "tag"
1645 new_tag['k'] = "tagtesting"
1646 new_tag['v'] = "valuetesting"
1647 new_changeset.find("//osm/changeset").first << new_tag
1648 content new_changeset
1650 # try without any authorization
1651 @request.env["HTTP_AUTHORIZATION"] = nil
1652 put :update, :id => changeset.id
1653 assert_response :unauthorized
1655 # try with the wrong authorization
1656 basic_authorization users(:second_public_user).email, "test"
1657 put :update, :id => changeset.id
1658 assert_response :conflict
1660 # now this should work...
1661 basic_authorization users(:public_user).email, "test"
1662 put :update, :id => changeset.id
1663 assert_response :success
1665 assert_select "osm>changeset[id='#{changeset.id}']", 1
1666 assert_select "osm>changeset>tag", 2
1667 assert_select "osm>changeset>tag[k='tagtesting'][v='valuetesting']", 1
1671 # check that a user different from the one who opened the changeset
1673 def test_changeset_update_invalid
1674 basic_authorization users(:public_user).email, "test"
1676 changeset = changesets(:normal_user_first_change)
1677 new_changeset = changeset.to_xml
1678 new_tag = XML::Node.new "tag"
1679 new_tag['k'] = "testing"
1680 new_tag['v'] = "testing"
1681 new_changeset.find("//osm/changeset").first << new_tag
1683 content new_changeset
1684 put :update, :id => changeset.id
1685 assert_response :conflict
1689 # check that a changeset can contain a certain max number of changes.
1690 ## FIXME should be changed to an integration test due to the with_controller
1691 def test_changeset_limits
1692 basic_authorization users(:public_user).email, "test"
1694 # open a new changeset
1695 content "<osm><changeset/></osm>"
1697 assert_response :success, "can't create a new changeset"
1698 cs_id = @response.body.to_i
1700 # start the counter just short of where the changeset should finish.
1702 # alter the database to set the counter on the changeset directly,
1703 # otherwise it takes about 6 minutes to fill all of them.
1704 changeset = Changeset.find(cs_id)
1705 changeset.num_changes = Changeset::MAX_ELEMENTS - offset
1708 with_controller(NodeController.new) do
1710 content "<osm><node changeset='#{cs_id}' lat='0.0' lon='0.0'/></osm>"
1712 assert_response :success, "can't create a new node"
1713 node_id = @response.body.to_i
1715 get :read, :id => node_id
1716 assert_response :success, "can't read back new node"
1717 node_doc = XML::Parser.string(@response.body).parse
1718 node_xml = node_doc.find("//osm/node").first
1720 # loop until we fill the changeset with nodes
1722 node_xml['lat'] = rand.to_s
1723 node_xml['lon'] = rand.to_s
1724 node_xml['version'] = (i + 1).to_s
1727 put :update, :id => node_id
1728 assert_response :success, "attempt #{i} should have succeeded"
1731 # trying again should fail
1732 node_xml['lat'] = rand.to_s
1733 node_xml['lon'] = rand.to_s
1734 node_xml['version'] = offset.to_s
1737 put :update, :id => node_id
1738 assert_response :conflict, "final attempt should have failed"
1741 changeset = Changeset.find(cs_id)
1742 assert_equal Changeset::MAX_ELEMENTS + 1, changeset.num_changes
1744 # check that the changeset is now closed as well
1745 assert(!changeset.is_open?,
1746 "changeset should have been auto-closed by exceeding " +
1751 # This should display the last 20 changesets closed.
1753 get :list, :format => "html"
1754 assert_response :success
1755 assert_template "history"
1756 assert_template :layout => "map"
1757 assert_select "h2", :text => "Changesets", :count => 1
1759 get :list, :format => "html", :list => '1', :bbox => '-180,-90,90,180'
1760 assert_response :success
1761 assert_template "list"
1763 changesets = Changeset
1764 .where("num_changes > 0 and min_lon is not null")
1765 .order(:created_at => :desc)
1767 assert changesets.size <= 20
1769 # Now check that all 20 (or however many were returned) changesets are in the html
1770 assert_select "li", :count => changesets.size
1771 changesets.each do |_changeset|
1772 # FIXME this test needs rewriting - test for table contents
1777 # This should display the last 20 changesets closed.
1779 xhr :get, :list, :format => "html"
1780 assert_response :success
1781 assert_template "history"
1782 assert_template :layout => "xhr"
1783 assert_select "h2", :text => "Changesets", :count => 1
1785 get :list, :format => "html", :list => '1', :bbox => '-180,-90,90,180'
1786 assert_response :success
1787 assert_template "list"
1789 changesets = Changeset
1790 .where("num_changes > 0 and min_lon is not null")
1791 .order(:created_at => :desc)
1793 assert changesets.size <= 20
1795 # Now check that all 20 (or however many were returned) changesets are in the html
1796 assert_select "li", :count => changesets.size
1797 changesets.each do |_changeset|
1798 # FIXME this test needs rewriting - test for table contents
1803 # Checks the display of the user changesets listing
1805 user = users(:public_user)
1806 get :list, :format => "html", :display_name => user.display_name
1807 assert_response :success
1808 assert_template "history"
1809 ## FIXME need to add more checks to see which if edits are actually shown if your data is public
1813 # Check the not found of the list user changesets
1814 def test_list_user_not_found
1815 get :list, :format => "html", :display_name => "Some random user"
1816 assert_response :not_found
1817 assert_template 'user/no_such_user'
1821 # This should display the last 20 changesets closed.
1823 changesets = Changeset.where("num_changes > 0").order(:created_at => :desc).limit(20)
1824 assert changesets.size <= 20
1825 get :feed, :format => "atom"
1826 assert_response :success
1827 assert_template "list"
1828 # Now check that all 20 (or however many were returned) changesets are in the html
1829 assert_select "feed", :count => 1
1830 assert_select "entry", :count => changesets.size
1831 changesets.each do |_changeset|
1832 # FIXME this test needs rewriting - test for feed contents
1837 # Checks the display of the user changesets feed
1839 user = users(:public_user)
1840 get :feed, :format => "atom", :display_name => user.display_name
1841 assert_response :success
1842 assert_template "list"
1843 assert_equal "application/atom+xml", response.content_type
1844 ## FIXME need to add more checks to see which if edits are actually shown if your data is public
1848 # Check the not found of the user changesets feed
1849 def test_feed_user_not_found
1850 get :feed, :format => "atom", :display_name => "Some random user"
1851 assert_response :not_found
1855 # check that the changeset download for a changeset with a redacted
1856 # element in it doesn't contain that element.
1857 def test_diff_download_redacted
1858 changeset_id = changesets(:public_user_first_change).id
1860 get :download, :id => changeset_id
1861 assert_response :success
1863 assert_select "osmChange", 1
1864 # this changeset contains node 17 in versions 1 & 2, but 1 should
1866 assert_select "osmChange node[id='17']", 1
1867 assert_select "osmChange node[id='17'][version='1']", 0
1871 # create comment success
1872 def test_create_comment_success
1873 basic_authorization(users(:public_user).email, 'test')
1875 assert_difference('ChangesetComment.count') do
1876 post :comment, :id => changesets(:normal_user_closed_change).id, :text => 'This is a comment'
1878 assert_response :success
1882 # create comment fail
1883 def test_create_comment_fail
1885 post :comment, :id => changesets(:normal_user_closed_change).id, :text => 'This is a comment'
1886 assert_response :unauthorized
1888 basic_authorization(users(:public_user).email, 'test')
1891 assert_no_difference('ChangesetComment.count') do
1892 post :comment, :id => 999111, :text => 'This is a comment'
1894 assert_response :not_found
1896 # not closed changeset
1897 assert_no_difference('ChangesetComment.count') do
1898 post :comment, :id => changesets(:normal_user_first_change).id, :text => 'This is a comment'
1900 assert_response :conflict
1903 assert_no_difference('ChangesetComment.count') do
1904 post :comment, :id => changesets(:normal_user_closed_change).id
1906 assert_response :bad_request
1909 assert_no_difference('ChangesetComment.count') do
1910 post :comment, :id => changesets(:normal_user_closed_change).id, :text => ''
1912 assert_response :bad_request
1916 # test subscribe success
1917 def test_subscribe_success
1918 basic_authorization(users(:public_user).email, 'test')
1919 changeset = changesets(:normal_user_closed_change)
1921 assert_difference('changeset.subscribers.count') do
1922 post :subscribe, :id => changeset.id
1924 assert_response :success
1928 # test subscribe fail
1929 def test_subscribe_fail
1931 changeset = changesets(:normal_user_closed_change)
1932 assert_no_difference('changeset.subscribers.count') do
1933 post :subscribe, :id => changeset.id
1935 assert_response :unauthorized
1937 basic_authorization(users(:public_user).email, 'test')
1940 assert_no_difference('changeset.subscribers.count') do
1941 post :subscribe, :id => 999111
1943 assert_response :not_found
1945 # not closed changeset
1946 changeset = changesets(:normal_user_first_change)
1947 assert_no_difference('changeset.subscribers.count') do
1948 post :subscribe, :id => changeset.id
1950 assert_response :conflict
1952 # trying to subscribe when already subscribed
1953 changeset = changesets(:normal_user_subscribed_change)
1954 assert_no_difference('changeset.subscribers.count') do
1955 post :subscribe, :id => changeset.id
1957 assert_response :conflict
1961 # test unsubscribe success
1962 def test_unsubscribe_success
1963 basic_authorization(users(:public_user).email, 'test')
1964 changeset = changesets(:normal_user_subscribed_change)
1966 assert_difference('changeset.subscribers.count', -1) do
1967 post :unsubscribe, :id => changeset.id
1969 assert_response :success
1973 # test unsubscribe fail
1974 def test_unsubscribe_fail
1976 changeset = changesets(:normal_user_closed_change)
1977 assert_no_difference('changeset.subscribers.count') do
1978 post :unsubscribe, :id => changeset.id
1980 assert_response :unauthorized
1982 basic_authorization(users(:public_user).email, 'test')
1985 assert_no_difference('changeset.subscribers.count', -1) do
1986 post :unsubscribe, :id => 999111
1988 assert_response :not_found
1990 # not closed changeset
1991 changeset = changesets(:normal_user_first_change)
1992 assert_no_difference('changeset.subscribers.count', -1) do
1993 post :unsubscribe, :id => changeset.id
1995 assert_response :conflict
1997 # trying to unsubscribe when not subscribed
1998 changeset = changesets(:normal_user_closed_change)
1999 assert_no_difference('changeset.subscribers.count') do
2000 post :unsubscribe, :id => changeset.id
2002 assert_response :not_found
2006 # test hide comment fail
2007 def test_hide_comment_fail
2009 comment = changeset_comments(:normal_comment_1)
2010 assert('comment.visible') do
2011 post :hide_comment, :id => comment.id
2012 assert_response :unauthorized
2015 basic_authorization(users(:public_user).email, 'test')
2018 assert('comment.visible') do
2019 post :hide_comment, :id => comment.id
2020 assert_response :forbidden
2023 basic_authorization(users(:moderator_user).email, 'test')
2026 post :hide_comment, :id => 999111
2027 assert_response :not_found
2031 # test hide comment succes
2032 def test_hide_comment_success
2033 comment = changeset_comments(:normal_comment_1)
2035 basic_authorization(users(:moderator_user).email, 'test')
2037 assert('!comment.visible') do
2038 post :hide_comment, :id => comment.id
2040 assert_response :success
2044 # test unhide comment fail
2045 def test_unhide_comment_fail
2047 comment = changeset_comments(:normal_comment_1)
2048 assert('comment.visible') do
2049 post :unhide_comment, :id => comment.id
2050 assert_response :unauthorized
2053 basic_authorization(users(:public_user).email, 'test')
2056 assert('comment.visible') do
2057 post :unhide_comment, :id => comment.id
2058 assert_response :forbidden
2061 basic_authorization(users(:moderator_user).email, 'test')
2064 post :unhide_comment, :id => 999111
2065 assert_response :not_found
2069 # test unhide comment succes
2070 def test_unhide_comment_success
2071 comment = changeset_comments(:normal_comment_1)
2073 basic_authorization(users(:moderator_user).email, 'test')
2075 assert('!comment.visible') do
2076 post :unhide_comment, :id => comment.id
2078 assert_response :success
2082 # test comments feed
2083 def test_comments_feed
2084 get :comments_feed, :format => "rss"
2085 assert_response :success
2086 assert_equal "application/rss+xml", @response.content_type
2087 assert_select "rss", :count => 1 do
2088 assert_select "channel", :count => 1 do
2089 assert_select "item", :count => 3
2093 get :comments_feed, :id => changesets(:normal_user_closed_change), :format => "rss"
2094 assert_response :success
2095 assert_equal "application/rss+xml", @response.content_type
2096 assert_select "rss", :count => 1 do
2097 assert_select "channel", :count => 1 do
2098 assert_select "item", :count => 3
2103 #------------------------------------------------------------
2105 #------------------------------------------------------------
2108 # boilerplate for checking that certain changesets exist in the
2110 def assert_changesets(ids)
2111 assert_select "osm>changeset", ids.size
2113 assert_select "osm>changeset[id='#{id}']", 1
2118 # call the include method and assert properties of the bbox
2119 def check_after_include(changeset_id, lon, lat, bbox)
2120 content "<osm><node lon='#{lon}' lat='#{lat}'/></osm>"
2121 post :expand_bbox, :id => changeset_id
2122 assert_response :success, "Setting include of changeset failed: #{@response.body}"
2124 # check exactly one changeset
2125 assert_select "osm>changeset", 1
2126 assert_select "osm>changeset[id='#{changeset_id}']", 1
2129 doc = XML::Parser.string(@response.body).parse
2130 changeset = doc.find("//osm/changeset").first
2131 assert_equal bbox[0], changeset['min_lon'].to_f, "min lon"
2132 assert_equal bbox[1], changeset['min_lat'].to_f, "min lat"
2133 assert_equal bbox[2], changeset['max_lon'].to_f, "max lon"
2134 assert_equal bbox[3], changeset['max_lat'].to_f, "max lat"
2138 # update the changeset_id of a way element
2139 def update_changeset(xml, changeset_id)
2140 xml_attr_rewrite(xml, 'changeset', changeset_id)
2144 # update an attribute in a way element
2145 def xml_attr_rewrite(xml, name, value)
2146 xml.find("//osm/way").first[name] = value.to_s