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 0, way[0]
30 assert_equal "", way[1]
31 assert_equal id, way[2]
32 assert_equal 1, way[3].length
33 assert_equal 3, way[3][0][2]
34 assert_equal 1, way[5]
35 assert_equal 2, way[6]
38 def test_getway_invisible
39 # check an invisible way
40 id = current_ways(:invisible_way).id
41 amf_content "getway", "/1", [id]
43 assert_response :success
45 way = amf_result("/1")
46 assert_equal -4, way[0], -4
47 assert_equal "way", way[1]
48 assert_equal id, way[2]
49 assert way[3].nil? and way[4].nil? and way[5].nil? and way[6].nil?
52 def test_getway_with_versions
53 # check a way with multiple versions
54 id = current_ways(:way_with_versions).id
55 amf_content "getway", "/1", [id]
57 assert_response :success
59 way = amf_result("/1")
60 assert_equal 0, way[0]
61 assert_equal "", way[1]
62 assert_equal id, way[2]
63 assert_equal 1, way[3].length
64 assert_equal 15, way[3][0][2]
65 assert_equal 4, way[5]
66 assert_equal 2, way[6]
69 def test_getway_with_duplicate_nodes
70 # check a way with duplicate nodes
71 id = current_ways(:way_with_duplicate_nodes).id
72 amf_content "getway", "/1", [id]
74 assert_response :success
76 way = amf_result("/1")
77 assert_equal 0, way[0]
78 assert_equal "", way[1]
79 assert_equal id, way[2]
80 assert_equal 2, way[3].length
81 assert_equal 4, way[3][0][2]
82 assert_equal 4, way[3][1][2]
83 assert_equal 1, way[5]
84 assert_equal 2, way[6]
87 def test_getway_with_multiple_nodes
88 # check a way with multiple nodes
89 id = current_ways(:way_with_multiple_nodes).id
90 amf_content "getway", "/1", [id]
92 assert_response :success
94 way = amf_result("/1")
95 assert_equal 0, way[0]
96 assert_equal "", way[1]
97 assert_equal id, way[2]
98 assert_equal 3, way[3].length
99 assert_equal 4, way[3][0][2]
100 assert_equal 15, way[3][1][2]
101 assert_equal 6, way[3][2][2]
102 assert_equal 2, way[5]
103 assert_equal 2, way[6]
106 def test_getway_nonexistent
107 # check chat a non-existent way is not returned
108 amf_content "getway", "/1", [0]
110 assert_response :success
112 way = amf_result("/1")
113 assert_equal -4, way[0]
114 assert_equal "way", way[1]
115 assert_equal 0, way[2]
116 assert way[3].nil? and way[4].nil? and way[5].nil? and way[6].nil?
120 node = current_nodes(:used_node_1)
121 minlon = node.lon-0.1
122 minlat = node.lat-0.1
123 maxlon = node.lon+0.1
124 maxlat = node.lat+0.1
125 amf_content "whichways", "/1", [minlon, minlat, maxlon, maxlat]
127 assert_response :success
130 # check contents of message
131 map = amf_result "/1"
132 assert_equal 0, map[0], 'map error code should be 0'
133 assert_equal "", map[1], 'map error text should be empty'
135 # check the formatting of the message
136 assert_equal 5, map.length, 'map should have length 5'
137 assert_equal Array, map[2].class, 'map "ways" element should be an array'
138 assert_equal Array, map[3].class, 'map "nodes" element should be an array'
139 assert_equal Array, map[4].class, 'map "relations" element should be an array'
141 assert_equal 2, w.length, 'way should be (id, version) pair'
142 assert w[0] == w[0].floor, 'way ID should be an integer'
143 assert w[1] == w[1].floor, 'way version should be an integer'
147 assert_equal 5, w.length, 'node should be (id, lat, lon, [tags], version) tuple'
148 assert n[0] == n[0].floor, 'node ID should be an integer'
149 assert n[1] >= minlat - 0.01, 'node lat should be greater than min'
150 assert n[1] <= maxlat - 0.01, 'node lat should be less than max'
151 assert n[2] >= minlon - 0.01, 'node lon should be greater than min'
152 assert n[2] <= maxlon - 0.01, 'node lon should be less than max'
153 assert_equal Array, a[3].class, 'node tags should be array'
154 assert n[4] == n[4].floor, 'node version should be an integer'
158 assert_equal 2, r.length, 'relation should be (id, version) pair'
159 assert r[0] == r[0].floor, 'relation ID should be an integer'
160 assert r[1] == r[1].floor, 'relation version should be an integer'
163 # TODO: looks like amf_controller changed since this test was written
164 # so someone who knows what they're doing should check this!
165 ways = map[2].collect { |x| x[0] }
166 assert ways.include?(current_ways(:used_way).id),
167 "map should include used way"
168 assert !ways.include?(current_ways(:invisible_way).id),
169 'map should not include deleted way'
173 # checks that too-large a bounding box will not be served.
174 def test_whichways_toobig
175 bbox = [-0.1,-0.1,1.1,1.1]
176 check_bboxes_are_bad [bbox] do |map,bbox|
177 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"
182 # checks that an invalid bounding box will not be served. in this case
183 # one with max < min latitudes.
185 # NOTE: the controller expands the bbox by 0.01 in each direction!
186 def test_whichways_badlat
187 bboxes = [[0,0.1,0.1,0], [-0.1,80,0.1,70], [0.24,54.35,0.25,54.33]]
188 check_bboxes_are_bad bboxes do |map, bbox|
189 assert_boundary_error map, " The server said: The minimum latitude must be less than the maximum latitude, but it wasn't", bbox.inspect
194 # same as test_whichways_badlat, but for longitudes
196 # NOTE: the controller expands the bbox by 0.01 in each direction!
197 def test_whichways_badlon
198 bboxes = [[80,-0.1,70,0.1], [54.35,0.24,54.33,0.25]]
199 check_bboxes_are_bad bboxes do |map, bbox|
200 assert_boundary_error map, " The server said: The minimum longitude must be less than the maximum longitude, but it wasn't", bbox.inspect
204 def test_whichways_deleted
205 node = current_nodes(:used_node_1)
206 minlon = node.lon-0.1
207 minlat = node.lat-0.1
208 maxlon = node.lon+0.1
209 maxlat = node.lat+0.1
210 amf_content "whichways_deleted", "/1", [minlon, minlat, maxlon, maxlat]
212 assert_response :success
215 # check contents of message
216 map = amf_result "/1"
217 assert_equal 0, map[0], 'first map element should be 0'
218 assert_equal "", map[1], 'second map element should be an empty string'
219 assert_equal Array, map[2].class, 'third map element should be an array'
220 # TODO: looks like amf_controller changed since this test was written
221 # so someone who knows what they're doing should check this!
222 assert !map[2].include?(current_ways(:used_way).id),
223 "map should not include used way"
224 assert map[2].include?(current_ways(:invisible_way).id),
225 'map should include deleted way'
228 def test_whichways_deleted_toobig
229 bbox = [-0.1,-0.1,1.1,1.1]
230 amf_content "whichways_deleted", "/1", bbox
232 assert_response :success
235 map = amf_result "/1"
236 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"
240 id = current_relations(:visible_relation).id
241 amf_content "getrelation", "/1", [id]
243 assert_response :success
245 rel = amf_result("/1")
246 assert_equal rel[0], 0
247 assert_equal rel[2], id
250 def test_getrelation_invisible
251 id = current_relations(:invisible_relation).id
252 amf_content "getrelation", "/1", [id]
254 assert_response :success
256 rel = amf_result("/1")
257 assert_equal rel[0], -4
258 assert_equal rel[1], "relation"
259 assert_equal rel[2], id
260 assert rel[3].nil? and rel[4].nil?
263 def test_getrelation_nonexistent
265 amf_content "getrelation", "/1", [id]
267 assert_response :success
269 rel = amf_result("/1")
270 assert_equal rel[0], -4
271 assert_equal rel[1], "relation"
272 assert_equal rel[2], id
273 assert rel[3].nil? and rel[4].nil?
277 # try to get the last visible version (specified by <0) (should be current version)
278 latest = current_ways(:way_with_versions)
279 # NOTE: looks from the API changes that this now expects a timestamp
280 # instead of a version number...
281 # try to get version 1
282 v1 = ways(:way_with_versions_v1)
284 v1.way_id => v1.timestamp.strftime("%d %b %Y, %H:%M:%S")
286 amf_content "getway_old", "/1", [id, t]
288 assert_response :success
290 returned_way = amf_result("/1")
291 assert_equal 0, returned_way[0]
292 assert_equal id, returned_way[2]
293 # API returns the *latest* version, even for old ways...
294 assert_equal latest.version, returned_way[5]
299 # test that the server doesn't fall over when rubbish is passed
300 # into the method args.
301 def test_getway_old_invalid
302 way_id = current_ways(:way_with_versions).id
304 way_id => "not a date",
305 way_id => "2009-03-25 00:00:00", # <- wrong format
306 way_id => "0 Jan 2009 00:00:00", # <- invalid date
307 -1 => "1 Jan 2009 00:00:00" # <- invalid ID
309 amf_content "getway_old", "/1", [id, t]
311 assert_response :success
313 returned_way = amf_result("/1")
314 assert_equal -1, returned_way[0]
315 assert returned_way[3].nil?
316 assert returned_way[4].nil?
317 assert returned_way[5].nil?
321 def test_getway_old_nonexistent
322 # try to get the last version+10 (shoudn't exist)
323 v1 = ways(:way_with_versions_v1)
324 # try to get last visible version of non-existent way
325 # try to get specific version of non-existent way
327 [0, '1 Jan 1970, 00:00:00'],
328 [v1.way_id, (v1.timestamp - 10).strftime("%d %b %Y, %H:%M:%S")]
330 amf_content "getway_old", "/1", [id, t]
332 assert_response :success
334 returned_way = amf_result("/1")
335 assert_equal -1, returned_way[0]
336 assert returned_way[3].nil?
337 assert returned_way[4].nil?
338 assert returned_way[5].nil?
342 def test_getway_history
343 latest = current_ways(:way_with_versions)
344 oldest = ways(:way_with_versions_v1)
346 amf_content "getway_history", "/1", [latest.id]
348 assert_response :success
350 history = amf_result("/1")
352 # ['way',wayid,history]
353 assert_equal 'way', history[0]
354 assert_equal latest.id, history[1]
355 # We use dates rather than version numbers here, because you might
356 # have moved a node within a way (i.e. way version not incremented).
357 # The timestamp is +1 because we say "give me the revision of 15:33:02",
358 # but that might actually include changes at 15:33:02.457.
359 assert_equal (latest.timestamp + 1).strftime("%d %b %Y, %H:%M:%S"), history[2].first[0]
360 assert_equal (oldest.timestamp + 1).strftime("%d %b %Y, %H:%M:%S"), history[2].last[0]
363 def test_getway_history_nonexistent
364 amf_content "getway_history", "/1", [0]
366 assert_response :success
368 history = amf_result("/1")
370 # ['way',wayid,history]
371 assert_equal history[0], 'way'
372 assert_equal history[1], 0
373 assert history[2].empty?
376 def test_getnode_history
377 latest = current_nodes(:node_with_versions)
378 amf_content "getnode_history", "/1", [latest.id]
380 assert_response :success
382 history = amf_result("/1")
384 # ['node',nodeid,history]
385 # note that (as per getway_history) we actually round up
387 assert_equal history[0], 'node',
388 'first element should be "node"'
389 assert_equal history[1], latest.id,
390 'second element should be the input node ID'
391 assert_equal history[2].first[0],
392 (latest.timestamp + 1).strftime("%d %b %Y, %H:%M:%S"),
393 'first element in third element (array) should be the latest version'
394 assert_equal history[2].last[0],
395 (nodes(:node_with_versions_v1).timestamp + 1).strftime("%d %b %Y, %H:%M:%S"),
396 'last element in third element (array) should be the initial version'
399 def test_getnode_history_nonexistent
400 amf_content "getnode_history", "/1", [0]
402 assert_response :success
404 history = amf_result("/1")
406 # ['node',nodeid,history]
407 assert_equal history[0], 'node'
408 assert_equal history[1], 0
409 assert history[2].empty?
412 # ************************************************************
414 def test_putpoi_update_valid
415 nd = current_nodes(:visible_node)
416 cs_id = changesets(:public_user_first_change).id
417 amf_content "putpoi", "/1", ["test@example.com:test", cs_id, nd.version, nd.id, nd.lon, nd.lat, nd.tags, nd.visible]
419 assert_response :success
421 result = amf_result("/1")
423 assert_equal 0, result[0]
424 assert_equal "", result[1]
425 assert_equal nd.id, result[2]
426 assert_equal nd.id, result[3]
427 assert_equal nd.version+1, result[4]
429 # Now try to update again, with a different lat/lon, using the updated version number
432 amf_content "putpoi", "/2", ["test@example.com:test", cs_id, nd.version+1, nd.id, lon, lat, nd.tags, nd.visible]
434 assert_response :success
436 result = amf_result("/2")
438 assert_equal 0, result[0]
439 assert_equal "", result[1]
440 assert_equal nd.id, result[2]
441 assert_equal nd.id, result[3]
442 assert_equal nd.version+2, result[4]
445 # Check that we can create a no valid poi
446 # Using similar method for the node controller test
447 def test_putpoi_create_valid
448 # This node has no tags
450 # create a node with random lat/lon
451 lat = rand(100)-50 + rand
452 lon = rand(100)-50 + rand
453 # normal user has a changeset open
454 changeset = changesets(:public_user_first_change)
456 amf_content "putpoi", "/1", ["test@example.com:test", changeset.id, nil, nil, lon, lat, {}, nil]
458 assert_response :success
460 result = amf_result("/1")
462 # check the array returned by the amf
463 assert_equal 5, result.size
464 assert_equal 0, result[0], "expected to get the status ok from the amf"
465 assert_equal 0, result[2], "The old id should be 0"
466 assert result[3] > 0, "The new id should be greater than 0"
467 assert_equal 1, result[4], "The new version should be 1"
469 # Finally check that the node that was saved has saved the data correctly
470 # in both the current and history tables
471 # First check the current table
472 current_node = Node.find(result[3].to_i)
473 assert_in_delta lat, current_node.lat, 0.00001, "The latitude was not retreieved correctly"
474 assert_in_delta lon, current_node.lon, 0.00001, "The longitude was not retreived correctly"
475 assert_equal 0, current_node.tags.size, "There seems to be a tag that has been added to the node"
476 assert_equal result[4], current_node.version, "The version returned, is different to the one returned by the amf"
477 # Now check the history table
478 historic_nodes = Node.where(:id => result[3])
479 assert_equal 1, historic_nodes.size, "There should only be one historic node created"
480 first_historic_node = historic_nodes.first
481 assert_in_delta lat, first_historic_node.lat, 0.00001, "The latitude was not retreived correctly"
482 assert_in_delta lon, first_historic_node.lon, 0.00001, "The longitude was not retreuved correctly"
483 assert_equal 0, first_historic_node.tags.size, "There seems to be a tag that have been attached to this node"
484 assert_equal result[4], first_historic_node.version, "The version returned, is different to the one returned by the amf"
487 # This node has some tags
489 # create a node with random lat/lon
490 lat = rand(100)-50 + rand
491 lon = rand(100)-50 + rand
492 # normal user has a changeset open
493 changeset = changesets(:public_user_first_change)
495 amf_content "putpoi", "/2", ["test@example.com:test", changeset.id, nil, nil, lon, lat, { "key" => "value", "ping" => "pong" }, nil]
497 assert_response :success
499 result = amf_result("/2")
501 # check the array returned by the amf
502 assert_equal 5, result.size
503 assert_equal 0, result[0], "Expected to get the status ok in the amf"
504 assert_equal 0, result[2], "The old id should be 0"
505 assert result[3] > 0, "The new id should be greater than 0"
506 assert_equal 1, result[4], "The new version should be 1"
508 # Finally check that the node that was saved has saved the data correctly
509 # in both the current and history tables
510 # First check the current table
511 current_node = Node.find(result[3].to_i)
512 assert_in_delta lat, current_node.lat, 0.00001, "The latitude was not retreieved correctly"
513 assert_in_delta lon, current_node.lon, 0.00001, "The longitude was not retreived correctly"
514 assert_equal 2, current_node.tags.size, "There seems to be a tag that has been added to the node"
515 assert_equal({ "key" => "value", "ping" => "pong" }, current_node.tags, "tags are different")
516 assert_equal result[4], current_node.version, "The version returned, is different to the one returned by the amf"
517 # Now check the history table
518 historic_nodes = Node.where(:id => result[3])
519 assert_equal 1, historic_nodes.size, "There should only be one historic node created"
520 first_historic_node = historic_nodes.first
521 assert_in_delta lat, first_historic_node.lat, 0.00001, "The latitude was not retreived correctly"
522 assert_in_delta lon, first_historic_node.lon, 0.00001, "The longitude was not retreuved correctly"
523 assert_equal 2, first_historic_node.tags.size, "There seems to be a tag that have been attached to this node"
524 assert_equal({ "key" => "value", "ping" => "pong" }, first_historic_node.tags, "tags are different")
525 assert_equal result[4], first_historic_node.version, "The version returned, is different to the one returned by the amf"
528 # try creating a POI with rubbish in the tags
529 def test_putpoi_create_with_control_chars
530 # This node has no tags
532 # create a node with random lat/lon
533 lat = rand(100)-50 + rand
534 lon = rand(100)-50 + rand
535 # normal user has a changeset open
536 changeset = changesets(:public_user_first_change)
538 mostly_invalid = (0..31).to_a.map {|i| i.chr}.join
539 tags = { "something" => "foo#{mostly_invalid}bar" }
541 amf_content "putpoi", "/1", ["test@example.com:test", changeset.id, nil, nil, lon, lat, tags, nil]
543 assert_response :success
545 result = amf_result("/1")
547 # check the array returned by the amf
548 assert_equal 5, result.size
549 assert_equal 0, result[0], "Expected to get the status ok in the amf"
550 assert_equal 0, result[2], "The old id should be 0"
551 assert result[3] > 0, "The new id should be greater than 0"
552 assert_equal 1, result[4], "The new version should be 1"
554 # Finally check that the node that was saved has saved the data correctly
555 # in both the current and history tables
556 # First check the current table
557 current_node = Node.find(result[3].to_i)
558 assert_equal 1, current_node.tags.size, "There seems to be a tag that has been added to the node"
559 assert_equal({ "something" => "foo\t\n\rbar" }, current_node.tags, "tags were not fixed correctly")
560 assert_equal result[4], current_node.version, "The version returned, is different to the one returned by the amf"
563 # try creating a POI with rubbish in the tags
564 def test_putpoi_create_with_invalid_utf8
565 # This node has no tags
567 # create a node with random lat/lon
568 lat = rand(100)-50 + rand
569 lon = rand(100)-50 + rand
570 # normal user has a changeset open
571 changeset = changesets(:public_user_first_change)
574 tags = { "something" => "foo#{invalid}bar" }
576 amf_content "putpoi", "/1", ["test@example.com:test", changeset.id, nil, nil, lon, lat, tags, nil]
578 assert_response :success
580 result = amf_result("/1")
582 assert_equal 2, result.size
583 assert_equal -1, result[0], "Expected to get the status FAIL in the amf"
584 assert_equal "One of the tags is invalid. Linux users may need to upgrade to Flash Player 10.1.", result[1]
587 def test_putpoi_delete_valid
591 def test_putpoi_delete_already_deleted
595 def test_putpoi_delete_not_found
599 def test_putpoi_invalid_latlon
603 def test_startchangeset_invalid_xmlchar_comment
605 comment = "foo#{invalid}bar"
607 amf_content "startchangeset", "/1", ["test@example.com:test", Hash.new, nil, comment, 1]
609 assert_response :success
611 result = amf_result("/1")
613 assert_equal 3, result.size, result.inspect
614 assert_equal 0, result[0]
615 new_cs_id = result[2].to_i
617 cs = Changeset.find(new_cs_id)
618 assert_equal "foobar", cs.tags["comment"]
621 # ************************************************************
622 # AMF Helper functions
624 # Get the result record for the specified ID
625 # It's an assertion FAIL if the record does not exist
627 assert @amf_result.has_key?("#{ref}/onResult")
628 @amf_result["#{ref}/onResult"]
631 # Encode the AMF message to invoke "target" with parameters as
632 # the passed data. The ref is used to retrieve the results.
633 def amf_content(target, ref, data)
636 c.write 0.chr+0.chr # version 0
637 c.write 0.chr+0.chr # n headers
638 c.write a.chr+b.chr # n bodies
639 c.write AMF.encodestring(target)
640 c.write AMF.encodestring(ref)
641 c.write [-1].pack("N")
642 c.write AMF.encodevalue(data)
644 @request.env["RAW_POST_DATA"] = c.string
647 # Parses the @response object as an AMF messsage.
648 # The result is a hash of message_ref => data.
649 # The attribute @amf_result is initialised to this hash.
650 def amf_parse_response
651 req = StringIO.new(@response.body)
653 req.read(2) # version
655 # parse through any headers
656 headers=AMF.getint(req) # Read number of headers
657 headers.times do # Read each header
658 name=AMF.getstring(req) # |
659 req.getc # | skip boolean
660 value=AMF.getvalue(req) # |
663 # parse through responses
665 bodies=AMF.getint(req) # Read number of bodies
666 bodies.times do # Read each body
667 message=AMF.getstring(req) # | get message name
668 index=AMF.getstring(req) # | get index in response sequence
669 bytes=AMF.getlong(req) # | get total size in bytes
670 args=AMF.getvalue(req) # | get response (probably an array)
671 results[message] = args
673 @amf_result = results
678 # given an array of bounding boxes (each an array of 4 floats), call the
679 # AMF "whichways" controller for each and pass the result back to the
680 # caller's block for assertion testing.
681 def check_bboxes_are_bad(bboxes)
682 bboxes.each do |bbox|
683 amf_content "whichways", "/1", bbox
685 assert_response :success
688 # pass the response back to the caller's block to be tested
689 # against what the caller expected.
690 map = amf_result "/1"
695 # this should be what AMF controller returns when the bbox of a
696 # whichways request is invalid or too large.
697 def assert_boundary_error(map, msg=nil, error_hint=nil)
698 expected_map = [-2, "Sorry - I can't get the map for that area.#{msg}"]
699 assert_equal expected_map, map, "AMF controller should have returned an error. (#{error_hint})"
702 # this should be what AMF controller returns when the bbox of a
703 # whichways_deleted request is invalid or too large.
704 def assert_deleted_boundary_error(map, msg=nil, error_hint=nil)
705 expected_map = [-2, "Sorry - I can't get the map for that area.#{msg}"]
706 assert_equal expected_map, map, "AMF controller should have returned an error. (#{error_hint})"