]> git.openstreetmap.org Git - rails.git/blob - test/controllers/amf_controller_test.rb
Added a few more tests
[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") }.each do |id, t|
312       amf_content "getway_old", "/1", [id, t]
313       post :amf_read
314       assert_response :success
315       amf_parse_response
316       returned_way = amf_result("/1")
317       assert_equal 0, returned_way[0]
318       assert_equal id, returned_way[2]
319       # API returns the *latest* version, even for old ways...
320       assert_equal latest.version, returned_way[5]
321     end
322   end
323
324   ##
325   # test that the server doesn't fall over when rubbish is passed
326   # into the method args.
327   def test_getway_old_invalid
328     way_id = current_ways(:way_with_versions).id
329     { "foo"  => "bar",
330       way_id => "not a date",
331       way_id => "2009-03-25 00:00:00",                   # <- wrong format
332       way_id => "0 Jan 2009 00:00:00",                   # <- invalid date
333       -1     => "1 Jan 2009 00:00:00" }.each do |id, t|  # <- invalid
334       amf_content "getway_old", "/1", [id, t]
335       post :amf_read
336       assert_response :success
337       amf_parse_response
338       returned_way = amf_result("/1")
339       assert_equal -1, returned_way[0]
340       assert returned_way[3].nil?
341       assert returned_way[4].nil?
342       assert returned_way[5].nil?
343     end
344   end
345
346   def test_getway_old_nonexistent
347     # try to get the last version-10 (shoudn't exist)
348     v1 = ways(:way_with_versions_v1)
349     # try to get last visible version of non-existent way
350     # try to get specific version of non-existent way
351     [[0, ""],
352      [0, "1 Jan 1970, 00:00:00"],
353      [v1.way_id, (v1.timestamp - 10).strftime("%d %b %Y, %H:%M:%S")]].each do |id, t|
354       amf_content "getway_old", "/1", [id, t]
355       post :amf_read
356       assert_response :success
357       amf_parse_response
358       returned_way = amf_result("/1")
359       assert_equal -1, returned_way[0]
360       assert returned_way[3].nil?
361       assert returned_way[4].nil?
362       assert returned_way[5].nil?
363     end
364   end
365
366   def test_getway_old_invisible
367     v1 = ways(:invisible_way)
368     # try to get deleted version
369     [[v1.way_id, (v1.timestamp + 10).strftime("%d %b %Y, %H:%M:%S")]].each do |id, t|
370       amf_content "getway_old", "/1", [id, t]
371       post :amf_read
372       assert_response :success
373       amf_parse_response
374       returned_way = amf_result("/1")
375       assert_equal -1, returned_way[0]
376       assert returned_way[3].nil?
377       assert returned_way[4].nil?
378       assert returned_way[5].nil?
379     end
380   end
381
382   def test_getway_history
383     latest = current_ways(:way_with_versions)
384     oldest = ways(:way_with_versions_v1)
385
386     amf_content "getway_history", "/1", [latest.id]
387     post :amf_read
388     assert_response :success
389     amf_parse_response
390     history = amf_result("/1")
391
392     # ['way',wayid,history]
393     assert_equal "way", history[0]
394     assert_equal latest.id, history[1]
395     # We use dates rather than version numbers here, because you might
396     # have moved a node within a way (i.e. way version not incremented).
397     # The timestamp is +1 because we say "give me the revision of 15:33:02",
398     # but that might actually include changes at 15:33:02.457.
399     assert_equal (latest.timestamp + 1).strftime("%d %b %Y, %H:%M:%S"), history[2].first[0]
400     assert_equal (oldest.timestamp + 1).strftime("%d %b %Y, %H:%M:%S"), history[2].last[0]
401   end
402
403   def test_getway_history_nonexistent
404     amf_content "getway_history", "/1", [0]
405     post :amf_read
406     assert_response :success
407     amf_parse_response
408     history = amf_result("/1")
409
410     # ['way',wayid,history]
411     assert_equal history[0], "way"
412     assert_equal history[1], 0
413     assert history[2].empty?
414   end
415
416   def test_getnode_history
417     latest = current_nodes(:node_with_versions)
418     amf_content "getnode_history", "/1", [latest.id]
419     post :amf_read
420     assert_response :success
421     amf_parse_response
422     history = amf_result("/1")
423
424     # ['node',nodeid,history]
425     # note that (as per getway_history) we actually round up
426     # to the next second
427     assert_equal history[0], "node",
428                  'first element should be "node"'
429     assert_equal history[1], latest.id,
430                  "second element should be the input node ID"
431     assert_equal history[2].first[0],
432                  (latest.timestamp + 1).strftime("%d %b %Y, %H:%M:%S"),
433                  "first element in third element (array) should be the latest version"
434     assert_equal history[2].last[0],
435                  (nodes(:node_with_versions_v1).timestamp + 1).strftime("%d %b %Y, %H:%M:%S"),
436                  "last element in third element (array) should be the initial version"
437   end
438
439   def test_getnode_history_nonexistent
440     amf_content "getnode_history", "/1", [0]
441     post :amf_read
442     assert_response :success
443     amf_parse_response
444     history = amf_result("/1")
445
446     # ['node',nodeid,history]
447     assert_equal history[0], "node"
448     assert_equal history[1], 0
449     assert history[2].empty?
450   end
451
452   def test_findgpx_bad_user
453     amf_content "findgpx", "/1", [1, "test@example.com:wrong"]
454     post :amf_read
455     assert_response :success
456     amf_parse_response
457     result = amf_result("/1")
458
459     assert_equal 2, result.length
460     assert_equal -1, result[0]
461     assert_match /must be logged in/, result[1]
462
463     amf_content "findgpx", "/1", [1, "blocked@openstreetmap.org:test"]
464     post :amf_read
465     assert_response :success
466     amf_parse_response
467     result = amf_result("/1")
468
469     assert_equal 2, result.length
470     assert_equal -1, result[0]
471     assert_match /access to the API has been blocked/, result[1]
472   end
473
474   def test_findgpx_by_id
475     trace = gpx_files(:anon_trace_file)
476
477     amf_content "findgpx", "/1", [trace.id, "test@example.com:test"]
478     post :amf_read
479     assert_response :success
480     amf_parse_response
481     result = amf_result("/1")
482
483     assert_equal 3, result.length
484     assert_equal 0, result[0]
485     assert_equal "", result[1]
486     traces = result[2]
487     assert_equal 1, traces.length
488     assert_equal 3, traces[0].length
489     assert_equal trace.id, traces[0][0]
490     assert_equal trace.name, traces[0][1]
491     assert_equal trace.description, traces[0][2]
492   end
493
494   def test_findgpx_by_name
495     amf_content "findgpx", "/1", ["Trace", "test@example.com:test"]
496     post :amf_read
497     assert_response :success
498     amf_parse_response
499     result = amf_result("/1")
500
501     # find by name fails as it uses mysql text search syntax...
502     assert_equal 2, result.length
503     assert_equal -2, result[0]
504   end
505
506   def test_findrelations_by_id
507     relation = current_relations(:relation_with_versions)
508
509     amf_content "findrelations", "/1", [relation.id]
510     post :amf_read
511     assert_response :success
512     amf_parse_response
513     result = amf_result("/1")
514
515     assert_equal 1, result.length
516     assert_equal 4, result[0].length
517     assert_equal relation.id, result[0][0]
518     assert_equal relation.tags, result[0][1]
519     assert_equal relation.members, result[0][2]
520     assert_equal relation.version, result[0][3]
521
522     amf_content "findrelations", "/1", [999999]
523     post :amf_read
524     assert_response :success
525     amf_parse_response
526     result = amf_result("/1")
527
528     assert_equal 0, result.length
529   end
530
531   def test_findrelations_by_tags
532     visible_relation = current_relations(:visible_relation)
533     used_relation = current_relations(:used_relation)
534
535     amf_content "findrelations", "/1", ["yes"]
536     post :amf_read
537     assert_response :success
538     amf_parse_response
539     result = amf_result("/1").sort
540
541     assert_equal 2, result.length
542     assert_equal 4, result[0].length
543     assert_equal visible_relation.id, result[0][0]
544     assert_equal visible_relation.tags, result[0][1]
545     assert_equal visible_relation.members, result[0][2]
546     assert_equal visible_relation.version, result[0][3]
547     assert_equal 4, result[1].length
548     assert_equal used_relation.id, result[1][0]
549     assert_equal used_relation.tags, result[1][1]
550     assert_equal used_relation.members, result[1][2]
551     assert_equal used_relation.version, result[1][3]
552
553     amf_content "findrelations", "/1", ["no"]
554     post :amf_read
555     assert_response :success
556     amf_parse_response
557     result = amf_result("/1").sort
558
559     assert_equal 0, result.length
560   end
561
562   def test_getpoi_without_timestamp
563     node = current_nodes(:node_with_versions)
564
565     amf_content "getpoi", "/1", [node.id, ""]
566     post :amf_read
567     assert_response :success
568     amf_parse_response
569     result = amf_result("/1")
570
571     assert_equal 7, result.length
572     assert_equal 0, result[0]
573     assert_equal "", result[1]
574     assert_equal node.id, result[2]
575     assert_equal node.lon, result[3]
576     assert_equal node.lat, result[4]
577     assert_equal node.tags, result[5]
578     assert_equal node.version, result[6]
579
580     amf_content "getpoi", "/1", [999999, ""]
581     post :amf_read
582     assert_response :success
583     amf_parse_response
584     result = amf_result("/1")
585
586     assert_equal 3, result.length
587     assert_equal -4, result[0]
588     assert_equal "node", result[1]
589     assert_equal 999999, result[2]
590   end
591
592   def test_getpoi_with_timestamp
593     node = nodes(:node_with_versions_v2)
594     current_node = current_nodes(:node_with_versions)
595
596     amf_content "getpoi", "/1", [node.node_id, node.timestamp.xmlschema]
597     post :amf_read
598     assert_response :success
599     amf_parse_response
600     result = amf_result("/1")
601
602     assert_equal 7, result.length
603     assert_equal 0, result[0]
604     assert_equal "", result[1]
605     assert_equal node.node_id, result[2]
606     assert_equal node.lon, result[3]
607     assert_equal node.lat, result[4]
608     assert_equal node.tags, result[5]
609     assert_equal current_node.version, result[6]
610
611     amf_content "getpoi", "/1", [node.node_id, "2000-01-01T00:00:00Z"]
612     post :amf_read
613     assert_response :success
614     amf_parse_response
615     result = amf_result("/1")
616
617     assert_equal 3, result.length
618     assert_equal -4, result[0]
619     assert_equal "node", result[1]
620     assert_equal node.node_id, result[2]
621
622     amf_content "getpoi", "/1", [999999, Time.now.xmlschema]
623     post :amf_read
624     assert_response :success
625     amf_parse_response
626     result = amf_result("/1")
627
628     assert_equal 3, result.length
629     assert_equal -4, result[0]
630     assert_equal "node", result[1]
631     assert_equal 999999, result[2]
632   end
633
634   # ************************************************************
635   # AMF Write tests
636
637   # check that we can update a poi
638   def test_putpoi_update_valid
639     nd = current_nodes(:visible_node)
640     cs_id = changesets(:public_user_first_change).id
641     amf_content "putpoi", "/1", ["test@example.com:test", cs_id, nd.version, nd.id, nd.lon, nd.lat, nd.tags, nd.visible]
642     post :amf_write
643     assert_response :success
644     amf_parse_response
645     result = amf_result("/1")
646
647     assert_equal 5, result.size
648     assert_equal 0, result[0]
649     assert_equal "", result[1]
650     assert_equal nd.id, result[2]
651     assert_equal nd.id, result[3]
652     assert_equal nd.version + 1, result[4]
653
654     # Now try to update again, with a different lat/lon, using the updated version number
655     lat = nd.lat + 0.1
656     lon = nd.lon - 0.1
657     amf_content "putpoi", "/2", ["test@example.com:test", cs_id, nd.version + 1, nd.id, lon, lat, nd.tags, nd.visible]
658     post :amf_write
659     assert_response :success
660     amf_parse_response
661     result = amf_result("/2")
662
663     assert_equal 5, result.size
664     assert_equal 0, result[0]
665     assert_equal "", result[1]
666     assert_equal nd.id, result[2]
667     assert_equal nd.id, result[3]
668     assert_equal nd.version + 2, result[4]
669   end
670
671   # Check that we can create a no valid poi
672   # Using similar method for the node controller test
673   def test_putpoi_create_valid
674     # This node has no tags
675
676     # create a node with random lat/lon
677     lat = rand(100) - 50 + rand
678     lon = rand(100) - 50 + rand
679     # normal user has a changeset open
680     changeset = changesets(:public_user_first_change)
681
682     amf_content "putpoi", "/1", ["test@example.com:test", changeset.id, nil, nil, lon, lat, {}, nil]
683     post :amf_write
684     assert_response :success
685     amf_parse_response
686     result = amf_result("/1")
687
688     # check the array returned by the amf
689     assert_equal 5, result.size
690     assert_equal 0, result[0], "expected to get the status ok from the amf"
691     assert_equal 0, result[2], "The old id should be 0"
692     assert result[3] > 0, "The new id should be greater than 0"
693     assert_equal 1, result[4], "The new version should be 1"
694
695     # Finally check that the node that was saved has saved the data correctly
696     # in both the current and history tables
697     # First check the current table
698     current_node = Node.find(result[3].to_i)
699     assert_in_delta lat, current_node.lat, 0.00001, "The latitude was not retreieved correctly"
700     assert_in_delta lon, current_node.lon, 0.00001, "The longitude was not retreived correctly"
701     assert_equal 0, current_node.tags.size, "There seems to be a tag that has been added to the node"
702     assert_equal result[4], current_node.version, "The version returned, is different to the one returned by the amf"
703     # Now check the history table
704     historic_nodes = Node.where(:id => result[3])
705     assert_equal 1, historic_nodes.size, "There should only be one historic node created"
706     first_historic_node = historic_nodes.first
707     assert_in_delta lat, first_historic_node.lat, 0.00001, "The latitude was not retreived correctly"
708     assert_in_delta lon, first_historic_node.lon, 0.00001, "The longitude was not retreuved correctly"
709     assert_equal 0, first_historic_node.tags.size, "There seems to be a tag that have been attached to this node"
710     assert_equal result[4], first_historic_node.version, "The version returned, is different to the one returned by the amf"
711
712     ####
713     # This node has some tags
714
715     # create a node with random lat/lon
716     lat = rand(100) - 50 + rand
717     lon = rand(100) - 50 + rand
718     # normal user has a changeset open
719     changeset = changesets(:public_user_first_change)
720
721     amf_content "putpoi", "/2", ["test@example.com:test", changeset.id, nil, nil, lon, lat, { "key" => "value", "ping" => "pong" }, nil]
722     post :amf_write
723     assert_response :success
724     amf_parse_response
725     result = amf_result("/2")
726
727     # check the array returned by the amf
728     assert_equal 5, result.size
729     assert_equal 0, result[0], "Expected to get the status ok in the amf"
730     assert_equal 0, result[2], "The old id should be 0"
731     assert result[3] > 0, "The new id should be greater than 0"
732     assert_equal 1, result[4], "The new version should be 1"
733
734     # Finally check that the node that was saved has saved the data correctly
735     # in both the current and history tables
736     # First check the current table
737     current_node = Node.find(result[3].to_i)
738     assert_in_delta lat, current_node.lat, 0.00001, "The latitude was not retreieved correctly"
739     assert_in_delta lon, current_node.lon, 0.00001, "The longitude was not retreived correctly"
740     assert_equal 2, current_node.tags.size, "There seems to be a tag that has been added to the node"
741     assert_equal({ "key" => "value", "ping" => "pong" }, current_node.tags, "tags are different")
742     assert_equal result[4], current_node.version, "The version returned, is different to the one returned by the amf"
743     # Now check the history table
744     historic_nodes = Node.where(:id => result[3])
745     assert_equal 1, historic_nodes.size, "There should only be one historic node created"
746     first_historic_node = historic_nodes.first
747     assert_in_delta lat, first_historic_node.lat, 0.00001, "The latitude was not retreived correctly"
748     assert_in_delta lon, first_historic_node.lon, 0.00001, "The longitude was not retreuved correctly"
749     assert_equal 2, first_historic_node.tags.size, "There seems to be a tag that have been attached to this node"
750     assert_equal({ "key" => "value", "ping" => "pong" }, first_historic_node.tags, "tags are different")
751     assert_equal result[4], first_historic_node.version, "The version returned, is different to the one returned by the amf"
752   end
753
754   # try creating a POI with rubbish in the tags
755   def test_putpoi_create_with_control_chars
756     # This node has no tags
757
758     # create a node with random lat/lon
759     lat = rand(100) - 50 + rand
760     lon = rand(100) - 50 + rand
761     # normal user has a changeset open
762     changeset = changesets(:public_user_first_change)
763
764     mostly_invalid = (0..31).to_a.map(&:chr).join
765     tags = { "something" => "foo#{mostly_invalid}bar" }
766
767     amf_content "putpoi", "/1", ["test@example.com:test", changeset.id, nil, nil, lon, lat, tags, nil]
768     post :amf_write
769     assert_response :success
770     amf_parse_response
771     result = amf_result("/1")
772
773     # check the array returned by the amf
774     assert_equal 5, result.size
775     assert_equal 0, result[0], "Expected to get the status ok in the amf"
776     assert_equal 0, result[2], "The old id should be 0"
777     assert result[3] > 0, "The new id should be greater than 0"
778     assert_equal 1, result[4], "The new version should be 1"
779
780     # Finally check that the node that was saved has saved the data correctly
781     # in both the current and history tables
782     # First check the current table
783     current_node = Node.find(result[3].to_i)
784     assert_equal 1, current_node.tags.size, "There seems to be a tag that has been added to the node"
785     assert_equal({ "something" => "foo\t\n\rbar" }, current_node.tags, "tags were not fixed correctly")
786     assert_equal result[4], current_node.version, "The version returned, is different to the one returned by the amf"
787   end
788
789   # try creating a POI with rubbish in the tags
790   def test_putpoi_create_with_invalid_utf8
791     # This node has no tags
792
793     # create a node with random lat/lon
794     lat = rand(100) - 50 + rand
795     lon = rand(100) - 50 + rand
796     # normal user has a changeset open
797     changeset = changesets(:public_user_first_change)
798
799     invalid = "\xc0\xc0"
800     tags = { "something" => "foo#{invalid}bar" }
801
802     amf_content "putpoi", "/1", ["test@example.com:test", changeset.id, nil, nil, lon, lat, tags, nil]
803     post :amf_write
804     assert_response :success
805     amf_parse_response
806     result = amf_result("/1")
807
808     assert_equal 2, result.size
809     assert_equal -1, result[0], "Expected to get the status FAIL in the amf"
810     assert_equal "One of the tags is invalid. Linux users may need to upgrade to Flash Player 10.1.", result[1]
811   end
812
813   # try deleting a node
814   def test_putpoi_delete_valid
815     nd = current_nodes(:visible_node)
816     cs_id = changesets(:public_user_first_change).id
817     amf_content "putpoi", "/1", ["test@example.com:test", cs_id, nd.version, nd.id, nd.lon, nd.lat, nd.tags, false]
818     post :amf_write
819     assert_response :success
820     amf_parse_response
821     result = amf_result("/1")
822
823     assert_equal 5, result.size
824     assert_equal 0, result[0]
825     assert_equal "", result[1]
826     assert_equal nd.id, result[2]
827     assert_equal nd.id, result[3]
828     assert_equal nd.version + 1, result[4]
829
830     current_node = Node.find(result[3].to_i)
831     assert_equal false, current_node.visible
832   end
833
834   # try deleting a node that is already deleted
835   def test_putpoi_delete_already_deleted
836     nd = current_nodes(:invisible_node)
837     cs_id = changesets(:public_user_first_change).id
838     amf_content "putpoi", "/1", ["test@example.com:test", cs_id, nd.version, nd.id, nd.lon, nd.lat, nd.tags, false]
839     post :amf_write
840     assert_response :success
841     amf_parse_response
842     result = amf_result("/1")
843
844     assert_equal 3, result.size
845     assert_equal -4, result[0]
846     assert_equal "node", result[1]
847     assert_equal nd.id, result[2]
848   end
849
850   # try deleting a node that has never existed
851   def test_putpoi_delete_not_found
852     cs_id = changesets(:public_user_first_change).id
853     amf_content "putpoi", "/1", ["test@example.com:test", cs_id, 1, 999999, 0, 0, {}, false]
854     post :amf_write
855     assert_response :success
856     amf_parse_response
857     result = amf_result("/1")
858
859     assert_equal 3, result.size
860     assert_equal -4, result[0]
861     assert_equal "node", result[1]
862     assert_equal 999999, result[2]
863   end
864
865   # try setting an invalid location on a node
866   def test_putpoi_invalid_latlon
867     nd = current_nodes(:visible_node)
868     cs_id = changesets(:public_user_first_change).id
869     amf_content "putpoi", "/1", ["test@example.com:test", cs_id, nd.version, nd.id, 200, 100, nd.tags, true]
870     post :amf_write
871     assert_response :success
872     amf_parse_response
873     result = amf_result("/1")
874
875     assert_equal 2, result.size
876     assert_equal -2, result[0]
877     assert_match /Node is not in the world/, result[1]
878   end
879
880   # check that we can create a way
881   def test_putway_create_valid
882     cs_id = changesets(:public_user_first_change).id
883
884     amf_content "putway", "/1", ["test@example.com:test", cs_id, 0, -1, [1, 4, 7], { "test" => "new" }, [], {}]
885     post :amf_write
886     assert_response :success
887     amf_parse_response
888     result = amf_result("/1")
889     new_way_id = result[3].to_i
890
891     assert_equal 8, result.size
892     assert_equal 0, result[0]
893     assert_equal "", result[1]
894     assert_equal -1, result[2]
895     assert_not_equal -1, result[3]
896     assert_equal({}, result[4])
897     assert_equal 1, result[5]
898     assert_equal({}, result[6])
899     assert_equal({}, result[7])
900
901     new_way = Way.find(new_way_id)
902     assert_equal 1, new_way.version
903     assert_equal [1, 4, 7], new_way.nds
904     assert_equal({ "test" => "new" }, new_way.tags)
905
906     amf_content "putway", "/1", ["test@example.com:test", cs_id, 0, -1, [4, 6, 15, 1], { "test" => "newer" }, [], {}]
907     post :amf_write
908     assert_response :success
909     amf_parse_response
910     result = amf_result("/1")
911     new_way_id = result[3].to_i
912
913     assert_equal 8, result.size
914     assert_equal 0, result[0]
915     assert_equal "", result[1]
916     assert_equal -1, result[2]
917     assert_not_equal -1, result[3]
918     assert_equal({}, result[4])
919     assert_equal 1, result[5]
920     assert_equal({}, result[6])
921     assert_equal({}, result[7])
922
923     new_way = Way.find(new_way_id)
924     assert_equal 1, new_way.version
925     assert_equal [4, 6, 15, 1], new_way.nds
926     assert_equal({ "test" => "newer" }, new_way.tags)
927
928     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 }]
929     post :amf_write
930     assert_response :success
931     amf_parse_response
932     result = amf_result("/1")
933     new_way_id = result[3].to_i
934     new_node_id = result[4]["-1"].to_i
935
936     assert_equal 8, result.size
937     assert_equal 0, result[0]
938     assert_equal "", result[1]
939     assert_equal -1, result[2]
940     assert_not_equal -1, result[3]
941     assert_equal({ "-1" => new_node_id }, result[4])
942     assert_equal 1, result[5]
943     assert_equal({ new_node_id.to_s => 1, "6" => 2 }, result[6])
944     assert_equal({ "1" => 1 }, result[7])
945
946     new_way = Way.find(new_way_id)
947     assert_equal 1, new_way.version
948     assert_equal [4, new_node_id, 6, 15], new_way.nds
949     assert_equal({ "test" => "newest" }, new_way.tags)
950
951     new_node = Node.find(new_node_id)
952     assert_equal 1, new_node.version
953     assert_equal true, new_node.visible
954     assert_equal 4.56, new_node.lon
955     assert_equal 12.34, new_node.lat
956     assert_equal({ "test" => "new" }, new_node.tags)
957
958     changed_node = Node.find(6)
959     assert_equal 2, changed_node.version
960     assert_equal true, changed_node.visible
961     assert_equal 12.34, changed_node.lon
962     assert_equal 4.56, changed_node.lat
963     assert_equal({ "test" => "ok" }, changed_node.tags)
964
965     # node is not deleted because our other ways are using it
966     deleted_node = Node.find(1)
967     assert_equal 1, deleted_node.version
968     assert_equal true, deleted_node.visible
969   end
970
971   # check that we can update a way
972   def test_putway_update_valid
973     way = current_ways(:way_with_multiple_nodes)
974     cs_id = changesets(:public_user_first_change).id
975
976     assert_not_equal({ "test" => "ok" }, way.tags)
977     amf_content "putway", "/1", ["test@example.com:test", cs_id, way.version, way.id, way.nds, { "test" => "ok" }, [], {}]
978     post :amf_write
979     assert_response :success
980     amf_parse_response
981     result = amf_result("/1")
982
983     assert_equal 8, result.size
984     assert_equal 0, result[0]
985     assert_equal "", result[1]
986     assert_equal way.id, result[2]
987     assert_equal way.id, result[3]
988     assert_equal({}, result[4])
989     assert_equal way.version + 1, result[5]
990     assert_equal({}, result[6])
991     assert_equal({}, result[7])
992
993     new_way = Way.find(way.id)
994     assert_equal way.version + 1, new_way.version
995     assert_equal way.nds, new_way.nds
996     assert_equal({ "test" => "ok" }, new_way.tags)
997
998     assert_not_equal [4, 6, 15, 1], way.tags
999     amf_content "putway", "/1", ["test@example.com:test", cs_id, way.version + 1, way.id, [4, 6, 15, 1], way.tags, [], {}]
1000     post :amf_write
1001     assert_response :success
1002     amf_parse_response
1003     result = amf_result("/1")
1004
1005     assert_equal 8, result.size
1006     assert_equal 0, result[0]
1007     assert_equal "", result[1]
1008     assert_equal way.id, result[2]
1009     assert_equal way.id, result[3]
1010     assert_equal({}, result[4])
1011     assert_equal way.version + 2, result[5]
1012     assert_equal({}, result[6])
1013     assert_equal({}, result[7])
1014
1015     new_way = Way.find(way.id)
1016     assert_equal way.version + 2, new_way.version
1017     assert_equal [4, 6, 15, 1], new_way.nds
1018     assert_equal way.tags, new_way.tags
1019
1020     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 }]
1021     post :amf_write
1022     assert_response :success
1023     amf_parse_response
1024     result = amf_result("/1")
1025     new_node_id = result[4]["-1"].to_i
1026
1027     assert_equal 8, result.size
1028     assert_equal 0, result[0]
1029     assert_equal "", result[1]
1030     assert_equal way.id, result[2]
1031     assert_equal way.id, result[3]
1032     assert_equal({ "-1" => new_node_id }, result[4])
1033     assert_equal way.version + 3, result[5]
1034     assert_equal({ new_node_id.to_s => 1, "6" => 2 }, result[6])
1035     assert_equal({ "1" => 1 }, result[7])
1036
1037     new_way = Way.find(way.id)
1038     assert_equal way.version + 3, new_way.version
1039     assert_equal [4, new_node_id, 6, 15], new_way.nds
1040     assert_equal way.tags, new_way.tags
1041
1042     new_node = Node.find(new_node_id)
1043     assert_equal 1, new_node.version
1044     assert_equal true, new_node.visible
1045     assert_equal 4.56, new_node.lon
1046     assert_equal 12.34, new_node.lat
1047     assert_equal({ "test" => "new" }, new_node.tags)
1048
1049     changed_node = Node.find(6)
1050     assert_equal 2, changed_node.version
1051     assert_equal true, changed_node.visible
1052     assert_equal 12.34, changed_node.lon
1053     assert_equal 4.56, changed_node.lat
1054     assert_equal({ "test" => "ok" }, changed_node.tags)
1055
1056     deleted_node = Node.find(1)
1057     assert_equal 2, deleted_node.version
1058     assert_equal false, deleted_node.visible
1059   end
1060
1061   # check that we can delete a way
1062   def test_deleteway_valid
1063     way = current_ways(:way_with_multiple_nodes)
1064     nodes = way.nodes.each_with_object({}) { |n, ns| ns[n.id] = n.version }
1065     cs_id = changesets(:public_user_first_change).id
1066
1067     amf_content "deleteway", "/1", ["test@example.com:test", cs_id, way.id, way.version, nodes]
1068     post :amf_write
1069     assert_response :success
1070     amf_parse_response
1071     result = amf_result("/1")
1072
1073     assert_equal 5, result.size
1074     assert_equal 0, result[0]
1075     assert_equal "", result[1]
1076     assert_equal way.id, result[2]
1077     assert_equal way.version + 1, result[3]
1078     assert_equal({ "11" => 2 }, result[4])
1079
1080     new_way = Way.find(way.id)
1081     assert_equal way.version + 1, new_way.version
1082     assert_equal false, new_way.visible
1083
1084     way.nds.each do |node_id|
1085       assert_equal result[4][node_id.to_s].nil?, Node.find(node_id).visible
1086     end
1087   end
1088
1089   # check that we can't delete a way that is in use
1090   def test_deleteway_inuse
1091     way = current_ways(:used_way)
1092     nodes = way.nodes.each_with_object({}) { |n, ns| ns[n.id] = n.version }
1093     cs_id = changesets(:public_user_first_change).id
1094
1095     amf_content "deleteway", "/1", ["test@example.com:test", cs_id, way.id, way.version, nodes]
1096     post :amf_write
1097     assert_response :success
1098     amf_parse_response
1099     result = amf_result("/1")
1100
1101     assert_equal 2, result.size
1102     assert_equal -1, result[0]
1103     assert_match /Way #{way.id} is still used/, result[1]
1104
1105     new_way = Way.find(way.id)
1106     assert_equal way.version, new_way.version
1107     assert_equal true, new_way.visible
1108
1109     way.nds.each do |node_id|
1110       assert_equal true, Node.find(node_id).visible
1111     end
1112   end
1113
1114   # check that we can create a relation
1115   def test_putrelation_create_valid
1116     cs_id = changesets(:public_user_first_change).id
1117
1118     amf_content "putrelation", "/1", ["test@example.com:test", cs_id, 0, -1, { "test" => "new" }, [["Node", 3, "node"], ["Way", 7, "way"], ["Relation", 1, "relation"]], true]
1119     post :amf_write
1120     assert_response :success
1121     amf_parse_response
1122     result = amf_result("/1")
1123     new_relation_id = result[3].to_i
1124
1125     assert_equal 5, result.size
1126     assert_equal 0, result[0]
1127     assert_equal "", result[1]
1128     assert_equal -1, result[2]
1129     assert_not_equal -1, result[3]
1130     assert_equal 1, result[4]
1131
1132     new_relation = Relation.find(new_relation_id)
1133     assert_equal 1, new_relation.version
1134     assert_equal [["Node", 3, "node"], ["Way", 7, "way"], ["Relation", 1, "relation"]], new_relation.members
1135     assert_equal({ "test" => "new" }, new_relation.tags)
1136     assert_equal true, new_relation.visible
1137   end
1138
1139   # check that we can update a relation
1140   def test_putrelation_update_valid
1141     relation = current_relations(:visible_relation)
1142     cs_id = changesets(:public_user_first_change).id
1143
1144     assert_not_equal({ "test" => "ok" }, relation.tags)
1145     amf_content "putrelation", "/1", ["test@example.com:test", cs_id, relation.version, relation.id, { "test" => "ok" }, relation.members, true]
1146     post :amf_write
1147     assert_response :success
1148     amf_parse_response
1149     result = amf_result("/1")
1150
1151     assert_equal 5, result.size
1152     assert_equal 0, result[0]
1153     assert_equal "", result[1]
1154     assert_equal relation.id, result[2]
1155     assert_equal relation.id, result[3]
1156     assert_equal relation.version + 1, result[4]
1157
1158     new_relation = Relation.find(relation.id)
1159     assert_equal relation.version + 1, new_relation.version
1160     assert_equal relation.members, new_relation.members
1161     assert_equal({ "test" => "ok" }, new_relation.tags)
1162     assert_equal true, new_relation.visible
1163   end
1164
1165   # check that we can delete a relation
1166   def test_putrelation_delete_valid
1167     relation = current_relations(:visible_relation)
1168     cs_id = changesets(:public_user_first_change).id
1169
1170     amf_content "putrelation", "/1", ["test@example.com:test", cs_id, relation.version, relation.id, relation.tags, relation.members, false]
1171     post :amf_write
1172     assert_response :success
1173     amf_parse_response
1174     result = amf_result("/1")
1175
1176     assert_equal 5, result.size
1177     assert_equal 0, result[0]
1178     assert_equal "", result[1]
1179     assert_equal relation.id, result[2]
1180     assert_equal relation.id, result[3]
1181     assert_equal relation.version + 1, result[4]
1182
1183     new_relation = Relation.find(relation.id)
1184     assert_equal relation.version + 1, new_relation.version
1185     assert_equal [], new_relation.members
1186     assert_equal({}, new_relation.tags)
1187     assert_equal false, new_relation.visible
1188   end
1189
1190   # check that we can't delete a relation that is in use
1191   def test_putrelation_delete_inuse
1192     relation = current_relations(:public_used_relation)
1193     cs_id = changesets(:public_user_first_change).id
1194
1195     amf_content "putrelation", "/1", ["test@example.com:test", cs_id, relation.version, relation.id, relation.tags, relation.members, false]
1196     post :amf_write
1197     assert_response :success
1198     amf_parse_response
1199     result = amf_result("/1")
1200
1201     assert_equal 2, result.size
1202     assert_equal -1, result[0]
1203     assert_match /relation #{relation.id} is used in/, result[1]
1204
1205     new_relation = Relation.find(relation.id)
1206     assert_equal relation.version, new_relation.version
1207     assert_equal relation.members, new_relation.members
1208     assert_equal relation.tags, new_relation.tags
1209     assert_equal true, new_relation.visible
1210   end
1211
1212   # check that we can open a changeset
1213   def test_startchangeset_valid
1214     amf_content "startchangeset", "/1", ["test@example.com:test", { "source" => "new" }, nil, "new", 1]
1215     post :amf_write
1216     assert_response :success
1217     amf_parse_response
1218     result = amf_result("/1")
1219     new_cs_id = result[2].to_i
1220
1221     assert_equal 3, result.size
1222     assert_equal 0, result[0]
1223     assert_equal "", result[1]
1224
1225     cs = Changeset.find(new_cs_id)
1226     assert_equal true, cs.is_open?
1227     assert_equal({ "comment" => "new", "source" => "new" }, cs.tags)
1228
1229     old_cs_id = new_cs_id
1230
1231     amf_content "startchangeset", "/1", ["test@example.com:test", { "source" => "newer" }, old_cs_id, "newer", 1]
1232     post :amf_write
1233     assert_response :success
1234     amf_parse_response
1235     result = amf_result("/1")
1236     new_cs_id = result[2].to_i
1237
1238     assert_not_equal old_cs_id, new_cs_id
1239
1240     assert_equal 3, result.size
1241     assert_equal 0, result[0]
1242     assert_equal "", result[1]
1243
1244     cs = Changeset.find(old_cs_id)
1245     assert_equal false, cs.is_open?
1246     assert_equal({ "comment" => "newer", "source" => "new" }, cs.tags)
1247
1248     cs = Changeset.find(new_cs_id)
1249     assert_equal true, cs.is_open?
1250     assert_equal({ "comment" => "newer", "source" => "newer" }, cs.tags)
1251
1252     old_cs_id = new_cs_id
1253
1254     amf_content "startchangeset", "/1", ["test@example.com:test", {}, old_cs_id, "", 0]
1255     post :amf_write
1256     assert_response :success
1257     amf_parse_response
1258     result = amf_result("/1")
1259
1260     assert_equal 3, result.size
1261     assert_equal 0, result[0]
1262     assert_equal "", result[1]
1263     assert_nil result[2]
1264
1265     cs = Changeset.find(old_cs_id)
1266     assert_equal false, cs.is_open?
1267     assert_equal({ "comment" => "newer", "source" => "newer" }, cs.tags)
1268   end
1269
1270   # check that we can't close somebody elses changeset
1271   def test_startchangeset_invalid_wrong_user
1272     amf_content "startchangeset", "/1", ["test@example.com:test", { "source" => "new" }, nil, "new", 1]
1273     post :amf_write
1274     assert_response :success
1275     amf_parse_response
1276     result = amf_result("/1")
1277     cs_id = result[2].to_i
1278
1279     assert_equal 3, result.size
1280     assert_equal 0, result[0]
1281     assert_equal "", result[1]
1282
1283     cs = Changeset.find(cs_id)
1284     assert_equal true, cs.is_open?
1285     assert_equal({ "comment" => "new", "source" => "new" }, cs.tags)
1286
1287     amf_content "startchangeset", "/1", ["test@openstreetmap.org:test", {}, cs_id, "delete", 0]
1288     post :amf_write
1289     assert_response :success
1290     amf_parse_response
1291     result = amf_result("/1")
1292
1293     assert_equal 2, result.size
1294     assert_equal -2, result[0]
1295     assert_equal "The user doesn't own that changeset", result[1]
1296
1297     cs = Changeset.find(cs_id)
1298     assert_equal true, cs.is_open?
1299     assert_equal({ "comment" => "new", "source" => "new" }, cs.tags)
1300   end
1301
1302   # check that invalid characters are stripped from changeset tags
1303   def test_startchangeset_invalid_xmlchar_comment
1304     invalid = "\035\022"
1305     comment = "foo#{invalid}bar"
1306
1307     amf_content "startchangeset", "/1", ["test@example.com:test", {}, nil, comment, 1]
1308     post :amf_write
1309     assert_response :success
1310     amf_parse_response
1311     result = amf_result("/1")
1312     new_cs_id = result[2].to_i
1313
1314     assert_equal 3, result.size
1315     assert_equal 0, result[0]
1316     assert_equal "", result[1]
1317
1318     cs = Changeset.find(new_cs_id)
1319     assert_equal true, cs.is_open?
1320     assert_equal({ "comment" => "foobar" }, cs.tags)
1321   end
1322
1323   private
1324
1325   # ************************************************************
1326   # AMF Helper functions
1327
1328   # Get the result record for the specified ID
1329   # It's an assertion FAIL if the record does not exist
1330   def amf_result(ref)
1331     assert @amf_result.key?("#{ref}/onResult")
1332     @amf_result["#{ref}/onResult"]
1333   end
1334
1335   # Encode the AMF message to invoke "target" with parameters as
1336   # the passed data. The ref is used to retrieve the results.
1337   def amf_content(target, ref, data)
1338     a, b = 1.divmod(256)
1339     c = StringIO.new
1340     c.write 0.chr + 0.chr   # version 0
1341     c.write 0.chr + 0.chr   # n headers
1342     c.write a.chr + b.chr   # n bodies
1343     c.write AMF.encodestring(target)
1344     c.write AMF.encodestring(ref)
1345     c.write [-1].pack("N")
1346     c.write AMF.encodevalue(data)
1347
1348     @request.env["RAW_POST_DATA"] = c.string
1349   end
1350
1351   # Parses the @response object as an AMF messsage.
1352   # The result is a hash of message_ref => data.
1353   # The attribute @amf_result is initialised to this hash.
1354   def amf_parse_response
1355     req = StringIO.new(@response.body)
1356
1357     req.read(2) # version
1358
1359     # parse through any headers
1360     headers = AMF.getint(req)        # Read number of headers
1361     headers.times do                 # Read each header
1362       AMF.getstring(req)             #  |
1363       req.getc                       #  | skip boolean
1364       AMF.getvalue(req)              #  |
1365     end
1366
1367     # parse through responses
1368     results = {}
1369     bodies = AMF.getint(req)         # Read number of bodies
1370     bodies.times do                  # Read each body
1371       message = AMF.getstring(req)   #  | get message name
1372       AMF.getstring(req)             #  | get index in response sequence
1373       AMF.getlong(req)               #  | get total size in bytes
1374       args = AMF.getvalue(req)       #  | get response (probably an array)
1375       results[message] = args
1376     end
1377     @amf_result = results
1378     results
1379   end
1380
1381   ##
1382   # given an array of bounding boxes (each an array of 4 floats), call the
1383   # AMF "whichways" controller for each and pass the result back to the
1384   # caller's block for assertion testing.
1385   def check_bboxes_are_bad(bboxes)
1386     bboxes.each do |bbox|
1387       amf_content "whichways", "/1", bbox
1388       post :amf_read
1389       assert_response :success
1390       amf_parse_response
1391
1392       # pass the response back to the caller's block to be tested
1393       # against what the caller expected.
1394       map = amf_result "/1"
1395       yield map, bbox
1396     end
1397   end
1398
1399   # this should be what AMF controller returns when the bbox of a
1400   # whichways request is invalid or too large.
1401   def assert_boundary_error(map, msg = nil, error_hint = nil)
1402     expected_map = [-2, "Sorry - I can't get the map for that area.#{msg}"]
1403     assert_equal expected_map, map, "AMF controller should have returned an error. (#{error_hint})"
1404   end
1405
1406   # this should be what AMF controller returns when the bbox of a
1407   # whichways_deleted request is invalid or too large.
1408   def assert_deleted_boundary_error(map, msg = nil, error_hint = nil)
1409     expected_map = [-2, "Sorry - I can't get the map for that area.#{msg}"]
1410     assert_equal expected_map, map, "AMF controller should have returned an error. (#{error_hint})"
1411   end
1412 end