]> git.openstreetmap.org Git - rails.git/blob - test/controllers/api_controller_test.rb
Remove custom deny_access handlers
[rails.git] / test / controllers / api_controller_test.rb
1 require "test_helper"
2
3 class ApiControllerTest < ActionController::TestCase
4   def setup
5     super
6     @badbigbbox = %w[-0.1,-0.1,1.1,1.1 10,10,11,11]
7     @badmalformedbbox = %w[-0.1 hello
8                            10N2W10.1N2.1W]
9     @badlatmixedbbox = %w[0,0.1,0.1,0 -0.1,80,0.1,70 0.24,54.34,0.25,54.33]
10     @badlonmixedbbox = %w[80,-0.1,70,0.1 54.34,0.24,54.33,0.25]
11     # @badlatlonoutboundsbbox = %w{ 191,-0.1,193,0.1  -190.1,89.9,-190,90 }
12     @goodbbox = %w[-0.1,-0.1,0.1,0.1 51.1,-0.1,51.2,0
13                    -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]
14     # That last item in the goodbbox really shouldn't be there, as the API should
15     # reall reject it, however this is to test to see if the api changes.
16   end
17
18   ##
19   # test all routes which lead to this controller
20   def test_routes
21     assert_routing(
22       { :path => "/api/capabilities", :method => :get },
23       { :controller => "api", :action => "capabilities" }
24     )
25     assert_recognizes(
26       { :controller => "api", :action => "capabilities" },
27       { :path => "/api/0.6/capabilities", :method => :get }
28     )
29     assert_routing(
30       { :path => "/api/0.6/permissions", :method => :get },
31       { :controller => "api", :action => "permissions" }
32     )
33     assert_routing(
34       { :path => "/api/0.6/map", :method => :get },
35       { :controller => "api", :action => "map" }
36     )
37     assert_routing(
38       { :path => "/api/0.6/trackpoints", :method => :get },
39       { :controller => "api", :action => "trackpoints" }
40     )
41     assert_routing(
42       { :path => "/api/0.6/changes", :method => :get },
43       { :controller => "api", :action => "changes" }
44     )
45   end
46
47   # -------------------------------------
48   # Test reading a bounding box.
49   # -------------------------------------
50
51   def test_map
52     node = create(:node, :lat => 7, :lon => 7)
53     tag = create(:node_tag, :node => node)
54     way1 = create(:way_node, :node => node).way
55     way2 = create(:way_node, :node => node).way
56     relation = create(:relation_member, :member => node).relation
57
58     # Need to split the min/max lat/lon out into their own variables here
59     # so that we can test they are returned later.
60     minlon = node.lon - 0.1
61     minlat = node.lat - 0.1
62     maxlon = node.lon + 0.1
63     maxlat = node.lat + 0.1
64     bbox = "#{minlon},#{minlat},#{maxlon},#{maxlat}"
65     get :map, :params => { :bbox => bbox }
66     if $VERBOSE
67       print @request.to_yaml
68       print @response.body
69     end
70     assert_response :success, "Expected scucess with the map call"
71     assert_select "osm[version='#{API_VERSION}'][generator='#{GENERATOR}']", :count => 1 do
72       assert_select "bounds[minlon='#{format('%.7f', minlon)}'][minlat='#{format('%.7f', minlat)}'][maxlon='#{format('%.7f', maxlon)}'][maxlat='#{format('%.7f', maxlat)}']", :count => 1
73       assert_select "node[id='#{node.id}'][lat='#{format('%.7f', node.lat)}'][lon='#{format('%.7f', node.lon)}'][version='#{node.version}'][changeset='#{node.changeset_id}'][visible='#{node.visible}'][timestamp='#{node.timestamp.xmlschema}']", :count => 1 do
74         # This should really be more generic
75         assert_select "tag[k='#{tag.k}'][v='#{tag.v}']"
76       end
77       assert_select "way", :count => 2
78       assert_select "way[id='#{way1.id}']", :count => 1
79       assert_select "way[id='#{way2.id}']", :count => 1
80       assert_select "relation", :count => 1
81       assert_select "relation[id='#{relation.id}']", :count => 1
82     end
83   end
84
85   # This differs from the above test in that we are making the bbox exactly
86   # the same as the node we are looking at
87   def test_map_inclusive
88     node = create(:node, :lat => 7, :lon => 7)
89     tag = create(:node_tag, :node => node)
90     way1 = create(:way_node, :node => node).way
91     way2 = create(:way_node, :node => node).way
92     relation = create(:relation_member, :member => node).relation
93
94     bbox = "#{node.lon},#{node.lat},#{node.lon},#{node.lat}"
95     get :map, :params => { :bbox => bbox }
96     assert_response :success, "The map call should have succeeded"
97     assert_select "osm[version='#{API_VERSION}'][generator='#{GENERATOR}']", :count => 1 do
98       assert_select "bounds[minlon='#{node.lon}'][minlat='#{node.lat}'][maxlon='#{node.lon}'][maxlat='#{node.lat}']", :count => 1
99       assert_select "node[id='#{node.id}'][lat='#{format('%.7f', node.lat)}'][lon='#{format('%.7f', node.lon)}'][version='#{node.version}'][changeset='#{node.changeset_id}'][visible='#{node.visible}'][timestamp='#{node.timestamp.xmlschema}']", :count => 1 do
100         # This should really be more generic
101         assert_select "tag[k='#{tag.k}'][v='#{tag.v}']"
102       end
103       assert_select "way", :count => 2
104       assert_select "way[id='#{way1.id}']", :count => 1
105       assert_select "way[id='#{way2.id}']", :count => 1
106       assert_select "relation", :count => 1
107       assert_select "relation[id='#{relation.id}']", :count => 1
108     end
109   end
110
111   def test_map_complete_way
112     node = create(:node, :lat => 7, :lon => 7)
113     # create a couple of nodes well outside of the bbox
114     node2 = create(:node, :lat => 45, :lon => 45)
115     node3 = create(:node, :lat => 10, :lon => 10)
116     way1 = create(:way_node, :node => node).way
117     create(:way_node, :way => way1, :node => node2, :sequence_id => 2)
118     way2 = create(:way_node, :node => node).way
119     create(:way_node, :way => way2, :node => node3, :sequence_id => 2)
120     relation = create(:relation_member, :member => way1).relation
121
122     bbox = "#{node.lon},#{node.lat},#{node.lon},#{node.lat}"
123     get :map, :params => { :bbox => bbox }
124     assert_response :success, "The map call should have succeeded"
125     assert_select "osm[version='#{API_VERSION}'][generator='#{GENERATOR}']", :count => 1 do
126       assert_select "bounds[minlon='#{node.lon}'][minlat='#{node.lat}'][maxlon='#{node.lon}'][maxlat='#{node.lat}']", :count => 1
127       assert_select "node", :count => 3
128       assert_select "node[id='#{node.id}']", :count => 1
129       assert_select "node[id='#{node2.id}']", :count => 1
130       assert_select "node[id='#{node3.id}']", :count => 1
131       assert_select "way", :count => 2
132       assert_select "way[id='#{way1.id}']", :count => 1
133       assert_select "way[id='#{way2.id}']", :count => 1
134       assert_select "relation", :count => 1
135       assert_select "relation[id='#{relation.id}']", :count => 1
136     end
137   end
138
139   def test_map_empty
140     get :map, :params => { :bbox => "179.998,89.998,179.999.1,89.999" }
141     assert_response :success, "The map call should have succeeded"
142     assert_select "osm[version='#{API_VERSION}'][generator='#{GENERATOR}']", :count => 1 do
143       assert_select "bounds[minlon='179.9980000'][minlat='89.9980000'][maxlon='179.9990000'][maxlat='89.9990000']", :count => 1
144       assert_select "node", :count => 0
145       assert_select "way", :count => 0
146       assert_select "relation", :count => 0
147     end
148   end
149
150   def test_tracepoints
151     point = create(:trace, :visibility => "public", :latitude => 1, :longitude => 1) do |trace|
152       create(:tracepoint, :trace => trace, :latitude => 1 * GeoRecord::SCALE, :longitude => 1 * GeoRecord::SCALE)
153     end
154     minlon = point.longitude - 0.001
155     minlat = point.latitude - 0.001
156     maxlon = point.longitude + 0.001
157     maxlat = point.latitude + 0.001
158     bbox = "#{minlon},#{minlat},#{maxlon},#{maxlat}"
159     get :trackpoints, :params => { :bbox => bbox }
160     assert_response :success
161     assert_select "gpx[version='1.0'][creator='OpenStreetMap.org']", :count => 1 do
162       assert_select "trk" do
163         assert_select "trkseg"
164       end
165     end
166   end
167
168   def test_tracepoints_trackable
169     point = create(:trace, :visibility => "trackable", :latitude => 51.51, :longitude => -0.14) do |trace|
170       create(:tracepoint, :trace => trace, :trackid => 1, :latitude => (51.510 * GeoRecord::SCALE).to_i, :longitude => (-0.140 * GeoRecord::SCALE).to_i)
171       create(:tracepoint, :trace => trace, :trackid => 2, :latitude => (51.511 * GeoRecord::SCALE).to_i, :longitude => (-0.141 * GeoRecord::SCALE).to_i)
172     end
173     minlon = point.longitude - 0.002
174     minlat = point.latitude - 0.002
175     maxlon = point.longitude + 0.002
176     maxlat = point.latitude + 0.002
177     bbox = "#{minlon},#{minlat},#{maxlon},#{maxlat}"
178     get :trackpoints, :params => { :bbox => bbox }
179     assert_response :success
180     assert_select "gpx[version='1.0'][creator='OpenStreetMap.org']", :count => 1 do
181       assert_select "trk", :count => 1 do
182         assert_select "trk > trkseg", :count => 2 do |trksegs|
183           trksegs.each do |trkseg|
184             assert_select trkseg, "trkpt", :count => 1 do |trkpt|
185               assert_select trkpt[0], "time", :count => 1
186             end
187           end
188         end
189       end
190     end
191   end
192
193   def test_tracepoints_identifiable
194     point = create(:trace, :visibility => "identifiable", :latitude => 51.512, :longitude => 0.142) do |trace|
195       create(:tracepoint, :trace => trace, :latitude => (51.512 * GeoRecord::SCALE).to_i, :longitude => (0.142 * GeoRecord::SCALE).to_i)
196     end
197     minlon = point.longitude - 0.002
198     minlat = point.latitude - 0.002
199     maxlon = point.longitude + 0.002
200     maxlat = point.latitude + 0.002
201     bbox = "#{minlon},#{minlat},#{maxlon},#{maxlat}"
202     get :trackpoints, :params => { :bbox => bbox }
203     assert_response :success
204     assert_select "gpx[version='1.0'][creator='OpenStreetMap.org']", :count => 1 do
205       assert_select "trk", :count => 1 do
206         assert_select "trk>name", :count => 1
207         assert_select "trk>desc", :count => 1
208         assert_select "trk>url", :count => 1
209         assert_select "trkseg", :count => 1 do
210           assert_select "trkpt", :count => 1 do
211             assert_select "time", :count => 1
212           end
213         end
214       end
215     end
216   end
217
218   def test_map_without_bbox
219     %w[trackpoints map].each do |tq|
220       get tq
221       assert_response :bad_request
222       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"
223     end
224   end
225
226   def test_traces_page_less_than_0
227     -10.upto(-1) do |i|
228       get :trackpoints, :params => { :page => i, :bbox => "-0.1,-0.1,0.1,0.1" }
229       assert_response :bad_request
230       assert_equal "Page number must be greater than or equal to 0", @response.body, "The page number was #{i}"
231     end
232     0.upto(10) do |i|
233       get :trackpoints, :params => { :page => i, :bbox => "-0.1,-0.1,0.1,0.1" }
234       assert_response :success, "The page number was #{i} and should have been accepted"
235     end
236   end
237
238   def test_bbox_too_big
239     @badbigbbox.each do |bbox|
240       %w[trackpoints map].each do |tq|
241         get tq, :params => { :bbox => bbox }
242         assert_response :bad_request, "The bbox:#{bbox} was expected to be too big"
243         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}"
244       end
245     end
246   end
247
248   def test_bbox_malformed
249     @badmalformedbbox.each do |bbox|
250       %w[trackpoints map].each do |tq|
251         get tq, :params => { :bbox => bbox }
252         assert_response :bad_request, "The bbox:#{bbox} was expected to be malformed"
253         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}"
254       end
255     end
256   end
257
258   def test_bbox_lon_mixedup
259     @badlonmixedbbox.each do |bbox|
260       %w[trackpoints map].each do |tq|
261         get tq, :params => { :bbox => bbox }
262         assert_response :bad_request, "The bbox:#{bbox} was expected to have the longitude mixed up"
263         assert_equal "The minimum longitude must be less than the maximum longitude, but it wasn't", @response.body, "bbox: #{bbox}"
264       end
265     end
266   end
267
268   def test_bbox_lat_mixedup
269     @badlatmixedbbox.each do |bbox|
270       %w[trackpoints map].each do |tq|
271         get tq, :params => { :bbox => bbox }
272         assert_response :bad_request, "The bbox:#{bbox} was expected to have the latitude mixed up"
273         assert_equal "The minimum latitude must be less than the maximum latitude, but it wasn't", @response.body, "bbox: #{bbox}"
274       end
275     end
276   end
277
278   # We can't actually get an out of bounds error, as the bbox is sanitised.
279   # def test_latlon_outofbounds
280   #  @badlatlonoutboundsbbox.each do |bbox|
281   #    [ "trackpoints", "map" ].each do |tq|
282   #      get tq, :bbox => bbox
283   #      #print @request.to_yaml
284   #      assert_response :bad_request, "The bbox #{bbox} was expected to be out of range"
285   #      assert_equal "The latitudes must be between -90 an 90, and longitudes between -180 and 180", @response.body, "bbox: #{bbox}"
286   #    end
287   #  end
288   # end
289
290   # MySQL and Postgres require that the C based functions are installed for
291   # this test to work. More information is available from:
292   # http://wiki.openstreetmap.org/wiki/Rails#Installing_the_quadtile_functions
293   # or by looking at the readme in db/README
294   def test_changes_simple
295     # create a selection of nodes
296     (1..5).each do |n|
297       create(:node, :timestamp => Time.utc(2007, 1, 1, 0, 0, 0), :lat => n, :lon => n)
298     end
299     # deleted nodes should also be counted
300     create(:node, :deleted, :timestamp => Time.utc(2007, 1, 1, 0, 0, 0), :lat => 6, :lon => 6)
301     # nodes in the same tile won't change the total
302     create(:node, :timestamp => Time.utc(2007, 1, 1, 0, 0, 0), :lat => 6, :lon => 6)
303     # nodes with a different timestamp should be ignored
304     create(:node, :timestamp => Time.utc(2008, 1, 1, 0, 0, 0), :lat => 7, :lon => 7)
305
306     travel_to Time.utc(2010, 4, 3, 10, 55, 0) do
307       get :changes
308       assert_response :success
309       now = Time.now.getutc
310       hourago = now - 1.hour
311       assert_select "osm[version='#{API_VERSION}'][generator='#{GENERATOR}']", :count => 1 do
312         assert_select "changes[starttime='#{hourago.xmlschema}'][endtime='#{now.xmlschema}']", :count => 1 do
313           assert_select "tile", :count => 0
314         end
315       end
316     end
317
318     travel_to Time.utc(2007, 1, 1, 0, 30, 0) do
319       get :changes
320       assert_response :success
321       # print @response.body
322       # As we have loaded the fixtures, we can assume that there are some
323       # changes at the time we have frozen at
324       now = Time.now.getutc
325       hourago = now - 1.hour
326       assert_select "osm[version='#{API_VERSION}'][generator='#{GENERATOR}']", :count => 1 do
327         assert_select "changes[starttime='#{hourago.xmlschema}'][endtime='#{now.xmlschema}']", :count => 1 do
328           assert_select "tile", :count => 6
329         end
330       end
331     end
332   end
333
334   def test_changes_zoom_invalid
335     zoom_to_test = %w[p -1 0 17 one two]
336     zoom_to_test.each do |zoom|
337       get :changes, :params => { :zoom => zoom }
338       assert_response :bad_request
339       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"
340     end
341   end
342
343   def test_changes_zoom_valid
344     1.upto(16) do |zoom|
345       get :changes, :params => { :zoom => zoom }
346       assert_response :success
347       # NOTE: there was a test here for the timing, but it was too sensitive to be a good test
348       # and it was annoying.
349       assert_select "osm[version='#{API_VERSION}'][generator='#{GENERATOR}']", :count => 1 do
350         assert_select "changes", :count => 1
351       end
352     end
353   end
354
355   def test_changes_hours_invalid
356     invalid = %w[-21 335 -1 0 25 26 100 one two three ping pong :]
357     invalid.each do |hour|
358       get :changes, :params => { :hours => hour }
359       assert_response :bad_request, "Problem with the hour: #{hour}"
360       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}."
361     end
362   end
363
364   def test_changes_hours_valid
365     1.upto(24) do |hour|
366       get :changes, :params => { :hours => hour }
367       assert_response :success
368     end
369   end
370
371   def test_changes_start_end_invalid
372     get :changes, :params => { :start => "2010-04-03 10:55:00", :end => "2010-04-03 09:55:00" }
373     assert_response :bad_request
374     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"
375   end
376
377   def test_changes_start_end_valid
378     get :changes, :params => { :start => "2010-04-03 09:55:00", :end => "2010-04-03 10:55:00" }
379     assert_response :success
380   end
381
382   def test_capabilities
383     get :capabilities
384     assert_response :success
385     assert_select "osm[version='#{API_VERSION}'][generator='#{GENERATOR}']", :count => 1 do
386       assert_select "api", :count => 1 do
387         assert_select "version[minimum='#{API_VERSION}'][maximum='#{API_VERSION}']", :count => 1
388         assert_select "area[maximum='#{MAX_REQUEST_AREA}']", :count => 1
389         assert_select "note_area[maximum='#{MAX_NOTE_REQUEST_AREA}']", :count => 1
390         assert_select "tracepoints[per_page='#{TRACEPOINTS_PER_PAGE}']", :count => 1
391         assert_select "changesets[maximum_elements='#{Changeset::MAX_ELEMENTS}']", :count => 1
392         assert_select "status[database='online']", :count => 1
393         assert_select "status[api='online']", :count => 1
394         assert_select "status[gpx='online']", :count => 1
395       end
396     end
397   end
398
399   def test_permissions_anonymous
400     get :permissions
401     assert_response :success
402     assert_select "osm > permissions", :count => 1 do
403       assert_select "permission", :count => 0
404     end
405   end
406
407   def test_permissions_basic_auth
408     basic_authorization create(:user).email, "test"
409     get :permissions
410     assert_response :success
411     assert_select "osm > permissions", :count => 1 do
412       assert_select "permission", :count => ClientApplication.all_permissions.size
413       ClientApplication.all_permissions.each do |p|
414         assert_select "permission[name='#{p}']", :count => 1
415       end
416     end
417   end
418
419   def test_permissions_oauth
420     @request.env["oauth.token"] = AccessToken.new do |token|
421       # Just to test a few
422       token.allow_read_prefs = true
423       token.allow_write_api = true
424       token.allow_read_gpx = false
425     end
426     get :permissions
427     assert_response :success
428     assert_select "osm > permissions", :count => 1 do
429       assert_select "permission", :count => 2
430       assert_select "permission[name='allow_read_prefs']", :count => 1
431       assert_select "permission[name='allow_write_api']", :count => 1
432       assert_select "permission[name='allow_read_gpx']", :count => 0
433     end
434   end
435 end