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