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