]> git.openstreetmap.org Git - rails.git/blob - test/controllers/changeset_controller_test.rb
3fb340ee8914997943615a6e652ef50732bc3e7e
[rails.git] / test / controllers / changeset_controller_test.rb
1 require "test_helper"
2 require "changeset_controller"
3
4 class ChangesetControllerTest < ActionController::TestCase
5   api_fixtures
6
7   ##
8   # test all routes which lead to this controller
9   def test_routes
10     assert_routing(
11       { :path => "/api/0.6/changeset/create", :method => :put },
12       { :controller => "changeset", :action => "create" }
13     )
14     assert_routing(
15       { :path => "/api/0.6/changeset/1/upload", :method => :post },
16       { :controller => "changeset", :action => "upload", :id => "1" }
17     )
18     assert_routing(
19       { :path => "/api/0.6/changeset/1/download", :method => :get },
20       { :controller => "changeset", :action => "download", :id => "1" }
21     )
22     assert_routing(
23       { :path => "/api/0.6/changeset/1/expand_bbox", :method => :post },
24       { :controller => "changeset", :action => "expand_bbox", :id => "1" }
25     )
26     assert_routing(
27       { :path => "/api/0.6/changeset/1", :method => :get },
28       { :controller => "changeset", :action => "read", :id => "1" }
29     )
30     assert_routing(
31       { :path => "/api/0.6/changeset/1/subscribe", :method => :post },
32       { :controller => "changeset", :action => "subscribe", :id => "1" }
33     )
34     assert_routing(
35       { :path => "/api/0.6/changeset/1/unsubscribe", :method => :post },
36       { :controller => "changeset", :action => "unsubscribe", :id => "1" }
37     )
38     assert_routing(
39       { :path => "/api/0.6/changeset/1", :method => :put },
40       { :controller => "changeset", :action => "update", :id => "1" }
41     )
42     assert_routing(
43       { :path => "/api/0.6/changeset/1/close", :method => :put },
44       { :controller => "changeset", :action => "close", :id => "1" }
45     )
46     assert_routing(
47       { :path => "/api/0.6/changeset/1/comment", :method => :post },
48       { :controller => "changeset", :action => "comment", :id => "1" }
49     )
50     assert_routing(
51       { :path => "/api/0.6/changeset/comment/1/hide", :method => :post },
52       { :controller => "changeset", :action => "hide_comment", :id => "1" }
53     )
54     assert_routing(
55       { :path => "/api/0.6/changeset/comment/1/unhide", :method => :post },
56       { :controller => "changeset", :action => "unhide_comment", :id => "1" }
57     )
58     assert_routing(
59       { :path => "/api/0.6/changesets", :method => :get },
60       { :controller => "changeset", :action => "query" }
61     )
62     assert_routing(
63       { :path => "/changeset/1/comments/feed", :method => :get },
64       { :controller => "changeset", :action => "comments_feed", :id => "1", :format => "rss" }
65     )
66     assert_routing(
67       { :path => "/user/name/history", :method => :get },
68       { :controller => "changeset", :action => "list", :display_name => "name" }
69     )
70     assert_routing(
71       { :path => "/user/name/history/feed", :method => :get },
72       { :controller => "changeset", :action => "feed", :display_name => "name", :format => :atom }
73     )
74     assert_routing(
75       { :path => "/history/friends", :method => :get },
76       { :controller => "changeset", :action => "list", :friends => true, :format => :html }
77     )
78     assert_routing(
79       { :path => "/history/nearby", :method => :get },
80       { :controller => "changeset", :action => "list", :nearby => true, :format => :html }
81     )
82     assert_routing(
83       { :path => "/history", :method => :get },
84       { :controller => "changeset", :action => "list" }
85     )
86     assert_routing(
87       { :path => "/history/feed", :method => :get },
88       { :controller => "changeset", :action => "feed", :format => :atom }
89     )
90     assert_routing(
91       { :path => "/history/comments/feed", :method => :get },
92       { :controller => "changeset", :action => "comments_feed", :format => "rss" }
93     )
94   end
95
96   # -----------------------
97   # Test simple changeset creation
98   # -----------------------
99
100   def test_create
101     basic_authorization create(:user, :data_public => false).email, "test"
102     # Create the first user's changeset
103     content "<osm><changeset>" +
104             "<tag k='created_by' v='osm test suite checking changesets'/>" +
105             "</changeset></osm>"
106     put :create
107     assert_require_public_data
108
109     basic_authorization create(:user).email, "test"
110     # Create the first user's changeset
111     content "<osm><changeset>" +
112             "<tag k='created_by' v='osm test suite checking changesets'/>" +
113             "</changeset></osm>"
114     put :create
115
116     assert_response :success, "Creation of changeset did not return sucess status"
117     newid = @response.body.to_i
118
119     # check end time, should be an hour ahead of creation time
120     cs = Changeset.find(newid)
121     duration = cs.closed_at - cs.created_at
122     # the difference can either be a rational, or a floating point number
123     # of seconds, depending on the code path taken :-(
124     if duration.class == Rational
125       assert_equal Rational(1, 24), duration, "initial idle timeout should be an hour (#{cs.created_at} -> #{cs.closed_at})"
126     else
127       # must be number of seconds...
128       assert_equal 3600, duration.round, "initial idle timeout should be an hour (#{cs.created_at} -> #{cs.closed_at})"
129     end
130
131     # checks if uploader was subscribed
132     assert_equal 1, cs.subscribers.length
133   end
134
135   def test_create_invalid
136     basic_authorization create(:user, :data_public => false).email, "test"
137     content "<osm><changeset></osm>"
138     put :create
139     assert_require_public_data
140
141     ## Try the public user
142     basic_authorization create(:user).email, "test"
143     content "<osm><changeset></osm>"
144     put :create
145     assert_response :bad_request, "creating a invalid changeset should fail"
146   end
147
148   def test_create_invalid_no_content
149     ## First check with no auth
150     put :create
151     assert_response :unauthorized, "shouldn't be able to create a changeset with no auth"
152
153     ## Now try to with a non-public user
154     basic_authorization create(:user, :data_public => false).email, "test"
155     put :create
156     assert_require_public_data
157
158     ## Try an inactive user
159     basic_authorization create(:user, :pending).email, "test"
160     put :create
161     assert_inactive_user
162
163     ## Now try to use a normal user
164     basic_authorization create(:user).email, "test"
165     put :create
166     assert_response :bad_request, "creating a changeset with no content should fail"
167   end
168
169   def test_create_wrong_method
170     basic_authorization create(:user).email, "test"
171     get :create
172     assert_response :method_not_allowed
173     post :create
174     assert_response :method_not_allowed
175   end
176
177   ##
178   # check that the changeset can be read and returns the correct
179   # document structure.
180   def test_read
181     changeset_id = changesets(:normal_user_first_change).id
182
183     get :read, :id => changeset_id
184     assert_response :success, "cannot get first changeset"
185
186     assert_select "osm[version='#{API_VERSION}'][generator='OpenStreetMap server']", 1
187     assert_select "osm>changeset[id='#{changeset_id}']", 1
188     assert_select "osm>changeset>discussion", 0
189
190     get :read, :id => changeset_id, :include_discussion => true
191     assert_response :success, "cannot get first changeset with comments"
192
193     assert_select "osm[version='#{API_VERSION}'][generator='OpenStreetMap server']", 1
194     assert_select "osm>changeset[id='#{changeset_id}']", 1
195     assert_select "osm>changeset>discussion", 1
196     assert_select "osm>changeset>discussion>comment", 0
197
198     changeset_id = changesets(:normal_user_closed_change).id
199     create_list(:changeset_comment, 3, :changeset_id => changeset_id)
200
201     get :read, :id => changeset_id, :include_discussion => true
202     assert_response :success, "cannot get closed changeset with comments"
203
204     assert_select "osm[version='#{API_VERSION}'][generator='OpenStreetMap server']", 1
205     assert_select "osm>changeset[id='#{changeset_id}']", 1
206     assert_select "osm>changeset>discussion", 1
207     assert_select "osm>changeset>discussion>comment", 3
208   end
209
210   ##
211   # check that a changeset that doesn't exist returns an appropriate message
212   def test_read_not_found
213     [0, -32, 233455644, "afg", "213"].each do |id|
214       begin
215         get :read, :id => id
216         assert_response :not_found, "should get a not found"
217       rescue ActionController::UrlGenerationError => ex
218         assert_match /No route matches/, ex.to_s
219       end
220     end
221   end
222
223   ##
224   # test that the user who opened a change can close it
225   def test_close
226     ## Try without authentication
227     put :close, :id => changesets(:public_user_first_change).id
228     assert_response :unauthorized
229
230     ## Try using the non-public user
231     basic_authorization changesets(:normal_user_first_change).user.email, "test"
232     put :close, :id => changesets(:normal_user_first_change).id
233     assert_require_public_data
234
235     ## The try with the public user
236     basic_authorization changesets(:public_user_first_change).user.email, "test"
237
238     cs_id = changesets(:public_user_first_change).id
239     put :close, :id => cs_id
240     assert_response :success
241
242     # test that it really is closed now
243     cs = Changeset.find(cs_id)
244     assert(!cs.is_open?,
245            "changeset should be closed now (#{cs.closed_at} > #{Time.now.getutc}.")
246   end
247
248   ##
249   # test that a different user can't close another user's changeset
250   def test_close_invalid
251     basic_authorization create(:user).email, "test"
252
253     put :close, :id => changesets(:normal_user_first_change).id
254     assert_response :conflict
255     assert_equal "The user doesn't own that changeset", @response.body
256   end
257
258   ##
259   # test that you can't close using another method
260   def test_close_method_invalid
261     basic_authorization changesets(:public_user_first_change).user.email, "test"
262
263     cs_id = changesets(:public_user_first_change).id
264     get :close, :id => cs_id
265     assert_response :method_not_allowed
266
267     post :close, :id => cs_id
268     assert_response :method_not_allowed
269   end
270
271   ##
272   # check that you can't close a changeset that isn't found
273   def test_close_not_found
274     cs_ids = [0, -132, "123"]
275
276     # First try to do it with no auth
277     cs_ids.each do |id|
278       begin
279         put :close, :id => id
280         assert_response :unauthorized, "Shouldn't be able close the non-existant changeset #{id}, when not authorized"
281       rescue ActionController::UrlGenerationError => ex
282         assert_match /No route matches/, ex.to_s
283       end
284     end
285
286     # Now try with auth
287     basic_authorization create(:user).email, "test"
288     cs_ids.each do |id|
289       begin
290         put :close, :id => id
291         assert_response :not_found, "The changeset #{id} doesn't exist, so can't be closed"
292       rescue ActionController::UrlGenerationError => ex
293         assert_match /No route matches/, ex.to_s
294       end
295     end
296   end
297
298   ##
299   # upload something simple, but valid and check that it can
300   # be read back ok
301   # Also try without auth and another user.
302   def test_upload_simple_valid
303     ## Try with no auth
304     changeset_id = changesets(:public_user_first_change).id
305
306     # simple diff to change a node, way and relation by removing
307     # their tags
308     diff = <<EOF
309 <osmChange>
310  <modify>
311   <node id='1' lon='0' lat='0' changeset='#{changeset_id}' version='1'/>
312   <way id='1' changeset='#{changeset_id}' version='1'>
313    <nd ref='3'/>
314   </way>
315  </modify>
316  <modify>
317   <relation id='1' changeset='#{changeset_id}' version='1'>
318    <member type='way' role='some' ref='3'/>
319    <member type='node' role='some' ref='5'/>
320    <member type='relation' role='some' ref='3'/>
321   </relation>
322  </modify>
323 </osmChange>
324 EOF
325
326     # upload it
327     content diff
328     post :upload, :id => changeset_id
329     assert_response :unauthorized,
330                     "shouldnn't be able to upload a simple valid diff to changeset: #{@response.body}"
331
332     ## Now try with a private user
333     basic_authorization changesets(:normal_user_first_change).user.email, "test"
334     changeset_id = changesets(:normal_user_first_change).id
335
336     # simple diff to change a node, way and relation by removing
337     # their tags
338     diff = <<EOF
339 <osmChange>
340  <modify>
341   <node id='1' lon='0' lat='0' changeset='#{changeset_id}' version='1'/>
342   <way id='1' changeset='#{changeset_id}' version='1'>
343    <nd ref='3'/>
344   </way>
345  </modify>
346  <modify>
347   <relation id='1' changeset='#{changeset_id}' version='1'>
348    <member type='way' role='some' ref='3'/>
349    <member type='node' role='some' ref='5'/>
350    <member type='relation' role='some' ref='3'/>
351   </relation>
352  </modify>
353 </osmChange>
354 EOF
355
356     # upload it
357     content diff
358     post :upload, :id => changeset_id
359     assert_response :forbidden,
360                     "can't upload a simple valid diff to changeset: #{@response.body}"
361
362     ## Now try with the public user
363     basic_authorization changesets(:public_user_first_change).user.email, "test"
364     changeset_id = changesets(:public_user_first_change).id
365
366     # simple diff to change a node, way and relation by removing
367     # their tags
368     diff = <<EOF
369 <osmChange>
370  <modify>
371   <node id='1' lon='0' lat='0' changeset='#{changeset_id}' version='1'/>
372   <way id='1' changeset='#{changeset_id}' version='1'>
373    <nd ref='3'/>
374   </way>
375  </modify>
376  <modify>
377   <relation id='1' changeset='#{changeset_id}' version='1'>
378    <member type='way' role='some' ref='3'/>
379    <member type='node' role='some' ref='5'/>
380    <member type='relation' role='some' ref='3'/>
381   </relation>
382  </modify>
383 </osmChange>
384 EOF
385
386     # upload it
387     content diff
388     post :upload, :id => changeset_id
389     assert_response :success,
390                     "can't upload a simple valid diff to changeset: #{@response.body}"
391
392     # check that the changes made it into the database
393     assert_equal 0, Node.find(1).tags.size, "node 1 should now have no tags"
394     assert_equal 0, Way.find(1).tags.size, "way 1 should now have no tags"
395     assert_equal 0, Relation.find(1).tags.size, "relation 1 should now have no tags"
396   end
397
398   ##
399   # upload something which creates new objects using placeholders
400   def test_upload_create_valid
401     basic_authorization changesets(:public_user_first_change).user.email, "test"
402     cs_id = changesets(:public_user_first_change).id
403
404     # simple diff to create a node way and relation using placeholders
405     diff = <<EOF
406 <osmChange>
407  <create>
408   <node id='-1' lon='0' lat='0' changeset='#{cs_id}'>
409    <tag k='foo' v='bar'/>
410    <tag k='baz' v='bat'/>
411   </node>
412   <way id='-1' changeset='#{cs_id}'>
413    <nd ref='3'/>
414   </way>
415  </create>
416  <create>
417   <relation id='-1' changeset='#{cs_id}'>
418    <member type='way' role='some' ref='3'/>
419    <member type='node' role='some' ref='5'/>
420    <member type='relation' role='some' ref='3'/>
421   </relation>
422  </create>
423 </osmChange>
424 EOF
425
426     # upload it
427     content diff
428     post :upload, :id => cs_id
429     assert_response :success,
430                     "can't upload a simple valid creation to changeset: #{@response.body}"
431
432     # check the returned payload
433     assert_select "diffResult[version='#{API_VERSION}'][generator='OpenStreetMap server']", 1
434     assert_select "diffResult>node", 1
435     assert_select "diffResult>way", 1
436     assert_select "diffResult>relation", 1
437
438     # inspect the response to find out what the new element IDs are
439     doc = XML::Parser.string(@response.body).parse
440     new_node_id = doc.find("//diffResult/node").first["new_id"].to_i
441     new_way_id = doc.find("//diffResult/way").first["new_id"].to_i
442     new_rel_id = doc.find("//diffResult/relation").first["new_id"].to_i
443
444     # check the old IDs are all present and negative one
445     assert_equal -1, doc.find("//diffResult/node").first["old_id"].to_i
446     assert_equal -1, doc.find("//diffResult/way").first["old_id"].to_i
447     assert_equal -1, doc.find("//diffResult/relation").first["old_id"].to_i
448
449     # check the versions are present and equal one
450     assert_equal 1, doc.find("//diffResult/node").first["new_version"].to_i
451     assert_equal 1, doc.find("//diffResult/way").first["new_version"].to_i
452     assert_equal 1, doc.find("//diffResult/relation").first["new_version"].to_i
453
454     # check that the changes made it into the database
455     assert_equal 2, Node.find(new_node_id).tags.size, "new node should have two tags"
456     assert_equal 0, Way.find(new_way_id).tags.size, "new way should have no tags"
457     assert_equal 0, Relation.find(new_rel_id).tags.size, "new relation should have no tags"
458   end
459
460   ##
461   # test a complex delete where we delete elements which rely on eachother
462   # in the same transaction.
463   def test_upload_delete
464     basic_authorization changesets(:public_user_first_change).user.display_name, "test"
465
466     diff = XML::Document.new
467     diff.root = XML::Node.new "osmChange"
468     delete = XML::Node.new "delete"
469     diff.root << delete
470     delete << current_relations(:visible_relation).to_xml_node
471     delete << current_relations(:used_relation).to_xml_node
472     delete << current_ways(:used_way).to_xml_node
473     delete << current_nodes(:node_used_by_relationship).to_xml_node
474
475     # update the changeset to one that this user owns
476     changeset_id = changesets(:public_user_first_change).id
477     %w(node way relation).each do |type|
478       delete.find("//osmChange/delete/#{type}").each do |n|
479         n["changeset"] = changeset_id.to_s
480       end
481     end
482
483     # upload it
484     content diff
485     post :upload, :id => changeset_id
486     assert_response :success,
487                     "can't upload a deletion diff to changeset: #{@response.body}"
488
489     # check the response is well-formed
490     assert_select "diffResult>node", 1
491     assert_select "diffResult>way", 1
492     assert_select "diffResult>relation", 2
493
494     # check that everything was deleted
495     assert_equal false, Node.find(current_nodes(:node_used_by_relationship).id).visible
496     assert_equal false, Way.find(current_ways(:used_way).id).visible
497     assert_equal false, Relation.find(current_relations(:visible_relation).id).visible
498     assert_equal false, Relation.find(current_relations(:used_relation).id).visible
499   end
500
501   ##
502   # test uploading a delete with no lat/lon, as they are optional in
503   # the osmChange spec.
504   def test_upload_nolatlon_delete
505     basic_authorization changesets(:public_user_first_change).user.display_name, "test"
506
507     node = current_nodes(:public_visible_node)
508     cs = changesets(:public_user_first_change)
509     diff = "<osmChange><delete><node id='#{node.id}' version='#{node.version}' changeset='#{cs.id}'/></delete></osmChange>"
510
511     # upload it
512     content diff
513     post :upload, :id => cs.id
514     assert_response :success,
515                     "can't upload a deletion diff to changeset: #{@response.body}"
516
517     # check the response is well-formed
518     assert_select "diffResult>node", 1
519
520     # check that everything was deleted
521     assert_equal false, Node.find(node.id).visible
522   end
523
524   def test_repeated_changeset_create
525     30.times do
526       basic_authorization create(:user).email, "test"
527
528       # create a temporary changeset
529       content "<osm><changeset>" +
530               "<tag k='created_by' v='osm test suite checking changesets'/>" +
531               "</changeset></osm>"
532       assert_difference "Changeset.count", 1 do
533         put :create
534       end
535       assert_response :success
536     end
537   end
538
539   def test_upload_large_changeset
540     basic_authorization create(:user).email, "test"
541
542     # create a changeset
543     content "<osm><changeset/></osm>"
544     put :create
545     assert_response :success, "Should be able to create a changeset: #{@response.body}"
546     changeset_id = @response.body.to_i
547
548     # upload some widely-spaced nodes, spiralling positive and negative to cause
549     # largest bbox over-expansion possible.
550     diff = <<EOF
551 <osmChange>
552  <create>
553   <node id='-1' lon='-20' lat='-10' changeset='#{changeset_id}'/>
554   <node id='-10' lon='20'  lat='10' changeset='#{changeset_id}'/>
555   <node id='-2' lon='-40' lat='-20' changeset='#{changeset_id}'/>
556   <node id='-11' lon='40'  lat='20' changeset='#{changeset_id}'/>
557   <node id='-3' lon='-60' lat='-30' changeset='#{changeset_id}'/>
558   <node id='-12' lon='60'  lat='30' changeset='#{changeset_id}'/>
559   <node id='-4' lon='-80' lat='-40' changeset='#{changeset_id}'/>
560   <node id='-13' lon='80'  lat='40' changeset='#{changeset_id}'/>
561   <node id='-5' lon='-100' lat='-50' changeset='#{changeset_id}'/>
562   <node id='-14' lon='100'  lat='50' changeset='#{changeset_id}'/>
563   <node id='-6' lon='-120' lat='-60' changeset='#{changeset_id}'/>
564   <node id='-15' lon='120'  lat='60' changeset='#{changeset_id}'/>
565   <node id='-7' lon='-140' lat='-70' changeset='#{changeset_id}'/>
566   <node id='-16' lon='140'  lat='70' changeset='#{changeset_id}'/>
567   <node id='-8' lon='-160' lat='-80' changeset='#{changeset_id}'/>
568   <node id='-17' lon='160'  lat='80' changeset='#{changeset_id}'/>
569   <node id='-9' lon='-179.9' lat='-89.9' changeset='#{changeset_id}'/>
570   <node id='-18' lon='179.9'  lat='89.9' changeset='#{changeset_id}'/>
571  </create>
572 </osmChange>
573 EOF
574
575     # upload it, which used to cause an error like "PGError: ERROR:
576     # integer out of range" (bug #2152). but shouldn't any more.
577     content diff
578     post :upload, :id => changeset_id
579     assert_response :success,
580                     "can't upload a spatially-large diff to changeset: #{@response.body}"
581
582     # check that the changeset bbox is within bounds
583     cs = Changeset.find(changeset_id)
584     assert cs.min_lon >= -180 * GeoRecord::SCALE, "Minimum longitude (#{cs.min_lon / GeoRecord::SCALE}) should be >= -180 to be valid."
585     assert cs.max_lon <= 180 * GeoRecord::SCALE, "Maximum longitude (#{cs.max_lon / GeoRecord::SCALE}) should be <= 180 to be valid."
586     assert cs.min_lat >= -90 * GeoRecord::SCALE, "Minimum latitude (#{cs.min_lat / GeoRecord::SCALE}) should be >= -90 to be valid."
587     assert cs.max_lat >= 90 * GeoRecord::SCALE, "Maximum latitude (#{cs.max_lat / GeoRecord::SCALE}) should be <= 90 to be valid."
588   end
589
590   ##
591   # test that deleting stuff in a transaction doesn't bypass the checks
592   # to ensure that used elements are not deleted.
593   def test_upload_delete_invalid
594     basic_authorization changesets(:public_user_first_change).user.email, "test"
595     cs = changesets(:public_user_first_change)
596
597     diff = XML::Document.new
598     diff.root = XML::Node.new "osmChange"
599     delete = XML::Node.new "delete"
600     diff.root << delete
601     delete << current_relations(:public_visible_relation).to_xml_node
602     delete << current_ways(:used_way).to_xml_node
603     delete << current_nodes(:node_used_by_relationship).to_xml_node
604
605     # upload it
606     content diff
607     post :upload, :id => cs.id
608     assert_response :precondition_failed,
609                     "shouldn't be able to upload a invalid deletion diff: #{@response.body}"
610     assert_equal "Precondition failed: Way 3 is still used by relations 1.", @response.body
611
612     # check that nothing was, in fact, deleted
613     assert_equal true, Node.find(current_nodes(:node_used_by_relationship).id).visible
614     assert_equal true, Way.find(current_ways(:used_way).id).visible
615     assert_equal true, Relation.find(current_relations(:visible_relation).id).visible
616   end
617
618   ##
619   # test that a conditional delete of an in use object works.
620   def test_upload_delete_if_unused
621     basic_authorization changesets(:public_user_first_change).user.email, "test"
622     cs = changesets(:public_user_first_change)
623
624     diff = XML::Document.new
625     diff.root = XML::Node.new "osmChange"
626     delete = XML::Node.new "delete"
627     diff.root << delete
628     delete["if-unused"] = ""
629     delete << current_relations(:public_used_relation).to_xml_node
630     delete << current_ways(:used_way).to_xml_node
631     delete << current_nodes(:node_used_by_relationship).to_xml_node
632
633     # upload it
634     content diff
635     post :upload, :id => cs.id
636     assert_response :success,
637                     "can't do a conditional delete of in use objects: #{@response.body}"
638
639     # check the returned payload
640     assert_select "diffResult[version='#{API_VERSION}'][generator='OpenStreetMap server']", 1
641     assert_select "diffResult>node", 1
642     assert_select "diffResult>way", 1
643     assert_select "diffResult>relation", 1
644
645     # parse the response
646     doc = XML::Parser.string(@response.body).parse
647
648     # check the old IDs are all present and what we expect
649     assert_equal current_nodes(:node_used_by_relationship).id, doc.find("//diffResult/node").first["old_id"].to_i
650     assert_equal current_ways(:used_way).id, doc.find("//diffResult/way").first["old_id"].to_i
651     assert_equal current_relations(:public_used_relation).id, doc.find("//diffResult/relation").first["old_id"].to_i
652
653     # check the new IDs are all present and unchanged
654     assert_equal current_nodes(:node_used_by_relationship).id, doc.find("//diffResult/node").first["new_id"].to_i
655     assert_equal current_ways(:used_way).id, doc.find("//diffResult/way").first["new_id"].to_i
656     assert_equal current_relations(:public_used_relation).id, doc.find("//diffResult/relation").first["new_id"].to_i
657
658     # check the new versions are all present and unchanged
659     assert_equal current_nodes(:node_used_by_relationship).version, doc.find("//diffResult/node").first["new_version"].to_i
660     assert_equal current_ways(:used_way).version, doc.find("//diffResult/way").first["new_version"].to_i
661     assert_equal current_relations(:public_used_relation).version, doc.find("//diffResult/relation").first["new_version"].to_i
662
663     # check that nothing was, in fact, deleted
664     assert_equal true, Node.find(current_nodes(:node_used_by_relationship).id).visible
665     assert_equal true, Way.find(current_ways(:used_way).id).visible
666     assert_equal true, Relation.find(current_relations(:public_used_relation).id).visible
667   end
668
669   ##
670   # upload an element with a really long tag value
671   def test_upload_invalid_too_long_tag
672     basic_authorization changesets(:public_user_first_change).user.email, "test"
673     cs_id = changesets(:public_user_first_change).id
674
675     # simple diff to create a node way and relation using placeholders
676     diff = <<EOF
677 <osmChange>
678  <create>
679   <node id='-1' lon='0' lat='0' changeset='#{cs_id}'>
680    <tag k='foo' v='#{'x' * 256}'/>
681   </node>
682  </create>
683 </osmChange>
684 EOF
685
686     # upload it
687     content diff
688     post :upload, :id => cs_id
689     assert_response :bad_request,
690                     "shoudln't be able to upload too long a tag to changeset: #{@response.body}"
691   end
692
693   ##
694   # upload something which creates new objects and inserts them into
695   # existing containers using placeholders.
696   def test_upload_complex
697     basic_authorization changesets(:public_user_first_change).user.email, "test"
698     cs_id = changesets(:public_user_first_change).id
699
700     # simple diff to create a node way and relation using placeholders
701     diff = <<EOF
702 <osmChange>
703  <create>
704   <node id='-1' lon='0' lat='0' changeset='#{cs_id}'>
705    <tag k='foo' v='bar'/>
706    <tag k='baz' v='bat'/>
707   </node>
708  </create>
709  <modify>
710   <way id='1' changeset='#{cs_id}' version='1'>
711    <nd ref='-1'/>
712    <nd ref='3'/>
713   </way>
714   <relation id='1' changeset='#{cs_id}' version='1'>
715    <member type='way' role='some' ref='3'/>
716    <member type='node' role='some' ref='-1'/>
717    <member type='relation' role='some' ref='3'/>
718   </relation>
719  </modify>
720 </osmChange>
721 EOF
722
723     # upload it
724     content diff
725     post :upload, :id => cs_id
726     assert_response :success,
727                     "can't upload a complex diff to changeset: #{@response.body}"
728
729     # check the returned payload
730     assert_select "diffResult[version='#{API_VERSION}'][generator='#{GENERATOR}']", 1
731     assert_select "diffResult>node", 1
732     assert_select "diffResult>way", 1
733     assert_select "diffResult>relation", 1
734
735     # inspect the response to find out what the new element IDs are
736     doc = XML::Parser.string(@response.body).parse
737     new_node_id = doc.find("//diffResult/node").first["new_id"].to_i
738
739     # check that the changes made it into the database
740     assert_equal 2, Node.find(new_node_id).tags.size, "new node should have two tags"
741     assert_equal [new_node_id, 3], Way.find(1).nds, "way nodes should match"
742     Relation.find(1).members.each do |type, id, _role|
743       if type == "node"
744         assert_equal new_node_id, id, "relation should contain new node"
745       end
746     end
747   end
748
749   ##
750   # create a diff which references several changesets, which should cause
751   # a rollback and none of the diff gets committed
752   def test_upload_invalid_changesets
753     basic_authorization changesets(:public_user_first_change).user.email, "test"
754     cs_id = changesets(:public_user_first_change).id
755
756     # simple diff to create a node way and relation using placeholders
757     diff = <<EOF
758 <osmChange>
759  <modify>
760   <node id='1' lon='0' lat='0' changeset='#{cs_id}' version='1'/>
761   <way id='1' changeset='#{cs_id}' version='1'>
762    <nd ref='3'/>
763   </way>
764  </modify>
765  <modify>
766   <relation id='1' changeset='#{cs_id}' version='1'>
767    <member type='way' role='some' ref='3'/>
768    <member type='node' role='some' ref='5'/>
769    <member type='relation' role='some' ref='3'/>
770   </relation>
771  </modify>
772  <create>
773   <node id='-1' lon='0' lat='0' changeset='4'>
774    <tag k='foo' v='bar'/>
775    <tag k='baz' v='bat'/>
776   </node>
777  </create>
778 </osmChange>
779 EOF
780     # cache the objects before uploading them
781     node = current_nodes(:visible_node)
782     way = current_ways(:visible_way)
783     rel = current_relations(:visible_relation)
784
785     # upload it
786     content diff
787     post :upload, :id => cs_id
788     assert_response :conflict,
789                     "uploading a diff with multiple changsets should have failed"
790
791     # check that objects are unmodified
792     assert_nodes_are_equal(node, Node.find(1))
793     assert_ways_are_equal(way, Way.find(1))
794     assert_relations_are_equal(rel, Relation.find(1))
795   end
796
797   ##
798   # upload multiple versions of the same element in the same diff.
799   def test_upload_multiple_valid
800     basic_authorization changesets(:public_user_first_change).user.email, "test"
801     cs_id = changesets(:public_user_first_change).id
802
803     # change the location of a node multiple times, each time referencing
804     # the last version. doesn't this depend on version numbers being
805     # sequential?
806     diff = <<EOF
807 <osmChange>
808  <modify>
809   <node id='1' lon='0' lat='0' changeset='#{cs_id}' version='1'/>
810   <node id='1' lon='1' lat='0' changeset='#{cs_id}' version='2'/>
811   <node id='1' lon='1' lat='1' changeset='#{cs_id}' version='3'/>
812   <node id='1' lon='1' lat='2' changeset='#{cs_id}' version='4'/>
813   <node id='1' lon='2' lat='2' changeset='#{cs_id}' version='5'/>
814   <node id='1' lon='3' lat='2' changeset='#{cs_id}' version='6'/>
815   <node id='1' lon='3' lat='3' changeset='#{cs_id}' version='7'/>
816   <node id='1' lon='9' lat='9' changeset='#{cs_id}' version='8'/>
817  </modify>
818 </osmChange>
819 EOF
820
821     # upload it
822     content diff
823     post :upload, :id => cs_id
824     assert_response :success,
825                     "can't upload multiple versions of an element in a diff: #{@response.body}"
826
827     # check the response is well-formed. its counter-intuitive, but the
828     # API will return multiple elements with the same ID and different
829     # version numbers for each change we made.
830     assert_select "diffResult>node", 8
831   end
832
833   ##
834   # upload multiple versions of the same element in the same diff, but
835   # keep the version numbers the same.
836   def test_upload_multiple_duplicate
837     basic_authorization changesets(:public_user_first_change).user.email, "test"
838     cs_id = changesets(:public_user_first_change).id
839
840     diff = <<EOF
841 <osmChange>
842  <modify>
843   <node id='1' lon='0' lat='0' changeset='#{cs_id}' version='1'/>
844   <node id='1' lon='1' lat='1' changeset='#{cs_id}' version='1'/>
845  </modify>
846 </osmChange>
847 EOF
848
849     # upload it
850     content diff
851     post :upload, :id => cs_id
852     assert_response :conflict,
853                     "shouldn't be able to upload the same element twice in a diff: #{@response.body}"
854   end
855
856   ##
857   # try to upload some elements without specifying the version
858   def test_upload_missing_version
859     basic_authorization changesets(:public_user_first_change).user.email, "test"
860     cs_id = changesets(:public_user_first_change).id
861
862     diff = <<EOF
863 <osmChange>
864  <modify>
865  <node id='1' lon='1' lat='1' changeset='cs_id'/>
866  </modify>
867 </osmChange>
868 EOF
869
870     # upload it
871     content diff
872     post :upload, :id => cs_id
873     assert_response :bad_request,
874                     "shouldn't be able to upload an element without version: #{@response.body}"
875   end
876
877   ##
878   # try to upload with commands other than create, modify, or delete
879   def test_action_upload_invalid
880     basic_authorization changesets(:public_user_first_change).user.email, "test"
881     cs_id = changesets(:public_user_first_change).id
882
883     diff = <<EOF
884 <osmChange>
885   <ping>
886    <node id='1' lon='1' lat='1' changeset='#{cs_id}' />
887   </ping>
888 </osmChange>
889 EOF
890     content diff
891     post :upload, :id => cs_id
892     assert_response :bad_request, "Shouldn't be able to upload a diff with the action ping"
893     assert_equal @response.body, "Unknown action ping, choices are create, modify, delete"
894   end
895
896   ##
897   # upload a valid changeset which has a mixture of whitespace
898   # to check a bug reported by ivansanchez (#1565).
899   def test_upload_whitespace_valid
900     basic_authorization changesets(:public_user_first_change).user.email, "test"
901     changeset_id = changesets(:public_user_first_change).id
902
903     diff = <<EOF
904 <osmChange>
905  <modify><node id='1' lon='0' lat='0' changeset='#{changeset_id}'
906   version='1'></node>
907   <node id='1' lon='1' lat='1' changeset='#{changeset_id}' version='2'><tag k='k' v='v'/></node></modify>
908  <modify>
909  <relation id='1' changeset='#{changeset_id}' version='1'><member
910    type='way' role='some' ref='3'/><member
911     type='node' role='some' ref='5'/>
912    <member type='relation' role='some' ref='3'/>
913   </relation>
914  </modify></osmChange>
915 EOF
916
917     # upload it
918     content diff
919     post :upload, :id => changeset_id
920     assert_response :success,
921                     "can't upload a valid diff with whitespace variations to changeset: #{@response.body}"
922
923     # check the response is well-formed
924     assert_select "diffResult>node", 2
925     assert_select "diffResult>relation", 1
926
927     # check that the changes made it into the database
928     assert_equal 1, Node.find(1).tags.size, "node 1 should now have one tag"
929     assert_equal 0, Relation.find(1).tags.size, "relation 1 should now have no tags"
930   end
931
932   ##
933   # upload a valid changeset which has a mixture of whitespace
934   # to check a bug reported by ivansanchez.
935   def test_upload_reuse_placeholder_valid
936     basic_authorization changesets(:public_user_first_change).user.email, "test"
937     changeset_id = changesets(:public_user_first_change).id
938
939     diff = <<EOF
940 <osmChange>
941  <create>
942   <node id='-1' lon='0' lat='0' changeset='#{changeset_id}'>
943    <tag k="foo" v="bar"/>
944   </node>
945  </create>
946  <modify>
947   <node id='-1' lon='1' lat='1' changeset='#{changeset_id}' version='1'/>
948  </modify>
949  <delete>
950   <node id='-1' lon='2' lat='2' changeset='#{changeset_id}' version='2'/>
951  </delete>
952 </osmChange>
953 EOF
954
955     # upload it
956     content diff
957     post :upload, :id => changeset_id
958     assert_response :success,
959                     "can't upload a valid diff with re-used placeholders to changeset: #{@response.body}"
960
961     # check the response is well-formed
962     assert_select "diffResult>node", 3
963     assert_select "diffResult>node[old_id='-1']", 3
964   end
965
966   ##
967   # test what happens if a diff upload re-uses placeholder IDs in an
968   # illegal way.
969   def test_upload_placeholder_invalid
970     basic_authorization changesets(:public_user_first_change).user.email, "test"
971     changeset_id = changesets(:public_user_first_change).id
972
973     diff = <<EOF
974 <osmChange>
975  <create>
976   <node id='-1' lon='0' lat='0' changeset='#{changeset_id}' version='1'/>
977   <node id='-1' lon='1' lat='1' changeset='#{changeset_id}' version='1'/>
978   <node id='-1' lon='2' lat='2' changeset='#{changeset_id}' version='2'/>
979  </create>
980 </osmChange>
981 EOF
982
983     # upload it
984     content diff
985     post :upload, :id => changeset_id
986     assert_response :bad_request,
987                     "shouldn't be able to re-use placeholder IDs"
988   end
989
990   ##
991   # test that uploading a way referencing invalid placeholders gives a
992   # proper error, not a 500.
993   def test_upload_placeholder_invalid_way
994     basic_authorization changesets(:public_user_first_change).user.email, "test"
995     changeset_id = changesets(:public_user_first_change).id
996
997     diff = <<EOF
998 <osmChange>
999  <create>
1000   <node id="-1" lon="0" lat="0" changeset="#{changeset_id}" version="1"/>
1001   <node id="-2" lon="1" lat="1" changeset="#{changeset_id}" version="1"/>
1002   <node id="-3" lon="2" lat="2" changeset="#{changeset_id}" version="1"/>
1003   <way id="-1" changeset="#{changeset_id}" version="1">
1004    <nd ref="-1"/>
1005    <nd ref="-2"/>
1006    <nd ref="-3"/>
1007    <nd ref="-4"/>
1008   </way>
1009  </create>
1010 </osmChange>
1011 EOF
1012
1013     # upload it
1014     content diff
1015     post :upload, :id => changeset_id
1016     assert_response :bad_request,
1017                     "shouldn't be able to use invalid placeholder IDs"
1018     assert_equal "Placeholder node not found for reference -4 in way -1", @response.body
1019
1020     # the same again, but this time use an existing way
1021     diff = <<EOF
1022 <osmChange>
1023  <create>
1024   <node id="-1" lon="0" lat="0" changeset="#{changeset_id}" version="1"/>
1025   <node id="-2" lon="1" lat="1" changeset="#{changeset_id}" version="1"/>
1026   <node id="-3" lon="2" lat="2" changeset="#{changeset_id}" version="1"/>
1027   <way id="1" changeset="#{changeset_id}" version="1">
1028    <nd ref="-1"/>
1029    <nd ref="-2"/>
1030    <nd ref="-3"/>
1031    <nd ref="-4"/>
1032   </way>
1033  </create>
1034 </osmChange>
1035 EOF
1036
1037     # upload it
1038     content diff
1039     post :upload, :id => changeset_id
1040     assert_response :bad_request,
1041                     "shouldn't be able to use invalid placeholder IDs"
1042     assert_equal "Placeholder node not found for reference -4 in way 1", @response.body
1043   end
1044
1045   ##
1046   # test that uploading a relation referencing invalid placeholders gives a
1047   # proper error, not a 500.
1048   def test_upload_placeholder_invalid_relation
1049     basic_authorization changesets(:public_user_first_change).user.email, "test"
1050     changeset_id = changesets(:public_user_first_change).id
1051
1052     diff = <<EOF
1053 <osmChange>
1054  <create>
1055   <node id="-1" lon="0" lat="0" changeset="#{changeset_id}" version="1"/>
1056   <node id="-2" lon="1" lat="1" changeset="#{changeset_id}" version="1"/>
1057   <node id="-3" lon="2" lat="2" changeset="#{changeset_id}" version="1"/>
1058   <relation id="-1" changeset="#{changeset_id}" version="1">
1059    <member type="node" role="foo" ref="-1"/>
1060    <member type="node" role="foo" ref="-2"/>
1061    <member type="node" role="foo" ref="-3"/>
1062    <member type="node" role="foo" ref="-4"/>
1063   </relation>
1064  </create>
1065 </osmChange>
1066 EOF
1067
1068     # upload it
1069     content diff
1070     post :upload, :id => changeset_id
1071     assert_response :bad_request,
1072                     "shouldn't be able to use invalid placeholder IDs"
1073     assert_equal "Placeholder Node not found for reference -4 in relation -1.", @response.body
1074
1075     # the same again, but this time use an existing way
1076     diff = <<EOF
1077 <osmChange>
1078  <create>
1079   <node id="-1" lon="0" lat="0" changeset="#{changeset_id}" version="1"/>
1080   <node id="-2" lon="1" lat="1" changeset="#{changeset_id}" version="1"/>
1081   <node id="-3" lon="2" lat="2" changeset="#{changeset_id}" version="1"/>
1082   <relation id="1" changeset="#{changeset_id}" version="1">
1083    <member type="node" role="foo" ref="-1"/>
1084    <member type="node" role="foo" ref="-2"/>
1085    <member type="node" role="foo" ref="-3"/>
1086    <member type="way" role="bar" ref="-1"/>
1087   </relation>
1088  </create>
1089 </osmChange>
1090 EOF
1091
1092     # upload it
1093     content diff
1094     post :upload, :id => changeset_id
1095     assert_response :bad_request,
1096                     "shouldn't be able to use invalid placeholder IDs"
1097     assert_equal "Placeholder Way not found for reference -1 in relation 1.", @response.body
1098   end
1099
1100   ##
1101   # test what happens if a diff is uploaded containing only a node
1102   # move.
1103   def test_upload_node_move
1104     basic_authorization create(:user).email, "test"
1105
1106     content "<osm><changeset>" +
1107             "<tag k='created_by' v='osm test suite checking changesets'/>" +
1108             "</changeset></osm>"
1109     put :create
1110     assert_response :success
1111     changeset_id = @response.body.to_i
1112
1113     old_node = current_nodes(:visible_node)
1114
1115     diff = XML::Document.new
1116     diff.root = XML::Node.new "osmChange"
1117     modify = XML::Node.new "modify"
1118     xml_old_node = old_node.to_xml_node
1119     xml_old_node["lat"] = 2.0.to_s
1120     xml_old_node["lon"] = 2.0.to_s
1121     xml_old_node["changeset"] = changeset_id.to_s
1122     modify << xml_old_node
1123     diff.root << modify
1124
1125     # upload it
1126     content diff
1127     post :upload, :id => changeset_id
1128     assert_response :success,
1129                     "diff should have uploaded OK"
1130
1131     # check the bbox
1132     changeset = Changeset.find(changeset_id)
1133     assert_equal 1 * GeoRecord::SCALE, changeset.min_lon, "min_lon should be 1 degree"
1134     assert_equal 2 * GeoRecord::SCALE, changeset.max_lon, "max_lon should be 2 degrees"
1135     assert_equal 1 * GeoRecord::SCALE, changeset.min_lat, "min_lat should be 1 degree"
1136     assert_equal 2 * GeoRecord::SCALE, changeset.max_lat, "max_lat should be 2 degrees"
1137   end
1138
1139   ##
1140   # test what happens if a diff is uploaded adding a node to a way.
1141   def test_upload_way_extend
1142     basic_authorization create(:user).email, "test"
1143
1144     content "<osm><changeset>" +
1145             "<tag k='created_by' v='osm test suite checking changesets'/>" +
1146             "</changeset></osm>"
1147     put :create
1148     assert_response :success
1149     changeset_id = @response.body.to_i
1150
1151     old_way = current_ways(:visible_way)
1152
1153     diff = XML::Document.new
1154     diff.root = XML::Node.new "osmChange"
1155     modify = XML::Node.new "modify"
1156     xml_old_way = old_way.to_xml_node
1157     nd_ref = XML::Node.new "nd"
1158     nd_ref["ref"] = current_nodes(:visible_node).id.to_s
1159     xml_old_way << nd_ref
1160     xml_old_way["changeset"] = changeset_id.to_s
1161     modify << xml_old_way
1162     diff.root << modify
1163
1164     # upload it
1165     content diff
1166     post :upload, :id => changeset_id
1167     assert_response :success,
1168                     "diff should have uploaded OK"
1169
1170     # check the bbox
1171     changeset = Changeset.find(changeset_id)
1172     assert_equal 1 * GeoRecord::SCALE, changeset.min_lon, "min_lon should be 1 degree"
1173     assert_equal 3 * GeoRecord::SCALE, changeset.max_lon, "max_lon should be 3 degrees"
1174     assert_equal 1 * GeoRecord::SCALE, changeset.min_lat, "min_lat should be 1 degree"
1175     assert_equal 3 * GeoRecord::SCALE, changeset.max_lat, "max_lat should be 3 degrees"
1176   end
1177
1178   ##
1179   # test for more issues in #1568
1180   def test_upload_empty_invalid
1181     basic_authorization changesets(:public_user_first_change).user.email, "test"
1182
1183     ["<osmChange/>",
1184      "<osmChange></osmChange>",
1185      "<osmChange><modify/></osmChange>",
1186      "<osmChange><modify></modify></osmChange>"].each do |diff|
1187       # upload it
1188       content diff
1189       post :upload, :id => changesets(:public_user_first_change).id
1190       assert_response(:success, "should be able to upload " +
1191                       "empty changeset: " + diff)
1192     end
1193   end
1194
1195   ##
1196   # test that the X-Error-Format header works to request XML errors
1197   def test_upload_xml_errors
1198     basic_authorization changesets(:public_user_first_change).user.email, "test"
1199     cs = changesets(:public_user_first_change)
1200
1201     # try and delete a node that is in use
1202     diff = XML::Document.new
1203     diff.root = XML::Node.new "osmChange"
1204     delete = XML::Node.new "delete"
1205     diff.root << delete
1206     delete << current_nodes(:node_used_by_relationship).to_xml_node
1207
1208     # upload it
1209     content diff
1210     error_format "xml"
1211     post :upload, :id => cs.id
1212     assert_response :success,
1213                     "failed to return error in XML format"
1214
1215     # check the returned payload
1216     assert_select "osmError[version='#{API_VERSION}'][generator='OpenStreetMap server']", 1
1217     assert_select "osmError>status", 1
1218     assert_select "osmError>message", 1
1219   end
1220
1221   ##
1222   # when we make some simple changes we get the same changes back from the
1223   # diff download.
1224   def test_diff_download_simple
1225     ## First try with a non-public user, which should get a forbidden
1226     basic_authorization(create(:user, :data_public => false).email, "test")
1227
1228     # create a temporary changeset
1229     content "<osm><changeset>" +
1230             "<tag k='created_by' v='osm test suite checking changesets'/>" +
1231             "</changeset></osm>"
1232     put :create
1233     assert_response :forbidden
1234
1235     ## Now try with a normal user
1236     basic_authorization(create(:user).email, "test")
1237
1238     # create a temporary changeset
1239     content "<osm><changeset>" +
1240             "<tag k='created_by' v='osm test suite checking changesets'/>" +
1241             "</changeset></osm>"
1242     put :create
1243     assert_response :success
1244     changeset_id = @response.body.to_i
1245
1246     # add a diff to it
1247     diff = <<EOF
1248 <osmChange>
1249  <modify>
1250   <node id='1' lon='0' lat='0' changeset='#{changeset_id}' version='1'/>
1251   <node id='1' lon='1' lat='0' changeset='#{changeset_id}' version='2'/>
1252   <node id='1' lon='1' lat='1' changeset='#{changeset_id}' version='3'/>
1253   <node id='1' lon='1' lat='2' changeset='#{changeset_id}' version='4'/>
1254   <node id='1' lon='2' lat='2' changeset='#{changeset_id}' version='5'/>
1255   <node id='1' lon='3' lat='2' changeset='#{changeset_id}' version='6'/>
1256   <node id='1' lon='3' lat='3' changeset='#{changeset_id}' version='7'/>
1257   <node id='1' lon='9' lat='9' changeset='#{changeset_id}' version='8'/>
1258  </modify>
1259 </osmChange>
1260 EOF
1261
1262     # upload it
1263     content diff
1264     post :upload, :id => changeset_id
1265     assert_response :success,
1266                     "can't upload multiple versions of an element in a diff: #{@response.body}"
1267
1268     get :download, :id => changeset_id
1269     assert_response :success
1270
1271     assert_select "osmChange", 1
1272     assert_select "osmChange>modify", 8
1273     assert_select "osmChange>modify>node", 8
1274   end
1275
1276   ##
1277   # culled this from josm to ensure that nothing in the way that josm
1278   # is formatting the request is causing it to fail.
1279   #
1280   # NOTE: the error turned out to be something else completely!
1281   def test_josm_upload
1282     basic_authorization(create(:user).email, "test")
1283
1284     # create a temporary changeset
1285     content "<osm><changeset>" +
1286             "<tag k='created_by' v='osm test suite checking changesets'/>" +
1287             "</changeset></osm>"
1288     put :create
1289     assert_response :success
1290     changeset_id = @response.body.to_i
1291
1292     diff = <<OSMFILE
1293 <osmChange version="0.6" generator="JOSM">
1294 <create version="0.6" generator="JOSM">
1295   <node id='-1' visible='true' changeset='#{changeset_id}' lat='51.49619982187321' lon='-0.18722061869438314' />
1296   <node id='-2' visible='true' changeset='#{changeset_id}' lat='51.496359883909605' lon='-0.18653093576241928' />
1297   <node id='-3' visible='true' changeset='#{changeset_id}' lat='51.49598132358285' lon='-0.18719613290981638' />
1298   <node id='-4' visible='true' changeset='#{changeset_id}' lat='51.4961591711078' lon='-0.18629015888084607' />
1299   <node id='-5' visible='true' changeset='#{changeset_id}' lat='51.49582126021711' lon='-0.18708186591517145' />
1300   <node id='-6' visible='true' changeset='#{changeset_id}' lat='51.49591018437858' lon='-0.1861432441734455' />
1301   <node id='-7' visible='true' changeset='#{changeset_id}' lat='51.49560784152179' lon='-0.18694719410005425' />
1302   <node id='-8' visible='true' changeset='#{changeset_id}' lat='51.49567389979617' lon='-0.1860289771788006' />
1303   <node id='-9' visible='true' changeset='#{changeset_id}' lat='51.49543761398892' lon='-0.186820684213126' />
1304   <way id='-10' action='modiy' visible='true' changeset='#{changeset_id}'>
1305     <nd ref='-1' />
1306     <nd ref='-2' />
1307     <nd ref='-3' />
1308     <nd ref='-4' />
1309     <nd ref='-5' />
1310     <nd ref='-6' />
1311     <nd ref='-7' />
1312     <nd ref='-8' />
1313     <nd ref='-9' />
1314     <tag k='highway' v='residential' />
1315     <tag k='name' v='Foobar Street' />
1316   </way>
1317 </create>
1318 </osmChange>
1319 OSMFILE
1320
1321     # upload it
1322     content diff
1323     post :upload, :id => changeset_id
1324     assert_response :success,
1325                     "can't upload a diff from JOSM: #{@response.body}"
1326
1327     get :download, :id => changeset_id
1328     assert_response :success
1329
1330     assert_select "osmChange", 1
1331     assert_select "osmChange>create>node", 9
1332     assert_select "osmChange>create>way", 1
1333     assert_select "osmChange>create>way>nd", 9
1334     assert_select "osmChange>create>way>tag", 2
1335   end
1336
1337   ##
1338   # when we make some complex changes we get the same changes back from the
1339   # diff download.
1340   def test_diff_download_complex
1341     basic_authorization(create(:user).email, "test")
1342
1343     # create a temporary changeset
1344     content "<osm><changeset>" +
1345             "<tag k='created_by' v='osm test suite checking changesets'/>" +
1346             "</changeset></osm>"
1347     put :create
1348     assert_response :success
1349     changeset_id = @response.body.to_i
1350
1351     # add a diff to it
1352     diff = <<EOF
1353 <osmChange>
1354  <delete>
1355   <node id='1' lon='0' lat='0' changeset='#{changeset_id}' version='1'/>
1356  </delete>
1357  <create>
1358   <node id='-1' lon='9' lat='9' changeset='#{changeset_id}' version='0'/>
1359   <node id='-2' lon='8' lat='9' changeset='#{changeset_id}' version='0'/>
1360   <node id='-3' lon='7' lat='9' changeset='#{changeset_id}' version='0'/>
1361  </create>
1362  <modify>
1363   <node id='3' lon='20' lat='15' changeset='#{changeset_id}' version='1'/>
1364   <way id='1' changeset='#{changeset_id}' version='1'>
1365    <nd ref='3'/>
1366    <nd ref='-1'/>
1367    <nd ref='-2'/>
1368    <nd ref='-3'/>
1369   </way>
1370  </modify>
1371 </osmChange>
1372 EOF
1373
1374     # upload it
1375     content diff
1376     post :upload, :id => changeset_id
1377     assert_response :success,
1378                     "can't upload multiple versions of an element in a diff: #{@response.body}"
1379
1380     get :download, :id => changeset_id
1381     assert_response :success
1382
1383     assert_select "osmChange", 1
1384     assert_select "osmChange>create", 3
1385     assert_select "osmChange>delete", 1
1386     assert_select "osmChange>modify", 2
1387     assert_select "osmChange>create>node", 3
1388     assert_select "osmChange>delete>node", 1
1389     assert_select "osmChange>modify>node", 1
1390     assert_select "osmChange>modify>way", 1
1391   end
1392
1393   def test_changeset_download
1394     tag = create(:old_node_tag, :old_node => nodes(:used_node_2))
1395
1396     get :download, :id => changesets(:normal_user_first_change).id
1397
1398     assert_response :success
1399     assert_template nil
1400     # print @response.body
1401     # FIXME: needs more assert_select tests
1402     assert_select "osmChange[version='#{API_VERSION}'][generator='#{GENERATOR}']" do
1403       assert_select "create", :count => 5
1404       assert_select "create>node[id='#{nodes(:used_node_2).node_id}'][visible='#{nodes(:used_node_2).visible?}'][version='#{nodes(:used_node_2).version}']" do
1405         assert_select "tag[k='#{tag.k}'][v='#{tag.v}']"
1406       end
1407       assert_select "create>node[id='#{nodes(:visible_node).node_id}']"
1408     end
1409   end
1410
1411   ##
1412   # check that the bounding box of a changeset gets updated correctly
1413   # FIXME: This should really be moded to a integration test due to the with_controller
1414   def test_changeset_bbox
1415     basic_authorization create(:user).email, "test"
1416
1417     # create a new changeset
1418     content "<osm><changeset/></osm>"
1419     put :create
1420     assert_response :success, "Creating of changeset failed."
1421     changeset_id = @response.body.to_i
1422
1423     # add a single node to it
1424     with_controller(NodeController.new) do
1425       content "<osm><node lon='1' lat='2' changeset='#{changeset_id}'/></osm>"
1426       put :create
1427       assert_response :success, "Couldn't create node."
1428     end
1429
1430     # get the bounding box back from the changeset
1431     get :read, :id => changeset_id
1432     assert_response :success, "Couldn't read back changeset."
1433     assert_select "osm>changeset[min_lon='1.0']", 1
1434     assert_select "osm>changeset[max_lon='1.0']", 1
1435     assert_select "osm>changeset[min_lat='2.0']", 1
1436     assert_select "osm>changeset[max_lat='2.0']", 1
1437
1438     # add another node to it
1439     with_controller(NodeController.new) do
1440       content "<osm><node lon='2' lat='1' changeset='#{changeset_id}'/></osm>"
1441       put :create
1442       assert_response :success, "Couldn't create second node."
1443     end
1444
1445     # get the bounding box back from the changeset
1446     get :read, :id => changeset_id
1447     assert_response :success, "Couldn't read back changeset for the second time."
1448     assert_select "osm>changeset[min_lon='1.0']", 1
1449     assert_select "osm>changeset[max_lon='2.0']", 1
1450     assert_select "osm>changeset[min_lat='1.0']", 1
1451     assert_select "osm>changeset[max_lat='2.0']", 1
1452
1453     # add (delete) a way to it, which contains a point at (3,3)
1454     with_controller(WayController.new) do
1455       content update_changeset(current_ways(:visible_way).to_xml,
1456                                changeset_id)
1457       put :delete, :id => current_ways(:visible_way).id
1458       assert_response :success, "Couldn't delete a way."
1459     end
1460
1461     # get the bounding box back from the changeset
1462     get :read, :id => changeset_id
1463     assert_response :success, "Couldn't read back changeset for the third time."
1464     # note that the 3.1 here is because of the bbox overexpansion
1465     assert_select "osm>changeset[min_lon='1.0']", 1
1466     assert_select "osm>changeset[max_lon='3.1']", 1
1467     assert_select "osm>changeset[min_lat='1.0']", 1
1468     assert_select "osm>changeset[max_lat='3.1']", 1
1469   end
1470
1471   ##
1472   # test that the changeset :include method works as it should
1473   def test_changeset_include
1474     basic_authorization create(:user).display_name, "test"
1475
1476     # create a new changeset
1477     content "<osm><changeset/></osm>"
1478     put :create
1479     assert_response :success, "Creating of changeset failed."
1480     changeset_id = @response.body.to_i
1481
1482     # NOTE: the include method doesn't over-expand, like inserting
1483     # a real method does. this is because we expect the client to
1484     # know what it is doing!
1485     check_after_include(changeset_id, 1, 1, [1, 1, 1, 1])
1486     check_after_include(changeset_id, 3, 3, [1, 1, 3, 3])
1487     check_after_include(changeset_id, 4, 2, [1, 1, 4, 3])
1488     check_after_include(changeset_id, 2, 2, [1, 1, 4, 3])
1489     check_after_include(changeset_id, -1, -1, [-1, -1, 4, 3])
1490     check_after_include(changeset_id, -2, 5, [-2, -1, 4, 5])
1491   end
1492
1493   ##
1494   # test that a not found, wrong method with the expand bbox works as expected
1495   def test_changeset_expand_bbox_error
1496     basic_authorization create(:user).display_name, "test"
1497
1498     # create a new changeset
1499     content "<osm><changeset/></osm>"
1500     put :create
1501     assert_response :success, "Creating of changeset failed."
1502     changeset_id = @response.body.to_i
1503
1504     lon = 58.2
1505     lat = -0.45
1506
1507     # Try and put
1508     content "<osm><node lon='#{lon}' lat='#{lat}'/></osm>"
1509     put :expand_bbox, :id => changeset_id
1510     assert_response :method_not_allowed, "shouldn't be able to put a bbox expand"
1511
1512     # Try to get the update
1513     content "<osm><node lon='#{lon}' lat='#{lat}'/></osm>"
1514     get :expand_bbox, :id => changeset_id
1515     assert_response :method_not_allowed, "shouldn't be able to get a bbox expand"
1516
1517     # Try to use a hopefully missing changeset
1518     content "<osm><node lon='#{lon}' lat='#{lat}'/></osm>"
1519     post :expand_bbox, :id => changeset_id + 13245
1520     assert_response :not_found, "shouldn't be able to do a bbox expand on a nonexistant changeset"
1521   end
1522
1523   ##
1524   # test the query functionality of changesets
1525   def test_query
1526     private_user = create(:user, :data_public => false)
1527     private_user_changeset = create(:changeset, :user => private_user)
1528     private_user_closed_changeset = create(:changeset, :closed, :user => private_user)
1529     user = create(:user)
1530     changeset = create(:changeset, :user => user)
1531     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))
1532
1533     get :query, :bbox => "-10,-10, 10, 10"
1534     assert_response :success, "can't get changesets in bbox"
1535     assert_changesets [1, 4, 6]
1536
1537     get :query, :bbox => "4.5,4.5,4.6,4.6"
1538     assert_response :success, "can't get changesets in bbox"
1539     assert_changesets [1]
1540
1541     # not found when looking for changesets of non-existing users
1542     get :query, :user => User.maximum(:id) + 1
1543     assert_response :not_found
1544     get :query, :display_name => " "
1545     assert_response :not_found
1546
1547     # can't get changesets of user 1 without authenticating
1548     get :query, :user => private_user.id
1549     assert_response :not_found, "shouldn't be able to get changesets by non-public user (ID)"
1550     get :query, :display_name => private_user.display_name
1551     assert_response :not_found, "shouldn't be able to get changesets by non-public user (name)"
1552
1553     # but this should work
1554     basic_authorization private_user.email, "test"
1555     get :query, :user => private_user.id
1556     assert_response :success, "can't get changesets by user ID"
1557     assert_changesets [private_user_changeset.id, private_user_closed_changeset.id]
1558
1559     get :query, :display_name => private_user.display_name
1560     assert_response :success, "can't get changesets by user name"
1561     assert_changesets [private_user_changeset.id, private_user_closed_changeset.id]
1562
1563     # check that the correct error is given when we provide both UID and name
1564     get :query, :user => private_user.id, :display_name => private_user.display_name
1565     assert_response :bad_request, "should be a bad request to have both ID and name specified"
1566
1567     get :query, :user => private_user.id, :open => true
1568     assert_response :success, "can't get changesets by user and open"
1569     assert_changesets [private_user_changeset.id]
1570
1571     get :query, :time => "2007-12-31"
1572     assert_response :success, "can't get changesets by time-since"
1573     assert_changesets [1, 2, 4, 5, 6, private_user_changeset.id, private_user_closed_changeset.id, changeset.id, closed_changeset.id]
1574
1575     get :query, :time => "2008-01-01T12:34Z"
1576     assert_response :success, "can't get changesets by time-since with hour"
1577     assert_changesets [1, 2, 4, 5, 6, private_user_changeset.id, private_user_closed_changeset.id, changeset.id, closed_changeset.id]
1578
1579     get :query, :time => "2007-12-31T23:59Z,2008-01-02T00:01Z"
1580     assert_response :success, "can't get changesets by time-range"
1581     assert_changesets [1, 5, 6, closed_changeset.id]
1582
1583     get :query, :open => "true"
1584     assert_response :success, "can't get changesets by open-ness"
1585     assert_changesets [1, 2, 4, private_user_changeset.id, changeset.id]
1586
1587     get :query, :closed => "true"
1588     assert_response :success, "can't get changesets by closed-ness"
1589     assert_changesets [3, 5, 6, 7, 8, 9, private_user_closed_changeset.id, closed_changeset.id]
1590
1591     get :query, :closed => "true", :user => private_user.id
1592     assert_response :success, "can't get changesets by closed-ness and user"
1593     assert_changesets [private_user_closed_changeset.id]
1594
1595     get :query, :closed => "true", :user => user.id
1596     assert_response :success, "can't get changesets by closed-ness and user"
1597     assert_changesets [closed_changeset.id]
1598
1599     get :query, :changesets => "#{private_user_changeset.id},#{changeset.id},#{closed_changeset.id}"
1600     assert_response :success, "can't get changesets by id (as comma-separated string)"
1601     assert_changesets [private_user_changeset.id, changeset.id, closed_changeset.id]
1602
1603     get :query, :changesets => ""
1604     assert_response :bad_request, "should be a bad request since changesets is empty"
1605   end
1606
1607   ##
1608   # check that errors are returned if garbage is inserted
1609   # into query strings
1610   def test_query_invalid
1611     ["abracadabra!",
1612      "1,2,3,F",
1613      ";drop table users;"].each do |bbox|
1614       get :query, :bbox => bbox
1615       assert_response :bad_request, "'#{bbox}' isn't a bbox"
1616     end
1617
1618     ["now()",
1619      "00-00-00",
1620      ";drop table users;",
1621      ",",
1622      "-,-"].each do |time|
1623       get :query, :time => time
1624       assert_response :bad_request, "'#{time}' isn't a valid time range"
1625     end
1626
1627     ["me",
1628      "foobar",
1629      "-1",
1630      "0"].each do |uid|
1631       get :query, :user => uid
1632       assert_response :bad_request, "'#{uid}' isn't a valid user ID"
1633     end
1634   end
1635
1636   ##
1637   # check updating tags on a changeset
1638   def test_changeset_update
1639     ## First try with a non-public user
1640     changeset = changesets(:normal_user_first_change)
1641     new_changeset = changeset.to_xml
1642     new_tag = XML::Node.new "tag"
1643     new_tag["k"] = "tagtesting"
1644     new_tag["v"] = "valuetesting"
1645     new_changeset.find("//osm/changeset").first << new_tag
1646     content new_changeset
1647
1648     # try without any authorization
1649     put :update, :id => changeset.id
1650     assert_response :unauthorized
1651
1652     # try with the wrong authorization
1653     basic_authorization create(:user).email, "test"
1654     put :update, :id => changeset.id
1655     assert_response :conflict
1656
1657     # now this should get an unauthorized
1658     basic_authorization changeset.user.email, "test"
1659     put :update, :id => changeset.id
1660     assert_require_public_data "user with their data non-public, shouldn't be able to edit their changeset"
1661
1662     ## Now try with the public user
1663     changeset = changesets(:public_user_first_change)
1664     create(:changeset_tag, :changeset => changeset)
1665     new_changeset = changeset.to_xml
1666     new_tag = XML::Node.new "tag"
1667     new_tag["k"] = "tagtesting"
1668     new_tag["v"] = "valuetesting"
1669     new_changeset.find("//osm/changeset").first << new_tag
1670     content new_changeset
1671
1672     # try without any authorization
1673     @request.env["HTTP_AUTHORIZATION"] = nil
1674     put :update, :id => changeset.id
1675     assert_response :unauthorized
1676
1677     # try with the wrong authorization
1678     basic_authorization create(:user).email, "test"
1679     put :update, :id => changeset.id
1680     assert_response :conflict
1681
1682     # now this should work...
1683     basic_authorization changeset.user.email, "test"
1684     put :update, :id => changeset.id
1685     assert_response :success
1686
1687     assert_select "osm>changeset[id='#{changeset.id}']", 1
1688     assert_select "osm>changeset>tag", 2
1689     assert_select "osm>changeset>tag[k='tagtesting'][v='valuetesting']", 1
1690   end
1691
1692   ##
1693   # check that a user different from the one who opened the changeset
1694   # can't modify it.
1695   def test_changeset_update_invalid
1696     basic_authorization create(:user).email, "test"
1697
1698     changeset = changesets(:normal_user_first_change)
1699     new_changeset = changeset.to_xml
1700     new_tag = XML::Node.new "tag"
1701     new_tag["k"] = "testing"
1702     new_tag["v"] = "testing"
1703     new_changeset.find("//osm/changeset").first << new_tag
1704
1705     content new_changeset
1706     put :update, :id => changeset.id
1707     assert_response :conflict
1708   end
1709
1710   ##
1711   # check that a changeset can contain a certain max number of changes.
1712   ## FIXME should be changed to an integration test due to the with_controller
1713   def test_changeset_limits
1714     basic_authorization create(:user).email, "test"
1715
1716     # open a new changeset
1717     content "<osm><changeset/></osm>"
1718     put :create
1719     assert_response :success, "can't create a new changeset"
1720     cs_id = @response.body.to_i
1721
1722     # start the counter just short of where the changeset should finish.
1723     offset = 10
1724     # alter the database to set the counter on the changeset directly,
1725     # otherwise it takes about 6 minutes to fill all of them.
1726     changeset = Changeset.find(cs_id)
1727     changeset.num_changes = Changeset::MAX_ELEMENTS - offset
1728     changeset.save!
1729
1730     with_controller(NodeController.new) do
1731       # create a new node
1732       content "<osm><node changeset='#{cs_id}' lat='0.0' lon='0.0'/></osm>"
1733       put :create
1734       assert_response :success, "can't create a new node"
1735       node_id = @response.body.to_i
1736
1737       get :read, :id => node_id
1738       assert_response :success, "can't read back new node"
1739       node_doc = XML::Parser.string(@response.body).parse
1740       node_xml = node_doc.find("//osm/node").first
1741
1742       # loop until we fill the changeset with nodes
1743       offset.times do |i|
1744         node_xml["lat"] = rand.to_s
1745         node_xml["lon"] = rand.to_s
1746         node_xml["version"] = (i + 1).to_s
1747
1748         content node_doc
1749         put :update, :id => node_id
1750         assert_response :success, "attempt #{i} should have succeeded"
1751       end
1752
1753       # trying again should fail
1754       node_xml["lat"] = rand.to_s
1755       node_xml["lon"] = rand.to_s
1756       node_xml["version"] = offset.to_s
1757
1758       content node_doc
1759       put :update, :id => node_id
1760       assert_response :conflict, "final attempt should have failed"
1761     end
1762
1763     changeset = Changeset.find(cs_id)
1764     assert_equal Changeset::MAX_ELEMENTS + 1, changeset.num_changes
1765
1766     # check that the changeset is now closed as well
1767     assert(!changeset.is_open?,
1768            "changeset should have been auto-closed by exceeding " +
1769            "element limit.")
1770   end
1771
1772   ##
1773   # This should display the last 20 changesets closed
1774   def test_list
1775     get :list, :format => "html"
1776     assert_response :success
1777     assert_template "history"
1778     assert_template :layout => "map"
1779     assert_select "h2", :text => "Changesets", :count => 1
1780
1781     xhr :get, :list, :format => "html", :list => "1"
1782     assert_response :success
1783     assert_template "list"
1784
1785     check_list_result(Changeset.all)
1786   end
1787
1788   ##
1789   # This should display the last 20 changesets closed
1790   def test_list_xhr
1791     xhr :get, :list, :format => "html"
1792     assert_response :success
1793     assert_template "history"
1794     assert_template :layout => "xhr"
1795     assert_select "h2", :text => "Changesets", :count => 1
1796
1797     xhr :get, :list, :format => "html", :list => "1"
1798     assert_response :success
1799     assert_template "list"
1800
1801     check_list_result(Changeset.all)
1802   end
1803
1804   ##
1805   # This should display the last 20 changesets closed in a specific area
1806   def test_list_bbox
1807     get :list, :format => "html", :bbox => "4.5,4.5,5.5,5.5"
1808     assert_response :success
1809     assert_template "history"
1810     assert_template :layout => "map"
1811     assert_select "h2", :text => "Changesets", :count => 1
1812
1813     xhr :get, :list, :format => "html", :bbox => "4.5,4.5,5.5,5.5", :list => "1"
1814     assert_response :success
1815     assert_template "list"
1816
1817     check_list_result(Changeset.where("min_lon < 55000000 and max_lon > 45000000 and min_lat < 55000000 and max_lat > 45000000"))
1818   end
1819
1820   ##
1821   # Checks the display of the user changesets listing
1822   def test_list_user
1823     user = create(:user)
1824     create(:changeset, :user => user)
1825     create(:changeset, :closed, :user => user)
1826
1827     get :list, :format => "html", :display_name => user.display_name
1828     assert_response :success
1829     assert_template "history"
1830
1831     xhr :get, :list, :format => "html", :display_name => user.display_name, :list => "1"
1832     assert_response :success
1833     assert_template "list"
1834
1835     check_list_result(user.changesets)
1836   end
1837
1838   ##
1839   # Checks the display of the user changesets listing for a private user
1840   def test_list_private_user
1841     private_user = create(:user, :data_public => false)
1842     create(:changeset, :user => private_user)
1843     create(:changeset, :closed, :user => private_user)
1844
1845     get :list, :format => "html", :display_name => private_user.display_name
1846     assert_response :success
1847     assert_template "history"
1848
1849     xhr :get, :list, :format => "html", :display_name => private_user.display_name, :list => "1"
1850     assert_response :success
1851     assert_template "list"
1852
1853     check_list_result(Changeset.none)
1854   end
1855
1856   ##
1857   # Check the not found of the list user changesets
1858   def test_list_user_not_found
1859     get :list, :format => "html", :display_name => "Some random user"
1860     assert_response :not_found
1861     assert_template "user/no_such_user"
1862
1863     xhr :get, :list, :format => "html", :display_name => "Some random user", :list => "1"
1864     assert_response :not_found
1865     assert_template "user/no_such_user"
1866   end
1867
1868   ##
1869   # Checks the display of the friends changesets listing
1870   def test_list_friends
1871     private_user = create(:user, :data_public => true)
1872     friend = create(:friend, :befriender => private_user)
1873     create(:changeset, :user => friend.befriendee)
1874
1875     get :list, :friends => true
1876     assert_response :redirect
1877     assert_redirected_to :controller => :user, :action => :login, :referer => friend_changesets_path
1878
1879     session[:user] = private_user.id
1880
1881     get :list, :friends => true
1882     assert_response :success
1883     assert_template "history"
1884
1885     xhr :get, :list, :friends => true, :list => "1"
1886     assert_response :success
1887     assert_template "list"
1888
1889     check_list_result(Changeset.where(:user => private_user.friend_users.identifiable))
1890   end
1891
1892   ##
1893   # Checks the display of the nearby user changesets listing
1894   def test_list_nearby
1895     private_user = create(:user, :data_public => false, :home_lat => 51.1, :home_lon => 1.0)
1896     user = create(:user, :home_lat => 51.0, :home_lon => 1.0)
1897     create(:changeset, :user => user)
1898
1899     get :list, :nearby => true
1900     assert_response :redirect
1901     assert_redirected_to :controller => :user, :action => :login, :referer => nearby_changesets_path
1902
1903     session[:user] = private_user.id
1904
1905     get :list, :nearby => true
1906     assert_response :success
1907     assert_template "history"
1908
1909     xhr :get, :list, :nearby => true, :list => "1"
1910     assert_response :success
1911     assert_template "list"
1912
1913     check_list_result(Changeset.where(:user => user.nearby))
1914   end
1915
1916   ##
1917   # Check that we can't request later pages of the changesets list
1918   def test_list_max_id
1919     xhr :get, :list, :format => "html", :max_id => 4
1920     assert_response :success
1921     assert_template "history"
1922     assert_template :layout => "xhr"
1923     assert_select "h2", :text => "Changesets", :count => 1
1924
1925     xhr :get, :list, :format => "html", :list => "1", :max_id => 4
1926     assert_response :success
1927     assert_template "list"
1928
1929     check_list_result(Changeset.where("id <= 4"))
1930   end
1931
1932   ##
1933   # This should display the last 20 changesets closed
1934   def test_feed
1935     get :feed, :format => :atom
1936     assert_response :success
1937     assert_template "list"
1938     assert_equal "application/atom+xml", response.content_type
1939
1940     check_feed_result(Changeset.all)
1941   end
1942
1943   ##
1944   # This should display the last 20 changesets closed in a specific area
1945   def test_feed_bbox
1946     get :feed, :format => :atom, :bbox => "4.5,4.5,5.5,5.5"
1947     assert_response :success
1948     assert_template "list"
1949     assert_equal "application/atom+xml", response.content_type
1950
1951     check_feed_result(Changeset.where("min_lon < 55000000 and max_lon > 45000000 and min_lat < 55000000 and max_lat > 45000000"))
1952   end
1953
1954   ##
1955   # Checks the display of the user changesets feed
1956   def test_feed_user
1957     user = create(:user)
1958     create_list(:changeset, 3, :user => user, :num_changes => 4)
1959
1960     get :feed, :format => :atom, :display_name => user.display_name
1961     assert_response :success
1962     assert_template "list"
1963     assert_equal "application/atom+xml", response.content_type
1964
1965     check_feed_result(user.changesets)
1966   end
1967
1968   ##
1969   # Check the not found of the user changesets feed
1970   def test_feed_user_not_found
1971     get :feed, :format => "atom", :display_name => "Some random user"
1972     assert_response :not_found
1973   end
1974
1975   ##
1976   # Check that we can't request later pages of the changesets feed
1977   def test_feed_max_id
1978     get :feed, :format => "atom", :max_id => 100
1979     assert_response :redirect
1980     assert_redirected_to :action => :feed
1981   end
1982
1983   ##
1984   # check that the changeset download for a changeset with a redacted
1985   # element in it doesn't contain that element.
1986   def test_diff_download_redacted
1987     changeset_id = changesets(:public_user_first_change).id
1988
1989     get :download, :id => changeset_id
1990     assert_response :success
1991
1992     assert_select "osmChange", 1
1993     # this changeset contains node 17 in versions 1 & 2, but 1 should
1994     # be hidden.
1995     assert_select "osmChange node[id='17']", 1
1996     assert_select "osmChange node[id='17'][version='1']", 0
1997   end
1998
1999   ##
2000   # create comment success
2001   def test_create_comment_success
2002     user = create(:user)
2003     user2 = create(:user)
2004     private_user = create(:user, :data_public => false)
2005     suspended_user = create(:user, :suspended)
2006     deleted_user = create(:user, :deleted)
2007     private_user_closed_changeset = create(:changeset, :closed, :user => private_user)
2008
2009     basic_authorization(user.email, "test")
2010
2011     assert_difference "ChangesetComment.count", 1 do
2012       assert_no_difference "ActionMailer::Base.deliveries.size" do
2013         post :comment, :id => private_user_closed_changeset.id, :text => "This is a comment"
2014       end
2015     end
2016     assert_response :success
2017
2018     changeset = create(:changeset, :closed, :user => private_user)
2019     changeset.subscribers.push(private_user)
2020     changeset.subscribers.push(user)
2021     changeset.subscribers.push(suspended_user)
2022     changeset.subscribers.push(deleted_user)
2023
2024     assert_difference "ChangesetComment.count", 1 do
2025       assert_difference "ActionMailer::Base.deliveries.size", 1 do
2026         post :comment, :id => changeset.id, :text => "This is a comment"
2027       end
2028     end
2029     assert_response :success
2030
2031     email = ActionMailer::Base.deliveries.first
2032     assert_equal 1, email.to.length
2033     assert_equal "[OpenStreetMap] #{user.display_name} has commented on one of your changesets", email.subject
2034     assert_equal private_user.email, email.to.first
2035
2036     ActionMailer::Base.deliveries.clear
2037
2038     basic_authorization(user2.email, "test")
2039
2040     assert_difference "ChangesetComment.count", 1 do
2041       assert_difference "ActionMailer::Base.deliveries.size", 2 do
2042         post :comment, :id => changeset.id, :text => "This is a comment"
2043       end
2044     end
2045     assert_response :success
2046
2047     email = ActionMailer::Base.deliveries.find { |e| e.to.first == private_user.email }
2048     assert_not_nil email
2049     assert_equal 1, email.to.length
2050     assert_equal "[OpenStreetMap] #{user2.display_name} has commented on one of your changesets", email.subject
2051
2052     email = ActionMailer::Base.deliveries.find { |e| e.to.first == user.email }
2053     assert_not_nil email
2054     assert_equal 1, email.to.length
2055     assert_equal "[OpenStreetMap] #{user2.display_name} has commented on a changeset you are interested in", email.subject
2056
2057     ActionMailer::Base.deliveries.clear
2058   end
2059
2060   ##
2061   # create comment fail
2062   def test_create_comment_fail
2063     # unauthorized
2064     post :comment, :id => changesets(:normal_user_closed_change).id, :text => "This is a comment"
2065     assert_response :unauthorized
2066
2067     basic_authorization(create(:user).email, "test")
2068
2069     # bad changeset id
2070     assert_no_difference "ChangesetComment.count" do
2071       post :comment, :id => 999111, :text => "This is a comment"
2072     end
2073     assert_response :not_found
2074
2075     # not closed changeset
2076     assert_no_difference "ChangesetComment.count" do
2077       post :comment, :id => changesets(:normal_user_first_change).id, :text => "This is a comment"
2078     end
2079     assert_response :conflict
2080
2081     # no text
2082     assert_no_difference "ChangesetComment.count" do
2083       post :comment, :id => changesets(:normal_user_closed_change).id
2084     end
2085     assert_response :bad_request
2086
2087     # empty text
2088     assert_no_difference "ChangesetComment.count" do
2089       post :comment, :id => changesets(:normal_user_closed_change).id, :text => ""
2090     end
2091     assert_response :bad_request
2092   end
2093
2094   ##
2095   # test subscribe success
2096   def test_subscribe_success
2097     basic_authorization(create(:user).email, "test")
2098     changeset = changesets(:normal_user_closed_change)
2099
2100     assert_difference "changeset.subscribers.count", 1 do
2101       post :subscribe, :id => changeset.id
2102     end
2103     assert_response :success
2104   end
2105
2106   ##
2107   # test subscribe fail
2108   def test_subscribe_fail
2109     user = create(:user)
2110
2111     # unauthorized
2112     changeset = changesets(:normal_user_closed_change)
2113     assert_no_difference "changeset.subscribers.count" do
2114       post :subscribe, :id => changeset.id
2115     end
2116     assert_response :unauthorized
2117
2118     basic_authorization(user.email, "test")
2119
2120     # bad changeset id
2121     assert_no_difference "changeset.subscribers.count" do
2122       post :subscribe, :id => 999111
2123     end
2124     assert_response :not_found
2125
2126     # not closed changeset
2127     changeset = changesets(:normal_user_first_change)
2128     assert_no_difference "changeset.subscribers.count" do
2129       post :subscribe, :id => changeset.id
2130     end
2131     assert_response :conflict
2132
2133     # trying to subscribe when already subscribed
2134     changeset = changesets(:normal_user_subscribed_change)
2135     changeset.subscribers.push(user)
2136     assert_no_difference "changeset.subscribers.count" do
2137       post :subscribe, :id => changeset.id
2138     end
2139     assert_response :conflict
2140   end
2141
2142   ##
2143   # test unsubscribe success
2144   def test_unsubscribe_success
2145     user = create(:user)
2146     basic_authorization(user.email, "test")
2147     changeset = changesets(:normal_user_subscribed_change)
2148     changeset.subscribers.push(user)
2149
2150     assert_difference "changeset.subscribers.count", -1 do
2151       post :unsubscribe, :id => changeset.id
2152     end
2153     assert_response :success
2154   end
2155
2156   ##
2157   # test unsubscribe fail
2158   def test_unsubscribe_fail
2159     # unauthorized
2160     changeset = changesets(:normal_user_closed_change)
2161     assert_no_difference "changeset.subscribers.count" do
2162       post :unsubscribe, :id => changeset.id
2163     end
2164     assert_response :unauthorized
2165
2166     basic_authorization(create(:user).email, "test")
2167
2168     # bad changeset id
2169     assert_no_difference "changeset.subscribers.count" do
2170       post :unsubscribe, :id => 999111
2171     end
2172     assert_response :not_found
2173
2174     # not closed changeset
2175     changeset = changesets(:normal_user_first_change)
2176     assert_no_difference "changeset.subscribers.count" do
2177       post :unsubscribe, :id => changeset.id
2178     end
2179     assert_response :conflict
2180
2181     # trying to unsubscribe when not subscribed
2182     changeset = changesets(:normal_user_closed_change)
2183     assert_no_difference "changeset.subscribers.count" do
2184       post :unsubscribe, :id => changeset.id
2185     end
2186     assert_response :not_found
2187   end
2188
2189   ##
2190   # test hide comment fail
2191   def test_hide_comment_fail
2192     # unauthorized
2193     comment = create(:changeset_comment)
2194     assert_equal true, comment.visible
2195
2196     post :hide_comment, :id => comment.id
2197     assert_response :unauthorized
2198     assert_equal true, comment.reload.visible
2199
2200     basic_authorization(create(:user).email, "test")
2201
2202     # not a moderator
2203     post :hide_comment, :id => comment.id
2204     assert_response :forbidden
2205     assert_equal true, comment.reload.visible
2206
2207     basic_authorization(create(:moderator_user).email, "test")
2208
2209     # bad comment id
2210     post :hide_comment, :id => 999111
2211     assert_response :not_found
2212     assert_equal true, comment.reload.visible
2213   end
2214
2215   ##
2216   # test hide comment succes
2217   def test_hide_comment_success
2218     comment = create(:changeset_comment)
2219     assert_equal true, comment.visible
2220
2221     basic_authorization(create(:moderator_user).email, "test")
2222
2223     post :hide_comment, :id => comment.id
2224     assert_response :success
2225     assert_equal false, comment.reload.visible
2226   end
2227
2228   ##
2229   # test unhide comment fail
2230   def test_unhide_comment_fail
2231     # unauthorized
2232     comment = create(:changeset_comment, :visible => false)
2233     assert_equal false, comment.visible
2234
2235     post :unhide_comment, :id => comment.id
2236     assert_response :unauthorized
2237     assert_equal false, comment.reload.visible
2238
2239     basic_authorization(create(:user).email, "test")
2240
2241     # not a moderator
2242     post :unhide_comment, :id => comment.id
2243     assert_response :forbidden
2244     assert_equal false, comment.reload.visible
2245
2246     basic_authorization(create(:moderator_user).email, "test")
2247
2248     # bad comment id
2249     post :unhide_comment, :id => 999111
2250     assert_response :not_found
2251     assert_equal false, comment.reload.visible
2252   end
2253
2254   ##
2255   # test unhide comment succes
2256   def test_unhide_comment_success
2257     comment = create(:changeset_comment, :visible => false)
2258     assert_equal false, comment.visible
2259
2260     basic_authorization(create(:moderator_user).email, "test")
2261
2262     post :unhide_comment, :id => comment.id
2263     assert_response :success
2264     assert_equal true, comment.reload.visible
2265   end
2266
2267   ##
2268   # test comments feed
2269   def test_comments_feed
2270     create_list(:changeset_comment, 3, :changeset_id => changesets(:normal_user_closed_change).id)
2271
2272     get :comments_feed, :format => "rss"
2273     assert_response :success
2274     assert_equal "application/rss+xml", @response.content_type
2275     assert_select "rss", :count => 1 do
2276       assert_select "channel", :count => 1 do
2277         assert_select "item", :count => 3
2278       end
2279     end
2280
2281     get :comments_feed, :format => "rss", :limit => 2
2282     assert_response :success
2283     assert_equal "application/rss+xml", @response.content_type
2284     assert_select "rss", :count => 1 do
2285       assert_select "channel", :count => 1 do
2286         assert_select "item", :count => 2
2287       end
2288     end
2289
2290     get :comments_feed, :id => changesets(:normal_user_closed_change), :format => "rss"
2291     assert_response :success
2292     assert_equal "application/rss+xml", @response.content_type
2293     assert_select "rss", :count => 1 do
2294       assert_select "channel", :count => 1 do
2295         assert_select "item", :count => 3
2296       end
2297     end
2298   end
2299
2300   ##
2301   # test comments feed
2302   def test_comments_feed_bad_limit
2303     get :comments_feed, :format => "rss", :limit => 0
2304     assert_response :bad_request
2305
2306     get :comments_feed, :format => "rss", :limit => 100001
2307     assert_response :bad_request
2308   end
2309
2310   private
2311
2312   ##
2313   # boilerplate for checking that certain changesets exist in the
2314   # output.
2315   def assert_changesets(ids)
2316     assert_select "osm>changeset", ids.size
2317     ids.each do |id|
2318       assert_select "osm>changeset[id='#{id}']", 1
2319     end
2320   end
2321
2322   ##
2323   # call the include method and assert properties of the bbox
2324   def check_after_include(changeset_id, lon, lat, bbox)
2325     content "<osm><node lon='#{lon}' lat='#{lat}'/></osm>"
2326     post :expand_bbox, :id => changeset_id
2327     assert_response :success, "Setting include of changeset failed: #{@response.body}"
2328
2329     # check exactly one changeset
2330     assert_select "osm>changeset", 1
2331     assert_select "osm>changeset[id='#{changeset_id}']", 1
2332
2333     # check the bbox
2334     doc = XML::Parser.string(@response.body).parse
2335     changeset = doc.find("//osm/changeset").first
2336     assert_equal bbox[0], changeset["min_lon"].to_f, "min lon"
2337     assert_equal bbox[1], changeset["min_lat"].to_f, "min lat"
2338     assert_equal bbox[2], changeset["max_lon"].to_f, "max lon"
2339     assert_equal bbox[3], changeset["max_lat"].to_f, "max lat"
2340   end
2341
2342   ##
2343   # update the changeset_id of a way element
2344   def update_changeset(xml, changeset_id)
2345     xml_attr_rewrite(xml, "changeset", changeset_id)
2346   end
2347
2348   ##
2349   # update an attribute in a way element
2350   def xml_attr_rewrite(xml, name, value)
2351     xml.find("//osm/way").first[name] = value.to_s
2352     xml
2353   end
2354
2355   ##
2356   # check the result of a list
2357   def check_list_result(changesets)
2358     changesets = changesets.where("num_changes > 0")
2359                            .order(:created_at => :desc)
2360                            .limit(20)
2361     assert changesets.size <= 20
2362
2363     assert_select "ol.changesets", :count => [changesets.size, 1].min do
2364       assert_select "li", :count => changesets.size
2365
2366       changesets.each do |changeset|
2367         assert_select "li#changeset_#{changeset.id}", :count => 1
2368       end
2369     end
2370   end
2371
2372   ##
2373   # check the result of a feed
2374   def check_feed_result(changesets)
2375     changesets = changesets.where("num_changes > 0")
2376                            .order(:created_at => :desc)
2377                            .limit(20)
2378     assert changesets.size <= 20
2379
2380     assert_select "feed", :count => [changesets.size, 1].min do
2381       assert_select "> title", :count => 1, :text => /^Changesets/
2382       assert_select "> entry", :count => changesets.size
2383
2384       changesets.each do |changeset|
2385         assert_select "> entry > id", changeset_url(:id => changeset.id)
2386       end
2387     end
2388   end
2389 end