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 way = amf_result("/1")
16 assert_equal way[0], 0
17 assert_equal way[2], id
20 def test_getway_invisible
21 # check an invisible way
22 id = current_ways(:invisible_way).id
23 amf_content "getway", "/1", [id]
25 assert_response :success
27 way = amf_result("/1")
28 assert_equal way[0], -4
29 assert_equal way[1], "way"
30 assert_equal way[2], id
31 assert way[3].empty? and way[4].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], -4
42 assert_equal way[1], "way"
43 assert_equal way[2], 0
44 assert way[3].empty? and way[4].empty?
48 node = current_nodes(:used_node_1)
53 amf_content "whichways", "/1", [minlon, minlat, maxlon, maxlat]
55 assert_response :success
58 # check contents of message
60 assert_equal 0, map[0], 'map error code should be 0'
61 assert_equal "", map[1], 'map error text should be empty'
63 # check the formatting of the message
64 assert_equal 5, map.length, 'map should have length 5'
65 assert_equal Array, map[2].class, 'map "ways" element should be an array'
66 assert_equal Array, map[3].class, 'map "nodes" element should be an array'
67 assert_equal Array, map[4].class, 'map "relations" element should be an array'
69 assert_equal 2, w.length, 'way should be (id, version) pair'
70 assert w[0] == w[0].floor, 'way ID should be an integer'
71 assert w[1] == w[1].floor, 'way version should be an integer'
75 assert_equal 5, w.length, 'node should be (id, lat, lon, [tags], version) tuple'
76 assert n[0] == n[0].floor, 'node ID should be an integer'
77 assert n[1] >= minlat - 0.01, 'node lat should be greater than min'
78 assert n[1] <= maxlat - 0.01, 'node lat should be less than max'
79 assert n[2] >= minlon - 0.01, 'node lon should be greater than min'
80 assert n[2] <= maxlon - 0.01, 'node lon should be less than max'
81 assert_equal Array, a[3].class, 'node tags should be array'
82 assert n[4] == n[4].floor, 'node version should be an integer'
86 assert_equal 2, r.length, 'relation should be (id, version) pair'
87 assert r[0] == r[0].floor, 'relation ID should be an integer'
88 assert r[1] == r[1].floor, 'relation version should be an integer'
91 # TODO: looks like amf_controller changed since this test was written
92 # so someone who knows what they're doing should check this!
93 ways = map[2].collect { |x| x[0] }
94 assert ways.include?(current_ways(:used_way).id),
95 "map should include used way"
96 assert !ways.include?(current_ways(:invisible_way).id),
97 'map should not include deleted way'
101 # checks that too-large a bounding box will not be served.
102 def test_whichways_toobig
103 bbox = [-0.1,-0.1,1.1,1.1]
104 check_bboxes_are_bad [bbox] do |map,bbox|
105 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"
110 # checks that an invalid bounding box will not be served. in this case
111 # one with max < min latitudes.
113 # NOTE: the controller expands the bbox by 0.01 in each direction!
114 def test_whichways_badlat
115 bboxes = [[0,0.1,0.1,0], [-0.1,80,0.1,70], [0.24,54.35,0.25,54.33]]
116 check_bboxes_are_bad bboxes do |map, bbox|
117 assert_boundary_error map, " The server said: The minimum latitude must be less than the maximum latitude, but it wasn't", bbox.inspect
122 # same as test_whichways_badlat, but for longitudes
124 # NOTE: the controller expands the bbox by 0.01 in each direction!
125 def test_whichways_badlon
126 bboxes = [[80,-0.1,70,0.1], [54.35,0.24,54.33,0.25]]
127 check_bboxes_are_bad bboxes do |map, bbox|
128 assert_boundary_error map, " The server said: The minimum longitude must be less than the maximum longitude, but it wasn't", bbox.inspect
132 def test_whichways_deleted
133 node = current_nodes(:used_node_1)
134 minlon = node.lon-0.1
135 minlat = node.lat-0.1
136 maxlon = node.lon+0.1
137 maxlat = node.lat+0.1
138 amf_content "whichways_deleted", "/1", [minlon, minlat, maxlon, maxlat]
140 assert_response :success
143 # check contents of message
144 map = amf_result "/1"
145 assert_equal 0, map[0], 'first map element should be 0'
146 assert_equal "", map[1], 'second map element should be an empty string'
147 assert_equal Array, map[2].class, 'third map element should be an array'
148 # TODO: looks like amf_controller changed since this test was written
149 # so someone who knows what they're doing should check this!
150 assert !map[2].include?(current_ways(:used_way).id),
151 "map should not include used way"
152 assert map[2].include?(current_ways(:invisible_way).id),
153 'map should include deleted way'
156 def test_whichways_deleted_toobig
157 bbox = [-0.1,-0.1,1.1,1.1]
158 amf_content "whichways_deleted", "/1", bbox
160 assert_response :success
163 map = amf_result "/1"
164 assert_deleted_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"
168 id = current_relations(:visible_relation).id
169 amf_content "getrelation", "/1", [id]
171 assert_response :success
173 rel = amf_result("/1")
174 assert_equal rel[0], 0
175 assert_equal rel[2], id
178 def test_getrelation_invisible
179 id = current_relations(:invisible_relation).id
180 amf_content "getrelation", "/1", [id]
182 assert_response :success
184 rel = amf_result("/1")
185 assert_equal rel[0], -4
186 assert_equal rel[1], "relation"
187 assert_equal rel[2], id
188 assert rel[3].empty? and rel[4].empty?
191 def test_getrelation_nonexistent
193 amf_content "getrelation", "/1", [id]
195 assert_response :success
197 rel = amf_result("/1")
198 assert_equal rel[0], -4
199 assert_equal rel[1], "relation"
200 assert_equal rel[2], id
201 assert rel[3].empty? and rel[4].empty?
205 # try to get the last visible version (specified by <0) (should be current version)
206 latest = current_ways(:way_with_versions)
207 # NOTE: looks from the API changes that this now expects a timestamp
208 # instead of a version number...
209 # try to get version 1
210 v1 = ways(:way_with_versions_v1)
212 v1 => v1.timestamp.strftime("%d %b %Y, %H:%M:%S")
214 amf_content "getway_old", "/1", [way.id, t]
216 assert_response :success
218 returned_way = amf_result("/1")
219 assert_equal 0, returned_way[0]
220 assert_equal way.id, returned_way[2]
221 # API returns the *latest* version, even for old ways...
222 assert_equal latest.version, returned_way[5]
227 # test that the server doesn't fall over when rubbish is passed
228 # into the method args.
229 def test_getway_old_invalid
230 way_id = current_ways(:way_with_versions).id
232 way_id => "not a date",
233 way_id => "2009-03-25 00:00:00", # <- wrong format
234 way_id => "0 Jan 2009 00:00:00", # <- invalid date
235 -1 => "1 Jan 2009 00:00:00" # <- invalid ID
237 amf_content "getway_old", "/1", [id, t]
239 assert_response :success
241 returned_way = amf_result("/1")
242 assert_equal -1, returned_way[0]
243 assert returned_way[3].empty?
244 assert returned_way[4].empty?
245 assert returned_way[5].nil?
249 def test_getway_old_nonexistent
250 # try to get the last version+10 (shoudn't exist)
251 v1 = ways(:way_with_versions_v1)
252 # try to get last visible version of non-existent way
253 # try to get specific version of non-existent way
255 [nil, '1 Jan 1970, 00:00:00'],
256 [v1, (v1.timestamp - 10).strftime("%d %b %Y, %H:%M:%S")]
258 amf_content "getway_old", "/1", [way.nil? ? 0 : way.id, t]
260 assert_response :success
262 returned_way = amf_result("/1")
263 assert_equal -1, returned_way[0]
264 assert returned_way[3].empty?
265 assert returned_way[4].empty?
266 assert returned_way[5].nil?
270 def test_getway_history
271 latest = current_ways(:way_with_versions)
272 oldest = ways(:way_with_versions_v1)
274 amf_content "getway_history", "/1", [latest.id]
276 assert_response :success
278 history = amf_result("/1")
280 # ['way',wayid,history]
281 assert_equal 'way', history[0]
282 assert_equal latest.id, history[1]
283 # for some reason undocumented, the potlatch API now prefers dates
284 # over version numbers. presumably no-one edits concurrently any more?
285 assert_equal latest.timestamp.strftime("%d %b %Y, %H:%M:%S"), history[2].first[0]
286 assert_equal oldest.timestamp.strftime("%d %b %Y, %H:%M:%S"), history[2].last[0]
289 def test_getway_history_nonexistent
290 amf_content "getway_history", "/1", [0]
292 assert_response :success
294 history = amf_result("/1")
296 # ['way',wayid,history]
297 assert_equal history[0], 'way'
298 assert_equal history[1], 0
299 assert history[2].empty?
302 def test_getnode_history
303 latest = current_nodes(:node_with_versions)
304 amf_content "getnode_history", "/1", [latest.id]
306 assert_response :success
308 history = amf_result("/1")
310 # ['node',nodeid,history]
311 assert_equal history[0], 'node',
312 'first element should be "node"'
313 assert_equal history[1], latest.id,
314 'second element should be the input node ID'
315 # NOTE: changed this test to match what amf_controller actually
316 # outputs - which may or may not be what potlatch is expecting.
317 # someone who knows potlatch (i.e: richard f) should review this.
318 # NOTE2: wow - this is the second time this has changed in the
319 # API and the tests are being patched up.
320 assert_equal history[2].first[0],
321 latest.timestamp.strftime("%d %b %Y, %H:%M:%S"),
322 'first part of third element should be the latest version'
323 assert_equal history[2].last[0],
324 nodes(:node_with_versions_v1).timestamp.strftime("%d %b %Y, %H:%M:%S"),
325 'second part of third element should be the initial version'
328 def test_getnode_history_nonexistent
329 amf_content "getnode_history", "/1", [0]
331 assert_response :success
333 history = amf_result("/1")
335 # ['node',nodeid,history]
336 assert_equal history[0], 'node'
337 assert_equal history[1], 0
338 assert history[2].empty?
341 # ************************************************************
343 def test_putpoi_update_valid
344 nd = current_nodes(:visible_node)
345 cs_id = changesets(:public_user_first_change).id
346 amf_content "putpoi", "/1", ["test@example.com:test", cs_id, nd.version, nd.id, nd.lon, nd.lat, nd.tags, nd.visible]
348 assert_response :success
350 result = amf_result("/1")
352 assert_equal 0, result[0]
353 assert_equal "", result[1]
354 assert_equal nd.id, result[2]
355 assert_equal nd.id, result[3]
356 assert_equal nd.version+1, result[4]
358 # Now try to update again, with a different lat/lon, using the updated version number
361 amf_content "putpoi", "/2", ["test@example.com:test", cs_id, nd.version+1, nd.id, lon, lat, nd.tags, nd.visible]
363 assert_response :success
365 result = amf_result("/2")
367 assert_equal 0, result[0]
368 assert_equal "", result[1]
369 assert_equal nd.id, result[2]
370 assert_equal nd.id, result[3]
371 assert_equal nd.version+2, result[4]
374 # Check that we can create a no valid poi
375 # Using similar method for the node controller test
376 def test_putpoi_create_valid
377 # This node has no tags
379 # create a node with random lat/lon
380 lat = rand(100)-50 + rand
381 lon = rand(100)-50 + rand
382 # normal user has a changeset open
383 changeset = changesets(:public_user_first_change)
385 amf_content "putpoi", "/1", ["test@example.com:test", changeset.id, nil, nil, lon, lat, {}, nil]
387 assert_response :success
389 result = amf_result("/1")
391 # check the array returned by the amf
392 assert_equal 5, result.size
393 assert_equal 0, result[0], "expected to get the status ok from the amf"
394 assert_equal 0, result[2], "The old id should be 0"
395 assert result[3] > 0, "The new id should be greater than 0"
396 assert_equal 1, result[4], "The new version should be 1"
398 # Finally check that the node that was saved has saved the data correctly
399 # in both the current and history tables
400 # First check the current table
401 current_node = Node.find(result[3])
402 assert_in_delta lat, current_node.lat, 0.00001, "The latitude was not retreieved correctly"
403 assert_in_delta lon, current_node.lon, 0.00001, "The longitude was not retreived correctly"
404 assert_equal 0, current_node.tags.size, "There seems to be a tag that has been added to the node"
405 assert_equal result[4], current_node.version, "The version returned, is different to the one returned by the amf"
406 # Now check the history table
407 historic_nodes = Node.find(:all, :conditions => { :id => result[3] })
408 assert_equal 1, historic_nodes.size, "There should only be one historic node created"
409 first_historic_node = historic_nodes.first
410 assert_in_delta lat, first_historic_node.lat, 0.00001, "The latitude was not retreived correctly"
411 assert_in_delta lon, first_historic_node.lon, 0.00001, "The longitude was not retreuved correctly"
412 assert_equal 0, first_historic_node.tags.size, "There seems to be a tag that have been attached to this node"
413 assert_equal result[4], first_historic_node.version, "The version returned, is different to the one returned by the amf"
416 # This node has some tags
418 # create a node with random lat/lon
419 lat = rand(100)-50 + rand
420 lon = rand(100)-50 + rand
421 # normal user has a changeset open
422 changeset = changesets(:public_user_first_change)
424 amf_content "putpoi", "/2", ["test@example.com:test", changeset.id, nil, nil, lon, lat, { "key" => "value", "ping" => "pong" }, nil]
426 assert_response :success
428 result = amf_result("/2")
430 # check the array returned by the amf
431 assert_equal 5, result.size
432 assert_equal 0, result[0], "Expected to get the status ok in the amf"
433 assert_equal 0, result[2], "The old id should be 0"
434 assert result[3] > 0, "The new id should be greater than 0"
435 assert_equal 1, result[4], "The new version should be 1"
437 # Finally check that the node that was saved has saved the data correctly
438 # in both the current and history tables
439 # First check the current table
440 current_node = Node.find(result[3])
441 assert_in_delta lat, current_node.lat, 0.00001, "The latitude was not retreieved correctly"
442 assert_in_delta lon, current_node.lon, 0.00001, "The longitude was not retreived correctly"
443 assert_equal 2, current_node.tags.size, "There seems to be a tag that has been added to the node"
444 assert_equal({ "key" => "value", "ping" => "pong" }, current_node.tags, "tags are different")
445 assert_equal result[4], current_node.version, "The version returned, is different to the one returned by the amf"
446 # Now check the history table
447 historic_nodes = Node.find(:all, :conditions => { :id => result[3] })
448 assert_equal 1, historic_nodes.size, "There should only be one historic node created"
449 first_historic_node = historic_nodes.first
450 assert_in_delta lat, first_historic_node.lat, 0.00001, "The latitude was not retreived correctly"
451 assert_in_delta lon, first_historic_node.lon, 0.00001, "The longitude was not retreuved correctly"
452 assert_equal 2, first_historic_node.tags.size, "There seems to be a tag that have been attached to this node"
453 assert_equal({ "key" => "value", "ping" => "pong" }, first_historic_node.tags, "tags are different")
454 assert_equal result[4], first_historic_node.version, "The version returned, is different to the one returned by the amf"
457 def test_putpoi_delete_valid
461 def test_putpoi_delete_already_deleted
465 def test_putpoi_delete_not_found
469 def test_putpoi_invalid_latlon
473 # ************************************************************
474 # AMF Helper functions
476 # Get the result record for the specified ID
477 # It's an assertion FAIL if the record does not exist
479 assert @amf_result.has_key?("#{ref}/onResult")
480 @amf_result["#{ref}/onResult"]
483 # Encode the AMF message to invoke "target" with parameters as
484 # the passed data. The ref is used to retrieve the results.
485 def amf_content(target, ref, data)
488 c.write 0.chr+0.chr # version 0
489 c.write 0.chr+0.chr # n headers
490 c.write a.chr+b.chr # n bodies
491 c.write AMF.encodestring(target)
492 c.write AMF.encodestring(ref)
493 c.write [-1].pack("N")
494 c.write AMF.encodevalue(data)
496 @request.env["RAW_POST_DATA"] = c.string
499 # Parses the @response object as an AMF messsage.
500 # The result is a hash of message_ref => data.
501 # The attribute @amf_result is initialised to this hash.
502 def amf_parse_response
503 if @response.body.class.to_s == 'Proc'
505 @response.body.call @response, res
506 req = StringIO.new(res.string)
508 req = StringIO.new(@response.body)
510 req.read(2) # version
512 # parse through any headers
513 headers=AMF.getint(req) # Read number of headers
514 headers.times do # Read each header
515 name=AMF.getstring(req) # |
516 req.getc # | skip boolean
517 value=AMF.getvalue(req) # |
520 # parse through responses
522 bodies=AMF.getint(req) # Read number of bodies
523 bodies.times do # Read each body
524 message=AMF.getstring(req) # | get message name
525 index=AMF.getstring(req) # | get index in response sequence
526 bytes=AMF.getlong(req) # | get total size in bytes
527 args=AMF.getvalue(req) # | get response (probably an array)
528 results[message] = args
530 @amf_result = results
535 # given an array of bounding boxes (each an array of 4 floats), call the
536 # AMF "whichways" controller for each and pass the result back to the
537 # caller's block for assertion testing.
538 def check_bboxes_are_bad(bboxes)
539 bboxes.each do |bbox|
540 amf_content "whichways", "/1", bbox
542 assert_response :success
545 # pass the response back to the caller's block to be tested
546 # against what the caller expected.
547 map = amf_result "/1"
552 # this should be what AMF controller returns when the bbox of a
553 # whichways request is invalid or too large.
554 def assert_boundary_error(map, msg=nil, error_hint=nil)
555 expected_map = [-2, "Sorry - I can't get the map for that area.#{msg}", [], [], []]
556 assert_equal expected_map, map, "AMF controller should have returned an error. (#{error_hint})"
559 # this should be what AMF controller returns when the bbox of a
560 # whichways_deleted request is invalid or too large.
561 def assert_deleted_boundary_error(map, msg=nil, error_hint=nil)
562 expected_map = [-2, "Sorry - I can't get the map for that area.#{msg}", []]
563 assert_equal expected_map, map, "AMF controller should have returned an error. (#{error_hint})"