]> git.openstreetmap.org Git - rails.git/blob - test/functional/amf_controller_test.rb
cosmetic tweaks to diary interface. icon for new entries link
[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   # ************************************************************
244   # AMF Helper functions
245
246   # Get the result record for the specified ID
247   # It's an assertion FAIL if the record does not exist
248   def amf_result ref
249     assert @amf_result.has_key?("#{ref}/onResult")
250     @amf_result["#{ref}/onResult"]
251   end
252
253   # Encode the AMF message to invoke "target" with parameters as
254   # the passed data. The ref is used to retrieve the results.
255   def amf_content(target, ref, data)
256     a,b=1.divmod(256)
257     c = StringIO.new()
258     c.write 0.chr+0.chr   # version 0
259     c.write 0.chr+0.chr   # n headers
260     c.write a.chr+b.chr   # n bodies
261     c.write AMF.encodestring(target)
262     c.write AMF.encodestring(ref)
263     c.write [-1].pack("N")
264     c.write AMF.encodevalue(data)
265
266     @request.env["RAW_POST_DATA"] = c.string
267   end
268
269   # Parses the @response object as an AMF messsage.
270   # The result is a hash of message_ref => data.
271   # The attribute @amf_result is initialised to this hash.
272   def amf_parse_response
273     if @response.body.class.to_s == 'Proc'
274       res = StringIO.new()
275       @response.body.call @response, res
276       req = StringIO.new(res.string)
277     else
278       req = StringIO.new(@response.body)
279     end
280     req.read(2)   # version
281
282     # parse through any headers
283         headers=AMF.getint(req)                                 # Read number of headers
284         headers.times do                                                # Read each header
285           name=AMF.getstring(req)                               #  |
286           req.getc                                                              #  | skip boolean
287           value=AMF.getvalue(req)                               #  |
288         end
289
290     # parse through responses
291     results = {}
292     bodies=AMF.getint(req)                                      # Read number of bodies
293         bodies.times do                                                 # Read each body
294           message=AMF.getstring(req)                    #  | get message name
295           index=AMF.getstring(req)                              #  | get index in response sequence
296           bytes=AMF.getlong(req)                                #  | get total size in bytes
297           args=AMF.getvalue(req)                                #  | get response (probably an array)
298       results[message] = args
299     end
300     @amf_result = results
301     results
302   end
303
304   ##
305   # given an array of bounding boxes (each an array of 4 floats), call the
306   # AMF "whichways" controller for each and pass the result back to the
307   # caller's block for assertion testing.
308   def check_bboxes_are_bad(bboxes)
309     bboxes.each do |bbox|
310       amf_content "whichways", "/1", bbox
311       post :amf_read
312       assert_response :success
313       amf_parse_response
314
315       # pass the response back to the caller's block to be tested
316       # against what the caller expected.
317       map = amf_result "/1"
318       yield map
319     end
320   end
321 end