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 because we say "give me the revision of 15:33:02",
299 # but that might actually include changes at 15:33:02.457.
300 assert_equal (latest.timestamp + 1).strftime("%d %b %Y, %H:%M:%S"), history[2].first[0]
301 assert_equal (oldest.timestamp + 1).strftime("%d %b %Y, %H:%M:%S"), history[2].last[0]
304 def test_getway_history_nonexistent
305 amf_content "getway_history", "/1", [0]
307 assert_response :success
309 history = amf_result("/1")
311 # ['way',wayid,history]
312 assert_equal history[0], 'way'
313 assert_equal history[1], 0
314 assert history[2].empty?
317 def test_getnode_history
318 latest = current_nodes(:node_with_versions)
319 amf_content "getnode_history", "/1", [latest.id]
321 assert_response :success
323 history = amf_result("/1")
325 # ['node',nodeid,history]
326 # note that (as per getway_history) we actually round up
328 assert_equal history[0], 'node',
329 'first element should be "node"'
330 assert_equal history[1], latest.id,
331 'second element should be the input node ID'
332 assert_equal history[2].first[0],
333 (latest.timestamp + 1).strftime("%d %b %Y, %H:%M:%S"),
334 'first element in third element (array) should be the latest version'
335 assert_equal history[2].last[0],
336 (nodes(:node_with_versions_v1).timestamp + 1).strftime("%d %b %Y, %H:%M:%S"),
337 'last element in third element (array) should be the initial version'
340 def test_getnode_history_nonexistent
341 amf_content "getnode_history", "/1", [0]
343 assert_response :success
345 history = amf_result("/1")
347 # ['node',nodeid,history]
348 assert_equal history[0], 'node'
349 assert_equal history[1], 0
350 assert history[2].empty?
353 # ************************************************************
355 def test_putpoi_update_valid
356 nd = current_nodes(:visible_node)
357 cs_id = changesets(:public_user_first_change).id
358 amf_content "putpoi", "/1", ["test@example.com:test", cs_id, nd.version, nd.id, nd.lon, nd.lat, nd.tags, nd.visible]
360 assert_response :success
362 result = amf_result("/1")
364 assert_equal 0, result[0]
365 assert_equal "", result[1]
366 assert_equal nd.id, result[2]
367 assert_equal nd.id, result[3]
368 assert_equal nd.version+1, result[4]
370 # Now try to update again, with a different lat/lon, using the updated version number
373 amf_content "putpoi", "/2", ["test@example.com:test", cs_id, nd.version+1, nd.id, lon, lat, nd.tags, nd.visible]
375 assert_response :success
377 result = amf_result("/2")
379 assert_equal 0, result[0]
380 assert_equal "", result[1]
381 assert_equal nd.id, result[2]
382 assert_equal nd.id, result[3]
383 assert_equal nd.version+2, result[4]
386 # Check that we can create a no valid poi
387 # Using similar method for the node controller test
388 def test_putpoi_create_valid
389 # This node has no tags
391 # create a node with random lat/lon
392 lat = rand(100)-50 + rand
393 lon = rand(100)-50 + rand
394 # normal user has a changeset open
395 changeset = changesets(:public_user_first_change)
397 amf_content "putpoi", "/1", ["test@example.com:test", changeset.id, nil, nil, lon, lat, {}, nil]
399 assert_response :success
401 result = amf_result("/1")
403 # check the array returned by the amf
404 assert_equal 5, result.size
405 assert_equal 0, result[0], "expected to get the status ok from the amf"
406 assert_equal 0, result[2], "The old id should be 0"
407 assert result[3] > 0, "The new id should be greater than 0"
408 assert_equal 1, result[4], "The new version should be 1"
410 # Finally check that the node that was saved has saved the data correctly
411 # in both the current and history tables
412 # First check the current table
413 current_node = Node.find(result[3].to_i)
414 assert_in_delta lat, current_node.lat, 0.00001, "The latitude was not retreieved correctly"
415 assert_in_delta lon, current_node.lon, 0.00001, "The longitude was not retreived correctly"
416 assert_equal 0, current_node.tags.size, "There seems to be a tag that has been added to the node"
417 assert_equal result[4], current_node.version, "The version returned, is different to the one returned by the amf"
418 # Now check the history table
419 historic_nodes = Node.find(:all, :conditions => { :id => result[3] })
420 assert_equal 1, historic_nodes.size, "There should only be one historic node created"
421 first_historic_node = historic_nodes.first
422 assert_in_delta lat, first_historic_node.lat, 0.00001, "The latitude was not retreived correctly"
423 assert_in_delta lon, first_historic_node.lon, 0.00001, "The longitude was not retreuved correctly"
424 assert_equal 0, first_historic_node.tags.size, "There seems to be a tag that have been attached to this node"
425 assert_equal result[4], first_historic_node.version, "The version returned, is different to the one returned by the amf"
428 # This node has some tags
430 # create a node with random lat/lon
431 lat = rand(100)-50 + rand
432 lon = rand(100)-50 + rand
433 # normal user has a changeset open
434 changeset = changesets(:public_user_first_change)
436 amf_content "putpoi", "/2", ["test@example.com:test", changeset.id, nil, nil, lon, lat, { "key" => "value", "ping" => "pong" }, nil]
438 assert_response :success
440 result = amf_result("/2")
442 # check the array returned by the amf
443 assert_equal 5, result.size
444 assert_equal 0, result[0], "Expected to get the status ok in the amf"
445 assert_equal 0, result[2], "The old id should be 0"
446 assert result[3] > 0, "The new id should be greater than 0"
447 assert_equal 1, result[4], "The new version should be 1"
449 # Finally check that the node that was saved has saved the data correctly
450 # in both the current and history tables
451 # First check the current table
452 current_node = Node.find(result[3].to_i)
453 assert_in_delta lat, current_node.lat, 0.00001, "The latitude was not retreieved correctly"
454 assert_in_delta lon, current_node.lon, 0.00001, "The longitude was not retreived correctly"
455 assert_equal 2, current_node.tags.size, "There seems to be a tag that has been added to the node"
456 assert_equal({ "key" => "value", "ping" => "pong" }, current_node.tags, "tags are different")
457 assert_equal result[4], current_node.version, "The version returned, is different to the one returned by the amf"
458 # Now check the history table
459 historic_nodes = Node.find(:all, :conditions => { :id => result[3] })
460 assert_equal 1, historic_nodes.size, "There should only be one historic node created"
461 first_historic_node = historic_nodes.first
462 assert_in_delta lat, first_historic_node.lat, 0.00001, "The latitude was not retreived correctly"
463 assert_in_delta lon, first_historic_node.lon, 0.00001, "The longitude was not retreuved correctly"
464 assert_equal 2, first_historic_node.tags.size, "There seems to be a tag that have been attached to this node"
465 assert_equal({ "key" => "value", "ping" => "pong" }, first_historic_node.tags, "tags are different")
466 assert_equal result[4], first_historic_node.version, "The version returned, is different to the one returned by the amf"
469 # try creating a POI with rubbish in the tags
470 def test_putpoi_create_with_control_chars
471 # This node has no tags
473 # create a node with random lat/lon
474 lat = rand(100)-50 + rand
475 lon = rand(100)-50 + rand
476 # normal user has a changeset open
477 changeset = changesets(:public_user_first_change)
479 mostly_invalid = (0..31).to_a.map {|i| i.chr}.join
480 tags = { "something" => "foo#{mostly_invalid}bar" }
482 amf_content "putpoi", "/1", ["test@example.com:test", changeset.id, nil, nil, lon, lat, tags, nil]
484 assert_response :success
486 result = amf_result("/1")
488 # check the array returned by the amf
489 assert_equal 5, result.size
490 assert_equal 0, result[0], "Expected to get the status ok in the amf"
491 assert_equal 0, result[2], "The old id should be 0"
492 assert result[3] > 0, "The new id should be greater than 0"
493 assert_equal 1, result[4], "The new version should be 1"
495 # Finally check that the node that was saved has saved the data correctly
496 # in both the current and history tables
497 # First check the current table
498 current_node = Node.find(result[3].to_i)
499 assert_equal 1, current_node.tags.size, "There seems to be a tag that has been added to the node"
500 assert_equal({ "something" => "foo\t\n\rbar" }, current_node.tags, "tags were not fixed correctly")
501 assert_equal result[4], current_node.version, "The version returned, is different to the one returned by the amf"
504 # try creating a POI with rubbish in the tags
505 def test_putpoi_create_with_invalid_utf8
506 # This node has no tags
508 # create a node with random lat/lon
509 lat = rand(100)-50 + rand
510 lon = rand(100)-50 + rand
511 # normal user has a changeset open
512 changeset = changesets(:public_user_first_change)
515 tags = { "something" => "foo#{invalid}bar" }
517 amf_content "putpoi", "/1", ["test@example.com:test", changeset.id, nil, nil, lon, lat, tags, nil]
519 assert_response :success
521 result = amf_result("/1")
523 assert_equal 2, result.size
524 assert_equal -1, result[0], "Expected to get the status FAIL in the amf"
525 assert_equal "One of the tags is invalid. Linux users may need to upgrade to Flash Player 10.1.", result[1]
528 def test_putpoi_delete_valid
532 def test_putpoi_delete_already_deleted
536 def test_putpoi_delete_not_found
540 def test_putpoi_invalid_latlon
544 def test_startchangeset_invalid_xmlchar_comment
546 comment = "foo#{invalid}bar"
548 amf_content "startchangeset", "/1", ["test@example.com:test", Hash.new, nil, comment, 1]
550 assert_response :success
552 result = amf_result("/1")
554 assert_equal 3, result.size, result.inspect
555 assert_equal 0, result[0]
556 new_cs_id = result[2].to_i
558 cs = Changeset.find(new_cs_id)
559 assert_equal "foobar", cs.tags["comment"]
562 # ************************************************************
563 # AMF Helper functions
565 # Get the result record for the specified ID
566 # It's an assertion FAIL if the record does not exist
568 assert @amf_result.has_key?("#{ref}/onResult")
569 @amf_result["#{ref}/onResult"]
572 # Encode the AMF message to invoke "target" with parameters as
573 # the passed data. The ref is used to retrieve the results.
574 def amf_content(target, ref, data)
577 c.write 0.chr+0.chr # version 0
578 c.write 0.chr+0.chr # n headers
579 c.write a.chr+b.chr # n bodies
580 c.write AMF.encodestring(target)
581 c.write AMF.encodestring(ref)
582 c.write [-1].pack("N")
583 c.write AMF.encodevalue(data)
585 @request.env["RAW_POST_DATA"] = c.string
588 # Parses the @response object as an AMF messsage.
589 # The result is a hash of message_ref => data.
590 # The attribute @amf_result is initialised to this hash.
591 def amf_parse_response
592 req = StringIO.new(@response.body)
594 req.read(2) # version
596 # parse through any headers
597 headers=AMF.getint(req) # Read number of headers
598 headers.times do # Read each header
599 name=AMF.getstring(req) # |
600 req.getc # | skip boolean
601 value=AMF.getvalue(req) # |
604 # parse through responses
606 bodies=AMF.getint(req) # Read number of bodies
607 bodies.times do # Read each body
608 message=AMF.getstring(req) # | get message name
609 index=AMF.getstring(req) # | get index in response sequence
610 bytes=AMF.getlong(req) # | get total size in bytes
611 args=AMF.getvalue(req) # | get response (probably an array)
612 results[message] = args
614 @amf_result = results
619 # given an array of bounding boxes (each an array of 4 floats), call the
620 # AMF "whichways" controller for each and pass the result back to the
621 # caller's block for assertion testing.
622 def check_bboxes_are_bad(bboxes)
623 bboxes.each do |bbox|
624 amf_content "whichways", "/1", bbox
626 assert_response :success
629 # pass the response back to the caller's block to be tested
630 # against what the caller expected.
631 map = amf_result "/1"
636 # this should be what AMF controller returns when the bbox of a
637 # whichways request is invalid or too large.
638 def assert_boundary_error(map, msg=nil, error_hint=nil)
639 expected_map = [-2, "Sorry - I can't get the map for that area.#{msg}"]
640 assert_equal expected_map, map, "AMF controller should have returned an error. (#{error_hint})"
643 # this should be what AMF controller returns when the bbox of a
644 # whichways_deleted request is invalid or too large.
645 def assert_deleted_boundary_error(map, msg=nil, error_hint=nil)
646 expected_map = [-2, "Sorry - I can't get the map for that area.#{msg}"]
647 assert_equal expected_map, map, "AMF controller should have returned an error. (#{error_hint})"