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