1 require File.dirname(__FILE__) + '/../test_helper'
5 class AmfControllerTest < ActionController::TestCase
10 id = current_ways(:visible_way).id
11 amf_content "getway", "/1", [id]
13 assert_response :success
15 assert_equal amf_result("/1")[0], id
18 def test_getway_invisible
19 # check an invisible way
20 id = current_ways(:invisible_way).id
21 amf_content "getway", "/1", [id]
23 assert_response :success
25 way = amf_result("/1")
26 assert_equal way[0], id
27 assert way[1].empty? and way[2].empty?
30 def test_getway_nonexistent
31 # check chat a non-existent way is not returned
32 amf_content "getway", "/1", [0]
34 assert_response :success
36 way = amf_result("/1")
37 assert_equal way[0], 0
38 assert way[1].empty? and way[2].empty?
42 node = current_nodes(:used_node_1)
47 amf_content "whichways", "/1", [minlon, minlat, maxlon, maxlat]
49 assert_response :success
52 # check contents of message
54 assert_equal 0, map[0], 'map error code should be 0'
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'
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'
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'
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'
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'
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"
103 # checks that an invalid bounding box will not be served. in this case
104 # one with max < min latitudes.
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
115 # same as test_whichways_badlat, but for longitudes
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
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]
133 assert_response :success
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'
148 def test_whichways_deleted_toobig
149 bbox = [-0.1,-0.1,1.1,1.1]
150 amf_content "whichways_deleted", "/1", bbox
152 assert_response :success
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"
160 id = current_relations(:visible_relation).id
161 amf_content "getrelation", "/1", [id]
163 assert_response :success
165 assert_equal amf_result("/1")[0], id
168 def test_getrelation_invisible
169 id = current_relations(:invisible_relation).id
170 amf_content "getrelation", "/1", [id]
172 assert_response :success
174 rel = amf_result("/1")
175 assert_equal rel[0], id
176 assert rel[1].empty? and rel[2].empty?
179 def test_getrelation_nonexistent
181 amf_content "getrelation", "/1", [id]
183 assert_response :success
185 rel = amf_result("/1")
186 assert_equal rel[0], id
187 assert rel[1].empty? and rel[2].empty?
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)
198 v1 => v1.timestamp.strftime("%d %b %Y, %H:%M:%S")
200 amf_content "getway_old", "/1", [way.id, t]
202 assert_response :success
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]
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
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
222 amf_content "getway_old", "/1", [id, t]
224 assert_response :success
226 returned_way = amf_result("/1")
227 assert returned_way[2].empty?
228 assert returned_way[3].empty?
229 assert returned_way[4] < 0
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
239 [nil, '1 Jan 1970, 00:00:00'],
240 [v1, (v1.timestamp - 10).strftime("%d %b %Y, %H:%M:%S")]
242 amf_content "getway_old", "/1", [way.nil? ? 0 : way.id, t]
244 assert_response :success
246 returned_way = amf_result("/1")
247 assert returned_way[2].empty?
248 assert returned_way[3].empty?
249 assert returned_way[4] < 0
253 def test_getway_history
254 latest = current_ways(:way_with_versions)
255 oldest = ways(:way_with_versions_v1)
257 amf_content "getway_history", "/1", [latest.id]
259 assert_response :success
261 history = amf_result("/1")
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]
272 def test_getway_history_nonexistent
273 amf_content "getway_history", "/1", [0]
275 assert_response :success
277 history = amf_result("/1")
279 # ['way',wayid,history]
280 assert_equal history[0], 'way'
281 assert_equal history[1], 0
282 assert history[2].empty?
285 def test_getnode_history
286 latest = current_nodes(:node_with_versions)
287 amf_content "getnode_history", "/1", [latest.id]
289 assert_response :success
291 history = amf_result("/1")
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'
311 def test_getnode_history_nonexistent
312 amf_content "getnode_history", "/1", [0]
314 assert_response :success
316 history = amf_result("/1")
318 # ['node',nodeid,history]
319 assert_equal history[0], 'node'
320 assert_equal history[1], 0
321 assert history[2].empty?
324 # ************************************************************
326 def test_putpoi_update_valid
327 nd = current_nodes(:visible_node)
328 cs_id = changesets(:public_user_first_change).id
329 amf_content "putpoi", "/1", ["test@example.com:test", cs_id, nd.version, nd.id, nd.lon, nd.lat, nd.tags, nd.visible]
331 assert_response :success
333 result = amf_result("/1")
335 assert_equal 0, result[0]
336 assert_equal nd.id, result[1]
337 assert_equal nd.id, result[2]
338 assert_equal nd.version+1, result[3]
340 # Now try to update again, with a different lat/lon, using the updated version number
343 amf_content "putpoi", "/2", ["test@example.com:test", cs_id, nd.version+1, nd.id, lon, lat, nd.tags, nd.visible]
345 assert_response :success
347 result = amf_result("/2")
349 assert_equal 0, result[0]
350 assert_equal nd.id, result[1]
351 assert_equal nd.id, result[2]
352 assert_equal nd.version+2, result[3]
355 # Check that we can create a no valid poi
356 # Using similar method for the node controller test
357 def test_putpoi_create_valid
358 # This node has no tags
360 # create a node with random lat/lon
361 lat = rand(100)-50 + rand
362 lon = rand(100)-50 + rand
363 # normal user has a changeset open
364 changeset = changesets(:public_user_first_change)
366 amf_content "putpoi", "/1", ["test@example.com:test", changeset.id, nil, nil, lon, lat, {}, nil]
368 assert_response :success
370 result = amf_result("/1")
372 # check the array returned by the amf
373 assert_equal 4, result.size
374 assert_equal 0, result[0], "expected to get the status ok from the amf"
375 assert_equal 0, result[1], "The old id should be 0"
376 assert result[2] > 0, "The new id should be greater than 0"
377 assert_equal 1, result[3], "The new version should be 1"
379 # Finally check that the node that was saved has saved the data correctly
380 # in both the current and history tables
381 # First check the current table
382 current_node = Node.find(result[2])
383 assert_in_delta lat, current_node.lat, 0.00001, "The latitude was not retreieved correctly"
384 assert_in_delta lon, current_node.lon, 0.00001, "The longitude was not retreived correctly"
385 assert_equal 0, current_node.tags.size, "There seems to be a tag that has been added to the node"
386 assert_equal result[3], current_node.version, "The version returned, is different to the one returned by the amf"
387 # Now check the history table
388 historic_nodes = Node.find(:all, :conditions => { :id => result[2] })
389 assert_equal 1, historic_nodes.size, "There should only be one historic node created"
390 first_historic_node = historic_nodes.first
391 assert_in_delta lat, first_historic_node.lat, 0.00001, "The latitude was not retreived correctly"
392 assert_in_delta lon, first_historic_node.lon, 0.00001, "The longitude was not retreuved correctly"
393 assert_equal 0, first_historic_node.tags.size, "There seems to be a tag that have been attached to this node"
394 assert_equal result[3], first_historic_node.version, "The version returned, is different to the one returned by the amf"
397 # This node has some tags
399 # create a node with random lat/lon
400 lat = rand(100)-50 + rand
401 lon = rand(100)-50 + rand
402 # normal user has a changeset open
403 changeset = changesets(:public_user_first_change)
405 amf_content "putpoi", "/2", ["test@example.com:test", changeset.id, nil, nil, lon, lat, { "key" => "value", "ping" => "pong" }, nil]
407 assert_response :success
409 result = amf_result("/2")
411 # check the array returned by the amf
412 assert_equal 4, result.size
413 assert_equal 0, result[0], "Expected to get the status ok in the amf"
414 assert_equal 0, result[1], "The old id should be 0"
415 assert result[2] > 0, "The new id should be greater than 0"
416 assert_equal 1, result[3], "The new version should be 1"
418 # Finally check that the node that was saved has saved the data correctly
419 # in both the current and history tables
420 # First check the current table
421 current_node = Node.find(result[2])
422 assert_in_delta lat, current_node.lat, 0.00001, "The latitude was not retreieved correctly"
423 assert_in_delta lon, current_node.lon, 0.00001, "The longitude was not retreived correctly"
424 assert_equal 2, current_node.tags.size, "There seems to be a tag that has been added to the node"
425 assert_equal({ "key" => "value", "ping" => "pong" }, current_node.tags, "tags are different")
426 assert_equal result[3], current_node.version, "The version returned, is different to the one returned by the amf"
427 # Now check the history table
428 historic_nodes = Node.find(:all, :conditions => { :id => result[2] })
429 assert_equal 1, historic_nodes.size, "There should only be one historic node created"
430 first_historic_node = historic_nodes.first
431 assert_in_delta lat, first_historic_node.lat, 0.00001, "The latitude was not retreived correctly"
432 assert_in_delta lon, first_historic_node.lon, 0.00001, "The longitude was not retreuved correctly"
433 assert_equal 2, first_historic_node.tags.size, "There seems to be a tag that have been attached to this node"
434 assert_equal({ "key" => "value", "ping" => "pong" }, first_historic_node.tags, "tags are different")
435 assert_equal result[3], first_historic_node.version, "The version returned, is different to the one returned by the amf"
438 def test_putpoi_delete_valid
442 def test_putpoi_delete_already_deleted
446 def test_putpoi_delete_not_found
450 def test_putpoi_invalid_latlon
454 # ************************************************************
455 # AMF Helper functions
457 # Get the result record for the specified ID
458 # It's an assertion FAIL if the record does not exist
460 assert @amf_result.has_key?("#{ref}/onResult")
461 @amf_result["#{ref}/onResult"]
464 # Encode the AMF message to invoke "target" with parameters as
465 # the passed data. The ref is used to retrieve the results.
466 def amf_content(target, ref, data)
469 c.write 0.chr+0.chr # version 0
470 c.write 0.chr+0.chr # n headers
471 c.write a.chr+b.chr # n bodies
472 c.write AMF.encodestring(target)
473 c.write AMF.encodestring(ref)
474 c.write [-1].pack("N")
475 c.write AMF.encodevalue(data)
477 @request.env["RAW_POST_DATA"] = c.string
480 # Parses the @response object as an AMF messsage.
481 # The result is a hash of message_ref => data.
482 # The attribute @amf_result is initialised to this hash.
483 def amf_parse_response
484 if @response.body.class.to_s == 'Proc'
486 @response.body.call @response, res
487 req = StringIO.new(res.string)
489 req = StringIO.new(@response.body)
491 req.read(2) # version
493 # parse through any headers
494 headers=AMF.getint(req) # Read number of headers
495 headers.times do # Read each header
496 name=AMF.getstring(req) # |
497 req.getc # | skip boolean
498 value=AMF.getvalue(req) # |
501 # parse through responses
503 bodies=AMF.getint(req) # Read number of bodies
504 bodies.times do # Read each body
505 message=AMF.getstring(req) # | get message name
506 index=AMF.getstring(req) # | get index in response sequence
507 bytes=AMF.getlong(req) # | get total size in bytes
508 args=AMF.getvalue(req) # | get response (probably an array)
509 results[message] = args
511 @amf_result = results
516 # given an array of bounding boxes (each an array of 4 floats), call the
517 # AMF "whichways" controller for each and pass the result back to the
518 # caller's block for assertion testing.
519 def check_bboxes_are_bad(bboxes)
520 bboxes.each do |bbox|
521 amf_content "whichways", "/1", bbox
523 assert_response :success
526 # pass the response back to the caller's block to be tested
527 # against what the caller expected.
528 map = amf_result "/1"
533 # this should be what AMF controller returns when the bbox of a request
534 # is invalid or too large.
535 def assert_boundary_error(map, msg=nil, error_hint=nil)
536 expected_map = [-2, "Sorry - I can't get the map for that area.#{msg}"]
537 assert_equal expected_map, map, "AMF controller should have returned an error. (#{error_hint})"