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