1 require File.dirname(__FILE__) + '/../test_helper'
5 class AmfControllerTest < ActionController::TestCase
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."]
14 id = current_ways(:visible_way).id
15 amf_content "getway", "/1", [id]
17 assert_response :success
19 assert_equal amf_result("/1")[0], id
22 def test_getway_invisible
23 # check an invisible way
24 id = current_ways(:invisible_way).id
25 amf_content "getway", "/1", [id]
27 assert_response :success
29 way = amf_result("/1")
30 assert_equal way[0], id
31 assert way[1].empty? and way[2].empty?
34 def test_getway_nonexistent
35 # check chat a non-existent way is not returned
36 amf_content "getway", "/1", [0]
38 assert_response :success
40 way = amf_result("/1")
41 assert_equal way[0], 0
42 assert way[1].empty? and way[2].empty?
46 node = current_nodes(:used_node_1)
51 amf_content "whichways", "/1", [minlon, minlat, maxlon, maxlat]
53 assert_response :success
56 # check contents of message
58 assert_equal 0, map[0], 'first map element should be 0'
59 assert_equal Array, map[1].class, 'second map element should be an array'
60 # TODO: looks like amf_controller changed since this test was written
61 # so someone who knows what they're doing should check this!
62 ways = map[1].collect { |x| x[0] }
63 assert ways.include?(current_ways(:used_way).id),
64 "map should include used way"
65 assert !ways.include?(current_ways(:invisible_way).id),
66 'map should not include deleted way'
70 # checks that too-large a bounding box will not be served.
71 def test_whichways_toobig
72 bbox = [-0.1,-0.1,1.1,1.1]
73 check_bboxes_are_bad [bbox] do |map,bbox|
74 assert_equal BOUNDARY_ERROR, map, "AMF controller should have returned an error."
79 # checks that an invalid bounding box will not be served. in this case
80 # one with max < min latitudes.
82 # NOTE: the controller expands the bbox by 0.01 in each direction!
83 def test_whichways_badlat
84 bboxes = [[0,0.1,0.1,0], [-0.1,80,0.1,70], [0.24,54.35,0.25,54.33]]
85 check_bboxes_are_bad bboxes do |map, bbox|
86 assert_equal BOUNDARY_ERROR, map, "AMF controller should have returned an error #{bbox.inspect}."
91 # same as test_whichways_badlat, but for longitudes
93 # NOTE: the controller expands the bbox by 0.01 in each direction!
94 def test_whichways_badlon
95 bboxes = [[80,-0.1,70,0.1], [54.35,0.24,54.33,0.25]]
96 check_bboxes_are_bad bboxes do |map, bbox|
97 assert_equal BOUNDARY_ERROR, map, "AMF controller should have returned an error #{bbox.inspect}."
101 def test_whichways_deleted
102 node = current_nodes(:used_node_1)
103 minlon = node.lon-0.1
104 minlat = node.lat-0.1
105 maxlon = node.lon+0.1
106 maxlat = node.lat+0.1
107 amf_content "whichways_deleted", "/1", [minlon, minlat, maxlon, maxlat]
109 assert_response :success
112 # check contents of message
113 map = amf_result "/1"
114 assert_equal 0, map[0], 'first map element should be 0'
115 assert_equal Array, map[1].class, 'second map element should be an array'
116 # TODO: looks like amf_controller changed since this test was written
117 # so someone who knows what they're doing should check this!
118 assert !map[1].include?(current_ways(:used_way).id),
119 "map should not include used way"
120 assert map[1].include?(current_ways(:invisible_way).id),
121 'map should include deleted way'
124 def test_whichways_deleted_toobig
125 bbox = [-0.1,-0.1,1.1,1.1]
126 amf_content "whichways_deleted", "/1", bbox
128 assert_response :success
131 map = amf_result "/1"
132 assert_equal BOUNDARY_ERROR, map, "AMF controller should have returned an error."
136 id = current_relations(:visible_relation).id
137 amf_content "getrelation", "/1", [id]
139 assert_response :success
141 assert_equal amf_result("/1")[0], id
144 def test_getrelation_invisible
145 id = current_relations(:invisible_relation).id
146 amf_content "getrelation", "/1", [id]
148 assert_response :success
150 rel = amf_result("/1")
151 assert_equal rel[0], id
152 assert rel[1].empty? and rel[2].empty?
155 def test_getrelation_nonexistent
157 amf_content "getrelation", "/1", [id]
159 assert_response :success
161 rel = amf_result("/1")
162 assert_equal rel[0], id
163 assert rel[1].empty? and rel[2].empty?
167 # try to get the last visible version (specified by <0) (should be current version)
168 latest = current_ways(:way_with_versions)
169 # try to get version 1
170 v1 = ways(:way_with_versions_v1)
171 {latest => -1, v1 => v1.version}.each do |way, v|
172 amf_content "getway_old", "/1", [way.id, v]
174 assert_response :success
176 returned_way = amf_result("/1")
177 assert_equal returned_way[1], way.id
178 assert_equal returned_way[4], way.version
182 def test_getway_old_nonexistent
183 # try to get the last version+10 (shoudn't exist)
184 latest = current_ways(:way_with_versions)
185 # try to get last visible version of non-existent way
186 # try to get specific version of non-existent way
187 {nil => -1, nil => 1, latest => latest.version + 10}.each do |way, v|
188 amf_content "getway_old", "/1", [way.nil? ? 0 : way.id, v]
190 assert_response :success
192 returned_way = amf_result("/1")
193 assert returned_way[2].empty?
194 assert returned_way[3].empty?
195 assert returned_way[4] < 0
199 def test_getway_history
200 latest = current_ways(:way_with_versions)
201 amf_content "getway_history", "/1", [latest.id]
203 assert_response :success
205 history = amf_result("/1")
207 # ['way',wayid,history]
208 assert_equal history[0], 'way'
209 assert_equal history[1], latest.id
210 assert_equal history[2].first[0], latest.version
211 assert_equal history[2].last[0], ways(:way_with_versions_v1).version
214 def test_getway_history_nonexistent
215 amf_content "getway_history", "/1", [0]
217 assert_response :success
219 history = amf_result("/1")
221 # ['way',wayid,history]
222 assert_equal history[0], 'way'
223 assert_equal history[1], 0
224 assert history[2].empty?
227 def test_getnode_history
228 latest = current_nodes(:node_with_versions)
229 amf_content "getnode_history", "/1", [latest.id]
231 assert_response :success
233 history = amf_result("/1")
235 # ['node',nodeid,history]
236 assert_equal history[0], 'node',
237 'first element should be "node"'
238 assert_equal history[1], latest.id,
239 'second element should be the input node ID'
240 # NOTE: changed this test to match what amf_controller actually
241 # outputs - which may or may not be what potlatch is expecting.
242 # someone who knows potlatch (i.e: richard f) should review this.
243 assert_equal history[2].first[0], latest.version,
244 'first part of third element should be the latest version'
245 assert_equal history[2].last[0],
246 nodes(:node_with_versions_v1).version,
247 'second part of third element should be the initial version'
250 def test_getnode_history_nonexistent
251 amf_content "getnode_history", "/1", [0]
253 assert_response :success
255 history = amf_result("/1")
257 # ['node',nodeid,history]
258 assert_equal history[0], 'node'
259 assert_equal history[1], 0
260 assert history[2].empty?
263 # ************************************************************
265 def test_putpoi_update_valid
266 nd = current_nodes(:visible_node)
267 amf_content "putpoi", "/1", ["test@openstreetmap.org:test", nd.changeset_id, nd.version, nd.id, nd.lon, nd.lat, nd.tags, nd.visible]
269 assert_response :success
271 result = amf_result("/1")
273 assert_equal 0, result[0]
274 assert_equal nd.id, result[1]
275 assert_equal nd.id, result[2]
276 assert_equal nd.version+1, result[3]
278 # Now try to update again, with a different lat/lon, using the updated version number
281 amf_content "putpoi", "/2", ["test@openstreetmap.org:test", nd.changeset_id, nd.version+1, nd.id, lon, lat, nd.tags, nd.visible]
283 assert_response :success
285 result = amf_result("/2")
287 assert_equal 0, result[0]
288 assert_equal nd.id, result[1]
289 assert_equal nd.id, result[2]
290 assert_equal nd.version+2, result[3]
293 # Check that we can create a no valid poi
294 # Using similar method for the node controller test
295 def test_putpoi_create_valid
296 # This node has no tags
298 # create a node with random lat/lon
299 lat = rand(100)-50 + rand
300 lon = rand(100)-50 + rand
301 # normal user has a changeset open
302 changeset = changesets(:normal_user_first_change)
304 amf_content "putpoi", "/1", ["test@openstreetmap.org:test", changeset.id, nil, nil, lon, lat, {}, nil]
306 assert_response :success
308 result = amf_result("/1")
310 # check the array returned by the amf
311 assert_equal 4, result.size
312 assert_equal 0, result[0], "expected to get the status ok from the amf"
313 assert_equal 0, result[1], "The old id should be 0"
314 assert result[2] > 0, "The new id should be greater than 0"
315 assert_equal 1, result[3], "The new version should be 1"
317 # Finally check that the node that was saved has saved the data correctly
318 # in both the current and history tables
319 # First check the current table
320 current_node = Node.find(result[2])
321 assert_in_delta lat, current_node.lat, 0.00001, "The latitude was not retreieved correctly"
322 assert_in_delta lon, current_node.lon, 0.00001, "The longitude was not retreived correctly"
323 assert_equal 0, current_node.tags.size, "There seems to be a tag that has been added to the node"
324 assert_equal result[3], current_node.version, "The version returned, is different to the one returned by the amf"
325 # Now check the history table
326 historic_nodes = Node.find(:all, :conditions => { :id => result[2] })
327 assert_equal 1, historic_nodes.size, "There should only be one historic node created"
328 first_historic_node = historic_nodes.first
329 assert_in_delta lat, first_historic_node.lat, 0.00001, "The latitude was not retreived correctly"
330 assert_in_delta lon, first_historic_node.lon, 0.00001, "The longitude was not retreuved correctly"
331 assert_equal 0, first_historic_node.tags.size, "There seems to be a tag that have been attached to this node"
332 assert_equal result[3], first_historic_node.version, "The version returned, is different to the one returned by the amf"
335 # This node has some tags
337 # create a node with random lat/lon
338 lat = rand(100)-50 + rand
339 lon = rand(100)-50 + rand
340 # normal user has a changeset open
341 changeset = changesets(:normal_user_first_change)
343 amf_content "putpoi", "/2", ["test@openstreetmap.org:test", changeset.id, nil, nil, lon, lat, { "key" => "value", "ping" => "pong" }, nil]
345 assert_response :success
347 result = amf_result("/2")
349 # check the array returned by the amf
350 assert_equal 4, result.size
351 assert_equal 0, result[0], "Expected to get the status ok in the amf"
352 assert_equal 0, result[1], "The old id should be 0"
353 assert result[2] > 0, "The new id should be greater than 0"
354 assert_equal 1, result[3], "The new version should be 1"
356 # Finally check that the node that was saved has saved the data correctly
357 # in both the current and history tables
358 # First check the current table
359 current_node = Node.find(result[2])
360 assert_in_delta lat, current_node.lat, 0.00001, "The latitude was not retreieved correctly"
361 assert_in_delta lon, current_node.lon, 0.00001, "The longitude was not retreived correctly"
362 assert_equal 2, current_node.tags.size, "There seems to be a tag that has been added to the node"
363 assert_equal({ "key" => "value", "ping" => "pong" }, current_node.tags, "tags are different")
364 assert_equal result[3], current_node.version, "The version returned, is different to the one returned by the amf"
365 # Now check the history table
366 historic_nodes = Node.find(:all, :conditions => { :id => result[2] })
367 assert_equal 1, historic_nodes.size, "There should only be one historic node created"
368 first_historic_node = historic_nodes.first
369 assert_in_delta lat, first_historic_node.lat, 0.00001, "The latitude was not retreived correctly"
370 assert_in_delta lon, first_historic_node.lon, 0.00001, "The longitude was not retreuved correctly"
371 assert_equal 2, first_historic_node.tags.size, "There seems to be a tag that have been attached to this node"
372 assert_equal({ "key" => "value", "ping" => "pong" }, first_historic_node.tags, "tags are different")
373 assert_equal result[3], first_historic_node.version, "The version returned, is different to the one returned by the amf"
377 # ************************************************************
378 # AMF Helper functions
380 # Get the result record for the specified ID
381 # It's an assertion FAIL if the record does not exist
383 assert @amf_result.has_key?("#{ref}/onResult")
384 @amf_result["#{ref}/onResult"]
387 # Encode the AMF message to invoke "target" with parameters as
388 # the passed data. The ref is used to retrieve the results.
389 def amf_content(target, ref, data)
392 c.write 0.chr+0.chr # version 0
393 c.write 0.chr+0.chr # n headers
394 c.write a.chr+b.chr # n bodies
395 c.write AMF.encodestring(target)
396 c.write AMF.encodestring(ref)
397 c.write [-1].pack("N")
398 c.write AMF.encodevalue(data)
400 @request.env["RAW_POST_DATA"] = c.string
403 # Parses the @response object as an AMF messsage.
404 # The result is a hash of message_ref => data.
405 # The attribute @amf_result is initialised to this hash.
406 def amf_parse_response
407 if @response.body.class.to_s == 'Proc'
409 @response.body.call @response, res
410 req = StringIO.new(res.string)
412 req = StringIO.new(@response.body)
414 req.read(2) # version
416 # parse through any headers
417 headers=AMF.getint(req) # Read number of headers
418 headers.times do # Read each header
419 name=AMF.getstring(req) # |
420 req.getc # | skip boolean
421 value=AMF.getvalue(req) # |
424 # parse through responses
426 bodies=AMF.getint(req) # Read number of bodies
427 bodies.times do # Read each body
428 message=AMF.getstring(req) # | get message name
429 index=AMF.getstring(req) # | get index in response sequence
430 bytes=AMF.getlong(req) # | get total size in bytes
431 args=AMF.getvalue(req) # | get response (probably an array)
432 results[message] = args
434 @amf_result = results
439 # given an array of bounding boxes (each an array of 4 floats), call the
440 # AMF "whichways" controller for each and pass the result back to the
441 # caller's block for assertion testing.
442 def check_bboxes_are_bad(bboxes)
443 bboxes.each do |bbox|
444 amf_content "whichways", "/1", bbox
446 assert_response :success
449 # pass the response back to the caller's block to be tested
450 # against what the caller expected.
451 map = amf_result "/1"