]> git.openstreetmap.org Git - rails.git/blob - test/functional/api_controller_test.rb
Merge branch 'master' into openstreetbugs
[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   # -------------------------------------
22   # Test reading a bounding box.
23   # -------------------------------------
24
25   def test_map
26     node = current_nodes(:used_node_1)
27     # Need to split the min/max lat/lon out into their own variables here
28     # so that we can test they are returned later.
29     minlon = node.lon-0.1
30     minlat = node.lat-0.1
31     maxlon = node.lon+0.1
32     maxlat = node.lat+0.1
33     bbox = "#{minlon},#{minlat},#{maxlon},#{maxlat}"
34     get :map, :bbox => bbox
35     if $VERBOSE
36       print @request.to_yaml
37       print @response.body
38     end
39     assert_response :success, "Expected scucess with the map call"
40     assert_select "osm[version='#{API_VERSION}'][generator='#{GENERATOR}']:root", :count => 1 do
41       assert_select "bounds[minlon=#{minlon}][minlat=#{minlat}][maxlon=#{maxlon}][maxlat=#{maxlat}]", :count => 1
42       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
43         # This should really be more generic
44         assert_select "tag[k='test'][v='yes']"
45       end
46       # Should also test for the ways and relation
47     end
48   end
49   
50   # This differs from the above test in that we are making the bbox exactly
51   # the same as the node we are looking at
52   def test_map_inclusive
53     node = current_nodes(:used_node_1)
54     bbox = "#{node.lon},#{node.lat},#{node.lon},#{node.lat}"
55     get :map, :bbox => bbox
56     #print @response.body
57     assert_response :success, "The map call should have succeeded"
58     assert_select "osm[version='#{API_VERSION}'][generator='#{GENERATOR}']:root:empty", :count => 1
59   end
60   
61   def test_tracepoints
62     point = gpx_files(:public_trace_file)
63     minlon = point.longitude-0.001
64     minlat = point.latitude-0.001
65     maxlon = point.longitude+0.001
66     maxlat = point.latitude+0.001
67     bbox = "#{minlon},#{minlat},#{maxlon},#{maxlat}"
68     get :trackpoints, :bbox => bbox
69     assert_response :success
70     assert_select "gpx[version=1.0][creator=OpenStreetMap.org][xmlns=http://www.topografix.com/GPX/1/0]:root", :count => 1 do
71       assert_select "trk" do
72         assert_select "trkseg"
73       end
74     end
75   end
76   
77   def test_tracepoints_trackable
78     point = gpx_files(:trackable_trace_file)
79     minlon = point.longitude-0.002
80     minlat = point.latitude-0.002
81     maxlon = point.longitude+0.002
82     maxlat = point.latitude+0.002
83     bbox = "#{minlon},#{minlat},#{maxlon},#{maxlat}"
84     get :trackpoints, :bbox => bbox
85     assert_response :success
86     assert_select "gpx[version=1.0][creator=OpenStreetMap.org][xmlns=http://www.topografix.com/GPX/1/0]:root", :count => 1 do
87       assert_select "trk", :count => 1 do
88         assert_select "trk > trkseg", :count => 2 do |trksegs|
89           trksegs.each do |trkseg|
90             assert_select trkseg, "trkpt", :count => 1 do |trkpt|
91               assert_select trkpt[0], "time", :count => 1
92             end
93           end
94         end
95       end
96     end
97   end
98   
99   def test_tracepoints_identifiable
100     point = gpx_files(:identifiable_trace_file)
101     minlon = point.longitude-0.002
102     minlat = point.latitude-0.002
103     maxlon = point.longitude+0.002
104     maxlat = point.latitude+0.002
105     bbox = "#{minlon},#{minlat},#{maxlon},#{maxlat}"
106     get :trackpoints, :bbox => bbox
107     assert_response :success
108     assert_select "gpx[version=1.0][creator=OpenStreetMap.org][xmlns=http://www.topografix.com/GPX/1/0]:root", :count => 1 do
109       assert_select "trk", :count => 1 do
110         assert_select "trk>name", :count => 1
111         assert_select "trk>desc", :count => 1
112         assert_select "trk>url", :count => 1
113         assert_select "trkseg", :count => 1 do
114           assert_select "trkpt", :count => 1 do
115             assert_select "time", :count => 1
116           end
117         end
118       end
119     end
120   end
121   
122   def test_map_without_bbox
123     ["trackpoints", "map"].each do |tq|
124       get tq
125       assert_response :bad_request
126       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"
127     end
128   end
129   
130   def test_traces_page_less_than_0
131     -10.upto(-1) do |i|
132       get :trackpoints, :page => i, :bbox => "-0.1,-0.1,0.1,0.1"
133       assert_response :bad_request
134       assert_equal "Page number must be greater than or equal to 0", @response.body, "The page number was #{i}"
135     end
136     0.upto(10) do |i|
137       get :trackpoints, :page => i, :bbox => "-0.1,-0.1,0.1,0.1"
138       assert_response :success, "The page number was #{i} and should have been accepted"
139     end
140   end
141   
142   def test_bbox_too_big
143     @badbigbbox.each do |bbox|
144       [ "trackpoints", "map" ].each do |tq|
145         get tq, :bbox => bbox
146         assert_response :bad_request, "The bbox:#{bbox} was expected to be too big"
147         assert_equal "The maximum bbox size is #{MAX_REQUEST_AREA}, and your request was too large. Either request a smaller area, or use planet.osm", @response.body, "bbox: #{bbox}"
148       end
149     end
150   end
151   
152   def test_bbox_malformed
153     @badmalformedbbox.each do |bbox|
154       [ "trackpoints", "map" ].each do |tq|
155         get tq, :bbox => bbox
156         assert_response :bad_request, "The bbox:#{bbox} was expected to be malformed"
157         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}"
158       end
159     end
160   end
161   
162   def test_bbox_lon_mixedup
163     @badlonmixedbbox.each do |bbox|
164       [ "trackpoints", "map" ].each do |tq|
165         get tq, :bbox => bbox
166         assert_response :bad_request, "The bbox:#{bbox} was expected to have the longitude mixed up"
167         assert_equal "The minimum longitude must be less than the maximum longitude, but it wasn't", @response.body, "bbox: #{bbox}"
168       end
169     end
170   end
171   
172   def test_bbox_lat_mixedup
173     @badlatmixedbbox.each do |bbox|
174       ["trackpoints", "map"].each do |tq|
175         get tq, :bbox => bbox
176         assert_response :bad_request, "The bbox:#{bbox} was expected to have the latitude mixed up"
177         assert_equal "The minimum latitude must be less than the maximum latitude, but it wasn't", @response.body, "bbox: #{bbox}"
178       end
179     end
180   end
181   
182   # We can't actually get an out of bounds error, as the bbox is sanitised.
183   #def test_latlon_outofbounds
184   #  @badlatlonoutboundsbbox.each do |bbox|
185   #    [ "trackpoints", "map" ].each do |tq|
186   #      get tq, :bbox => bbox
187   #      #print @request.to_yaml
188   #      assert_response :bad_request, "The bbox #{bbox} was expected to be out of range"
189   #      assert_equal "The latitudes must be between -90 an 90, and longitudes between -180 and 180", @response.body, "bbox: #{bbox}"
190   #    end
191   #  end
192   #end
193   
194   # MySQL and Postgres require that the C based functions are installed for 
195   # this test to work. More information is available from:
196   # http://wiki.openstreetmap.org/wiki/Rails#Installing_the_quadtile_functions
197   # or by looking at the readme in db/README
198   def test_changes_simple
199     Timecop.freeze(Time.parse('2010-04-03 10:55:00'))
200     get :changes
201     assert_response :success
202     #print @response.body
203     # As we have loaded the fixtures, we can assume that there are no 
204     # changes at the time we have frozen at
205     now = Time.now.getutc
206     hourago = now - 1.hour
207     assert_select "osm[version='#{API_VERSION}'][generator='#{GENERATOR}']:root", :count => 1 do
208       assert_select "changes[starttime='#{hourago.xmlschema}'][endtime='#{now.xmlschema}']", :count => 1
209     end
210     Timecop.return
211   end
212   
213   def test_changes_zoom_invalid
214     zoom_to_test = %w{ p -1 0 17 one two }
215     zoom_to_test.each do |zoom|
216       get :changes, :zoom => zoom
217       assert_response :bad_request
218       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"
219     end
220   end
221   
222   def test_changes_zoom_valid
223     1.upto(16) do |zoom|
224       get :changes, :zoom => zoom
225       assert_response :success
226       # NOTE: there was a test here for the timing, but it was too sensitive to be a good test
227       # and it was annoying.
228       assert_select "osm[version='#{API_VERSION}'][generator='#{GENERATOR}']:root", :count => 1 do
229         assert_select "changes", :count => 1
230       end
231     end
232   end
233   
234   def test_start_end_time_invalid
235     
236   end
237   
238   def test_start_end_time_invalid
239     
240   end
241   
242   def test_hours_invalid
243     invalid = %w{ -21 335 -1 0 25 26 100 one two three ping pong : }
244     invalid.each do |hour|
245       get :changes, :hours => hour
246       assert_response :bad_request, "Problem with the hour: #{hour}"
247       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}."
248     end
249   end
250   
251   def test_hours_valid
252     1.upto(24) do |hour|
253       get :changes, :hours => hour
254       assert_response :success
255     end
256   end
257   
258   def test_capabilities
259     get :capabilities
260     assert_response :success
261     assert_select "osm:root[version='#{API_VERSION}'][generator='#{GENERATOR}']", :count => 1 do
262       assert_select "api", :count => 1 do
263         assert_select "version[minimum=#{API_VERSION}][maximum=#{API_VERSION}]", :count => 1
264         assert_select "area[maximum=#{MAX_REQUEST_AREA}]", :count => 1
265         assert_select "tracepoints[per_page=#{TRACEPOINTS_PER_PAGE}]", :count => 1
266         assert_select "changesets[maximum_elements=#{Changeset::MAX_ELEMENTS}]", :count => 1
267       end
268     end
269   end
270 end