]> git.openstreetmap.org Git - rails.git/blob - test/functional/changeset_controller_test.rb
4c98fb36d037fabb064c9aded73e2172e44fbc2e
[rails.git] / test / functional / changeset_controller_test.rb
1 require File.dirname(__FILE__) + '/../test_helper'
2 require 'changeset_controller'
3
4 class ChangesetControllerTest < ActionController::TestCase
5   api_fixtures
6
7   def basic_authorization(user, pass)
8     @request.env["HTTP_AUTHORIZATION"] = "Basic %s" % Base64.encode64("#{user}:#{pass}")
9   end
10
11   def content(c)
12     @request.env["RAW_POST_DATA"] = c.to_s
13   end
14   
15   # -----------------------
16   # Test simple changeset creation
17   # -----------------------
18   
19   def test_create
20     basic_authorization "test@openstreetmap.org", "test"
21     
22     # Create the first user's changeset
23     content "<osm><changeset>" +
24       "<tag k='created_by' v='osm test suite checking changesets'/>" + 
25       "</changeset></osm>"
26     put :create
27     
28     assert_response :success, "Creation of changeset did not return sucess status"
29     newid = @response.body.to_i
30
31     # check end time, should be an hour ahead of creation time
32     cs = Changeset.find(newid)
33     duration = cs.closed_at - cs.created_at
34     # the difference can either be a rational, or a floating point number
35     # of seconds, depending on the code path taken :-(
36     if duration.class == Rational
37       assert_equal Rational(1,24), duration , "initial idle timeout should be an hour (#{cs.created_at} -> #{cs.closed_at})"
38     else
39       # must be number of seconds...
40       assert_equal 3600.0, duration , "initial idle timeout should be an hour (#{cs.created_at} -> #{cs.closed_at})"
41     end
42   end
43   
44   def test_create_invalid
45     basic_authorization "test@openstreetmap.org", "test"
46     content "<osm><changeset></osm>"
47     put :create
48     assert_response :bad_request, "creating a invalid changeset should fail"
49   end
50
51   ##
52   # check that the changeset can be read and returns the correct
53   # document structure.
54   def test_read
55     changeset_id = changesets(:normal_user_first_change).id
56     get :read, :id => changeset_id
57     assert_response :success, "cannot get first changeset"
58     
59     assert_select "osm[version=#{API_VERSION}][generator=\"OpenStreetMap server\"]", 1
60     assert_select "osm>changeset[id=#{changeset_id}]", 1
61   end
62   
63   ##
64   # test that the user who opened a change can close it
65   def test_close
66     basic_authorization "test@openstreetmap.org", "test"
67
68     cs_id = changesets(:normal_user_first_change).id
69     put :close, :id => cs_id
70     assert_response :success
71
72     # test that it really is closed now
73     cs = Changeset.find(cs_id)
74     assert(!cs.is_open?, 
75            "changeset should be closed now (#{cs.closed_at} > #{Time.now}.")
76   end
77
78   ##
79   # test that a different user can't close another user's changeset
80   def test_close_invalid
81     basic_authorization "test@example.com", "test"
82
83     put :close, :id => changesets(:normal_user_first_change).id
84     assert_response :conflict
85     assert_equal "The user doesn't own that changeset", @response.body
86   end
87
88   ##
89   # upload something simple, but valid and check that it can 
90   # be read back ok.
91   def test_upload_simple_valid
92     basic_authorization "test@openstreetmap.org", "test"
93
94     # simple diff to change a node, way and relation by removing 
95     # their tags
96     diff = <<EOF
97 <osmChange>
98  <modify>
99   <node id='1' lon='0' lat='0' changeset='1' version='1'/>
100   <way id='1' changeset='1' version='1'>
101    <nd ref='3'/>
102   </way>
103  </modify>
104  <modify>
105   <relation id='1' changeset='1' version='1'>
106    <member type='way' role='some' ref='3'/>
107    <member type='node' role='some' ref='5'/>
108    <member type='relation' role='some' ref='3'/>
109   </relation>
110  </modify>
111 </osmChange>
112 EOF
113
114     # upload it
115     content diff
116     post :upload, :id => 1
117     assert_response :success, 
118       "can't upload a simple valid diff to changeset: #{@response.body}"
119
120     # check that the changes made it into the database
121     assert_equal 0, Node.find(1).tags.size, "node 1 should now have no tags"
122     assert_equal 0, Way.find(1).tags.size, "way 1 should now have no tags"
123     assert_equal 0, Relation.find(1).tags.size, "relation 1 should now have no tags"
124   end
125     
126   ##
127   # upload something which creates new objects using placeholders
128   def test_upload_create_valid
129     basic_authorization "test@openstreetmap.org", "test"
130
131     # simple diff to create a node way and relation using placeholders
132     diff = <<EOF
133 <osmChange>
134  <create>
135   <node id='-1' lon='0' lat='0' changeset='1'>
136    <tag k='foo' v='bar'/>
137    <tag k='baz' v='bat'/>
138   </node>
139   <way id='-1' changeset='1'>
140    <nd ref='3'/>
141   </way>
142  </create>
143  <create>
144   <relation id='-1' changeset='1'>
145    <member type='way' role='some' ref='3'/>
146    <member type='node' role='some' ref='5'/>
147    <member type='relation' role='some' ref='3'/>
148   </relation>
149  </create>
150 </osmChange>
151 EOF
152
153     # upload it
154     content diff
155     post :upload, :id => 1
156     assert_response :success, 
157       "can't upload a simple valid creation to changeset: #{@response.body}"
158
159     # check the returned payload
160     assert_select "diffResult[version=#{API_VERSION}][generator=\"OpenStreetMap server\"]", 1
161     assert_select "diffResult>node", 1
162     assert_select "diffresult>way", 1
163     assert_select "diffResult>relation", 1
164
165     # inspect the response to find out what the new element IDs are
166     doc = XML::Parser.string(@response.body).parse
167     new_node_id = doc.find("//diffResult/node").first["new_id"].to_i
168     new_way_id = doc.find("//diffResult/way").first["new_id"].to_i
169     new_rel_id = doc.find("//diffResult/relation").first["new_id"].to_i
170
171     # check the old IDs are all present and negative one
172     assert_equal -1, doc.find("//diffResult/node").first["old_id"].to_i
173     assert_equal -1, doc.find("//diffResult/way").first["old_id"].to_i
174     assert_equal -1, doc.find("//diffResult/relation").first["old_id"].to_i
175
176     # check the versions are present and equal one
177     assert_equal 1, doc.find("//diffResult/node").first["new_version"].to_i
178     assert_equal 1, doc.find("//diffResult/way").first["new_version"].to_i
179     assert_equal 1, doc.find("//diffResult/relation").first["new_version"].to_i
180
181     # check that the changes made it into the database
182     assert_equal 2, Node.find(new_node_id).tags.size, "new node should have two tags"
183     assert_equal 0, Way.find(new_way_id).tags.size, "new way should have no tags"
184     assert_equal 0, Relation.find(new_rel_id).tags.size, "new relation should have no tags"
185   end
186     
187   ##
188   # test a complex delete where we delete elements which rely on eachother
189   # in the same transaction.
190   def test_upload_delete
191     basic_authorization "test@openstreetmap.org", "test"
192
193     diff = XML::Document.new
194     diff.root = XML::Node.new "osmChange"
195     delete = XML::Node.new "delete"
196     diff.root << delete
197     delete << current_relations(:visible_relation).to_xml_node
198     delete << current_relations(:used_relation).to_xml_node
199     delete << current_ways(:used_way).to_xml_node
200     delete << current_nodes(:node_used_by_relationship).to_xml_node
201
202     # upload it
203     content diff
204     post :upload, :id => 1
205     assert_response :success, 
206       "can't upload a deletion diff to changeset: #{@response.body}"
207
208     # check the response is well-formed
209     assert_select "diffResult>node", 1
210     assert_select "diffResult>way", 1
211     assert_select "diffResult>relation", 2
212
213     # check that everything was deleted
214     assert_equal false, Node.find(current_nodes(:node_used_by_relationship).id).visible
215     assert_equal false, Way.find(current_ways(:used_way).id).visible
216     assert_equal false, Relation.find(current_relations(:visible_relation).id).visible
217     assert_equal false, Relation.find(current_relations(:used_relation).id).visible
218   end
219
220   ##
221   # test uploading a delete with no lat/lon, as they are optional in
222   # the osmChange spec.
223   def test_upload_nolatlon_delete
224     basic_authorization "test@openstreetmap.org", "test"
225
226     node = current_nodes(:visible_node)
227     cs = changesets(:normal_user_first_change)
228     diff = "<osmChange><delete><node id='#{node.id}' version='#{node.version}' changeset='#{cs.id}'/></delete></osmChange>"
229
230     # upload it
231     content diff
232     post :upload, :id => cs.id
233     assert_response :success, 
234       "can't upload a deletion diff to changeset: #{@response.body}"
235
236     # check the response is well-formed
237     assert_select "diffResult>node", 1
238
239     # check that everything was deleted
240     assert_equal false, Node.find(node.id).visible
241   end
242
243   ##
244   # test that deleting stuff in a transaction doesn't bypass the checks
245   # to ensure that used elements are not deleted.
246   def test_upload_delete_invalid
247     basic_authorization "test@openstreetmap.org", "test"
248
249     diff = XML::Document.new
250     diff.root = XML::Node.new "osmChange"
251     delete = XML::Node.new "delete"
252     diff.root << delete
253     delete << current_relations(:visible_relation).to_xml_node
254     delete << current_ways(:used_way).to_xml_node
255     delete << current_nodes(:node_used_by_relationship).to_xml_node
256
257     # upload it
258     content diff
259     post :upload, :id => 1
260     assert_response :precondition_failed, 
261       "shouldn't be able to upload a invalid deletion diff: #{@response.body}"
262
263     # check that nothing was, in fact, deleted
264     assert_equal true, Node.find(current_nodes(:node_used_by_relationship).id).visible
265     assert_equal true, Way.find(current_ways(:used_way).id).visible
266     assert_equal true, Relation.find(current_relations(:visible_relation).id).visible
267   end
268
269   ##
270   # upload something which creates new objects and inserts them into
271   # existing containers using placeholders.
272   def test_upload_complex
273     basic_authorization "test@openstreetmap.org", "test"
274
275     # simple diff to create a node way and relation using placeholders
276     diff = <<EOF
277 <osmChange>
278  <create>
279   <node id='-1' lon='0' lat='0' changeset='1'>
280    <tag k='foo' v='bar'/>
281    <tag k='baz' v='bat'/>
282   </node>
283  </create>
284  <modify>
285   <way id='1' changeset='1' version='1'>
286    <nd ref='-1'/>
287    <nd ref='3'/>
288   </way>
289   <relation id='1' changeset='1' version='1'>
290    <member type='way' role='some' ref='3'/>
291    <member type='node' role='some' ref='-1'/>
292    <member type='relation' role='some' ref='3'/>
293   </relation>
294  </modify>
295 </osmChange>
296 EOF
297
298     # upload it
299     content diff
300     post :upload, :id => 1
301     assert_response :success, 
302       "can't upload a complex diff to changeset: #{@response.body}"
303
304     # check the returned payload
305     assert_select "diffResult[version=#{API_VERSION}][generator=\"#{GENERATOR}\"]", 1
306     assert_select "diffResult>node", 1
307     assert_select "diffResult>way", 1
308     assert_select "diffResult>relation", 1
309
310     # inspect the response to find out what the new element IDs are
311     doc = XML::Parser.string(@response.body).parse
312     new_node_id = doc.find("//diffResult/node").first["new_id"].to_i
313
314     # check that the changes made it into the database
315     assert_equal 2, Node.find(new_node_id).tags.size, "new node should have two tags"
316     assert_equal [new_node_id, 3], Way.find(1).nds, "way nodes should match"
317     Relation.find(1).members.each do |type,id,role|
318       if type == 'node'
319         assert_equal new_node_id, id, "relation should contain new node"
320       end
321     end
322   end
323     
324   ##
325   # create a diff which references several changesets, which should cause
326   # a rollback and none of the diff gets committed
327   def test_upload_invalid_changesets
328     basic_authorization "test@openstreetmap.org", "test"
329
330     # simple diff to create a node way and relation using placeholders
331     diff = <<EOF
332 <osmChange>
333  <modify>
334   <node id='1' lon='0' lat='0' changeset='1' version='1'/>
335   <way id='1' changeset='1' version='1'>
336    <nd ref='3'/>
337   </way>
338  </modify>
339  <modify>
340   <relation id='1' changeset='1' 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  <create>
347   <node id='-1' lon='0' lat='0' changeset='4'>
348    <tag k='foo' v='bar'/>
349    <tag k='baz' v='bat'/>
350   </node>
351  </create>
352 </osmChange>
353 EOF
354     # cache the objects before uploading them
355     node = current_nodes(:visible_node)
356     way = current_ways(:visible_way)
357     rel = current_relations(:visible_relation)
358
359     # upload it
360     content diff
361     post :upload, :id => 1
362     assert_response :conflict, 
363       "uploading a diff with multiple changsets should have failed"
364
365     # check that objects are unmodified
366     assert_nodes_are_equal(node, Node.find(1))
367     assert_ways_are_equal(way, Way.find(1))
368   end
369     
370   ##
371   # upload multiple versions of the same element in the same diff.
372   def test_upload_multiple_valid
373     basic_authorization "test@openstreetmap.org", "test"
374
375     # change the location of a node multiple times, each time referencing
376     # the last version. doesn't this depend on version numbers being
377     # sequential?
378     diff = <<EOF
379 <osmChange>
380  <modify>
381   <node id='1' lon='0' lat='0' changeset='1' version='1'/>
382   <node id='1' lon='1' lat='0' changeset='1' version='2'/>
383   <node id='1' lon='1' lat='1' changeset='1' version='3'/>
384   <node id='1' lon='1' lat='2' changeset='1' version='4'/>
385   <node id='1' lon='2' lat='2' changeset='1' version='5'/>
386   <node id='1' lon='3' lat='2' changeset='1' version='6'/>
387   <node id='1' lon='3' lat='3' changeset='1' version='7'/>
388   <node id='1' lon='9' lat='9' changeset='1' version='8'/>
389  </modify>
390 </osmChange>
391 EOF
392
393     # upload it
394     content diff
395     post :upload, :id => 1
396     assert_response :success, 
397       "can't upload multiple versions of an element in a diff: #{@response.body}"
398     
399     # check the response is well-formed. its counter-intuitive, but the
400     # API will return multiple elements with the same ID and different
401     # version numbers for each change we made.
402     assert_select "diffResult>node", 8
403   end
404
405   ##
406   # upload multiple versions of the same element in the same diff, but
407   # keep the version numbers the same.
408   def test_upload_multiple_duplicate
409     basic_authorization "test@openstreetmap.org", "test"
410
411     diff = <<EOF
412 <osmChange>
413  <modify>
414   <node id='1' lon='0' lat='0' changeset='1' version='1'/>
415   <node id='1' lon='1' lat='1' changeset='1' version='1'/>
416  </modify>
417 </osmChange>
418 EOF
419
420     # upload it
421     content diff
422     post :upload, :id => 1
423     assert_response :conflict, 
424       "shouldn't be able to upload the same element twice in a diff: #{@response.body}"
425   end
426
427   ##
428   # try to upload some elements without specifying the version
429   def test_upload_missing_version
430     basic_authorization "test@openstreetmap.org", "test"
431
432     diff = <<EOF
433 <osmChange>
434  <modify>
435   <node id='1' lon='1' lat='1' changeset='1'/>
436  </modify>
437 </osmChange>
438 EOF
439
440     # upload it
441     content diff
442     post :upload, :id => 1
443     assert_response :bad_request, 
444       "shouldn't be able to upload an element without version: #{@response.body}"
445   end
446   
447   ##
448   # try to upload with commands other than create, modify, or delete
449   def test_action_upload_invalid
450     basic_authorization "test@openstreetmap.org", "test"
451     
452     diff = <<EOF
453 <osmChange>
454   <ping>
455     <node id='1' lon='1' lat='1' changeset='1' />
456   </ping>
457 </osmChange>
458 EOF
459   content diff
460   post :upload, :id => 1
461   assert_response :bad_request, "Shouldn't be able to upload a diff with the action ping"
462   assert_equal @response.body, "Unknown action ping, choices are create, modify, delete."
463   end
464
465   ##
466   # upload a valid changeset which has a mixture of whitespace
467   # to check a bug reported by ivansanchez (#1565).
468   def test_upload_whitespace_valid
469     basic_authorization "test@openstreetmap.org", "test"
470
471     diff = <<EOF
472 <osmChange>
473  <modify><node id='1' lon='0' lat='0' changeset='1' 
474   version='1'></node>
475   <node id='1' lon='1' lat='1' changeset='1' version='2'><tag k='k' v='v'/></node></modify>
476  <modify>
477   <relation id='1' changeset='1' version='1'><member 
478    type='way' role='some' ref='3'/><member 
479     type='node' role='some' ref='5'/>
480    <member type='relation' role='some' ref='3'/>
481   </relation>
482  </modify></osmChange>
483 EOF
484
485     # upload it
486     content diff
487     post :upload, :id => 1
488     assert_response :success, 
489       "can't upload a valid diff with whitespace variations to changeset: #{@response.body}"
490
491     # check the response is well-formed
492     assert_select "diffResult>node", 2
493     assert_select "diffResult>relation", 1
494
495     # check that the changes made it into the database
496     assert_equal 1, Node.find(1).tags.size, "node 1 should now have one tag"
497     assert_equal 0, Relation.find(1).tags.size, "relation 1 should now have no tags"
498   end
499
500   ##
501   # upload a valid changeset which has a mixture of whitespace
502   # to check a bug reported by ivansanchez.
503   def test_upload_reuse_placeholder_valid
504     basic_authorization "test@openstreetmap.org", "test"
505
506     diff = <<EOF
507 <osmChange>
508  <create>
509   <node id='-1' lon='0' lat='0' changeset='1'>
510    <tag k="foo" v="bar"/>
511   </node>
512  </create>
513  <modify>
514   <node id='-1' lon='1' lat='1' changeset='1' version='1'/>
515  </modify>
516  <delete>
517   <node id='-1' lon='2' lat='2' changeset='1' version='2'/>
518  </delete>
519 </osmChange>
520 EOF
521
522     # upload it
523     content diff
524     post :upload, :id => 1
525     assert_response :success, 
526       "can't upload a valid diff with re-used placeholders to changeset: #{@response.body}"
527
528     # check the response is well-formed
529     assert_select "diffResult>node", 3
530     assert_select "diffResult>node[old_id=-1]", 3
531   end
532
533   ##
534   # test what happens if a diff upload re-uses placeholder IDs in an
535   # illegal way.
536   def test_upload_placeholder_invalid
537     basic_authorization "test@openstreetmap.org", "test"
538
539     diff = <<EOF
540 <osmChange>
541  <create>
542   <node id='-1' lon='0' lat='0' changeset='1' version='1'/>
543   <node id='-1' lon='1' lat='1' changeset='1' version='1'/>
544   <node id='-1' lon='2' lat='2' changeset='1' version='2'/>
545  </create>
546 </osmChange>
547 EOF
548
549     # upload it
550     content diff
551     post :upload, :id => 1
552     assert_response :bad_request, 
553       "shouldn't be able to re-use placeholder IDs"
554   end
555
556   ##
557   # test what happens if a diff is uploaded containing only a node
558   # move.
559   def test_upload_node_move
560     basic_authorization "test@openstreetmap.org", "test"
561
562     content "<osm><changeset>" +
563       "<tag k='created_by' v='osm test suite checking changesets'/>" + 
564       "</changeset></osm>"
565     put :create
566     assert_response :success
567     changeset_id = @response.body.to_i
568
569     old_node = current_nodes(:visible_node)
570
571     diff = XML::Document.new
572     diff.root = XML::Node.new "osmChange"
573     modify = XML::Node.new "modify"
574     xml_old_node = old_node.to_xml_node
575     xml_old_node["lat"] = (2.0).to_s
576     xml_old_node["lon"] = (2.0).to_s
577     xml_old_node["changeset"] = changeset_id.to_s
578     modify << xml_old_node
579     diff.root << modify
580
581     # upload it
582     content diff
583     post :upload, :id => changeset_id
584     assert_response :success, 
585       "diff should have uploaded OK"
586
587     # check the bbox
588     changeset = Changeset.find(changeset_id)
589     assert_equal 1*SCALE, changeset.min_lon, "min_lon should be 1 degree"
590     assert_equal 2*SCALE, changeset.max_lon, "max_lon should be 2 degrees"
591     assert_equal 1*SCALE, changeset.min_lat, "min_lat should be 1 degree"
592     assert_equal 2*SCALE, changeset.max_lat, "max_lat should be 2 degrees"
593   end
594
595   ##
596   # test what happens if a diff is uploaded adding a node to a way.
597   def test_upload_way_extend
598     basic_authorization "test@openstreetmap.org", "test"
599
600     content "<osm><changeset>" +
601       "<tag k='created_by' v='osm test suite checking changesets'/>" + 
602       "</changeset></osm>"
603     put :create
604     assert_response :success
605     changeset_id = @response.body.to_i
606
607     old_way = current_ways(:visible_way)
608
609     diff = XML::Document.new
610     diff.root = XML::Node.new "osmChange"
611     modify = XML::Node.new "modify"
612     xml_old_way = old_way.to_xml_node
613     nd_ref = XML::Node.new "nd"
614     nd_ref["ref"] = current_nodes(:visible_node).id.to_s
615     xml_old_way << nd_ref
616     xml_old_way["changeset"] = changeset_id.to_s
617     modify << xml_old_way
618     diff.root << modify
619
620     # upload it
621     content diff
622     post :upload, :id => changeset_id
623     assert_response :success, 
624       "diff should have uploaded OK"
625
626     # check the bbox
627     changeset = Changeset.find(changeset_id)
628     assert_equal 1*SCALE, changeset.min_lon, "min_lon should be 1 degree"
629     assert_equal 3*SCALE, changeset.max_lon, "max_lon should be 3 degrees"
630     assert_equal 1*SCALE, changeset.min_lat, "min_lat should be 1 degree"
631     assert_equal 3*SCALE, changeset.max_lat, "max_lat should be 3 degrees"
632   end
633
634   ##
635   # test for more issues in #1568
636   def test_upload_empty_invalid
637     basic_authorization "test@openstreetmap.org", "test"
638
639     [ "<osmChange/>",
640       "<osmChange></osmChange>",
641       "<osmChange><modify/></osmChange>",
642       "<osmChange><modify></modify></osmChange>"
643     ].each do |diff|
644       # upload it
645       content diff
646       post :upload, :id => 1
647       assert_response(:success, "should be able to upload " +
648                       "empty changeset: " + diff)
649     end
650   end
651
652   ##
653   # when we make some simple changes we get the same changes back from the 
654   # diff download.
655   def test_diff_download_simple
656     basic_authorization(users(:normal_user).email, "test")
657
658     # create a temporary changeset
659     content "<osm><changeset>" +
660       "<tag k='created_by' v='osm test suite checking changesets'/>" + 
661       "</changeset></osm>"
662     put :create
663     assert_response :success
664     changeset_id = @response.body.to_i
665
666     # add a diff to it
667     diff = <<EOF
668 <osmChange>
669  <modify>
670   <node id='1' lon='0' lat='0' changeset='#{changeset_id}' version='1'/>
671   <node id='1' lon='1' lat='0' changeset='#{changeset_id}' version='2'/>
672   <node id='1' lon='1' lat='1' changeset='#{changeset_id}' version='3'/>
673   <node id='1' lon='1' lat='2' changeset='#{changeset_id}' version='4'/>
674   <node id='1' lon='2' lat='2' changeset='#{changeset_id}' version='5'/>
675   <node id='1' lon='3' lat='2' changeset='#{changeset_id}' version='6'/>
676   <node id='1' lon='3' lat='3' changeset='#{changeset_id}' version='7'/>
677   <node id='1' lon='9' lat='9' changeset='#{changeset_id}' version='8'/>
678  </modify>
679 </osmChange>
680 EOF
681
682     # upload it
683     content diff
684     post :upload, :id => changeset_id
685     assert_response :success, 
686       "can't upload multiple versions of an element in a diff: #{@response.body}"
687     
688     get :download, :id => changeset_id
689     assert_response :success
690
691     assert_select "osmChange", 1
692     assert_select "osmChange>modify", 8
693     assert_select "osmChange>modify>node", 8
694   end
695   
696   ##
697   # culled this from josm to ensure that nothing in the way that josm
698   # is formatting the request is causing it to fail.
699   #
700   # NOTE: the error turned out to be something else completely!
701   def test_josm_upload
702     basic_authorization(users(:normal_user).email, "test")
703
704     # create a temporary changeset
705     content "<osm><changeset>" +
706       "<tag k='created_by' v='osm test suite checking changesets'/>" + 
707       "</changeset></osm>"
708     put :create
709     assert_response :success
710     changeset_id = @response.body.to_i
711
712     diff = <<OSM
713 <osmChange version="0.6" generator="JOSM">
714 <create version="0.6" generator="JOSM">
715   <node id='-1' visible='true' changeset='#{changeset_id}' lat='51.49619982187321' lon='-0.18722061869438314' />
716   <node id='-2' visible='true' changeset='#{changeset_id}' lat='51.496359883909605' lon='-0.18653093576241928' />
717   <node id='-3' visible='true' changeset='#{changeset_id}' lat='51.49598132358285' lon='-0.18719613290981638' />
718   <node id='-4' visible='true' changeset='#{changeset_id}' lat='51.4961591711078' lon='-0.18629015888084607' />
719   <node id='-5' visible='true' changeset='#{changeset_id}' lat='51.49582126021711' lon='-0.18708186591517145' />
720   <node id='-6' visible='true' changeset='#{changeset_id}' lat='51.49591018437858' lon='-0.1861432441734455' />
721   <node id='-7' visible='true' changeset='#{changeset_id}' lat='51.49560784152179' lon='-0.18694719410005425' />
722   <node id='-8' visible='true' changeset='#{changeset_id}' lat='51.49567389979617' lon='-0.1860289771788006' />
723   <node id='-9' visible='true' changeset='#{changeset_id}' lat='51.49543761398892' lon='-0.186820684213126' />
724   <way id='-10' action='modiy' visible='true' changeset='#{changeset_id}'>
725     <nd ref='-1' />
726     <nd ref='-2' />
727     <nd ref='-3' />
728     <nd ref='-4' />
729     <nd ref='-5' />
730     <nd ref='-6' />
731     <nd ref='-7' />
732     <nd ref='-8' />
733     <nd ref='-9' />
734     <tag k='highway' v='residential' />
735     <tag k='name' v='Foobar Street' />
736   </way>
737 </create>
738 </osmChange>
739 OSM
740
741     # upload it
742     content diff
743     post :upload, :id => changeset_id
744     assert_response :success, 
745       "can't upload a diff from JOSM: #{@response.body}"
746     
747     get :download, :id => changeset_id
748     assert_response :success
749
750     assert_select "osmChange", 1
751     assert_select "osmChange>create>node", 9
752     assert_select "osmChange>create>way", 1
753     assert_select "osmChange>create>way>nd", 9
754     assert_select "osmChange>create>way>tag", 2
755   end
756
757   ##
758   # when we make some complex changes we get the same changes back from the 
759   # diff download.
760   def test_diff_download_complex
761     basic_authorization(users(:normal_user).email, "test")
762
763     # create a temporary changeset
764     content "<osm><changeset>" +
765       "<tag k='created_by' v='osm test suite checking changesets'/>" + 
766       "</changeset></osm>"
767     put :create
768     assert_response :success
769     changeset_id = @response.body.to_i
770
771     # add a diff to it
772     diff = <<EOF
773 <osmChange>
774  <delete>
775   <node id='1' lon='0' lat='0' changeset='#{changeset_id}' version='1'/>
776  </delete>
777  <create>
778   <node id='-1' lon='9' lat='9' changeset='#{changeset_id}' version='0'/>
779   <node id='-2' lon='8' lat='9' changeset='#{changeset_id}' version='0'/>
780   <node id='-3' lon='7' lat='9' changeset='#{changeset_id}' version='0'/>
781  </create>
782  <modify>
783   <node id='3' lon='20' lat='15' changeset='#{changeset_id}' version='1'/>
784   <way id='1' changeset='#{changeset_id}' version='1'>
785    <nd ref='3'/>
786    <nd ref='-1'/>
787    <nd ref='-2'/>
788    <nd ref='-3'/>
789   </way>
790  </modify>
791 </osmChange>
792 EOF
793
794     # upload it
795     content diff
796     post :upload, :id => changeset_id
797     assert_response :success, 
798       "can't upload multiple versions of an element in a diff: #{@response.body}"
799     
800     get :download, :id => changeset_id
801     assert_response :success
802
803     assert_select "osmChange", 1
804     assert_select "osmChange>create", 3
805     assert_select "osmChange>delete", 1
806     assert_select "osmChange>modify", 2
807     assert_select "osmChange>create>node", 3
808     assert_select "osmChange>delete>node", 1 
809     assert_select "osmChange>modify>node", 1
810     assert_select "osmChange>modify>way", 1
811   end
812
813   ##
814   # check that the bounding box of a changeset gets updated correctly
815   def test_changeset_bbox
816     basic_authorization "test@openstreetmap.org", "test"
817
818     # create a new changeset
819     content "<osm><changeset/></osm>"
820     put :create
821     assert_response :success, "Creating of changeset failed."
822     changeset_id = @response.body.to_i
823     
824     # add a single node to it
825     with_controller(NodeController.new) do
826       content "<osm><node lon='1' lat='2' changeset='#{changeset_id}'/></osm>"
827       put :create
828       assert_response :success, "Couldn't create node."
829     end
830
831     # get the bounding box back from the changeset
832     get :read, :id => changeset_id
833     assert_response :success, "Couldn't read back changeset."
834     assert_select "osm>changeset[min_lon=1.0]", 1
835     assert_select "osm>changeset[max_lon=1.0]", 1
836     assert_select "osm>changeset[min_lat=2.0]", 1
837     assert_select "osm>changeset[max_lat=2.0]", 1
838
839     # add another node to it
840     with_controller(NodeController.new) do
841       content "<osm><node lon='2' lat='1' changeset='#{changeset_id}'/></osm>"
842       put :create
843       assert_response :success, "Couldn't create second node."
844     end
845
846     # get the bounding box back from the changeset
847     get :read, :id => changeset_id
848     assert_response :success, "Couldn't read back changeset for the second time."
849     assert_select "osm>changeset[min_lon=1.0]", 1
850     assert_select "osm>changeset[max_lon=2.0]", 1
851     assert_select "osm>changeset[min_lat=1.0]", 1
852     assert_select "osm>changeset[max_lat=2.0]", 1
853
854     # add (delete) a way to it, which contains a point at (3,3)
855     with_controller(WayController.new) do
856       content update_changeset(current_ways(:visible_way).to_xml,
857                                changeset_id)
858       put :delete, :id => current_ways(:visible_way).id
859       assert_response :success, "Couldn't delete a way."
860     end
861
862     # get the bounding box back from the changeset
863     get :read, :id => changeset_id
864     assert_response :success, "Couldn't read back changeset for the third time."
865     # note that the 3.1 here is because of the bbox overexpansion
866     assert_select "osm>changeset[min_lon=1.0]", 1
867     assert_select "osm>changeset[max_lon=3.1]", 1
868     assert_select "osm>changeset[min_lat=1.0]", 1
869     assert_select "osm>changeset[max_lat=3.1]", 1    
870   end
871
872   ##
873   # test that the changeset :include method works as it should
874   def test_changeset_include
875     basic_authorization "test@openstreetmap.org", "test"
876
877     # create a new changeset
878     content "<osm><changeset/></osm>"
879     put :create
880     assert_response :success, "Creating of changeset failed."
881     changeset_id = @response.body.to_i
882
883     # NOTE: the include method doesn't over-expand, like inserting
884     # a real method does. this is because we expect the client to 
885     # know what it is doing!
886     check_after_include(changeset_id,  1,  1, [ 1,  1,  1,  1])
887     check_after_include(changeset_id,  3,  3, [ 1,  1,  3,  3])
888     check_after_include(changeset_id,  4,  2, [ 1,  1,  4,  3])
889     check_after_include(changeset_id,  2,  2, [ 1,  1,  4,  3])
890     check_after_include(changeset_id, -1, -1, [-1, -1,  4,  3])
891     check_after_include(changeset_id, -2,  5, [-2, -1,  4,  5])
892   end
893
894   ##
895   # test the query functionality of changesets
896   def test_query
897     get :query, :bbox => "-10,-10, 10, 10"
898     assert_response :success, "can't get changesets in bbox"
899     assert_changesets [1,4,6]
900
901     get :query, :bbox => "4.5,4.5,4.6,4.6"
902     assert_response :success, "can't get changesets in bbox"
903     assert_changesets [1]
904
905     # can't get changesets of user 1 without authenticating
906     get :query, :user => users(:normal_user).id
907     assert_response :not_found, "shouldn't be able to get changesets by non-public user"
908
909     # but this should work
910     basic_authorization "test@openstreetmap.org", "test"
911     get :query, :user => users(:normal_user).id
912     assert_response :success, "can't get changesets by user"
913     assert_changesets [1,3,4,6]
914
915     get :query, :user => users(:normal_user).id, :open => true
916     assert_response :success, "can't get changesets by user and open"
917     assert_changesets [1,4]
918
919     get :query, :time => '2007-12-31'
920     assert_response :success, "can't get changesets by time-since"
921     assert_changesets [1,2,4,5,6]
922
923     get :query, :time => '2008-01-01T12:34Z'
924     assert_response :success, "can't get changesets by time-since with hour"
925     assert_changesets [1,2,4,5,6]
926
927     get :query, :time => '2007-12-31T23:59Z,2008-01-01T00:01Z'
928     assert_response :success, "can't get changesets by time-range"
929     assert_changesets [1,4,5,6]
930
931     get :query, :open => 'true'
932     assert_response :success, "can't get changesets by open-ness"
933     assert_changesets [1,2,4]
934   end
935
936   ##
937   # check that errors are returned if garbage is inserted 
938   # into query strings
939   def test_query_invalid
940     [ "abracadabra!",
941       "1,2,3,F",
942       ";drop table users;"
943       ].each do |bbox|
944       get :query, :bbox => bbox
945       assert_response :bad_request, "'#{bbox}' isn't a bbox"
946     end
947
948     [ "now()",
949       "00-00-00",
950       ";drop table users;",
951       ",",
952       "-,-"
953       ].each do |time|
954       get :query, :time => time
955       assert_response :bad_request, "'#{time}' isn't a valid time range"
956     end
957
958     [ "me",
959       "foobar",
960       "-1",
961       "0"
962       ].each do |uid|
963       get :query, :user => uid
964       assert_response :bad_request, "'#{uid}' isn't a valid user ID"
965     end
966   end
967
968   ##
969   # check updating tags on a changeset
970   def test_changeset_update
971     changeset = changesets(:normal_user_first_change)
972     new_changeset = changeset.to_xml
973     new_tag = XML::Node.new "tag"
974     new_tag['k'] = "tagtesting"
975     new_tag['v'] = "valuetesting"
976     new_changeset.find("//osm/changeset").first << new_tag
977     content new_changeset
978
979     # try without any authorization
980     put :update, :id => changeset.id
981     assert_response :unauthorized
982
983     # try with the wrong authorization
984     basic_authorization "test@example.com", "test"
985     put :update, :id => changeset.id
986     assert_response :conflict
987
988     # now this should work...
989     basic_authorization "test@openstreetmap.org", "test"
990     put :update, :id => changeset.id
991     assert_response :success
992
993     assert_select "osm>changeset[id=#{changeset.id}]", 1
994     assert_select "osm>changeset>tag", 2
995     assert_select "osm>changeset>tag[k=tagtesting][v=valuetesting]", 1
996   end
997   
998   ##
999   # check that a user different from the one who opened the changeset
1000   # can't modify it.
1001   def test_changeset_update_invalid
1002     basic_authorization "test@example.com", "test"
1003
1004     changeset = changesets(:normal_user_first_change)
1005     new_changeset = changeset.to_xml
1006     new_tag = XML::Node.new "tag"
1007     new_tag['k'] = "testing"
1008     new_tag['v'] = "testing"
1009     new_changeset.find("//osm/changeset").first << new_tag
1010
1011     content new_changeset
1012     put :update, :id => changeset.id
1013     assert_response :conflict
1014   end
1015
1016   ##
1017   # check that a changeset can contain a certain max number of changes.
1018   def test_changeset_limits
1019     basic_authorization "test@openstreetmap.org", "test"
1020
1021     # open a new changeset
1022     content "<osm><changeset/></osm>"
1023     put :create
1024     assert_response :success, "can't create a new changeset"
1025     cs_id = @response.body.to_i
1026
1027     # start the counter just short of where the changeset should finish.
1028     offset = 10
1029     # alter the database to set the counter on the changeset directly, 
1030     # otherwise it takes about 6 minutes to fill all of them.
1031     changeset = Changeset.find(cs_id)
1032     changeset.num_changes = Changeset::MAX_ELEMENTS - offset
1033     changeset.save!
1034
1035     with_controller(NodeController.new) do
1036       # create a new node
1037       content "<osm><node changeset='#{cs_id}' lat='0.0' lon='0.0'/></osm>"
1038       put :create
1039       assert_response :success, "can't create a new node"
1040       node_id = @response.body.to_i
1041
1042       get :read, :id => node_id
1043       assert_response :success, "can't read back new node"
1044       node_doc = XML::Parser.string(@response.body).parse
1045       node_xml = node_doc.find("//osm/node").first
1046
1047       # loop until we fill the changeset with nodes
1048       offset.times do |i|
1049         node_xml['lat'] = rand.to_s
1050         node_xml['lon'] = rand.to_s
1051         node_xml['version'] = (i+1).to_s
1052
1053         content node_doc
1054         put :update, :id => node_id
1055         assert_response :success, "attempt #{i} should have succeeded"
1056       end
1057
1058       # trying again should fail
1059       node_xml['lat'] = rand.to_s
1060       node_xml['lon'] = rand.to_s
1061       node_xml['version'] = offset.to_s
1062       
1063       content node_doc
1064       put :update, :id => node_id
1065       assert_response :conflict, "final attempt should have failed"
1066     end
1067
1068     changeset = Changeset.find(cs_id)
1069     assert_equal Changeset::MAX_ELEMENTS + 1, changeset.num_changes
1070
1071     # check that the changeset is now closed as well
1072     assert(!changeset.is_open?, 
1073            "changeset should have been auto-closed by exceeding " + 
1074            "element limit.")
1075   end
1076   
1077   # This should display the last 20 changesets closed.
1078   def test_list
1079     @changesets = Changeset.find(:all, :order => "created_at DESC", :conditions => ['min_lat IS NOT NULL'], :limit=> 20)
1080     assert @changesets.size <= 20
1081     get :list
1082     assert_response :success
1083     assert_template "list"
1084     # Now check that all 20 (or however many were returned) changesets are in the html
1085     assert_select "h1", :text => "Recent Changes", :count => 1
1086     assert_select "table[id='keyvalue'] tr", :count => @changesets.size + 1
1087     @changesets.each do |changeset|
1088       # FIXME this test needs rewriting - test for table contents
1089     end
1090   end
1091   
1092   #------------------------------------------------------------
1093   # utility functions
1094   #------------------------------------------------------------
1095
1096   ##
1097   # boilerplate for checking that certain changesets exist in the
1098   # output.
1099   def assert_changesets(ids)
1100     assert_select "osm>changeset", ids.size
1101     ids.each do |id|
1102       assert_select "osm>changeset[id=#{id}]", 1
1103     end
1104   end
1105
1106   ##
1107   # call the include method and assert properties of the bbox
1108   def check_after_include(changeset_id, lon, lat, bbox)
1109     content "<osm><node lon='#{lon}' lat='#{lat}'/></osm>"
1110     post :expand_bbox, :id => changeset_id
1111     assert_response :success, "Setting include of changeset failed: #{@response.body}"
1112
1113     # check exactly one changeset
1114     assert_select "osm>changeset", 1
1115     assert_select "osm>changeset[id=#{changeset_id}]", 1
1116
1117     # check the bbox
1118     doc = XML::Parser.string(@response.body).parse
1119     changeset = doc.find("//osm/changeset").first
1120     assert_equal bbox[0], changeset['min_lon'].to_f, "min lon"
1121     assert_equal bbox[1], changeset['min_lat'].to_f, "min lat"
1122     assert_equal bbox[2], changeset['max_lon'].to_f, "max lon"
1123     assert_equal bbox[3], changeset['max_lat'].to_f, "max lat"
1124   end
1125
1126   ##
1127   # update the changeset_id of a way element
1128   def update_changeset(xml, changeset_id)
1129     xml_attr_rewrite(xml, 'changeset', changeset_id)
1130   end
1131
1132   ##
1133   # update an attribute in a way element
1134   def xml_attr_rewrite(xml, name, value)
1135     xml.find("//osm/way").first[name] = value.to_s
1136     return xml
1137   end
1138
1139 end