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].nil? and way[4].nil?
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].nil? and way[4].nil?
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].nil? and rel[4].nil?
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].nil? and rel[4].nil?
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].nil?
244 assert returned_way[4].nil?
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].nil?
265 assert returned_way[4].nil?
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 # note that (as per getway_history) we actually round up
316 assert_equal history[0], 'node',
317 'first element should be "node"'
318 assert_equal history[1], latest.id,
319 'second element should be the input node ID'
320 assert_equal history[2].first[0],
321 latest.timestamp.succ.strftime("%d %b %Y, %H:%M:%S"),
322 'first element in third element (array) should be the latest version'
323 assert_equal history[2].last[0],
324 nodes(:node_with_versions_v1).timestamp.succ.strftime("%d %b %Y, %H:%M:%S"),
325 'last element in third element (array) 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 # try creating a POI with rubbish in the tags
458 def test_putpoi_create_with_control_chars
459 # This node has no tags
461 # create a node with random lat/lon
462 lat = rand(100)-50 + rand
463 lon = rand(100)-50 + rand
464 # normal user has a changeset open
465 changeset = changesets(:public_user_first_change)
467 mostly_invalid = (0..31).to_a.map {|i| i.chr}.join
468 tags = { "something" => "foo#{mostly_invalid}bar" }
470 amf_content "putpoi", "/1", ["test@example.com:test", changeset.id, nil, nil, lon, lat, tags, nil]
472 assert_response :success
474 result = amf_result("/1")
476 # check the array returned by the amf
477 assert_equal 5, result.size
478 assert_equal 0, result[0], "Expected to get the status ok in the amf"
479 assert_equal 0, result[2], "The old id should be 0"
480 assert result[3] > 0, "The new id should be greater than 0"
481 assert_equal 1, result[4], "The new version should be 1"
483 # Finally check that the node that was saved has saved the data correctly
484 # in both the current and history tables
485 # First check the current table
486 current_node = Node.find(result[3])
487 assert_equal 1, current_node.tags.size, "There seems to be a tag that has been added to the node"
488 assert_equal({ "something" => "foo\t\n\rbar" }, current_node.tags, "tags were not fixed correctly")
489 assert_equal result[4], current_node.version, "The version returned, is different to the one returned by the amf"
492 # try creating a POI with rubbish in the tags
493 def test_putpoi_create_with_invalid_utf8
494 # This node has no tags
496 # create a node with random lat/lon
497 lat = rand(100)-50 + rand
498 lon = rand(100)-50 + rand
499 # normal user has a changeset open
500 changeset = changesets(:public_user_first_change)
503 tags = { "something" => "foo#{invalid}bar" }
505 amf_content "putpoi", "/1", ["test@example.com:test", changeset.id, nil, nil, lon, lat, tags, nil]
507 assert_response :success
509 result = amf_result("/1")
511 assert_equal 2, result.size
512 assert_equal -1, result[0], "Expected to get the status FAIL in the amf"
513 assert_equal "One of the tags is invalid. Linux users may need to upgrade to Flash Player 10.1.", result[1]
516 def test_putpoi_delete_valid
520 def test_putpoi_delete_already_deleted
524 def test_putpoi_delete_not_found
528 def test_putpoi_invalid_latlon
532 # ************************************************************
533 # AMF Helper functions
535 # Get the result record for the specified ID
536 # It's an assertion FAIL if the record does not exist
538 assert @amf_result.has_key?("#{ref}/onResult")
539 @amf_result["#{ref}/onResult"]
542 # Encode the AMF message to invoke "target" with parameters as
543 # the passed data. The ref is used to retrieve the results.
544 def amf_content(target, ref, data)
547 c.write 0.chr+0.chr # version 0
548 c.write 0.chr+0.chr # n headers
549 c.write a.chr+b.chr # n bodies
550 c.write AMF.encodestring(target)
551 c.write AMF.encodestring(ref)
552 c.write [-1].pack("N")
553 c.write AMF.encodevalue(data)
555 @request.env["RAW_POST_DATA"] = c.string
558 # Parses the @response object as an AMF messsage.
559 # The result is a hash of message_ref => data.
560 # The attribute @amf_result is initialised to this hash.
561 def amf_parse_response
562 if @response.body.class.to_s == 'Proc'
564 @response.body.call @response, res
565 req = StringIO.new(res.string)
567 req = StringIO.new(@response.body)
569 req.read(2) # version
571 # parse through any headers
572 headers=AMF.getint(req) # Read number of headers
573 headers.times do # Read each header
574 name=AMF.getstring(req) # |
575 req.getc # | skip boolean
576 value=AMF.getvalue(req) # |
579 # parse through responses
581 bodies=AMF.getint(req) # Read number of bodies
582 bodies.times do # Read each body
583 message=AMF.getstring(req) # | get message name
584 index=AMF.getstring(req) # | get index in response sequence
585 bytes=AMF.getlong(req) # | get total size in bytes
586 args=AMF.getvalue(req) # | get response (probably an array)
587 results[message] = args
589 @amf_result = results
594 # given an array of bounding boxes (each an array of 4 floats), call the
595 # AMF "whichways" controller for each and pass the result back to the
596 # caller's block for assertion testing.
597 def check_bboxes_are_bad(bboxes)
598 bboxes.each do |bbox|
599 amf_content "whichways", "/1", bbox
601 assert_response :success
604 # pass the response back to the caller's block to be tested
605 # against what the caller expected.
606 map = amf_result "/1"
611 # this should be what AMF controller returns when the bbox of a
612 # whichways request is invalid or too large.
613 def assert_boundary_error(map, msg=nil, error_hint=nil)
614 expected_map = [-2, "Sorry - I can't get the map for that area.#{msg}"]
615 assert_equal expected_map, map, "AMF controller should have returned an error. (#{error_hint})"
618 # this should be what AMF controller returns when the bbox of a
619 # whichways_deleted request is invalid or too large.
620 def assert_deleted_boundary_error(map, msg=nil, error_hint=nil)
621 expected_map = [-2, "Sorry - I can't get the map for that area.#{msg}"]
622 assert_equal expected_map, map, "AMF controller should have returned an error. (#{error_hint})"