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