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