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 # We use dates rather than version numbers here, because you might
284 # have moved a node within a way (i.e. way version not incremented).
285 # The timestamp is +1 (timestamp.succ) because we say "give me the
286 # revision of 15:33:02", but that might actually include changes at
288 assert_equal latest.timestamp.succ.strftime("%d %b %Y, %H:%M:%S"), history[2].first[0]
289 assert_equal oldest.timestamp.succ.strftime("%d %b %Y, %H:%M:%S"), history[2].last[0]
292 def test_getway_history_nonexistent
293 amf_content "getway_history", "/1", [0]
295 assert_response :success
297 history = amf_result("/1")
299 # ['way',wayid,history]
300 assert_equal history[0], 'way'
301 assert_equal history[1], 0
302 assert history[2].empty?
305 def test_getnode_history
306 latest = current_nodes(:node_with_versions)
307 amf_content "getnode_history", "/1", [latest.id]
309 assert_response :success
311 history = amf_result("/1")
313 # ['node',nodeid,history]
314 assert_equal history[0], 'node',
315 'first element should be "node"'
316 assert_equal history[1], latest.id,
317 'second element should be the input node ID'
318 # NOTE: changed this test to match what amf_controller actually
319 # outputs - which may or may not be what potlatch is expecting.
320 # someone who knows potlatch (i.e: richard f) should review this.
321 # NOTE2: wow - this is the second time this has changed in the
322 # API and the tests are being patched up.
323 assert_equal history[2].first[0],
324 latest.timestamp.strftime("%d %b %Y, %H:%M:%S"),
325 'first part of third element should be the latest version'
326 assert_equal history[2].last[0],
327 nodes(:node_with_versions_v1).timestamp.strftime("%d %b %Y, %H:%M:%S"),
328 'second part of third element should be the initial version'
331 def test_getnode_history_nonexistent
332 amf_content "getnode_history", "/1", [0]
334 assert_response :success
336 history = amf_result("/1")
338 # ['node',nodeid,history]
339 assert_equal history[0], 'node'
340 assert_equal history[1], 0
341 assert history[2].empty?
344 # ************************************************************
346 def test_putpoi_update_valid
347 nd = current_nodes(:visible_node)
348 cs_id = changesets(:public_user_first_change).id
349 amf_content "putpoi", "/1", ["test@example.com:test", cs_id, nd.version, nd.id, nd.lon, nd.lat, nd.tags, nd.visible]
351 assert_response :success
353 result = amf_result("/1")
355 assert_equal 0, result[0]
356 assert_equal "", result[1]
357 assert_equal nd.id, result[2]
358 assert_equal nd.id, result[3]
359 assert_equal nd.version+1, result[4]
361 # Now try to update again, with a different lat/lon, using the updated version number
364 amf_content "putpoi", "/2", ["test@example.com:test", cs_id, nd.version+1, nd.id, lon, lat, nd.tags, nd.visible]
366 assert_response :success
368 result = amf_result("/2")
370 assert_equal 0, result[0]
371 assert_equal "", result[1]
372 assert_equal nd.id, result[2]
373 assert_equal nd.id, result[3]
374 assert_equal nd.version+2, result[4]
377 # Check that we can create a no valid poi
378 # Using similar method for the node controller test
379 def test_putpoi_create_valid
380 # This node has no tags
382 # create a node with random lat/lon
383 lat = rand(100)-50 + rand
384 lon = rand(100)-50 + rand
385 # normal user has a changeset open
386 changeset = changesets(:public_user_first_change)
388 amf_content "putpoi", "/1", ["test@example.com:test", changeset.id, nil, nil, lon, lat, {}, nil]
390 assert_response :success
392 result = amf_result("/1")
394 # check the array returned by the amf
395 assert_equal 5, result.size
396 assert_equal 0, result[0], "expected to get the status ok from the amf"
397 assert_equal 0, result[2], "The old id should be 0"
398 assert result[3] > 0, "The new id should be greater than 0"
399 assert_equal 1, result[4], "The new version should be 1"
401 # Finally check that the node that was saved has saved the data correctly
402 # in both the current and history tables
403 # First check the current table
404 current_node = Node.find(result[3])
405 assert_in_delta lat, current_node.lat, 0.00001, "The latitude was not retreieved correctly"
406 assert_in_delta lon, current_node.lon, 0.00001, "The longitude was not retreived correctly"
407 assert_equal 0, current_node.tags.size, "There seems to be a tag that has been added to the node"
408 assert_equal result[4], current_node.version, "The version returned, is different to the one returned by the amf"
409 # Now check the history table
410 historic_nodes = Node.find(:all, :conditions => { :id => result[3] })
411 assert_equal 1, historic_nodes.size, "There should only be one historic node created"
412 first_historic_node = historic_nodes.first
413 assert_in_delta lat, first_historic_node.lat, 0.00001, "The latitude was not retreived correctly"
414 assert_in_delta lon, first_historic_node.lon, 0.00001, "The longitude was not retreuved correctly"
415 assert_equal 0, first_historic_node.tags.size, "There seems to be a tag that have been attached to this node"
416 assert_equal result[4], first_historic_node.version, "The version returned, is different to the one returned by the amf"
419 # This node has some tags
421 # create a node with random lat/lon
422 lat = rand(100)-50 + rand
423 lon = rand(100)-50 + rand
424 # normal user has a changeset open
425 changeset = changesets(:public_user_first_change)
427 amf_content "putpoi", "/2", ["test@example.com:test", changeset.id, nil, nil, lon, lat, { "key" => "value", "ping" => "pong" }, nil]
429 assert_response :success
431 result = amf_result("/2")
433 # check the array returned by the amf
434 assert_equal 5, result.size
435 assert_equal 0, result[0], "Expected to get the status ok in the amf"
436 assert_equal 0, result[2], "The old id should be 0"
437 assert result[3] > 0, "The new id should be greater than 0"
438 assert_equal 1, result[4], "The new version should be 1"
440 # Finally check that the node that was saved has saved the data correctly
441 # in both the current and history tables
442 # First check the current table
443 current_node = Node.find(result[3])
444 assert_in_delta lat, current_node.lat, 0.00001, "The latitude was not retreieved correctly"
445 assert_in_delta lon, current_node.lon, 0.00001, "The longitude was not retreived correctly"
446 assert_equal 2, current_node.tags.size, "There seems to be a tag that has been added to the node"
447 assert_equal({ "key" => "value", "ping" => "pong" }, current_node.tags, "tags are different")
448 assert_equal result[4], current_node.version, "The version returned, is different to the one returned by the amf"
449 # Now check the history table
450 historic_nodes = Node.find(:all, :conditions => { :id => result[3] })
451 assert_equal 1, historic_nodes.size, "There should only be one historic node created"
452 first_historic_node = historic_nodes.first
453 assert_in_delta lat, first_historic_node.lat, 0.00001, "The latitude was not retreived correctly"
454 assert_in_delta lon, first_historic_node.lon, 0.00001, "The longitude was not retreuved correctly"
455 assert_equal 2, first_historic_node.tags.size, "There seems to be a tag that have been attached to this node"
456 assert_equal({ "key" => "value", "ping" => "pong" }, first_historic_node.tags, "tags are different")
457 assert_equal result[4], first_historic_node.version, "The version returned, is different to the one returned by the amf"
460 def test_putpoi_delete_valid
464 def test_putpoi_delete_already_deleted
468 def test_putpoi_delete_not_found
472 def test_putpoi_invalid_latlon
476 # ************************************************************
477 # AMF Helper functions
479 # Get the result record for the specified ID
480 # It's an assertion FAIL if the record does not exist
482 assert @amf_result.has_key?("#{ref}/onResult")
483 @amf_result["#{ref}/onResult"]
486 # Encode the AMF message to invoke "target" with parameters as
487 # the passed data. The ref is used to retrieve the results.
488 def amf_content(target, ref, data)
491 c.write 0.chr+0.chr # version 0
492 c.write 0.chr+0.chr # n headers
493 c.write a.chr+b.chr # n bodies
494 c.write AMF.encodestring(target)
495 c.write AMF.encodestring(ref)
496 c.write [-1].pack("N")
497 c.write AMF.encodevalue(data)
499 @request.env["RAW_POST_DATA"] = c.string
502 # Parses the @response object as an AMF messsage.
503 # The result is a hash of message_ref => data.
504 # The attribute @amf_result is initialised to this hash.
505 def amf_parse_response
506 if @response.body.class.to_s == 'Proc'
508 @response.body.call @response, res
509 req = StringIO.new(res.string)
511 req = StringIO.new(@response.body)
513 req.read(2) # version
515 # parse through any headers
516 headers=AMF.getint(req) # Read number of headers
517 headers.times do # Read each header
518 name=AMF.getstring(req) # |
519 req.getc # | skip boolean
520 value=AMF.getvalue(req) # |
523 # parse through responses
525 bodies=AMF.getint(req) # Read number of bodies
526 bodies.times do # Read each body
527 message=AMF.getstring(req) # | get message name
528 index=AMF.getstring(req) # | get index in response sequence
529 bytes=AMF.getlong(req) # | get total size in bytes
530 args=AMF.getvalue(req) # | get response (probably an array)
531 results[message] = args
533 @amf_result = results
538 # given an array of bounding boxes (each an array of 4 floats), call the
539 # AMF "whichways" controller for each and pass the result back to the
540 # caller's block for assertion testing.
541 def check_bboxes_are_bad(bboxes)
542 bboxes.each do |bbox|
543 amf_content "whichways", "/1", bbox
545 assert_response :success
548 # pass the response back to the caller's block to be tested
549 # against what the caller expected.
550 map = amf_result "/1"
555 # this should be what AMF controller returns when the bbox of a
556 # whichways request is invalid or too large.
557 def assert_boundary_error(map, msg=nil, error_hint=nil)
558 expected_map = [-2, "Sorry - I can't get the map for that area.#{msg}"]
559 assert_equal expected_map, map, "AMF controller should have returned an error. (#{error_hint})"
562 # this should be what AMF controller returns when the bbox of a
563 # whichways_deleted request is invalid or too large.
564 def assert_deleted_boundary_error(map, msg=nil, error_hint=nil)
565 expected_map = [-2, "Sorry - I can't get the map for that area.#{msg}"]
566 assert_equal expected_map, map, "AMF controller should have returned an error. (#{error_hint})"