]> git.openstreetmap.org Git - rails.git/blob - test/functional/api_controller_test.rb
more testing of the api. Changing the generator so that it is a constant to come...
[rails.git] / test / functional / api_controller_test.rb
1 require File.dirname(__FILE__) + '/../test_helper'
2 require 'api_controller'
3
4 # Re-raise errors caught by the controller.
5 class ApiController; def rescue_action(e) raise e end; end
6
7 class ApiControllerTest < Test::Unit::TestCase
8   api_fixtures
9
10   def setup
11     @controller = ApiController.new
12     @request    = ActionController::TestRequest.new
13     @response   = ActionController::TestResponse.new
14   end
15
16   def basic_authorization(user, pass)
17     @request.env["HTTP_AUTHORIZATION"] = "Basic %s" % Base64.encode64("#{user}:#{pass}")
18   end
19
20   # -------------------------------------
21   # Test reading a bounding box.
22   # -------------------------------------
23
24   def test_map
25     node = current_nodes(:used_node_1)
26     # Need to split the min/max lat/lon out into their own variables here
27     # so that we can test they are returned later.
28     minlon = node.lon-0.1
29     minlat = node.lat-0.1
30     maxlon = node.lon+0.1
31     maxlat = node.lat+0.1
32     bbox = "#{minlon},#{minlat},#{maxlon},#{maxlat}"
33     get :map, :bbox => bbox
34     if $VERBOSE
35       print @request.to_yaml
36       print @response.body
37     end
38     assert_response :success
39     assert_select "osm[version='#{API_VERSION}'][generator='#{GENERATOR}']:root", :count => 1 do
40       assert_select "bounds[minlon=#{minlon}][minlat=#{minlat}][maxlon=#{maxlon}][maxlat=#{maxlat}]", :count => 1
41       assert_select "node[id=#{node.id}][lat=#{node.lat}][lon=#{node.lon}][version=#{node.version}][changeset=#{node.changeset_id}][visible=#{node.visible}][timestamp=#{node.timestamp.xmlschema}]", :count => 1 do
42         # This should really be more generic
43         assert_select "tag[k=test][v=1]"
44       end
45       # Should also test for the ways and relation
46     end
47   end
48   
49   def test_map_without_bbox
50     get :map
51     assert_response :bad_request
52     assert_equal "The parameter bbox is required, and must be of the form min_lon,min_lat,max_lon,max_lat", @response.body
53   end
54   
55   def test_traces_without_bbox
56     get :trackpoints
57     assert_response :bad_request
58     assert_equal "The parameter bbox is required, and must be of the form min_lon,min_lat,max_lon,max_lat", @response.body
59   end
60   
61   def test_traces_page_less_than_0
62     -10.upto(-1) do |i|
63       get :trackpoints, :page => i, :bbox => "-0.1,-0.1,0.1,0.1"
64       assert_response :bad_request
65       assert_equal "Page number must be greater than or equal to 0", @response.body
66     end
67     0.upto(10) do |i|
68       get :trackpoints, :page => i, :bbox => "-0.1,-0.1,0.1,0.1"
69       assert_response :success
70     end
71   end
72   
73   def test_traces_bbox_too_big
74     bad = %w{ -0.1,-0.1,1.1,1.1 10,10,11,11 }
75     bad.each do |bbox|
76       get :trackpoints, :bbox => bbox
77       assert_response :bad_request
78       assert_equal "The maximum bbox size is #{APP_CONFIG['max_request_area']}, and your request was too large. Either request a smaller area, or use planet.osm", @response.body
79     end
80   end
81   
82   def test_capabilities
83     get :capabilities
84     assert_response :success
85     assert_select "osm:root[version='#{API_VERSION}'][generator='#{GENERATOR}']", :count => 1 do
86       assert_select "api", :count => 1 do
87         assert_select "version[minimum=#{API_VERSION}][maximum=#{API_VERSION}]", :count => 1
88         assert_select "area[maximum=#{APP_CONFIG['max_request_area']}]", :count => 1
89         assert_select "tracepoints[per_page=#{APP_CONFIG['tracepoints_per_page']}]", :count => 1
90       end
91     end
92   end
93 end