1 require File.dirname(__FILE__) + '/../test_helper'
5 class AmfControllerTest < ActionController::TestCase
9 # test all routes which lead to this controller
12 { :path => "/api/0.6/amf/read", :method => :post },
13 { :controller => "amf", :action => "amf_read" }
16 { :path => "/api/0.6/amf/write", :method => :post },
17 { :controller => "amf", :action => "amf_write" }
23 id = current_ways(:visible_way).id
24 amf_content "getway", "/1", [id]
26 assert_response :success
28 way = amf_result("/1")
29 assert_equal way[0], 0
30 assert_equal way[2], id
33 def test_getway_invisible
34 # check an invisible way
35 id = current_ways(:invisible_way).id
36 amf_content "getway", "/1", [id]
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], id
44 assert way[3].nil? and way[4].nil?
47 def test_getway_nonexistent
48 # check chat a non-existent way is not returned
49 amf_content "getway", "/1", [0]
51 assert_response :success
53 way = amf_result("/1")
54 assert_equal way[0], -4
55 assert_equal way[1], "way"
56 assert_equal way[2], 0
57 assert way[3].nil? and way[4].nil?
61 node = current_nodes(:used_node_1)
66 amf_content "whichways", "/1", [minlon, minlat, maxlon, maxlat]
68 assert_response :success
71 # check contents of message
73 assert_equal 0, map[0], 'map error code should be 0'
74 assert_equal "", map[1], 'map error text should be empty'
76 # check the formatting of the message
77 assert_equal 5, map.length, 'map should have length 5'
78 assert_equal Array, map[2].class, 'map "ways" element should be an array'
79 assert_equal Array, map[3].class, 'map "nodes" element should be an array'
80 assert_equal Array, map[4].class, 'map "relations" element should be an array'
82 assert_equal 2, w.length, 'way should be (id, version) pair'
83 assert w[0] == w[0].floor, 'way ID should be an integer'
84 assert w[1] == w[1].floor, 'way version should be an integer'
88 assert_equal 5, w.length, 'node should be (id, lat, lon, [tags], version) tuple'
89 assert n[0] == n[0].floor, 'node ID should be an integer'
90 assert n[1] >= minlat - 0.01, 'node lat should be greater than min'
91 assert n[1] <= maxlat - 0.01, 'node lat should be less than max'
92 assert n[2] >= minlon - 0.01, 'node lon should be greater than min'
93 assert n[2] <= maxlon - 0.01, 'node lon should be less than max'
94 assert_equal Array, a[3].class, 'node tags should be array'
95 assert n[4] == n[4].floor, 'node version should be an integer'
99 assert_equal 2, r.length, 'relation should be (id, version) pair'
100 assert r[0] == r[0].floor, 'relation ID should be an integer'
101 assert r[1] == r[1].floor, 'relation version should be an integer'
104 # TODO: looks like amf_controller changed since this test was written
105 # so someone who knows what they're doing should check this!
106 ways = map[2].collect { |x| x[0] }
107 assert ways.include?(current_ways(:used_way).id),
108 "map should include used way"
109 assert !ways.include?(current_ways(:invisible_way).id),
110 'map should not include deleted way'
114 # checks that too-large a bounding box will not be served.
115 def test_whichways_toobig
116 bbox = [-0.1,-0.1,1.1,1.1]
117 check_bboxes_are_bad [bbox] do |map,bbox|
118 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"
123 # checks that an invalid bounding box will not be served. in this case
124 # one with max < min latitudes.
126 # NOTE: the controller expands the bbox by 0.01 in each direction!
127 def test_whichways_badlat
128 bboxes = [[0,0.1,0.1,0], [-0.1,80,0.1,70], [0.24,54.35,0.25,54.33]]
129 check_bboxes_are_bad bboxes do |map, bbox|
130 assert_boundary_error map, " The server said: The minimum latitude must be less than the maximum latitude, but it wasn't", bbox.inspect
135 # same as test_whichways_badlat, but for longitudes
137 # NOTE: the controller expands the bbox by 0.01 in each direction!
138 def test_whichways_badlon
139 bboxes = [[80,-0.1,70,0.1], [54.35,0.24,54.33,0.25]]
140 check_bboxes_are_bad bboxes do |map, bbox|
141 assert_boundary_error map, " The server said: The minimum longitude must be less than the maximum longitude, but it wasn't", bbox.inspect
145 def test_whichways_deleted
146 node = current_nodes(:used_node_1)
147 minlon = node.lon-0.1
148 minlat = node.lat-0.1
149 maxlon = node.lon+0.1
150 maxlat = node.lat+0.1
151 amf_content "whichways_deleted", "/1", [minlon, minlat, maxlon, maxlat]
153 assert_response :success
156 # check contents of message
157 map = amf_result "/1"
158 assert_equal 0, map[0], 'first map element should be 0'
159 assert_equal "", map[1], 'second map element should be an empty string'
160 assert_equal Array, map[2].class, 'third map element should be an array'
161 # TODO: looks like amf_controller changed since this test was written
162 # so someone who knows what they're doing should check this!
163 assert !map[2].include?(current_ways(:used_way).id),
164 "map should not include used way"
165 assert map[2].include?(current_ways(:invisible_way).id),
166 'map should include deleted way'
169 def test_whichways_deleted_toobig
170 bbox = [-0.1,-0.1,1.1,1.1]
171 amf_content "whichways_deleted", "/1", bbox
173 assert_response :success
176 map = amf_result "/1"
177 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"
181 id = current_relations(:visible_relation).id
182 amf_content "getrelation", "/1", [id]
184 assert_response :success
186 rel = amf_result("/1")
187 assert_equal rel[0], 0
188 assert_equal rel[2], id
191 def test_getrelation_invisible
192 id = current_relations(:invisible_relation).id
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?
204 def test_getrelation_nonexistent
206 amf_content "getrelation", "/1", [id]
208 assert_response :success
210 rel = amf_result("/1")
211 assert_equal rel[0], -4
212 assert_equal rel[1], "relation"
213 assert_equal rel[2], id
214 assert rel[3].nil? and rel[4].nil?
218 # try to get the last visible version (specified by <0) (should be current version)
219 latest = current_ways(:way_with_versions)
220 # NOTE: looks from the API changes that this now expects a timestamp
221 # instead of a version number...
222 # try to get version 1
223 v1 = ways(:way_with_versions_v1)
225 v1.way_id => v1.timestamp.strftime("%d %b %Y, %H:%M:%S")
227 amf_content "getway_old", "/1", [id, t]
229 assert_response :success
231 returned_way = amf_result("/1")
232 assert_equal 0, returned_way[0]
233 assert_equal id, returned_way[2]
234 # API returns the *latest* version, even for old ways...
235 assert_equal latest.version, returned_way[5]
240 # test that the server doesn't fall over when rubbish is passed
241 # into the method args.
242 def test_getway_old_invalid
243 way_id = current_ways(:way_with_versions).id
245 way_id => "not a date",
246 way_id => "2009-03-25 00:00:00", # <- wrong format
247 way_id => "0 Jan 2009 00:00:00", # <- invalid date
248 -1 => "1 Jan 2009 00:00:00" # <- invalid ID
250 amf_content "getway_old", "/1", [id, t]
252 assert_response :success
254 returned_way = amf_result("/1")
255 assert_equal -1, returned_way[0]
256 assert returned_way[3].nil?
257 assert returned_way[4].nil?
258 assert returned_way[5].nil?
262 def test_getway_old_nonexistent
263 # try to get the last version+10 (shoudn't exist)
264 v1 = ways(:way_with_versions_v1)
265 # try to get last visible version of non-existent way
266 # try to get specific version of non-existent way
268 [0, '1 Jan 1970, 00:00:00'],
269 [v1.way_id, (v1.timestamp - 10).strftime("%d %b %Y, %H:%M:%S")]
271 amf_content "getway_old", "/1", [id, t]
273 assert_response :success
275 returned_way = amf_result("/1")
276 assert_equal -1, returned_way[0]
277 assert returned_way[3].nil?
278 assert returned_way[4].nil?
279 assert returned_way[5].nil?
283 def test_getway_history
284 latest = current_ways(:way_with_versions)
285 oldest = ways(:way_with_versions_v1)
287 amf_content "getway_history", "/1", [latest.id]
289 assert_response :success
291 history = amf_result("/1")
293 # ['way',wayid,history]
294 assert_equal 'way', history[0]
295 assert_equal latest.id, history[1]
296 # We use dates rather than version numbers here, because you might
297 # have moved a node within a way (i.e. way version not incremented).
298 # The timestamp is +1 (timestamp.succ) because we say "give me the
299 # revision of 15:33:02", but that might actually include changes at
301 assert_equal latest.timestamp.succ.strftime("%d %b %Y, %H:%M:%S"), history[2].first[0]
302 assert_equal oldest.timestamp.succ.strftime("%d %b %Y, %H:%M:%S"), history[2].last[0]
305 def test_getway_history_nonexistent
306 amf_content "getway_history", "/1", [0]
308 assert_response :success
310 history = amf_result("/1")
312 # ['way',wayid,history]
313 assert_equal history[0], 'way'
314 assert_equal history[1], 0
315 assert history[2].empty?
318 def test_getnode_history
319 latest = current_nodes(:node_with_versions)
320 amf_content "getnode_history", "/1", [latest.id]
322 assert_response :success
324 history = amf_result("/1")
326 # ['node',nodeid,history]
327 # note that (as per getway_history) we actually round up
329 assert_equal history[0], 'node',
330 'first element should be "node"'
331 assert_equal history[1], latest.id,
332 'second element should be the input node ID'
333 assert_equal history[2].first[0],
334 latest.timestamp.succ.strftime("%d %b %Y, %H:%M:%S"),
335 'first element in third element (array) should be the latest version'
336 assert_equal history[2].last[0],
337 nodes(:node_with_versions_v1).timestamp.succ.strftime("%d %b %Y, %H:%M:%S"),
338 'last element in third element (array) should be the initial version'
341 def test_getnode_history_nonexistent
342 amf_content "getnode_history", "/1", [0]
344 assert_response :success
346 history = amf_result("/1")
348 # ['node',nodeid,history]
349 assert_equal history[0], 'node'
350 assert_equal history[1], 0
351 assert history[2].empty?
354 # ************************************************************
356 def test_putpoi_update_valid
357 nd = current_nodes(:visible_node)
358 cs_id = changesets(:public_user_first_change).id
359 amf_content "putpoi", "/1", ["test@example.com:test", cs_id, nd.version, nd.id, nd.lon, nd.lat, nd.tags, nd.visible]
361 assert_response :success
363 result = amf_result("/1")
365 assert_equal 0, result[0]
366 assert_equal "", result[1]
367 assert_equal nd.id, result[2]
368 assert_equal nd.id, result[3]
369 assert_equal nd.version+1, result[4]
371 # Now try to update again, with a different lat/lon, using the updated version number
374 amf_content "putpoi", "/2", ["test@example.com:test", cs_id, nd.version+1, nd.id, lon, lat, nd.tags, nd.visible]
376 assert_response :success
378 result = amf_result("/2")
380 assert_equal 0, result[0]
381 assert_equal "", result[1]
382 assert_equal nd.id, result[2]
383 assert_equal nd.id, result[3]
384 assert_equal nd.version+2, result[4]
387 # Check that we can create a no valid poi
388 # Using similar method for the node controller test
389 def test_putpoi_create_valid
390 # This node has no tags
392 # create a node with random lat/lon
393 lat = rand(100)-50 + rand
394 lon = rand(100)-50 + rand
395 # normal user has a changeset open
396 changeset = changesets(:public_user_first_change)
398 amf_content "putpoi", "/1", ["test@example.com:test", changeset.id, nil, nil, lon, lat, {}, nil]
400 assert_response :success
402 result = amf_result("/1")
404 # check the array returned by the amf
405 assert_equal 5, result.size
406 assert_equal 0, result[0], "expected to get the status ok from the amf"
407 assert_equal 0, result[2], "The old id should be 0"
408 assert result[3] > 0, "The new id should be greater than 0"
409 assert_equal 1, result[4], "The new version should be 1"
411 # Finally check that the node that was saved has saved the data correctly
412 # in both the current and history tables
413 # First check the current table
414 current_node = Node.find(result[3].to_i)
415 assert_in_delta lat, current_node.lat, 0.00001, "The latitude was not retreieved correctly"
416 assert_in_delta lon, current_node.lon, 0.00001, "The longitude was not retreived correctly"
417 assert_equal 0, current_node.tags.size, "There seems to be a tag that has been added to the node"
418 assert_equal result[4], current_node.version, "The version returned, is different to the one returned by the amf"
419 # Now check the history table
420 historic_nodes = Node.find(:all, :conditions => { :id => result[3] })
421 assert_equal 1, historic_nodes.size, "There should only be one historic node created"
422 first_historic_node = historic_nodes.first
423 assert_in_delta lat, first_historic_node.lat, 0.00001, "The latitude was not retreived correctly"
424 assert_in_delta lon, first_historic_node.lon, 0.00001, "The longitude was not retreuved correctly"
425 assert_equal 0, first_historic_node.tags.size, "There seems to be a tag that have been attached to this node"
426 assert_equal result[4], first_historic_node.version, "The version returned, is different to the one returned by the amf"
429 # This node has some tags
431 # create a node with random lat/lon
432 lat = rand(100)-50 + rand
433 lon = rand(100)-50 + rand
434 # normal user has a changeset open
435 changeset = changesets(:public_user_first_change)
437 amf_content "putpoi", "/2", ["test@example.com:test", changeset.id, nil, nil, lon, lat, { "key" => "value", "ping" => "pong" }, nil]
439 assert_response :success
441 result = amf_result("/2")
443 # check the array returned by the amf
444 assert_equal 5, result.size
445 assert_equal 0, result[0], "Expected to get the status ok in the amf"
446 assert_equal 0, result[2], "The old id should be 0"
447 assert result[3] > 0, "The new id should be greater than 0"
448 assert_equal 1, result[4], "The new version should be 1"
450 # Finally check that the node that was saved has saved the data correctly
451 # in both the current and history tables
452 # First check the current table
453 current_node = Node.find(result[3].to_i)
454 assert_in_delta lat, current_node.lat, 0.00001, "The latitude was not retreieved correctly"
455 assert_in_delta lon, current_node.lon, 0.00001, "The longitude was not retreived correctly"
456 assert_equal 2, current_node.tags.size, "There seems to be a tag that has been added to the node"
457 assert_equal({ "key" => "value", "ping" => "pong" }, current_node.tags, "tags are different")
458 assert_equal result[4], current_node.version, "The version returned, is different to the one returned by the amf"
459 # Now check the history table
460 historic_nodes = Node.find(:all, :conditions => { :id => result[3] })
461 assert_equal 1, historic_nodes.size, "There should only be one historic node created"
462 first_historic_node = historic_nodes.first
463 assert_in_delta lat, first_historic_node.lat, 0.00001, "The latitude was not retreived correctly"
464 assert_in_delta lon, first_historic_node.lon, 0.00001, "The longitude was not retreuved correctly"
465 assert_equal 2, first_historic_node.tags.size, "There seems to be a tag that have been attached to this node"
466 assert_equal({ "key" => "value", "ping" => "pong" }, first_historic_node.tags, "tags are different")
467 assert_equal result[4], first_historic_node.version, "The version returned, is different to the one returned by the amf"
470 # try creating a POI with rubbish in the tags
471 def test_putpoi_create_with_control_chars
472 # This node has no tags
474 # create a node with random lat/lon
475 lat = rand(100)-50 + rand
476 lon = rand(100)-50 + rand
477 # normal user has a changeset open
478 changeset = changesets(:public_user_first_change)
480 mostly_invalid = (0..31).to_a.map {|i| i.chr}.join
481 tags = { "something" => "foo#{mostly_invalid}bar" }
483 amf_content "putpoi", "/1", ["test@example.com:test", changeset.id, nil, nil, lon, lat, tags, nil]
485 assert_response :success
487 result = amf_result("/1")
489 # check the array returned by the amf
490 assert_equal 5, result.size
491 assert_equal 0, result[0], "Expected to get the status ok in the amf"
492 assert_equal 0, result[2], "The old id should be 0"
493 assert result[3] > 0, "The new id should be greater than 0"
494 assert_equal 1, result[4], "The new version should be 1"
496 # Finally check that the node that was saved has saved the data correctly
497 # in both the current and history tables
498 # First check the current table
499 current_node = Node.find(result[3].to_i)
500 assert_equal 1, current_node.tags.size, "There seems to be a tag that has been added to the node"
501 assert_equal({ "something" => "foo\t\n\rbar" }, current_node.tags, "tags were not fixed correctly")
502 assert_equal result[4], current_node.version, "The version returned, is different to the one returned by the amf"
505 # try creating a POI with rubbish in the tags
506 def test_putpoi_create_with_invalid_utf8
507 # This node has no tags
509 # create a node with random lat/lon
510 lat = rand(100)-50 + rand
511 lon = rand(100)-50 + rand
512 # normal user has a changeset open
513 changeset = changesets(:public_user_first_change)
516 tags = { "something" => "foo#{invalid}bar" }
518 amf_content "putpoi", "/1", ["test@example.com:test", changeset.id, nil, nil, lon, lat, tags, nil]
520 assert_response :success
522 result = amf_result("/1")
524 assert_equal 2, result.size
525 assert_equal -1, result[0], "Expected to get the status FAIL in the amf"
526 assert_equal "One of the tags is invalid. Linux users may need to upgrade to Flash Player 10.1.", result[1]
529 def test_putpoi_delete_valid
533 def test_putpoi_delete_already_deleted
537 def test_putpoi_delete_not_found
541 def test_putpoi_invalid_latlon
545 def test_startchangeset_invalid_xmlchar_comment
547 comment = "foo#{invalid}bar"
549 amf_content "startchangeset", "/1", ["test@example.com:test", Hash.new, nil, comment, 1]
551 assert_response :success
553 result = amf_result("/1")
555 assert_equal 3, result.size, result.inspect
556 assert_equal 0, result[0]
557 new_cs_id = result[2].to_i
559 cs = Changeset.find(new_cs_id)
560 assert_equal "foobar", cs.tags["comment"]
563 # ************************************************************
564 # AMF Helper functions
566 # Get the result record for the specified ID
567 # It's an assertion FAIL if the record does not exist
569 assert @amf_result.has_key?("#{ref}/onResult")
570 @amf_result["#{ref}/onResult"]
573 # Encode the AMF message to invoke "target" with parameters as
574 # the passed data. The ref is used to retrieve the results.
575 def amf_content(target, ref, data)
578 c.write 0.chr+0.chr # version 0
579 c.write 0.chr+0.chr # n headers
580 c.write a.chr+b.chr # n bodies
581 c.write AMF.encodestring(target)
582 c.write AMF.encodestring(ref)
583 c.write [-1].pack("N")
584 c.write AMF.encodevalue(data)
586 @request.env["RAW_POST_DATA"] = c.string
589 # Parses the @response object as an AMF messsage.
590 # The result is a hash of message_ref => data.
591 # The attribute @amf_result is initialised to this hash.
592 def amf_parse_response
593 req = StringIO.new(@response.body)
595 req.read(2) # version
597 # parse through any headers
598 headers=AMF.getint(req) # Read number of headers
599 headers.times do # Read each header
600 name=AMF.getstring(req) # |
601 req.getc # | skip boolean
602 value=AMF.getvalue(req) # |
605 # parse through responses
607 bodies=AMF.getint(req) # Read number of bodies
608 bodies.times do # Read each body
609 message=AMF.getstring(req) # | get message name
610 index=AMF.getstring(req) # | get index in response sequence
611 bytes=AMF.getlong(req) # | get total size in bytes
612 args=AMF.getvalue(req) # | get response (probably an array)
613 results[message] = args
615 @amf_result = results
620 # given an array of bounding boxes (each an array of 4 floats), call the
621 # AMF "whichways" controller for each and pass the result back to the
622 # caller's block for assertion testing.
623 def check_bboxes_are_bad(bboxes)
624 bboxes.each do |bbox|
625 amf_content "whichways", "/1", bbox
627 assert_response :success
630 # pass the response back to the caller's block to be tested
631 # against what the caller expected.
632 map = amf_result "/1"
637 # this should be what AMF controller returns when the bbox of a
638 # whichways request is invalid or too large.
639 def assert_boundary_error(map, msg=nil, error_hint=nil)
640 expected_map = [-2, "Sorry - I can't get the map for that area.#{msg}"]
641 assert_equal expected_map, map, "AMF controller should have returned an error. (#{error_hint})"
644 # this should be what AMF controller returns when the bbox of a
645 # whichways_deleted request is invalid or too large.
646 def assert_deleted_boundary_error(map, msg=nil, error_hint=nil)
647 expected_map = [-2, "Sorry - I can't get the map for that area.#{msg}"]
648 assert_equal expected_map, map, "AMF controller should have returned an error. (#{error_hint})"