4 class ChangesetsControllerTest < ActionDispatch::IntegrationTest
6 # test all routes which lead to this controller
9 { :path => "/api/0.6/changeset/create", :method => :put },
10 { :controller => "api/changesets", :action => "create" }
13 { :path => "/api/0.6/changeset/1/upload", :method => :post },
14 { :controller => "api/changesets", :action => "upload", :id => "1" }
17 { :path => "/api/0.6/changeset/1/download", :method => :get },
18 { :controller => "api/changesets", :action => "download", :id => "1" }
21 { :path => "/api/0.6/changeset/1", :method => :get },
22 { :controller => "api/changesets", :action => "show", :id => "1" }
25 { :path => "/api/0.6/changeset/1.json", :method => :get },
26 { :controller => "api/changesets", :action => "show", :id => "1", :format => "json" }
29 { :path => "/api/0.6/changeset/1/subscribe", :method => :post },
30 { :controller => "api/changesets", :action => "subscribe", :id => "1" }
33 { :path => "/api/0.6/changeset/1/subscribe.json", :method => :post },
34 { :controller => "api/changesets", :action => "subscribe", :id => "1", :format => "json" }
37 { :path => "/api/0.6/changeset/1/unsubscribe", :method => :post },
38 { :controller => "api/changesets", :action => "unsubscribe", :id => "1" }
41 { :path => "/api/0.6/changeset/1/unsubscribe.json", :method => :post },
42 { :controller => "api/changesets", :action => "unsubscribe", :id => "1", :format => "json" }
45 { :path => "/api/0.6/changeset/1", :method => :put },
46 { :controller => "api/changesets", :action => "update", :id => "1" }
49 { :path => "/api/0.6/changeset/1/close", :method => :put },
50 { :controller => "api/changesets", :action => "close", :id => "1" }
53 { :path => "/api/0.6/changesets", :method => :get },
54 { :controller => "api/changesets", :action => "query" }
57 { :path => "/api/0.6/changesets.json", :method => :get },
58 { :controller => "api/changesets", :action => "query", :format => "json" }
62 # -----------------------
63 # Test simple changeset creation
64 # -----------------------
67 auth_header = basic_authorization_header create(:user, :data_public => false).email, "test"
68 # Create the first user's changeset
69 xml = "<osm><changeset>" \
70 "<tag k='created_by' v='osm test suite checking changesets'/>" \
72 put changeset_create_path, :params => xml, :headers => auth_header
73 assert_require_public_data
75 auth_header = basic_authorization_header create(:user).email, "test"
76 # Create the first user's changeset
77 xml = "<osm><changeset>" \
78 "<tag k='created_by' v='osm test suite checking changesets'/>" \
80 put changeset_create_path, :params => xml, :headers => auth_header
82 assert_response :success, "Creation of changeset did not return success status"
83 newid = @response.body.to_i
85 # check end time, should be an hour ahead of creation time
86 cs = Changeset.find(newid)
87 duration = cs.closed_at - cs.created_at
88 # the difference can either be a rational, or a floating point number
89 # of seconds, depending on the code path taken :-(
90 if duration.instance_of?(Rational)
91 assert_equal Rational(1, 24), duration, "initial idle timeout should be an hour (#{cs.created_at} -> #{cs.closed_at})"
93 # must be number of seconds...
94 assert_equal 3600, duration.round, "initial idle timeout should be an hour (#{cs.created_at} -> #{cs.closed_at})"
97 # checks if uploader was subscribed
98 assert_equal 1, cs.subscribers.length
101 def test_create_invalid
102 auth_header = basic_authorization_header create(:user, :data_public => false).email, "test"
103 xml = "<osm><changeset></osm>"
104 put changeset_create_path, :params => xml, :headers => auth_header
105 assert_require_public_data
107 ## Try the public user
108 auth_header = basic_authorization_header create(:user).email, "test"
109 xml = "<osm><changeset></osm>"
110 put changeset_create_path, :params => xml, :headers => auth_header
111 assert_response :bad_request, "creating a invalid changeset should fail"
114 def test_create_invalid_no_content
115 ## First check with no auth
116 put changeset_create_path
117 assert_response :unauthorized, "shouldn't be able to create a changeset with no auth"
119 ## Now try to with a non-public user
120 auth_header = basic_authorization_header create(:user, :data_public => false).email, "test"
121 put changeset_create_path, :headers => auth_header
122 assert_require_public_data
124 ## Try an inactive user
125 auth_header = basic_authorization_header create(:user, :pending).email, "test"
126 put changeset_create_path, :headers => auth_header
129 ## Now try to use a normal user
130 auth_header = basic_authorization_header create(:user).email, "test"
131 put changeset_create_path, :headers => auth_header
132 assert_response :bad_request, "creating a changeset with no content should fail"
135 def test_create_wrong_method
136 auth_header = basic_authorization_header create(:user).email, "test"
137 assert_raise ActionController::RoutingError do
138 get changeset_create_path, :headers => auth_header
140 assert_raise ActionController::RoutingError do
141 post changeset_create_path, :headers => auth_header
146 # check that the changeset can be shown and returns the correct
147 # document structure.
149 changeset = create(:changeset)
151 get changeset_show_path(changeset)
152 assert_response :success, "cannot get first changeset"
154 assert_select "osm[version='#{Settings.api_version}'][generator='#{Settings.generator}']", 1
155 assert_select "osm>changeset[id='#{changeset.id}']", 1
156 assert_select "osm>changeset>@open", "true"
157 assert_select "osm>changeset>@created_at", changeset.created_at.xmlschema
158 assert_select "osm>changeset>@closed_at", 0
159 assert_select "osm>changeset>discussion", 0
161 get changeset_show_path(changeset), :params => { :include_discussion => true }
162 assert_response :success, "cannot get first changeset with comments"
164 assert_select "osm[version='#{Settings.api_version}'][generator='#{Settings.generator}']", 1
165 assert_select "osm>changeset[id='#{changeset.id}']", 1
166 assert_select "osm>changeset>@open", "true"
167 assert_select "osm>changeset>@created_at", changeset.created_at.xmlschema
168 assert_select "osm>changeset>@closed_at", 0
169 assert_select "osm>changeset>discussion", 1
170 assert_select "osm>changeset>discussion>comment", 0
172 changeset = create(:changeset, :closed)
173 comment1, comment2, comment3 = create_list(:changeset_comment, 3, :changeset_id => changeset.id)
175 get changeset_show_path(changeset), :params => { :include_discussion => true }
176 assert_response :success, "cannot get closed changeset with comments"
178 assert_select "osm[version='#{Settings.api_version}'][generator='#{Settings.generator}']", 1
179 assert_select "osm>changeset[id='#{changeset.id}']", 1
180 assert_select "osm>changeset>@open", "false"
181 assert_select "osm>changeset>@created_at", changeset.created_at.xmlschema
182 assert_select "osm>changeset>@closed_at", changeset.closed_at.xmlschema
183 assert_select "osm>changeset>discussion", 1
184 assert_select "osm>changeset>discussion>comment", 3
185 assert_select "osm>changeset>discussion>comment:nth-child(1)>@id", comment1.id.to_s
186 assert_select "osm>changeset>discussion>comment:nth-child(2)>@id", comment2.id.to_s
187 assert_select "osm>changeset>discussion>comment:nth-child(3)>@id", comment3.id.to_s
191 changeset = create(:changeset)
193 get changeset_show_path(changeset), :params => { :format => "json" }
194 assert_response :success, "cannot get first changeset"
196 js = ActiveSupport::JSON.decode(@response.body)
199 assert_equal Settings.api_version, js["version"]
200 assert_equal Settings.generator, js["generator"]
201 assert_equal changeset.id, js["changeset"]["id"]
202 assert_operator js["changeset"], :[], "open"
203 assert_equal changeset.created_at.xmlschema, js["changeset"]["created_at"]
204 assert_nil js["changeset"]["closed_at"]
205 assert_nil js["changeset"]["tags"]
206 assert_nil js["changeset"]["comments"]
207 assert_equal changeset.user.id, js["changeset"]["uid"]
208 assert_equal changeset.user.display_name, js["changeset"]["user"]
210 get changeset_show_path(changeset), :params => { :format => "json", :include_discussion => true }
211 assert_response :success, "cannot get first changeset with comments"
213 js = ActiveSupport::JSON.decode(@response.body)
215 assert_equal Settings.api_version, js["version"]
216 assert_equal Settings.generator, js["generator"]
217 assert_equal changeset.id, js["changeset"]["id"]
218 assert_operator js["changeset"], :[], "open"
219 assert_equal changeset.created_at.xmlschema, js["changeset"]["created_at"]
220 assert_nil js["changeset"]["closed_at"]
221 assert_nil js["changeset"]["tags"]
222 assert_nil js["changeset"]["min_lat"]
223 assert_nil js["changeset"]["min_lon"]
224 assert_nil js["changeset"]["max_lat"]
225 assert_nil js["changeset"]["max_lon"]
226 assert_equal 0, js["changeset"]["comments"].count
228 changeset = create(:changeset, :closed)
229 comment0, comment1, comment2 = create_list(:changeset_comment, 3, :changeset_id => changeset.id)
231 get changeset_show_path(changeset), :params => { :format => "json", :include_discussion => true }
232 assert_response :success, "cannot get closed changeset with comments"
234 js = ActiveSupport::JSON.decode(@response.body)
236 assert_equal Settings.api_version, js["version"]
237 assert_equal Settings.generator, js["generator"]
238 assert_equal changeset.id, js["changeset"]["id"]
239 assert_not js["changeset"]["open"]
240 assert_equal changeset.created_at.xmlschema, js["changeset"]["created_at"]
241 assert_equal changeset.closed_at.xmlschema, js["changeset"]["closed_at"]
242 assert_equal 3, js["changeset"]["comments"].count
243 assert_equal comment0.id, js["changeset"]["comments"][0]["id"]
244 assert_equal comment1.id, js["changeset"]["comments"][1]["id"]
245 assert_equal comment2.id, js["changeset"]["comments"][2]["id"]
248 def test_show_tag_and_discussion_json
249 changeset = create(:changeset, :closed)
251 tag1 = ChangesetTag.new
252 tag1.changeset_id = changeset.id
253 tag1.k = "created_by"
254 tag1.v = "JOSM/1.5 (18364)"
256 tag2 = ChangesetTag.new
257 tag2.changeset_id = changeset.id
259 tag2.v = "changeset comment"
261 changeset.changeset_tags = [tag1, tag2]
263 create_list(:changeset_comment, 3, :changeset_id => changeset.id)
265 get changeset_show_path(changeset), :params => { :format => "json", :include_discussion => true }
266 assert_response :success, "cannot get closed changeset with comments"
268 js = ActiveSupport::JSON.decode(@response.body)
271 assert_equal Settings.api_version, js["version"]
272 assert_equal Settings.generator, js["generator"]
273 assert_equal changeset.id, js["changeset"]["id"]
274 assert_not js["changeset"]["open"]
275 assert_equal changeset.created_at.xmlschema, js["changeset"]["created_at"]
276 assert_equal changeset.closed_at.xmlschema, js["changeset"]["closed_at"]
277 assert_equal 2, js["changeset"]["tags"].count
278 assert_equal 3, js["changeset"]["comments"].count
279 assert_equal 3, js["changeset"]["comments_count"]
280 assert_equal 0, js["changeset"]["changes_count"]
281 assert_not_nil js["changeset"]["comments"][0]["uid"]
282 assert_not_nil js["changeset"]["comments"][0]["user"]
283 assert_not_nil js["changeset"]["comments"][0]["text"]
286 def test_show_bbox_json
287 # test bbox attribute
288 changeset = create(:changeset, :min_lat => (-5 * GeoRecord::SCALE).round, :min_lon => (5 * GeoRecord::SCALE).round,
289 :max_lat => (15 * GeoRecord::SCALE).round, :max_lon => (12 * GeoRecord::SCALE).round)
291 get changeset_show_path(changeset), :params => { :format => "json" }
292 assert_response :success, "cannot get first changeset"
294 js = ActiveSupport::JSON.decode(@response.body)
296 assert_equal(-5, js["changeset"]["min_lat"])
297 assert_equal 5, js["changeset"]["min_lon"]
298 assert_equal 15, js["changeset"]["max_lat"]
299 assert_equal 12, js["changeset"]["max_lon"]
303 # check that a changeset that doesn't exist returns an appropriate message
304 def test_show_not_found
305 [0, -32, 233455644, "afg", "213"].each do |id|
306 get changeset_show_path(:id => id)
307 assert_response :not_found, "should get a not found"
308 rescue ActionController::UrlGenerationError => e
309 assert_match(/No route matches/, e.to_s)
314 # test that the user who opened a change can close it
316 private_user = create(:user, :data_public => false)
317 private_changeset = create(:changeset, :user => private_user)
319 changeset = create(:changeset, :user => user)
321 ## Try without authentication
322 put changeset_close_path(changeset)
323 assert_response :unauthorized
325 ## Try using the non-public user
326 auth_header = basic_authorization_header private_user.email, "test"
327 put changeset_close_path(private_changeset), :headers => auth_header
328 assert_require_public_data
330 ## The try with the public user
331 auth_header = basic_authorization_header user.email, "test"
334 put changeset_close_path(:id => cs_id), :headers => auth_header
335 assert_response :success
337 # test that it really is closed now
338 cs = Changeset.find(changeset.id)
340 "changeset should be closed now (#{cs.closed_at} > #{Time.now.utc}.")
344 # test that a different user can't close another user's changeset
345 def test_close_invalid
347 changeset = create(:changeset)
349 auth_header = basic_authorization_header user.email, "test"
351 put changeset_close_path(changeset), :headers => auth_header
352 assert_response :conflict
353 assert_equal "The user doesn't own that changeset", @response.body
357 # test that you can't close using another method
358 def test_close_method_invalid
360 changeset = create(:changeset, :user => user)
362 auth_header = basic_authorization_header user.email, "test"
364 assert_raise ActionController::RoutingError do
365 get changeset_close_path(changeset), :headers => auth_header
368 assert_raise ActionController::RoutingError do
369 post changeset_close_path(changeset), :headers => auth_header
374 # check that you can't close a changeset that isn't found
375 def test_close_not_found
376 cs_ids = [0, -132, "123"]
378 # First try to do it with no auth
380 put changeset_close_path(:id => id)
381 assert_response :unauthorized, "Shouldn't be able close the non-existant changeset #{id}, when not authorized"
382 rescue ActionController::UrlGenerationError => e
383 assert_match(/No route matches/, e.to_s)
387 auth_header = basic_authorization_header create(:user).email, "test"
389 put changeset_close_path(:id => id), :headers => auth_header
390 assert_response :not_found, "The changeset #{id} doesn't exist, so can't be closed"
391 rescue ActionController::UrlGenerationError => e
392 assert_match(/No route matches/, e.to_s)
397 # upload something simple, but valid and check that it can
399 # Also try without auth and another user.
400 def test_upload_simple_valid
401 private_user = create(:user, :data_public => false)
402 private_changeset = create(:changeset, :user => private_user)
404 changeset = create(:changeset, :user => user)
408 relation = create(:relation)
409 other_relation = create(:relation)
410 # create some tags, since we test that they are removed later
411 create(:node_tag, :node => node)
412 create(:way_tag, :way => way)
413 create(:relation_tag, :relation => relation)
416 changeset_id = changeset.id
418 # simple diff to change a node, way and relation by removing
423 <node id='#{node.id}' lon='0' lat='0' changeset='#{changeset_id}' version='1'/>
424 <way id='#{way.id}' changeset='#{changeset_id}' version='1'>
425 <nd ref='#{node.id}'/>
429 <relation id='#{relation.id}' changeset='#{changeset_id}' version='1'>
430 <member type='way' role='some' ref='#{way.id}'/>
431 <member type='node' role='some' ref='#{node.id}'/>
432 <member type='relation' role='some' ref='#{other_relation.id}'/>
439 post changeset_upload_path(changeset), :params => diff
440 assert_response :unauthorized,
441 "shouldn't be able to upload a simple valid diff to changeset: #{@response.body}"
443 ## Now try with a private user
444 auth_header = basic_authorization_header private_user.email, "test"
445 changeset_id = private_changeset.id
447 # simple diff to change a node, way and relation by removing
452 <node id='#{node.id}' lon='0' lat='0' changeset='#{changeset_id}' version='1'/>
453 <way id='#{way.id}' changeset='#{changeset_id}' version='1'>
454 <nd ref='#{node.id}'/>
458 <relation id='#{relation.id}' changeset='#{changeset_id}' version='1'>
459 <member type='way' role='some' ref='#{way.id}'/>
460 <member type='node' role='some' ref='#{node.id}'/>
461 <member type='relation' role='some' ref='#{other_relation.id}'/>
468 post changeset_upload_path(private_changeset), :params => diff, :headers => auth_header
469 assert_response :forbidden,
470 "can't upload a simple valid diff to changeset: #{@response.body}"
472 ## Now try with the public user
473 auth_header = basic_authorization_header user.email, "test"
474 changeset_id = changeset.id
476 # simple diff to change a node, way and relation by removing
481 <node id='#{node.id}' lon='0' lat='0' changeset='#{changeset_id}' version='1'/>
482 <way id='#{way.id}' changeset='#{changeset_id}' version='1'>
483 <nd ref='#{node.id}'/>
487 <relation id='#{relation.id}' changeset='#{changeset_id}' version='1'>
488 <member type='way' role='some' ref='#{way.id}'/>
489 <member type='node' role='some' ref='#{node.id}'/>
490 <member type='relation' role='some' ref='#{other_relation.id}'/>
497 post changeset_upload_path(changeset), :params => diff, :headers => auth_header
498 assert_response :success,
499 "can't upload a simple valid diff to changeset: #{@response.body}"
501 # check that the changes made it into the database
502 assert_equal 0, Node.find(node.id).tags.size, "node #{node.id} should now have no tags"
503 assert_equal 0, Way.find(way.id).tags.size, "way #{way.id} should now have no tags"
504 assert_equal 0, Relation.find(relation.id).tags.size, "relation #{relation.id} should now have no tags"
508 # upload something which creates new objects using placeholders
509 def test_upload_create_valid
511 changeset = create(:changeset, :user => user)
513 way = create(:way_with_nodes, :nodes_count => 2)
514 relation = create(:relation)
516 auth_header = basic_authorization_header user.email, "test"
518 # simple diff to create a node way and relation using placeholders
522 <node id='-1' lon='0' lat='0' changeset='#{changeset.id}'>
523 <tag k='foo' v='bar'/>
524 <tag k='baz' v='bat'/>
526 <way id='-1' changeset='#{changeset.id}'>
527 <nd ref='#{node.id}'/>
531 <relation id='-1' changeset='#{changeset.id}'>
532 <member type='way' role='some' ref='#{way.id}'/>
533 <member type='node' role='some' ref='#{node.id}'/>
534 <member type='relation' role='some' ref='#{relation.id}'/>
541 post changeset_upload_path(changeset), :params => diff, :headers => auth_header
542 assert_response :success,
543 "can't upload a simple valid creation to changeset: #{@response.body}"
545 # check the returned payload
546 assert_select "diffResult[version='#{Settings.api_version}'][generator='#{Settings.generator}']", 1
547 assert_select "diffResult>node", 1
548 assert_select "diffResult>way", 1
549 assert_select "diffResult>relation", 1
551 # inspect the response to find out what the new element IDs are
552 doc = XML::Parser.string(@response.body).parse
553 new_node_id = doc.find("//diffResult/node").first["new_id"].to_i
554 new_way_id = doc.find("//diffResult/way").first["new_id"].to_i
555 new_rel_id = doc.find("//diffResult/relation").first["new_id"].to_i
557 # check the old IDs are all present and negative one
558 assert_equal(-1, doc.find("//diffResult/node").first["old_id"].to_i)
559 assert_equal(-1, doc.find("//diffResult/way").first["old_id"].to_i)
560 assert_equal(-1, doc.find("//diffResult/relation").first["old_id"].to_i)
562 # check the versions are present and equal one
563 assert_equal 1, doc.find("//diffResult/node").first["new_version"].to_i
564 assert_equal 1, doc.find("//diffResult/way").first["new_version"].to_i
565 assert_equal 1, doc.find("//diffResult/relation").first["new_version"].to_i
567 # check that the changes made it into the database
568 assert_equal 2, Node.find(new_node_id).tags.size, "new node should have two tags"
569 assert_equal 0, Way.find(new_way_id).tags.size, "new way should have no tags"
570 assert_equal 0, Relation.find(new_rel_id).tags.size, "new relation should have no tags"
574 # test a complex delete where we delete elements which rely on eachother
575 # in the same transaction.
576 def test_upload_delete
577 changeset = create(:changeset)
578 super_relation = create(:relation)
579 used_relation = create(:relation)
580 used_way = create(:way)
581 used_node = create(:node)
582 create(:relation_member, :relation => super_relation, :member => used_relation)
583 create(:relation_member, :relation => super_relation, :member => used_way)
584 create(:relation_member, :relation => super_relation, :member => used_node)
586 auth_header = basic_authorization_header changeset.user.display_name, "test"
588 diff = XML::Document.new
589 diff.root = XML::Node.new "osmChange"
590 delete = XML::Node.new "delete"
592 delete << xml_node_for_relation(super_relation)
593 delete << xml_node_for_relation(used_relation)
594 delete << xml_node_for_way(used_way)
595 delete << xml_node_for_node(used_node)
597 # update the changeset to one that this user owns
598 %w[node way relation].each do |type|
599 delete.find("//osmChange/delete/#{type}").each do |n|
600 n["changeset"] = changeset.id.to_s
605 post changeset_upload_path(changeset), :params => diff.to_s, :headers => auth_header
606 assert_response :success,
607 "can't upload a deletion diff to changeset: #{@response.body}"
609 # check the response is well-formed
610 assert_select "diffResult>node", 1
611 assert_select "diffResult>way", 1
612 assert_select "diffResult>relation", 2
614 # check that everything was deleted
615 assert_not Node.find(used_node.id).visible
616 assert_not Way.find(used_way.id).visible
617 assert_not Relation.find(super_relation.id).visible
618 assert_not Relation.find(used_relation.id).visible
622 # test uploading a delete with no lat/lon, as they are optional in
623 # the osmChange spec.
624 def test_upload_nolatlon_delete
626 changeset = create(:changeset)
628 auth_header = basic_authorization_header changeset.user.display_name, "test"
629 diff = "<osmChange><delete><node id='#{node.id}' version='#{node.version}' changeset='#{changeset.id}'/></delete></osmChange>"
632 post changeset_upload_path(changeset), :params => diff, :headers => auth_header
633 assert_response :success,
634 "can't upload a deletion diff to changeset: #{@response.body}"
636 # check the response is well-formed
637 assert_select "diffResult>node", 1
639 # check that everything was deleted
640 assert_not Node.find(node.id).visible
643 def test_repeated_changeset_create
645 auth_header = basic_authorization_header create(:user).email, "test"
647 # create a temporary changeset
648 xml = "<osm><changeset>" \
649 "<tag k='created_by' v='osm test suite checking changesets'/>" \
651 assert_difference "Changeset.count", 1 do
652 put changeset_create_path, :params => xml, :headers => auth_header
654 assert_response :success
658 def test_upload_large_changeset
659 auth_header = basic_authorization_header create(:user).email, "test"
662 put changeset_create_path, :params => "<osm><changeset/></osm>", :headers => auth_header
663 assert_response :success, "Should be able to create a changeset: #{@response.body}"
664 changeset_id = @response.body.to_i
666 # upload some widely-spaced nodes, spiralling positive and negative
670 <node id='-1' lon='-20' lat='-10' changeset='#{changeset_id}'/>
671 <node id='-10' lon='20' lat='10' changeset='#{changeset_id}'/>
672 <node id='-2' lon='-40' lat='-20' changeset='#{changeset_id}'/>
673 <node id='-11' lon='40' lat='20' changeset='#{changeset_id}'/>
674 <node id='-3' lon='-60' lat='-30' changeset='#{changeset_id}'/>
675 <node id='-12' lon='60' lat='30' changeset='#{changeset_id}'/>
676 <node id='-4' lon='-80' lat='-40' changeset='#{changeset_id}'/>
677 <node id='-13' lon='80' lat='40' changeset='#{changeset_id}'/>
678 <node id='-5' lon='-100' lat='-50' changeset='#{changeset_id}'/>
679 <node id='-14' lon='100' lat='50' changeset='#{changeset_id}'/>
680 <node id='-6' lon='-120' lat='-60' changeset='#{changeset_id}'/>
681 <node id='-15' lon='120' lat='60' changeset='#{changeset_id}'/>
682 <node id='-7' lon='-140' lat='-70' changeset='#{changeset_id}'/>
683 <node id='-16' lon='140' lat='70' changeset='#{changeset_id}'/>
684 <node id='-8' lon='-160' lat='-80' changeset='#{changeset_id}'/>
685 <node id='-17' lon='160' lat='80' changeset='#{changeset_id}'/>
686 <node id='-9' lon='-179.9' lat='-89.9' changeset='#{changeset_id}'/>
687 <node id='-18' lon='179.9' lat='89.9' changeset='#{changeset_id}'/>
692 # upload it, which used to cause an error like "PGError: ERROR:
693 # integer out of range" (bug #2152). but shouldn't any more.
694 post changeset_upload_path(:id => changeset_id), :params => diff, :headers => auth_header
695 assert_response :success,
696 "can't upload a spatially-large diff to changeset: #{@response.body}"
698 # check that the changeset bbox is within bounds
699 cs = Changeset.find(changeset_id)
700 assert_operator cs.min_lon, :>=, -180 * GeoRecord::SCALE, "Minimum longitude (#{cs.min_lon / GeoRecord::SCALE}) should be >= -180 to be valid."
701 assert_operator cs.max_lon, :<=, 180 * GeoRecord::SCALE, "Maximum longitude (#{cs.max_lon / GeoRecord::SCALE}) should be <= 180 to be valid."
702 assert_operator cs.min_lat, :>=, -90 * GeoRecord::SCALE, "Minimum latitude (#{cs.min_lat / GeoRecord::SCALE}) should be >= -90 to be valid."
703 assert_operator cs.max_lat, :<=, 90 * GeoRecord::SCALE, "Maximum latitude (#{cs.max_lat / GeoRecord::SCALE}) should be <= 90 to be valid."
707 # test that deleting stuff in a transaction doesn't bypass the checks
708 # to ensure that used elements are not deleted.
709 def test_upload_delete_invalid
710 changeset = create(:changeset)
711 relation = create(:relation)
712 other_relation = create(:relation)
713 used_way = create(:way)
714 used_node = create(:node)
715 create(:relation_member, :relation => relation, :member => used_way)
716 create(:relation_member, :relation => relation, :member => used_node)
718 auth_header = basic_authorization_header changeset.user.email, "test"
720 diff = XML::Document.new
721 diff.root = XML::Node.new "osmChange"
722 delete = XML::Node.new "delete"
724 delete << xml_node_for_relation(other_relation)
725 delete << xml_node_for_way(used_way)
726 delete << xml_node_for_node(used_node)
728 # update the changeset to one that this user owns
729 %w[node way relation].each do |type|
730 delete.find("//osmChange/delete/#{type}").each do |n|
731 n["changeset"] = changeset.id.to_s
736 post changeset_upload_path(changeset), :params => diff.to_s, :headers => auth_header
737 assert_response :precondition_failed,
738 "shouldn't be able to upload a invalid deletion diff: #{@response.body}"
739 assert_equal "Precondition failed: Way #{used_way.id} is still used by relations #{relation.id}.", @response.body
741 # check that nothing was, in fact, deleted
742 assert Node.find(used_node.id).visible
743 assert Way.find(used_way.id).visible
744 assert Relation.find(relation.id).visible
745 assert Relation.find(other_relation.id).visible
749 # test that a conditional delete of an in use object works.
750 def test_upload_delete_if_unused
751 changeset = create(:changeset)
752 super_relation = create(:relation)
753 used_relation = create(:relation)
754 used_way = create(:way)
755 used_node = create(:node)
756 create(:relation_member, :relation => super_relation, :member => used_relation)
757 create(:relation_member, :relation => super_relation, :member => used_way)
758 create(:relation_member, :relation => super_relation, :member => used_node)
760 auth_header = basic_authorization_header changeset.user.email, "test"
762 diff = XML::Document.new
763 diff.root = XML::Node.new "osmChange"
764 delete = XML::Node.new "delete"
766 delete["if-unused"] = ""
767 delete << xml_node_for_relation(used_relation)
768 delete << xml_node_for_way(used_way)
769 delete << xml_node_for_node(used_node)
771 # update the changeset to one that this user owns
772 %w[node way relation].each do |type|
773 delete.find("//osmChange/delete/#{type}").each do |n|
774 n["changeset"] = changeset.id.to_s
779 post changeset_upload_path(changeset), :params => diff.to_s, :headers => auth_header
780 assert_response :success,
781 "can't do a conditional delete of in use objects: #{@response.body}"
783 # check the returned payload
784 assert_select "diffResult[version='#{Settings.api_version}'][generator='#{Settings.generator}']", 1
785 assert_select "diffResult>node", 1
786 assert_select "diffResult>way", 1
787 assert_select "diffResult>relation", 1
790 doc = XML::Parser.string(@response.body).parse
792 # check the old IDs are all present and what we expect
793 assert_equal used_node.id, doc.find("//diffResult/node").first["old_id"].to_i
794 assert_equal used_way.id, doc.find("//diffResult/way").first["old_id"].to_i
795 assert_equal used_relation.id, doc.find("//diffResult/relation").first["old_id"].to_i
797 # check the new IDs are all present and unchanged
798 assert_equal used_node.id, doc.find("//diffResult/node").first["new_id"].to_i
799 assert_equal used_way.id, doc.find("//diffResult/way").first["new_id"].to_i
800 assert_equal used_relation.id, doc.find("//diffResult/relation").first["new_id"].to_i
802 # check the new versions are all present and unchanged
803 assert_equal used_node.version, doc.find("//diffResult/node").first["new_version"].to_i
804 assert_equal used_way.version, doc.find("//diffResult/way").first["new_version"].to_i
805 assert_equal used_relation.version, doc.find("//diffResult/relation").first["new_version"].to_i
807 # check that nothing was, in fact, deleted
808 assert Node.find(used_node.id).visible
809 assert Way.find(used_way.id).visible
810 assert Relation.find(used_relation.id).visible
814 # upload an element with a really long tag value
815 def test_upload_invalid_too_long_tag
816 changeset = create(:changeset)
818 auth_header = basic_authorization_header changeset.user.email, "test"
820 # simple diff to create a node way and relation using placeholders
824 <node id='-1' lon='0' lat='0' changeset='#{changeset.id}'>
825 <tag k='foo' v='#{'x' * 256}'/>
832 post changeset_upload_path(changeset), :params => diff, :headers => auth_header
833 assert_response :bad_request,
834 "shouldn't be able to upload too long a tag to changeset: #{@response.body}"
838 # upload something which creates new objects and inserts them into
839 # existing containers using placeholders.
840 def test_upload_complex
843 relation = create(:relation)
844 create(:way_node, :way => way, :node => node)
846 changeset = create(:changeset)
848 auth_header = basic_authorization_header changeset.user.email, "test"
850 # simple diff to create a node way and relation using placeholders
854 <node id='-1' lon='0' lat='0' changeset='#{changeset.id}'>
855 <tag k='foo' v='bar'/>
856 <tag k='baz' v='bat'/>
860 <way id='#{way.id}' changeset='#{changeset.id}' version='1'>
862 <nd ref='#{node.id}'/>
864 <relation id='#{relation.id}' changeset='#{changeset.id}' version='1'>
865 <member type='way' role='some' ref='#{way.id}'/>
866 <member type='node' role='some' ref='-1'/>
867 <member type='relation' role='some' ref='#{relation.id}'/>
874 post changeset_upload_path(changeset), :params => diff, :headers => auth_header
875 assert_response :success,
876 "can't upload a complex diff to changeset: #{@response.body}"
878 # check the returned payload
879 assert_select "diffResult[version='#{Settings.api_version}'][generator='#{Settings.generator}']", 1
880 assert_select "diffResult>node", 1
881 assert_select "diffResult>way", 1
882 assert_select "diffResult>relation", 1
884 # inspect the response to find out what the new element IDs are
885 doc = XML::Parser.string(@response.body).parse
886 new_node_id = doc.find("//diffResult/node").first["new_id"].to_i
888 # check that the changes made it into the database
889 assert_equal 2, Node.find(new_node_id).tags.size, "new node should have two tags"
890 assert_equal [new_node_id, node.id], Way.find(way.id).nds, "way nodes should match"
891 Relation.find(relation.id).members.each do |type, id, _role|
892 assert_equal new_node_id, id, "relation should contain new node" if type == "node"
897 # create a diff which references several changesets, which should cause
898 # a rollback and none of the diff gets committed
899 def test_upload_invalid_changesets
900 changeset = create(:changeset)
901 other_changeset = create(:changeset, :user => changeset.user)
904 relation = create(:relation)
905 other_relation = create(:relation)
907 auth_header = basic_authorization_header changeset.user.email, "test"
909 # simple diff to create a node way and relation using placeholders
913 <node id='#{node.id}' lon='0' lat='0' changeset='#{changeset.id}' version='1'/>
914 <way id='#{way.id}' changeset='#{changeset.id}' version='1'>
915 <nd ref='#{node.id}'/>
919 <relation id='#{relation.id}' changeset='#{changeset.id}' version='1'>
920 <member type='way' role='some' ref='#{way.id}'/>
921 <member type='node' role='some' ref='#{node.id}'/>
922 <member type='relation' role='some' ref='#{other_relation.id}'/>
926 <node id='-1' lon='0' lat='0' changeset='#{other_changeset.id}'>
927 <tag k='foo' v='bar'/>
928 <tag k='baz' v='bat'/>
935 post changeset_upload_path(changeset), :params => diff, :headers => auth_header
936 assert_response :conflict,
937 "uploading a diff with multiple changesets should have failed"
939 # check that objects are unmodified
940 assert_nodes_are_equal(node, Node.find(node.id))
941 assert_ways_are_equal(way, Way.find(way.id))
942 assert_relations_are_equal(relation, Relation.find(relation.id))
946 # upload multiple versions of the same element in the same diff.
947 def test_upload_multiple_valid
949 changeset = create(:changeset)
950 auth_header = basic_authorization_header changeset.user.email, "test"
952 # change the location of a node multiple times, each time referencing
953 # the last version. doesn't this depend on version numbers being
958 <node id='#{node.id}' lon='0' lat='0' changeset='#{changeset.id}' version='1'/>
959 <node id='#{node.id}' lon='1' lat='0' changeset='#{changeset.id}' version='2'/>
960 <node id='#{node.id}' lon='1' lat='1' changeset='#{changeset.id}' version='3'/>
961 <node id='#{node.id}' lon='1' lat='2' changeset='#{changeset.id}' version='4'/>
962 <node id='#{node.id}' lon='2' lat='2' changeset='#{changeset.id}' version='5'/>
963 <node id='#{node.id}' lon='3' lat='2' changeset='#{changeset.id}' version='6'/>
964 <node id='#{node.id}' lon='3' lat='3' changeset='#{changeset.id}' version='7'/>
965 <node id='#{node.id}' lon='9' lat='9' changeset='#{changeset.id}' version='8'/>
971 post changeset_upload_path(changeset), :params => diff, :headers => auth_header
972 assert_response :success,
973 "can't upload multiple versions of an element in a diff: #{@response.body}"
975 # check the response is well-formed. its counter-intuitive, but the
976 # API will return multiple elements with the same ID and different
977 # version numbers for each change we made.
978 assert_select "diffResult>node", 8
982 # upload multiple versions of the same element in the same diff, but
983 # keep the version numbers the same.
984 def test_upload_multiple_duplicate
986 changeset = create(:changeset)
988 auth_header = basic_authorization_header changeset.user.email, "test"
993 <node id='#{node.id}' lon='0' lat='0' changeset='#{changeset.id}' version='1'/>
994 <node id='#{node.id}' lon='1' lat='1' changeset='#{changeset.id}' version='1'/>
1000 post changeset_upload_path(changeset), :params => diff, :headers => auth_header
1001 assert_response :conflict,
1002 "shouldn't be able to upload the same element twice in a diff: #{@response.body}"
1006 # try to upload some elements without specifying the version
1007 def test_upload_missing_version
1008 changeset = create(:changeset)
1010 auth_header = basic_authorization_header changeset.user.email, "test"
1015 <node id='1' lon='1' lat='1' changeset='#{changeset.id}'/>
1021 post changeset_upload_path(changeset), :params => diff, :headers => auth_header
1022 assert_response :bad_request,
1023 "shouldn't be able to upload an element without version: #{@response.body}"
1027 # try to upload with commands other than create, modify, or delete
1028 def test_action_upload_invalid
1029 changeset = create(:changeset)
1031 auth_header = basic_authorization_header changeset.user.email, "test"
1036 <node id='1' lon='1' lat='1' changeset='#{changeset.id}' />
1040 post changeset_upload_path(changeset), :params => diff, :headers => auth_header
1041 assert_response :bad_request, "Shouldn't be able to upload a diff with the action ping"
1042 assert_equal("Unknown action ping, choices are create, modify, delete", @response.body)
1046 # upload a valid changeset which has a mixture of whitespace
1047 # to check a bug reported by ivansanchez (#1565).
1048 def test_upload_whitespace_valid
1049 changeset = create(:changeset)
1050 node = create(:node)
1051 way = create(:way_with_nodes, :nodes_count => 2)
1052 relation = create(:relation)
1053 other_relation = create(:relation)
1054 create(:relation_tag, :relation => relation)
1056 auth_header = basic_authorization_header changeset.user.email, "test"
1060 <modify><node id='#{node.id}' lon='0' lat='0' changeset='#{changeset.id}'
1062 <node id='#{node.id}' lon='1' lat='1' changeset='#{changeset.id}' version='2'><tag k='k' v='v'/></node></modify>
1064 <relation id='#{relation.id}' changeset='#{changeset.id}' version='1'><member
1065 type='way' role='some' ref='#{way.id}'/><member
1066 type='node' role='some' ref='#{node.id}'/>
1067 <member type='relation' role='some' ref='#{other_relation.id}'/>
1069 </modify></osmChange>
1073 post changeset_upload_path(changeset), :params => diff, :headers => auth_header
1074 assert_response :success,
1075 "can't upload a valid diff with whitespace variations to changeset: #{@response.body}"
1077 # check the response is well-formed
1078 assert_select "diffResult>node", 2
1079 assert_select "diffResult>relation", 1
1081 # check that the changes made it into the database
1082 assert_equal 1, Node.find(node.id).tags.size, "node #{node.id} should now have one tag"
1083 assert_equal 0, Relation.find(relation.id).tags.size, "relation #{relation.id} should now have no tags"
1087 # test that a placeholder can be reused within the same upload.
1088 def test_upload_reuse_placeholder_valid
1089 changeset = create(:changeset)
1091 auth_header = basic_authorization_header changeset.user.email, "test"
1096 <node id='-1' lon='0' lat='0' changeset='#{changeset.id}'>
1097 <tag k="foo" v="bar"/>
1101 <node id='-1' lon='1' lat='1' changeset='#{changeset.id}' version='1'/>
1104 <node id='-1' lon='2' lat='2' changeset='#{changeset.id}' version='2'/>
1110 post changeset_upload_path(changeset), :params => diff, :headers => auth_header
1111 assert_response :success,
1112 "can't upload a valid diff with re-used placeholders to changeset: #{@response.body}"
1114 # check the response is well-formed
1115 assert_select "diffResult>node", 3
1116 assert_select "diffResult>node[old_id='-1']", 3
1120 # test what happens if a diff upload re-uses placeholder IDs in an
1122 def test_upload_placeholder_invalid
1123 changeset = create(:changeset)
1125 auth_header = basic_authorization_header changeset.user.email, "test"
1130 <node id='-1' lon='0' lat='0' changeset='#{changeset.id}' version='1'/>
1131 <node id='-1' lon='1' lat='1' changeset='#{changeset.id}' version='1'/>
1132 <node id='-1' lon='2' lat='2' changeset='#{changeset.id}' version='2'/>
1138 post changeset_upload_path(changeset), :params => diff, :headers => auth_header
1139 assert_response :bad_request,
1140 "shouldn't be able to re-use placeholder IDs"
1142 # placeholder_ids must be unique across all action blocks
1146 <node id='-1' lon='0' lat='0' changeset='#{changeset.id}' version='1'/>
1149 <node id='-1' lon='1' lat='1' changeset='#{changeset.id}' version='1'/>
1155 post changeset_upload_path(changeset), :params => diff, :headers => auth_header
1156 assert_response :bad_request,
1157 "shouldn't be able to re-use placeholder IDs"
1160 def test_upload_process_order
1161 changeset = create(:changeset)
1163 auth_header = basic_authorization_header changeset.user.email, "test"
1168 <node id="-1" lat="1" lon="2" changeset="#{changeset.id}"/>
1169 <way id="-1" changeset="#{changeset.id}">
1173 <node id="-2" lat="1" lon="2" changeset="#{changeset.id}"/>
1179 post changeset_upload_path(changeset), :params => diff, :headers => auth_header
1180 assert_response :bad_request,
1181 "shouldn't refer elements behind it"
1184 def test_upload_duplicate_delete
1185 changeset = create(:changeset)
1187 auth_header = basic_authorization_header changeset.user.email, "test"
1192 <node id="-1" lat="39" lon="116" changeset="#{changeset.id}" />
1195 <node id="-1" version="1" changeset="#{changeset.id}" />
1196 <node id="-1" version="1" changeset="#{changeset.id}" />
1202 post changeset_upload_path(changeset), :params => diff, :headers => auth_header
1203 assert_response :gone,
1204 "transaction should be cancelled by second deletion"
1209 <node id="-1" lat="39" lon="116" changeset="#{changeset.id}" />
1211 <delete if-unused="true">
1212 <node id="-1" version="1" changeset="#{changeset.id}" />
1213 <node id="-1" version="1" changeset="#{changeset.id}" />
1219 post changeset_upload_path(changeset), :params => diff, :headers => auth_header
1221 assert_select "diffResult>node", 3
1222 assert_select "diffResult>node[old_id='-1']", 3
1223 assert_select "diffResult>node[new_version='1']", 1
1224 assert_select "diffResult>node[new_version='2']", 1
1228 # test that uploading a way referencing invalid placeholders gives a
1229 # proper error, not a 500.
1230 def test_upload_placeholder_invalid_way
1231 changeset = create(:changeset)
1234 auth_header = basic_authorization_header changeset.user.email, "test"
1239 <node id="-1" lon="0" lat="0" changeset="#{changeset.id}" version="1"/>
1240 <node id="-2" lon="1" lat="1" changeset="#{changeset.id}" version="1"/>
1241 <node id="-3" lon="2" lat="2" changeset="#{changeset.id}" version="1"/>
1242 <way id="-1" changeset="#{changeset.id}" version="1">
1253 post changeset_upload_path(changeset), :params => diff, :headers => auth_header
1254 assert_response :bad_request,
1255 "shouldn't be able to use invalid placeholder IDs"
1256 assert_equal "Placeholder node not found for reference -4 in way -1", @response.body
1258 # the same again, but this time use an existing way
1262 <node id="-1" lon="0" lat="0" changeset="#{changeset.id}" version="1"/>
1263 <node id="-2" lon="1" lat="1" changeset="#{changeset.id}" version="1"/>
1264 <node id="-3" lon="2" lat="2" changeset="#{changeset.id}" version="1"/>
1265 <way id="#{way.id}" changeset="#{changeset.id}" version="1">
1276 post changeset_upload_path(changeset), :params => diff, :headers => auth_header
1277 assert_response :bad_request,
1278 "shouldn't be able to use invalid placeholder IDs"
1279 assert_equal "Placeholder node not found for reference -4 in way #{way.id}", @response.body
1283 # test that uploading a relation referencing invalid placeholders gives a
1284 # proper error, not a 500.
1285 def test_upload_placeholder_invalid_relation
1286 changeset = create(:changeset)
1287 relation = create(:relation)
1289 auth_header = basic_authorization_header changeset.user.email, "test"
1294 <node id="-1" lon="0" lat="0" changeset="#{changeset.id}" version="1"/>
1295 <node id="-2" lon="1" lat="1" changeset="#{changeset.id}" version="1"/>
1296 <node id="-3" lon="2" lat="2" changeset="#{changeset.id}" version="1"/>
1297 <relation id="-1" changeset="#{changeset.id}" version="1">
1298 <member type="node" role="foo" ref="-1"/>
1299 <member type="node" role="foo" ref="-2"/>
1300 <member type="node" role="foo" ref="-3"/>
1301 <member type="node" role="foo" ref="-4"/>
1308 post changeset_upload_path(changeset), :params => diff, :headers => auth_header
1309 assert_response :bad_request,
1310 "shouldn't be able to use invalid placeholder IDs"
1311 assert_equal "Placeholder Node not found for reference -4 in relation -1.", @response.body
1313 # the same again, but this time use an existing relation
1317 <node id="-1" lon="0" lat="0" changeset="#{changeset.id}" version="1"/>
1318 <node id="-2" lon="1" lat="1" changeset="#{changeset.id}" version="1"/>
1319 <node id="-3" lon="2" lat="2" changeset="#{changeset.id}" version="1"/>
1320 <relation id="#{relation.id}" changeset="#{changeset.id}" version="1">
1321 <member type="node" role="foo" ref="-1"/>
1322 <member type="node" role="foo" ref="-2"/>
1323 <member type="node" role="foo" ref="-3"/>
1324 <member type="way" role="bar" ref="-1"/>
1331 post changeset_upload_path(changeset), :params => diff, :headers => auth_header
1332 assert_response :bad_request,
1333 "shouldn't be able to use invalid placeholder IDs"
1334 assert_equal "Placeholder Way not found for reference -1 in relation #{relation.id}.", @response.body
1338 # test what happens if a diff is uploaded containing only a node
1340 def test_upload_node_move
1341 auth_header = basic_authorization_header create(:user).email, "test"
1343 xml = "<osm><changeset>" \
1344 "<tag k='created_by' v='osm test suite checking changesets'/>" \
1345 "</changeset></osm>"
1346 put changeset_create_path, :params => xml, :headers => auth_header
1347 assert_response :success
1348 changeset_id = @response.body.to_i
1350 old_node = create(:node, :lat => 1, :lon => 1)
1352 diff = XML::Document.new
1353 diff.root = XML::Node.new "osmChange"
1354 modify = XML::Node.new "modify"
1355 xml_old_node = xml_node_for_node(old_node)
1356 xml_old_node["lat"] = 2.0.to_s
1357 xml_old_node["lon"] = 2.0.to_s
1358 xml_old_node["changeset"] = changeset_id.to_s
1359 modify << xml_old_node
1363 post changeset_upload_path(:id => changeset_id), :params => diff.to_s, :headers => auth_header
1364 assert_response :success,
1365 "diff should have uploaded OK"
1368 changeset = Changeset.find(changeset_id)
1369 assert_equal 1 * GeoRecord::SCALE, changeset.min_lon, "min_lon should be 1 degree"
1370 assert_equal 2 * GeoRecord::SCALE, changeset.max_lon, "max_lon should be 2 degrees"
1371 assert_equal 1 * GeoRecord::SCALE, changeset.min_lat, "min_lat should be 1 degree"
1372 assert_equal 2 * GeoRecord::SCALE, changeset.max_lat, "max_lat should be 2 degrees"
1376 # test what happens if a diff is uploaded adding a node to a way.
1377 def test_upload_way_extend
1378 auth_header = basic_authorization_header create(:user).email, "test"
1380 xml = "<osm><changeset>" \
1381 "<tag k='created_by' v='osm test suite checking changesets'/>" \
1382 "</changeset></osm>"
1383 put changeset_create_path, :params => xml, :headers => auth_header
1384 assert_response :success
1385 changeset_id = @response.body.to_i
1387 old_way = create(:way)
1388 create(:way_node, :way => old_way, :node => create(:node, :lat => 1, :lon => 1))
1390 diff = XML::Document.new
1391 diff.root = XML::Node.new "osmChange"
1392 modify = XML::Node.new "modify"
1393 xml_old_way = xml_node_for_way(old_way)
1394 nd_ref = XML::Node.new "nd"
1395 nd_ref["ref"] = create(:node, :lat => 3, :lon => 3).id.to_s
1396 xml_old_way << nd_ref
1397 xml_old_way["changeset"] = changeset_id.to_s
1398 modify << xml_old_way
1402 post changeset_upload_path(:id => changeset_id), :params => diff.to_s, :headers => auth_header
1403 assert_response :success,
1404 "diff should have uploaded OK"
1407 changeset = Changeset.find(changeset_id)
1408 assert_equal 1 * GeoRecord::SCALE, changeset.min_lon, "min_lon should be 1 degree"
1409 assert_equal 3 * GeoRecord::SCALE, changeset.max_lon, "max_lon should be 3 degrees"
1410 assert_equal 1 * GeoRecord::SCALE, changeset.min_lat, "min_lat should be 1 degree"
1411 assert_equal 3 * GeoRecord::SCALE, changeset.max_lat, "max_lat should be 3 degrees"
1415 # test for more issues in #1568
1416 def test_upload_empty_invalid
1417 changeset = create(:changeset)
1419 auth_header = basic_authorization_header changeset.user.email, "test"
1422 "<osmChange></osmChange>",
1423 "<osmChange><modify/></osmChange>",
1424 "<osmChange><modify></modify></osmChange>"].each do |diff|
1426 post changeset_upload_path(changeset), :params => diff, :headers => auth_header
1427 assert_response(:success, "should be able to upload " \
1428 "empty changeset: " + diff)
1433 # test that the X-Error-Format header works to request XML errors
1434 def test_upload_xml_errors
1435 changeset = create(:changeset)
1436 node = create(:node)
1437 create(:relation_member, :member => node)
1439 auth_header = basic_authorization_header changeset.user.email, "test"
1441 # try and delete a node that is in use
1442 diff = XML::Document.new
1443 diff.root = XML::Node.new "osmChange"
1444 delete = XML::Node.new "delete"
1446 delete << xml_node_for_node(node)
1449 error_header = error_format_header "xml"
1450 post changeset_upload_path(changeset), :params => diff.to_s, :headers => auth_header.merge(error_header)
1451 assert_response :success,
1452 "failed to return error in XML format"
1454 # check the returned payload
1455 assert_select "osmError[version='#{Settings.api_version}'][generator='#{Settings.generator}']", 1
1456 assert_select "osmError>status", 1
1457 assert_select "osmError>message", 1
1460 def test_upload_not_found
1461 changeset = create(:changeset)
1463 auth_header = basic_authorization_header changeset.user.email, "test"
1469 <node id='-1' lon='0' lat='0' changeset='#{changeset.id}' version='1'/>
1475 post changeset_upload_path(changeset), :params => diff, :headers => auth_header
1476 assert_response :not_found, "Node should not be found"
1482 <way id='-1' lon='0' lat='0' changeset='#{changeset.id}' version='1'/>
1488 post changeset_upload_path(changeset), :params => diff, :headers => auth_header
1489 assert_response :not_found, "Way should not be found"
1495 <relation id='-1' lon='0' lat='0' changeset='#{changeset.id}' version='1'/>
1501 post changeset_upload_path(changeset), :params => diff, :headers => auth_header
1502 assert_response :not_found, "Relation should not be found"
1508 <node id='-1' lon='0' lat='0' changeset='#{changeset.id}' version='1'/>
1514 post changeset_upload_path(changeset), :params => diff, :headers => auth_header
1515 assert_response :not_found, "Node should not be deleted"
1521 <way id='-1' lon='0' lat='0' changeset='#{changeset.id}' version='1'/>
1527 post changeset_upload_path(changeset), :params => diff, :headers => auth_header
1528 assert_response :not_found, "Way should not be deleted"
1534 <relation id='-1' lon='0' lat='0' changeset='#{changeset.id}' version='1'/>
1540 post changeset_upload_path(changeset), :params => diff, :headers => auth_header
1541 assert_response :not_found, "Relation should not be deleted"
1544 def test_upload_relation_placeholder_not_fix
1545 changeset = create(:changeset)
1547 auth_header = basic_authorization_header changeset.user.email, "test"
1551 <osmChange version='0.6'>
1553 <relation id='-2' version='0' changeset='#{changeset.id}'>
1554 <member type='relation' role='' ref='-4' />
1555 <tag k='type' v='route' />
1556 <tag k='name' v='AtoB' />
1558 <relation id='-3' version='0' changeset='#{changeset.id}'>
1559 <tag k='type' v='route' />
1560 <tag k='name' v='BtoA' />
1562 <relation id='-4' version='0' changeset='#{changeset.id}'>
1563 <member type='relation' role='' ref='-2' />
1564 <member type='relation' role='' ref='-3' />
1565 <tag k='type' v='route_master' />
1566 <tag k='name' v='master' />
1573 post changeset_upload_path(changeset), :params => diff.to_s, :headers => auth_header
1574 assert_response :bad_request, "shouldn't be able to use reference -4 in relation -2: #{@response.body}"
1575 assert_equal "Placeholder Relation not found for reference -4 in relation -2.", @response.body
1578 def test_upload_multiple_delete_block
1579 changeset = create(:changeset)
1581 auth_header = basic_authorization_header changeset.user.email, "test"
1583 node = create(:node)
1585 create(:way_node, :way => way, :node => node)
1586 alone_node = create(:node)
1590 <osmChange version='0.6'>
1591 <delete version="0.6">
1592 <node id="#{node.id}" version="#{node.version}" changeset="#{changeset.id}"/>
1594 <delete version="0.6" if-unused="true">
1595 <node id="#{alone_node.id}" version="#{alone_node.version}" changeset="#{changeset.id}"/>
1601 post changeset_upload_path(changeset), :params => diff.to_s, :headers => auth_header
1602 assert_response :precondition_failed,
1603 "shouldn't be able to upload a invalid deletion diff: #{@response.body}"
1604 assert_equal "Precondition failed: Node #{node.id} is still used by ways #{way.id}.", @response.body
1608 # when we make some simple changes we get the same changes back from the
1610 def test_diff_download_simple
1611 node = create(:node)
1613 ## First try with a non-public user, which should get a forbidden
1614 auth_header = basic_authorization_header create(:user, :data_public => false).email, "test"
1616 # create a temporary changeset
1617 xml = "<osm><changeset>" \
1618 "<tag k='created_by' v='osm test suite checking changesets'/>" \
1619 "</changeset></osm>"
1620 put changeset_create_path, :params => xml, :headers => auth_header
1621 assert_response :forbidden
1623 ## Now try with a normal user
1624 auth_header = basic_authorization_header create(:user).email, "test"
1626 # create a temporary changeset
1627 xml = "<osm><changeset>" \
1628 "<tag k='created_by' v='osm test suite checking changesets'/>" \
1629 "</changeset></osm>"
1630 put changeset_create_path, :params => xml, :headers => auth_header
1631 assert_response :success
1632 changeset_id = @response.body.to_i
1638 <node id='#{node.id}' lon='0' lat='0' changeset='#{changeset_id}' version='1'/>
1639 <node id='#{node.id}' lon='1' lat='0' changeset='#{changeset_id}' version='2'/>
1640 <node id='#{node.id}' lon='1' lat='1' changeset='#{changeset_id}' version='3'/>
1641 <node id='#{node.id}' lon='1' lat='2' changeset='#{changeset_id}' version='4'/>
1642 <node id='#{node.id}' lon='2' lat='2' changeset='#{changeset_id}' version='5'/>
1643 <node id='#{node.id}' lon='3' lat='2' changeset='#{changeset_id}' version='6'/>
1644 <node id='#{node.id}' lon='3' lat='3' changeset='#{changeset_id}' version='7'/>
1645 <node id='#{node.id}' lon='9' lat='9' changeset='#{changeset_id}' version='8'/>
1651 post changeset_upload_path(:id => changeset_id), :params => diff, :headers => auth_header
1652 assert_response :success,
1653 "can't upload multiple versions of an element in a diff: #{@response.body}"
1655 get changeset_download_path(:id => changeset_id)
1656 assert_response :success
1658 assert_select "osmChange", 1
1659 assert_select "osmChange>modify", 8
1660 assert_select "osmChange>modify>node", 8
1664 # culled this from josm to ensure that nothing in the way that josm
1665 # is formatting the request is causing it to fail.
1667 # NOTE: the error turned out to be something else completely!
1668 def test_josm_upload
1669 auth_header = basic_authorization_header create(:user).email, "test"
1671 # create a temporary changeset
1672 xml = "<osm><changeset>" \
1673 "<tag k='created_by' v='osm test suite checking changesets'/>" \
1674 "</changeset></osm>"
1675 put changeset_create_path, :params => xml, :headers => auth_header
1676 assert_response :success
1677 changeset_id = @response.body.to_i
1680 <osmChange version="0.6" generator="JOSM">
1681 <create version="0.6" generator="JOSM">
1682 <node id='-1' visible='true' changeset='#{changeset_id}' lat='51.49619982187321' lon='-0.18722061869438314' />
1683 <node id='-2' visible='true' changeset='#{changeset_id}' lat='51.496359883909605' lon='-0.18653093576241928' />
1684 <node id='-3' visible='true' changeset='#{changeset_id}' lat='51.49598132358285' lon='-0.18719613290981638' />
1685 <node id='-4' visible='true' changeset='#{changeset_id}' lat='51.4961591711078' lon='-0.18629015888084607' />
1686 <node id='-5' visible='true' changeset='#{changeset_id}' lat='51.49582126021711' lon='-0.18708186591517145' />
1687 <node id='-6' visible='true' changeset='#{changeset_id}' lat='51.49591018437858' lon='-0.1861432441734455' />
1688 <node id='-7' visible='true' changeset='#{changeset_id}' lat='51.49560784152179' lon='-0.18694719410005425' />
1689 <node id='-8' visible='true' changeset='#{changeset_id}' lat='51.49567389979617' lon='-0.1860289771788006' />
1690 <node id='-9' visible='true' changeset='#{changeset_id}' lat='51.49543761398892' lon='-0.186820684213126' />
1691 <way id='-10' action='modify' visible='true' changeset='#{changeset_id}'>
1701 <tag k='highway' v='residential' />
1702 <tag k='name' v='Foobar Street' />
1709 post changeset_upload_path(:id => changeset_id), :params => diff, :headers => auth_header
1710 assert_response :success,
1711 "can't upload a diff from JOSM: #{@response.body}"
1713 get changeset_download_path(:id => changeset_id)
1714 assert_response :success
1716 assert_select "osmChange", 1
1717 assert_select "osmChange>create>node", 9
1718 assert_select "osmChange>create>way", 1
1719 assert_select "osmChange>create>way>nd", 9
1720 assert_select "osmChange>create>way>tag", 2
1724 # when we make some complex changes we get the same changes back from the
1726 def test_diff_download_complex
1727 node = create(:node)
1728 node2 = create(:node)
1730 auth_header = basic_authorization_header create(:user).email, "test"
1732 # create a temporary changeset
1733 xml = "<osm><changeset>" \
1734 "<tag k='created_by' v='osm test suite checking changesets'/>" \
1735 "</changeset></osm>"
1736 put changeset_create_path, :params => xml, :headers => auth_header
1737 assert_response :success
1738 changeset_id = @response.body.to_i
1744 <node id='#{node.id}' lon='0' lat='0' changeset='#{changeset_id}' version='1'/>
1747 <node id='-1' lon='9' lat='9' changeset='#{changeset_id}' version='0'/>
1748 <node id='-2' lon='8' lat='9' changeset='#{changeset_id}' version='0'/>
1749 <node id='-3' lon='7' lat='9' changeset='#{changeset_id}' version='0'/>
1752 <node id='#{node2.id}' lon='20' lat='15' changeset='#{changeset_id}' version='1'/>
1753 <way id='#{way.id}' changeset='#{changeset_id}' version='1'>
1754 <nd ref='#{node2.id}'/>
1764 post changeset_upload_path(:id => changeset_id), :params => diff, :headers => auth_header
1765 assert_response :success,
1766 "can't upload multiple versions of an element in a diff: #{@response.body}"
1768 get changeset_download_path(:id => changeset_id)
1769 assert_response :success
1771 assert_select "osmChange", 1
1772 assert_select "osmChange>create", 3
1773 assert_select "osmChange>delete", 1
1774 assert_select "osmChange>modify", 2
1775 assert_select "osmChange>create>node", 3
1776 assert_select "osmChange>delete>node", 1
1777 assert_select "osmChange>modify>node", 1
1778 assert_select "osmChange>modify>way", 1
1781 def test_changeset_download
1782 changeset = create(:changeset)
1783 node = create(:node, :with_history, :version => 1, :changeset => changeset)
1784 tag = create(:old_node_tag, :old_node => node.old_nodes.find_by(:version => 1))
1785 node2 = create(:node, :with_history, :version => 1, :changeset => changeset)
1786 _node3 = create(:node, :with_history, :deleted, :version => 1, :changeset => changeset)
1787 _relation = create(:relation, :with_history, :version => 1, :changeset => changeset)
1788 _relation2 = create(:relation, :with_history, :deleted, :version => 1, :changeset => changeset)
1790 get changeset_download_path(changeset)
1792 assert_response :success
1794 # FIXME: needs more assert_select tests
1795 assert_select "osmChange[version='#{Settings.api_version}'][generator='#{Settings.generator}']" do
1796 assert_select "create", :count => 5
1797 assert_select "create>node[id='#{node.id}'][visible='#{node.visible?}'][version='#{node.version}']" do
1798 assert_select "tag[k='#{tag.k}'][v='#{tag.v}']"
1800 assert_select "create>node[id='#{node2.id}']"
1805 # check that the bounding box of a changeset gets updated correctly
1806 # FIXME: This should really be moded to a integration test due to the with_controller
1807 def test_changeset_bbox
1809 create(:way_node, :way => way, :node => create(:node, :lat => 3, :lon => 3))
1811 auth_header = basic_authorization_header create(:user).email, "test"
1813 # create a new changeset
1814 xml = "<osm><changeset/></osm>"
1815 put changeset_create_path, :params => xml, :headers => auth_header
1816 assert_response :success, "Creating of changeset failed."
1817 changeset_id = @response.body.to_i
1819 # add a single node to it
1820 with_controller(NodesController.new) do
1821 xml = "<osm><node lon='1' lat='2' changeset='#{changeset_id}'/></osm>"
1822 put node_create_path, :params => xml, :headers => auth_header
1823 assert_response :success, "Couldn't create node."
1826 # get the bounding box back from the changeset
1827 get changeset_show_path(:id => changeset_id)
1828 assert_response :success, "Couldn't read back changeset."
1829 assert_select "osm>changeset[min_lon='1.0000000']", 1
1830 assert_select "osm>changeset[max_lon='1.0000000']", 1
1831 assert_select "osm>changeset[min_lat='2.0000000']", 1
1832 assert_select "osm>changeset[max_lat='2.0000000']", 1
1834 # add another node to it
1835 with_controller(NodesController.new) do
1836 xml = "<osm><node lon='2' lat='1' changeset='#{changeset_id}'/></osm>"
1837 put node_create_path, :params => xml, :headers => auth_header
1838 assert_response :success, "Couldn't create second node."
1841 # get the bounding box back from the changeset
1842 get changeset_show_path(:id => changeset_id)
1843 assert_response :success, "Couldn't read back changeset for the second time."
1844 assert_select "osm>changeset[min_lon='1.0000000']", 1
1845 assert_select "osm>changeset[max_lon='2.0000000']", 1
1846 assert_select "osm>changeset[min_lat='1.0000000']", 1
1847 assert_select "osm>changeset[max_lat='2.0000000']", 1
1849 # add (delete) a way to it, which contains a point at (3,3)
1850 with_controller(WaysController.new) do
1851 xml = update_changeset(xml_for_way(way), changeset_id)
1852 delete api_way_path(way), :params => xml.to_s, :headers => auth_header
1853 assert_response :success, "Couldn't delete a way."
1856 # get the bounding box back from the changeset
1857 get changeset_show_path(:id => changeset_id)
1858 assert_response :success, "Couldn't read back changeset for the third time."
1859 assert_select "osm>changeset[min_lon='1.0000000']", 1
1860 assert_select "osm>changeset[max_lon='3.0000000']", 1
1861 assert_select "osm>changeset[min_lat='1.0000000']", 1
1862 assert_select "osm>changeset[max_lat='3.0000000']", 1
1866 # test the query functionality of changesets
1868 private_user = create(:user, :data_public => false)
1869 private_user_changeset = create(:changeset, :user => private_user)
1870 private_user_closed_changeset = create(:changeset, :closed, :user => private_user)
1871 user = create(:user)
1872 changeset = create(:changeset, :user => user)
1873 closed_changeset = create(:changeset, :closed, :user => user, :created_at => Time.utc(2008, 1, 1, 0, 0, 0), :closed_at => Time.utc(2008, 1, 2, 0, 0, 0))
1874 changeset2 = create(:changeset, :min_lat => (5 * GeoRecord::SCALE).round, :min_lon => (5 * GeoRecord::SCALE).round, :max_lat => (15 * GeoRecord::SCALE).round, :max_lon => (15 * GeoRecord::SCALE).round)
1875 changeset3 = create(:changeset, :min_lat => (4.5 * GeoRecord::SCALE).round, :min_lon => (4.5 * GeoRecord::SCALE).round, :max_lat => (5 * GeoRecord::SCALE).round, :max_lon => (5 * GeoRecord::SCALE).round)
1877 get changesets_path(:bbox => "-10,-10, 10, 10")
1878 assert_response :success, "can't get changesets in bbox"
1879 assert_changesets [changeset2, changeset3]
1881 get changesets_path(:bbox => "4.5,4.5,4.6,4.6")
1882 assert_response :success, "can't get changesets in bbox"
1883 assert_changesets [changeset3]
1885 # not found when looking for changesets of non-existing users
1886 get changesets_path(:user => User.maximum(:id) + 1)
1887 assert_response :not_found
1888 assert_equal "text/plain", @response.media_type
1889 get changesets_path(:display_name => " ")
1890 assert_response :not_found
1891 assert_equal "text/plain", @response.media_type
1893 # can't get changesets of user 1 without authenticating
1894 get changesets_path(:user => private_user.id)
1895 assert_response :not_found, "shouldn't be able to get changesets by non-public user (ID)"
1896 get changesets_path(:display_name => private_user.display_name)
1897 assert_response :not_found, "shouldn't be able to get changesets by non-public user (name)"
1899 # but this should work
1900 auth_header = basic_authorization_header private_user.email, "test"
1901 get changesets_path(:user => private_user.id), :headers => auth_header
1902 assert_response :success, "can't get changesets by user ID"
1903 assert_changesets [private_user_changeset, private_user_closed_changeset]
1905 get changesets_path(:display_name => private_user.display_name), :headers => auth_header
1906 assert_response :success, "can't get changesets by user name"
1907 assert_changesets [private_user_changeset, private_user_closed_changeset]
1909 # test json endpoint
1910 get changesets_path(:display_name => private_user.display_name), :headers => auth_header, :params => { :format => "json" }
1911 assert_response :success, "can't get changesets by user name"
1913 js = ActiveSupport::JSON.decode(@response.body)
1916 assert_equal Settings.api_version, js["version"]
1917 assert_equal Settings.generator, js["generator"]
1918 assert_equal 2, js["changesets"].count
1920 # check that the correct error is given when we provide both UID and name
1921 get changesets_path(:user => private_user.id,
1922 :display_name => private_user.display_name), :headers => auth_header
1923 assert_response :bad_request, "should be a bad request to have both ID and name specified"
1925 get changesets_path(:user => private_user.id, :open => true), :headers => auth_header
1926 assert_response :success, "can't get changesets by user and open"
1927 assert_changesets [private_user_changeset]
1929 get changesets_path(:time => "2007-12-31"), :headers => auth_header
1930 assert_response :success, "can't get changesets by time-since"
1931 assert_changesets [private_user_changeset, private_user_closed_changeset, changeset, closed_changeset, changeset2, changeset3]
1933 get changesets_path(:time => "2008-01-01T12:34Z"), :headers => auth_header
1934 assert_response :success, "can't get changesets by time-since with hour"
1935 assert_changesets [private_user_changeset, private_user_closed_changeset, changeset, closed_changeset, changeset2, changeset3]
1937 get changesets_path(:time => "2007-12-31T23:59Z,2008-01-02T00:01Z"), :headers => auth_header
1938 assert_response :success, "can't get changesets by time-range"
1939 assert_changesets [closed_changeset]
1941 get changesets_path(:open => "true"), :headers => auth_header
1942 assert_response :success, "can't get changesets by open-ness"
1943 assert_changesets [private_user_changeset, changeset, changeset2, changeset3]
1945 get changesets_path(:closed => "true"), :headers => auth_header
1946 assert_response :success, "can't get changesets by closed-ness"
1947 assert_changesets [private_user_closed_changeset, closed_changeset]
1949 get changesets_path(:closed => "true", :user => private_user.id), :headers => auth_header
1950 assert_response :success, "can't get changesets by closed-ness and user"
1951 assert_changesets [private_user_closed_changeset]
1953 get changesets_path(:closed => "true", :user => user.id), :headers => auth_header
1954 assert_response :success, "can't get changesets by closed-ness and user"
1955 assert_changesets [closed_changeset]
1957 get changesets_path(:changesets => "#{private_user_changeset.id},#{changeset.id},#{closed_changeset.id}"), :headers => auth_header
1958 assert_response :success, "can't get changesets by id (as comma-separated string)"
1959 assert_changesets [private_user_changeset, changeset, closed_changeset]
1961 get changesets_path(:changesets => ""), :headers => auth_header
1962 assert_response :bad_request, "should be a bad request since changesets is empty"
1966 # test the query functionality of changesets with the limit parameter
1967 def test_query_limit
1968 user = create(:user)
1969 changeset1 = create(:changeset, :closed, :user => user, :created_at => Time.utc(2008, 1, 1, 0, 0, 0), :closed_at => Time.utc(2008, 1, 2, 0, 0, 0))
1970 changeset2 = create(:changeset, :closed, :user => user, :created_at => Time.utc(2008, 2, 1, 0, 0, 0), :closed_at => Time.utc(2008, 2, 2, 0, 0, 0))
1971 changeset3 = create(:changeset, :closed, :user => user, :created_at => Time.utc(2008, 3, 1, 0, 0, 0), :closed_at => Time.utc(2008, 3, 2, 0, 0, 0))
1972 changeset4 = create(:changeset, :closed, :user => user, :created_at => Time.utc(2008, 4, 1, 0, 0, 0), :closed_at => Time.utc(2008, 4, 2, 0, 0, 0))
1973 changeset5 = create(:changeset, :closed, :user => user, :created_at => Time.utc(2008, 5, 1, 0, 0, 0), :closed_at => Time.utc(2008, 5, 2, 0, 0, 0))
1976 assert_response :success
1977 assert_changesets_in_order [changeset5, changeset4, changeset3, changeset2, changeset1]
1979 get changesets_path(:limit => "3")
1980 assert_response :success
1981 assert_changesets_in_order [changeset5, changeset4, changeset3]
1983 get changesets_path(:limit => "0")
1984 assert_response :bad_request
1986 get changesets_path(:limit => Settings.max_changeset_query_limit)
1987 assert_response :success
1988 assert_changesets_in_order [changeset5, changeset4, changeset3, changeset2, changeset1]
1990 get changesets_path(:limit => Settings.max_changeset_query_limit + 1)
1991 assert_response :bad_request
1995 # test the query functionality of sequential changesets with order and time parameters
1996 def test_query_order
1997 user = create(:user)
1998 changeset1 = create(:changeset, :closed, :user => user, :created_at => Time.utc(2008, 1, 1, 0, 0, 0), :closed_at => Time.utc(2008, 1, 2, 0, 0, 0))
1999 changeset2 = create(:changeset, :closed, :user => user, :created_at => Time.utc(2008, 2, 1, 0, 0, 0), :closed_at => Time.utc(2008, 2, 2, 0, 0, 0))
2000 changeset3 = create(:changeset, :closed, :user => user, :created_at => Time.utc(2008, 3, 1, 0, 0, 0), :closed_at => Time.utc(2008, 3, 2, 0, 0, 0))
2001 changeset4 = create(:changeset, :closed, :user => user, :created_at => Time.utc(2008, 4, 1, 0, 0, 0), :closed_at => Time.utc(2008, 4, 2, 0, 0, 0))
2002 changeset5 = create(:changeset, :closed, :user => user, :created_at => Time.utc(2008, 5, 1, 0, 0, 0), :closed_at => Time.utc(2008, 5, 2, 0, 0, 0))
2003 changeset6 = create(:changeset, :closed, :user => user, :created_at => Time.utc(2008, 6, 1, 0, 0, 0), :closed_at => Time.utc(2008, 6, 2, 0, 0, 0))
2006 assert_response :success
2007 assert_changesets_in_order [changeset6, changeset5, changeset4, changeset3, changeset2, changeset1]
2009 get changesets_path(:order => "oldest")
2010 assert_response :success
2011 assert_changesets_in_order [changeset1, changeset2, changeset3, changeset4, changeset5, changeset6]
2014 # lower time bound at the opening time of a changeset
2015 ["2008-02-01T00:00:00Z", "2008-05-15T00:00:00Z", [changeset5, changeset4, changeset3, changeset2], [changeset5, changeset4, changeset3, changeset2]],
2016 # lower time bound in the middle of a changeset
2017 ["2008-02-01T12:00:00Z", "2008-05-15T00:00:00Z", [changeset5, changeset4, changeset3, changeset2], [changeset5, changeset4, changeset3]],
2018 # lower time bound at the closing time of a changeset
2019 ["2008-02-02T00:00:00Z", "2008-05-15T00:00:00Z", [changeset5, changeset4, changeset3, changeset2], [changeset5, changeset4, changeset3]],
2020 # lower time bound after the closing time of a changeset
2021 ["2008-02-02T00:00:01Z", "2008-05-15T00:00:00Z", [changeset5, changeset4, changeset3], [changeset5, changeset4, changeset3]],
2022 # upper time bound in the middle of a changeset
2023 ["2007-09-09T12:00:00Z", "2008-04-01T12:00:00Z", [changeset4, changeset3, changeset2, changeset1], [changeset4, changeset3, changeset2, changeset1]],
2025 ["2009-02-02T00:00:01Z", "2018-05-15T00:00:00Z", [], []]
2026 ].each do |from, to, interval_changesets, point_changesets|
2027 get changesets_path(:time => "#{from},#{to}")
2028 assert_response :success
2029 assert_changesets_in_order interval_changesets
2031 get changesets_path(:from => from, :to => to)
2032 assert_response :success
2033 assert_changesets_in_order point_changesets
2035 get changesets_path(:from => from, :to => to, :order => "oldest")
2036 assert_response :success
2037 assert_changesets_in_order point_changesets.reverse
2042 # test the query functionality of overlapping changesets with order and time parameters
2043 def test_query_order_overlapping
2044 user = create(:user)
2045 changeset1 = create(:changeset, :closed, :user => user, :created_at => Time.utc(2015, 6, 4, 17, 0, 0), :closed_at => Time.utc(2015, 6, 4, 17, 0, 0))
2046 changeset2 = create(:changeset, :closed, :user => user, :created_at => Time.utc(2015, 6, 4, 16, 0, 0), :closed_at => Time.utc(2015, 6, 4, 18, 0, 0))
2047 changeset3 = create(:changeset, :closed, :user => user, :created_at => Time.utc(2015, 6, 4, 14, 0, 0), :closed_at => Time.utc(2015, 6, 4, 20, 0, 0))
2048 changeset4 = create(:changeset, :closed, :user => user, :created_at => Time.utc(2015, 6, 3, 23, 0, 0), :closed_at => Time.utc(2015, 6, 4, 23, 0, 0))
2049 create(:changeset, :closed, :user => user, :created_at => Time.utc(2015, 6, 2, 23, 0, 0), :closed_at => Time.utc(2015, 6, 3, 23, 0, 0))
2051 get changesets_path(:time => "2015-06-04T00:00:00Z")
2052 assert_response :success
2053 assert_changesets_in_order [changeset1, changeset2, changeset3, changeset4]
2055 get changesets_path(:from => "2015-06-04T00:00:00Z")
2056 assert_response :success
2057 assert_changesets_in_order [changeset1, changeset2, changeset3]
2059 get changesets_path(:from => "2015-06-04T00:00:00Z", :order => "oldest")
2060 assert_response :success
2061 assert_changesets_in_order [changeset3, changeset2, changeset1]
2063 get changesets_path(:time => "2015-06-04T16:00:00Z,2015-06-04T17:30:00Z")
2064 assert_response :success
2065 assert_changesets_in_order [changeset1, changeset2, changeset3, changeset4]
2067 get changesets_path(:from => "2015-06-04T16:00:00Z", :to => "2015-06-04T17:30:00Z")
2068 assert_response :success
2069 assert_changesets_in_order [changeset1, changeset2]
2071 get changesets_path(:from => "2015-06-04T16:00:00Z", :to => "2015-06-04T17:30:00Z", :order => "oldest")
2072 assert_response :success
2073 assert_changesets_in_order [changeset2, changeset1]
2077 # check that errors are returned if garbage is inserted
2078 # into query strings
2079 def test_query_invalid
2082 ";drop table users;"].each do |bbox|
2083 get changesets_path(:bbox => bbox)
2084 assert_response :bad_request, "'#{bbox}' isn't a bbox"
2089 ";drop table users;",
2091 "-,-"].each do |time|
2092 get changesets_path(:time => time)
2093 assert_response :bad_request, "'#{time}' isn't a valid time range"
2100 get changesets_path(:user => uid)
2101 assert_response :bad_request, "'#{uid}' isn't a valid user ID"
2104 get changesets_path(:order => "oldest", :time => "2008-01-01T00:00Z,2018-01-01T00:00Z")
2105 assert_response :bad_request, "cannot use order=oldest with time"
2109 # check updating tags on a changeset
2110 def test_changeset_update
2111 private_user = create(:user, :data_public => false)
2112 private_changeset = create(:changeset, :user => private_user)
2113 user = create(:user)
2114 changeset = create(:changeset, :user => user)
2116 ## First try with a non-public user
2117 new_changeset = create_changeset_xml(:user => private_user)
2118 new_tag = XML::Node.new "tag"
2119 new_tag["k"] = "tagtesting"
2120 new_tag["v"] = "valuetesting"
2121 new_changeset.find("//osm/changeset").first << new_tag
2123 # try without any authorization
2124 put changeset_show_path(private_changeset), :params => new_changeset.to_s
2125 assert_response :unauthorized
2127 # try with the wrong authorization
2128 auth_header = basic_authorization_header create(:user).email, "test"
2129 put changeset_show_path(private_changeset), :params => new_changeset.to_s, :headers => auth_header
2130 assert_response :conflict
2132 # now this should get an unauthorized
2133 auth_header = basic_authorization_header private_user.email, "test"
2134 put changeset_show_path(private_changeset), :params => new_changeset.to_s, :headers => auth_header
2135 assert_require_public_data "user with their data non-public, shouldn't be able to edit their changeset"
2137 ## Now try with the public user
2138 new_changeset = create_changeset_xml(:id => 1)
2139 new_tag = XML::Node.new "tag"
2140 new_tag["k"] = "tagtesting"
2141 new_tag["v"] = "valuetesting"
2142 new_changeset.find("//osm/changeset").first << new_tag
2144 # try without any authorization
2145 put changeset_show_path(changeset), :params => new_changeset.to_s
2146 assert_response :unauthorized
2148 # try with the wrong authorization
2149 auth_header = basic_authorization_header create(:user).email, "test"
2150 put changeset_show_path(changeset), :params => new_changeset.to_s, :headers => auth_header
2151 assert_response :conflict
2153 # now this should work...
2154 auth_header = basic_authorization_header user.email, "test"
2155 put changeset_show_path(changeset), :params => new_changeset.to_s, :headers => auth_header
2156 assert_response :success
2158 assert_select "osm>changeset[id='#{changeset.id}']", 1
2159 assert_select "osm>changeset>tag", 1
2160 assert_select "osm>changeset>tag[k='tagtesting'][v='valuetesting']", 1
2164 # check that a user different from the one who opened the changeset
2166 def test_changeset_update_invalid
2167 auth_header = basic_authorization_header create(:user).email, "test"
2169 changeset = create(:changeset)
2170 new_changeset = create_changeset_xml(:user => changeset.user, :id => changeset.id)
2171 new_tag = XML::Node.new "tag"
2172 new_tag["k"] = "testing"
2173 new_tag["v"] = "testing"
2174 new_changeset.find("//osm/changeset").first << new_tag
2176 put changeset_show_path(changeset), :params => new_changeset.to_s, :headers => auth_header
2177 assert_response :conflict
2181 # check that a changeset can contain a certain max number of changes.
2182 ## FIXME should be changed to an integration test due to the with_controller
2183 def test_changeset_limits
2184 auth_header = basic_authorization_header create(:user).email, "test"
2186 # open a new changeset
2187 xml = "<osm><changeset/></osm>"
2188 put changeset_create_path, :params => xml, :headers => auth_header
2189 assert_response :success, "can't create a new changeset"
2190 cs_id = @response.body.to_i
2192 # start the counter just short of where the changeset should finish.
2194 # alter the database to set the counter on the changeset directly,
2195 # otherwise it takes about 6 minutes to fill all of them.
2196 changeset = Changeset.find(cs_id)
2197 changeset.num_changes = Changeset::MAX_ELEMENTS - offset
2200 with_controller(NodesController.new) do
2202 xml = "<osm><node changeset='#{cs_id}' lat='0.0' lon='0.0'/></osm>"
2203 put node_create_path, :params => xml, :headers => auth_header
2204 assert_response :success, "can't create a new node"
2205 node_id = @response.body.to_i
2207 get api_node_path(:id => node_id)
2208 assert_response :success, "can't read back new node"
2209 node_doc = XML::Parser.string(@response.body).parse
2210 node_xml = node_doc.find("//osm/node").first
2212 # loop until we fill the changeset with nodes
2214 node_xml["lat"] = rand.to_s
2215 node_xml["lon"] = rand.to_s
2216 node_xml["version"] = (i + 1).to_s
2218 put api_node_path(:id => node_id), :params => node_doc.to_s, :headers => auth_header
2219 assert_response :success, "attempt #{i} should have succeeded"
2222 # trying again should fail
2223 node_xml["lat"] = rand.to_s
2224 node_xml["lon"] = rand.to_s
2225 node_xml["version"] = offset.to_s
2227 put api_node_path(:id => node_id), :params => node_doc.to_s, :headers => auth_header
2228 assert_response :conflict, "final attempt should have failed"
2231 changeset = Changeset.find(cs_id)
2232 assert_equal Changeset::MAX_ELEMENTS + 1, changeset.num_changes
2234 # check that the changeset is now closed as well
2235 assert_not(changeset.open?,
2236 "changeset should have been auto-closed by exceeding " \
2241 # check that the changeset download for a changeset with a redacted
2242 # element in it doesn't contain that element.
2243 def test_diff_download_redacted
2244 changeset = create(:changeset)
2245 node = create(:node, :with_history, :version => 2, :changeset => changeset)
2246 node_v1 = node.old_nodes.find_by(:version => 1)
2247 node_v1.redact!(create(:redaction))
2249 get changeset_download_path(changeset)
2250 assert_response :success
2252 assert_select "osmChange", 1
2253 # this changeset contains the node in versions 1 & 2, but 1 should
2255 assert_select "osmChange node[id='#{node.id}']", 1
2256 assert_select "osmChange node[id='#{node.id}'][version='1']", 0
2260 # test subscribe success
2261 def test_subscribe_success
2262 auth_header = basic_authorization_header create(:user).email, "test"
2263 changeset = create(:changeset, :closed)
2265 assert_difference "changeset.subscribers.count", 1 do
2266 post changeset_subscribe_path(changeset), :headers => auth_header
2268 assert_response :success
2270 # not closed changeset
2271 changeset = create(:changeset)
2272 assert_difference "changeset.subscribers.count", 1 do
2273 post changeset_subscribe_path(changeset), :headers => auth_header
2275 assert_response :success
2279 # test subscribe fail
2280 def test_subscribe_fail
2281 user = create(:user)
2284 changeset = create(:changeset, :closed)
2285 assert_no_difference "changeset.subscribers.count" do
2286 post changeset_subscribe_path(changeset)
2288 assert_response :unauthorized
2290 auth_header = basic_authorization_header user.email, "test"
2293 assert_no_difference "changeset.subscribers.count" do
2294 post changeset_subscribe_path(:id => 999111), :headers => auth_header
2296 assert_response :not_found
2298 # trying to subscribe when already subscribed
2299 changeset = create(:changeset, :closed)
2300 changeset.subscribers.push(user)
2301 assert_no_difference "changeset.subscribers.count" do
2302 post changeset_subscribe_path(changeset), :headers => auth_header
2304 assert_response :conflict
2308 # test unsubscribe success
2309 def test_unsubscribe_success
2310 user = create(:user)
2311 auth_header = basic_authorization_header user.email, "test"
2312 changeset = create(:changeset, :closed)
2313 changeset.subscribers.push(user)
2315 assert_difference "changeset.subscribers.count", -1 do
2316 post changeset_unsubscribe_path(changeset), :headers => auth_header
2318 assert_response :success
2320 # not closed changeset
2321 changeset = create(:changeset)
2322 changeset.subscribers.push(user)
2324 assert_difference "changeset.subscribers.count", -1 do
2325 post changeset_unsubscribe_path(changeset), :headers => auth_header
2327 assert_response :success
2331 # test unsubscribe fail
2332 def test_unsubscribe_fail
2334 changeset = create(:changeset, :closed)
2335 assert_no_difference "changeset.subscribers.count" do
2336 post changeset_unsubscribe_path(changeset)
2338 assert_response :unauthorized
2340 auth_header = basic_authorization_header create(:user).email, "test"
2343 assert_no_difference "changeset.subscribers.count" do
2344 post changeset_unsubscribe_path(:id => 999111), :headers => auth_header
2346 assert_response :not_found
2348 # trying to unsubscribe when not subscribed
2349 changeset = create(:changeset, :closed)
2350 assert_no_difference "changeset.subscribers.count" do
2351 post changeset_unsubscribe_path(changeset), :headers => auth_header
2353 assert_response :not_found
2359 # check that certain changesets exist in the output
2360 def assert_changesets(changesets)
2361 assert_select "osm>changeset", changesets.size
2362 changesets.each do |changeset|
2363 assert_select "osm>changeset[id='#{changeset.id}']", 1
2368 # check that certain changesets exist in the output in the specified order
2369 def assert_changesets_in_order(changesets)
2370 assert_select "osm>changeset", changesets.size
2371 changesets.each_with_index do |changeset, index|
2372 assert_select "osm>changeset:nth-child(#{index + 1})[id='#{changeset.id}']", 1
2377 # update the changeset_id of a way element
2378 def update_changeset(xml, changeset_id)
2379 xml_attr_rewrite(xml, "changeset", changeset_id)
2383 # update an attribute in a way element
2384 def xml_attr_rewrite(xml, name, value)
2385 xml.find("//osm/way").first[name] = value.to_s
2390 # build XML for changesets
2391 def create_changeset_xml(user: nil, id: nil)
2392 root = XML::Document.new
2393 root.root = XML::Node.new "osm"
2394 cs = XML::Node.new "changeset"
2396 cs["user"] = user.display_name
2397 cs["uid"] = user.id.to_s
2399 cs["id"] = id.to_s if id