source: subversion/sites/rails_port/test/functional/amf_controller_test.rb @ 17044

Last change on this file since 17044 was 16708, checked in by zere, 10 years ago

Fix compatibility with older versions of the ruby std lib (i.e: the one on cruise.osm.org)

File size: 22.7 KB
Line 
1require File.dirname(__FILE__) + '/../test_helper'
2require 'stringio'
3include Potlatch
4
5class AmfControllerTest < ActionController::TestCase
6  api_fixtures
7
8  def test_getway
9    # check a visible way
10    id = current_ways(:visible_way).id
11    amf_content "getway", "/1", [id]
12    post :amf_read
13    assert_response :success
14    amf_parse_response                                         
15    way = amf_result("/1")
16    assert_equal way[0], 0
17    assert_equal way[2], id
18  end
19
20  def test_getway_invisible
21    # check an invisible way
22    id = current_ways(:invisible_way).id
23    amf_content "getway", "/1", [id]
24    post :amf_read
25    assert_response :success
26    amf_parse_response
27    way = amf_result("/1")
28    assert_equal way[0], -4
29    assert_equal way[1], "way"
30    assert_equal way[2], id
31    assert way[3].empty? and way[4].empty?
32  end
33
34  def test_getway_nonexistent
35    # check chat a non-existent way is not returned
36    amf_content "getway", "/1", [0]
37    post :amf_read
38    assert_response :success
39    amf_parse_response
40    way = amf_result("/1")
41    assert_equal way[0], -4
42    assert_equal way[1], "way"
43    assert_equal way[2], 0
44    assert way[3].empty? and way[4].empty?
45  end
46
47  def test_whichways
48    node = current_nodes(:used_node_1)
49    minlon = node.lon-0.1
50    minlat = node.lat-0.1
51    maxlon = node.lon+0.1
52    maxlat = node.lat+0.1
53    amf_content "whichways", "/1", [minlon, minlat, maxlon, maxlat]
54    post :amf_read
55    assert_response :success
56    amf_parse_response
57
58    # check contents of message
59    map = amf_result "/1"
60    assert_equal 0, map[0], 'map error code should be 0'
61    assert_equal "", map[1], 'map error text should be empty'
62
63    # check the formatting of the message
64    assert_equal 5, map.length, 'map should have length 5'
65    assert_equal Array, map[2].class, 'map "ways" element should be an array'
66    assert_equal Array, map[3].class, 'map "nodes" element should be an array'
67    assert_equal Array, map[4].class, 'map "relations" element should be an array'
68    map[2].each do |w|
69      assert_equal 2, w.length, 'way should be (id, version) pair'
70      assert w[0] == w[0].floor, 'way ID should be an integer'
71      assert w[1] == w[1].floor, 'way version should be an integer'
72    end
73
74    map[3].each do |n|
75      assert_equal 5, w.length, 'node should be (id, lat, lon, [tags], version) tuple'
76      assert n[0] == n[0].floor, 'node ID should be an integer'
77      assert n[1] >= minlat - 0.01, 'node lat should be greater than min'
78      assert n[1] <= maxlat - 0.01, 'node lat should be less than max'
79      assert n[2] >= minlon - 0.01, 'node lon should be greater than min'
80      assert n[2] <= maxlon - 0.01, 'node lon should be less than max'
81      assert_equal Array, a[3].class, 'node tags should be array'
82      assert n[4] == n[4].floor, 'node version should be an integer'
83    end
84
85    map[4].each do |r|
86      assert_equal 2, r.length, 'relation should be (id, version) pair'
87      assert r[0] == r[0].floor, 'relation ID should be an integer'
88      assert r[1] == r[1].floor, 'relation version should be an integer'
89    end
90
91    # TODO: looks like amf_controller changed since this test was written
92    # so someone who knows what they're doing should check this!
93    ways = map[2].collect { |x| x[0] }
94    assert ways.include?(current_ways(:used_way).id),
95      "map should include used way"
96    assert !ways.include?(current_ways(:invisible_way).id),
97      'map should not include deleted way'
98  end
99
100  ##
101  # checks that too-large a bounding box will not be served.
102  def test_whichways_toobig
103    bbox = [-0.1,-0.1,1.1,1.1]
104    check_bboxes_are_bad [bbox] do |map,bbox|
105      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"
106    end
107  end
108
109  ##
110  # checks that an invalid bounding box will not be served. in this case
111  # one with max < min latitudes.
112  #
113  # NOTE: the controller expands the bbox by 0.01 in each direction!
114  def test_whichways_badlat
115    bboxes = [[0,0.1,0.1,0], [-0.1,80,0.1,70], [0.24,54.35,0.25,54.33]]
116    check_bboxes_are_bad bboxes do |map, bbox|
117      assert_boundary_error map, " The server said: The minimum latitude must be less than the maximum latitude, but it wasn't", bbox.inspect
118    end
119  end
120
121  ##
122  # same as test_whichways_badlat, but for longitudes
123  #
124  # NOTE: the controller expands the bbox by 0.01 in each direction!
125  def test_whichways_badlon
126    bboxes = [[80,-0.1,70,0.1], [54.35,0.24,54.33,0.25]]
127    check_bboxes_are_bad bboxes do |map, bbox|
128      assert_boundary_error map, " The server said: The minimum longitude must be less than the maximum longitude, but it wasn't", bbox.inspect
129    end
130  end
131
132  def test_whichways_deleted
133    node = current_nodes(:used_node_1)
134    minlon = node.lon-0.1
135    minlat = node.lat-0.1
136    maxlon = node.lon+0.1
137    maxlat = node.lat+0.1
138    amf_content "whichways_deleted", "/1", [minlon, minlat, maxlon, maxlat]
139    post :amf_read
140    assert_response :success
141    amf_parse_response
142
143    # check contents of message
144    map = amf_result "/1"
145    assert_equal 0, map[0], 'first map element should be 0'
146    assert_equal "", map[1], 'second map element should be an empty string'
147    assert_equal Array, map[2].class, 'third map element should be an array'
148    # TODO: looks like amf_controller changed since this test was written
149    # so someone who knows what they're doing should check this!
150    assert !map[2].include?(current_ways(:used_way).id),
151      "map should not include used way"
152    assert map[2].include?(current_ways(:invisible_way).id),
153      'map should include deleted way'
154  end
155
156  def test_whichways_deleted_toobig
157    bbox = [-0.1,-0.1,1.1,1.1]
158    amf_content "whichways_deleted", "/1", bbox
159    post :amf_read
160    assert_response :success
161    amf_parse_response
162
163    map = amf_result "/1"
164    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"
165  end
166
167  def test_getrelation
168    id = current_relations(:visible_relation).id
169    amf_content "getrelation", "/1", [id]
170    post :amf_read
171    assert_response :success
172    amf_parse_response
173    rel = amf_result("/1")
174    assert_equal rel[0], 0
175    assert_equal rel[2], id
176  end
177
178  def test_getrelation_invisible
179    id = current_relations(:invisible_relation).id
180    amf_content "getrelation", "/1", [id]
181    post :amf_read
182    assert_response :success
183    amf_parse_response
184    rel = amf_result("/1")
185    assert_equal rel[0], -4
186    assert_equal rel[1], "relation"
187    assert_equal rel[2], id
188    assert rel[3].empty? and rel[4].empty?
189  end
190
191  def test_getrelation_nonexistent
192    id = 0
193    amf_content "getrelation", "/1", [id]
194    post :amf_read
195    assert_response :success
196    amf_parse_response
197    rel = amf_result("/1")
198    assert_equal rel[0], -4
199    assert_equal rel[1], "relation"
200    assert_equal rel[2], id
201    assert rel[3].empty? and rel[4].empty?
202  end
203
204  def test_getway_old
205    # try to get the last visible version (specified by <0) (should be current version)
206    latest = current_ways(:way_with_versions)
207    # NOTE: looks from the API changes that this now expects a timestamp
208    # instead of a version number...
209    # try to get version 1
210    v1 = ways(:way_with_versions_v1)
211    { latest => '', 
212      v1 => v1.timestamp.strftime("%d %b %Y, %H:%M:%S")
213    }.each do |way, t|
214      amf_content "getway_old", "/1", [way.id, t]
215      post :amf_read     
216      assert_response :success
217      amf_parse_response
218      returned_way = amf_result("/1")
219      assert_equal 0, returned_way[0]
220      assert_equal way.id, returned_way[2]
221      # API returns the *latest* version, even for old ways...
222      assert_equal latest.version, returned_way[5]
223    end
224  end
225 
226  ##
227  # test that the server doesn't fall over when rubbish is passed
228  # into the method args.
229  def test_getway_old_invalid
230    way_id = current_ways(:way_with_versions).id
231    { "foo"  => "bar",
232      way_id => "not a date",
233      way_id => "2009-03-25 00:00:00", # <- wrong format
234      way_id => "0 Jan 2009 00:00:00", # <- invalid date
235      -1     => "1 Jan 2009 00:00:00"  # <- invalid ID
236    }.each do |id, t|
237      amf_content "getway_old", "/1", [id, t]
238      post :amf_read
239      assert_response :success
240      amf_parse_response
241      returned_way = amf_result("/1")
242      assert_equal -1, returned_way[0]
243      assert returned_way[3].empty?
244      assert returned_way[4].empty?
245      assert returned_way[5].nil?
246    end
247  end
248
249  def test_getway_old_nonexistent
250    # try to get the last version+10 (shoudn't exist)
251    v1 = ways(:way_with_versions_v1)
252    # try to get last visible version of non-existent way
253    # try to get specific version of non-existent way
254    [[nil, ''], 
255     [nil, '1 Jan 1970, 00:00:00'], 
256     [v1, (v1.timestamp - 10).strftime("%d %b %Y, %H:%M:%S")]
257    ].each do |way, t|
258      amf_content "getway_old", "/1", [way.nil? ? 0 : way.id, t]
259      post :amf_read
260      assert_response :success
261      amf_parse_response
262      returned_way = amf_result("/1")
263      assert_equal -1, returned_way[0]
264      assert returned_way[3].empty?
265      assert returned_way[4].empty?
266      assert returned_way[5].nil?
267    end
268  end
269
270  def test_getway_history
271    latest = current_ways(:way_with_versions)
272    oldest = ways(:way_with_versions_v1)
273
274    amf_content "getway_history", "/1", [latest.id]
275    post :amf_read
276    assert_response :success
277    amf_parse_response
278    history = amf_result("/1")
279
280    # ['way',wayid,history]
281    assert_equal 'way', history[0]
282    assert_equal latest.id, history[1] 
283    # We use dates rather than version numbers here, because you might
284    # have moved a node within a way (i.e. way version not incremented).
285    # The timestamp is +1 (timestamp.succ) because we say "give me the
286    # revision of 15:33:02", but that might actually include changes at
287    # 15:33:02.457.
288    assert_equal latest.timestamp.succ.strftime("%d %b %Y, %H:%M:%S"), history[2].first[0]
289    assert_equal oldest.timestamp.succ.strftime("%d %b %Y, %H:%M:%S"), history[2].last[0]
290  end
291
292  def test_getway_history_nonexistent
293    amf_content "getway_history", "/1", [0]
294    post :amf_read
295    assert_response :success
296    amf_parse_response
297    history = amf_result("/1")
298
299    # ['way',wayid,history]
300    assert_equal history[0], 'way'
301    assert_equal history[1], 0
302    assert history[2].empty?
303  end
304
305  def test_getnode_history
306    latest = current_nodes(:node_with_versions)
307    amf_content "getnode_history", "/1", [latest.id]
308    post :amf_read
309    assert_response :success
310    amf_parse_response
311    history = amf_result("/1")
312
313    # ['node',nodeid,history]
314    # note that (as per getway_history) we actually round up
315    # to the next second
316    assert_equal history[0], 'node', 
317      'first element should be "node"'
318    assert_equal history[1], latest.id,
319      'second element should be the input node ID'
320    assert_equal history[2].first[0], 
321      latest.timestamp.succ.strftime("%d %b %Y, %H:%M:%S"),
322      'first element in third element (array) should be the latest version'
323    assert_equal history[2].last[0], 
324      nodes(:node_with_versions_v1).timestamp.succ.strftime("%d %b %Y, %H:%M:%S"),
325      'last element in third element (array) should be the initial version'
326  end
327
328  def test_getnode_history_nonexistent
329    amf_content "getnode_history", "/1", [0]
330    post :amf_read
331    assert_response :success
332    amf_parse_response
333    history = amf_result("/1")
334
335    # ['node',nodeid,history]
336    assert_equal history[0], 'node'
337    assert_equal history[1], 0
338    assert history[2].empty?
339  end
340
341  # ************************************************************
342  # AMF Write tests
343  def test_putpoi_update_valid
344    nd = current_nodes(:visible_node)
345    cs_id = changesets(:public_user_first_change).id
346    amf_content "putpoi", "/1", ["test@example.com:test", cs_id, nd.version, nd.id, nd.lon, nd.lat, nd.tags, nd.visible]
347    post :amf_write
348    assert_response :success
349    amf_parse_response
350    result = amf_result("/1")
351   
352    assert_equal 0, result[0]
353    assert_equal "", result[1]
354    assert_equal nd.id, result[2]
355    assert_equal nd.id, result[3]
356    assert_equal nd.version+1, result[4]
357   
358    # Now try to update again, with a different lat/lon, using the updated version number
359    lat = nd.lat+0.1
360    lon = nd.lon-0.1
361    amf_content "putpoi", "/2", ["test@example.com:test", cs_id, nd.version+1, nd.id, lon, lat, nd.tags, nd.visible]
362    post :amf_write
363    assert_response :success
364    amf_parse_response
365    result = amf_result("/2")
366   
367    assert_equal 0, result[0]
368    assert_equal "", result[1]
369    assert_equal nd.id, result[2]
370    assert_equal nd.id, result[3]
371    assert_equal nd.version+2, result[4]
372  end
373 
374  # Check that we can create a no valid poi
375  # Using similar method for the node controller test
376  def test_putpoi_create_valid
377    # This node has no tags
378    nd = Node.new
379    # create a node with random lat/lon
380    lat = rand(100)-50 + rand
381    lon = rand(100)-50 + rand
382    # normal user has a changeset open
383    changeset = changesets(:public_user_first_change)
384   
385    amf_content "putpoi", "/1", ["test@example.com:test", changeset.id, nil, nil, lon, lat, {}, nil]
386    post :amf_write
387    assert_response :success
388    amf_parse_response
389    result = amf_result("/1")
390   
391    # check the array returned by the amf
392    assert_equal 5, result.size
393    assert_equal 0, result[0], "expected to get the status ok from the amf"
394    assert_equal 0, result[2], "The old id should be 0"
395    assert result[3] > 0, "The new id should be greater than 0"
396    assert_equal 1, result[4], "The new version should be 1"
397   
398    # Finally check that the node that was saved has saved the data correctly
399    # in both the current and history tables
400    # First check the current table
401    current_node = Node.find(result[3])
402    assert_in_delta lat, current_node.lat, 0.00001, "The latitude was not retreieved correctly"
403    assert_in_delta lon, current_node.lon, 0.00001, "The longitude was not retreived correctly"
404    assert_equal 0, current_node.tags.size, "There seems to be a tag that has been added to the node"
405    assert_equal result[4], current_node.version, "The version returned, is different to the one returned by the amf"
406    # Now check the history table
407    historic_nodes = Node.find(:all, :conditions => { :id => result[3] })
408    assert_equal 1, historic_nodes.size, "There should only be one historic node created"
409    first_historic_node = historic_nodes.first
410    assert_in_delta lat, first_historic_node.lat, 0.00001, "The latitude was not retreived correctly"
411    assert_in_delta lon, first_historic_node.lon, 0.00001, "The longitude was not retreuved correctly"
412    assert_equal 0, first_historic_node.tags.size, "There seems to be a tag that have been attached to this node"
413    assert_equal result[4], first_historic_node.version, "The version returned, is different to the one returned by the amf"
414   
415    ####
416    # This node has some tags
417    tnd = Node.new
418    # create a node with random lat/lon
419    lat = rand(100)-50 + rand
420    lon = rand(100)-50 + rand
421    # normal user has a changeset open
422    changeset = changesets(:public_user_first_change)
423   
424    amf_content "putpoi", "/2", ["test@example.com:test", changeset.id, nil, nil, lon, lat, { "key" => "value", "ping" => "pong" }, nil]
425    post :amf_write
426    assert_response :success
427    amf_parse_response
428    result = amf_result("/2")
429
430    # check the array returned by the amf
431    assert_equal 5, result.size
432    assert_equal 0, result[0], "Expected to get the status ok in the amf"
433    assert_equal 0, result[2], "The old id should be 0"
434    assert result[3] > 0, "The new id should be greater than 0"
435    assert_equal 1, result[4], "The new version should be 1"
436   
437    # Finally check that the node that was saved has saved the data correctly
438    # in both the current and history tables
439    # First check the current table
440    current_node = Node.find(result[3])
441    assert_in_delta lat, current_node.lat, 0.00001, "The latitude was not retreieved correctly"
442    assert_in_delta lon, current_node.lon, 0.00001, "The longitude was not retreived correctly"
443    assert_equal 2, current_node.tags.size, "There seems to be a tag that has been added to the node"
444    assert_equal({ "key" => "value", "ping" => "pong" }, current_node.tags, "tags are different")
445    assert_equal result[4], current_node.version, "The version returned, is different to the one returned by the amf"
446    # Now check the history table
447    historic_nodes = Node.find(:all, :conditions => { :id => result[3] })
448    assert_equal 1, historic_nodes.size, "There should only be one historic node created"
449    first_historic_node = historic_nodes.first
450    assert_in_delta lat, first_historic_node.lat, 0.00001, "The latitude was not retreived correctly"
451    assert_in_delta lon, first_historic_node.lon, 0.00001, "The longitude was not retreuved correctly"
452    assert_equal 2, first_historic_node.tags.size, "There seems to be a tag that have been attached to this node"
453    assert_equal({ "key" => "value", "ping" => "pong" }, first_historic_node.tags, "tags are different")
454    assert_equal result[4], first_historic_node.version, "The version returned, is different to the one returned by the amf"
455  end
456 
457  # try creating a POI with rubbish in the tags
458  def test_putpoi_create_with_control_chars
459    # This node has no tags
460    nd = Node.new
461    # create a node with random lat/lon
462    lat = rand(100)-50 + rand
463    lon = rand(100)-50 + rand
464    # normal user has a changeset open
465    changeset = changesets(:public_user_first_change)
466   
467    mostly_invalid = (0..31).to_a.map {|i| i.chr}.join
468    tags = { "something" => "foo#{mostly_invalid}bar" }
469     
470    amf_content "putpoi", "/1", ["test@example.com:test", changeset.id, nil, nil, lon, lat, tags, nil]
471    post :amf_write
472    assert_response :success
473    amf_parse_response
474    result = amf_result("/1")
475     
476    # check the array returned by the amf
477    assert_equal 5, result.size
478    assert_equal 0, result[0], "Expected to get the status ok in the amf"
479    assert_equal 0, result[2], "The old id should be 0"
480    assert result[3] > 0, "The new id should be greater than 0"
481    assert_equal 1, result[4], "The new version should be 1"
482   
483    # Finally check that the node that was saved has saved the data correctly
484    # in both the current and history tables
485    # First check the current table
486    current_node = Node.find(result[3])
487    assert_equal 1, current_node.tags.size, "There seems to be a tag that has been added to the node"
488    assert_equal({ "something" => "foo\t\n\rbar" }, current_node.tags, "tags were not fixed correctly")
489    assert_equal result[4], current_node.version, "The version returned, is different to the one returned by the amf"
490  end
491
492  # try creating a POI with rubbish in the tags
493  def test_putpoi_create_with_invalid_utf8
494    # This node has no tags
495    nd = Node.new
496    # create a node with random lat/lon
497    lat = rand(100)-50 + rand
498    lon = rand(100)-50 + rand
499    # normal user has a changeset open
500    changeset = changesets(:public_user_first_change)
501   
502    invalid = "\xc0\xc0"
503    tags = { "something" => "foo#{invalid}bar" }
504     
505    amf_content "putpoi", "/1", ["test@example.com:test", changeset.id, nil, nil, lon, lat, tags, nil]
506    post :amf_write
507    assert_response :success
508    amf_parse_response
509    result = amf_result("/1")
510
511    assert_equal 2, result.size
512    assert_equal -1, result[0], "Expected to get the status FAIL in the amf"
513    assert_equal "One of the tags is invalid. Please pester Adobe to fix Flash on Linux.", result[1] 
514  end
515     
516  def test_putpoi_delete_valid
517   
518  end
519 
520  def test_putpoi_delete_already_deleted
521   
522  end
523 
524  def test_putpoi_delete_not_found
525   
526  end
527 
528  def test_putpoi_invalid_latlon
529   
530  end
531
532  # ************************************************************
533  # AMF Helper functions
534
535  # Get the result record for the specified ID
536  # It's an assertion FAIL if the record does not exist
537  def amf_result ref
538    assert @amf_result.has_key?("#{ref}/onResult")
539    @amf_result["#{ref}/onResult"]
540  end
541
542  # Encode the AMF message to invoke "target" with parameters as
543  # the passed data. The ref is used to retrieve the results.
544  def amf_content(target, ref, data)
545    a,b=1.divmod(256)
546    c = StringIO.new()
547    c.write 0.chr+0.chr   # version 0
548    c.write 0.chr+0.chr   # n headers
549    c.write a.chr+b.chr   # n bodies
550    c.write AMF.encodestring(target)
551    c.write AMF.encodestring(ref)
552    c.write [-1].pack("N")
553    c.write AMF.encodevalue(data)
554
555    @request.env["RAW_POST_DATA"] = c.string
556  end
557
558  # Parses the @response object as an AMF messsage.
559  # The result is a hash of message_ref => data.
560  # The attribute @amf_result is initialised to this hash.
561  def amf_parse_response
562    if @response.body.class.to_s == 'Proc'
563      res = StringIO.new()
564      @response.body.call @response, res
565      req = StringIO.new(res.string)
566    else
567      req = StringIO.new(@response.body)
568    end
569    req.read(2)   # version
570
571    # parse through any headers
572        headers=AMF.getint(req)                                 # Read number of headers
573        headers.times do                                                # Read each header
574          name=AMF.getstring(req)                               #  |
575          req.getc                                                              #  | skip boolean
576          value=AMF.getvalue(req)                               #  |
577        end
578
579    # parse through responses
580    results = {}
581    bodies=AMF.getint(req)                                      # Read number of bodies
582        bodies.times do                                                 # Read each body
583          message=AMF.getstring(req)                    #  | get message name
584          index=AMF.getstring(req)                              #  | get index in response sequence
585          bytes=AMF.getlong(req)                                #  | get total size in bytes
586          args=AMF.getvalue(req)                                #  | get response (probably an array)
587      results[message] = args
588    end
589    @amf_result = results
590    results
591  end
592
593  ##
594  # given an array of bounding boxes (each an array of 4 floats), call the
595  # AMF "whichways" controller for each and pass the result back to the
596  # caller's block for assertion testing.
597  def check_bboxes_are_bad(bboxes)
598    bboxes.each do |bbox|
599      amf_content "whichways", "/1", bbox
600      post :amf_read
601      assert_response :success
602      amf_parse_response
603
604      # pass the response back to the caller's block to be tested
605      # against what the caller expected.
606      map = amf_result "/1"
607      yield map, bbox
608    end
609  end
610 
611  # this should be what AMF controller returns when the bbox of a
612  # whichways request is invalid or too large.
613  def assert_boundary_error(map, msg=nil, error_hint=nil)
614    expected_map = [-2, "Sorry - I can't get the map for that area.#{msg}"]
615    assert_equal expected_map, map, "AMF controller should have returned an error. (#{error_hint})"
616  end
617
618  # this should be what AMF controller returns when the bbox of a
619  # whichways_deleted request is invalid or too large.
620  def assert_deleted_boundary_error(map, msg=nil, error_hint=nil)
621    expected_map = [-2, "Sorry - I can't get the map for that area.#{msg}"]
622    assert_equal expected_map, map, "AMF controller should have returned an error. (#{error_hint})"
623  end
624end
Note: See TracBrowser for help on using the repository browser.