]> git.openstreetmap.org Git - rails.git/blob - test/controllers/amf_controller_test.rb
371319c23d65a6e46919262c7f70691ccd7fbe5c
[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     amf_content "getpresets", "/1", ["test@example.com:test", ""]
23     post :amf_read
24     assert_response :success
25     amf_parse_response
26     presets = amf_result("/1")
27
28     assert_equal 15, presets.length
29     assert_equal POTLATCH_PRESETS[0], presets[0]
30     assert_equal POTLATCH_PRESETS[1], presets[1]
31     assert_equal POTLATCH_PRESETS[2], presets[2]
32     assert_equal POTLATCH_PRESETS[3], presets[3]
33     assert_equal POTLATCH_PRESETS[4], presets[4]
34     assert_equal POTLATCH_PRESETS[5], presets[5]
35     assert_equal POTLATCH_PRESETS[6], presets[6]
36     assert_equal POTLATCH_PRESETS[7], presets[7]
37     assert_equal POTLATCH_PRESETS[8], presets[8]
38     assert_equal POTLATCH_PRESETS[9], presets[9]
39     assert_equal POTLATCH_PRESETS[10], presets[10]
40     assert_equal POTLATCH_PRESETS[12], presets[12]
41   end
42
43   def test_getway
44     # check a visible way
45     id = current_ways(:visible_way).id
46     amf_content "getway", "/1", [id]
47     post :amf_read
48     assert_response :success
49     amf_parse_response
50     way = amf_result("/1")
51     assert_equal 0, way[0]
52     assert_equal "", way[1]
53     assert_equal id, way[2]
54     assert_equal 1, way[3].length
55     assert_equal 3, way[3][0][2]
56     assert_equal 1, way[5]
57     assert_equal 2, way[6]
58   end
59
60   def test_getway_invisible
61     # check an invisible way
62     id = current_ways(:invisible_way).id
63     amf_content "getway", "/1", [id]
64     post :amf_read
65     assert_response :success
66     amf_parse_response
67     way = amf_result("/1")
68     assert_equal -4, way[0], -4
69     assert_equal "way", way[1]
70     assert_equal id, way[2]
71     assert(way[3].nil?) && way[4].nil? && way[5].nil? && way[6].nil?
72   end
73
74   def test_getway_with_versions
75     # check a way with multiple versions
76     id = current_ways(:way_with_versions).id
77     amf_content "getway", "/1", [id]
78     post :amf_read
79     assert_response :success
80     amf_parse_response
81     way = amf_result("/1")
82     assert_equal 0, way[0]
83     assert_equal "", way[1]
84     assert_equal id, way[2]
85     assert_equal 1, way[3].length
86     assert_equal 15, way[3][0][2]
87     assert_equal 4, way[5]
88     assert_equal 2, way[6]
89   end
90
91   def test_getway_with_duplicate_nodes
92     # check a way with duplicate nodes
93     id = current_ways(:way_with_duplicate_nodes).id
94     amf_content "getway", "/1", [id]
95     post :amf_read
96     assert_response :success
97     amf_parse_response
98     way = amf_result("/1")
99     assert_equal 0, way[0]
100     assert_equal "", way[1]
101     assert_equal id, way[2]
102     assert_equal 2, way[3].length
103     assert_equal 4, way[3][0][2]
104     assert_equal 4, way[3][1][2]
105     assert_equal 1, way[5]
106     assert_equal 2, way[6]
107   end
108
109   def test_getway_with_multiple_nodes
110     # check a way with multiple nodes
111     id = current_ways(:way_with_multiple_nodes).id
112     amf_content "getway", "/1", [id]
113     post :amf_read
114     assert_response :success
115     amf_parse_response
116     way = amf_result("/1")
117     assert_equal 0, way[0]
118     assert_equal "", way[1]
119     assert_equal id, way[2]
120     assert_equal 3, way[3].length
121     assert_equal 4, way[3][0][2]
122     assert_equal 15, way[3][1][2]
123     assert_equal 6, way[3][2][2]
124     assert_equal 2, way[5]
125     assert_equal 2, way[6]
126   end
127
128   def test_getway_nonexistent
129     # check chat a non-existent way is not returned
130     amf_content "getway", "/1", [0]
131     post :amf_read
132     assert_response :success
133     amf_parse_response
134     way = amf_result("/1")
135     assert_equal -4, way[0]
136     assert_equal "way", way[1]
137     assert_equal 0, way[2]
138     assert(way[3].nil?) && way[4].nil? && way[5].nil? && way[6].nil?
139   end
140
141   def test_whichways
142     node = current_nodes(:used_node_1)
143     minlon = node.lon - 0.1
144     minlat = node.lat - 0.1
145     maxlon = node.lon + 0.1
146     maxlat = node.lat + 0.1
147     amf_content "whichways", "/1", [minlon, minlat, maxlon, maxlat]
148     post :amf_read
149     assert_response :success
150     amf_parse_response
151
152     # check contents of message
153     map = amf_result "/1"
154     assert_equal 0, map[0], "map error code should be 0"
155     assert_equal "", map[1], "map error text should be empty"
156
157     # check the formatting of the message
158     assert_equal 5, map.length, "map should have length 5"
159     assert_equal Array, map[2].class, 'map "ways" element should be an array'
160     assert_equal Array, map[3].class, 'map "nodes" element should be an array'
161     assert_equal Array, map[4].class, 'map "relations" element should be an array'
162     map[2].each do |w|
163       assert_equal 2, w.length, "way should be (id, version) pair"
164       assert w[0] == w[0].floor, "way ID should be an integer"
165       assert w[1] == w[1].floor, "way version should be an integer"
166     end
167
168     map[3].each do |n|
169       assert_equal 5, w.length, "node should be (id, lat, lon, [tags], version) tuple"
170       assert n[0] == n[0].floor, "node ID should be an integer"
171       assert n[1] >= minlat - 0.01, "node lat should be greater than min"
172       assert n[1] <= maxlat - 0.01, "node lat should be less than max"
173       assert n[2] >= minlon - 0.01, "node lon should be greater than min"
174       assert n[2] <= maxlon - 0.01, "node lon should be less than max"
175       assert_equal Array, a[3].class, "node tags should be array"
176       assert n[4] == n[4].floor, "node version should be an integer"
177     end
178
179     map[4].each do |r|
180       assert_equal 2, r.length, "relation should be (id, version) pair"
181       assert r[0] == r[0].floor, "relation ID should be an integer"
182       assert r[1] == r[1].floor, "relation version should be an integer"
183     end
184
185     # TODO: looks like amf_controller changed since this test was written
186     # so someone who knows what they're doing should check this!
187     ways = map[2].collect { |x| x[0] }
188     assert ways.include?(current_ways(:used_way).id),
189            "map should include used way"
190     assert !ways.include?(current_ways(:invisible_way).id),
191            "map should not include deleted way"
192   end
193
194   ##
195   # checks that too-large a bounding box will not be served.
196   def test_whichways_toobig
197     bbox = [-0.1, -0.1, 1.1, 1.1]
198     check_bboxes_are_bad [bbox] do |map, _bbox|
199       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"
200     end
201   end
202
203   ##
204   # checks that an invalid bounding box will not be served. in this case
205   # one with max < min latitudes.
206   #
207   # NOTE: the controller expands the bbox by 0.01 in each direction!
208   def test_whichways_badlat
209     bboxes = [[0, 0.1, 0.1, 0], [-0.1, 80, 0.1, 70], [0.24, 54.35, 0.25, 54.33]]
210     check_bboxes_are_bad bboxes do |map, bbox|
211       assert_boundary_error map, " The server said: The minimum latitude must be less than the maximum latitude, but it wasn't", bbox.inspect
212     end
213   end
214
215   ##
216   # same as test_whichways_badlat, but for longitudes
217   #
218   # NOTE: the controller expands the bbox by 0.01 in each direction!
219   def test_whichways_badlon
220     bboxes = [[80, -0.1, 70, 0.1], [54.35, 0.24, 54.33, 0.25]]
221     check_bboxes_are_bad bboxes do |map, bbox|
222       assert_boundary_error map, " The server said: The minimum longitude must be less than the maximum longitude, but it wasn't", bbox.inspect
223     end
224   end
225
226   def test_whichways_deleted
227     node = current_nodes(:used_node_1)
228     minlon = node.lon - 0.1
229     minlat = node.lat - 0.1
230     maxlon = node.lon + 0.1
231     maxlat = node.lat + 0.1
232     amf_content "whichways_deleted", "/1", [minlon, minlat, maxlon, maxlat]
233     post :amf_read
234     assert_response :success
235     amf_parse_response
236
237     # check contents of message
238     map = amf_result "/1"
239     assert_equal 0, map[0], "first map element should be 0"
240     assert_equal "", map[1], "second map element should be an empty string"
241     assert_equal Array, map[2].class, "third map element should be an array"
242     # TODO: looks like amf_controller changed since this test was written
243     # so someone who knows what they're doing should check this!
244     assert !map[2].include?(current_ways(:used_way).id),
245            "map should not include used way"
246     assert map[2].include?(current_ways(:invisible_way).id),
247            "map should include deleted way"
248   end
249
250   def test_whichways_deleted_toobig
251     bbox = [-0.1, -0.1, 1.1, 1.1]
252     amf_content "whichways_deleted", "/1", bbox
253     post :amf_read
254     assert_response :success
255     amf_parse_response
256
257     map = amf_result "/1"
258     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"
259   end
260
261   def test_getrelation
262     id = current_relations(:visible_relation).id
263     amf_content "getrelation", "/1", [id]
264     post :amf_read
265     assert_response :success
266     amf_parse_response
267     rel = amf_result("/1")
268     assert_equal rel[0], 0
269     assert_equal rel[2], id
270   end
271
272   def test_getrelation_invisible
273     id = current_relations(:invisible_relation).id
274     amf_content "getrelation", "/1", [id]
275     post :amf_read
276     assert_response :success
277     amf_parse_response
278     rel = amf_result("/1")
279     assert_equal rel[0], -4
280     assert_equal rel[1], "relation"
281     assert_equal rel[2], id
282     assert(rel[3].nil?) && rel[4].nil?
283   end
284
285   def test_getrelation_nonexistent
286     id = 0
287     amf_content "getrelation", "/1", [id]
288     post :amf_read
289     assert_response :success
290     amf_parse_response
291     rel = amf_result("/1")
292     assert_equal rel[0], -4
293     assert_equal rel[1], "relation"
294     assert_equal rel[2], id
295     assert(rel[3].nil?) && rel[4].nil?
296   end
297
298   def test_getway_old
299     # try to get the last visible version (specified by <0) (should be current version)
300     latest = current_ways(:way_with_versions)
301     # NOTE: looks from the API changes that this now expects a timestamp
302     # instead of a version number...
303     # try to get version 1
304     v1 = ways(:way_with_versions_v1)
305     { latest.id => "",
306       v1.way_id => v1.timestamp.strftime("%d %b %Y, %H:%M:%S")
307     }.each do |id, t|
308       amf_content "getway_old", "/1", [id, t]
309       post :amf_read
310       assert_response :success
311       amf_parse_response
312       returned_way = amf_result("/1")
313       assert_equal 0, returned_way[0]
314       assert_equal id, returned_way[2]
315       # API returns the *latest* version, even for old ways...
316       assert_equal latest.version, returned_way[5]
317     end
318   end
319
320   ##
321   # test that the server doesn't fall over when rubbish is passed
322   # into the method args.
323   def test_getway_old_invalid
324     way_id = current_ways(:way_with_versions).id
325     { "foo"  => "bar",
326       way_id => "not a date",
327       way_id => "2009-03-25 00:00:00", # <- wrong format
328       way_id => "0 Jan 2009 00:00:00", # <- invalid date
329       -1     => "1 Jan 2009 00:00:00"  # <- invalid ID
330     }.each do |id, t|
331       amf_content "getway_old", "/1", [id, t]
332       post :amf_read
333       assert_response :success
334       amf_parse_response
335       returned_way = amf_result("/1")
336       assert_equal -1, returned_way[0]
337       assert returned_way[3].nil?
338       assert returned_way[4].nil?
339       assert returned_way[5].nil?
340     end
341   end
342
343   def test_getway_old_nonexistent
344     # try to get the last version+10 (shoudn't exist)
345     v1 = ways(:way_with_versions_v1)
346     # try to get last visible version of non-existent way
347     # try to get specific version of non-existent way
348     [[0, ""],
349      [0, "1 Jan 1970, 00:00:00"],
350      [v1.way_id, (v1.timestamp - 10).strftime("%d %b %Y, %H:%M:%S")]
351     ].each do |id, t|
352       amf_content "getway_old", "/1", [id, t]
353       post :amf_read
354       assert_response :success
355       amf_parse_response
356       returned_way = amf_result("/1")
357       assert_equal -1, returned_way[0]
358       assert returned_way[3].nil?
359       assert returned_way[4].nil?
360       assert returned_way[5].nil?
361     end
362   end
363
364   def test_getway_history
365     latest = current_ways(:way_with_versions)
366     oldest = ways(:way_with_versions_v1)
367
368     amf_content "getway_history", "/1", [latest.id]
369     post :amf_read
370     assert_response :success
371     amf_parse_response
372     history = amf_result("/1")
373
374     # ['way',wayid,history]
375     assert_equal "way", history[0]
376     assert_equal latest.id, history[1]
377     # We use dates rather than version numbers here, because you might
378     # have moved a node within a way (i.e. way version not incremented).
379     # The timestamp is +1 because we say "give me the revision of 15:33:02",
380     # but that might actually include changes at 15:33:02.457.
381     assert_equal (latest.timestamp + 1).strftime("%d %b %Y, %H:%M:%S"), history[2].first[0]
382     assert_equal (oldest.timestamp + 1).strftime("%d %b %Y, %H:%M:%S"), history[2].last[0]
383   end
384
385   def test_getway_history_nonexistent
386     amf_content "getway_history", "/1", [0]
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 history[0], "way"
394     assert_equal history[1], 0
395     assert history[2].empty?
396   end
397
398   def test_getnode_history
399     latest = current_nodes(:node_with_versions)
400     amf_content "getnode_history", "/1", [latest.id]
401     post :amf_read
402     assert_response :success
403     amf_parse_response
404     history = amf_result("/1")
405
406     # ['node',nodeid,history]
407     # note that (as per getway_history) we actually round up
408     # to the next second
409     assert_equal history[0], "node",
410                  'first element should be "node"'
411     assert_equal history[1], latest.id,
412                  "second element should be the input node ID"
413     assert_equal history[2].first[0],
414                  (latest.timestamp + 1).strftime("%d %b %Y, %H:%M:%S"),
415                  "first element in third element (array) should be the latest version"
416     assert_equal history[2].last[0],
417                  (nodes(:node_with_versions_v1).timestamp + 1).strftime("%d %b %Y, %H:%M:%S"),
418                  "last element in third element (array) should be the initial version"
419   end
420
421   def test_getnode_history_nonexistent
422     amf_content "getnode_history", "/1", [0]
423     post :amf_read
424     assert_response :success
425     amf_parse_response
426     history = amf_result("/1")
427
428     # ['node',nodeid,history]
429     assert_equal history[0], "node"
430     assert_equal history[1], 0
431     assert history[2].empty?
432   end
433
434   def test_findgpx_bad_user
435     amf_content "findgpx", "/1", [1, "test@example.com:wrong"]
436     post :amf_read
437     assert_response :success
438     amf_parse_response
439     result = amf_result("/1")
440
441     assert_equal 2, result.length
442     assert_equal -1, result[0]
443     assert_match /must be logged in/, result[1]
444
445     amf_content "findgpx", "/1", [1, "blocked@openstreetmap.org:test"]
446     post :amf_read
447     assert_response :success
448     amf_parse_response
449     result = amf_result("/1")
450
451     assert_equal 2, result.length
452     assert_equal -1, result[0]
453     assert_match /access to the API has been blocked/, result[1]
454   end
455
456   def test_findgpx_by_id
457     trace = gpx_files(:anon_trace_file)
458
459     amf_content "findgpx", "/1", [trace.id, "test@example.com:test"]
460     post :amf_read
461     assert_response :success
462     amf_parse_response
463     result = amf_result("/1")
464
465     assert_equal 3, result.length
466     assert_equal 0, result[0]
467     assert_equal "", result[1]
468     traces = result[2]
469     assert_equal 1, traces.length
470     assert_equal 3, traces[0].length
471     assert_equal trace.id, traces[0][0]
472     assert_equal trace.name, traces[0][1]
473     assert_equal trace.description, traces[0][2]
474   end
475
476   def test_findgpx_by_name
477     amf_content "findgpx", "/1", ["Trace", "test@example.com:test"]
478     post :amf_read
479     assert_response :success
480     amf_parse_response
481     result = amf_result("/1")
482
483     # find by name fails as it uses mysql text search syntax...
484     assert_equal 2, result.length
485     assert_equal -2, result[0]
486   end
487
488   def test_findrelations_by_id
489     relation = current_relations(:relation_with_versions)
490
491     amf_content "findrelations", "/1", [relation.id]
492     post :amf_read
493     assert_response :success
494     amf_parse_response
495     result = amf_result("/1")
496
497     assert_equal 1, result.length
498     assert_equal 4, result[0].length
499     assert_equal relation.id, result[0][0]
500     assert_equal relation.tags, result[0][1]
501     assert_equal relation.members, result[0][2]
502     assert_equal relation.version, result[0][3]
503
504     amf_content "findrelations", "/1", [999999]
505     post :amf_read
506     assert_response :success
507     amf_parse_response
508     result = amf_result("/1")
509
510     assert_equal 0, result.length
511   end
512
513   def test_findrelations_by_tags
514     visible_relation = current_relations(:visible_relation)
515     used_relation = current_relations(:used_relation)
516
517     amf_content "findrelations", "/1", ["yes"]
518     post :amf_read
519     assert_response :success
520     amf_parse_response
521     result = amf_result("/1").sort
522
523     assert_equal 2, result.length
524     assert_equal 4, result[0].length
525     assert_equal visible_relation.id, result[0][0]
526     assert_equal visible_relation.tags, result[0][1]
527     assert_equal visible_relation.members, result[0][2]
528     assert_equal visible_relation.version, result[0][3]
529     assert_equal 4, result[1].length
530     assert_equal used_relation.id, result[1][0]
531     assert_equal used_relation.tags, result[1][1]
532     assert_equal used_relation.members, result[1][2]
533     assert_equal used_relation.version, result[1][3]
534
535     amf_content "findrelations", "/1", ["no"]
536     post :amf_read
537     assert_response :success
538     amf_parse_response
539     result = amf_result("/1").sort
540
541     assert_equal 0, result.length
542   end
543
544   def test_getpoi_without_timestamp
545     node = current_nodes(:node_with_versions)
546
547     amf_content "getpoi", "/1", [node.id, ""]
548     post :amf_read
549     assert_response :success
550     amf_parse_response
551     result = amf_result("/1")
552
553     assert_equal 7, result.length
554     assert_equal 0, result[0]
555     assert_equal "", result[1]
556     assert_equal node.id, result[2]
557     assert_equal node.lon, result[3]
558     assert_equal node.lat, result[4]
559     assert_equal node.tags, result[5]
560     assert_equal node.version, result[6]
561
562     amf_content "getpoi", "/1", [999999, ""]
563     post :amf_read
564     assert_response :success
565     amf_parse_response
566     result = amf_result("/1")
567
568     assert_equal 3, result.length
569     assert_equal -4, result[0]
570     assert_equal "node", result[1]
571     assert_equal 999999, result[2]
572   end
573
574   def test_getpoi_with_timestamp
575     node = nodes(:node_with_versions_v2)
576     current_node = current_nodes(:node_with_versions)
577
578     amf_content "getpoi", "/1", [node.node_id, node.timestamp.xmlschema]
579     post :amf_read
580     assert_response :success
581     amf_parse_response
582     result = amf_result("/1")
583
584     assert_equal 7, result.length
585     assert_equal 0, result[0]
586     assert_equal "", result[1]
587     assert_equal node.node_id, result[2]
588     assert_equal node.lon, result[3]
589     assert_equal node.lat, result[4]
590     assert_equal node.tags, result[5]
591     assert_equal current_node.version, result[6]
592
593     amf_content "getpoi", "/1", [node.node_id, "2000-01-01T00:00:00Z"]
594     post :amf_read
595     assert_response :success
596     amf_parse_response
597     result = amf_result("/1")
598
599     assert_equal 3, result.length
600     assert_equal -4, result[0]
601     assert_equal "node", result[1]
602     assert_equal node.node_id, result[2]
603
604     amf_content "getpoi", "/1", [999999, Time.now.xmlschema]
605     post :amf_read
606     assert_response :success
607     amf_parse_response
608     result = amf_result("/1")
609
610     assert_equal 3, result.length
611     assert_equal -4, result[0]
612     assert_equal "node", result[1]
613     assert_equal 999999, result[2]
614   end
615
616   # ************************************************************
617   # AMF Write tests
618
619   # check that we can update a poi
620   def test_putpoi_update_valid
621     nd = current_nodes(:visible_node)
622     cs_id = changesets(:public_user_first_change).id
623     amf_content "putpoi", "/1", ["test@example.com:test", cs_id, nd.version, nd.id, nd.lon, nd.lat, nd.tags, nd.visible]
624     post :amf_write
625     assert_response :success
626     amf_parse_response
627     result = amf_result("/1")
628
629     assert_equal 5, result.size
630     assert_equal 0, result[0]
631     assert_equal "", result[1]
632     assert_equal nd.id, result[2]
633     assert_equal nd.id, result[3]
634     assert_equal nd.version + 1, result[4]
635
636     # Now try to update again, with a different lat/lon, using the updated version number
637     lat = nd.lat + 0.1
638     lon = nd.lon - 0.1
639     amf_content "putpoi", "/2", ["test@example.com:test", cs_id, nd.version + 1, nd.id, lon, lat, nd.tags, nd.visible]
640     post :amf_write
641     assert_response :success
642     amf_parse_response
643     result = amf_result("/2")
644
645     assert_equal 5, result.size
646     assert_equal 0, result[0]
647     assert_equal "", result[1]
648     assert_equal nd.id, result[2]
649     assert_equal nd.id, result[3]
650     assert_equal nd.version + 2, result[4]
651   end
652
653   # Check that we can create a no valid poi
654   # Using similar method for the node controller test
655   def test_putpoi_create_valid
656     # This node has no tags
657
658     # create a node with random lat/lon
659     lat = rand(100) - 50 + rand
660     lon = rand(100) - 50 + rand
661     # normal user has a changeset open
662     changeset = changesets(:public_user_first_change)
663
664     amf_content "putpoi", "/1", ["test@example.com:test", changeset.id, nil, nil, lon, lat, {}, nil]
665     post :amf_write
666     assert_response :success
667     amf_parse_response
668     result = amf_result("/1")
669
670     # check the array returned by the amf
671     assert_equal 5, result.size
672     assert_equal 0, result[0], "expected to get the status ok from the amf"
673     assert_equal 0, result[2], "The old id should be 0"
674     assert result[3] > 0, "The new id should be greater than 0"
675     assert_equal 1, result[4], "The new version should be 1"
676
677     # Finally check that the node that was saved has saved the data correctly
678     # in both the current and history tables
679     # First check the current table
680     current_node = Node.find(result[3].to_i)
681     assert_in_delta lat, current_node.lat, 0.00001, "The latitude was not retreieved correctly"
682     assert_in_delta lon, current_node.lon, 0.00001, "The longitude was not retreived correctly"
683     assert_equal 0, current_node.tags.size, "There seems to be a tag that has been added to the node"
684     assert_equal result[4], current_node.version, "The version returned, is different to the one returned by the amf"
685     # Now check the history table
686     historic_nodes = Node.where(:id => result[3])
687     assert_equal 1, historic_nodes.size, "There should only be one historic node created"
688     first_historic_node = historic_nodes.first
689     assert_in_delta lat, first_historic_node.lat, 0.00001, "The latitude was not retreived correctly"
690     assert_in_delta lon, first_historic_node.lon, 0.00001, "The longitude was not retreuved correctly"
691     assert_equal 0, first_historic_node.tags.size, "There seems to be a tag that have been attached to this node"
692     assert_equal result[4], first_historic_node.version, "The version returned, is different to the one returned by the amf"
693
694     ####
695     # This node has some tags
696
697     # create a node with random lat/lon
698     lat = rand(100) - 50 + rand
699     lon = rand(100) - 50 + rand
700     # normal user has a changeset open
701     changeset = changesets(:public_user_first_change)
702
703     amf_content "putpoi", "/2", ["test@example.com:test", changeset.id, nil, nil, lon, lat, { "key" => "value", "ping" => "pong" }, nil]
704     post :amf_write
705     assert_response :success
706     amf_parse_response
707     result = amf_result("/2")
708
709     # check the array returned by the amf
710     assert_equal 5, result.size
711     assert_equal 0, result[0], "Expected to get the status ok in the amf"
712     assert_equal 0, result[2], "The old id should be 0"
713     assert result[3] > 0, "The new id should be greater than 0"
714     assert_equal 1, result[4], "The new version should be 1"
715
716     # Finally check that the node that was saved has saved the data correctly
717     # in both the current and history tables
718     # First check the current table
719     current_node = Node.find(result[3].to_i)
720     assert_in_delta lat, current_node.lat, 0.00001, "The latitude was not retreieved correctly"
721     assert_in_delta lon, current_node.lon, 0.00001, "The longitude was not retreived correctly"
722     assert_equal 2, current_node.tags.size, "There seems to be a tag that has been added to the node"
723     assert_equal({ "key" => "value", "ping" => "pong" }, current_node.tags, "tags are different")
724     assert_equal result[4], current_node.version, "The version returned, is different to the one returned by the amf"
725     # Now check the history table
726     historic_nodes = Node.where(:id => result[3])
727     assert_equal 1, historic_nodes.size, "There should only be one historic node created"
728     first_historic_node = historic_nodes.first
729     assert_in_delta lat, first_historic_node.lat, 0.00001, "The latitude was not retreived correctly"
730     assert_in_delta lon, first_historic_node.lon, 0.00001, "The longitude was not retreuved correctly"
731     assert_equal 2, first_historic_node.tags.size, "There seems to be a tag that have been attached to this node"
732     assert_equal({ "key" => "value", "ping" => "pong" }, first_historic_node.tags, "tags are different")
733     assert_equal result[4], first_historic_node.version, "The version returned, is different to the one returned by the amf"
734   end
735
736   # try creating a POI with rubbish in the tags
737   def test_putpoi_create_with_control_chars
738     # This node has no tags
739
740     # create a node with random lat/lon
741     lat = rand(100) - 50 + rand
742     lon = rand(100) - 50 + rand
743     # normal user has a changeset open
744     changeset = changesets(:public_user_first_change)
745
746     mostly_invalid = (0..31).to_a.map(&:chr).join
747     tags = { "something" => "foo#{mostly_invalid}bar" }
748
749     amf_content "putpoi", "/1", ["test@example.com:test", changeset.id, nil, nil, lon, lat, tags, nil]
750     post :amf_write
751     assert_response :success
752     amf_parse_response
753     result = amf_result("/1")
754
755     # check the array returned by the amf
756     assert_equal 5, result.size
757     assert_equal 0, result[0], "Expected to get the status ok in the amf"
758     assert_equal 0, result[2], "The old id should be 0"
759     assert result[3] > 0, "The new id should be greater than 0"
760     assert_equal 1, result[4], "The new version should be 1"
761
762     # Finally check that the node that was saved has saved the data correctly
763     # in both the current and history tables
764     # First check the current table
765     current_node = Node.find(result[3].to_i)
766     assert_equal 1, current_node.tags.size, "There seems to be a tag that has been added to the node"
767     assert_equal({ "something" => "foo\t\n\rbar" }, current_node.tags, "tags were not fixed correctly")
768     assert_equal result[4], current_node.version, "The version returned, is different to the one returned by the amf"
769   end
770
771   # try creating a POI with rubbish in the tags
772   def test_putpoi_create_with_invalid_utf8
773     # This node has no tags
774
775     # create a node with random lat/lon
776     lat = rand(100) - 50 + rand
777     lon = rand(100) - 50 + rand
778     # normal user has a changeset open
779     changeset = changesets(:public_user_first_change)
780
781     invalid = "\xc0\xc0"
782     tags = { "something" => "foo#{invalid}bar" }
783
784     amf_content "putpoi", "/1", ["test@example.com:test", changeset.id, nil, nil, lon, lat, tags, nil]
785     post :amf_write
786     assert_response :success
787     amf_parse_response
788     result = amf_result("/1")
789
790     assert_equal 2, result.size
791     assert_equal -1, result[0], "Expected to get the status FAIL in the amf"
792     assert_equal "One of the tags is invalid. Linux users may need to upgrade to Flash Player 10.1.", result[1]
793   end
794
795   # try deleting a node
796   def test_putpoi_delete_valid
797     nd = current_nodes(:visible_node)
798     cs_id = changesets(:public_user_first_change).id
799     amf_content "putpoi", "/1", ["test@example.com:test", cs_id, nd.version, nd.id, nd.lon, nd.lat, nd.tags, false]
800     post :amf_write
801     assert_response :success
802     amf_parse_response
803     result = amf_result("/1")
804
805     assert_equal 5, result.size
806     assert_equal 0, result[0]
807     assert_equal "", result[1]
808     assert_equal nd.id, result[2]
809     assert_equal nd.id, result[3]
810     assert_equal nd.version + 1, result[4]
811
812     current_node = Node.find(result[3].to_i)
813     assert_equal false, current_node.visible
814   end
815
816   # try deleting a node that is already deleted
817   def test_putpoi_delete_already_deleted
818     nd = current_nodes(:invisible_node)
819     cs_id = changesets(:public_user_first_change).id
820     amf_content "putpoi", "/1", ["test@example.com:test", cs_id, nd.version, nd.id, nd.lon, nd.lat, nd.tags, false]
821     post :amf_write
822     assert_response :success
823     amf_parse_response
824     result = amf_result("/1")
825
826     assert_equal 3, result.size
827     assert_equal -4, result[0]
828     assert_equal "node", result[1]
829     assert_equal nd.id, result[2]
830   end
831
832   # try deleting a node that has never existed
833   def test_putpoi_delete_not_found
834     cs_id = changesets(:public_user_first_change).id
835     amf_content "putpoi", "/1", ["test@example.com:test", cs_id, 1, 999999, 0, 0, {}, false]
836     post :amf_write
837     assert_response :success
838     amf_parse_response
839     result = amf_result("/1")
840
841     assert_equal 3, result.size
842     assert_equal -4, result[0]
843     assert_equal "node", result[1]
844     assert_equal 999999, result[2]
845   end
846
847   # try setting an invalid location on a node
848   def test_putpoi_invalid_latlon
849     nd = current_nodes(:visible_node)
850     cs_id = changesets(:public_user_first_change).id
851     amf_content "putpoi", "/1", ["test@example.com:test", cs_id, nd.version, nd.id, 200, 100, nd.tags, true]
852     post :amf_write
853     assert_response :success
854     amf_parse_response
855     result = amf_result("/1")
856
857     assert_equal 2, result.size
858     assert_equal -2, result[0]
859     assert_match /Node is not in the world/, result[1]
860   end
861
862   # check that we can update way
863   def test_putway_update_valid
864     way = current_ways(:way_with_multiple_nodes)
865     cs_id = changesets(:public_user_first_change).id
866     amf_content "putway", "/1", ["test@example.com:test", cs_id, way.version, way.id, way.nds, { "test" => "ok" }, [], {}]
867     post :amf_write
868     assert_response :success
869     amf_parse_response
870     result = amf_result("/1")
871
872     assert_equal 8, result.size
873     assert_equal 0, result[0]
874     assert_equal "", result[1]
875     assert_equal way.id, result[2]
876     assert_equal way.id, result[3]
877     assert_equal({}, result[4])
878     assert_equal way.version + 1, result[5]
879     assert_equal({}, result[6])
880     assert_equal({}, result[7])
881
882     new_way = Way.find(way.id)
883     assert_equal way.version + 1, new_way.version
884     assert_equal way.nds, new_way.nds
885     assert_equal({ "test" => "ok" }, new_way.tags)
886
887     amf_content "putway", "/1", ["test@example.com:test", cs_id, way.version + 1, way.id, [4, 6, 15, 1], way.tags, [], {}]
888     post :amf_write
889     assert_response :success
890     amf_parse_response
891     result = amf_result("/1")
892
893     assert_equal 8, result.size
894     assert_equal 0, result[0]
895     assert_equal "", result[1]
896     assert_equal way.id, result[2]
897     assert_equal way.id, result[3]
898     assert_equal({}, result[4])
899     assert_equal way.version + 2, result[5]
900     assert_equal({}, result[6])
901     assert_equal({}, result[7])
902
903     new_way = Way.find(way.id)
904     assert_equal way.version + 2, new_way.version
905     assert_equal [4, 6, 15, 1], new_way.nds
906     assert_equal way.tags, new_way.tags
907   end
908
909   def test_startchangeset_invalid_xmlchar_comment
910     invalid = "\035\022"
911     comment = "foo#{invalid}bar"
912
913     amf_content "startchangeset", "/1", ["test@example.com:test", {}, nil, comment, 1]
914     post :amf_write
915     assert_response :success
916     amf_parse_response
917     result = amf_result("/1")
918
919     assert_equal 3, result.size, result.inspect
920     assert_equal 0, result[0]
921     new_cs_id = result[2].to_i
922
923     cs = Changeset.find(new_cs_id)
924     assert_equal "foobar", cs.tags["comment"]
925   end
926
927   private
928
929   # ************************************************************
930   # AMF Helper functions
931
932   # Get the result record for the specified ID
933   # It's an assertion FAIL if the record does not exist
934   def amf_result(ref)
935     assert @amf_result.key?("#{ref}/onResult")
936     @amf_result["#{ref}/onResult"]
937   end
938
939   # Encode the AMF message to invoke "target" with parameters as
940   # the passed data. The ref is used to retrieve the results.
941   def amf_content(target, ref, data)
942     a, b = 1.divmod(256)
943     c = StringIO.new
944     c.write 0.chr + 0.chr   # version 0
945     c.write 0.chr + 0.chr   # n headers
946     c.write a.chr + b.chr   # n bodies
947     c.write AMF.encodestring(target)
948     c.write AMF.encodestring(ref)
949     c.write [-1].pack("N")
950     c.write AMF.encodevalue(data)
951
952     @request.env["RAW_POST_DATA"] = c.string
953   end
954
955   # Parses the @response object as an AMF messsage.
956   # The result is a hash of message_ref => data.
957   # The attribute @amf_result is initialised to this hash.
958   def amf_parse_response
959     req = StringIO.new(@response.body)
960
961     req.read(2)   # version
962
963     # parse through any headers
964     headers = AMF.getint(req)           # Read number of headers
965     headers.times do                    # Read each header
966       AMF.getstring(req)                #  |
967       req.getc                          #  | skip boolean
968       AMF.getvalue(req)                 #  |
969     end
970
971     # parse through responses
972     results = {}
973     bodies = AMF.getint(req)            # Read number of bodies
974     bodies.times do                     # Read each body
975       message = AMF.getstring(req)      #  | get message name
976       AMF.getstring(req)                #  | get index in response sequence
977       AMF.getlong(req)                  #  | get total size in bytes
978       args = AMF.getvalue(req)          #  | get response (probably an array)
979       results[message] = args
980     end
981     @amf_result = results
982     results
983   end
984
985   ##
986   # given an array of bounding boxes (each an array of 4 floats), call the
987   # AMF "whichways" controller for each and pass the result back to the
988   # caller's block for assertion testing.
989   def check_bboxes_are_bad(bboxes)
990     bboxes.each do |bbox|
991       amf_content "whichways", "/1", bbox
992       post :amf_read
993       assert_response :success
994       amf_parse_response
995
996       # pass the response back to the caller's block to be tested
997       # against what the caller expected.
998       map = amf_result "/1"
999       yield map, bbox
1000     end
1001   end
1002
1003   # this should be what AMF controller returns when the bbox of a
1004   # whichways request is invalid or too large.
1005   def assert_boundary_error(map, msg = nil, error_hint = nil)
1006     expected_map = [-2, "Sorry - I can't get the map for that area.#{msg}"]
1007     assert_equal expected_map, map, "AMF controller should have returned an error. (#{error_hint})"
1008   end
1009
1010   # this should be what AMF controller returns when the bbox of a
1011   # whichways_deleted request is invalid or too large.
1012   def assert_deleted_boundary_error(map, msg = nil, error_hint = nil)
1013     expected_map = [-2, "Sorry - I can't get the map for that area.#{msg}"]
1014     assert_equal expected_map, map, "AMF controller should have returned an error. (#{error_hint})"
1015   end
1016 end