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