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"
138 get changeset_create_path, :headers => auth_header
139 assert_response :not_found
140 assert_template "rescues/routing_error"
142 post changeset_create_path, :headers => auth_header
143 assert_response :not_found
144 assert_template "rescues/routing_error"
148 # check that the changeset can be shown and returns the correct
149 # document structure.
151 changeset = create(:changeset)
153 get changeset_show_path(changeset)
154 assert_response :success, "cannot get first changeset"
156 assert_select "osm[version='#{Settings.api_version}'][generator='#{Settings.generator}']", 1
157 assert_single_changeset changeset
158 assert_select "osm>changeset>discussion", 0
160 get changeset_show_path(changeset), :params => { :include_discussion => true }
161 assert_response :success, "cannot get first changeset with comments"
163 assert_select "osm[version='#{Settings.api_version}'][generator='#{Settings.generator}']", 1
164 assert_single_changeset changeset
165 assert_select "osm>changeset>discussion", 1
166 assert_select "osm>changeset>discussion>comment", 0
169 def test_show_comments
170 # all comments visible
171 changeset = create(:changeset, :closed)
172 comment1, comment2, comment3 = create_list(:changeset_comment, 3, :changeset_id => changeset.id)
174 get changeset_show_path(changeset), :params => { :include_discussion => true }
175 assert_response :success, "cannot get closed changeset with comments"
177 assert_select "osm[version='#{Settings.api_version}'][generator='#{Settings.generator}']", 1
178 assert_single_changeset changeset
179 assert_select "osm>changeset>discussion", 1
180 assert_select "osm>changeset>discussion>comment", 3
181 assert_select "osm>changeset>discussion>comment:nth-child(1)>@id", comment1.id.to_s
182 assert_select "osm>changeset>discussion>comment:nth-child(1)>@visible", "true"
183 assert_select "osm>changeset>discussion>comment:nth-child(2)>@id", comment2.id.to_s
184 assert_select "osm>changeset>discussion>comment:nth-child(2)>@visible", "true"
185 assert_select "osm>changeset>discussion>comment:nth-child(3)>@id", comment3.id.to_s
186 assert_select "osm>changeset>discussion>comment:nth-child(3)>@visible", "true"
188 # one hidden comment not included because not asked for
189 comment2.update(:visible => false)
191 get changeset_show_path(changeset), :params => { :include_discussion => true }
192 assert_response :success, "cannot get closed changeset with comments"
194 assert_select "osm[version='#{Settings.api_version}'][generator='#{Settings.generator}']", 1
195 assert_single_changeset changeset
196 assert_select "osm>changeset>discussion", 1
197 assert_select "osm>changeset>discussion>comment", 2
198 assert_select "osm>changeset>discussion>comment:nth-child(1)>@id", comment1.id.to_s
199 assert_select "osm>changeset>discussion>comment:nth-child(1)>@visible", "true"
200 assert_select "osm>changeset>discussion>comment:nth-child(2)>@id", comment3.id.to_s
201 assert_select "osm>changeset>discussion>comment:nth-child(2)>@visible", "true"
203 # one hidden comment not included because no permissions
204 get changeset_show_path(changeset), :params => { :include_discussion => true, :show_hidden_comments => true }
205 assert_response :success, "cannot get closed changeset with comments"
207 assert_select "osm[version='#{Settings.api_version}'][generator='#{Settings.generator}']", 1
208 assert_single_changeset changeset
209 assert_select "osm>changeset>discussion", 1
210 assert_select "osm>changeset>discussion>comment", 2
211 assert_select "osm>changeset>discussion>comment:nth-child(1)>@id", comment1.id.to_s
212 assert_select "osm>changeset>discussion>comment:nth-child(1)>@visible", "true"
213 # maybe will show an empty comment element with visible=false in the future
214 assert_select "osm>changeset>discussion>comment:nth-child(2)>@id", comment3.id.to_s
215 assert_select "osm>changeset>discussion>comment:nth-child(2)>@visible", "true"
217 # one hidden comment shown to moderators
218 moderator_user = create(:moderator_user)
219 auth_header = basic_authorization_header moderator_user.email, "test"
220 get changeset_show_path(changeset), :params => { :include_discussion => true, :show_hidden_comments => true },
221 :headers => auth_header
222 assert_response :success, "cannot get closed changeset with comments"
224 assert_select "osm[version='#{Settings.api_version}'][generator='#{Settings.generator}']", 1
225 assert_single_changeset changeset
226 assert_select "osm>changeset>discussion", 1
227 assert_select "osm>changeset>discussion>comment", 3
228 assert_select "osm>changeset>discussion>comment:nth-child(1)>@id", comment1.id.to_s
229 assert_select "osm>changeset>discussion>comment:nth-child(1)>@visible", "true"
230 assert_select "osm>changeset>discussion>comment:nth-child(2)>@id", comment2.id.to_s
231 assert_select "osm>changeset>discussion>comment:nth-child(2)>@visible", "false"
232 assert_select "osm>changeset>discussion>comment:nth-child(3)>@id", comment3.id.to_s
233 assert_select "osm>changeset>discussion>comment:nth-child(3)>@visible", "true"
237 changeset = create(:changeset)
239 get changeset_show_path(changeset), :params => { :format => "json" }
240 assert_response :success, "cannot get first changeset"
242 js = ActiveSupport::JSON.decode(@response.body)
245 assert_equal Settings.api_version, js["version"]
246 assert_equal Settings.generator, js["generator"]
247 assert_single_changeset_json changeset, js
248 assert_nil js["changeset"]["tags"]
249 assert_nil js["changeset"]["comments"]
250 assert_equal changeset.user.id, js["changeset"]["uid"]
251 assert_equal changeset.user.display_name, js["changeset"]["user"]
253 get changeset_show_path(changeset), :params => { :format => "json", :include_discussion => true }
254 assert_response :success, "cannot get first changeset with comments"
256 js = ActiveSupport::JSON.decode(@response.body)
258 assert_equal Settings.api_version, js["version"]
259 assert_equal Settings.generator, js["generator"]
260 assert_single_changeset_json changeset, js
261 assert_nil js["changeset"]["tags"]
262 assert_nil js["changeset"]["min_lat"]
263 assert_nil js["changeset"]["min_lon"]
264 assert_nil js["changeset"]["max_lat"]
265 assert_nil js["changeset"]["max_lon"]
266 assert_equal 0, js["changeset"]["comments"].count
269 def test_show_comments_json
270 # all comments visible
271 changeset = create(:changeset, :closed)
272 comment0, comment1, comment2 = create_list(:changeset_comment, 3, :changeset_id => changeset.id)
274 get changeset_show_path(changeset), :params => { :format => "json", :include_discussion => true }
275 assert_response :success, "cannot get closed changeset with comments"
277 js = ActiveSupport::JSON.decode(@response.body)
279 assert_equal Settings.api_version, js["version"]
280 assert_equal Settings.generator, js["generator"]
281 assert_single_changeset_json changeset, js
282 assert_equal 3, js["changeset"]["comments"].count
283 assert_equal comment0.id, js["changeset"]["comments"][0]["id"]
284 assert js["changeset"]["comments"][0]["visible"]
285 assert_equal comment1.id, js["changeset"]["comments"][1]["id"]
286 assert js["changeset"]["comments"][1]["visible"]
287 assert_equal comment2.id, js["changeset"]["comments"][2]["id"]
288 assert js["changeset"]["comments"][2]["visible"]
290 # one hidden comment not included because not asked for
291 comment1.update(:visible => false)
293 get changeset_show_path(changeset), :params => { :format => "json", :include_discussion => true }
294 assert_response :success, "cannot get closed changeset with comments"
296 js = ActiveSupport::JSON.decode(@response.body)
298 assert_equal Settings.api_version, js["version"]
299 assert_equal Settings.generator, js["generator"]
300 assert_single_changeset_json changeset, js
301 assert_equal 2, js["changeset"]["comments"].count
302 assert_equal comment0.id, js["changeset"]["comments"][0]["id"]
303 assert js["changeset"]["comments"][0]["visible"]
304 assert_equal comment2.id, js["changeset"]["comments"][1]["id"]
305 assert js["changeset"]["comments"][1]["visible"]
307 # one hidden comment not included because no permissions
308 get changeset_show_path(changeset), :params => { :format => "json", :include_discussion => true, :show_hidden_comments => true }
309 assert_response :success, "cannot get closed changeset with comments"
311 js = ActiveSupport::JSON.decode(@response.body)
313 assert_equal Settings.api_version, js["version"]
314 assert_equal Settings.generator, js["generator"]
315 assert_single_changeset_json changeset, js
316 assert_equal 2, js["changeset"]["comments"].count
317 assert_equal comment0.id, js["changeset"]["comments"][0]["id"]
318 assert js["changeset"]["comments"][0]["visible"]
319 # maybe will show an empty comment element with visible=false in the future
320 assert_equal comment2.id, js["changeset"]["comments"][1]["id"]
321 assert js["changeset"]["comments"][1]["visible"]
323 # one hidden comment shown to moderators
324 moderator_user = create(:moderator_user)
325 auth_header = basic_authorization_header moderator_user.email, "test"
326 get changeset_show_path(changeset), :params => { :format => "json", :include_discussion => true, :show_hidden_comments => true },
327 :headers => auth_header
328 assert_response :success, "cannot get closed changeset with comments"
330 js = ActiveSupport::JSON.decode(@response.body)
332 assert_equal Settings.api_version, js["version"]
333 assert_equal Settings.generator, js["generator"]
334 assert_single_changeset_json changeset, js
335 assert_equal 3, js["changeset"]["comments"].count
336 assert_equal comment0.id, js["changeset"]["comments"][0]["id"]
337 assert js["changeset"]["comments"][0]["visible"]
338 assert_equal comment1.id, js["changeset"]["comments"][1]["id"]
339 assert_not js["changeset"]["comments"][1]["visible"]
340 assert_equal comment2.id, js["changeset"]["comments"][2]["id"]
341 assert js["changeset"]["comments"][2]["visible"]
344 def test_show_tag_and_discussion_json
345 changeset = create(:changeset, :closed)
347 tag1 = ChangesetTag.new
348 tag1.changeset_id = changeset.id
349 tag1.k = "created_by"
350 tag1.v = "JOSM/1.5 (18364)"
352 tag2 = ChangesetTag.new
353 tag2.changeset_id = changeset.id
355 tag2.v = "changeset comment"
357 changeset.changeset_tags = [tag1, tag2]
359 create_list(:changeset_comment, 3, :changeset_id => changeset.id)
361 get changeset_show_path(changeset), :params => { :format => "json", :include_discussion => true }
362 assert_response :success, "cannot get closed changeset with comments"
364 js = ActiveSupport::JSON.decode(@response.body)
367 assert_equal Settings.api_version, js["version"]
368 assert_equal Settings.generator, js["generator"]
369 assert_single_changeset_json changeset, js
370 assert_equal 2, js["changeset"]["tags"].count
371 assert_equal 3, js["changeset"]["comments"].count
372 assert_equal 3, js["changeset"]["comments_count"]
373 assert_equal 0, js["changeset"]["changes_count"]
374 assert_not_nil js["changeset"]["comments"][0]["uid"]
375 assert_not_nil js["changeset"]["comments"][0]["user"]
376 assert_not_nil js["changeset"]["comments"][0]["text"]
379 def test_show_bbox_json
380 # test bbox attribute
381 changeset = create(:changeset, :min_lat => (-5 * GeoRecord::SCALE).round, :min_lon => (5 * GeoRecord::SCALE).round,
382 :max_lat => (15 * GeoRecord::SCALE).round, :max_lon => (12 * GeoRecord::SCALE).round)
384 get changeset_show_path(changeset), :params => { :format => "json" }
385 assert_response :success, "cannot get first changeset"
387 js = ActiveSupport::JSON.decode(@response.body)
389 assert_equal(-5, js["changeset"]["min_lat"])
390 assert_equal 5, js["changeset"]["min_lon"]
391 assert_equal 15, js["changeset"]["max_lat"]
392 assert_equal 12, js["changeset"]["max_lon"]
396 # check that a changeset that doesn't exist returns an appropriate message
397 def test_show_not_found
398 [0, -32, 233455644, "afg", "213"].each do |id|
399 get changeset_show_path(:id => id)
400 assert_response :not_found, "should get a not found"
401 rescue ActionController::UrlGenerationError => e
402 assert_match(/No route matches/, e.to_s)
407 # test that the user who opened a change can close it
409 private_user = create(:user, :data_public => false)
410 private_changeset = create(:changeset, :user => private_user)
412 changeset = create(:changeset, :user => user)
414 ## Try without authentication
415 put changeset_close_path(changeset)
416 assert_response :unauthorized
418 ## Try using the non-public user
419 auth_header = basic_authorization_header private_user.email, "test"
420 put changeset_close_path(private_changeset), :headers => auth_header
421 assert_require_public_data
423 ## The try with the public user
424 auth_header = basic_authorization_header user.email, "test"
427 put changeset_close_path(:id => cs_id), :headers => auth_header
428 assert_response :success
430 # test that it really is closed now
431 cs = Changeset.find(changeset.id)
433 "changeset should be closed now (#{cs.closed_at} > #{Time.now.utc}.")
437 # test that a different user can't close another user's changeset
438 def test_close_invalid
440 changeset = create(:changeset)
442 auth_header = basic_authorization_header user.email, "test"
444 put changeset_close_path(changeset), :headers => auth_header
445 assert_response :conflict
446 assert_equal "The user doesn't own that changeset", @response.body
450 # test that you can't close using another method
451 def test_close_method_invalid
453 changeset = create(:changeset, :user => user)
455 auth_header = basic_authorization_header user.email, "test"
457 get changeset_close_path(changeset), :headers => auth_header
458 assert_response :not_found
459 assert_template "rescues/routing_error"
461 post changeset_close_path(changeset), :headers => auth_header
462 assert_response :not_found
463 assert_template "rescues/routing_error"
467 # check that you can't close a changeset that isn't found
468 def test_close_not_found
469 cs_ids = [0, -132, "123"]
471 # First try to do it with no auth
473 put changeset_close_path(:id => id)
474 assert_response :unauthorized, "Shouldn't be able close the non-existant changeset #{id}, when not authorized"
475 rescue ActionController::UrlGenerationError => e
476 assert_match(/No route matches/, e.to_s)
480 auth_header = basic_authorization_header create(:user).email, "test"
482 put changeset_close_path(:id => id), :headers => auth_header
483 assert_response :not_found, "The changeset #{id} doesn't exist, so can't be closed"
484 rescue ActionController::UrlGenerationError => e
485 assert_match(/No route matches/, e.to_s)
490 # upload something simple, but valid and check that it can
492 # Also try without auth and another user.
493 def test_upload_simple_valid
494 private_user = create(:user, :data_public => false)
495 private_changeset = create(:changeset, :user => private_user)
497 changeset = create(:changeset, :user => user)
501 relation = create(:relation)
502 other_relation = create(:relation)
503 # create some tags, since we test that they are removed later
504 create(:node_tag, :node => node)
505 create(:way_tag, :way => way)
506 create(:relation_tag, :relation => relation)
509 changeset_id = changeset.id
511 # simple diff to change a node, way and relation by removing
516 <node id='#{node.id}' lon='0' lat='0' changeset='#{changeset_id}' version='1'/>
517 <way id='#{way.id}' changeset='#{changeset_id}' version='1'>
518 <nd ref='#{node.id}'/>
522 <relation id='#{relation.id}' changeset='#{changeset_id}' version='1'>
523 <member type='way' role='some' ref='#{way.id}'/>
524 <member type='node' role='some' ref='#{node.id}'/>
525 <member type='relation' role='some' ref='#{other_relation.id}'/>
532 post changeset_upload_path(changeset), :params => diff
533 assert_response :unauthorized,
534 "shouldn't be able to upload a simple valid diff to changeset: #{@response.body}"
536 ## Now try with a private user
537 auth_header = basic_authorization_header private_user.email, "test"
538 changeset_id = private_changeset.id
540 # simple diff to change a node, way and relation by removing
545 <node id='#{node.id}' lon='0' lat='0' changeset='#{changeset_id}' version='1'/>
546 <way id='#{way.id}' changeset='#{changeset_id}' version='1'>
547 <nd ref='#{node.id}'/>
551 <relation id='#{relation.id}' changeset='#{changeset_id}' version='1'>
552 <member type='way' role='some' ref='#{way.id}'/>
553 <member type='node' role='some' ref='#{node.id}'/>
554 <member type='relation' role='some' ref='#{other_relation.id}'/>
561 post changeset_upload_path(private_changeset), :params => diff, :headers => auth_header
562 assert_response :forbidden,
563 "can't upload a simple valid diff to changeset: #{@response.body}"
565 ## Now try with the public user
566 auth_header = basic_authorization_header user.email, "test"
567 changeset_id = changeset.id
569 # simple diff to change a node, way and relation by removing
574 <node id='#{node.id}' lon='0' lat='0' changeset='#{changeset_id}' version='1'/>
575 <way id='#{way.id}' changeset='#{changeset_id}' version='1'>
576 <nd ref='#{node.id}'/>
580 <relation id='#{relation.id}' changeset='#{changeset_id}' version='1'>
581 <member type='way' role='some' ref='#{way.id}'/>
582 <member type='node' role='some' ref='#{node.id}'/>
583 <member type='relation' role='some' ref='#{other_relation.id}'/>
590 post changeset_upload_path(changeset), :params => diff, :headers => auth_header
591 assert_response :success,
592 "can't upload a simple valid diff to changeset: #{@response.body}"
594 # check that the changes made it into the database
595 assert_equal 0, Node.find(node.id).tags.size, "node #{node.id} should now have no tags"
596 assert_equal 0, Way.find(way.id).tags.size, "way #{way.id} should now have no tags"
597 assert_equal 0, Relation.find(relation.id).tags.size, "relation #{relation.id} should now have no tags"
601 # upload something which creates new objects using placeholders
602 def test_upload_create_valid
604 changeset = create(:changeset, :user => user)
606 way = create(:way_with_nodes, :nodes_count => 2)
607 relation = create(:relation)
609 auth_header = basic_authorization_header user.email, "test"
611 # simple diff to create a node way and relation using placeholders
615 <node id='-1' lon='0' lat='0' changeset='#{changeset.id}'>
616 <tag k='foo' v='bar'/>
617 <tag k='baz' v='bat'/>
619 <way id='-1' changeset='#{changeset.id}'>
620 <nd ref='#{node.id}'/>
624 <relation id='-1' changeset='#{changeset.id}'>
625 <member type='way' role='some' ref='#{way.id}'/>
626 <member type='node' role='some' ref='#{node.id}'/>
627 <member type='relation' role='some' ref='#{relation.id}'/>
634 post changeset_upload_path(changeset), :params => diff, :headers => auth_header
635 assert_response :success,
636 "can't upload a simple valid creation to changeset: #{@response.body}"
638 # check the returned payload
639 assert_select "diffResult[version='#{Settings.api_version}'][generator='#{Settings.generator}']", 1
640 assert_select "diffResult>node", 1
641 assert_select "diffResult>way", 1
642 assert_select "diffResult>relation", 1
644 # inspect the response to find out what the new element IDs are
645 doc = XML::Parser.string(@response.body).parse
646 new_node_id = doc.find("//diffResult/node").first["new_id"].to_i
647 new_way_id = doc.find("//diffResult/way").first["new_id"].to_i
648 new_rel_id = doc.find("//diffResult/relation").first["new_id"].to_i
650 # check the old IDs are all present and negative one
651 assert_equal(-1, doc.find("//diffResult/node").first["old_id"].to_i)
652 assert_equal(-1, doc.find("//diffResult/way").first["old_id"].to_i)
653 assert_equal(-1, doc.find("//diffResult/relation").first["old_id"].to_i)
655 # check the versions are present and equal one
656 assert_equal 1, doc.find("//diffResult/node").first["new_version"].to_i
657 assert_equal 1, doc.find("//diffResult/way").first["new_version"].to_i
658 assert_equal 1, doc.find("//diffResult/relation").first["new_version"].to_i
660 # check that the changes made it into the database
661 assert_equal 2, Node.find(new_node_id).tags.size, "new node should have two tags"
662 assert_equal 0, Way.find(new_way_id).tags.size, "new way should have no tags"
663 assert_equal 0, Relation.find(new_rel_id).tags.size, "new relation should have no tags"
667 # test a complex delete where we delete elements which rely on eachother
668 # in the same transaction.
669 def test_upload_delete
670 changeset = create(:changeset)
671 super_relation = create(:relation)
672 used_relation = create(:relation)
673 used_way = create(:way)
674 used_node = create(:node)
675 create(:relation_member, :relation => super_relation, :member => used_relation)
676 create(:relation_member, :relation => super_relation, :member => used_way)
677 create(:relation_member, :relation => super_relation, :member => used_node)
679 auth_header = basic_authorization_header changeset.user.display_name, "test"
681 diff = XML::Document.new
682 diff.root = XML::Node.new "osmChange"
683 delete = XML::Node.new "delete"
685 delete << xml_node_for_relation(super_relation)
686 delete << xml_node_for_relation(used_relation)
687 delete << xml_node_for_way(used_way)
688 delete << xml_node_for_node(used_node)
690 # update the changeset to one that this user owns
691 %w[node way relation].each do |type|
692 delete.find("//osmChange/delete/#{type}").each do |n|
693 n["changeset"] = changeset.id.to_s
698 post changeset_upload_path(changeset), :params => diff.to_s, :headers => auth_header
699 assert_response :success,
700 "can't upload a deletion diff to changeset: #{@response.body}"
702 # check the response is well-formed
703 assert_select "diffResult>node", 1
704 assert_select "diffResult>way", 1
705 assert_select "diffResult>relation", 2
707 # check that everything was deleted
708 assert_not Node.find(used_node.id).visible
709 assert_not Way.find(used_way.id).visible
710 assert_not Relation.find(super_relation.id).visible
711 assert_not Relation.find(used_relation.id).visible
715 # test uploading a delete with no lat/lon, as they are optional in
716 # the osmChange spec.
717 def test_upload_nolatlon_delete
719 changeset = create(:changeset)
721 auth_header = basic_authorization_header changeset.user.display_name, "test"
722 diff = "<osmChange><delete><node id='#{node.id}' version='#{node.version}' changeset='#{changeset.id}'/></delete></osmChange>"
725 post changeset_upload_path(changeset), :params => diff, :headers => auth_header
726 assert_response :success,
727 "can't upload a deletion diff to changeset: #{@response.body}"
729 # check the response is well-formed
730 assert_select "diffResult>node", 1
732 # check that everything was deleted
733 assert_not Node.find(node.id).visible
736 def test_repeated_changeset_create
738 auth_header = basic_authorization_header create(:user).email, "test"
740 # create a temporary changeset
741 xml = "<osm><changeset>" \
742 "<tag k='created_by' v='osm test suite checking changesets'/>" \
744 assert_difference "Changeset.count", 1 do
745 put changeset_create_path, :params => xml, :headers => auth_header
747 assert_response :success
751 def test_upload_large_changeset
752 auth_header = basic_authorization_header create(:user).email, "test"
755 put changeset_create_path, :params => "<osm><changeset/></osm>", :headers => auth_header
756 assert_response :success, "Should be able to create a changeset: #{@response.body}"
757 changeset_id = @response.body.to_i
759 # upload some widely-spaced nodes, spiralling positive and negative
763 <node id='-1' lon='-20' lat='-10' changeset='#{changeset_id}'/>
764 <node id='-10' lon='20' lat='10' changeset='#{changeset_id}'/>
765 <node id='-2' lon='-40' lat='-20' changeset='#{changeset_id}'/>
766 <node id='-11' lon='40' lat='20' changeset='#{changeset_id}'/>
767 <node id='-3' lon='-60' lat='-30' changeset='#{changeset_id}'/>
768 <node id='-12' lon='60' lat='30' changeset='#{changeset_id}'/>
769 <node id='-4' lon='-80' lat='-40' changeset='#{changeset_id}'/>
770 <node id='-13' lon='80' lat='40' changeset='#{changeset_id}'/>
771 <node id='-5' lon='-100' lat='-50' changeset='#{changeset_id}'/>
772 <node id='-14' lon='100' lat='50' changeset='#{changeset_id}'/>
773 <node id='-6' lon='-120' lat='-60' changeset='#{changeset_id}'/>
774 <node id='-15' lon='120' lat='60' changeset='#{changeset_id}'/>
775 <node id='-7' lon='-140' lat='-70' changeset='#{changeset_id}'/>
776 <node id='-16' lon='140' lat='70' changeset='#{changeset_id}'/>
777 <node id='-8' lon='-160' lat='-80' changeset='#{changeset_id}'/>
778 <node id='-17' lon='160' lat='80' changeset='#{changeset_id}'/>
779 <node id='-9' lon='-179.9' lat='-89.9' changeset='#{changeset_id}'/>
780 <node id='-18' lon='179.9' lat='89.9' changeset='#{changeset_id}'/>
785 # upload it, which used to cause an error like "PGError: ERROR:
786 # integer out of range" (bug #2152). but shouldn't any more.
787 post changeset_upload_path(:id => changeset_id), :params => diff, :headers => auth_header
788 assert_response :success,
789 "can't upload a spatially-large diff to changeset: #{@response.body}"
791 # check that the changeset bbox is within bounds
792 cs = Changeset.find(changeset_id)
793 assert_operator cs.min_lon, :>=, -180 * GeoRecord::SCALE, "Minimum longitude (#{cs.min_lon / GeoRecord::SCALE}) should be >= -180 to be valid."
794 assert_operator cs.max_lon, :<=, 180 * GeoRecord::SCALE, "Maximum longitude (#{cs.max_lon / GeoRecord::SCALE}) should be <= 180 to be valid."
795 assert_operator cs.min_lat, :>=, -90 * GeoRecord::SCALE, "Minimum latitude (#{cs.min_lat / GeoRecord::SCALE}) should be >= -90 to be valid."
796 assert_operator cs.max_lat, :<=, 90 * GeoRecord::SCALE, "Maximum latitude (#{cs.max_lat / GeoRecord::SCALE}) should be <= 90 to be valid."
800 # test that deleting stuff in a transaction doesn't bypass the checks
801 # to ensure that used elements are not deleted.
802 def test_upload_delete_invalid
803 changeset = create(:changeset)
804 relation = create(:relation)
805 other_relation = create(:relation)
806 used_way = create(:way)
807 used_node = create(:node)
808 create(:relation_member, :relation => relation, :member => used_way)
809 create(:relation_member, :relation => relation, :member => used_node)
811 auth_header = basic_authorization_header changeset.user.email, "test"
813 diff = XML::Document.new
814 diff.root = XML::Node.new "osmChange"
815 delete = XML::Node.new "delete"
817 delete << xml_node_for_relation(other_relation)
818 delete << xml_node_for_way(used_way)
819 delete << xml_node_for_node(used_node)
821 # update the changeset to one that this user owns
822 %w[node way relation].each do |type|
823 delete.find("//osmChange/delete/#{type}").each do |n|
824 n["changeset"] = changeset.id.to_s
829 post changeset_upload_path(changeset), :params => diff.to_s, :headers => auth_header
830 assert_response :precondition_failed,
831 "shouldn't be able to upload a invalid deletion diff: #{@response.body}"
832 assert_equal "Precondition failed: Way #{used_way.id} is still used by relations #{relation.id}.", @response.body
834 # check that nothing was, in fact, deleted
835 assert Node.find(used_node.id).visible
836 assert Way.find(used_way.id).visible
837 assert Relation.find(relation.id).visible
838 assert Relation.find(other_relation.id).visible
842 # test that a conditional delete of an in use object works.
843 def test_upload_delete_if_unused
844 changeset = create(:changeset)
845 super_relation = create(:relation)
846 used_relation = create(:relation)
847 used_way = create(:way)
848 used_node = create(:node)
849 create(:relation_member, :relation => super_relation, :member => used_relation)
850 create(:relation_member, :relation => super_relation, :member => used_way)
851 create(:relation_member, :relation => super_relation, :member => used_node)
853 auth_header = basic_authorization_header changeset.user.email, "test"
855 diff = XML::Document.new
856 diff.root = XML::Node.new "osmChange"
857 delete = XML::Node.new "delete"
859 delete["if-unused"] = ""
860 delete << xml_node_for_relation(used_relation)
861 delete << xml_node_for_way(used_way)
862 delete << xml_node_for_node(used_node)
864 # update the changeset to one that this user owns
865 %w[node way relation].each do |type|
866 delete.find("//osmChange/delete/#{type}").each do |n|
867 n["changeset"] = changeset.id.to_s
872 post changeset_upload_path(changeset), :params => diff.to_s, :headers => auth_header
873 assert_response :success,
874 "can't do a conditional delete of in use objects: #{@response.body}"
876 # check the returned payload
877 assert_select "diffResult[version='#{Settings.api_version}'][generator='#{Settings.generator}']", 1
878 assert_select "diffResult>node", 1
879 assert_select "diffResult>way", 1
880 assert_select "diffResult>relation", 1
883 doc = XML::Parser.string(@response.body).parse
885 # check the old IDs are all present and what we expect
886 assert_equal used_node.id, doc.find("//diffResult/node").first["old_id"].to_i
887 assert_equal used_way.id, doc.find("//diffResult/way").first["old_id"].to_i
888 assert_equal used_relation.id, doc.find("//diffResult/relation").first["old_id"].to_i
890 # check the new IDs are all present and unchanged
891 assert_equal used_node.id, doc.find("//diffResult/node").first["new_id"].to_i
892 assert_equal used_way.id, doc.find("//diffResult/way").first["new_id"].to_i
893 assert_equal used_relation.id, doc.find("//diffResult/relation").first["new_id"].to_i
895 # check the new versions are all present and unchanged
896 assert_equal used_node.version, doc.find("//diffResult/node").first["new_version"].to_i
897 assert_equal used_way.version, doc.find("//diffResult/way").first["new_version"].to_i
898 assert_equal used_relation.version, doc.find("//diffResult/relation").first["new_version"].to_i
900 # check that nothing was, in fact, deleted
901 assert Node.find(used_node.id).visible
902 assert Way.find(used_way.id).visible
903 assert Relation.find(used_relation.id).visible
907 # upload an element with a really long tag value
908 def test_upload_invalid_too_long_tag
909 changeset = create(:changeset)
911 auth_header = basic_authorization_header changeset.user.email, "test"
913 # simple diff to create a node way and relation using placeholders
917 <node id='-1' lon='0' lat='0' changeset='#{changeset.id}'>
918 <tag k='foo' v='#{'x' * 256}'/>
925 post changeset_upload_path(changeset), :params => diff, :headers => auth_header
926 assert_response :bad_request,
927 "shouldn't be able to upload too long a tag to changeset: #{@response.body}"
931 # upload something which creates new objects and inserts them into
932 # existing containers using placeholders.
933 def test_upload_complex
936 relation = create(:relation)
937 create(:way_node, :way => way, :node => node)
939 changeset = create(:changeset)
941 auth_header = basic_authorization_header changeset.user.email, "test"
943 # simple diff to create a node way and relation using placeholders
947 <node id='-1' lon='0' lat='0' changeset='#{changeset.id}'>
948 <tag k='foo' v='bar'/>
949 <tag k='baz' v='bat'/>
953 <way id='#{way.id}' changeset='#{changeset.id}' version='1'>
955 <nd ref='#{node.id}'/>
957 <relation id='#{relation.id}' changeset='#{changeset.id}' version='1'>
958 <member type='way' role='some' ref='#{way.id}'/>
959 <member type='node' role='some' ref='-1'/>
960 <member type='relation' role='some' ref='#{relation.id}'/>
967 post changeset_upload_path(changeset), :params => diff, :headers => auth_header
968 assert_response :success,
969 "can't upload a complex diff to changeset: #{@response.body}"
971 # check the returned payload
972 assert_select "diffResult[version='#{Settings.api_version}'][generator='#{Settings.generator}']", 1
973 assert_select "diffResult>node", 1
974 assert_select "diffResult>way", 1
975 assert_select "diffResult>relation", 1
977 # inspect the response to find out what the new element IDs are
978 doc = XML::Parser.string(@response.body).parse
979 new_node_id = doc.find("//diffResult/node").first["new_id"].to_i
981 # check that the changes made it into the database
982 assert_equal 2, Node.find(new_node_id).tags.size, "new node should have two tags"
983 assert_equal [new_node_id, node.id], Way.find(way.id).nds, "way nodes should match"
984 Relation.find(relation.id).members.each do |type, id, _role|
985 assert_equal new_node_id, id, "relation should contain new node" if type == "node"
990 # create a diff which references several changesets, which should cause
991 # a rollback and none of the diff gets committed
992 def test_upload_invalid_changesets
993 changeset = create(:changeset)
994 other_changeset = create(:changeset, :user => changeset.user)
997 relation = create(:relation)
998 other_relation = create(:relation)
1000 auth_header = basic_authorization_header changeset.user.email, "test"
1002 # simple diff to create a node way and relation using placeholders
1006 <node id='#{node.id}' lon='0' lat='0' changeset='#{changeset.id}' version='1'/>
1007 <way id='#{way.id}' changeset='#{changeset.id}' version='1'>
1008 <nd ref='#{node.id}'/>
1012 <relation id='#{relation.id}' changeset='#{changeset.id}' version='1'>
1013 <member type='way' role='some' ref='#{way.id}'/>
1014 <member type='node' role='some' ref='#{node.id}'/>
1015 <member type='relation' role='some' ref='#{other_relation.id}'/>
1019 <node id='-1' lon='0' lat='0' changeset='#{other_changeset.id}'>
1020 <tag k='foo' v='bar'/>
1021 <tag k='baz' v='bat'/>
1028 post changeset_upload_path(changeset), :params => diff, :headers => auth_header
1029 assert_response :conflict,
1030 "uploading a diff with multiple changesets should have failed"
1032 # check that objects are unmodified
1033 assert_nodes_are_equal(node, Node.find(node.id))
1034 assert_ways_are_equal(way, Way.find(way.id))
1035 assert_relations_are_equal(relation, Relation.find(relation.id))
1039 # upload multiple versions of the same element in the same diff.
1040 def test_upload_multiple_valid
1041 node = create(:node)
1042 changeset = create(:changeset)
1043 auth_header = basic_authorization_header changeset.user.email, "test"
1045 # change the location of a node multiple times, each time referencing
1046 # the last version. doesn't this depend on version numbers being
1051 <node id='#{node.id}' lon='0' lat='0' changeset='#{changeset.id}' version='1'/>
1052 <node id='#{node.id}' lon='1' lat='0' changeset='#{changeset.id}' version='2'/>
1053 <node id='#{node.id}' lon='1' lat='1' changeset='#{changeset.id}' version='3'/>
1054 <node id='#{node.id}' lon='1' lat='2' changeset='#{changeset.id}' version='4'/>
1055 <node id='#{node.id}' lon='2' lat='2' changeset='#{changeset.id}' version='5'/>
1056 <node id='#{node.id}' lon='3' lat='2' changeset='#{changeset.id}' version='6'/>
1057 <node id='#{node.id}' lon='3' lat='3' changeset='#{changeset.id}' version='7'/>
1058 <node id='#{node.id}' lon='9' lat='9' changeset='#{changeset.id}' version='8'/>
1064 post changeset_upload_path(changeset), :params => diff, :headers => auth_header
1065 assert_response :success,
1066 "can't upload multiple versions of an element in a diff: #{@response.body}"
1068 # check the response is well-formed. its counter-intuitive, but the
1069 # API will return multiple elements with the same ID and different
1070 # version numbers for each change we made.
1071 assert_select "diffResult>node", 8
1075 # upload multiple versions of the same element in the same diff, but
1076 # keep the version numbers the same.
1077 def test_upload_multiple_duplicate
1078 node = create(:node)
1079 changeset = create(:changeset)
1081 auth_header = basic_authorization_header changeset.user.email, "test"
1086 <node id='#{node.id}' lon='0' lat='0' changeset='#{changeset.id}' version='1'/>
1087 <node id='#{node.id}' lon='1' lat='1' changeset='#{changeset.id}' version='1'/>
1093 post changeset_upload_path(changeset), :params => diff, :headers => auth_header
1094 assert_response :conflict,
1095 "shouldn't be able to upload the same element twice in a diff: #{@response.body}"
1099 # try to upload some elements without specifying the version
1100 def test_upload_missing_version
1101 changeset = create(:changeset)
1103 auth_header = basic_authorization_header changeset.user.email, "test"
1108 <node id='1' lon='1' lat='1' changeset='#{changeset.id}'/>
1114 post changeset_upload_path(changeset), :params => diff, :headers => auth_header
1115 assert_response :bad_request,
1116 "shouldn't be able to upload an element without version: #{@response.body}"
1120 # try to upload with commands other than create, modify, or delete
1121 def test_action_upload_invalid
1122 changeset = create(:changeset)
1124 auth_header = basic_authorization_header changeset.user.email, "test"
1129 <node id='1' lon='1' lat='1' changeset='#{changeset.id}' />
1133 post changeset_upload_path(changeset), :params => diff, :headers => auth_header
1134 assert_response :bad_request, "Shouldn't be able to upload a diff with the action ping"
1135 assert_equal("Unknown action ping, choices are create, modify, delete", @response.body)
1139 # upload a valid changeset which has a mixture of whitespace
1140 # to check a bug reported by ivansanchez (#1565).
1141 def test_upload_whitespace_valid
1142 changeset = create(:changeset)
1143 node = create(:node)
1144 way = create(:way_with_nodes, :nodes_count => 2)
1145 relation = create(:relation)
1146 other_relation = create(:relation)
1147 create(:relation_tag, :relation => relation)
1149 auth_header = basic_authorization_header changeset.user.email, "test"
1153 <modify><node id='#{node.id}' lon='0' lat='0' changeset='#{changeset.id}'
1155 <node id='#{node.id}' lon='1' lat='1' changeset='#{changeset.id}' version='2'><tag k='k' v='v'/></node></modify>
1157 <relation id='#{relation.id}' changeset='#{changeset.id}' version='1'><member
1158 type='way' role='some' ref='#{way.id}'/><member
1159 type='node' role='some' ref='#{node.id}'/>
1160 <member type='relation' role='some' ref='#{other_relation.id}'/>
1162 </modify></osmChange>
1166 post changeset_upload_path(changeset), :params => diff, :headers => auth_header
1167 assert_response :success,
1168 "can't upload a valid diff with whitespace variations to changeset: #{@response.body}"
1170 # check the response is well-formed
1171 assert_select "diffResult>node", 2
1172 assert_select "diffResult>relation", 1
1174 # check that the changes made it into the database
1175 assert_equal 1, Node.find(node.id).tags.size, "node #{node.id} should now have one tag"
1176 assert_equal 0, Relation.find(relation.id).tags.size, "relation #{relation.id} should now have no tags"
1180 # test that a placeholder can be reused within the same upload.
1181 def test_upload_reuse_placeholder_valid
1182 changeset = create(:changeset)
1184 auth_header = basic_authorization_header changeset.user.email, "test"
1189 <node id='-1' lon='0' lat='0' changeset='#{changeset.id}'>
1190 <tag k="foo" v="bar"/>
1194 <node id='-1' lon='1' lat='1' changeset='#{changeset.id}' version='1'/>
1197 <node id='-1' lon='2' lat='2' changeset='#{changeset.id}' version='2'/>
1203 post changeset_upload_path(changeset), :params => diff, :headers => auth_header
1204 assert_response :success,
1205 "can't upload a valid diff with re-used placeholders to changeset: #{@response.body}"
1207 # check the response is well-formed
1208 assert_select "diffResult>node", 3
1209 assert_select "diffResult>node[old_id='-1']", 3
1213 # test what happens if a diff upload re-uses placeholder IDs in an
1215 def test_upload_placeholder_invalid
1216 changeset = create(:changeset)
1218 auth_header = basic_authorization_header changeset.user.email, "test"
1223 <node id='-1' lon='0' lat='0' changeset='#{changeset.id}' version='1'/>
1224 <node id='-1' lon='1' lat='1' changeset='#{changeset.id}' version='1'/>
1225 <node id='-1' lon='2' lat='2' changeset='#{changeset.id}' version='2'/>
1231 post changeset_upload_path(changeset), :params => diff, :headers => auth_header
1232 assert_response :bad_request,
1233 "shouldn't be able to re-use placeholder IDs"
1235 # placeholder_ids must be unique across all action blocks
1239 <node id='-1' lon='0' lat='0' changeset='#{changeset.id}' version='1'/>
1242 <node id='-1' lon='1' lat='1' changeset='#{changeset.id}' version='1'/>
1248 post changeset_upload_path(changeset), :params => diff, :headers => auth_header
1249 assert_response :bad_request,
1250 "shouldn't be able to re-use placeholder IDs"
1253 def test_upload_process_order
1254 changeset = create(:changeset)
1256 auth_header = basic_authorization_header changeset.user.email, "test"
1261 <node id="-1" lat="1" lon="2" changeset="#{changeset.id}"/>
1262 <way id="-1" changeset="#{changeset.id}">
1266 <node id="-2" lat="1" lon="2" changeset="#{changeset.id}"/>
1272 post changeset_upload_path(changeset), :params => diff, :headers => auth_header
1273 assert_response :bad_request,
1274 "shouldn't refer elements behind it"
1277 def test_upload_duplicate_delete
1278 changeset = create(:changeset)
1280 auth_header = basic_authorization_header changeset.user.email, "test"
1285 <node id="-1" lat="39" lon="116" changeset="#{changeset.id}" />
1288 <node id="-1" version="1" changeset="#{changeset.id}" />
1289 <node id="-1" version="1" changeset="#{changeset.id}" />
1295 post changeset_upload_path(changeset), :params => diff, :headers => auth_header
1296 assert_response :gone,
1297 "transaction should be cancelled by second deletion"
1302 <node id="-1" lat="39" lon="116" changeset="#{changeset.id}" />
1304 <delete if-unused="true">
1305 <node id="-1" version="1" changeset="#{changeset.id}" />
1306 <node id="-1" version="1" changeset="#{changeset.id}" />
1312 post changeset_upload_path(changeset), :params => diff, :headers => auth_header
1314 assert_select "diffResult>node", 3
1315 assert_select "diffResult>node[old_id='-1']", 3
1316 assert_select "diffResult>node[new_version='1']", 1
1317 assert_select "diffResult>node[new_version='2']", 1
1321 # test that uploading a way referencing invalid placeholders gives a
1322 # proper error, not a 500.
1323 def test_upload_placeholder_invalid_way
1324 changeset = create(:changeset)
1327 auth_header = basic_authorization_header changeset.user.email, "test"
1332 <node id="-1" lon="0" lat="0" changeset="#{changeset.id}" version="1"/>
1333 <node id="-2" lon="1" lat="1" changeset="#{changeset.id}" version="1"/>
1334 <node id="-3" lon="2" lat="2" changeset="#{changeset.id}" version="1"/>
1335 <way id="-1" changeset="#{changeset.id}" version="1">
1346 post changeset_upload_path(changeset), :params => diff, :headers => auth_header
1347 assert_response :bad_request,
1348 "shouldn't be able to use invalid placeholder IDs"
1349 assert_equal "Placeholder node not found for reference -4 in way -1", @response.body
1351 # the same again, but this time use an existing way
1355 <node id="-1" lon="0" lat="0" changeset="#{changeset.id}" version="1"/>
1356 <node id="-2" lon="1" lat="1" changeset="#{changeset.id}" version="1"/>
1357 <node id="-3" lon="2" lat="2" changeset="#{changeset.id}" version="1"/>
1358 <way id="#{way.id}" changeset="#{changeset.id}" version="1">
1369 post changeset_upload_path(changeset), :params => diff, :headers => auth_header
1370 assert_response :bad_request,
1371 "shouldn't be able to use invalid placeholder IDs"
1372 assert_equal "Placeholder node not found for reference -4 in way #{way.id}", @response.body
1376 # test that uploading a relation referencing invalid placeholders gives a
1377 # proper error, not a 500.
1378 def test_upload_placeholder_invalid_relation
1379 changeset = create(:changeset)
1380 relation = create(:relation)
1382 auth_header = basic_authorization_header changeset.user.email, "test"
1387 <node id="-1" lon="0" lat="0" changeset="#{changeset.id}" version="1"/>
1388 <node id="-2" lon="1" lat="1" changeset="#{changeset.id}" version="1"/>
1389 <node id="-3" lon="2" lat="2" changeset="#{changeset.id}" version="1"/>
1390 <relation id="-1" changeset="#{changeset.id}" version="1">
1391 <member type="node" role="foo" ref="-1"/>
1392 <member type="node" role="foo" ref="-2"/>
1393 <member type="node" role="foo" ref="-3"/>
1394 <member type="node" role="foo" ref="-4"/>
1401 post changeset_upload_path(changeset), :params => diff, :headers => auth_header
1402 assert_response :bad_request,
1403 "shouldn't be able to use invalid placeholder IDs"
1404 assert_equal "Placeholder Node not found for reference -4 in relation -1.", @response.body
1406 # the same again, but this time use an existing relation
1410 <node id="-1" lon="0" lat="0" changeset="#{changeset.id}" version="1"/>
1411 <node id="-2" lon="1" lat="1" changeset="#{changeset.id}" version="1"/>
1412 <node id="-3" lon="2" lat="2" changeset="#{changeset.id}" version="1"/>
1413 <relation id="#{relation.id}" changeset="#{changeset.id}" version="1">
1414 <member type="node" role="foo" ref="-1"/>
1415 <member type="node" role="foo" ref="-2"/>
1416 <member type="node" role="foo" ref="-3"/>
1417 <member type="way" role="bar" ref="-1"/>
1424 post changeset_upload_path(changeset), :params => diff, :headers => auth_header
1425 assert_response :bad_request,
1426 "shouldn't be able to use invalid placeholder IDs"
1427 assert_equal "Placeholder Way not found for reference -1 in relation #{relation.id}.", @response.body
1431 # test what happens if a diff is uploaded containing only a node
1433 def test_upload_node_move
1434 auth_header = basic_authorization_header create(:user).email, "test"
1436 xml = "<osm><changeset>" \
1437 "<tag k='created_by' v='osm test suite checking changesets'/>" \
1438 "</changeset></osm>"
1439 put changeset_create_path, :params => xml, :headers => auth_header
1440 assert_response :success
1441 changeset_id = @response.body.to_i
1443 old_node = create(:node, :lat => 1, :lon => 1)
1445 diff = XML::Document.new
1446 diff.root = XML::Node.new "osmChange"
1447 modify = XML::Node.new "modify"
1448 xml_old_node = xml_node_for_node(old_node)
1449 xml_old_node["lat"] = 2.0.to_s
1450 xml_old_node["lon"] = 2.0.to_s
1451 xml_old_node["changeset"] = changeset_id.to_s
1452 modify << xml_old_node
1456 post changeset_upload_path(:id => changeset_id), :params => diff.to_s, :headers => auth_header
1457 assert_response :success,
1458 "diff should have uploaded OK"
1461 changeset = Changeset.find(changeset_id)
1462 assert_equal 1 * GeoRecord::SCALE, changeset.min_lon, "min_lon should be 1 degree"
1463 assert_equal 2 * GeoRecord::SCALE, changeset.max_lon, "max_lon should be 2 degrees"
1464 assert_equal 1 * GeoRecord::SCALE, changeset.min_lat, "min_lat should be 1 degree"
1465 assert_equal 2 * GeoRecord::SCALE, changeset.max_lat, "max_lat should be 2 degrees"
1469 # test what happens if a diff is uploaded adding a node to a way.
1470 def test_upload_way_extend
1471 auth_header = basic_authorization_header create(:user).email, "test"
1473 xml = "<osm><changeset>" \
1474 "<tag k='created_by' v='osm test suite checking changesets'/>" \
1475 "</changeset></osm>"
1476 put changeset_create_path, :params => xml, :headers => auth_header
1477 assert_response :success
1478 changeset_id = @response.body.to_i
1480 old_way = create(:way)
1481 create(:way_node, :way => old_way, :node => create(:node, :lat => 1, :lon => 1))
1483 diff = XML::Document.new
1484 diff.root = XML::Node.new "osmChange"
1485 modify = XML::Node.new "modify"
1486 xml_old_way = xml_node_for_way(old_way)
1487 nd_ref = XML::Node.new "nd"
1488 nd_ref["ref"] = create(:node, :lat => 3, :lon => 3).id.to_s
1489 xml_old_way << nd_ref
1490 xml_old_way["changeset"] = changeset_id.to_s
1491 modify << xml_old_way
1495 post changeset_upload_path(:id => changeset_id), :params => diff.to_s, :headers => auth_header
1496 assert_response :success,
1497 "diff should have uploaded OK"
1500 changeset = Changeset.find(changeset_id)
1501 assert_equal 1 * GeoRecord::SCALE, changeset.min_lon, "min_lon should be 1 degree"
1502 assert_equal 3 * GeoRecord::SCALE, changeset.max_lon, "max_lon should be 3 degrees"
1503 assert_equal 1 * GeoRecord::SCALE, changeset.min_lat, "min_lat should be 1 degree"
1504 assert_equal 3 * GeoRecord::SCALE, changeset.max_lat, "max_lat should be 3 degrees"
1508 # test for more issues in #1568
1509 def test_upload_empty_invalid
1510 changeset = create(:changeset)
1512 auth_header = basic_authorization_header changeset.user.email, "test"
1515 "<osmChange></osmChange>",
1516 "<osmChange><modify/></osmChange>",
1517 "<osmChange><modify></modify></osmChange>"].each do |diff|
1519 post changeset_upload_path(changeset), :params => diff, :headers => auth_header
1520 assert_response(:success, "should be able to upload " \
1521 "empty changeset: " + diff)
1526 # test that the X-Error-Format header works to request XML errors
1527 def test_upload_xml_errors
1528 changeset = create(:changeset)
1529 node = create(:node)
1530 create(:relation_member, :member => node)
1532 auth_header = basic_authorization_header changeset.user.email, "test"
1534 # try and delete a node that is in use
1535 diff = XML::Document.new
1536 diff.root = XML::Node.new "osmChange"
1537 delete = XML::Node.new "delete"
1539 delete << xml_node_for_node(node)
1542 error_header = error_format_header "xml"
1543 post changeset_upload_path(changeset), :params => diff.to_s, :headers => auth_header.merge(error_header)
1544 assert_response :success,
1545 "failed to return error in XML format"
1547 # check the returned payload
1548 assert_select "osmError[version='#{Settings.api_version}'][generator='#{Settings.generator}']", 1
1549 assert_select "osmError>status", 1
1550 assert_select "osmError>message", 1
1553 def test_upload_not_found
1554 changeset = create(:changeset)
1556 auth_header = basic_authorization_header changeset.user.email, "test"
1562 <node id='-1' lon='0' lat='0' changeset='#{changeset.id}' version='1'/>
1568 post changeset_upload_path(changeset), :params => diff, :headers => auth_header
1569 assert_response :not_found, "Node should not be found"
1575 <way id='-1' lon='0' lat='0' changeset='#{changeset.id}' version='1'/>
1581 post changeset_upload_path(changeset), :params => diff, :headers => auth_header
1582 assert_response :not_found, "Way should not be found"
1588 <relation id='-1' lon='0' lat='0' changeset='#{changeset.id}' version='1'/>
1594 post changeset_upload_path(changeset), :params => diff, :headers => auth_header
1595 assert_response :not_found, "Relation should not be found"
1601 <node id='-1' lon='0' lat='0' changeset='#{changeset.id}' version='1'/>
1607 post changeset_upload_path(changeset), :params => diff, :headers => auth_header
1608 assert_response :not_found, "Node should not be deleted"
1614 <way id='-1' lon='0' lat='0' changeset='#{changeset.id}' version='1'/>
1620 post changeset_upload_path(changeset), :params => diff, :headers => auth_header
1621 assert_response :not_found, "Way should not be deleted"
1627 <relation id='-1' lon='0' lat='0' changeset='#{changeset.id}' version='1'/>
1633 post changeset_upload_path(changeset), :params => diff, :headers => auth_header
1634 assert_response :not_found, "Relation should not be deleted"
1637 def test_upload_relation_placeholder_not_fix
1638 changeset = create(:changeset)
1640 auth_header = basic_authorization_header changeset.user.email, "test"
1644 <osmChange version='0.6'>
1646 <relation id='-2' version='0' changeset='#{changeset.id}'>
1647 <member type='relation' role='' ref='-4' />
1648 <tag k='type' v='route' />
1649 <tag k='name' v='AtoB' />
1651 <relation id='-3' version='0' changeset='#{changeset.id}'>
1652 <tag k='type' v='route' />
1653 <tag k='name' v='BtoA' />
1655 <relation id='-4' version='0' changeset='#{changeset.id}'>
1656 <member type='relation' role='' ref='-2' />
1657 <member type='relation' role='' ref='-3' />
1658 <tag k='type' v='route_master' />
1659 <tag k='name' v='master' />
1666 post changeset_upload_path(changeset), :params => diff.to_s, :headers => auth_header
1667 assert_response :bad_request, "shouldn't be able to use reference -4 in relation -2: #{@response.body}"
1668 assert_equal "Placeholder Relation not found for reference -4 in relation -2.", @response.body
1671 def test_upload_multiple_delete_block
1672 changeset = create(:changeset)
1674 auth_header = basic_authorization_header changeset.user.email, "test"
1676 node = create(:node)
1678 create(:way_node, :way => way, :node => node)
1679 alone_node = create(:node)
1683 <osmChange version='0.6'>
1684 <delete version="0.6">
1685 <node id="#{node.id}" version="#{node.version}" changeset="#{changeset.id}"/>
1687 <delete version="0.6" if-unused="true">
1688 <node id="#{alone_node.id}" version="#{alone_node.version}" changeset="#{changeset.id}"/>
1694 post changeset_upload_path(changeset), :params => diff.to_s, :headers => auth_header
1695 assert_response :precondition_failed,
1696 "shouldn't be able to upload a invalid deletion diff: #{@response.body}"
1697 assert_equal "Precondition failed: Node #{node.id} is still used by ways #{way.id}.", @response.body
1701 # test initial rate limit
1702 def test_upload_initial_rate_limit
1704 user = create(:user)
1706 # create some objects to use
1707 node = create(:node)
1708 way = create(:way_with_nodes, :nodes_count => 2)
1709 relation = create(:relation)
1711 # create a changeset that puts us near the initial rate limit
1712 changeset = create(:changeset, :user => user,
1713 :created_at => Time.now.utc - 5.minutes,
1714 :num_changes => Settings.initial_changes_per_hour - 2)
1716 # create authentication header
1717 auth_header = basic_authorization_header user.email, "test"
1719 # simple diff to create a node way and relation using placeholders
1723 <node id='-1' lon='0' lat='0' changeset='#{changeset.id}'>
1724 <tag k='foo' v='bar'/>
1725 <tag k='baz' v='bat'/>
1727 <way id='-1' changeset='#{changeset.id}'>
1728 <nd ref='#{node.id}'/>
1732 <relation id='-1' changeset='#{changeset.id}'>
1733 <member type='way' role='some' ref='#{way.id}'/>
1734 <member type='node' role='some' ref='#{node.id}'/>
1735 <member type='relation' role='some' ref='#{relation.id}'/>
1742 post changeset_upload_path(changeset), :params => diff, :headers => auth_header
1743 assert_response :too_many_requests, "upload did not hit rate limit"
1747 # test maximum rate limit
1748 def test_upload_maximum_rate_limit
1750 user = create(:user)
1752 # create some objects to use
1753 node = create(:node)
1754 way = create(:way_with_nodes, :nodes_count => 2)
1755 relation = create(:relation)
1757 # create a changeset to establish our initial edit time
1758 changeset = create(:changeset, :user => user,
1759 :created_at => Time.now.utc - 28.days)
1761 # create changeset to put us near the maximum rate limit
1762 total_changes = Settings.max_changes_per_hour - 2
1763 while total_changes.positive?
1764 changes = [total_changes, Changeset::MAX_ELEMENTS].min
1765 changeset = create(:changeset, :user => user,
1766 :created_at => Time.now.utc - 5.minutes,
1767 :num_changes => changes)
1768 total_changes -= changes
1771 # create authentication header
1772 auth_header = basic_authorization_header user.email, "test"
1774 # simple diff to create a node way and relation using placeholders
1778 <node id='-1' lon='0' lat='0' changeset='#{changeset.id}'>
1779 <tag k='foo' v='bar'/>
1780 <tag k='baz' v='bat'/>
1782 <way id='-1' changeset='#{changeset.id}'>
1783 <nd ref='#{node.id}'/>
1787 <relation id='-1' changeset='#{changeset.id}'>
1788 <member type='way' role='some' ref='#{way.id}'/>
1789 <member type='node' role='some' ref='#{node.id}'/>
1790 <member type='relation' role='some' ref='#{relation.id}'/>
1797 post changeset_upload_path(changeset), :params => diff, :headers => auth_header
1798 assert_response :too_many_requests, "upload did not hit rate limit"
1802 # when we make some simple changes we get the same changes back from the
1804 def test_diff_download_simple
1805 node = create(:node)
1807 ## First try with a non-public user, which should get a forbidden
1808 auth_header = basic_authorization_header create(:user, :data_public => false).email, "test"
1810 # create a temporary changeset
1811 xml = "<osm><changeset>" \
1812 "<tag k='created_by' v='osm test suite checking changesets'/>" \
1813 "</changeset></osm>"
1814 put changeset_create_path, :params => xml, :headers => auth_header
1815 assert_response :forbidden
1817 ## Now try with a normal user
1818 auth_header = basic_authorization_header create(:user).email, "test"
1820 # create a temporary changeset
1821 xml = "<osm><changeset>" \
1822 "<tag k='created_by' v='osm test suite checking changesets'/>" \
1823 "</changeset></osm>"
1824 put changeset_create_path, :params => xml, :headers => auth_header
1825 assert_response :success
1826 changeset_id = @response.body.to_i
1832 <node id='#{node.id}' lon='0' lat='0' changeset='#{changeset_id}' version='1'/>
1833 <node id='#{node.id}' lon='1' lat='0' changeset='#{changeset_id}' version='2'/>
1834 <node id='#{node.id}' lon='1' lat='1' changeset='#{changeset_id}' version='3'/>
1835 <node id='#{node.id}' lon='1' lat='2' changeset='#{changeset_id}' version='4'/>
1836 <node id='#{node.id}' lon='2' lat='2' changeset='#{changeset_id}' version='5'/>
1837 <node id='#{node.id}' lon='3' lat='2' changeset='#{changeset_id}' version='6'/>
1838 <node id='#{node.id}' lon='3' lat='3' changeset='#{changeset_id}' version='7'/>
1839 <node id='#{node.id}' lon='9' lat='9' changeset='#{changeset_id}' version='8'/>
1845 post changeset_upload_path(:id => changeset_id), :params => diff, :headers => auth_header
1846 assert_response :success,
1847 "can't upload multiple versions of an element in a diff: #{@response.body}"
1849 get changeset_download_path(:id => changeset_id)
1850 assert_response :success
1852 assert_select "osmChange", 1
1853 assert_select "osmChange>modify", 8
1854 assert_select "osmChange>modify>node", 8
1858 # culled this from josm to ensure that nothing in the way that josm
1859 # is formatting the request is causing it to fail.
1861 # NOTE: the error turned out to be something else completely!
1862 def test_josm_upload
1863 auth_header = basic_authorization_header create(:user).email, "test"
1865 # create a temporary changeset
1866 xml = "<osm><changeset>" \
1867 "<tag k='created_by' v='osm test suite checking changesets'/>" \
1868 "</changeset></osm>"
1869 put changeset_create_path, :params => xml, :headers => auth_header
1870 assert_response :success
1871 changeset_id = @response.body.to_i
1874 <osmChange version="0.6" generator="JOSM">
1875 <create version="0.6" generator="JOSM">
1876 <node id='-1' visible='true' changeset='#{changeset_id}' lat='51.49619982187321' lon='-0.18722061869438314' />
1877 <node id='-2' visible='true' changeset='#{changeset_id}' lat='51.496359883909605' lon='-0.18653093576241928' />
1878 <node id='-3' visible='true' changeset='#{changeset_id}' lat='51.49598132358285' lon='-0.18719613290981638' />
1879 <node id='-4' visible='true' changeset='#{changeset_id}' lat='51.4961591711078' lon='-0.18629015888084607' />
1880 <node id='-5' visible='true' changeset='#{changeset_id}' lat='51.49582126021711' lon='-0.18708186591517145' />
1881 <node id='-6' visible='true' changeset='#{changeset_id}' lat='51.49591018437858' lon='-0.1861432441734455' />
1882 <node id='-7' visible='true' changeset='#{changeset_id}' lat='51.49560784152179' lon='-0.18694719410005425' />
1883 <node id='-8' visible='true' changeset='#{changeset_id}' lat='51.49567389979617' lon='-0.1860289771788006' />
1884 <node id='-9' visible='true' changeset='#{changeset_id}' lat='51.49543761398892' lon='-0.186820684213126' />
1885 <way id='-10' action='modify' visible='true' changeset='#{changeset_id}'>
1895 <tag k='highway' v='residential' />
1896 <tag k='name' v='Foobar Street' />
1903 post changeset_upload_path(:id => changeset_id), :params => diff, :headers => auth_header
1904 assert_response :success,
1905 "can't upload a diff from JOSM: #{@response.body}"
1907 get changeset_download_path(:id => changeset_id)
1908 assert_response :success
1910 assert_select "osmChange", 1
1911 assert_select "osmChange>create>node", 9
1912 assert_select "osmChange>create>way", 1
1913 assert_select "osmChange>create>way>nd", 9
1914 assert_select "osmChange>create>way>tag", 2
1918 # when we make some complex changes we get the same changes back from the
1920 def test_diff_download_complex
1921 node = create(:node)
1922 node2 = create(:node)
1924 auth_header = basic_authorization_header create(:user).email, "test"
1926 # create a temporary changeset
1927 xml = "<osm><changeset>" \
1928 "<tag k='created_by' v='osm test suite checking changesets'/>" \
1929 "</changeset></osm>"
1930 put changeset_create_path, :params => xml, :headers => auth_header
1931 assert_response :success
1932 changeset_id = @response.body.to_i
1938 <node id='#{node.id}' lon='0' lat='0' changeset='#{changeset_id}' version='1'/>
1941 <node id='-1' lon='9' lat='9' changeset='#{changeset_id}' version='0'/>
1942 <node id='-2' lon='8' lat='9' changeset='#{changeset_id}' version='0'/>
1943 <node id='-3' lon='7' lat='9' changeset='#{changeset_id}' version='0'/>
1946 <node id='#{node2.id}' lon='20' lat='15' changeset='#{changeset_id}' version='1'/>
1947 <way id='#{way.id}' changeset='#{changeset_id}' version='1'>
1948 <nd ref='#{node2.id}'/>
1958 post changeset_upload_path(:id => changeset_id), :params => diff, :headers => auth_header
1959 assert_response :success,
1960 "can't upload multiple versions of an element in a diff: #{@response.body}"
1962 get changeset_download_path(:id => changeset_id)
1963 assert_response :success
1965 assert_select "osmChange", 1
1966 assert_select "osmChange>create", 3
1967 assert_select "osmChange>delete", 1
1968 assert_select "osmChange>modify", 2
1969 assert_select "osmChange>create>node", 3
1970 assert_select "osmChange>delete>node", 1
1971 assert_select "osmChange>modify>node", 1
1972 assert_select "osmChange>modify>way", 1
1975 def test_changeset_download
1976 changeset = create(:changeset)
1977 node = create(:node, :with_history, :version => 1, :changeset => changeset)
1978 tag = create(:old_node_tag, :old_node => node.old_nodes.find_by(:version => 1))
1979 node2 = create(:node, :with_history, :version => 1, :changeset => changeset)
1980 _node3 = create(:node, :with_history, :deleted, :version => 1, :changeset => changeset)
1981 _relation = create(:relation, :with_history, :version => 1, :changeset => changeset)
1982 _relation2 = create(:relation, :with_history, :deleted, :version => 1, :changeset => changeset)
1984 get changeset_download_path(changeset)
1986 assert_response :success
1988 # FIXME: needs more assert_select tests
1989 assert_select "osmChange[version='#{Settings.api_version}'][generator='#{Settings.generator}']" do
1990 assert_select "create", :count => 5
1991 assert_select "create>node[id='#{node.id}'][visible='#{node.visible?}'][version='#{node.version}']" do
1992 assert_select "tag[k='#{tag.k}'][v='#{tag.v}']"
1994 assert_select "create>node[id='#{node2.id}']"
1999 # check that the bounding box of a changeset gets updated correctly
2000 # FIXME: This should really be moded to a integration test due to the with_controller
2001 def test_changeset_bbox
2003 create(:way_node, :way => way, :node => create(:node, :lat => 3, :lon => 3))
2005 auth_header = basic_authorization_header create(:user).email, "test"
2007 # create a new changeset
2008 xml = "<osm><changeset/></osm>"
2009 put changeset_create_path, :params => xml, :headers => auth_header
2010 assert_response :success, "Creating of changeset failed."
2011 changeset_id = @response.body.to_i
2013 # add a single node to it
2014 with_controller(NodesController.new) do
2015 xml = "<osm><node lon='1' lat='2' changeset='#{changeset_id}'/></osm>"
2016 put node_create_path, :params => xml, :headers => auth_header
2017 assert_response :success, "Couldn't create node."
2020 # get the bounding box back from the changeset
2021 get changeset_show_path(:id => changeset_id)
2022 assert_response :success, "Couldn't read back changeset."
2023 assert_select "osm>changeset[min_lon='1.0000000']", 1
2024 assert_select "osm>changeset[max_lon='1.0000000']", 1
2025 assert_select "osm>changeset[min_lat='2.0000000']", 1
2026 assert_select "osm>changeset[max_lat='2.0000000']", 1
2028 # add another node to it
2029 with_controller(NodesController.new) do
2030 xml = "<osm><node lon='2' lat='1' changeset='#{changeset_id}'/></osm>"
2031 put node_create_path, :params => xml, :headers => auth_header
2032 assert_response :success, "Couldn't create second node."
2035 # get the bounding box back from the changeset
2036 get changeset_show_path(:id => changeset_id)
2037 assert_response :success, "Couldn't read back changeset for the second time."
2038 assert_select "osm>changeset[min_lon='1.0000000']", 1
2039 assert_select "osm>changeset[max_lon='2.0000000']", 1
2040 assert_select "osm>changeset[min_lat='1.0000000']", 1
2041 assert_select "osm>changeset[max_lat='2.0000000']", 1
2043 # add (delete) a way to it, which contains a point at (3,3)
2044 with_controller(WaysController.new) do
2045 xml = update_changeset(xml_for_way(way), changeset_id)
2046 delete api_way_path(way), :params => xml.to_s, :headers => auth_header
2047 assert_response :success, "Couldn't delete a way."
2050 # get the bounding box back from the changeset
2051 get changeset_show_path(:id => changeset_id)
2052 assert_response :success, "Couldn't read back changeset for the third time."
2053 assert_select "osm>changeset[min_lon='1.0000000']", 1
2054 assert_select "osm>changeset[max_lon='3.0000000']", 1
2055 assert_select "osm>changeset[min_lat='1.0000000']", 1
2056 assert_select "osm>changeset[max_lat='3.0000000']", 1
2060 # test the query functionality of changesets
2062 private_user = create(:user, :data_public => false)
2063 private_user_changeset = create(:changeset, :user => private_user)
2064 private_user_closed_changeset = create(:changeset, :closed, :user => private_user)
2065 user = create(:user)
2066 changeset = create(:changeset, :user => user)
2067 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))
2068 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)
2069 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)
2071 get changesets_path(:bbox => "-10,-10, 10, 10")
2072 assert_response :success, "can't get changesets in bbox"
2073 assert_changesets [changeset2, changeset3]
2075 get changesets_path(:bbox => "4.5,4.5,4.6,4.6")
2076 assert_response :success, "can't get changesets in bbox"
2077 assert_changesets [changeset3]
2079 # not found when looking for changesets of non-existing users
2080 get changesets_path(:user => User.maximum(:id) + 1)
2081 assert_response :not_found
2082 assert_equal "text/plain", @response.media_type
2083 get changesets_path(:display_name => " ")
2084 assert_response :not_found
2085 assert_equal "text/plain", @response.media_type
2087 # can't get changesets of user 1 without authenticating
2088 get changesets_path(:user => private_user.id)
2089 assert_response :not_found, "shouldn't be able to get changesets by non-public user (ID)"
2090 get changesets_path(:display_name => private_user.display_name)
2091 assert_response :not_found, "shouldn't be able to get changesets by non-public user (name)"
2093 # but this should work
2094 auth_header = basic_authorization_header private_user.email, "test"
2095 get changesets_path(:user => private_user.id), :headers => auth_header
2096 assert_response :success, "can't get changesets by user ID"
2097 assert_changesets [private_user_changeset, private_user_closed_changeset]
2099 get changesets_path(:display_name => private_user.display_name), :headers => auth_header
2100 assert_response :success, "can't get changesets by user name"
2101 assert_changesets [private_user_changeset, private_user_closed_changeset]
2103 # test json endpoint
2104 get changesets_path(:display_name => private_user.display_name), :headers => auth_header, :params => { :format => "json" }
2105 assert_response :success, "can't get changesets by user name"
2107 js = ActiveSupport::JSON.decode(@response.body)
2110 assert_equal Settings.api_version, js["version"]
2111 assert_equal Settings.generator, js["generator"]
2112 assert_equal 2, js["changesets"].count
2114 # check that the correct error is given when we provide both UID and name
2115 get changesets_path(:user => private_user.id,
2116 :display_name => private_user.display_name), :headers => auth_header
2117 assert_response :bad_request, "should be a bad request to have both ID and name specified"
2119 get changesets_path(:user => private_user.id, :open => true), :headers => auth_header
2120 assert_response :success, "can't get changesets by user and open"
2121 assert_changesets [private_user_changeset]
2123 get changesets_path(:time => "2007-12-31"), :headers => auth_header
2124 assert_response :success, "can't get changesets by time-since"
2125 assert_changesets [private_user_changeset, private_user_closed_changeset, changeset, closed_changeset, changeset2, changeset3]
2127 get changesets_path(:time => "2008-01-01T12:34Z"), :headers => auth_header
2128 assert_response :success, "can't get changesets by time-since with hour"
2129 assert_changesets [private_user_changeset, private_user_closed_changeset, changeset, closed_changeset, changeset2, changeset3]
2131 get changesets_path(:time => "2007-12-31T23:59Z,2008-01-02T00:01Z"), :headers => auth_header
2132 assert_response :success, "can't get changesets by time-range"
2133 assert_changesets [closed_changeset]
2135 get changesets_path(:open => "true"), :headers => auth_header
2136 assert_response :success, "can't get changesets by open-ness"
2137 assert_changesets [private_user_changeset, changeset, changeset2, changeset3]
2139 get changesets_path(:closed => "true"), :headers => auth_header
2140 assert_response :success, "can't get changesets by closed-ness"
2141 assert_changesets [private_user_closed_changeset, closed_changeset]
2143 get changesets_path(:closed => "true", :user => private_user.id), :headers => auth_header
2144 assert_response :success, "can't get changesets by closed-ness and user"
2145 assert_changesets [private_user_closed_changeset]
2147 get changesets_path(:closed => "true", :user => user.id), :headers => auth_header
2148 assert_response :success, "can't get changesets by closed-ness and user"
2149 assert_changesets [closed_changeset]
2151 get changesets_path(:changesets => "#{private_user_changeset.id},#{changeset.id},#{closed_changeset.id}"), :headers => auth_header
2152 assert_response :success, "can't get changesets by id (as comma-separated string)"
2153 assert_changesets [private_user_changeset, changeset, closed_changeset]
2155 get changesets_path(:changesets => ""), :headers => auth_header
2156 assert_response :bad_request, "should be a bad request since changesets is empty"
2160 # test the query functionality of changesets with the limit parameter
2161 def test_query_limit
2162 user = create(:user)
2163 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))
2164 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))
2165 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))
2166 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))
2167 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))
2170 assert_response :success
2171 assert_changesets_in_order [changeset5, changeset4, changeset3, changeset2, changeset1]
2173 get changesets_path(:limit => "3")
2174 assert_response :success
2175 assert_changesets_in_order [changeset5, changeset4, changeset3]
2177 get changesets_path(:limit => "0")
2178 assert_response :bad_request
2180 get changesets_path(:limit => Settings.max_changeset_query_limit)
2181 assert_response :success
2182 assert_changesets_in_order [changeset5, changeset4, changeset3, changeset2, changeset1]
2184 get changesets_path(:limit => Settings.max_changeset_query_limit + 1)
2185 assert_response :bad_request
2189 # test the query functionality of sequential changesets with order and time parameters
2190 def test_query_order
2191 user = create(:user)
2192 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))
2193 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))
2194 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))
2195 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))
2196 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))
2197 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))
2200 assert_response :success
2201 assert_changesets_in_order [changeset6, changeset5, changeset4, changeset3, changeset2, changeset1]
2203 get changesets_path(:order => "oldest")
2204 assert_response :success
2205 assert_changesets_in_order [changeset1, changeset2, changeset3, changeset4, changeset5, changeset6]
2208 # lower time bound at the opening time of a changeset
2209 ["2008-02-01T00:00:00Z", "2008-05-15T00:00:00Z", [changeset5, changeset4, changeset3, changeset2], [changeset5, changeset4, changeset3, changeset2]],
2210 # lower time bound in the middle of a changeset
2211 ["2008-02-01T12:00:00Z", "2008-05-15T00:00:00Z", [changeset5, changeset4, changeset3, changeset2], [changeset5, changeset4, changeset3]],
2212 # lower time bound at the closing time of a changeset
2213 ["2008-02-02T00:00:00Z", "2008-05-15T00:00:00Z", [changeset5, changeset4, changeset3, changeset2], [changeset5, changeset4, changeset3]],
2214 # lower time bound after the closing time of a changeset
2215 ["2008-02-02T00:00:01Z", "2008-05-15T00:00:00Z", [changeset5, changeset4, changeset3], [changeset5, changeset4, changeset3]],
2216 # upper time bound in the middle of a changeset
2217 ["2007-09-09T12:00:00Z", "2008-04-01T12:00:00Z", [changeset4, changeset3, changeset2, changeset1], [changeset4, changeset3, changeset2, changeset1]],
2219 ["2009-02-02T00:00:01Z", "2018-05-15T00:00:00Z", [], []]
2220 ].each do |from, to, interval_changesets, point_changesets|
2221 get changesets_path(:time => "#{from},#{to}")
2222 assert_response :success
2223 assert_changesets_in_order interval_changesets
2225 get changesets_path(:from => from, :to => to)
2226 assert_response :success
2227 assert_changesets_in_order point_changesets
2229 get changesets_path(:from => from, :to => to, :order => "oldest")
2230 assert_response :success
2231 assert_changesets_in_order point_changesets.reverse
2236 # test the query functionality of overlapping changesets with order and time parameters
2237 def test_query_order_overlapping
2238 user = create(:user)
2239 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))
2240 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))
2241 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))
2242 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))
2243 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))
2245 get changesets_path(:time => "2015-06-04T00:00:00Z")
2246 assert_response :success
2247 assert_changesets_in_order [changeset1, changeset2, changeset3, changeset4]
2249 get changesets_path(:from => "2015-06-04T00:00:00Z")
2250 assert_response :success
2251 assert_changesets_in_order [changeset1, changeset2, changeset3]
2253 get changesets_path(:from => "2015-06-04T00:00:00Z", :order => "oldest")
2254 assert_response :success
2255 assert_changesets_in_order [changeset3, changeset2, changeset1]
2257 get changesets_path(:time => "2015-06-04T16:00:00Z,2015-06-04T17:30:00Z")
2258 assert_response :success
2259 assert_changesets_in_order [changeset1, changeset2, changeset3, changeset4]
2261 get changesets_path(:from => "2015-06-04T16:00:00Z", :to => "2015-06-04T17:30:00Z")
2262 assert_response :success
2263 assert_changesets_in_order [changeset1, changeset2]
2265 get changesets_path(:from => "2015-06-04T16:00:00Z", :to => "2015-06-04T17:30:00Z", :order => "oldest")
2266 assert_response :success
2267 assert_changesets_in_order [changeset2, changeset1]
2271 # check that errors are returned if garbage is inserted
2272 # into query strings
2273 def test_query_invalid
2276 ";drop table users;"].each do |bbox|
2277 get changesets_path(:bbox => bbox)
2278 assert_response :bad_request, "'#{bbox}' isn't a bbox"
2283 ";drop table users;",
2285 "-,-"].each do |time|
2286 get changesets_path(:time => time)
2287 assert_response :bad_request, "'#{time}' isn't a valid time range"
2294 get changesets_path(:user => uid)
2295 assert_response :bad_request, "'#{uid}' isn't a valid user ID"
2298 get changesets_path(:order => "oldest", :time => "2008-01-01T00:00Z,2018-01-01T00:00Z")
2299 assert_response :bad_request, "cannot use order=oldest with time"
2303 # check updating tags on a changeset
2304 def test_changeset_update
2305 private_user = create(:user, :data_public => false)
2306 private_changeset = create(:changeset, :user => private_user)
2307 user = create(:user)
2308 changeset = create(:changeset, :user => user)
2310 ## First try with a non-public user
2311 new_changeset = create_changeset_xml(:user => private_user)
2312 new_tag = XML::Node.new "tag"
2313 new_tag["k"] = "tagtesting"
2314 new_tag["v"] = "valuetesting"
2315 new_changeset.find("//osm/changeset").first << new_tag
2317 # try without any authorization
2318 put changeset_show_path(private_changeset), :params => new_changeset.to_s
2319 assert_response :unauthorized
2321 # try with the wrong authorization
2322 auth_header = basic_authorization_header create(:user).email, "test"
2323 put changeset_show_path(private_changeset), :params => new_changeset.to_s, :headers => auth_header
2324 assert_response :conflict
2326 # now this should get an unauthorized
2327 auth_header = basic_authorization_header private_user.email, "test"
2328 put changeset_show_path(private_changeset), :params => new_changeset.to_s, :headers => auth_header
2329 assert_require_public_data "user with their data non-public, shouldn't be able to edit their changeset"
2331 ## Now try with the public user
2332 new_changeset = create_changeset_xml(:id => 1)
2333 new_tag = XML::Node.new "tag"
2334 new_tag["k"] = "tagtesting"
2335 new_tag["v"] = "valuetesting"
2336 new_changeset.find("//osm/changeset").first << new_tag
2338 # try without any authorization
2339 put changeset_show_path(changeset), :params => new_changeset.to_s
2340 assert_response :unauthorized
2342 # try with the wrong authorization
2343 auth_header = basic_authorization_header create(:user).email, "test"
2344 put changeset_show_path(changeset), :params => new_changeset.to_s, :headers => auth_header
2345 assert_response :conflict
2347 # now this should work...
2348 auth_header = basic_authorization_header user.email, "test"
2349 put changeset_show_path(changeset), :params => new_changeset.to_s, :headers => auth_header
2350 assert_response :success
2352 assert_select "osm>changeset[id='#{changeset.id}']", 1
2353 assert_select "osm>changeset>tag", 1
2354 assert_select "osm>changeset>tag[k='tagtesting'][v='valuetesting']", 1
2358 # check that a user different from the one who opened the changeset
2360 def test_changeset_update_invalid
2361 auth_header = basic_authorization_header create(:user).email, "test"
2363 changeset = create(:changeset)
2364 new_changeset = create_changeset_xml(:user => changeset.user, :id => changeset.id)
2365 new_tag = XML::Node.new "tag"
2366 new_tag["k"] = "testing"
2367 new_tag["v"] = "testing"
2368 new_changeset.find("//osm/changeset").first << new_tag
2370 put changeset_show_path(changeset), :params => new_changeset.to_s, :headers => auth_header
2371 assert_response :conflict
2375 # check that a changeset can contain a certain max number of changes.
2376 ## FIXME should be changed to an integration test due to the with_controller
2377 def test_changeset_limits
2378 user = create(:user)
2379 auth_header = basic_authorization_header user.email, "test"
2381 # create an old changeset to ensure we have the maximum rate limit
2382 create(:changeset, :user => user, :created_at => Time.now.utc - 28.days)
2384 # open a new changeset
2385 xml = "<osm><changeset/></osm>"
2386 put changeset_create_path, :params => xml, :headers => auth_header
2387 assert_response :success, "can't create a new changeset"
2388 cs_id = @response.body.to_i
2390 # start the counter just short of where the changeset should finish.
2392 # alter the database to set the counter on the changeset directly,
2393 # otherwise it takes about 6 minutes to fill all of them.
2394 changeset = Changeset.find(cs_id)
2395 changeset.num_changes = Changeset::MAX_ELEMENTS - offset
2398 with_controller(NodesController.new) do
2400 xml = "<osm><node changeset='#{cs_id}' lat='0.0' lon='0.0'/></osm>"
2401 put node_create_path, :params => xml, :headers => auth_header
2402 assert_response :success, "can't create a new node"
2403 node_id = @response.body.to_i
2405 get api_node_path(:id => node_id)
2406 assert_response :success, "can't read back new node"
2407 node_doc = XML::Parser.string(@response.body).parse
2408 node_xml = node_doc.find("//osm/node").first
2410 # loop until we fill the changeset with nodes
2412 node_xml["lat"] = rand.to_s
2413 node_xml["lon"] = rand.to_s
2414 node_xml["version"] = (i + 1).to_s
2416 put api_node_path(:id => node_id), :params => node_doc.to_s, :headers => auth_header
2417 assert_response :success, "attempt #{i} should have succeeded"
2420 # trying again should fail
2421 node_xml["lat"] = rand.to_s
2422 node_xml["lon"] = rand.to_s
2423 node_xml["version"] = offset.to_s
2425 put api_node_path(:id => node_id), :params => node_doc.to_s, :headers => auth_header
2426 assert_response :conflict, "final attempt should have failed"
2429 changeset = Changeset.find(cs_id)
2430 assert_equal Changeset::MAX_ELEMENTS + 1, changeset.num_changes
2432 # check that the changeset is now closed as well
2433 assert_not(changeset.open?,
2434 "changeset should have been auto-closed by exceeding " \
2439 # check that the changeset download for a changeset with a redacted
2440 # element in it doesn't contain that element.
2441 def test_diff_download_redacted
2442 changeset = create(:changeset)
2443 node = create(:node, :with_history, :version => 2, :changeset => changeset)
2444 node_v1 = node.old_nodes.find_by(:version => 1)
2445 node_v1.redact!(create(:redaction))
2447 get changeset_download_path(changeset)
2448 assert_response :success
2450 assert_select "osmChange", 1
2451 # this changeset contains the node in versions 1 & 2, but 1 should
2453 assert_select "osmChange node[id='#{node.id}']", 1
2454 assert_select "osmChange node[id='#{node.id}'][version='1']", 0
2458 # test subscribe success
2459 def test_subscribe_success
2460 auth_header = basic_authorization_header create(:user).email, "test"
2461 changeset = create(:changeset, :closed)
2463 assert_difference "changeset.subscribers.count", 1 do
2464 post api_changeset_subscribe_path(changeset), :headers => auth_header
2466 assert_response :success
2468 # not closed changeset
2469 changeset = create(:changeset)
2470 assert_difference "changeset.subscribers.count", 1 do
2471 post api_changeset_subscribe_path(changeset), :headers => auth_header
2473 assert_response :success
2477 # test subscribe fail
2478 def test_subscribe_fail
2479 user = create(:user)
2482 changeset = create(:changeset, :closed)
2483 assert_no_difference "changeset.subscribers.count" do
2484 post api_changeset_subscribe_path(changeset)
2486 assert_response :unauthorized
2488 auth_header = basic_authorization_header user.email, "test"
2491 assert_no_difference "changeset.subscribers.count" do
2492 post api_changeset_subscribe_path(:id => 999111), :headers => auth_header
2494 assert_response :not_found
2496 # trying to subscribe when already subscribed
2497 changeset = create(:changeset, :closed)
2498 changeset.subscribers.push(user)
2499 assert_no_difference "changeset.subscribers.count" do
2500 post api_changeset_subscribe_path(changeset), :headers => auth_header
2502 assert_response :conflict
2506 # test unsubscribe success
2507 def test_unsubscribe_success
2508 user = create(:user)
2509 auth_header = basic_authorization_header user.email, "test"
2510 changeset = create(:changeset, :closed)
2511 changeset.subscribers.push(user)
2513 assert_difference "changeset.subscribers.count", -1 do
2514 post api_changeset_unsubscribe_path(changeset), :headers => auth_header
2516 assert_response :success
2518 # not closed changeset
2519 changeset = create(:changeset)
2520 changeset.subscribers.push(user)
2522 assert_difference "changeset.subscribers.count", -1 do
2523 post api_changeset_unsubscribe_path(changeset), :headers => auth_header
2525 assert_response :success
2529 # test unsubscribe fail
2530 def test_unsubscribe_fail
2532 changeset = create(:changeset, :closed)
2533 assert_no_difference "changeset.subscribers.count" do
2534 post api_changeset_unsubscribe_path(changeset)
2536 assert_response :unauthorized
2538 auth_header = basic_authorization_header create(:user).email, "test"
2541 assert_no_difference "changeset.subscribers.count" do
2542 post api_changeset_unsubscribe_path(:id => 999111), :headers => auth_header
2544 assert_response :not_found
2546 # trying to unsubscribe when not subscribed
2547 changeset = create(:changeset, :closed)
2548 assert_no_difference "changeset.subscribers.count" do
2549 post api_changeset_unsubscribe_path(changeset), :headers => auth_header
2551 assert_response :not_found
2557 # check that the output consists of one specific changeset
2558 def assert_single_changeset(changeset)
2559 assert_select "osm>changeset", 1
2560 assert_select "osm>changeset>@id", changeset.id.to_s
2561 assert_select "osm>changeset>@created_at", changeset.created_at.xmlschema
2563 assert_select "osm>changeset>@open", "true"
2564 assert_select "osm>changeset>@closed_at", 0
2566 assert_select "osm>changeset>@open", "false"
2567 assert_select "osm>changeset>@closed_at", changeset.closed_at.xmlschema
2571 def assert_single_changeset_json(changeset, js)
2572 assert_equal changeset.id, js["changeset"]["id"]
2573 assert_equal changeset.created_at.xmlschema, js["changeset"]["created_at"]
2575 assert js["changeset"]["open"]
2576 assert_nil js["changeset"]["closed_at"]
2578 assert_not js["changeset"]["open"]
2579 assert_equal changeset.closed_at.xmlschema, js["changeset"]["closed_at"]
2584 # check that certain changesets exist in the output
2585 def assert_changesets(changesets)
2586 assert_select "osm>changeset", changesets.size
2587 changesets.each do |changeset|
2588 assert_select "osm>changeset[id='#{changeset.id}']", 1
2593 # check that certain changesets exist in the output in the specified order
2594 def assert_changesets_in_order(changesets)
2595 assert_select "osm>changeset", changesets.size
2596 changesets.each_with_index do |changeset, index|
2597 assert_select "osm>changeset:nth-child(#{index + 1})[id='#{changeset.id}']", 1
2602 # update the changeset_id of a way element
2603 def update_changeset(xml, changeset_id)
2604 xml_attr_rewrite(xml, "changeset", changeset_id)
2608 # update an attribute in a way element
2609 def xml_attr_rewrite(xml, name, value)
2610 xml.find("//osm/way").first[name] = value.to_s
2615 # build XML for changesets
2616 def create_changeset_xml(user: nil, id: nil)
2617 root = XML::Document.new
2618 root.root = XML::Node.new "osm"
2619 cs = XML::Node.new "changeset"
2621 cs["user"] = user.display_name
2622 cs["uid"] = user.id.to_s
2624 cs["id"] = id.to_s if id