]> git.openstreetmap.org Git - rails.git/blob - test/functional/amf_controller_test.rb
count -> size for compatibility with ruby 1.8.6
[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   # this should be what AMF controller returns when the bbox of a request
9   # is invalid or too large.
10   BOUNDARY_ERROR = [-2,"Sorry - I can't get the map for that area."]
11
12   def test_getway
13     # check a visible way
14     id = current_ways(:visible_way).id
15     amf_content "getway", "/1", [id]
16     post :amf_read
17     assert_response :success
18     amf_parse_response                                         
19     assert_equal amf_result("/1")[0], id
20   end
21
22   def test_getway_invisible
23     # check an invisible way
24     id = current_ways(:invisible_way).id
25     amf_content "getway", "/1", [id]
26     post :amf_read
27     assert_response :success
28     amf_parse_response
29     way = amf_result("/1")
30     assert_equal way[0], id
31     assert way[1].empty? and way[2].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], 0
42     assert way[1].empty? and way[2].empty?
43   end
44
45   def test_whichways
46     node = current_nodes(:used_node_1)
47     minlon = node.lon-0.1
48     minlat = node.lat-0.1
49     maxlon = node.lon+0.1
50     maxlat = node.lat+0.1
51     amf_content "whichways", "/1", [minlon, minlat, maxlon, maxlat]
52     post :amf_read
53     assert_response :success
54     amf_parse_response 
55
56     # check contents of message
57     map = amf_result "/1"
58     assert_equal 0, map[0]
59     assert_equal Array, map[1].class
60     assert map[1].include?(current_ways(:used_way).id)
61     assert !map[1].include?(current_ways(:invisible_way).id)
62   end
63
64   ##
65   # checks that too-large a bounding box will not be served.
66   def test_whichways_toobig
67     bbox = [-0.1,-0.1,1.1,1.1]
68     check_bboxes_are_bad [bbox] do |map|
69       assert_equal BOUNDARY_ERROR, map, "AMF controller should have returned an error."
70     end
71   end
72
73   ##
74   # checks that an invalid bounding box will not be served. in this case
75   # one with max < min latitudes.
76   def test_whichways_badlat
77     bboxes = [[0,0.1,0.1,0], [-0.1,80,0.1,70], [0.24,54.34,0.25,54.33]]
78     check_bboxes_are_bad bboxes do |map|
79       assert_equal BOUNDARY_ERROR, map, "AMF controller should have returned an error."
80     end
81   end
82
83   ##
84   # same as test_whichways_badlat, but for longitudes
85   def test_whichways_badlon
86     bboxes = [[80,-0.1,70,0.1], [54.34,0.24,54.33,0.25]]
87     check_bboxes_are_bad bboxes do |map|
88       assert_equal BOUNDARY_ERROR, map, "AMF controller should have returned an error."
89     end
90   end
91
92   def test_whichways_deleted
93     node = current_nodes(:used_node_1)
94     minlon = node.lon-0.1
95     minlat = node.lat-0.1
96     maxlon = node.lon+0.1
97     maxlat = node.lat+0.1
98     amf_content "whichways_deleted", "/1", [minlon, minlat, maxlon, maxlat]
99     post :amf_read
100     assert_response :success
101     amf_parse_response
102
103     # check contents of message
104     map = amf_result "/1"
105     assert_equal 0, map[0]
106     assert_equal Array, map[1].class
107     assert map[1].include?(current_ways(:used_way).id)
108     assert !map[1].include?(current_ways(:invisible_way).id)
109   end
110
111   def test_whichways_deleted_toobig
112     bbox = [-0.1,-0.1,1.1,1.1]
113     amf_content "whichways_deleted", "/1", bbox
114     post :amf_read
115     assert_response :success
116     amf_parse_response 
117
118     map = amf_result "/1"
119     assert_equal BOUNDARY_ERROR, map, "AMF controller should have returned an error."
120   end
121
122   def test_getrelation
123     id = current_relations(:visible_relation).id
124     amf_content "getrelation", "/1", [id]
125     post :amf_read
126     assert_response :success
127     amf_parse_response
128     assert_equal amf_result("/1")[0], id
129   end
130
131   def test_getrelation_invisible
132     id = current_relations(:invisible_relation).id
133     amf_content "getrelation", "/1", [id]
134     post :amf_read
135     assert_response :success
136     amf_parse_response
137     rel = amf_result("/1")
138     assert_equal rel[0], id
139     assert rel[1].empty? and rel[2].empty?
140   end
141
142   def test_getrelation_nonexistent
143     id = 0
144     amf_content "getrelation", "/1", [id]
145     post :amf_read
146     assert_response :success
147     amf_parse_response
148     rel = amf_result("/1")
149     assert_equal rel[0], id
150     assert rel[1].empty? and rel[2].empty?
151   end
152
153   def test_getway_old
154     # try to get the last visible version (specified by <0) (should be current version)
155     latest = current_ways(:way_with_versions)
156     # try to get version 1
157     v1 = ways(:way_with_versions_v1)
158     {latest => -1, v1 => v1.version}.each do |way, v|
159       amf_content "getway_old", "/1", [way.id, v]
160       post :amf_read
161       assert_response :success
162       amf_parse_response
163       returned_way = amf_result("/1")
164       assert_equal returned_way[1], way.id
165       assert_equal returned_way[4], way.version
166     end
167   end
168
169   def test_getway_old_nonexistent
170     # try to get the last version+10 (shoudn't exist)
171     latest = current_ways(:way_with_versions)
172     # try to get last visible version of non-existent way
173     # try to get specific version of non-existent way
174     {nil => -1, nil => 1, latest => latest.version + 10}.each do |way, v|
175       amf_content "getway_old", "/1", [way.nil? ? 0 : way.id, v]
176       post :amf_read
177       assert_response :success
178       amf_parse_response
179       returned_way = amf_result("/1")
180       assert returned_way[2].empty?
181       assert returned_way[3].empty?
182       assert returned_way[4] < 0
183     end
184   end
185
186   def test_getway_history
187     latest = current_ways(:way_with_versions)
188     amf_content "getway_history", "/1", [latest.id]
189     post :amf_read
190     assert_response :success
191     amf_parse_response
192     history = amf_result("/1")
193
194     # ['way',wayid,history]
195     assert_equal history[0], 'way'
196     assert_equal history[1], latest.id
197     assert_equal history[2].first[0], latest.version
198     assert_equal history[2].last[0], ways(:way_with_versions_v1).version
199   end
200
201   def test_getway_history_nonexistent
202     amf_content "getway_history", "/1", [0]
203     post :amf_read
204     assert_response :success
205     amf_parse_response
206     history = amf_result("/1")
207
208     # ['way',wayid,history]
209     assert_equal history[0], 'way'
210     assert_equal history[1], 0
211     assert history[2].empty?
212   end
213
214   def test_getnode_history
215     latest = current_nodes(:node_with_versions)
216     amf_content "getnode_history", "/1", [latest.id]
217     post :amf_read
218     assert_response :success
219     amf_parse_response
220     history = amf_result("/1")
221
222     # ['node',nodeid,history]
223     assert_equal history[0], 'node'
224     assert_equal history[1], latest.id
225     assert_equal history[2].first[0], latest.timestamp.to_i
226     assert_equal history[2].last[0], nodes(:node_with_versions_v1).timestamp.to_i
227   end
228
229   def test_getnode_history_nonexistent
230     amf_content "getnode_history", "/1", [0]
231     post :amf_read
232     assert_response :success
233     amf_parse_response
234     history = amf_result("/1")
235
236     # ['node',nodeid,history]
237     assert_equal history[0], 'node'
238     assert_equal history[1], 0
239     assert history[2].empty?
240   end
241
242   # ************************************************************
243   # AMF Write tests
244   def test_putpoi_update_valid
245     nd = current_nodes(:visible_node)
246     amf_content "putpoi", "/1", ["test@openstreetmap.org:test", nd.changeset_id, nd.version, nd.id, nd.lon, nd.lat, nd.tags, nd.visible]
247     post :amf_write
248     assert_response :success
249     amf_parse_response
250     result = amf_result("/1")
251     
252     assert_equal 0, result[0]
253     assert_equal nd.id, result[1]
254     assert_equal nd.id, result[2]
255     assert_equal nd.version+1, result[3]
256     
257     # Now try to update again, with a different lat/lon, using the updated version number
258     lat = nd.lat+0.1
259     lon = nd.lon-0.1
260     amf_content "putpoi", "/2", ["test@openstreetmap.org:test", nd.changeset_id, nd.version+1, nd.id, lon, lat, nd.tags, nd.visible]
261     post :amf_write
262     assert_response :success
263     amf_parse_response
264     result = amf_result("/2")
265     
266     assert_equal 0, result[0]
267     assert_equal nd.id, result[1]
268     assert_equal nd.id, result[2]
269     assert_equal nd.version+2, result[3]
270   end
271   
272   # Check that we can create a no valid poi
273   # Using similar method for the node controller test
274   def test_putpoi_create_valid
275     # This node has no tags
276     nd = Node.new
277     # create a node with random lat/lon
278     lat = rand(100)-50 + rand
279     lon = rand(100)-50 + rand
280     # normal user has a changeset open
281     changeset = changesets(:normal_user_first_change)
282     
283     amf_content "putpoi", "/1", ["test@openstreetmap.org:test", changeset.id, nil, nil, lon, lat, {}, nil]
284     post :amf_write
285     assert_response :success
286     amf_parse_response
287     result = amf_result("/1")
288     
289     # check the array returned by the amf
290     assert_equal 4, result.size
291     assert_equal 0, result[0], "expected to get the status ok from the amf"
292     assert_equal 0, result[1], "The old id should be 0"
293     assert result[2] > 0, "The new id should be greater than 0"
294     assert_equal 1, result[3], "The new version should be 1"
295     
296     # Finally check that the node that was saved has saved the data correctly 
297     # in both the current and history tables
298     # First check the current table
299     current_node = Node.find(result[2])
300     assert_in_delta lat, current_node.lat, 0.00001, "The latitude was not retreieved correctly"
301     assert_in_delta lon, current_node.lon, 0.00001, "The longitude was not retreived correctly"
302     assert_equal 0, current_node.tags.size, "There seems to be a tag that has been added to the node"
303     assert_equal result[3], current_node.version, "The version returned, is different to the one returned by the amf"
304     # Now check the history table
305     historic_nodes = Node.find(:all, :conditions => { :id => result[2] })
306     assert_equal 1, historic_nodes.size, "There should only be one historic node created"
307     first_historic_node = historic_nodes.first
308     assert_in_delta lat, first_historic_node.lat, 0.00001, "The latitude was not retreived correctly"
309     assert_in_delta lon, first_historic_node.lon, 0.00001, "The longitude was not retreuved correctly"
310     assert_equal 0, first_historic_node.tags.size, "There seems to be a tag that have been attached to this node"
311     assert_equal result[3], first_historic_node.version, "The version returned, is different to the one returned by the amf"
312     
313     ####
314     # This node has some tags
315     tnd = Node.new
316     # create a node with random lat/lon
317     lat = rand(100)-50 + rand
318     lon = rand(100)-50 + rand
319     # normal user has a changeset open
320     changeset = changesets(:normal_user_first_change)
321     
322     amf_content "putpoi", "/2", ["test@openstreetmap.org:test", changeset.id, nil, nil, lon, lat, { "key" => "value", "ping" => "pong" }, nil]
323     post :amf_write
324     assert_response :success
325     amf_parse_response
326     result = amf_result("/2")
327
328     # check the array returned by the amf
329     assert_equal 4, result.size
330     assert_equal 0, result[0], "Expected to get the status ok in the amf"
331     assert_equal 0, result[1], "The old id should be 0"
332     assert result[2] > 0, "The new id should be greater than 0"
333     assert_equal 1, result[3], "The new version should be 1"
334     
335     # Finally check that the node that was saved has saved the data correctly 
336     # in both the current and history tables
337     # First check the current table
338     current_node = Node.find(result[2])
339     assert_in_delta lat, current_node.lat, 0.00001, "The latitude was not retreieved correctly"
340     assert_in_delta lon, current_node.lon, 0.00001, "The longitude was not retreived correctly"
341     assert_equal 2, current_node.tags.size, "There seems to be a tag that has been added to the node"
342     assert_equal({ "key" => "value", "ping" => "pong" }, current_node.tags, "tags are different")
343     assert_equal result[3], current_node.version, "The version returned, is different to the one returned by the amf"
344     # Now check the history table
345     historic_nodes = Node.find(:all, :conditions => { :id => result[2] })
346     assert_equal 1, historic_nodes.size, "There should only be one historic node created"
347     first_historic_node = historic_nodes.first
348     assert_in_delta lat, first_historic_node.lat, 0.00001, "The latitude was not retreived correctly"
349     assert_in_delta lon, first_historic_node.lon, 0.00001, "The longitude was not retreuved correctly"
350     assert_equal 2, first_historic_node.tags.size, "There seems to be a tag that have been attached to this node"
351     assert_equal({ "key" => "value", "ping" => "pong" }, first_historic_node.tags, "tags are different")
352     assert_equal result[3], first_historic_node.version, "The version returned, is different to the one returned by the amf"
353
354   end
355
356   # ************************************************************
357   # AMF Helper functions
358
359   # Get the result record for the specified ID
360   # It's an assertion FAIL if the record does not exist
361   def amf_result ref
362     assert @amf_result.has_key?("#{ref}/onResult")
363     @amf_result["#{ref}/onResult"]
364   end
365
366   # Encode the AMF message to invoke "target" with parameters as
367   # the passed data. The ref is used to retrieve the results.
368   def amf_content(target, ref, data)
369     a,b=1.divmod(256)
370     c = StringIO.new()
371     c.write 0.chr+0.chr   # version 0
372     c.write 0.chr+0.chr   # n headers
373     c.write a.chr+b.chr   # n bodies
374     c.write AMF.encodestring(target)
375     c.write AMF.encodestring(ref)
376     c.write [-1].pack("N")
377     c.write AMF.encodevalue(data)
378
379     @request.env["RAW_POST_DATA"] = c.string
380   end
381
382   # Parses the @response object as an AMF messsage.
383   # The result is a hash of message_ref => data.
384   # The attribute @amf_result is initialised to this hash.
385   def amf_parse_response
386     if @response.body.class.to_s == 'Proc'
387       res = StringIO.new()
388       @response.body.call @response, res
389       req = StringIO.new(res.string)
390     else
391       req = StringIO.new(@response.body)
392     end
393     req.read(2)   # version
394
395     # parse through any headers
396         headers=AMF.getint(req)                                 # Read number of headers
397         headers.times do                                                # Read each header
398           name=AMF.getstring(req)                               #  |
399           req.getc                                                              #  | skip boolean
400           value=AMF.getvalue(req)                               #  |
401         end
402
403     # parse through responses
404     results = {}
405     bodies=AMF.getint(req)                                      # Read number of bodies
406         bodies.times do                                                 # Read each body
407           message=AMF.getstring(req)                    #  | get message name
408           index=AMF.getstring(req)                              #  | get index in response sequence
409           bytes=AMF.getlong(req)                                #  | get total size in bytes
410           args=AMF.getvalue(req)                                #  | get response (probably an array)
411       results[message] = args
412     end
413     @amf_result = results
414     results
415   end
416
417   ##
418   # given an array of bounding boxes (each an array of 4 floats), call the
419   # AMF "whichways" controller for each and pass the result back to the
420   # caller's block for assertion testing.
421   def check_bboxes_are_bad(bboxes)
422     bboxes.each do |bbox|
423       amf_content "whichways", "/1", bbox
424       post :amf_read
425       assert_response :success
426       amf_parse_response
427
428       # pass the response back to the caller's block to be tested
429       # against what the caller expected.
430       map = amf_result "/1"
431       yield map
432     end
433   end
434 end