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
111 basic_authorization users(:public_user).email, "test"
112 # Create the first user's changeset
113 content "<osm><changeset>" +
114 "<tag k='created_by' v='osm test suite checking changesets'/>" +
118 assert_response :success, "Creation of changeset did not return sucess status"
119 newid = @response.body.to_i
121 # check end time, should be an hour ahead of creation time
122 cs = Changeset.find(newid)
123 duration = cs.closed_at - cs.created_at
124 # the difference can either be a rational, or a floating point number
125 # of seconds, depending on the code path taken :-(
126 if duration.class == Rational
127 assert_equal Rational(1,24), duration , "initial idle timeout should be an hour (#{cs.created_at} -> #{cs.closed_at})"
129 # must be number of seconds...
130 assert_equal 3600, duration.round, "initial idle timeout should be an hour (#{cs.created_at} -> #{cs.closed_at})"
133 # checks if uploader was subscribed
134 assert_equal 1, cs.subscribers.length
137 def test_create_invalid
138 basic_authorization users(:normal_user).email, "test"
139 content "<osm><changeset></osm>"
141 assert_require_public_data
143 ## Try the public user
144 basic_authorization users(:public_user).email, "test"
145 content "<osm><changeset></osm>"
147 assert_response :bad_request, "creating a invalid changeset should fail"
150 def test_create_invalid_no_content
151 ## First check with no auth
153 assert_response :unauthorized, "shouldn't be able to create a changeset with no auth"
155 ## Now try to with the non-public user
156 basic_authorization users(:normal_user).email, "test"
158 assert_require_public_data
160 ## Try the inactive user
161 basic_authorization users(:inactive_user).email, "test"
165 ## Now try to use the public user
166 basic_authorization users(:public_user).email, "test"
168 assert_response :bad_request, "creating a changeset with no content should fail"
171 def test_create_wrong_method
172 basic_authorization users(:public_user).email, "test"
174 assert_response :method_not_allowed
176 assert_response :method_not_allowed
180 # check that the changeset can be read and returns the correct
181 # document structure.
183 changeset_id = changesets(:normal_user_first_change).id
184 get :read, :id => changeset_id
185 assert_response :success, "cannot get first changeset"
187 assert_select "osm[version='#{API_VERSION}'][generator='OpenStreetMap server']", 1
188 assert_select "osm>changeset[id='#{changeset_id}']", 1
189 assert_select "osm>changeset>discussion", 0
191 get :read, :id => changeset_id, :include_discussion => true
192 assert_response :success, "cannot get first changeset with comments"
194 assert_select "osm[version='#{API_VERSION}'][generator='OpenStreetMap server']", 1
195 assert_select "osm>changeset[id='#{changeset_id}']", 1
196 assert_select "osm>changeset>discussion", 1
200 # check that a changeset that doesn't exist returns an appropriate message
201 def test_read_not_found
202 [0, -32, 233455644, "afg", "213"].each do |id|
205 assert_response :not_found, "should get a not found"
206 rescue ActionController::UrlGenerationError => ex
207 assert_match /No route matches/, ex.to_s
213 # test that the user who opened a change can close it
215 ## Try without authentication
216 put :close, :id => changesets(:public_user_first_change).id
217 assert_response :unauthorized
220 ## Try using the non-public user
221 basic_authorization users(:normal_user).email, "test"
222 put :close, :id => changesets(:normal_user_first_change).id
223 assert_require_public_data
226 ## The try with the public user
227 basic_authorization users(:public_user).email, "test"
229 cs_id = changesets(:public_user_first_change).id
230 put :close, :id => cs_id
231 assert_response :success
233 # test that it really is closed now
234 cs = Changeset.find(cs_id)
236 "changeset should be closed now (#{cs.closed_at} > #{Time.now.getutc}.")
240 # test that a different user can't close another user's changeset
241 def test_close_invalid
242 basic_authorization users(:public_user).email, "test"
244 put :close, :id => changesets(:normal_user_first_change).id
245 assert_response :conflict
246 assert_equal "The user doesn't own that changeset", @response.body
250 # test that you can't close using another method
251 def test_close_method_invalid
252 basic_authorization users(:public_user).email, "test"
254 cs_id = changesets(:public_user_first_change).id
255 get :close, :id => cs_id
256 assert_response :method_not_allowed
258 post :close, :id => cs_id
259 assert_response :method_not_allowed
263 # check that you can't close a changeset that isn't found
264 def test_close_not_found
265 cs_ids = [0, -132, "123"]
267 # First try to do it with no auth
270 put :close, :id => id
271 assert_response :unauthorized, "Shouldn't be able close the non-existant changeset #{id}, when not authorized"
272 rescue ActionController::UrlGenerationError => ex
273 assert_match /No route matches/, ex.to_s
278 basic_authorization users(:public_user).email, "test"
281 put :close, :id => id
282 assert_response :not_found, "The changeset #{id} doesn't exist, so can't be closed"
283 rescue ActionController::UrlGenerationError => ex
284 assert_match /No route matches/, ex.to_s
290 # upload something simple, but valid and check that it can
292 # Also try without auth and another user.
293 def test_upload_simple_valid
295 changeset_id = changesets(:public_user_first_change).id
297 # simple diff to change a node, way and relation by removing
302 <node id='1' lon='0' lat='0' changeset='#{changeset_id}' version='1'/>
303 <way id='1' changeset='#{changeset_id}' version='1'>
308 <relation id='1' changeset='#{changeset_id}' version='1'>
309 <member type='way' role='some' ref='3'/>
310 <member type='node' role='some' ref='5'/>
311 <member type='relation' role='some' ref='3'/>
319 post :upload, :id => changeset_id
320 assert_response :unauthorized,
321 "shouldnn't be able to upload a simple valid diff to changeset: #{@response.body}"
325 ## Now try with a private user
326 basic_authorization users(:normal_user).email, "test"
327 changeset_id = changesets(:normal_user_first_change).id
329 # simple diff to change a node, way and relation by removing
334 <node id='1' lon='0' lat='0' changeset='#{changeset_id}' version='1'/>
335 <way id='1' changeset='#{changeset_id}' version='1'>
340 <relation id='1' changeset='#{changeset_id}' version='1'>
341 <member type='way' role='some' ref='3'/>
342 <member type='node' role='some' ref='5'/>
343 <member type='relation' role='some' ref='3'/>
351 post :upload, :id => changeset_id
352 assert_response :forbidden,
353 "can't upload a simple valid diff to changeset: #{@response.body}"
357 ## Now try with the public user
358 basic_authorization users(:public_user).email, "test"
359 changeset_id = changesets(:public_user_first_change).id
361 # simple diff to change a node, way and relation by removing
366 <node id='1' lon='0' lat='0' changeset='#{changeset_id}' version='1'/>
367 <way id='1' changeset='#{changeset_id}' version='1'>
372 <relation id='1' changeset='#{changeset_id}' version='1'>
373 <member type='way' role='some' ref='3'/>
374 <member type='node' role='some' ref='5'/>
375 <member type='relation' role='some' ref='3'/>
383 post :upload, :id => changeset_id
384 assert_response :success,
385 "can't upload a simple valid diff to changeset: #{@response.body}"
387 # check that the changes made it into the database
388 assert_equal 0, Node.find(1).tags.size, "node 1 should now have no tags"
389 assert_equal 0, Way.find(1).tags.size, "way 1 should now have no tags"
390 assert_equal 0, Relation.find(1).tags.size, "relation 1 should now have no tags"
394 # upload something which creates new objects using placeholders
395 def test_upload_create_valid
396 basic_authorization users(:public_user).email, "test"
397 cs_id = changesets(:public_user_first_change).id
399 # simple diff to create a node way and relation using placeholders
403 <node id='-1' lon='0' lat='0' changeset='#{cs_id}'>
404 <tag k='foo' v='bar'/>
405 <tag k='baz' v='bat'/>
407 <way id='-1' changeset='#{cs_id}'>
412 <relation id='-1' changeset='#{cs_id}'>
413 <member type='way' role='some' ref='3'/>
414 <member type='node' role='some' ref='5'/>
415 <member type='relation' role='some' ref='3'/>
423 post :upload, :id => cs_id
424 assert_response :success,
425 "can't upload a simple valid creation to changeset: #{@response.body}"
427 # check the returned payload
428 assert_select "diffResult[version=#{API_VERSION}][generator=\"OpenStreetMap server\"]", 1
429 assert_select "diffResult>node", 1
430 assert_select "diffresult>way", 1
431 assert_select "diffResult>relation", 1
433 # inspect the response to find out what the new element IDs are
434 doc = XML::Parser.string(@response.body).parse
435 new_node_id = doc.find("//diffResult/node").first["new_id"].to_i
436 new_way_id = doc.find("//diffResult/way").first["new_id"].to_i
437 new_rel_id = doc.find("//diffResult/relation").first["new_id"].to_i
439 # check the old IDs are all present and negative one
440 assert_equal -1, doc.find("//diffResult/node").first["old_id"].to_i
441 assert_equal -1, doc.find("//diffResult/way").first["old_id"].to_i
442 assert_equal -1, doc.find("//diffResult/relation").first["old_id"].to_i
444 # check the versions are present and equal one
445 assert_equal 1, doc.find("//diffResult/node").first["new_version"].to_i
446 assert_equal 1, doc.find("//diffResult/way").first["new_version"].to_i
447 assert_equal 1, doc.find("//diffResult/relation").first["new_version"].to_i
449 # check that the changes made it into the database
450 assert_equal 2, Node.find(new_node_id).tags.size, "new node should have two tags"
451 assert_equal 0, Way.find(new_way_id).tags.size, "new way should have no tags"
452 assert_equal 0, Relation.find(new_rel_id).tags.size, "new relation should have no tags"
456 # test a complex delete where we delete elements which rely on eachother
457 # in the same transaction.
458 def test_upload_delete
459 basic_authorization users(:public_user).display_name, "test"
461 diff = XML::Document.new
462 diff.root = XML::Node.new "osmChange"
463 delete = XML::Node.new "delete"
465 delete << current_relations(:visible_relation).to_xml_node
466 delete << current_relations(:used_relation).to_xml_node
467 delete << current_ways(:used_way).to_xml_node
468 delete << current_nodes(:node_used_by_relationship).to_xml_node
470 # update the changeset to one that this user owns
471 changeset_id = changesets(:public_user_first_change).id
472 ["node", "way", "relation"].each do |type|
473 delete.find("//osmChange/delete/#{type}").each do |n|
474 n['changeset'] = changeset_id.to_s
480 post :upload, :id => changeset_id
481 assert_response :success,
482 "can't upload a deletion diff to changeset: #{@response.body}"
484 # check the response is well-formed
485 assert_select "diffResult>node", 1
486 assert_select "diffResult>way", 1
487 assert_select "diffResult>relation", 2
489 # check that everything was deleted
490 assert_equal false, Node.find(current_nodes(:node_used_by_relationship).id).visible
491 assert_equal false, Way.find(current_ways(:used_way).id).visible
492 assert_equal false, Relation.find(current_relations(:visible_relation).id).visible
493 assert_equal false, Relation.find(current_relations(:used_relation).id).visible
497 # test uploading a delete with no lat/lon, as they are optional in
498 # the osmChange spec.
499 def test_upload_nolatlon_delete
500 basic_authorization users(:public_user).display_name, "test"
502 node = current_nodes(:public_visible_node)
503 cs = changesets(:public_user_first_change)
504 diff = "<osmChange><delete><node id='#{node.id}' version='#{node.version}' changeset='#{cs.id}'/></delete></osmChange>"
508 post :upload, :id => cs.id
509 assert_response :success,
510 "can't upload a deletion diff to changeset: #{@response.body}"
512 # check the response is well-formed
513 assert_select "diffResult>node", 1
515 # check that everything was deleted
516 assert_equal false, Node.find(node.id).visible
519 def test_repeated_changeset_create
521 basic_authorization users(:public_user).email, "test"
523 # create a temporary changeset
524 content "<osm><changeset>" +
525 "<tag k='created_by' v='osm test suite checking changesets'/>" +
527 assert_difference('Changeset.count', 1) do
530 assert_response :success
531 changeset_id = @response.body.to_i
535 def test_upload_large_changeset
536 basic_authorization users(:public_user).email, "test"
539 content "<osm><changeset/></osm>"
541 assert_response :success, "Should be able to create a changeset: #{@response.body}"
542 changeset_id = @response.body.to_i
544 # upload some widely-spaced nodes, spiralling positive and negative to cause
545 # largest bbox over-expansion possible.
549 <node id='-1' lon='-20' lat='-10' changeset='#{changeset_id}'/>
550 <node id='-10' lon='20' lat='10' changeset='#{changeset_id}'/>
551 <node id='-2' lon='-40' lat='-20' changeset='#{changeset_id}'/>
552 <node id='-11' lon='40' lat='20' changeset='#{changeset_id}'/>
553 <node id='-3' lon='-60' lat='-30' changeset='#{changeset_id}'/>
554 <node id='-12' lon='60' lat='30' changeset='#{changeset_id}'/>
555 <node id='-4' lon='-80' lat='-40' changeset='#{changeset_id}'/>
556 <node id='-13' lon='80' lat='40' changeset='#{changeset_id}'/>
557 <node id='-5' lon='-100' lat='-50' changeset='#{changeset_id}'/>
558 <node id='-14' lon='100' lat='50' changeset='#{changeset_id}'/>
559 <node id='-6' lon='-120' lat='-60' changeset='#{changeset_id}'/>
560 <node id='-15' lon='120' lat='60' changeset='#{changeset_id}'/>
561 <node id='-7' lon='-140' lat='-70' changeset='#{changeset_id}'/>
562 <node id='-16' lon='140' lat='70' changeset='#{changeset_id}'/>
563 <node id='-8' lon='-160' lat='-80' changeset='#{changeset_id}'/>
564 <node id='-17' lon='160' lat='80' changeset='#{changeset_id}'/>
565 <node id='-9' lon='-179.9' lat='-89.9' changeset='#{changeset_id}'/>
566 <node id='-18' lon='179.9' lat='89.9' changeset='#{changeset_id}'/>
571 # upload it, which used to cause an error like "PGError: ERROR:
572 # integer out of range" (bug #2152). but shouldn't any more.
574 post :upload, :id => changeset_id
575 assert_response :success,
576 "can't upload a spatially-large diff to changeset: #{@response.body}"
578 # check that the changeset bbox is within bounds
579 cs = Changeset.find(changeset_id)
580 assert cs.min_lon >= -180 * GeoRecord::SCALE, "Minimum longitude (#{cs.min_lon / GeoRecord::SCALE}) should be >= -180 to be valid."
581 assert cs.max_lon <= 180 * GeoRecord::SCALE, "Maximum longitude (#{cs.max_lon / GeoRecord::SCALE}) should be <= 180 to be valid."
582 assert cs.min_lat >= -90 * GeoRecord::SCALE, "Minimum latitude (#{cs.min_lat / GeoRecord::SCALE}) should be >= -90 to be valid."
583 assert cs.max_lat >= 90 * GeoRecord::SCALE, "Maximum latitude (#{cs.max_lat / GeoRecord::SCALE}) should be <= 90 to be valid."
587 # test that deleting stuff in a transaction doesn't bypass the checks
588 # to ensure that used elements are not deleted.
589 def test_upload_delete_invalid
590 basic_authorization users(:public_user).email, "test"
592 diff = XML::Document.new
593 diff.root = XML::Node.new "osmChange"
594 delete = XML::Node.new "delete"
596 delete << current_relations(:public_visible_relation).to_xml_node
597 delete << current_ways(:used_way).to_xml_node
598 delete << current_nodes(:node_used_by_relationship).to_xml_node
602 post :upload, :id => 2
603 assert_response :precondition_failed,
604 "shouldn't be able to upload a invalid deletion diff: #{@response.body}"
605 assert_equal "Precondition failed: Way 3 is still used by relations 1.", @response.body
607 # check that nothing was, in fact, deleted
608 assert_equal true, Node.find(current_nodes(:node_used_by_relationship).id).visible
609 assert_equal true, Way.find(current_ways(:used_way).id).visible
610 assert_equal true, Relation.find(current_relations(:visible_relation).id).visible
614 # test that a conditional delete of an in use object works.
615 def test_upload_delete_if_unused
616 basic_authorization users(:public_user).email, "test"
618 diff = XML::Document.new
619 diff.root = XML::Node.new "osmChange"
620 delete = XML::Node.new "delete"
622 delete["if-unused"] = ""
623 delete << current_relations(:public_used_relation).to_xml_node
624 delete << current_ways(:used_way).to_xml_node
625 delete << current_nodes(:node_used_by_relationship).to_xml_node
629 post :upload, :id => 2
630 assert_response :success,
631 "can't do a conditional delete of in use objects: #{@response.body}"
633 # check the returned payload
634 assert_select "diffResult[version='#{API_VERSION}'][generator='OpenStreetMap server']", 1
635 assert_select "diffResult>node", 1
636 assert_select "diffresult>way", 1
637 assert_select "diffResult>relation", 1
640 doc = XML::Parser.string(@response.body).parse
642 # check the old IDs are all present and what we expect
643 assert_equal current_nodes(:node_used_by_relationship).id, doc.find("//diffResult/node").first["old_id"].to_i
644 assert_equal current_ways(:used_way).id, doc.find("//diffResult/way").first["old_id"].to_i
645 assert_equal current_relations(:public_used_relation).id, doc.find("//diffResult/relation").first["old_id"].to_i
647 # check the new IDs are all present and unchanged
648 assert_equal current_nodes(:node_used_by_relationship).id, doc.find("//diffResult/node").first["new_id"].to_i
649 assert_equal current_ways(:used_way).id, doc.find("//diffResult/way").first["new_id"].to_i
650 assert_equal current_relations(:public_used_relation).id, doc.find("//diffResult/relation").first["new_id"].to_i
652 # check the new versions are all present and unchanged
653 assert_equal current_nodes(:node_used_by_relationship).version, doc.find("//diffResult/node").first["new_version"].to_i
654 assert_equal current_ways(:used_way).version, doc.find("//diffResult/way").first["new_version"].to_i
655 assert_equal current_relations(:public_used_relation).version, doc.find("//diffResult/relation").first["new_version"].to_i
657 # check that nothing was, in fact, deleted
658 assert_equal true, Node.find(current_nodes(:node_used_by_relationship).id).visible
659 assert_equal true, Way.find(current_ways(:used_way).id).visible
660 assert_equal true, Relation.find(current_relations(:public_used_relation).id).visible
664 # upload an element with a really long tag value
665 def test_upload_invalid_too_long_tag
666 basic_authorization users(:public_user).email, "test"
667 cs_id = changesets(:public_user_first_change).id
669 # simple diff to create a node way and relation using placeholders
673 <node id='-1' lon='0' lat='0' changeset='#{cs_id}'>
674 <tag k='foo' v='#{"x"*256}'/>
682 post :upload, :id => cs_id
683 assert_response :bad_request,
684 "shoudln't be able to upload too long a tag to changeset: #{@response.body}"
689 # upload something which creates new objects and inserts them into
690 # existing containers using placeholders.
691 def test_upload_complex
692 basic_authorization users(:public_user).email, "test"
693 cs_id = changesets(:public_user_first_change).id
695 # simple diff to create a node way and relation using placeholders
699 <node id='-1' lon='0' lat='0' changeset='#{cs_id}'>
700 <tag k='foo' v='bar'/>
701 <tag k='baz' v='bat'/>
705 <way id='1' changeset='#{cs_id}' version='1'>
709 <relation id='1' changeset='#{cs_id}' version='1'>
710 <member type='way' role='some' ref='3'/>
711 <member type='node' role='some' ref='-1'/>
712 <member type='relation' role='some' ref='3'/>
720 post :upload, :id => cs_id
721 assert_response :success,
722 "can't upload a complex diff to changeset: #{@response.body}"
724 # check the returned payload
725 assert_select "diffResult[version=#{API_VERSION}][generator=\"#{GENERATOR}\"]", 1
726 assert_select "diffResult>node", 1
727 assert_select "diffResult>way", 1
728 assert_select "diffResult>relation", 1
730 # inspect the response to find out what the new element IDs are
731 doc = XML::Parser.string(@response.body).parse
732 new_node_id = doc.find("//diffResult/node").first["new_id"].to_i
734 # check that the changes made it into the database
735 assert_equal 2, Node.find(new_node_id).tags.size, "new node should have two tags"
736 assert_equal [new_node_id, 3], Way.find(1).nds, "way nodes should match"
737 Relation.find(1).members.each do |type,id,role|
739 assert_equal new_node_id, id, "relation should contain new node"
745 # create a diff which references several changesets, which should cause
746 # a rollback and none of the diff gets committed
747 def test_upload_invalid_changesets
748 basic_authorization users(:public_user).email, "test"
749 cs_id = changesets(:public_user_first_change).id
751 # simple diff to create a node way and relation using placeholders
755 <node id='1' lon='0' lat='0' changeset='#{cs_id}' version='1'/>
756 <way id='1' changeset='#{cs_id}' version='1'>
761 <relation id='1' changeset='#{cs_id}' version='1'>
762 <member type='way' role='some' ref='3'/>
763 <member type='node' role='some' ref='5'/>
764 <member type='relation' role='some' ref='3'/>
768 <node id='-1' lon='0' lat='0' changeset='4'>
769 <tag k='foo' v='bar'/>
770 <tag k='baz' v='bat'/>
775 # cache the objects before uploading them
776 node = current_nodes(:visible_node)
777 way = current_ways(:visible_way)
778 rel = current_relations(:visible_relation)
782 post :upload, :id => cs_id
783 assert_response :conflict,
784 "uploading a diff with multiple changsets should have failed"
786 # check that objects are unmodified
787 assert_nodes_are_equal(node, Node.find(1))
788 assert_ways_are_equal(way, Way.find(1))
792 # upload multiple versions of the same element in the same diff.
793 def test_upload_multiple_valid
794 basic_authorization users(:public_user).email, "test"
795 cs_id = changesets(:public_user_first_change).id
797 # change the location of a node multiple times, each time referencing
798 # the last version. doesn't this depend on version numbers being
803 <node id='1' lon='0' lat='0' changeset='#{cs_id}' version='1'/>
804 <node id='1' lon='1' lat='0' changeset='#{cs_id}' version='2'/>
805 <node id='1' lon='1' lat='1' changeset='#{cs_id}' version='3'/>
806 <node id='1' lon='1' lat='2' changeset='#{cs_id}' version='4'/>
807 <node id='1' lon='2' lat='2' changeset='#{cs_id}' version='5'/>
808 <node id='1' lon='3' lat='2' changeset='#{cs_id}' version='6'/>
809 <node id='1' lon='3' lat='3' changeset='#{cs_id}' version='7'/>
810 <node id='1' lon='9' lat='9' changeset='#{cs_id}' version='8'/>
817 post :upload, :id => cs_id
818 assert_response :success,
819 "can't upload multiple versions of an element in a diff: #{@response.body}"
821 # check the response is well-formed. its counter-intuitive, but the
822 # API will return multiple elements with the same ID and different
823 # version numbers for each change we made.
824 assert_select "diffResult>node", 8
828 # upload multiple versions of the same element in the same diff, but
829 # keep the version numbers the same.
830 def test_upload_multiple_duplicate
831 basic_authorization users(:public_user).email, "test"
832 cs_id = changesets(:public_user_first_change).id
837 <node id='1' lon='0' lat='0' changeset='#{cs_id}' version='1'/>
838 <node id='1' lon='1' lat='1' changeset='#{cs_id}' version='1'/>
845 post :upload, :id => cs_id
846 assert_response :conflict,
847 "shouldn't be able to upload the same element twice in a diff: #{@response.body}"
851 # try to upload some elements without specifying the version
852 def test_upload_missing_version
853 basic_authorization users(:public_user).email, "test"
854 cs_id = changesets(:public_user_first_change).id
859 <node id='1' lon='1' lat='1' changeset='cs_id'/>
866 post :upload, :id => cs_id
867 assert_response :bad_request,
868 "shouldn't be able to upload an element without version: #{@response.body}"
872 # try to upload with commands other than create, modify, or delete
873 def test_action_upload_invalid
874 basic_authorization users(:public_user).email, "test"
875 cs_id = changesets(:public_user_first_change).id
880 <node id='1' lon='1' lat='1' changeset='#{cs_id}' />
885 post :upload, :id => cs_id
886 assert_response :bad_request, "Shouldn't be able to upload a diff with the action ping"
887 assert_equal @response.body, "Unknown action ping, choices are create, modify, delete"
891 # upload a valid changeset which has a mixture of whitespace
892 # to check a bug reported by ivansanchez (#1565).
893 def test_upload_whitespace_valid
894 basic_authorization users(:public_user).email, "test"
895 changeset_id = changesets(:public_user_first_change).id
899 <modify><node id='1' lon='0' lat='0' changeset='#{changeset_id}'
901 <node id='1' lon='1' lat='1' changeset='#{changeset_id}' version='2'><tag k='k' v='v'/></node></modify>
903 <relation id='1' changeset='#{changeset_id}' version='1'><member
904 type='way' role='some' ref='3'/><member
905 type='node' role='some' ref='5'/>
906 <member type='relation' role='some' ref='3'/>
908 </modify></osmChange>
913 post :upload, :id => changeset_id
914 assert_response :success,
915 "can't upload a valid diff with whitespace variations to changeset: #{@response.body}"
917 # check the response is well-formed
918 assert_select "diffResult>node", 2
919 assert_select "diffResult>relation", 1
921 # check that the changes made it into the database
922 assert_equal 1, Node.find(1).tags.size, "node 1 should now have one tag"
923 assert_equal 0, Relation.find(1).tags.size, "relation 1 should now have no tags"
927 # upload a valid changeset which has a mixture of whitespace
928 # to check a bug reported by ivansanchez.
929 def test_upload_reuse_placeholder_valid
930 basic_authorization users(:public_user).email, "test"
931 changeset_id = changesets(:public_user_first_change).id
936 <node id='-1' lon='0' lat='0' changeset='#{changeset_id}'>
937 <tag k="foo" v="bar"/>
941 <node id='-1' lon='1' lat='1' changeset='#{changeset_id}' version='1'/>
944 <node id='-1' lon='2' lat='2' changeset='#{changeset_id}' version='2'/>
951 post :upload, :id => changeset_id
952 assert_response :success,
953 "can't upload a valid diff with re-used placeholders to changeset: #{@response.body}"
955 # check the response is well-formed
956 assert_select "diffResult>node", 3
957 assert_select "diffResult>node[old_id=-1]", 3
961 # test what happens if a diff upload re-uses placeholder IDs in an
963 def test_upload_placeholder_invalid
964 basic_authorization users(:public_user).email, "test"
965 changeset_id = changesets(:public_user_first_change).id
970 <node id='-1' lon='0' lat='0' changeset='#{changeset_id}' version='1'/>
971 <node id='-1' lon='1' lat='1' changeset='#{changeset_id}' version='1'/>
972 <node id='-1' lon='2' lat='2' changeset='#{changeset_id}' version='2'/>
979 post :upload, :id => changeset_id
980 assert_response :bad_request,
981 "shouldn't be able to re-use placeholder IDs"
985 # test that uploading a way referencing invalid placeholders gives a
986 # proper error, not a 500.
987 def test_upload_placeholder_invalid_way
988 basic_authorization users(:public_user).email, "test"
989 changeset_id = changesets(:public_user_first_change).id
994 <node id="-1" lon="0" lat="0" changeset="#{changeset_id}" version="1"/>
995 <node id="-2" lon="1" lat="1" changeset="#{changeset_id}" version="1"/>
996 <node id="-3" lon="2" lat="2" changeset="#{changeset_id}" version="1"/>
997 <way id="-1" changeset="#{changeset_id}" version="1">
1009 post :upload, :id => changeset_id
1010 assert_response :bad_request,
1011 "shouldn't be able to use invalid placeholder IDs"
1012 assert_equal "Placeholder node not found for reference -4 in way -1", @response.body
1014 # the same again, but this time use an existing way
1018 <node id="-1" lon="0" lat="0" changeset="#{changeset_id}" version="1"/>
1019 <node id="-2" lon="1" lat="1" changeset="#{changeset_id}" version="1"/>
1020 <node id="-3" lon="2" lat="2" changeset="#{changeset_id}" version="1"/>
1021 <way id="1" changeset="#{changeset_id}" version="1">
1033 post :upload, :id => changeset_id
1034 assert_response :bad_request,
1035 "shouldn't be able to use invalid placeholder IDs"
1036 assert_equal "Placeholder node not found for reference -4 in way 1", @response.body
1040 # test that uploading a relation referencing invalid placeholders gives a
1041 # proper error, not a 500.
1042 def test_upload_placeholder_invalid_relation
1043 basic_authorization users(:public_user).email, "test"
1044 changeset_id = changesets(:public_user_first_change).id
1049 <node id="-1" lon="0" lat="0" changeset="#{changeset_id}" version="1"/>
1050 <node id="-2" lon="1" lat="1" changeset="#{changeset_id}" version="1"/>
1051 <node id="-3" lon="2" lat="2" changeset="#{changeset_id}" version="1"/>
1052 <relation id="-1" changeset="#{changeset_id}" version="1">
1053 <member type="node" role="foo" ref="-1"/>
1054 <member type="node" role="foo" ref="-2"/>
1055 <member type="node" role="foo" ref="-3"/>
1056 <member type="node" role="foo" ref="-4"/>
1064 post :upload, :id => changeset_id
1065 assert_response :bad_request,
1066 "shouldn't be able to use invalid placeholder IDs"
1067 assert_equal "Placeholder Node not found for reference -4 in relation -1.", @response.body
1069 # the same again, but this time use an existing way
1073 <node id="-1" lon="0" lat="0" changeset="#{changeset_id}" version="1"/>
1074 <node id="-2" lon="1" lat="1" changeset="#{changeset_id}" version="1"/>
1075 <node id="-3" lon="2" lat="2" changeset="#{changeset_id}" version="1"/>
1076 <relation id="1" changeset="#{changeset_id}" version="1">
1077 <member type="node" role="foo" ref="-1"/>
1078 <member type="node" role="foo" ref="-2"/>
1079 <member type="node" role="foo" ref="-3"/>
1080 <member type="way" role="bar" ref="-1"/>
1088 post :upload, :id => changeset_id
1089 assert_response :bad_request,
1090 "shouldn't be able to use invalid placeholder IDs"
1091 assert_equal "Placeholder Way not found for reference -1 in relation 1.", @response.body
1095 # test what happens if a diff is uploaded containing only a node
1097 def test_upload_node_move
1098 basic_authorization users(:public_user).email, "test"
1100 content "<osm><changeset>" +
1101 "<tag k='created_by' v='osm test suite checking changesets'/>" +
1102 "</changeset></osm>"
1104 assert_response :success
1105 changeset_id = @response.body.to_i
1107 old_node = current_nodes(:visible_node)
1109 diff = XML::Document.new
1110 diff.root = XML::Node.new "osmChange"
1111 modify = XML::Node.new "modify"
1112 xml_old_node = old_node.to_xml_node
1113 xml_old_node["lat"] = (2.0).to_s
1114 xml_old_node["lon"] = (2.0).to_s
1115 xml_old_node["changeset"] = changeset_id.to_s
1116 modify << xml_old_node
1121 post :upload, :id => changeset_id
1122 assert_response :success,
1123 "diff should have uploaded OK"
1126 changeset = Changeset.find(changeset_id)
1127 assert_equal 1*GeoRecord::SCALE, changeset.min_lon, "min_lon should be 1 degree"
1128 assert_equal 2*GeoRecord::SCALE, changeset.max_lon, "max_lon should be 2 degrees"
1129 assert_equal 1*GeoRecord::SCALE, changeset.min_lat, "min_lat should be 1 degree"
1130 assert_equal 2*GeoRecord::SCALE, changeset.max_lat, "max_lat should be 2 degrees"
1134 # test what happens if a diff is uploaded adding a node to a way.
1135 def test_upload_way_extend
1136 basic_authorization users(:public_user).email, "test"
1138 content "<osm><changeset>" +
1139 "<tag k='created_by' v='osm test suite checking changesets'/>" +
1140 "</changeset></osm>"
1142 assert_response :success
1143 changeset_id = @response.body.to_i
1145 old_way = current_ways(:visible_way)
1147 diff = XML::Document.new
1148 diff.root = XML::Node.new "osmChange"
1149 modify = XML::Node.new "modify"
1150 xml_old_way = old_way.to_xml_node
1151 nd_ref = XML::Node.new "nd"
1152 nd_ref["ref"] = current_nodes(:visible_node).id.to_s
1153 xml_old_way << nd_ref
1154 xml_old_way["changeset"] = changeset_id.to_s
1155 modify << xml_old_way
1160 post :upload, :id => changeset_id
1161 assert_response :success,
1162 "diff should have uploaded OK"
1165 changeset = Changeset.find(changeset_id)
1166 assert_equal 1*GeoRecord::SCALE, changeset.min_lon, "min_lon should be 1 degree"
1167 assert_equal 3*GeoRecord::SCALE, changeset.max_lon, "max_lon should be 3 degrees"
1168 assert_equal 1*GeoRecord::SCALE, changeset.min_lat, "min_lat should be 1 degree"
1169 assert_equal 3*GeoRecord::SCALE, changeset.max_lat, "max_lat should be 3 degrees"
1173 # test for more issues in #1568
1174 def test_upload_empty_invalid
1175 basic_authorization users(:public_user).email, "test"
1178 "<osmChange></osmChange>",
1179 "<osmChange><modify/></osmChange>",
1180 "<osmChange><modify></modify></osmChange>"
1184 post :upload, :id => changesets(:public_user_first_change).id
1185 assert_response(:success, "should be able to upload " +
1186 "empty changeset: " + diff)
1191 # test that the X-Error-Format header works to request XML errors
1192 def test_upload_xml_errors
1193 basic_authorization users(:public_user).email, "test"
1195 # try and delete a node that is in use
1196 diff = XML::Document.new
1197 diff.root = XML::Node.new "osmChange"
1198 delete = XML::Node.new "delete"
1200 delete << current_nodes(:node_used_by_relationship).to_xml_node
1205 post :upload, :id => 2
1206 assert_response :success,
1207 "failed to return error in XML format"
1209 # check the returned payload
1210 assert_select "osmError[version='#{API_VERSION}'][generator='OpenStreetMap server']", 1
1211 assert_select "osmError>status", 1
1212 assert_select "osmError>message", 1
1217 # when we make some simple changes we get the same changes back from the
1219 def test_diff_download_simple
1220 ## First try with the normal user, which should get a forbidden
1221 basic_authorization(users(:normal_user).email, "test")
1223 # create a temporary changeset
1224 content "<osm><changeset>" +
1225 "<tag k='created_by' v='osm test suite checking changesets'/>" +
1226 "</changeset></osm>"
1228 assert_response :forbidden
1232 ## Now try with the public user
1233 basic_authorization(users(:public_user).email, "test")
1235 # create a temporary changeset
1236 content "<osm><changeset>" +
1237 "<tag k='created_by' v='osm test suite checking changesets'/>" +
1238 "</changeset></osm>"
1240 assert_response :success
1241 changeset_id = @response.body.to_i
1247 <node id='1' lon='0' lat='0' changeset='#{changeset_id}' version='1'/>
1248 <node id='1' lon='1' lat='0' changeset='#{changeset_id}' version='2'/>
1249 <node id='1' lon='1' lat='1' changeset='#{changeset_id}' version='3'/>
1250 <node id='1' lon='1' lat='2' changeset='#{changeset_id}' version='4'/>
1251 <node id='1' lon='2' lat='2' changeset='#{changeset_id}' version='5'/>
1252 <node id='1' lon='3' lat='2' changeset='#{changeset_id}' version='6'/>
1253 <node id='1' lon='3' lat='3' changeset='#{changeset_id}' version='7'/>
1254 <node id='1' lon='9' lat='9' changeset='#{changeset_id}' version='8'/>
1261 post :upload, :id => changeset_id
1262 assert_response :success,
1263 "can't upload multiple versions of an element in a diff: #{@response.body}"
1265 get :download, :id => changeset_id
1266 assert_response :success
1268 assert_select "osmChange", 1
1269 assert_select "osmChange>modify", 8
1270 assert_select "osmChange>modify>node", 8
1274 # culled this from josm to ensure that nothing in the way that josm
1275 # is formatting the request is causing it to fail.
1277 # NOTE: the error turned out to be something else completely!
1278 def test_josm_upload
1279 basic_authorization(users(:public_user).email, "test")
1281 # create a temporary changeset
1282 content "<osm><changeset>" +
1283 "<tag k='created_by' v='osm test suite checking changesets'/>" +
1284 "</changeset></osm>"
1286 assert_response :success
1287 changeset_id = @response.body.to_i
1290 <osmChange version="0.6" generator="JOSM">
1291 <create version="0.6" generator="JOSM">
1292 <node id='-1' visible='true' changeset='#{changeset_id}' lat='51.49619982187321' lon='-0.18722061869438314' />
1293 <node id='-2' visible='true' changeset='#{changeset_id}' lat='51.496359883909605' lon='-0.18653093576241928' />
1294 <node id='-3' visible='true' changeset='#{changeset_id}' lat='51.49598132358285' lon='-0.18719613290981638' />
1295 <node id='-4' visible='true' changeset='#{changeset_id}' lat='51.4961591711078' lon='-0.18629015888084607' />
1296 <node id='-5' visible='true' changeset='#{changeset_id}' lat='51.49582126021711' lon='-0.18708186591517145' />
1297 <node id='-6' visible='true' changeset='#{changeset_id}' lat='51.49591018437858' lon='-0.1861432441734455' />
1298 <node id='-7' visible='true' changeset='#{changeset_id}' lat='51.49560784152179' lon='-0.18694719410005425' />
1299 <node id='-8' visible='true' changeset='#{changeset_id}' lat='51.49567389979617' lon='-0.1860289771788006' />
1300 <node id='-9' visible='true' changeset='#{changeset_id}' lat='51.49543761398892' lon='-0.186820684213126' />
1301 <way id='-10' action='modiy' visible='true' changeset='#{changeset_id}'>
1311 <tag k='highway' v='residential' />
1312 <tag k='name' v='Foobar Street' />
1320 post :upload, :id => changeset_id
1321 assert_response :success,
1322 "can't upload a diff from JOSM: #{@response.body}"
1324 get :download, :id => changeset_id
1325 assert_response :success
1327 assert_select "osmChange", 1
1328 assert_select "osmChange>create>node", 9
1329 assert_select "osmChange>create>way", 1
1330 assert_select "osmChange>create>way>nd", 9
1331 assert_select "osmChange>create>way>tag", 2
1335 # when we make some complex changes we get the same changes back from the
1337 def test_diff_download_complex
1338 basic_authorization(users(:public_user).email, "test")
1340 # create a temporary changeset
1341 content "<osm><changeset>" +
1342 "<tag k='created_by' v='osm test suite checking changesets'/>" +
1343 "</changeset></osm>"
1345 assert_response :success
1346 changeset_id = @response.body.to_i
1352 <node id='1' lon='0' lat='0' changeset='#{changeset_id}' version='1'/>
1355 <node id='-1' lon='9' lat='9' changeset='#{changeset_id}' version='0'/>
1356 <node id='-2' lon='8' lat='9' changeset='#{changeset_id}' version='0'/>
1357 <node id='-3' lon='7' lat='9' changeset='#{changeset_id}' version='0'/>
1360 <node id='3' lon='20' lat='15' changeset='#{changeset_id}' version='1'/>
1361 <way id='1' changeset='#{changeset_id}' version='1'>
1373 post :upload, :id => changeset_id
1374 assert_response :success,
1375 "can't upload multiple versions of an element in a diff: #{@response.body}"
1377 get :download, :id => changeset_id
1378 assert_response :success
1380 assert_select "osmChange", 1
1381 assert_select "osmChange>create", 3
1382 assert_select "osmChange>delete", 1
1383 assert_select "osmChange>modify", 2
1384 assert_select "osmChange>create>node", 3
1385 assert_select "osmChange>delete>node", 1
1386 assert_select "osmChange>modify>node", 1
1387 assert_select "osmChange>modify>way", 1
1390 def test_changeset_download
1391 get :download, :id => changesets(:normal_user_first_change).id
1392 assert_response :success
1394 #print @response.body
1395 # FIXME needs more assert_select tests
1396 assert_select "osmChange[version='#{API_VERSION}'][generator='#{GENERATOR}']" do
1397 assert_select "create", :count => 5
1398 assert_select "create>node[id='#{nodes(:used_node_2).node_id}'][visible='#{nodes(:used_node_2).visible?}'][version='#{nodes(:used_node_2).version}']" do
1399 assert_select "tag[k='#{node_tags(:t3).k}'][v='#{node_tags(:t3).v}']"
1401 assert_select "create>node[id='#{nodes(:visible_node).node_id}']"
1406 # check that the bounding box of a changeset gets updated correctly
1407 ## FIXME: This should really be moded to a integration test due to the with_controller
1408 def test_changeset_bbox
1409 basic_authorization users(:public_user).email, "test"
1411 # create a new changeset
1412 content "<osm><changeset/></osm>"
1414 assert_response :success, "Creating of changeset failed."
1415 changeset_id = @response.body.to_i
1417 # add a single node to it
1418 with_controller(NodeController.new) do
1419 content "<osm><node lon='1' lat='2' changeset='#{changeset_id}'/></osm>"
1421 assert_response :success, "Couldn't create node."
1424 # get the bounding box back from the changeset
1425 get :read, :id => changeset_id
1426 assert_response :success, "Couldn't read back changeset."
1427 assert_select "osm>changeset[min_lon='1.0']", 1
1428 assert_select "osm>changeset[max_lon='1.0']", 1
1429 assert_select "osm>changeset[min_lat='2.0']", 1
1430 assert_select "osm>changeset[max_lat='2.0']", 1
1432 # add another node to it
1433 with_controller(NodeController.new) do
1434 content "<osm><node lon='2' lat='1' changeset='#{changeset_id}'/></osm>"
1436 assert_response :success, "Couldn't create second node."
1439 # get the bounding box back from the changeset
1440 get :read, :id => changeset_id
1441 assert_response :success, "Couldn't read back changeset for the second time."
1442 assert_select "osm>changeset[min_lon='1.0']", 1
1443 assert_select "osm>changeset[max_lon='2.0']", 1
1444 assert_select "osm>changeset[min_lat='1.0']", 1
1445 assert_select "osm>changeset[max_lat='2.0']", 1
1447 # add (delete) a way to it, which contains a point at (3,3)
1448 with_controller(WayController.new) do
1449 content update_changeset(current_ways(:visible_way).to_xml,
1451 put :delete, :id => current_ways(:visible_way).id
1452 assert_response :success, "Couldn't delete a way."
1455 # get the bounding box back from the changeset
1456 get :read, :id => changeset_id
1457 assert_response :success, "Couldn't read back changeset for the third time."
1458 # note that the 3.1 here is because of the bbox overexpansion
1459 assert_select "osm>changeset[min_lon='1.0']", 1
1460 assert_select "osm>changeset[max_lon='3.1']", 1
1461 assert_select "osm>changeset[min_lat='1.0']", 1
1462 assert_select "osm>changeset[max_lat='3.1']", 1
1466 # test that the changeset :include method works as it should
1467 def test_changeset_include
1468 basic_authorization users(:public_user).display_name, "test"
1470 # create a new changeset
1471 content "<osm><changeset/></osm>"
1473 assert_response :success, "Creating of changeset failed."
1474 changeset_id = @response.body.to_i
1476 # NOTE: the include method doesn't over-expand, like inserting
1477 # a real method does. this is because we expect the client to
1478 # know what it is doing!
1479 check_after_include(changeset_id, 1, 1, [ 1, 1, 1, 1])
1480 check_after_include(changeset_id, 3, 3, [ 1, 1, 3, 3])
1481 check_after_include(changeset_id, 4, 2, [ 1, 1, 4, 3])
1482 check_after_include(changeset_id, 2, 2, [ 1, 1, 4, 3])
1483 check_after_include(changeset_id, -1, -1, [-1, -1, 4, 3])
1484 check_after_include(changeset_id, -2, 5, [-2, -1, 4, 5])
1488 # test that a not found, wrong method with the expand bbox works as expected
1489 def test_changeset_expand_bbox_error
1490 basic_authorization users(:public_user).display_name, "test"
1492 # create a new changeset
1493 content "<osm><changeset/></osm>"
1495 assert_response :success, "Creating of changeset failed."
1496 changeset_id = @response.body.to_i
1502 content "<osm><node lon='#{lon}' lat='#{lat}'/></osm>"
1503 put :expand_bbox, :id => changeset_id
1504 assert_response :method_not_allowed, "shouldn't be able to put a bbox expand"
1506 # Try to get the update
1507 content "<osm><node lon='#{lon}' lat='#{lat}'/></osm>"
1508 get :expand_bbox, :id => changeset_id
1509 assert_response :method_not_allowed, "shouldn't be able to get a bbox expand"
1511 # Try to use a hopefully missing changeset
1512 content "<osm><node lon='#{lon}' lat='#{lat}'/></osm>"
1513 post :expand_bbox, :id => changeset_id+13245
1514 assert_response :not_found, "shouldn't be able to do a bbox expand on a nonexistant changeset"
1519 # test the query functionality of changesets
1521 get :query, :bbox => "-10,-10, 10, 10"
1522 assert_response :success, "can't get changesets in bbox"
1523 assert_changesets [1,4,6]
1525 get :query, :bbox => "4.5,4.5,4.6,4.6"
1526 assert_response :success, "can't get changesets in bbox"
1527 assert_changesets [1]
1529 # not found when looking for changesets of non-existing users
1530 get :query, :user => User.maximum(:id) + 1
1531 assert_response :not_found
1532 get :query, :display_name => " "
1533 assert_response :not_found
1535 # can't get changesets of user 1 without authenticating
1536 get :query, :user => users(:normal_user).id
1537 assert_response :not_found, "shouldn't be able to get changesets by non-public user (ID)"
1538 get :query, :display_name => users(:normal_user).display_name
1539 assert_response :not_found, "shouldn't be able to get changesets by non-public user (name)"
1541 # but this should work
1542 basic_authorization "test@openstreetmap.org", "test"
1543 get :query, :user => users(:normal_user).id
1544 assert_response :success, "can't get changesets by user ID"
1545 assert_changesets [1,3,6,8]
1547 get :query, :display_name => users(:normal_user).display_name
1548 assert_response :success, "can't get changesets by user name"
1549 assert_changesets [1,3,6,8]
1551 # check that the correct error is given when we provide both UID and name
1552 get :query, :user => users(:normal_user).id, :display_name => users(:normal_user).display_name
1553 assert_response :bad_request, "should be a bad request to have both ID and name specified"
1555 get :query, :user => users(:normal_user).id, :open => true
1556 assert_response :success, "can't get changesets by user and open"
1557 assert_changesets [1]
1559 get :query, :time => '2007-12-31'
1560 assert_response :success, "can't get changesets by time-since"
1561 assert_changesets [1,2,4,5,6]
1563 get :query, :time => '2008-01-01T12:34Z'
1564 assert_response :success, "can't get changesets by time-since with hour"
1565 assert_changesets [1,2,4,5,6]
1567 get :query, :time => '2007-12-31T23:59Z,2008-01-01T00:01Z'
1568 assert_response :success, "can't get changesets by time-range"
1569 assert_changesets [1,5,6]
1571 get :query, :open => 'true'
1572 assert_response :success, "can't get changesets by open-ness"
1573 assert_changesets [1,2,4]
1575 get :query, :closed => 'true'
1576 assert_response :success, "can't get changesets by closed-ness"
1577 assert_changesets [3,5,6,7,8]
1579 get :query, :closed => 'true', :user => users(:normal_user).id
1580 assert_response :success, "can't get changesets by closed-ness and user"
1581 assert_changesets [3,6,8]
1583 get :query, :closed => 'true', :user => users(:public_user).id
1584 assert_response :success, "can't get changesets by closed-ness and user"
1585 assert_changesets [7]
1587 get :query, :changesets => '1,2,3'
1588 assert_response :success, "can't get changesets by id (as comma-separated string)"
1589 assert_changesets [1,2,3]
1591 get :query, :changesets => ''
1592 assert_response :bad_request, "should be a bad request since changesets is empty"
1596 # check that errors are returned if garbage is inserted
1597 # into query strings
1598 def test_query_invalid
1601 ";drop table users;"
1603 get :query, :bbox => bbox
1604 assert_response :bad_request, "'#{bbox}' isn't a bbox"
1609 ";drop table users;",
1613 get :query, :time => time
1614 assert_response :bad_request, "'#{time}' isn't a valid time range"
1622 get :query, :user => uid
1623 assert_response :bad_request, "'#{uid}' isn't a valid user ID"
1628 # check updating tags on a changeset
1629 def test_changeset_update
1630 ## First try with the non-public user
1631 changeset = changesets(:normal_user_first_change)
1632 new_changeset = changeset.to_xml
1633 new_tag = XML::Node.new "tag"
1634 new_tag['k'] = "tagtesting"
1635 new_tag['v'] = "valuetesting"
1636 new_changeset.find("//osm/changeset").first << new_tag
1637 content new_changeset
1639 # try without any authorization
1640 put :update, :id => changeset.id
1641 assert_response :unauthorized
1643 # try with the wrong authorization
1644 basic_authorization users(:public_user).email, "test"
1645 put :update, :id => changeset.id
1646 assert_response :conflict
1648 # now this should get an unauthorized
1649 basic_authorization users(:normal_user).email, "test"
1650 put :update, :id => changeset.id
1651 assert_require_public_data "user with their data non-public, shouldn't be able to edit their changeset"
1654 ## Now try with the public user
1655 changeset = changesets(:public_user_first_change)
1656 new_changeset = changeset.to_xml
1657 new_tag = XML::Node.new "tag"
1658 new_tag['k'] = "tagtesting"
1659 new_tag['v'] = "valuetesting"
1660 new_changeset.find("//osm/changeset").first << new_tag
1661 content new_changeset
1663 # try without any authorization
1664 @request.env["HTTP_AUTHORIZATION"] = nil
1665 put :update, :id => changeset.id
1666 assert_response :unauthorized
1668 # try with the wrong authorization
1669 basic_authorization users(:second_public_user).email, "test"
1670 put :update, :id => changeset.id
1671 assert_response :conflict
1673 # now this should work...
1674 basic_authorization users(:public_user).email, "test"
1675 put :update, :id => changeset.id
1676 assert_response :success
1678 assert_select "osm>changeset[id='#{changeset.id}']", 1
1679 assert_select "osm>changeset>tag", 2
1680 assert_select "osm>changeset>tag[k=tagtesting][v=valuetesting]", 1
1684 # check that a user different from the one who opened the changeset
1686 def test_changeset_update_invalid
1687 basic_authorization users(:public_user).email, "test"
1689 changeset = changesets(:normal_user_first_change)
1690 new_changeset = changeset.to_xml
1691 new_tag = XML::Node.new "tag"
1692 new_tag['k'] = "testing"
1693 new_tag['v'] = "testing"
1694 new_changeset.find("//osm/changeset").first << new_tag
1696 content new_changeset
1697 put :update, :id => changeset.id
1698 assert_response :conflict
1702 # check that a changeset can contain a certain max number of changes.
1703 ## FIXME should be changed to an integration test due to the with_controller
1704 def test_changeset_limits
1705 basic_authorization users(:public_user).email, "test"
1707 # open a new changeset
1708 content "<osm><changeset/></osm>"
1710 assert_response :success, "can't create a new changeset"
1711 cs_id = @response.body.to_i
1713 # start the counter just short of where the changeset should finish.
1715 # alter the database to set the counter on the changeset directly,
1716 # otherwise it takes about 6 minutes to fill all of them.
1717 changeset = Changeset.find(cs_id)
1718 changeset.num_changes = Changeset::MAX_ELEMENTS - offset
1721 with_controller(NodeController.new) do
1723 content "<osm><node changeset='#{cs_id}' lat='0.0' lon='0.0'/></osm>"
1725 assert_response :success, "can't create a new node"
1726 node_id = @response.body.to_i
1728 get :read, :id => node_id
1729 assert_response :success, "can't read back new node"
1730 node_doc = XML::Parser.string(@response.body).parse
1731 node_xml = node_doc.find("//osm/node").first
1733 # loop until we fill the changeset with nodes
1735 node_xml['lat'] = rand.to_s
1736 node_xml['lon'] = rand.to_s
1737 node_xml['version'] = (i+1).to_s
1740 put :update, :id => node_id
1741 assert_response :success, "attempt #{i} should have succeeded"
1744 # trying again should fail
1745 node_xml['lat'] = rand.to_s
1746 node_xml['lon'] = rand.to_s
1747 node_xml['version'] = offset.to_s
1750 put :update, :id => node_id
1751 assert_response :conflict, "final attempt should have failed"
1754 changeset = Changeset.find(cs_id)
1755 assert_equal Changeset::MAX_ELEMENTS + 1, changeset.num_changes
1757 # check that the changeset is now closed as well
1758 assert(!changeset.is_open?,
1759 "changeset should have been auto-closed by exceeding " +
1764 # This should display the last 20 changesets closed.
1766 get :list, {:format => "html"}
1767 assert_response :success
1768 assert_template "history"
1769 assert_template :layout => "map"
1770 assert_select "h2", :text => "Changesets", :count => 1
1772 get :list, {:format => "html", :list => '1', :bbox => '-180,-90,90,180'}
1773 assert_response :success
1774 assert_template "list"
1776 changesets = Changeset.
1777 where("num_changes > 0 and min_lon is not null").
1778 order(:created_at => :desc).
1780 assert changesets.size <= 20
1782 # Now check that all 20 (or however many were returned) changesets are in the html
1783 assert_select "li", :count => changesets.size
1784 changesets.each do |changeset|
1785 # FIXME this test needs rewriting - test for table contents
1790 # This should display the last 20 changesets closed.
1792 xhr :get, :list, {:format => "html"}
1793 assert_response :success
1794 assert_template "history"
1795 assert_template :layout => "xhr"
1796 assert_select "h2", :text => "Changesets", :count => 1
1798 get :list, {:format => "html", :list => '1', :bbox => '-180,-90,90,180'}
1799 assert_response :success
1800 assert_template "list"
1802 changesets = Changeset.
1803 where("num_changes > 0 and min_lon is not null").
1804 order(:created_at => :desc).
1806 assert changesets.size <= 20
1808 # Now check that all 20 (or however many were returned) changesets are in the html
1809 assert_select "li", :count => changesets.size
1810 changesets.each do |changeset|
1811 # FIXME this test needs rewriting - test for table contents
1816 # Checks the display of the user changesets listing
1818 user = users(:public_user)
1819 get :list, {:format => "html", :display_name => user.display_name}
1820 assert_response :success
1821 assert_template "history"
1822 ## FIXME need to add more checks to see which if edits are actually shown if your data is public
1826 # Check the not found of the list user changesets
1827 def test_list_user_not_found
1828 get :list, {:format => "html", :display_name => "Some random user"}
1829 assert_response :not_found
1830 assert_template 'user/no_such_user'
1834 # This should display the last 20 changesets closed.
1836 changesets = Changeset.where("num_changes > 0").order(:created_at => :desc).limit(20)
1837 assert changesets.size <= 20
1838 get :feed, {:format => "atom"}
1839 assert_response :success
1840 assert_template "list"
1841 # Now check that all 20 (or however many were returned) changesets are in the html
1842 assert_select "feed", :count => 1
1843 assert_select "entry", :count => changesets.size
1844 changesets.each do |changeset|
1845 # FIXME this test needs rewriting - test for feed contents
1850 # Checks the display of the user changesets feed
1852 user = users(:public_user)
1853 get :feed, {:format => "atom", :display_name => user.display_name}
1854 assert_response :success
1855 assert_template "list"
1856 assert_equal "application/atom+xml", response.content_type
1857 ## FIXME need to add more checks to see which if edits are actually shown if your data is public
1861 # Check the not found of the user changesets feed
1862 def test_feed_user_not_found
1863 get :feed, {:format => "atom", :display_name => "Some random user"}
1864 assert_response :not_found
1868 # check that the changeset download for a changeset with a redacted
1869 # element in it doesn't contain that element.
1870 def test_diff_download_redacted
1871 changeset_id = changesets(:public_user_first_change).id
1873 get :download, :id => changeset_id
1874 assert_response :success
1876 assert_select "osmChange", 1
1877 # this changeset contains node 17 in versions 1 & 2, but 1 should
1879 assert_select "osmChange node[id='17']", 1
1880 assert_select "osmChange node[id='17'][version='1']", 0
1884 # create comment success
1885 def test_create_comment_success
1886 basic_authorization(users(:public_user).email, 'test')
1888 assert_difference('ChangesetComment.count') do
1889 post :comment, { :id => changesets(:normal_user_closed_change).id, :text => 'This is a comment' }
1891 assert_response :success
1895 # create comment fail
1896 def test_create_comment_fail
1898 post :comment, { :id => changesets(:normal_user_closed_change).id, :text => 'This is a comment' }
1899 assert_response :unauthorized
1901 basic_authorization(users(:public_user).email, 'test')
1904 assert_no_difference('ChangesetComment.count') do
1905 post :comment, { :id => 999111, :text => 'This is a comment' }
1907 assert_response :not_found
1909 # not closed changeset
1910 assert_no_difference('ChangesetComment.count') do
1911 post :comment, { :id => changesets(:normal_user_first_change).id, :text => 'This is a comment' }
1913 assert_response :conflict
1916 assert_no_difference('ChangesetComment.count') do
1917 post :comment, { :id => changesets(:normal_user_closed_change).id }
1919 assert_response :bad_request
1922 assert_no_difference('ChangesetComment.count') do
1923 post :comment, { :id => changesets(:normal_user_closed_change).id, :text => '' }
1925 assert_response :bad_request
1929 # test subscribe success
1930 def test_subscribe_success
1931 basic_authorization(users(:public_user).email, 'test')
1932 changeset = changesets(:normal_user_closed_change)
1934 assert_difference('changeset.subscribers.count') do
1935 post :subscribe, { :id => changeset.id }
1937 assert_response :success
1941 # test subscribe fail
1942 def test_subscribe_fail
1944 changeset = changesets(:normal_user_closed_change)
1945 assert_no_difference('changeset.subscribers.count') do
1946 post :subscribe, { :id => changeset.id }
1948 assert_response :unauthorized
1950 basic_authorization(users(:public_user).email, 'test')
1953 assert_no_difference('changeset.subscribers.count') do
1954 post :subscribe, { :id => 999111 }
1956 assert_response :not_found
1958 # not closed changeset
1959 changeset = changesets(:normal_user_first_change)
1960 assert_no_difference('changeset.subscribers.count') do
1961 post :subscribe, { :id => changeset.id }
1963 assert_response :conflict
1965 # trying to subscribe when already subscribed
1966 changeset = changesets(:normal_user_subscribed_change)
1967 assert_no_difference('changeset.subscribers.count') do
1968 post :subscribe, { :id => changeset.id }
1970 assert_response :conflict
1974 # test unsubscribe success
1975 def test_unsubscribe_success
1976 basic_authorization(users(:public_user).email, 'test')
1977 changeset = changesets(:normal_user_subscribed_change)
1979 assert_difference('changeset.subscribers.count', -1) do
1980 post :unsubscribe, { :id => changeset.id }
1982 assert_response :success
1986 # test unsubscribe fail
1987 def test_unsubscribe_fail
1989 changeset = changesets(:normal_user_closed_change)
1990 assert_no_difference('changeset.subscribers.count') do
1991 post :unsubscribe, { :id => changeset.id }
1993 assert_response :unauthorized
1995 basic_authorization(users(:public_user).email, 'test')
1998 assert_no_difference('changeset.subscribers.count', -1) do
1999 post :unsubscribe, { :id => 999111 }
2001 assert_response :not_found
2003 # not closed changeset
2004 changeset = changesets(:normal_user_first_change)
2005 assert_no_difference('changeset.subscribers.count', -1) do
2006 post :unsubscribe, { :id => changeset.id }
2008 assert_response :conflict
2010 # trying to unsubscribe when not subscribed
2011 changeset = changesets(:normal_user_closed_change)
2012 assert_no_difference('changeset.subscribers.count') do
2013 post :unsubscribe, { :id => changeset.id }
2015 assert_response :not_found
2019 # test hide comment fail
2020 def test_hide_comment_fail
2022 comment = changeset_comments(:normal_comment_1)
2023 assert('comment.visible') do
2024 post :hide_comment, { :id => comment.id }
2025 assert_response :unauthorized
2028 basic_authorization(users(:public_user).email, 'test')
2031 assert('comment.visible') do
2032 post :hide_comment, { :id => comment.id }
2033 assert_response :forbidden
2036 basic_authorization(users(:moderator_user).email, 'test')
2039 post :hide_comment, { :id => 999111 }
2040 assert_response :not_found
2044 # test hide comment succes
2045 def test_hide_comment_success
2046 comment = changeset_comments(:normal_comment_1)
2048 basic_authorization(users(:moderator_user).email, 'test')
2050 assert('!comment.visible') do
2051 post :hide_comment, { :id => comment.id }
2053 assert_response :success
2057 # test unhide comment fail
2058 def test_unhide_comment_fail
2060 comment = changeset_comments(:normal_comment_1)
2061 assert('comment.visible') do
2062 post :unhide_comment, { :id => comment.id }
2063 assert_response :unauthorized
2066 basic_authorization(users(:public_user).email, 'test')
2069 assert('comment.visible') do
2070 post :unhide_comment, { :id => comment.id }
2071 assert_response :forbidden
2074 basic_authorization(users(:moderator_user).email, 'test')
2077 post :unhide_comment, { :id => 999111 }
2078 assert_response :not_found
2082 # test unhide comment succes
2083 def test_unhide_comment_success
2084 comment = changeset_comments(:normal_comment_1)
2086 basic_authorization(users(:moderator_user).email, 'test')
2088 assert('!comment.visible') do
2089 post :unhide_comment, { :id => comment.id }
2091 assert_response :success
2095 # test comments feed
2096 def test_comments_feed
2097 get :comments_feed, {:format => "rss"}
2098 assert_response :success
2099 assert_equal "application/rss+xml", @response.content_type
2100 assert_select "rss", :count => 1 do
2101 assert_select "channel", :count => 1 do
2102 assert_select "item", :count => 3
2106 get :comments_feed, { :id => changesets(:normal_user_closed_change), :format => "rss"}
2107 assert_response :success
2108 assert_equal "application/rss+xml", @response.content_type
2109 assert_select "rss", :count => 1 do
2110 assert_select "channel", :count => 1 do
2111 assert_select "item", :count => 3
2116 #------------------------------------------------------------
2118 #------------------------------------------------------------
2121 # boilerplate for checking that certain changesets exist in the
2123 def assert_changesets(ids)
2124 assert_select "osm>changeset", ids.size
2126 assert_select "osm>changeset[id='#{id}']", 1
2131 # call the include method and assert properties of the bbox
2132 def check_after_include(changeset_id, lon, lat, bbox)
2133 content "<osm><node lon='#{lon}' lat='#{lat}'/></osm>"
2134 post :expand_bbox, :id => changeset_id
2135 assert_response :success, "Setting include of changeset failed: #{@response.body}"
2137 # check exactly one changeset
2138 assert_select "osm>changeset", 1
2139 assert_select "osm>changeset[id='#{changeset_id}']", 1
2142 doc = XML::Parser.string(@response.body).parse
2143 changeset = doc.find("//osm/changeset").first
2144 assert_equal bbox[0], changeset['min_lon'].to_f, "min lon"
2145 assert_equal bbox[1], changeset['min_lat'].to_f, "min lat"
2146 assert_equal bbox[2], changeset['max_lon'].to_f, "max lon"
2147 assert_equal bbox[3], changeset['max_lat'].to_f, "max lat"
2151 # update the changeset_id of a way element
2152 def update_changeset(xml, changeset_id)
2153 xml_attr_rewrite(xml, 'changeset', changeset_id)
2157 # update an attribute in a way element
2158 def xml_attr_rewrite(xml, name, value)
2159 xml.find("//osm/way").first[name] = value.to_s