]> git.openstreetmap.org Git - rails.git/blob - test/functional/api_controller_test.rb
4cd20d3dd9af1c175e5102cf1be66961651ff66a
[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     @badbigbbox = %w{ -0.1,-0.1,1.1,1.1 10,10,11,11 }
15     @badmalformedbbox = %w{ -0.1  hello S0.1,W0.1,N0.1,E0.1
16     10N2W10.1N2.1W }
17     @badlatmixedbbox = %w{ 0,0.1,0.1,0 }
18     @badlonmixedbbox = %w{}
19     @badlatlonoutboundsbbox = %w{ -190.2,-190.2,-190.1,-190.1 -190.1,89.9,-190,90 }
20     @goodbbox = %w{ -0.1,-0.1,0.1,0.1 51.1,-0.1,51.2,0 
21     -0.1,%20-0.1,%200.1,%200.1 -0.1edcd,-0.1d,0.1,0.1 -0.1E,-0.1E,0.1S,0.1N }
22   end
23
24   def basic_authorization(user, pass)
25     @request.env["HTTP_AUTHORIZATION"] = "Basic %s" % Base64.encode64("#{user}:#{pass}")
26   end
27
28   # -------------------------------------
29   # Test reading a bounding box.
30   # -------------------------------------
31
32   def test_map
33     node = current_nodes(:used_node_1)
34     # Need to split the min/max lat/lon out into their own variables here
35     # so that we can test they are returned later.
36     minlon = node.lon-0.1
37     minlat = node.lat-0.1
38     maxlon = node.lon+0.1
39     maxlat = node.lat+0.1
40     bbox = "#{minlon},#{minlat},#{maxlon},#{maxlat}"
41     get :map, :bbox => bbox
42     if $VERBOSE
43       print @request.to_yaml
44       print @response.body
45     end
46     assert_response :success
47     assert_select "osm[version='#{API_VERSION}'][generator='#{GENERATOR}']:root", :count => 1 do
48       assert_select "bounds[minlon=#{minlon}][minlat=#{minlat}][maxlon=#{maxlon}][maxlat=#{maxlat}]", :count => 1
49       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
50         # This should really be more generic
51         assert_select "tag[k=test][v=1]"
52       end
53       # Should also test for the ways and relation
54     end
55   end
56   
57   def test_map_without_bbox
58     ["trackpoints", "map"].each do |tq|
59       get tq
60       assert_response :bad_request
61       assert_equal "The parameter bbox is required, and must be of the form min_lon,min_lat,max_lon,max_lat", @response.body
62     end
63   end
64   
65   def test_traces_page_less_than_0
66     -10.upto(-1) do |i|
67       get :trackpoints, :page => i, :bbox => "-0.1,-0.1,0.1,0.1"
68       assert_response :bad_request
69       assert_equal "Page number must be greater than or equal to 0", @response.body
70     end
71     0.upto(10) do |i|
72       get :trackpoints, :page => i, :bbox => "-0.1,-0.1,0.1,0.1"
73       assert_response :success
74     end
75   end
76   
77   def test_bbox_too_big
78     @badbigbbox.each do |bbox|
79       [ "trackpoints", "map" ].each do |tq|
80         get tq, :bbox => bbox
81         assert_response :bad_request, "The bbox:#{bbox} was expected to be too big"
82         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
83       end
84     end
85   end
86   
87   def test_bbox_malformed
88     @badmalformedbbox.each do |bbox|
89       [ "trackpoints", "map" ].each do |tq|
90         get tq, :bbox => bbox
91         assert_response :bad_request, "The bbox:#{bbox} was expected to be malformed"
92         assert_equal "The parameter bbox is required, and must be of the form min_lon,min_lat,max_lon,max_lat", @response.body
93       end
94     end
95   end
96   
97   def test_bbox_lon_mixedup
98     @badlonmixedbbox.each do |bbox|
99       [ "trackpoints", "map" ].each do |tq|
100         get tq, :bbox => bbox
101         assert_response :bad_request, "The bbox:#{bbox} was expected to have the longitude mixed up"
102         assert_equal "The minimum longitude must be less than the maximum longitude, but it wasn't", @response.body
103       end
104     end
105   end
106   
107   def test_bbox_lat_mixedup
108     @badlatmixedbbox.each do |bbox|
109       ["trackpoints", "map"].each do |tq|
110         get tq, :bbox => bbox
111         assert_response :bad_request, "The bbox:#{bbox} was expected to have the latitude mixed up"
112         assert_equal "The minimum latitude must be less than the maximum latitude, but it wasn't", @response.body
113       end
114     end
115   end
116   
117   def test_latlon_outofbounds
118     @badlatlonoutboundsbbox.each do |bbox|
119       [ "trackpoints", "map" ].each do |tq|
120         get tq, :bbox => bbox
121         #print @request.to_yaml
122         assert_response :bad_request, "The bbox was expected to be out of range"
123         assert_equal "The latitudes must be between -90 an 90, and longitudes between -180 and 180", @response.body
124       end
125     end
126   end
127   
128   def test_capabilities
129     get :capabilities
130     assert_response :success
131     assert_select "osm:root[version='#{API_VERSION}'][generator='#{GENERATOR}']", :count => 1 do
132       assert_select "api", :count => 1 do
133         assert_select "version[minimum=#{API_VERSION}][maximum=#{API_VERSION}]", :count => 1
134         assert_select "area[maximum=#{APP_CONFIG['max_request_area']}]", :count => 1
135         assert_select "tracepoints[per_page=#{APP_CONFIG['tracepoints_per_page']}]", :count => 1
136       end
137     end
138   end
139 end