]> git.openstreetmap.org Git - rails.git/blob - test/controllers/amf_controller_test.rb
d388114dd99626db7f0f54ff3fa8e07f086e8880
[rails.git] / test / controllers / amf_controller_test.rb
1 require "test_helper"
2 require "stringio"
3 include Potlatch
4
5 class AmfControllerTest < ActionController::TestCase
6   api_fixtures
7
8   ##
9   # test all routes which lead to this controller
10   def test_routes
11     assert_routing(
12       { :path => "/api/0.6/amf/read", :method => :post },
13       { :controller => "amf", :action => "amf_read" }
14     )
15     assert_routing(
16       { :path => "/api/0.6/amf/write", :method => :post },
17       { :controller => "amf", :action => "amf_write" }
18     )
19   end
20
21   def test_getpresets
22     [:public_user, :german_user].each do |id|
23       user = users(id)
24
25       amf_content "getpresets", "/1", ["#{user.email}:test", ""]
26       post :amf_read
27       assert_response :success
28       amf_parse_response
29       presets = amf_result("/1")
30
31       assert_equal 15, presets.length
32       assert_equal POTLATCH_PRESETS[0], presets[0]
33       assert_equal POTLATCH_PRESETS[1], presets[1]
34       assert_equal POTLATCH_PRESETS[2], presets[2]
35       assert_equal POTLATCH_PRESETS[3], presets[3]
36       assert_equal POTLATCH_PRESETS[4], presets[4]
37       assert_equal POTLATCH_PRESETS[5], presets[5]
38       assert_equal POTLATCH_PRESETS[6], presets[6]
39       assert_equal POTLATCH_PRESETS[7], presets[7]
40       assert_equal POTLATCH_PRESETS[8], presets[8]
41       assert_equal POTLATCH_PRESETS[9], presets[9]
42       assert_equal POTLATCH_PRESETS[10], presets[10]
43       assert_equal POTLATCH_PRESETS[12], presets[12]
44       assert_equal user.languages.first, presets[13]["__potlatch_locale"]
45     end
46   end
47
48   def test_getway
49     # check a visible way
50     id = current_ways(:visible_way).id
51     amf_content "getway", "/1", [id]
52     post :amf_read
53     assert_response :success
54     amf_parse_response
55     way = amf_result("/1")
56     assert_equal 0, way[0]
57     assert_equal "", way[1]
58     assert_equal id, way[2]
59     assert_equal 1, way[3].length
60     assert_equal 3, way[3][0][2]
61     assert_equal 1, way[5]
62     assert_equal 2, way[6]
63   end
64
65   def test_getway_invisible
66     # check an invisible way
67     id = current_ways(:invisible_way).id
68     amf_content "getway", "/1", [id]
69     post :amf_read
70     assert_response :success
71     amf_parse_response
72     way = amf_result("/1")
73     assert_equal -4, way[0], -4
74     assert_equal "way", way[1]
75     assert_equal id, way[2]
76     assert(way[3].nil?) && way[4].nil? && way[5].nil? && way[6].nil?
77   end
78
79   def test_getway_with_versions
80     # check a way with multiple versions
81     id = current_ways(:way_with_versions).id
82     amf_content "getway", "/1", [id]
83     post :amf_read
84     assert_response :success
85     amf_parse_response
86     way = amf_result("/1")
87     assert_equal 0, way[0]
88     assert_equal "", way[1]
89     assert_equal id, way[2]
90     assert_equal 1, way[3].length
91     assert_equal 15, way[3][0][2]
92     assert_equal 4, way[5]
93     assert_equal 2, way[6]
94   end
95
96   def test_getway_with_duplicate_nodes
97     # check a way with duplicate nodes
98     id = current_ways(:way_with_duplicate_nodes).id
99     amf_content "getway", "/1", [id]
100     post :amf_read
101     assert_response :success
102     amf_parse_response
103     way = amf_result("/1")
104     assert_equal 0, way[0]
105     assert_equal "", way[1]
106     assert_equal id, way[2]
107     assert_equal 2, way[3].length
108     assert_equal 4, way[3][0][2]
109     assert_equal 4, way[3][1][2]
110     assert_equal 1, way[5]
111     assert_equal 2, way[6]
112   end
113
114   def test_getway_with_multiple_nodes
115     # check a way with multiple nodes
116     id = current_ways(:way_with_multiple_nodes).id
117     amf_content "getway", "/1", [id]
118     post :amf_read
119     assert_response :success
120     amf_parse_response
121     way = amf_result("/1")
122     assert_equal 0, way[0]
123     assert_equal "", way[1]
124     assert_equal id, way[2]
125     assert_equal 3, way[3].length
126     assert_equal 4, way[3][0][2]
127     assert_equal 15, way[3][1][2]
128     assert_equal 11, way[3][2][2]
129     assert_equal 2, way[5]
130     assert_equal 2, way[6]
131   end
132
133   def test_getway_nonexistent
134     # check chat a non-existent way is not returned
135     amf_content "getway", "/1", [0]
136     post :amf_read
137     assert_response :success
138     amf_parse_response
139     way = amf_result("/1")
140     assert_equal -4, way[0]
141     assert_equal "way", way[1]
142     assert_equal 0, way[2]
143     assert(way[3].nil?) && way[4].nil? && way[5].nil? && way[6].nil?
144   end
145
146   def test_whichways
147     node = current_nodes(:used_node_1)
148     minlon = node.lon - 0.1
149     minlat = node.lat - 0.1
150     maxlon = node.lon + 0.1
151     maxlat = node.lat + 0.1
152     amf_content "whichways", "/1", [minlon, minlat, maxlon, maxlat]
153     post :amf_read
154     assert_response :success
155     amf_parse_response
156
157     # check contents of message
158     map = amf_result "/1"
159     assert_equal 0, map[0], "map error code should be 0"
160     assert_equal "", map[1], "map error text should be empty"
161
162     # check the formatting of the message
163     assert_equal 5, map.length, "map should have length 5"
164     assert_equal Array, map[2].class, 'map "ways" element should be an array'
165     assert_equal Array, map[3].class, 'map "nodes" element should be an array'
166     assert_equal Array, map[4].class, 'map "relations" element should be an array'
167     map[2].each do |w|
168       assert_equal 2, w.length, "way should be (id, version) pair"
169       assert w[0] == w[0].floor, "way ID should be an integer"
170       assert w[1] == w[1].floor, "way version should be an integer"
171     end
172
173     map[3].each do |n|
174       assert_equal 5, w.length, "node should be (id, lat, lon, [tags], version) tuple"
175       assert n[0] == n[0].floor, "node ID should be an integer"
176       assert n[1] >= minlat - 0.01, "node lat should be greater than min"
177       assert n[1] <= maxlat - 0.01, "node lat should be less than max"
178       assert n[2] >= minlon - 0.01, "node lon should be greater than min"
179       assert n[2] <= maxlon - 0.01, "node lon should be less than max"
180       assert_equal Array, a[3].class, "node tags should be array"
181       assert n[4] == n[4].floor, "node version should be an integer"
182     end
183
184     map[4].each do |r|
185       assert_equal 2, r.length, "relation should be (id, version) pair"
186       assert r[0] == r[0].floor, "relation ID should be an integer"
187       assert r[1] == r[1].floor, "relation version should be an integer"
188     end
189
190     # TODO: looks like amf_controller changed since this test was written
191     # so someone who knows what they're doing should check this!
192     ways = map[2].collect { |x| x[0] }
193     assert ways.include?(current_ways(:used_way).id),
194            "map should include used way"
195     assert !ways.include?(current_ways(:invisible_way).id),
196            "map should not include deleted way"
197   end
198
199   ##
200   # checks that too-large a bounding box will not be served.
201   def test_whichways_toobig
202     bbox = [-0.1, -0.1, 1.1, 1.1]
203     check_bboxes_are_bad [bbox] do |map, _bbox|
204       assert_boundary_error map, " The server said: The maximum bbox size is 0.25, and your request was too large. Either request a smaller area, or use planet.osm"
205     end
206   end
207
208   ##
209   # checks that an invalid bounding box will not be served. in this case
210   # one with max < min latitudes.
211   #
212   # NOTE: the controller expands the bbox by 0.01 in each direction!
213   def test_whichways_badlat
214     bboxes = [[0, 0.1, 0.1, 0], [-0.1, 80, 0.1, 70], [0.24, 54.35, 0.25, 54.33]]
215     check_bboxes_are_bad bboxes do |map, bbox|
216       assert_boundary_error map, " The server said: The minimum latitude must be less than the maximum latitude, but it wasn't", bbox.inspect
217     end
218   end
219
220   ##
221   # same as test_whichways_badlat, but for longitudes
222   #
223   # NOTE: the controller expands the bbox by 0.01 in each direction!
224   def test_whichways_badlon
225     bboxes = [[80, -0.1, 70, 0.1], [54.35, 0.24, 54.33, 0.25]]
226     check_bboxes_are_bad bboxes do |map, bbox|
227       assert_boundary_error map, " The server said: The minimum longitude must be less than the maximum longitude, but it wasn't", bbox.inspect
228     end
229   end
230
231   def test_whichways_deleted
232     node = current_nodes(:used_node_1)
233     minlon = node.lon - 0.1
234     minlat = node.lat - 0.1
235     maxlon = node.lon + 0.1
236     maxlat = node.lat + 0.1
237     amf_content "whichways_deleted", "/1", [minlon, minlat, maxlon, maxlat]
238     post :amf_read
239     assert_response :success
240     amf_parse_response
241
242     # check contents of message
243     map = amf_result "/1"
244     assert_equal 0, map[0], "first map element should be 0"
245     assert_equal "", map[1], "second map element should be an empty string"
246     assert_equal Array, map[2].class, "third map element should be an array"
247     # TODO: looks like amf_controller changed since this test was written
248     # so someone who knows what they're doing should check this!
249     assert !map[2].include?(current_ways(:used_way).id),
250            "map should not include used way"
251     assert map[2].include?(current_ways(:invisible_way).id),
252            "map should include deleted way"
253   end
254
255   def test_whichways_deleted_toobig
256     bbox = [-0.1, -0.1, 1.1, 1.1]
257     amf_content "whichways_deleted", "/1", bbox
258     post :amf_read
259     assert_response :success
260     amf_parse_response
261
262     map = amf_result "/1"
263     assert_deleted_boundary_error map, " The server said: The maximum bbox size is 0.25, and your request was too large. Either request a smaller area, or use planet.osm"
264   end
265
266   def test_getrelation
267     id = current_relations(:visible_relation).id
268     amf_content "getrelation", "/1", [id]
269     post :amf_read
270     assert_response :success
271     amf_parse_response
272     rel = amf_result("/1")
273     assert_equal rel[0], 0
274     assert_equal rel[2], id
275   end
276
277   def test_getrelation_invisible
278     id = current_relations(:invisible_relation).id
279     amf_content "getrelation", "/1", [id]
280     post :amf_read
281     assert_response :success
282     amf_parse_response
283     rel = amf_result("/1")
284     assert_equal rel[0], -4
285     assert_equal rel[1], "relation"
286     assert_equal rel[2], id
287     assert(rel[3].nil?) && rel[4].nil?
288   end
289
290   def test_getrelation_nonexistent
291     id = 0
292     amf_content "getrelation", "/1", [id]
293     post :amf_read
294     assert_response :success
295     amf_parse_response
296     rel = amf_result("/1")
297     assert_equal rel[0], -4
298     assert_equal rel[1], "relation"
299     assert_equal rel[2], id
300     assert(rel[3].nil?) && rel[4].nil?
301   end
302
303   def test_getway_old
304     # try to get the last visible version (specified by <0) (should be current version)
305     latest = current_ways(:way_with_versions)
306     # NOTE: looks from the API changes that this now expects a timestamp
307     # instead of a version number...
308     # try to get version 1
309     v1 = ways(:way_with_versions_v2)
310     { latest.id => "",
311       v1.way_id => v1.timestamp.strftime("%d %b %Y, %H:%M:%S")
312     }.each do |id, t|
313       amf_content "getway_old", "/1", [id, t]
314       post :amf_read
315       assert_response :success
316       amf_parse_response
317       returned_way = amf_result("/1")
318       assert_equal 0, returned_way[0]
319       assert_equal id, returned_way[2]
320       # API returns the *latest* version, even for old ways...
321       assert_equal latest.version, returned_way[5]
322     end
323   end
324
325   ##
326   # test that the server doesn't fall over when rubbish is passed
327   # into the method args.
328   def test_getway_old_invalid
329     way_id = current_ways(:way_with_versions).id
330     { "foo"  => "bar",
331       way_id => "not a date",
332       way_id => "2009-03-25 00:00:00", # <- wrong format
333       way_id => "0 Jan 2009 00:00:00", # <- invalid date
334       -1     => "1 Jan 2009 00:00:00"  # <- invalid ID
335     }.each do |id, t|
336       amf_content "getway_old", "/1", [id, t]
337       post :amf_read
338       assert_response :success
339       amf_parse_response
340       returned_way = amf_result("/1")
341       assert_equal -1, returned_way[0]
342       assert returned_way[3].nil?
343       assert returned_way[4].nil?
344       assert returned_way[5].nil?
345     end
346   end
347
348   def test_getway_old_nonexistent
349     # try to get the last version-10 (shoudn't exist)
350     v1 = ways(:way_with_versions_v1)
351     # try to get last visible version of non-existent way
352     # try to get specific version of non-existent way
353     [[0, ""],
354      [0, "1 Jan 1970, 00:00:00"],
355      [v1.way_id, (v1.timestamp - 10).strftime("%d %b %Y, %H:%M:%S")]
356     ].each do |id, t|
357       amf_content "getway_old", "/1", [id, t]
358       post :amf_read
359       assert_response :success
360       amf_parse_response
361       returned_way = amf_result("/1")
362       assert_equal -1, returned_way[0]
363       assert returned_way[3].nil?
364       assert returned_way[4].nil?
365       assert returned_way[5].nil?
366     end
367   end
368
369   def test_getway_old_invisible
370     v1 = ways(:invisible_way)
371     # try to get deleted version
372     [[v1.way_id, (v1.timestamp + 10).strftime("%d %b %Y, %H:%M:%S")]
373     ].each do |id, t|
374       amf_content "getway_old", "/1", [id, t]
375       post :amf_read
376       assert_response :success
377       amf_parse_response
378       returned_way = amf_result("/1")
379       assert_equal -1, returned_way[0]
380       assert returned_way[3].nil?
381       assert returned_way[4].nil?
382       assert returned_way[5].nil?
383     end
384   end
385
386   def test_getway_history
387     latest = current_ways(:way_with_versions)
388     oldest = ways(:way_with_versions_v1)
389
390     amf_content "getway_history", "/1", [latest.id]
391     post :amf_read
392     assert_response :success
393     amf_parse_response
394     history = amf_result("/1")
395
396     # ['way',wayid,history]
397     assert_equal "way", history[0]
398     assert_equal latest.id, history[1]
399     # We use dates rather than version numbers here, because you might
400     # have moved a node within a way (i.e. way version not incremented).
401     # The timestamp is +1 because we say "give me the revision of 15:33:02",
402     # but that might actually include changes at 15:33:02.457.
403     assert_equal (latest.timestamp + 1).strftime("%d %b %Y, %H:%M:%S"), history[2].first[0]
404     assert_equal (oldest.timestamp + 1).strftime("%d %b %Y, %H:%M:%S"), history[2].last[0]
405   end
406
407   def test_getway_history_nonexistent
408     amf_content "getway_history", "/1", [0]
409     post :amf_read
410     assert_response :success
411     amf_parse_response
412     history = amf_result("/1")
413
414     # ['way',wayid,history]
415     assert_equal history[0], "way"
416     assert_equal history[1], 0
417     assert history[2].empty?
418   end
419
420   def test_getnode_history
421     latest = current_nodes(:node_with_versions)
422     amf_content "getnode_history", "/1", [latest.id]
423     post :amf_read
424     assert_response :success
425     amf_parse_response
426     history = amf_result("/1")
427
428     # ['node',nodeid,history]
429     # note that (as per getway_history) we actually round up
430     # to the next second
431     assert_equal history[0], "node",
432                  'first element should be "node"'
433     assert_equal history[1], latest.id,
434                  "second element should be the input node ID"
435     assert_equal history[2].first[0],
436                  (latest.timestamp + 1).strftime("%d %b %Y, %H:%M:%S"),
437                  "first element in third element (array) should be the latest version"
438     assert_equal history[2].last[0],
439                  (nodes(:node_with_versions_v1).timestamp + 1).strftime("%d %b %Y, %H:%M:%S"),
440                  "last element in third element (array) should be the initial version"
441   end
442
443   def test_getnode_history_nonexistent
444     amf_content "getnode_history", "/1", [0]
445     post :amf_read
446     assert_response :success
447     amf_parse_response
448     history = amf_result("/1")
449
450     # ['node',nodeid,history]
451     assert_equal history[0], "node"
452     assert_equal history[1], 0
453     assert history[2].empty?
454   end
455
456   def test_findgpx_bad_user
457     amf_content "findgpx", "/1", [1, "test@example.com:wrong"]
458     post :amf_read
459     assert_response :success
460     amf_parse_response
461     result = amf_result("/1")
462
463     assert_equal 2, result.length
464     assert_equal -1, result[0]
465     assert_match /must be logged in/, result[1]
466
467     amf_content "findgpx", "/1", [1, "blocked@openstreetmap.org:test"]
468     post :amf_read
469     assert_response :success
470     amf_parse_response
471     result = amf_result("/1")
472
473     assert_equal 2, result.length
474     assert_equal -1, result[0]
475     assert_match /access to the API has been blocked/, result[1]
476   end
477
478   def test_findgpx_by_id
479     trace = gpx_files(:anon_trace_file)
480
481     amf_content "findgpx", "/1", [trace.id, "test@example.com:test"]
482     post :amf_read
483     assert_response :success
484     amf_parse_response
485     result = amf_result("/1")
486
487     assert_equal 3, result.length
488     assert_equal 0, result[0]
489     assert_equal "", result[1]
490     traces = result[2]
491     assert_equal 1, traces.length
492     assert_equal 3, traces[0].length
493     assert_equal trace.id, traces[0][0]
494     assert_equal trace.name, traces[0][1]
495     assert_equal trace.description, traces[0][2]
496   end
497
498   def test_findgpx_by_name
499     amf_content "findgpx", "/1", ["Trace", "test@example.com:test"]
500     post :amf_read
501     assert_response :success
502     amf_parse_response
503     result = amf_result("/1")
504
505     # find by name fails as it uses mysql text search syntax...
506     assert_equal 2, result.length
507     assert_equal -2, result[0]
508   end
509
510   def test_findrelations_by_id
511     relation = current_relations(:relation_with_versions)
512
513     amf_content "findrelations", "/1", [relation.id]
514     post :amf_read
515     assert_response :success
516     amf_parse_response
517     result = amf_result("/1")
518
519     assert_equal 1, result.length
520     assert_equal 4, result[0].length
521     assert_equal relation.id, result[0][0]
522     assert_equal relation.tags, result[0][1]
523     assert_equal relation.members, result[0][2]
524     assert_equal relation.version, result[0][3]
525
526     amf_content "findrelations", "/1", [999999]
527     post :amf_read
528     assert_response :success
529     amf_parse_response
530     result = amf_result("/1")
531
532     assert_equal 0, result.length
533   end
534
535   def test_findrelations_by_tags
536     visible_relation = current_relations(:visible_relation)
537     used_relation = current_relations(:used_relation)
538
539     amf_content "findrelations", "/1", ["yes"]
540     post :amf_read
541     assert_response :success
542     amf_parse_response
543     result = amf_result("/1").sort
544
545     assert_equal 2, result.length
546     assert_equal 4, result[0].length
547     assert_equal visible_relation.id, result[0][0]
548     assert_equal visible_relation.tags, result[0][1]
549     assert_equal visible_relation.members, result[0][2]
550     assert_equal visible_relation.version, result[0][3]
551     assert_equal 4, result[1].length
552     assert_equal used_relation.id, result[1][0]
553     assert_equal used_relation.tags, result[1][1]
554     assert_equal used_relation.members, result[1][2]
555     assert_equal used_relation.version, result[1][3]
556
557     amf_content "findrelations", "/1", ["no"]
558     post :amf_read
559     assert_response :success
560     amf_parse_response
561     result = amf_result("/1").sort
562
563     assert_equal 0, result.length
564   end
565
566   def test_getpoi_without_timestamp
567     node = current_nodes(:node_with_versions)
568
569     amf_content "getpoi", "/1", [node.id, ""]
570     post :amf_read
571     assert_response :success
572     amf_parse_response
573     result = amf_result("/1")
574
575     assert_equal 7, result.length
576     assert_equal 0, result[0]
577     assert_equal "", result[1]
578     assert_equal node.id, result[2]
579     assert_equal node.lon, result[3]
580     assert_equal node.lat, result[4]
581     assert_equal node.tags, result[5]
582     assert_equal node.version, result[6]
583
584     amf_content "getpoi", "/1", [999999, ""]
585     post :amf_read
586     assert_response :success
587     amf_parse_response
588     result = amf_result("/1")
589
590     assert_equal 3, result.length
591     assert_equal -4, result[0]
592     assert_equal "node", result[1]
593     assert_equal 999999, result[2]
594   end
595
596   def test_getpoi_with_timestamp
597     node = nodes(:node_with_versions_v2)
598     current_node = current_nodes(:node_with_versions)
599
600     amf_content "getpoi", "/1", [node.node_id, node.timestamp.xmlschema]
601     post :amf_read
602     assert_response :success
603     amf_parse_response
604     result = amf_result("/1")
605
606     assert_equal 7, result.length
607     assert_equal 0, result[0]
608     assert_equal "", result[1]
609     assert_equal node.node_id, result[2]
610     assert_equal node.lon, result[3]
611     assert_equal node.lat, result[4]
612     assert_equal node.tags, result[5]
613     assert_equal current_node.version, result[6]
614
615     amf_content "getpoi", "/1", [node.node_id, "2000-01-01T00:00:00Z"]
616     post :amf_read
617     assert_response :success
618     amf_parse_response
619     result = amf_result("/1")
620
621     assert_equal 3, result.length
622     assert_equal -4, result[0]
623     assert_equal "node", result[1]
624     assert_equal node.node_id, result[2]
625
626     amf_content "getpoi", "/1", [999999, Time.now.xmlschema]
627     post :amf_read
628     assert_response :success
629     amf_parse_response
630     result = amf_result("/1")
631
632     assert_equal 3, result.length
633     assert_equal -4, result[0]
634     assert_equal "node", result[1]
635     assert_equal 999999, result[2]
636   end
637
638   # ************************************************************
639   # AMF Write tests
640
641   # check that we can update a poi
642   def test_putpoi_update_valid
643     nd = current_nodes(:visible_node)
644     cs_id = changesets(:public_user_first_change).id
645     amf_content "putpoi", "/1", ["test@example.com:test", cs_id, nd.version, nd.id, nd.lon, nd.lat, nd.tags, nd.visible]
646     post :amf_write
647     assert_response :success
648     amf_parse_response
649     result = amf_result("/1")
650
651     assert_equal 5, result.size
652     assert_equal 0, result[0]
653     assert_equal "", result[1]
654     assert_equal nd.id, result[2]
655     assert_equal nd.id, result[3]
656     assert_equal nd.version + 1, result[4]
657
658     # Now try to update again, with a different lat/lon, using the updated version number
659     lat = nd.lat + 0.1
660     lon = nd.lon - 0.1
661     amf_content "putpoi", "/2", ["test@example.com:test", cs_id, nd.version + 1, nd.id, lon, lat, nd.tags, nd.visible]
662     post :amf_write
663     assert_response :success
664     amf_parse_response
665     result = amf_result("/2")
666
667     assert_equal 5, result.size
668     assert_equal 0, result[0]
669     assert_equal "", result[1]
670     assert_equal nd.id, result[2]
671     assert_equal nd.id, result[3]
672     assert_equal nd.version + 2, result[4]
673   end
674
675   # Check that we can create a no valid poi
676   # Using similar method for the node controller test
677   def test_putpoi_create_valid
678     # This node has no tags
679
680     # create a node with random lat/lon
681     lat = rand(100) - 50 + rand
682     lon = rand(100) - 50 + rand
683     # normal user has a changeset open
684     changeset = changesets(:public_user_first_change)
685
686     amf_content "putpoi", "/1", ["test@example.com:test", changeset.id, nil, nil, lon, lat, {}, nil]
687     post :amf_write
688     assert_response :success
689     amf_parse_response
690     result = amf_result("/1")
691
692     # check the array returned by the amf
693     assert_equal 5, result.size
694     assert_equal 0, result[0], "expected to get the status ok from the amf"
695     assert_equal 0, result[2], "The old id should be 0"
696     assert result[3] > 0, "The new id should be greater than 0"
697     assert_equal 1, result[4], "The new version should be 1"
698
699     # Finally check that the node that was saved has saved the data correctly
700     # in both the current and history tables
701     # First check the current table
702     current_node = Node.find(result[3].to_i)
703     assert_in_delta lat, current_node.lat, 0.00001, "The latitude was not retreieved correctly"
704     assert_in_delta lon, current_node.lon, 0.00001, "The longitude was not retreived correctly"
705     assert_equal 0, current_node.tags.size, "There seems to be a tag that has been added to the node"
706     assert_equal result[4], current_node.version, "The version returned, is different to the one returned by the amf"
707     # Now check the history table
708     historic_nodes = Node.where(:id => result[3])
709     assert_equal 1, historic_nodes.size, "There should only be one historic node created"
710     first_historic_node = historic_nodes.first
711     assert_in_delta lat, first_historic_node.lat, 0.00001, "The latitude was not retreived correctly"
712     assert_in_delta lon, first_historic_node.lon, 0.00001, "The longitude was not retreuved correctly"
713     assert_equal 0, first_historic_node.tags.size, "There seems to be a tag that have been attached to this node"
714     assert_equal result[4], first_historic_node.version, "The version returned, is different to the one returned by the amf"
715
716     ####
717     # This node has some tags
718
719     # create a node with random lat/lon
720     lat = rand(100) - 50 + rand
721     lon = rand(100) - 50 + rand
722     # normal user has a changeset open
723     changeset = changesets(:public_user_first_change)
724
725     amf_content "putpoi", "/2", ["test@example.com:test", changeset.id, nil, nil, lon, lat, { "key" => "value", "ping" => "pong" }, nil]
726     post :amf_write
727     assert_response :success
728     amf_parse_response
729     result = amf_result("/2")
730
731     # check the array returned by the amf
732     assert_equal 5, result.size
733     assert_equal 0, result[0], "Expected to get the status ok in the amf"
734     assert_equal 0, result[2], "The old id should be 0"
735     assert result[3] > 0, "The new id should be greater than 0"
736     assert_equal 1, result[4], "The new version should be 1"
737
738     # Finally check that the node that was saved has saved the data correctly
739     # in both the current and history tables
740     # First check the current table
741     current_node = Node.find(result[3].to_i)
742     assert_in_delta lat, current_node.lat, 0.00001, "The latitude was not retreieved correctly"
743     assert_in_delta lon, current_node.lon, 0.00001, "The longitude was not retreived correctly"
744     assert_equal 2, current_node.tags.size, "There seems to be a tag that has been added to the node"
745     assert_equal({ "key" => "value", "ping" => "pong" }, current_node.tags, "tags are different")
746     assert_equal result[4], current_node.version, "The version returned, is different to the one returned by the amf"
747     # Now check the history table
748     historic_nodes = Node.where(:id => result[3])
749     assert_equal 1, historic_nodes.size, "There should only be one historic node created"
750     first_historic_node = historic_nodes.first
751     assert_in_delta lat, first_historic_node.lat, 0.00001, "The latitude was not retreived correctly"
752     assert_in_delta lon, first_historic_node.lon, 0.00001, "The longitude was not retreuved correctly"
753     assert_equal 2, first_historic_node.tags.size, "There seems to be a tag that have been attached to this node"
754     assert_equal({ "key" => "value", "ping" => "pong" }, first_historic_node.tags, "tags are different")
755     assert_equal result[4], first_historic_node.version, "The version returned, is different to the one returned by the amf"
756   end
757
758   # try creating a POI with rubbish in the tags
759   def test_putpoi_create_with_control_chars
760     # This node has no tags
761
762     # create a node with random lat/lon
763     lat = rand(100) - 50 + rand
764     lon = rand(100) - 50 + rand
765     # normal user has a changeset open
766     changeset = changesets(:public_user_first_change)
767
768     mostly_invalid = (0..31).to_a.map(&:chr).join
769     tags = { "something" => "foo#{mostly_invalid}bar" }
770
771     amf_content "putpoi", "/1", ["test@example.com:test", changeset.id, nil, nil, lon, lat, tags, nil]
772     post :amf_write
773     assert_response :success
774     amf_parse_response
775     result = amf_result("/1")
776
777     # check the array returned by the amf
778     assert_equal 5, result.size
779     assert_equal 0, result[0], "Expected to get the status ok in the amf"
780     assert_equal 0, result[2], "The old id should be 0"
781     assert result[3] > 0, "The new id should be greater than 0"
782     assert_equal 1, result[4], "The new version should be 1"
783
784     # Finally check that the node that was saved has saved the data correctly
785     # in both the current and history tables
786     # First check the current table
787     current_node = Node.find(result[3].to_i)
788     assert_equal 1, current_node.tags.size, "There seems to be a tag that has been added to the node"
789     assert_equal({ "something" => "foo\t\n\rbar" }, current_node.tags, "tags were not fixed correctly")
790     assert_equal result[4], current_node.version, "The version returned, is different to the one returned by the amf"
791   end
792
793   # try creating a POI with rubbish in the tags
794   def test_putpoi_create_with_invalid_utf8
795     # This node has no tags
796
797     # create a node with random lat/lon
798     lat = rand(100) - 50 + rand
799     lon = rand(100) - 50 + rand
800     # normal user has a changeset open
801     changeset = changesets(:public_user_first_change)
802
803     invalid = "\xc0\xc0"
804     tags = { "something" => "foo#{invalid}bar" }
805
806     amf_content "putpoi", "/1", ["test@example.com:test", changeset.id, nil, nil, lon, lat, tags, nil]
807     post :amf_write
808     assert_response :success
809     amf_parse_response
810     result = amf_result("/1")
811
812     assert_equal 2, result.size
813     assert_equal -1, result[0], "Expected to get the status FAIL in the amf"
814     assert_equal "One of the tags is invalid. Linux users may need to upgrade to Flash Player 10.1.", result[1]
815   end
816
817   # try deleting a node
818   def test_putpoi_delete_valid
819     nd = current_nodes(:visible_node)
820     cs_id = changesets(:public_user_first_change).id
821     amf_content "putpoi", "/1", ["test@example.com:test", cs_id, nd.version, nd.id, nd.lon, nd.lat, nd.tags, false]
822     post :amf_write
823     assert_response :success
824     amf_parse_response
825     result = amf_result("/1")
826
827     assert_equal 5, result.size
828     assert_equal 0, result[0]
829     assert_equal "", result[1]
830     assert_equal nd.id, result[2]
831     assert_equal nd.id, result[3]
832     assert_equal nd.version + 1, result[4]
833
834     current_node = Node.find(result[3].to_i)
835     assert_equal false, current_node.visible
836   end
837
838   # try deleting a node that is already deleted
839   def test_putpoi_delete_already_deleted
840     nd = current_nodes(:invisible_node)
841     cs_id = changesets(:public_user_first_change).id
842     amf_content "putpoi", "/1", ["test@example.com:test", cs_id, nd.version, nd.id, nd.lon, nd.lat, nd.tags, false]
843     post :amf_write
844     assert_response :success
845     amf_parse_response
846     result = amf_result("/1")
847
848     assert_equal 3, result.size
849     assert_equal -4, result[0]
850     assert_equal "node", result[1]
851     assert_equal nd.id, result[2]
852   end
853
854   # try deleting a node that has never existed
855   def test_putpoi_delete_not_found
856     cs_id = changesets(:public_user_first_change).id
857     amf_content "putpoi", "/1", ["test@example.com:test", cs_id, 1, 999999, 0, 0, {}, false]
858     post :amf_write
859     assert_response :success
860     amf_parse_response
861     result = amf_result("/1")
862
863     assert_equal 3, result.size
864     assert_equal -4, result[0]
865     assert_equal "node", result[1]
866     assert_equal 999999, result[2]
867   end
868
869   # try setting an invalid location on a node
870   def test_putpoi_invalid_latlon
871     nd = current_nodes(:visible_node)
872     cs_id = changesets(:public_user_first_change).id
873     amf_content "putpoi", "/1", ["test@example.com:test", cs_id, nd.version, nd.id, 200, 100, nd.tags, true]
874     post :amf_write
875     assert_response :success
876     amf_parse_response
877     result = amf_result("/1")
878
879     assert_equal 2, result.size
880     assert_equal -2, result[0]
881     assert_match /Node is not in the world/, result[1]
882   end
883
884   # check that we can create a way
885   def test_putway_create_valid
886     cs_id = changesets(:public_user_first_change).id
887
888     amf_content "putway", "/1", ["test@example.com:test", cs_id, 0, -1, [1, 4, 7], { "test" => "new" }, [], {}]
889     post :amf_write
890     assert_response :success
891     amf_parse_response
892     result = amf_result("/1")
893     new_way_id = result[3].to_i
894
895     assert_equal 8, result.size
896     assert_equal 0, result[0]
897     assert_equal "", result[1]
898     assert_equal -1, result[2]
899     assert_not_equal -1, result[3]
900     assert_equal({}, result[4])
901     assert_equal 1, result[5]
902     assert_equal({}, result[6])
903     assert_equal({}, result[7])
904
905     new_way = Way.find(new_way_id)
906     assert_equal 1, new_way.version
907     assert_equal [1, 4, 7], new_way.nds
908     assert_equal({ "test" => "new" }, new_way.tags)
909
910     amf_content "putway", "/1", ["test@example.com:test", cs_id, 0, -1, [4, 6, 15, 1], { "test" => "newer" }, [], {}]
911     post :amf_write
912     assert_response :success
913     amf_parse_response
914     result = amf_result("/1")
915     new_way_id = result[3].to_i
916
917     assert_equal 8, result.size
918     assert_equal 0, result[0]
919     assert_equal "", result[1]
920     assert_equal -1, result[2]
921     assert_not_equal -1, result[3]
922     assert_equal({}, result[4])
923     assert_equal 1, result[5]
924     assert_equal({}, result[6])
925     assert_equal({}, result[7])
926
927     new_way = Way.find(new_way_id)
928     assert_equal 1, new_way.version
929     assert_equal [4, 6, 15, 1], new_way.nds
930     assert_equal({ "test" => "newer" }, new_way.tags)
931
932     amf_content "putway", "/1", ["test@example.com:test", cs_id, 0, -1, [4, -1, 6, 15], { "test" => "newest" }, [[4.56, 12.34, -1, 0, { "test" => "new" }], [12.34, 4.56, 6, 1, { "test" => "ok" }]], { 1 => 1 }]
933     post :amf_write
934     assert_response :success
935     amf_parse_response
936     result = amf_result("/1")
937     new_way_id = result[3].to_i
938     new_node_id = result[4]["-1"].to_i
939
940     assert_equal 8, result.size
941     assert_equal 0, result[0]
942     assert_equal "", result[1]
943     assert_equal -1, result[2]
944     assert_not_equal -1, result[3]
945     assert_equal({ "-1" => new_node_id }, result[4])
946     assert_equal 1, result[5]
947     assert_equal({ new_node_id.to_s => 1, "6" => 2 }, result[6])
948     assert_equal({ "1" => 1 }, result[7])
949
950     new_way = Way.find(new_way_id)
951     assert_equal 1, new_way.version
952     assert_equal [4, new_node_id, 6, 15], new_way.nds
953     assert_equal({ "test" => "newest" }, new_way.tags)
954
955     new_node = Node.find(new_node_id)
956     assert_equal 1, new_node.version
957     assert_equal true, new_node.visible
958     assert_equal 4.56, new_node.lon
959     assert_equal 12.34, new_node.lat
960     assert_equal({ "test" => "new" }, new_node.tags)
961
962     changed_node = Node.find(6)
963     assert_equal 2, changed_node.version
964     assert_equal true, changed_node.visible
965     assert_equal 12.34, changed_node.lon
966     assert_equal 4.56, changed_node.lat
967     assert_equal({ "test" => "ok" }, changed_node.tags)
968
969     # node is not deleted because our other ways are using it
970     deleted_node = Node.find(1)
971     assert_equal 1, deleted_node.version
972     assert_equal true, deleted_node.visible
973   end
974
975   # check that we can update a way
976   def test_putway_update_valid
977     way = current_ways(:way_with_multiple_nodes)
978     cs_id = changesets(:public_user_first_change).id
979
980     assert_not_equal({ "test" => "ok" }, way.tags)
981     amf_content "putway", "/1", ["test@example.com:test", cs_id, way.version, way.id, way.nds, { "test" => "ok" }, [], {}]
982     post :amf_write
983     assert_response :success
984     amf_parse_response
985     result = amf_result("/1")
986
987     assert_equal 8, result.size
988     assert_equal 0, result[0]
989     assert_equal "", result[1]
990     assert_equal way.id, result[2]
991     assert_equal way.id, result[3]
992     assert_equal({}, result[4])
993     assert_equal way.version + 1, result[5]
994     assert_equal({}, result[6])
995     assert_equal({}, result[7])
996
997     new_way = Way.find(way.id)
998     assert_equal way.version + 1, new_way.version
999     assert_equal way.nds, new_way.nds
1000     assert_equal({ "test" => "ok" }, new_way.tags)
1001
1002     assert_not_equal [4, 6, 15, 1], way.tags
1003     amf_content "putway", "/1", ["test@example.com:test", cs_id, way.version + 1, way.id, [4, 6, 15, 1], way.tags, [], {}]
1004     post :amf_write
1005     assert_response :success
1006     amf_parse_response
1007     result = amf_result("/1")
1008
1009     assert_equal 8, result.size
1010     assert_equal 0, result[0]
1011     assert_equal "", result[1]
1012     assert_equal way.id, result[2]
1013     assert_equal way.id, result[3]
1014     assert_equal({}, result[4])
1015     assert_equal way.version + 2, result[5]
1016     assert_equal({}, result[6])
1017     assert_equal({}, result[7])
1018
1019     new_way = Way.find(way.id)
1020     assert_equal way.version + 2, new_way.version
1021     assert_equal [4, 6, 15, 1], new_way.nds
1022     assert_equal way.tags, new_way.tags
1023
1024     amf_content "putway", "/1", ["test@example.com:test", cs_id, way.version + 2, way.id, [4, -1, 6, 15], way.tags, [[4.56, 12.34, -1, 0, { "test" => "new" }], [12.34, 4.56, 6, 1, { "test" => "ok" }]], { 1 => 1 }]
1025     post :amf_write
1026     assert_response :success
1027     amf_parse_response
1028     result = amf_result("/1")
1029     new_node_id = result[4]["-1"].to_i
1030
1031     assert_equal 8, result.size
1032     assert_equal 0, result[0]
1033     assert_equal "", result[1]
1034     assert_equal way.id, result[2]
1035     assert_equal way.id, result[3]
1036     assert_equal({ "-1" => new_node_id }, result[4])
1037     assert_equal way.version + 3, result[5]
1038     assert_equal({ new_node_id.to_s => 1, "6" => 2 }, result[6])
1039     assert_equal({ "1" => 1 }, result[7])
1040
1041     new_way = Way.find(way.id)
1042     assert_equal way.version + 3, new_way.version
1043     assert_equal [4, new_node_id, 6, 15], new_way.nds
1044     assert_equal way.tags, new_way.tags
1045
1046     new_node = Node.find(new_node_id)
1047     assert_equal 1, new_node.version
1048     assert_equal true, new_node.visible
1049     assert_equal 4.56, new_node.lon
1050     assert_equal 12.34, new_node.lat
1051     assert_equal({ "test" => "new" }, new_node.tags)
1052
1053     changed_node = Node.find(6)
1054     assert_equal 2, changed_node.version
1055     assert_equal true, changed_node.visible
1056     assert_equal 12.34, changed_node.lon
1057     assert_equal 4.56, changed_node.lat
1058     assert_equal({ "test" => "ok" }, changed_node.tags)
1059
1060     deleted_node = Node.find(1)
1061     assert_equal 2, deleted_node.version
1062     assert_equal false, deleted_node.visible
1063   end
1064
1065   # check that we can delete a way
1066   def test_deleteway_valid
1067     way = current_ways(:way_with_multiple_nodes)
1068     nodes = way.nodes.each_with_object({}) { |n, ns| ns[n.id] = n.version }
1069     cs_id = changesets(:public_user_first_change).id
1070
1071     amf_content "deleteway", "/1", ["test@example.com:test", cs_id, way.id, way.version, nodes]
1072     post :amf_write
1073     assert_response :success
1074     amf_parse_response
1075     result = amf_result("/1")
1076
1077     assert_equal 5, result.size
1078     assert_equal 0, result[0]
1079     assert_equal "", result[1]
1080     assert_equal way.id, result[2]
1081     assert_equal way.version + 1, result[3]
1082     assert_equal({ "11" => 2 }, result[4])
1083
1084     new_way = Way.find(way.id)
1085     assert_equal way.version + 1, new_way.version
1086     assert_equal false, new_way.visible
1087
1088     way.nds.each do |node_id|
1089       assert_equal result[4][node_id.to_s].nil?, Node.find(node_id).visible
1090     end
1091   end
1092
1093   # check that we can't delete a way that is in use
1094   def test_deleteway_inuse
1095     way = current_ways(:used_way)
1096     nodes = way.nodes.each_with_object({}) { |n, ns| ns[n.id] = n.version }
1097     cs_id = changesets(:public_user_first_change).id
1098
1099     amf_content "deleteway", "/1", ["test@example.com:test", cs_id, way.id, way.version, nodes]
1100     post :amf_write
1101     assert_response :success
1102     amf_parse_response
1103     result = amf_result("/1")
1104
1105     assert_equal 2, result.size
1106     assert_equal -1, result[0]
1107     assert_match /Way #{way.id} is still used/, result[1]
1108
1109     new_way = Way.find(way.id)
1110     assert_equal way.version, new_way.version
1111     assert_equal true, new_way.visible
1112
1113     way.nds.each do |node_id|
1114       assert_equal true, Node.find(node_id).visible
1115     end
1116   end
1117
1118   # check that we can create a relation
1119   def test_putrelation_create_valid
1120     cs_id = changesets(:public_user_first_change).id
1121
1122     amf_content "putrelation", "/1", ["test@example.com:test", cs_id, 0, -1, { "test" => "new" }, [["Node", 3, "node"], ["Way", 7, "way"], ["Relation", 1, "relation"]], true]
1123     post :amf_write
1124     assert_response :success
1125     amf_parse_response
1126     result = amf_result("/1")
1127     new_relation_id = result[3].to_i
1128
1129     assert_equal 5, result.size
1130     assert_equal 0, result[0]
1131     assert_equal "", result[1]
1132     assert_equal -1, result[2]
1133     assert_not_equal -1, result[3]
1134     assert_equal 1, result[4]
1135
1136     new_relation = Relation.find(new_relation_id)
1137     assert_equal 1, new_relation.version
1138     assert_equal [["Node", 3, "node"], ["Way", 7, "way"], ["Relation", 1, "relation"]], new_relation.members
1139     assert_equal({ "test" => "new" }, new_relation.tags)
1140     assert_equal true, new_relation.visible
1141   end
1142
1143   # check that we can update a relation
1144   def test_putrelation_update_valid
1145     relation = current_relations(:visible_relation)
1146     cs_id = changesets(:public_user_first_change).id
1147
1148     assert_not_equal({ "test" => "ok" }, relation.tags)
1149     amf_content "putrelation", "/1", ["test@example.com:test", cs_id, relation.version, relation.id, { "test" => "ok" }, relation.members, true]
1150     post :amf_write
1151     assert_response :success
1152     amf_parse_response
1153     result = amf_result("/1")
1154
1155     assert_equal 5, result.size
1156     assert_equal 0, result[0]
1157     assert_equal "", result[1]
1158     assert_equal relation.id, result[2]
1159     assert_equal relation.id, result[3]
1160     assert_equal relation.version + 1, result[4]
1161
1162     new_relation = Relation.find(relation.id)
1163     assert_equal relation.version + 1, new_relation.version
1164     assert_equal relation.members, new_relation.members
1165     assert_equal({ "test" => "ok" }, new_relation.tags)
1166     assert_equal true, new_relation.visible
1167   end
1168
1169   # check that we can delete a relation
1170   def test_putrelation_delete_valid
1171     relation = current_relations(:visible_relation)
1172     cs_id = changesets(:public_user_first_change).id
1173
1174     amf_content "putrelation", "/1", ["test@example.com:test", cs_id, relation.version, relation.id, relation.tags, relation.members, false]
1175     post :amf_write
1176     assert_response :success
1177     amf_parse_response
1178     result = amf_result("/1")
1179
1180     assert_equal 5, result.size
1181     assert_equal 0, result[0]
1182     assert_equal "", result[1]
1183     assert_equal relation.id, result[2]
1184     assert_equal relation.id, result[3]
1185     assert_equal relation.version + 1, result[4]
1186
1187     new_relation = Relation.find(relation.id)
1188     assert_equal relation.version + 1, new_relation.version
1189     assert_equal [], new_relation.members
1190     assert_equal({}, new_relation.tags)
1191     assert_equal false, new_relation.visible
1192   end
1193
1194   # check that we can't delete a relation that is in use
1195   def test_putrelation_delete_inuse
1196     relation = current_relations(:public_used_relation)
1197     cs_id = changesets(:public_user_first_change).id
1198
1199     amf_content "putrelation", "/1", ["test@example.com:test", cs_id, relation.version, relation.id, relation.tags, relation.members, false]
1200     post :amf_write
1201     assert_response :success
1202     amf_parse_response
1203     result = amf_result("/1")
1204
1205     assert_equal 2, result.size
1206     assert_equal -1, result[0]
1207     assert_match /relation #{relation.id} is used in/, result[1]
1208
1209     new_relation = Relation.find(relation.id)
1210     assert_equal relation.version, new_relation.version
1211     assert_equal relation.members, new_relation.members
1212     assert_equal relation.tags, new_relation.tags
1213     assert_equal true, new_relation.visible
1214   end
1215
1216   # check that we can open a changeset
1217   def test_startchangeset_valid
1218     amf_content "startchangeset", "/1", ["test@example.com:test", { "source" => "new" }, nil, "new", 1]
1219     post :amf_write
1220     assert_response :success
1221     amf_parse_response
1222     result = amf_result("/1")
1223     new_cs_id = result[2].to_i
1224
1225     assert_equal 3, result.size
1226     assert_equal 0, result[0]
1227     assert_equal "", result[1]
1228
1229     cs = Changeset.find(new_cs_id)
1230     assert_equal true, cs.is_open?
1231     assert_equal({ "comment" => "new", "source" => "new" }, cs.tags)
1232
1233     old_cs_id = new_cs_id
1234
1235     amf_content "startchangeset", "/1", ["test@example.com:test", { "source" => "newer" }, old_cs_id, "newer", 1]
1236     post :amf_write
1237     assert_response :success
1238     amf_parse_response
1239     result = amf_result("/1")
1240     new_cs_id = result[2].to_i
1241
1242     assert_not_equal old_cs_id, new_cs_id
1243
1244     assert_equal 3, result.size
1245     assert_equal 0, result[0]
1246     assert_equal "", result[1]
1247
1248     cs = Changeset.find(old_cs_id)
1249     assert_equal false, cs.is_open?
1250     assert_equal({ "comment" => "newer", "source" => "new" }, cs.tags)
1251
1252     cs = Changeset.find(new_cs_id)
1253     assert_equal true, cs.is_open?
1254     assert_equal({ "comment" => "newer", "source" => "newer" }, cs.tags)
1255
1256     old_cs_id = new_cs_id
1257
1258     amf_content "startchangeset", "/1", ["test@example.com:test", {}, old_cs_id, "", 0]
1259     post :amf_write
1260     assert_response :success
1261     amf_parse_response
1262     result = amf_result("/1")
1263
1264     assert_equal 3, result.size
1265     assert_equal 0, result[0]
1266     assert_equal "", result[1]
1267     assert_nil result[2]
1268
1269     cs = Changeset.find(old_cs_id)
1270     assert_equal false, cs.is_open?
1271     assert_equal({ "comment" => "newer", "source" => "newer" }, cs.tags)
1272   end
1273
1274   # check that we can't close somebody elses changeset
1275   def test_startchangeset_invalid_wrong_user
1276     amf_content "startchangeset", "/1", ["test@example.com:test", { "source" => "new" }, nil, "new", 1]
1277     post :amf_write
1278     assert_response :success
1279     amf_parse_response
1280     result = amf_result("/1")
1281     cs_id = result[2].to_i
1282
1283     assert_equal 3, result.size
1284     assert_equal 0, result[0]
1285     assert_equal "", result[1]
1286
1287     cs = Changeset.find(cs_id)
1288     assert_equal true, cs.is_open?
1289     assert_equal({ "comment" => "new", "source" => "new" }, cs.tags)
1290
1291     amf_content "startchangeset", "/1", ["test@openstreetmap.org:test", {}, cs_id, "delete", 0]
1292     post :amf_write
1293     assert_response :success
1294     amf_parse_response
1295     result = amf_result("/1")
1296
1297     assert_equal 2, result.size
1298     assert_equal -2, result[0]
1299     assert_equal "The user doesn't own that changeset", result[1]
1300
1301     cs = Changeset.find(cs_id)
1302     assert_equal true, cs.is_open?
1303     assert_equal({ "comment" => "new", "source" => "new" }, cs.tags)
1304   end
1305
1306   # check that invalid characters are stripped from changeset tags
1307   def test_startchangeset_invalid_xmlchar_comment
1308     invalid = "\035\022"
1309     comment = "foo#{invalid}bar"
1310
1311     amf_content "startchangeset", "/1", ["test@example.com:test", {}, nil, comment, 1]
1312     post :amf_write
1313     assert_response :success
1314     amf_parse_response
1315     result = amf_result("/1")
1316     new_cs_id = result[2].to_i
1317
1318     assert_equal 3, result.size
1319     assert_equal 0, result[0]
1320     assert_equal "", result[1]
1321
1322     cs = Changeset.find(new_cs_id)
1323     assert_equal true, cs.is_open?
1324     assert_equal({ "comment" => "foobar" }, cs.tags)
1325   end
1326
1327   private
1328
1329   # ************************************************************
1330   # AMF Helper functions
1331
1332   # Get the result record for the specified ID
1333   # It's an assertion FAIL if the record does not exist
1334   def amf_result(ref)
1335     assert @amf_result.key?("#{ref}/onResult")
1336     @amf_result["#{ref}/onResult"]
1337   end
1338
1339   # Encode the AMF message to invoke "target" with parameters as
1340   # the passed data. The ref is used to retrieve the results.
1341   def amf_content(target, ref, data)
1342     a, b = 1.divmod(256)
1343     c = StringIO.new
1344     c.write 0.chr + 0.chr   # version 0
1345     c.write 0.chr + 0.chr   # n headers
1346     c.write a.chr + b.chr   # n bodies
1347     c.write AMF.encodestring(target)
1348     c.write AMF.encodestring(ref)
1349     c.write [-1].pack("N")
1350     c.write AMF.encodevalue(data)
1351
1352     @request.env["RAW_POST_DATA"] = c.string
1353   end
1354
1355   # Parses the @response object as an AMF messsage.
1356   # The result is a hash of message_ref => data.
1357   # The attribute @amf_result is initialised to this hash.
1358   def amf_parse_response
1359     req = StringIO.new(@response.body)
1360
1361     req.read(2) # version
1362
1363     # parse through any headers
1364     headers = AMF.getint(req)        # Read number of headers
1365     headers.times do                 # Read each header
1366       AMF.getstring(req)             #  |
1367       req.getc                       #  | skip boolean
1368       AMF.getvalue(req)              #  |
1369     end
1370
1371     # parse through responses
1372     results = {}
1373     bodies = AMF.getint(req)         # Read number of bodies
1374     bodies.times do                  # Read each body
1375       message = AMF.getstring(req)   #  | get message name
1376       AMF.getstring(req)             #  | get index in response sequence
1377       AMF.getlong(req)               #  | get total size in bytes
1378       args = AMF.getvalue(req)       #  | get response (probably an array)
1379       results[message] = args
1380     end
1381     @amf_result = results
1382     results
1383   end
1384
1385   ##
1386   # given an array of bounding boxes (each an array of 4 floats), call the
1387   # AMF "whichways" controller for each and pass the result back to the
1388   # caller's block for assertion testing.
1389   def check_bboxes_are_bad(bboxes)
1390     bboxes.each do |bbox|
1391       amf_content "whichways", "/1", bbox
1392       post :amf_read
1393       assert_response :success
1394       amf_parse_response
1395
1396       # pass the response back to the caller's block to be tested
1397       # against what the caller expected.
1398       map = amf_result "/1"
1399       yield map, bbox
1400     end
1401   end
1402
1403   # this should be what AMF controller returns when the bbox of a
1404   # whichways request is invalid or too large.
1405   def assert_boundary_error(map, msg = nil, error_hint = nil)
1406     expected_map = [-2, "Sorry - I can't get the map for that area.#{msg}"]
1407     assert_equal expected_map, map, "AMF controller should have returned an error. (#{error_hint})"
1408   end
1409
1410   # this should be what AMF controller returns when the bbox of a
1411   # whichways_deleted request is invalid or too large.
1412   def assert_deleted_boundary_error(map, msg = nil, error_hint = nil)
1413     expected_map = [-2, "Sorry - I can't get the map for that area.#{msg}"]
1414     assert_equal expected_map, map, "AMF controller should have returned an error. (#{error_hint})"
1415   end
1416 end