]> git.openstreetmap.org Git - rails.git/blob - test/functional/api_controller_test.rb
forgot the test change from 11866
[rails.git] / test / functional / api_controller_test.rb
1 require File.dirname(__FILE__) + '/../test_helper'
2 require 'api_controller'
3
4 class ApiControllerTest < ActionController::TestCase
5   api_fixtures
6   
7   def setup
8     super
9     @badbigbbox = %w{ -0.1,-0.1,1.1,1.1  10,10,11,11 }
10     @badmalformedbbox = %w{ -0.1  hello 
11     10N2W10.1N2.1W }
12     @badlatmixedbbox = %w{ 0,0.1,0.1,0  -0.1,80,0.1,70  0.24,54.34,0.25,54.33 }
13     @badlonmixedbbox = %w{ 80,-0.1,70,0.1  54.34,0.24,54.33,0.25 }  
14     #@badlatlonoutboundsbbox = %w{ 191,-0.1,193,0.1  -190.1,89.9,-190,90 }
15     @goodbbox = %w{ -0.1,-0.1,0.1,0.1  51.1,-0.1,51.2,0 
16     -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 S0.1,W0.1,N0.1,E0.1}
17     # That last item in the goodbbox really shouldn't be there, as the API should
18     # reall reject it, however this is to test to see if the api changes.
19   end
20
21   def basic_authorization(user, pass)
22     @request.env["HTTP_AUTHORIZATION"] = "Basic %s" % Base64.encode64("#{user}:#{pass}")
23   end
24
25   # -------------------------------------
26   # Test reading a bounding box.
27   # -------------------------------------
28
29   def test_map
30     node = current_nodes(:used_node_1)
31     # Need to split the min/max lat/lon out into their own variables here
32     # so that we can test they are returned later.
33     minlon = node.lon-0.1
34     minlat = node.lat-0.1
35     maxlon = node.lon+0.1
36     maxlat = node.lat+0.1
37     bbox = "#{minlon},#{minlat},#{maxlon},#{maxlat}"
38     get :map, :bbox => bbox
39     if $VERBOSE
40       print @request.to_yaml
41       print @response.body
42     end
43     assert_response :success, "Expected scucess with the map call"
44     assert_select "osm[version='#{API_VERSION}'][generator='#{GENERATOR}']:root", :count => 1 do
45       assert_select "bounds[minlon=#{minlon}][minlat=#{minlat}][maxlon=#{maxlon}][maxlat=#{maxlat}]", :count => 1
46       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
47         # This should really be more generic
48         assert_select "tag[k='test'][v='yes']"
49       end
50       # Should also test for the ways and relation
51     end
52   end
53   
54   # This differs from the above test in that we are making the bbox exactly
55   # the same as the node we are looking at
56   def test_map_inclusive
57     node = current_nodes(:used_node_1)
58     bbox = "#{node.lon},#{node.lat},#{node.lon},#{node.lat}"
59     get :map, :bbox => bbox
60     #print @response.body
61     assert_response :success, "The map call should have succeeded"
62     assert_select "osm[version='#{API_VERSION}'][generator='#{GENERATOR}']:root:empty", :count => 1
63   end
64   
65   def test_tracepoints
66     point = gpx_files(:first_trace_file)
67     minlon = point.longitude-0.1
68     minlat = point.latitude-0.1
69     maxlon = point.longitude+0.1
70     maxlat = point.latitude+0.1
71     bbox = "#{minlon},#{minlat},#{maxlon},#{maxlat}"
72     get :trackpoints, :bbox => bbox
73     #print @response.body
74     assert_response :success
75     assert_select "gpx[version=1.0][creator=OpenStreetMap.org][xmlns=http://www.topografix.com/GPX/1/0/]:root", :count => 1 do
76       assert_select "trk" do
77         assert_select "trkseg"
78       end
79     end
80   end
81   
82   def test_map_without_bbox
83     ["trackpoints", "map"].each do |tq|
84       get tq
85       assert_response :bad_request
86       assert_equal "The parameter bbox is required, and must be of the form min_lon,min_lat,max_lon,max_lat", @response.body, "A bbox param was expected"
87     end
88   end
89   
90   def test_traces_page_less_than_0
91     -10.upto(-1) do |i|
92       get :trackpoints, :page => i, :bbox => "-0.1,-0.1,0.1,0.1"
93       assert_response :bad_request
94       assert_equal "Page number must be greater than or equal to 0", @response.body, "The page number was #{i}"
95     end
96     0.upto(10) do |i|
97       get :trackpoints, :page => i, :bbox => "-0.1,-0.1,0.1,0.1"
98       assert_response :success, "The page number was #{i} and should have been accepted"
99     end
100   end
101   
102   def test_bbox_too_big
103     @badbigbbox.each do |bbox|
104       [ "trackpoints", "map" ].each do |tq|
105         get tq, :bbox => bbox
106         assert_response :bad_request, "The bbox:#{bbox} was expected to be too big"
107         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, "bbox: #{bbox}"
108       end
109     end
110   end
111   
112   def test_bbox_malformed
113     @badmalformedbbox.each do |bbox|
114       [ "trackpoints", "map" ].each do |tq|
115         get tq, :bbox => bbox
116         assert_response :bad_request, "The bbox:#{bbox} was expected to be malformed"
117         assert_equal "The parameter bbox is required, and must be of the form min_lon,min_lat,max_lon,max_lat", @response.body, "bbox: #{bbox}"
118       end
119     end
120   end
121   
122   def test_bbox_lon_mixedup
123     @badlonmixedbbox.each do |bbox|
124       [ "trackpoints", "map" ].each do |tq|
125         get tq, :bbox => bbox
126         assert_response :bad_request, "The bbox:#{bbox} was expected to have the longitude mixed up"
127         assert_equal "The minimum longitude must be less than the maximum longitude, but it wasn't", @response.body, "bbox: #{bbox}"
128       end
129     end
130   end
131   
132   def test_bbox_lat_mixedup
133     @badlatmixedbbox.each do |bbox|
134       ["trackpoints", "map"].each do |tq|
135         get tq, :bbox => bbox
136         assert_response :bad_request, "The bbox:#{bbox} was expected to have the latitude mixed up"
137         assert_equal "The minimum latitude must be less than the maximum latitude, but it wasn't", @response.body, "bbox: #{bbox}"
138       end
139     end
140   end
141   
142   # We can't actually get an out of bounds error, as the bbox is sanitised.
143   #def test_latlon_outofbounds
144   #  @badlatlonoutboundsbbox.each do |bbox|
145   #    [ "trackpoints", "map" ].each do |tq|
146   #      get tq, :bbox => bbox
147   #      #print @request.to_yaml
148   #      assert_response :bad_request, "The bbox #{bbox} was expected to be out of range"
149   #      assert_equal "The latitudes must be between -90 an 90, and longitudes between -180 and 180", @response.body, "bbox: #{bbox}"
150   #    end
151   #  end
152   #end
153   
154   # MySQL requires that the C based functions are installed for this test to 
155   # work. More information is available from:
156   # http://wiki.openstreetmap.org/index.php/Rails#Installing_the_quadtile_functions
157   def test_changes_simple
158     get :changes
159     assert_response :success
160     #print @response.body
161     # As we have loaded the fixtures, we can assume that there are no 
162     # changes recently
163     now = Time.now
164     hourago = now - 1.hour
165     # Note that this may fail on a very slow machine, so isn't a great test
166     assert_select "osm[version='#{API_VERSION}'][generator='#{GENERATOR}']:root", :count => 1 do
167       assert_select "changes[starttime='#{hourago.xmlschema}'][endtime='#{now.xmlschema}']", :count => 1
168     end
169   end
170   
171   def test_changes_zoom_invalid
172     zoom_to_test = %w{ p -1 0 17 one two }
173     zoom_to_test.each do |zoom|
174       get :changes, :zoom => zoom
175       assert_response :bad_request
176       assert_equal @response.body, "Requested zoom is invalid, or the supplied start is after the end time, or the start duration is more than 24 hours"
177     end
178   end
179   
180   def test_changes_zoom_valid
181     1.upto(16) do |zoom|
182       get :changes, :zoom => zoom
183       assert_response :success
184       now = Time.now
185       hourago = now - 1.hour
186       # Note that this may fail on a very slow machine, so isn't a great test
187       assert_select "osm[version='#{API_VERSION}'][generator='#{GENERATOR}']:root", :count => 1 do
188         assert_select "changes[starttime='#{hourago.xmlschema}'][endtime='#{now.xmlschema}']", :count => 1
189       end
190     end
191   end
192   
193   def test_start_end_time_invalid
194     
195   end
196   
197   def test_start_end_time_invalid
198     
199   end
200   
201   def test_hours_invalid
202     invalid = %w{ -21 335 -1 0 25 26 100 one two three ping pong : }
203     invalid.each do |hour|
204       get :changes, :hours => hour
205       assert_response :bad_request, "Problem with the hour: #{hour}"
206       assert_equal @response.body, "Requested zoom is invalid, or the supplied start is after the end time, or the start duration is more than 24 hours", "Problem with the hour: #{hour}."
207     end
208   end
209   
210   def test_hours_valid
211     1.upto(24) do |hour|
212       get :changes, :hours => hour
213       assert_response :success
214     end
215   end
216   
217   def test_capabilities
218     get :capabilities
219     assert_response :success
220     assert_select "osm:root[version='#{API_VERSION}'][generator='#{GENERATOR}']", :count => 1 do
221       assert_select "api", :count => 1 do
222         assert_select "version[minimum=#{API_VERSION}][maximum=#{API_VERSION}]", :count => 1
223         assert_select "area[maximum=#{APP_CONFIG['max_request_area']}]", :count => 1
224         assert_select "tracepoints[per_page=#{APP_CONFIG['tracepoints_per_page']}]", :count => 1
225       end
226     end
227   end
228 end