]> git.openstreetmap.org Git - rails.git/blob - test/functional/changeset_controller_test.rb
4669df07d4ed96d0714d349ea41721b7c4392e3f
[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 that deleting stuff in a transaction doesn't bypass the checks
222   # to ensure that used elements are not deleted.
223   def test_upload_delete_invalid
224     basic_authorization "test@openstreetmap.org", "test"
225
226     diff = XML::Document.new
227     diff.root = XML::Node.new "osmChange"
228     delete = XML::Node.new "delete"
229     diff.root << delete
230     delete << current_relations(:visible_relation).to_xml_node
231     delete << current_ways(:used_way).to_xml_node
232     delete << current_nodes(:node_used_by_relationship).to_xml_node
233
234     # upload it
235     content diff
236     post :upload, :id => 1
237     assert_response :precondition_failed, 
238       "shouldn't be able to upload a invalid deletion diff: #{@response.body}"
239
240     # check that nothing was, in fact, deleted
241     assert_equal true, Node.find(current_nodes(:node_used_by_relationship).id).visible
242     assert_equal true, Way.find(current_ways(:used_way).id).visible
243     assert_equal true, Relation.find(current_relations(:visible_relation).id).visible
244   end
245
246   ##
247   # upload something which creates new objects and inserts them into
248   # existing containers using placeholders.
249   def test_upload_complex
250     basic_authorization "test@openstreetmap.org", "test"
251
252     # simple diff to create a node way and relation using placeholders
253     diff = <<EOF
254 <osmChange>
255  <create>
256   <node id='-1' lon='0' lat='0' changeset='1'>
257    <tag k='foo' v='bar'/>
258    <tag k='baz' v='bat'/>
259   </node>
260  </create>
261  <modify>
262   <way id='1' changeset='1' version='1'>
263    <nd ref='-1'/>
264    <nd ref='3'/>
265   </way>
266   <relation id='1' changeset='1' version='1'>
267    <member type='way' role='some' ref='3'/>
268    <member type='node' role='some' ref='-1'/>
269    <member type='relation' role='some' ref='3'/>
270   </relation>
271  </modify>
272 </osmChange>
273 EOF
274
275     # upload it
276     content diff
277     post :upload, :id => 1
278     assert_response :success, 
279       "can't upload a complex diff to changeset: #{@response.body}"
280
281     # check the returned payload
282     assert_select "diffResult[version=#{API_VERSION}][generator=\"#{GENERATOR}\"]", 1
283     assert_select "diffResult>node", 1
284     assert_select "diffResult>way", 1
285     assert_select "diffResult>relation", 1
286
287     # inspect the response to find out what the new element IDs are
288     doc = XML::Parser.string(@response.body).parse
289     new_node_id = doc.find("//diffResult/node").first["new_id"].to_i
290
291     # check that the changes made it into the database
292     assert_equal 2, Node.find(new_node_id).tags.size, "new node should have two tags"
293     assert_equal [new_node_id, 3], Way.find(1).nds, "way nodes should match"
294     Relation.find(1).members.each do |type,id,role|
295       if type == 'node'
296         assert_equal new_node_id, id, "relation should contain new node"
297       end
298     end
299   end
300     
301   ##
302   # create a diff which references several changesets, which should cause
303   # a rollback and none of the diff gets committed
304   def test_upload_invalid_changesets
305     basic_authorization "test@openstreetmap.org", "test"
306
307     # simple diff to create a node way and relation using placeholders
308     diff = <<EOF
309 <osmChange>
310  <modify>
311   <node id='1' lon='0' lat='0' changeset='1' version='1'/>
312   <way id='1' changeset='1' version='1'>
313    <nd ref='3'/>
314   </way>
315  </modify>
316  <modify>
317   <relation id='1' changeset='1' version='1'>
318    <member type='way' role='some' ref='3'/>
319    <member type='node' role='some' ref='5'/>
320    <member type='relation' role='some' ref='3'/>
321   </relation>
322  </modify>
323  <create>
324   <node id='-1' lon='0' lat='0' changeset='4'>
325    <tag k='foo' v='bar'/>
326    <tag k='baz' v='bat'/>
327   </node>
328  </create>
329 </osmChange>
330 EOF
331     # cache the objects before uploading them
332     node = current_nodes(:visible_node)
333     way = current_ways(:visible_way)
334     rel = current_relations(:visible_relation)
335
336     # upload it
337     content diff
338     post :upload, :id => 1
339     assert_response :conflict, 
340       "uploading a diff with multiple changsets should have failed"
341
342     # check that objects are unmodified
343     assert_nodes_are_equal(node, Node.find(1))
344     assert_ways_are_equal(way, Way.find(1))
345   end
346     
347   ##
348   # upload multiple versions of the same element in the same diff.
349   def test_upload_multiple_valid
350     basic_authorization "test@openstreetmap.org", "test"
351
352     # change the location of a node multiple times, each time referencing
353     # the last version. doesn't this depend on version numbers being
354     # sequential?
355     diff = <<EOF
356 <osmChange>
357  <modify>
358   <node id='1' lon='0' lat='0' changeset='1' version='1'/>
359   <node id='1' lon='1' lat='0' changeset='1' version='2'/>
360   <node id='1' lon='1' lat='1' changeset='1' version='3'/>
361   <node id='1' lon='1' lat='2' changeset='1' version='4'/>
362   <node id='1' lon='2' lat='2' changeset='1' version='5'/>
363   <node id='1' lon='3' lat='2' changeset='1' version='6'/>
364   <node id='1' lon='3' lat='3' changeset='1' version='7'/>
365   <node id='1' lon='9' lat='9' changeset='1' version='8'/>
366  </modify>
367 </osmChange>
368 EOF
369
370     # upload it
371     content diff
372     post :upload, :id => 1
373     assert_response :success, 
374       "can't upload multiple versions of an element in a diff: #{@response.body}"
375     
376     # check the response is well-formed. its counter-intuitive, but the
377     # API will return multiple elements with the same ID and different
378     # version numbers for each change we made.
379     assert_select "diffResult>node", 8
380   end
381
382   ##
383   # upload multiple versions of the same element in the same diff, but
384   # keep the version numbers the same.
385   def test_upload_multiple_duplicate
386     basic_authorization "test@openstreetmap.org", "test"
387
388     diff = <<EOF
389 <osmChange>
390  <modify>
391   <node id='1' lon='0' lat='0' changeset='1' version='1'/>
392   <node id='1' lon='1' lat='1' changeset='1' version='1'/>
393  </modify>
394 </osmChange>
395 EOF
396
397     # upload it
398     content diff
399     post :upload, :id => 1
400     assert_response :conflict, 
401       "shouldn't be able to upload the same element twice in a diff: #{@response.body}"
402   end
403
404   ##
405   # try to upload some elements without specifying the version
406   def test_upload_missing_version
407     basic_authorization "test@openstreetmap.org", "test"
408
409     diff = <<EOF
410 <osmChange>
411  <modify>
412   <node id='1' lon='1' lat='1' changeset='1'/>
413  </modify>
414 </osmChange>
415 EOF
416
417     # upload it
418     content diff
419     post :upload, :id => 1
420     assert_response :bad_request, 
421       "shouldn't be able to upload an element without version: #{@response.body}"
422   end
423   
424   ##
425   # try to upload with commands other than create, modify, or delete
426   def test_action_upload_invalid
427     basic_authorization "test@openstreetmap.org", "test"
428     
429     diff = <<EOF
430 <osmChange>
431   <ping>
432     <node id='1' lon='1' lat='1' changeset='1' />
433   </ping>
434 </osmChange>
435 EOF
436   content diff
437   post :upload, :id => 1
438   assert_response :bad_request, "Shouldn't be able to upload a diff with the action ping"
439   assert_equal @response.body, "Unknown action ping, choices are create, modify, delete."
440   end
441
442   ##
443   # upload a valid changeset which has a mixture of whitespace
444   # to check a bug reported by ivansanchez (#1565).
445   def test_upload_whitespace_valid
446     basic_authorization "test@openstreetmap.org", "test"
447
448     diff = <<EOF
449 <osmChange>
450  <modify><node id='1' lon='0' lat='0' changeset='1' 
451   version='1'></node>
452   <node id='1' lon='1' lat='1' changeset='1' version='2'><tag k='k' v='v'/></node></modify>
453  <modify>
454   <relation id='1' changeset='1' version='1'><member 
455    type='way' role='some' ref='3'/><member 
456     type='node' role='some' ref='5'/>
457    <member type='relation' role='some' ref='3'/>
458   </relation>
459  </modify></osmChange>
460 EOF
461
462     # upload it
463     content diff
464     post :upload, :id => 1
465     assert_response :success, 
466       "can't upload a valid diff with whitespace variations to changeset: #{@response.body}"
467
468     # check the response is well-formed
469     assert_select "diffResult>node", 2
470     assert_select "diffResult>relation", 1
471
472     # check that the changes made it into the database
473     assert_equal 1, Node.find(1).tags.size, "node 1 should now have one tag"
474     assert_equal 0, Relation.find(1).tags.size, "relation 1 should now have no tags"
475   end
476
477   ##
478   # upload a valid changeset which has a mixture of whitespace
479   # to check a bug reported by ivansanchez.
480   def test_upload_reuse_placeholder_valid
481     basic_authorization "test@openstreetmap.org", "test"
482
483     diff = <<EOF
484 <osmChange>
485  <create>
486   <node id='-1' lon='0' lat='0' changeset='1'>
487    <tag k="foo" v="bar"/>
488   </node>
489  </create>
490  <modify>
491   <node id='-1' lon='1' lat='1' changeset='1' version='1'/>
492  </modify>
493  <delete>
494   <node id='-1' lon='2' lat='2' changeset='1' version='2'/>
495  </delete>
496 </osmChange>
497 EOF
498
499     # upload it
500     content diff
501     post :upload, :id => 1
502     assert_response :success, 
503       "can't upload a valid diff with re-used placeholders to changeset: #{@response.body}"
504
505     # check the response is well-formed
506     assert_select "diffResult>node", 3
507     assert_select "diffResult>node[old_id=-1]", 3
508   end
509
510   ##
511   # test what happens if a diff upload re-uses placeholder IDs in an
512   # illegal way.
513   def test_upload_placeholder_invalid
514     basic_authorization "test@openstreetmap.org", "test"
515
516     diff = <<EOF
517 <osmChange>
518  <create>
519   <node id='-1' lon='0' lat='0' changeset='1' version='1'/>
520   <node id='-1' lon='1' lat='1' changeset='1' version='1'/>
521   <node id='-1' lon='2' lat='2' changeset='1' version='2'/>
522  </create>
523 </osmChange>
524 EOF
525
526     # upload it
527     content diff
528     post :upload, :id => 1
529     assert_response :bad_request, 
530       "shouldn't be able to re-use placeholder IDs"
531   end
532
533   ##
534   # when we make some simple changes we get the same changes back from the 
535   # diff download.
536   def test_diff_download_simple
537     basic_authorization(users(:normal_user).email, "test")
538
539     # create a temporary changeset
540     content "<osm><changeset>" +
541       "<tag k='created_by' v='osm test suite checking changesets'/>" + 
542       "</changeset></osm>"
543     put :create
544     assert_response :success
545     changeset_id = @response.body.to_i
546
547     # add a diff to it
548     diff = <<EOF
549 <osmChange>
550  <modify>
551   <node id='1' lon='0' lat='0' changeset='#{changeset_id}' version='1'/>
552   <node id='1' lon='1' lat='0' changeset='#{changeset_id}' version='2'/>
553   <node id='1' lon='1' lat='1' changeset='#{changeset_id}' version='3'/>
554   <node id='1' lon='1' lat='2' changeset='#{changeset_id}' version='4'/>
555   <node id='1' lon='2' lat='2' changeset='#{changeset_id}' version='5'/>
556   <node id='1' lon='3' lat='2' changeset='#{changeset_id}' version='6'/>
557   <node id='1' lon='3' lat='3' changeset='#{changeset_id}' version='7'/>
558   <node id='1' lon='9' lat='9' changeset='#{changeset_id}' version='8'/>
559  </modify>
560 </osmChange>
561 EOF
562
563     # upload it
564     content diff
565     post :upload, :id => changeset_id
566     assert_response :success, 
567       "can't upload multiple versions of an element in a diff: #{@response.body}"
568     
569     get :download, :id => changeset_id
570     assert_response :success
571
572     assert_select "osmChange", 1
573     assert_select "osmChange>modify", 8
574     assert_select "osmChange>modify>node", 8
575   end
576   
577   ##
578   # culled this from josm to ensure that nothing in the way that josm
579   # is formatting the request is causing it to fail.
580   #
581   # NOTE: the error turned out to be something else completely!
582   def test_josm_upload
583     basic_authorization(users(:normal_user).email, "test")
584
585     # create a temporary changeset
586     content "<osm><changeset>" +
587       "<tag k='created_by' v='osm test suite checking changesets'/>" + 
588       "</changeset></osm>"
589     put :create
590     assert_response :success
591     changeset_id = @response.body.to_i
592
593     diff = <<OSM
594 <osmChange version="0.6" generator="JOSM">
595 <create version="0.6" generator="JOSM">
596   <node id='-1' visible='true' changeset='#{changeset_id}' lat='51.49619982187321' lon='-0.18722061869438314' />
597   <node id='-2' visible='true' changeset='#{changeset_id}' lat='51.496359883909605' lon='-0.18653093576241928' />
598   <node id='-3' visible='true' changeset='#{changeset_id}' lat='51.49598132358285' lon='-0.18719613290981638' />
599   <node id='-4' visible='true' changeset='#{changeset_id}' lat='51.4961591711078' lon='-0.18629015888084607' />
600   <node id='-5' visible='true' changeset='#{changeset_id}' lat='51.49582126021711' lon='-0.18708186591517145' />
601   <node id='-6' visible='true' changeset='#{changeset_id}' lat='51.49591018437858' lon='-0.1861432441734455' />
602   <node id='-7' visible='true' changeset='#{changeset_id}' lat='51.49560784152179' lon='-0.18694719410005425' />
603   <node id='-8' visible='true' changeset='#{changeset_id}' lat='51.49567389979617' lon='-0.1860289771788006' />
604   <node id='-9' visible='true' changeset='#{changeset_id}' lat='51.49543761398892' lon='-0.186820684213126' />
605   <way id='-10' action='modiy' visible='true' changeset='#{changeset_id}'>
606     <nd ref='-1' />
607     <nd ref='-2' />
608     <nd ref='-3' />
609     <nd ref='-4' />
610     <nd ref='-5' />
611     <nd ref='-6' />
612     <nd ref='-7' />
613     <nd ref='-8' />
614     <nd ref='-9' />
615     <tag k='highway' v='residential' />
616     <tag k='name' v='Foobar Street' />
617   </way>
618 </create>
619 </osmChange>
620 OSM
621
622     # upload it
623     content diff
624     post :upload, :id => changeset_id
625     assert_response :success, 
626       "can't upload a diff from JOSM: #{@response.body}"
627     
628     get :download, :id => changeset_id
629     assert_response :success
630
631     assert_select "osmChange", 1
632     assert_select "osmChange>create>node", 9
633     assert_select "osmChange>create>way", 1
634     assert_select "osmChange>create>way>nd", 9
635     assert_select "osmChange>create>way>tag", 2
636   end
637
638   ##
639   # when we make some complex changes we get the same changes back from the 
640   # diff download.
641   def test_diff_download_complex
642     basic_authorization(users(:normal_user).email, "test")
643
644     # create a temporary changeset
645     content "<osm><changeset>" +
646       "<tag k='created_by' v='osm test suite checking changesets'/>" + 
647       "</changeset></osm>"
648     put :create
649     assert_response :success
650     changeset_id = @response.body.to_i
651
652     # add a diff to it
653     diff = <<EOF
654 <osmChange>
655  <delete>
656   <node id='1' lon='0' lat='0' changeset='#{changeset_id}' version='1'/>
657  </delete>
658  <create>
659   <node id='-1' lon='9' lat='9' changeset='#{changeset_id}' version='0'/>
660   <node id='-2' lon='8' lat='9' changeset='#{changeset_id}' version='0'/>
661   <node id='-3' lon='7' lat='9' changeset='#{changeset_id}' version='0'/>
662  </create>
663  <modify>
664   <node id='3' lon='20' lat='15' changeset='#{changeset_id}' version='1'/>
665   <way id='1' changeset='#{changeset_id}' version='1'>
666    <nd ref='3'/>
667    <nd ref='-1'/>
668    <nd ref='-2'/>
669    <nd ref='-3'/>
670   </way>
671  </modify>
672 </osmChange>
673 EOF
674
675     # upload it
676     content diff
677     post :upload, :id => changeset_id
678     assert_response :success, 
679       "can't upload multiple versions of an element in a diff: #{@response.body}"
680     
681     get :download, :id => changeset_id
682     assert_response :success
683
684     assert_select "osmChange", 1
685     assert_select "osmChange>create", 3
686     assert_select "osmChange>delete", 1
687     assert_select "osmChange>modify", 2
688     assert_select "osmChange>create>node", 3
689     assert_select "osmChange>delete>node", 1 
690     assert_select "osmChange>modify>node", 1
691     assert_select "osmChange>modify>way", 1
692   end
693
694   ##
695   # check that the bounding box of a changeset gets updated correctly
696   def test_changeset_bbox
697     basic_authorization "test@openstreetmap.org", "test"
698
699     # create a new changeset
700     content "<osm><changeset/></osm>"
701     put :create
702     assert_response :success, "Creating of changeset failed."
703     changeset_id = @response.body.to_i
704     
705     # add a single node to it
706     with_controller(NodeController.new) do
707       content "<osm><node lon='1' lat='2' changeset='#{changeset_id}'/></osm>"
708       put :create
709       assert_response :success, "Couldn't create node."
710     end
711
712     # get the bounding box back from the changeset
713     get :read, :id => changeset_id
714     assert_response :success, "Couldn't read back changeset."
715     assert_select "osm>changeset[min_lon=1.0]", 1
716     assert_select "osm>changeset[max_lon=1.0]", 1
717     assert_select "osm>changeset[min_lat=2.0]", 1
718     assert_select "osm>changeset[max_lat=2.0]", 1
719
720     # add another node to it
721     with_controller(NodeController.new) do
722       content "<osm><node lon='2' lat='1' changeset='#{changeset_id}'/></osm>"
723       put :create
724       assert_response :success, "Couldn't create second node."
725     end
726
727     # get the bounding box back from the changeset
728     get :read, :id => changeset_id
729     assert_response :success, "Couldn't read back changeset for the second time."
730     assert_select "osm>changeset[min_lon=1.0]", 1
731     assert_select "osm>changeset[max_lon=2.0]", 1
732     assert_select "osm>changeset[min_lat=1.0]", 1
733     assert_select "osm>changeset[max_lat=2.0]", 1
734
735     # add (delete) a way to it
736     with_controller(WayController.new) do
737       content update_changeset(current_ways(:visible_way).to_xml,
738                                changeset_id)
739       put :delete, :id => current_ways(:visible_way).id
740       assert_response :success, "Couldn't delete a way."
741     end
742
743     # get the bounding box back from the changeset
744     get :read, :id => changeset_id
745     assert_response :success, "Couldn't read back changeset for the third time."
746     assert_select "osm>changeset[min_lon=1.0]", 1
747     assert_select "osm>changeset[max_lon=3.1]", 1
748     assert_select "osm>changeset[min_lat=1.0]", 1
749     assert_select "osm>changeset[max_lat=3.1]", 1    
750   end
751
752   ##
753   # test that the changeset :include method works as it should
754   def test_changeset_include
755     basic_authorization "test@openstreetmap.org", "test"
756
757     # create a new changeset
758     content "<osm><changeset/></osm>"
759     put :create
760     assert_response :success, "Creating of changeset failed."
761     changeset_id = @response.body.to_i
762
763     # NOTE: the include method doesn't over-expand, like inserting
764     # a real method does. this is because we expect the client to 
765     # know what it is doing!
766     check_after_include(changeset_id,  1,  1, [ 1,  1,  1,  1])
767     check_after_include(changeset_id,  3,  3, [ 1,  1,  3,  3])
768     check_after_include(changeset_id,  4,  2, [ 1,  1,  4,  3])
769     check_after_include(changeset_id,  2,  2, [ 1,  1,  4,  3])
770     check_after_include(changeset_id, -1, -1, [-1, -1,  4,  3])
771     check_after_include(changeset_id, -2,  5, [-2, -1,  4,  5])
772   end
773
774   ##
775   # test the query functionality of changesets
776   def test_query
777     get :query, :bbox => "-10,-10, 10, 10"
778     assert_response :success, "can't get changesets in bbox"
779     assert_changesets [1,4,6]
780
781     get :query, :bbox => "4.5,4.5,4.6,4.6"
782     assert_response :success, "can't get changesets in bbox"
783     assert_changesets [1]
784
785     # can't get changesets of user 1 without authenticating
786     get :query, :user => users(:normal_user).id
787     assert_response :not_found, "shouldn't be able to get changesets by non-public user"
788
789     # but this should work
790     basic_authorization "test@openstreetmap.org", "test"
791     get :query, :user => users(:normal_user).id
792     assert_response :success, "can't get changesets by user"
793     assert_changesets [1,3,4,6]
794
795     get :query, :user => users(:normal_user).id, :open => true
796     assert_response :success, "can't get changesets by user and open"
797     assert_changesets [1,4]
798
799     get :query, :time => '2007-12-31'
800     assert_response :success, "can't get changesets by time-since"
801     assert_changesets [1,2,4,5,6]
802
803     get :query, :time => '2008-01-01T12:34Z'
804     assert_response :success, "can't get changesets by time-since with hour"
805     assert_changesets [1,2,4,5,6]
806
807     get :query, :time => '2007-12-31T23:59Z,2008-01-01T00:01Z'
808     assert_response :success, "can't get changesets by time-range"
809     assert_changesets [1,4,5,6]
810
811     get :query, :open => 'true'
812     assert_response :success, "can't get changesets by open-ness"
813     assert_changesets [1,2,4]
814   end
815
816   ##
817   # check that errors are returned if garbage is inserted 
818   # into query strings
819   def test_query_invalid
820     [ "abracadabra!",
821       "1,2,3,F",
822       ";drop table users;"
823       ].each do |bbox|
824       get :query, :bbox => bbox
825       assert_response :bad_request, "'#{bbox}' isn't a bbox"
826     end
827
828     [ "now()",
829       "00-00-00",
830       ";drop table users;",
831       ",",
832       "-,-"
833       ].each do |time|
834       get :query, :time => time
835       assert_response :bad_request, "'#{time}' isn't a valid time range"
836     end
837
838     [ "me",
839       "foobar",
840       "-1",
841       "0"
842       ].each do |uid|
843       get :query, :user => uid
844       assert_response :bad_request, "'#{uid}' isn't a valid user ID"
845     end
846   end
847
848   ##
849   # check updating tags on a changeset
850   def test_changeset_update
851     changeset = changesets(:normal_user_first_change)
852     new_changeset = changeset.to_xml
853     new_tag = XML::Node.new "tag"
854     new_tag['k'] = "tagtesting"
855     new_tag['v'] = "valuetesting"
856     new_changeset.find("//osm/changeset").first << new_tag
857     content new_changeset
858
859     # try without any authorization
860     put :update, :id => changeset.id
861     assert_response :unauthorized
862
863     # try with the wrong authorization
864     basic_authorization "test@example.com", "test"
865     put :update, :id => changeset.id
866     assert_response :conflict
867
868     # now this should work...
869     basic_authorization "test@openstreetmap.org", "test"
870     put :update, :id => changeset.id
871     assert_response :success
872
873     assert_select "osm>changeset[id=#{changeset.id}]", 1
874     assert_select "osm>changeset>tag", 2
875     assert_select "osm>changeset>tag[k=tagtesting][v=valuetesting]", 1
876   end
877   
878   ##
879   # check that a user different from the one who opened the changeset
880   # can't modify it.
881   def test_changeset_update_invalid
882     basic_authorization "test@example.com", "test"
883
884     changeset = changesets(:normal_user_first_change)
885     new_changeset = changeset.to_xml
886     new_tag = XML::Node.new "tag"
887     new_tag['k'] = "testing"
888     new_tag['v'] = "testing"
889     new_changeset.find("//osm/changeset").first << new_tag
890
891     content new_changeset
892     put :update, :id => changeset.id
893     assert_response :conflict
894   end
895
896   ##
897   # check that a changeset can contain a certain max number of changes.
898   def test_changeset_limits
899     basic_authorization "test@openstreetmap.org", "test"
900
901     # open a new changeset
902     content "<osm><changeset/></osm>"
903     put :create
904     assert_response :success, "can't create a new changeset"
905     cs_id = @response.body.to_i
906
907     # start the counter just short of where the changeset should finish.
908     offset = 10
909     # alter the database to set the counter on the changeset directly, 
910     # otherwise it takes about 6 minutes to fill all of them.
911     changeset = Changeset.find(cs_id)
912     changeset.num_changes = Changeset::MAX_ELEMENTS - offset
913     changeset.save!
914
915     with_controller(NodeController.new) do
916       # create a new node
917       content "<osm><node changeset='#{cs_id}' lat='0.0' lon='0.0'/></osm>"
918       put :create
919       assert_response :success, "can't create a new node"
920       node_id = @response.body.to_i
921
922       get :read, :id => node_id
923       assert_response :success, "can't read back new node"
924       node_doc = XML::Parser.string(@response.body).parse
925       node_xml = node_doc.find("//osm/node").first
926
927       # loop until we fill the changeset with nodes
928       offset.times do |i|
929         node_xml['lat'] = rand.to_s
930         node_xml['lon'] = rand.to_s
931         node_xml['version'] = (i+1).to_s
932
933         content node_doc
934         put :update, :id => node_id
935         assert_response :success, "attempt #{i} should have succeeded"
936       end
937
938       # trying again should fail
939       node_xml['lat'] = rand.to_s
940       node_xml['lon'] = rand.to_s
941       node_xml['version'] = offset.to_s
942       
943       content node_doc
944       put :update, :id => node_id
945       assert_response :conflict, "final attempt should have failed"
946     end
947
948     changeset = Changeset.find(cs_id)
949     assert_equal Changeset::MAX_ELEMENTS + 1, changeset.num_changes
950
951     # check that the changeset is now closed as well
952     assert(!changeset.is_open?, 
953            "changeset should have been auto-closed by exceeding " + 
954            "element limit.")
955   end
956   
957   #------------------------------------------------------------
958   # utility functions
959   #------------------------------------------------------------
960
961   ##
962   # boilerplate for checking that certain changesets exist in the
963   # output.
964   def assert_changesets(ids)
965     assert_select "osm>changeset", ids.size
966     ids.each do |id|
967       assert_select "osm>changeset[id=#{id}]", 1
968     end
969   end
970
971   ##
972   # call the include method and assert properties of the bbox
973   def check_after_include(changeset_id, lon, lat, bbox)
974     content "<osm><node lon='#{lon}' lat='#{lat}'/></osm>"
975     post :expand_bbox, :id => changeset_id
976     assert_response :success, "Setting include of changeset failed: #{@response.body}"
977
978     # check exactly one changeset
979     assert_select "osm>changeset", 1
980     assert_select "osm>changeset[id=#{changeset_id}]", 1
981
982     # check the bbox
983     doc = XML::Parser.string(@response.body).parse
984     changeset = doc.find("//osm/changeset").first
985     assert_equal bbox[0], changeset['min_lon'].to_f, "min lon"
986     assert_equal bbox[1], changeset['min_lat'].to_f, "min lat"
987     assert_equal bbox[2], changeset['max_lon'].to_f, "max lon"
988     assert_equal bbox[3], changeset['max_lat'].to_f, "max lat"
989   end
990
991   ##
992   # update the changeset_id of a way element
993   def update_changeset(xml, changeset_id)
994     xml_attr_rewrite(xml, 'changeset', changeset_id)
995   end
996
997   ##
998   # update an attribute in a way element
999   def xml_attr_rewrite(xml, name, value)
1000     xml.find("//osm/way").first[name] = value.to_s
1001     return xml
1002   end
1003
1004 end