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 amf_content "putpoi", "/1", ["test@openstreetmap.org:test", nd.changeset_id, nd.version, nd.id, nd.lon, nd.lat, nd.tags, nd.visible]
330 assert_response :success
332 result = amf_result("/1")
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]
339 # Now try to update again, with a different lat/lon, using the updated version number
342 amf_content "putpoi", "/2", ["test@openstreetmap.org:test", nd.changeset_id, nd.version+1, nd.id, lon, lat, nd.tags, nd.visible]
344 assert_response :success
346 result = amf_result("/2")
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]
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
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)
365 amf_content "putpoi", "/1", ["test@openstreetmap.org:test", changeset.id, nil, nil, lon, lat, {}, nil]
367 assert_response :success
369 result = amf_result("/1")
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"
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"
396 # This node has some tags
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)
404 amf_content "putpoi", "/2", ["test@openstreetmap.org:test", changeset.id, nil, nil, lon, lat, { "key" => "value", "ping" => "pong" }, nil]
406 assert_response :success
408 result = amf_result("/2")
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"
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"
437 def test_putpoi_delete_valid
441 def test_putpoi_delete_already_deleted
445 def test_putpoi_delete_not_found
449 def test_putpoi_invalid_latlon
453 # ************************************************************
454 # AMF Helper functions
456 # Get the result record for the specified ID
457 # It's an assertion FAIL if the record does not exist
459 assert @amf_result.has_key?("#{ref}/onResult")
460 @amf_result["#{ref}/onResult"]
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)
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)
476 @request.env["RAW_POST_DATA"] = c.string
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'
485 @response.body.call @response, res
486 req = StringIO.new(res.string)
488 req = StringIO.new(@response.body)
490 req.read(2) # version
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) # |
500 # parse through responses
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
510 @amf_result = results
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
522 assert_response :success
525 # pass the response back to the caller's block to be tested
526 # against what the caller expected.
527 map = amf_result "/1"
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})"