]> git.openstreetmap.org Git - rails.git/blob - test/controllers/api_controller_test.rb
Load api fixtures in tag model tests
[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 = gpx_files(:public_trace_file)
138     minlon = point.longitude - 0.001
139     minlat = point.latitude - 0.001
140     maxlon = point.longitude + 0.001
141     maxlat = point.latitude + 0.001
142     bbox = "#{minlon},#{minlat},#{maxlon},#{maxlat}"
143     get :trackpoints, :bbox => bbox
144     assert_response :success
145     assert_select "gpx[version='1.0'][creator='OpenStreetMap.org']", :count => 1 do
146       assert_select "trk" do
147         assert_select "trkseg"
148       end
149     end
150   end
151
152   def test_tracepoints_trackable
153     point = gpx_files(:trackable_trace_file)
154     minlon = point.longitude - 0.002
155     minlat = point.latitude - 0.002
156     maxlon = point.longitude + 0.002
157     maxlat = point.latitude + 0.002
158     bbox = "#{minlon},#{minlat},#{maxlon},#{maxlat}"
159     get :trackpoints, :bbox => bbox
160     assert_response :success
161     assert_select "gpx[version='1.0'][creator='OpenStreetMap.org']", :count => 1 do
162       assert_select "trk", :count => 1 do
163         assert_select "trk > trkseg", :count => 2 do |trksegs|
164           trksegs.each do |trkseg|
165             assert_select trkseg, "trkpt", :count => 1 do |trkpt|
166               assert_select trkpt[0], "time", :count => 1
167             end
168           end
169         end
170       end
171     end
172   end
173
174   def test_tracepoints_identifiable
175     point = gpx_files(:identifiable_trace_file)
176     minlon = point.longitude - 0.002
177     minlat = point.latitude - 0.002
178     maxlon = point.longitude + 0.002
179     maxlat = point.latitude + 0.002
180     bbox = "#{minlon},#{minlat},#{maxlon},#{maxlat}"
181     get :trackpoints, :bbox => bbox
182     assert_response :success
183     assert_select "gpx[version='1.0'][creator='OpenStreetMap.org']", :count => 1 do
184       assert_select "trk", :count => 1 do
185         assert_select "trk>name", :count => 1
186         assert_select "trk>desc", :count => 1
187         assert_select "trk>url", :count => 1
188         assert_select "trkseg", :count => 1 do
189           assert_select "trkpt", :count => 1 do
190             assert_select "time", :count => 1
191           end
192         end
193       end
194     end
195   end
196
197   def test_map_without_bbox
198     %w(trackpoints map).each do |tq|
199       get tq
200       assert_response :bad_request
201       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"
202     end
203   end
204
205   def test_traces_page_less_than_0
206     -10.upto(-1) do |i|
207       get :trackpoints, :page => i, :bbox => "-0.1,-0.1,0.1,0.1"
208       assert_response :bad_request
209       assert_equal "Page number must be greater than or equal to 0", @response.body, "The page number was #{i}"
210     end
211     0.upto(10) do |i|
212       get :trackpoints, :page => i, :bbox => "-0.1,-0.1,0.1,0.1"
213       assert_response :success, "The page number was #{i} and should have been accepted"
214     end
215   end
216
217   def test_bbox_too_big
218     @badbigbbox.each do |bbox|
219       %w(trackpoints map).each do |tq|
220         get tq, :bbox => bbox
221         assert_response :bad_request, "The bbox:#{bbox} was expected to be too big"
222         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}"
223       end
224     end
225   end
226
227   def test_bbox_malformed
228     @badmalformedbbox.each do |bbox|
229       %w(trackpoints map).each do |tq|
230         get tq, :bbox => bbox
231         assert_response :bad_request, "The bbox:#{bbox} was expected to be malformed"
232         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}"
233       end
234     end
235   end
236
237   def test_bbox_lon_mixedup
238     @badlonmixedbbox.each do |bbox|
239       %w(trackpoints map).each do |tq|
240         get tq, :bbox => bbox
241         assert_response :bad_request, "The bbox:#{bbox} was expected to have the longitude mixed up"
242         assert_equal "The minimum longitude must be less than the maximum longitude, but it wasn't", @response.body, "bbox: #{bbox}"
243       end
244     end
245   end
246
247   def test_bbox_lat_mixedup
248     @badlatmixedbbox.each do |bbox|
249       %w(trackpoints map).each do |tq|
250         get tq, :bbox => bbox
251         assert_response :bad_request, "The bbox:#{bbox} was expected to have the latitude mixed up"
252         assert_equal "The minimum latitude must be less than the maximum latitude, but it wasn't", @response.body, "bbox: #{bbox}"
253       end
254     end
255   end
256
257   # We can't actually get an out of bounds error, as the bbox is sanitised.
258   # def test_latlon_outofbounds
259   #  @badlatlonoutboundsbbox.each do |bbox|
260   #    [ "trackpoints", "map" ].each do |tq|
261   #      get tq, :bbox => bbox
262   #      #print @request.to_yaml
263   #      assert_response :bad_request, "The bbox #{bbox} was expected to be out of range"
264   #      assert_equal "The latitudes must be between -90 an 90, and longitudes between -180 and 180", @response.body, "bbox: #{bbox}"
265   #    end
266   #  end
267   # end
268
269   # MySQL and Postgres require that the C based functions are installed for
270   # this test to work. More information is available from:
271   # http://wiki.openstreetmap.org/wiki/Rails#Installing_the_quadtile_functions
272   # or by looking at the readme in db/README
273   def test_changes_simple
274     Timecop.freeze(Time.utc(2010, 4, 3, 10, 55, 0))
275     get :changes
276     assert_response :success
277     # print @response.body
278     # As we have loaded the fixtures, we can assume that there are no
279     # changes at the time we have frozen at
280     now = Time.now.getutc
281     hourago = now - 1.hour
282     assert_select "osm[version='#{API_VERSION}'][generator='#{GENERATOR}']", :count => 1 do
283       assert_select "changes[starttime='#{hourago.xmlschema}'][endtime='#{now.xmlschema}']", :count => 1 do
284         assert_select "tile", :count => 0
285       end
286     end
287     Timecop.return
288
289     Timecop.freeze(Time.utc(2007, 1, 1, 0, 30, 0))
290     get :changes
291     assert_response :success
292     # print @response.body
293     # As we have loaded the fixtures, we can assume that there are some
294     # changes at the time we have frozen at
295     now = Time.now.getutc
296     hourago = now - 1.hour
297     assert_select "osm[version='#{API_VERSION}'][generator='#{GENERATOR}']", :count => 1 do
298       assert_select "changes[starttime='#{hourago.xmlschema}'][endtime='#{now.xmlschema}']", :count => 1 do
299         assert_select "tile", :count => 10
300       end
301     end
302     Timecop.return
303   end
304
305   def test_changes_zoom_invalid
306     zoom_to_test = %w(p -1 0 17 one two)
307     zoom_to_test.each do |zoom|
308       get :changes, :zoom => zoom
309       assert_response :bad_request
310       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"
311     end
312   end
313
314   def test_changes_zoom_valid
315     1.upto(16) do |zoom|
316       get :changes, :zoom => zoom
317       assert_response :success
318       # NOTE: there was a test here for the timing, but it was too sensitive to be a good test
319       # and it was annoying.
320       assert_select "osm[version='#{API_VERSION}'][generator='#{GENERATOR}']", :count => 1 do
321         assert_select "changes", :count => 1
322       end
323     end
324   end
325
326   def test_changes_hours_invalid
327     invalid = %w(-21 335 -1 0 25 26 100 one two three ping pong :)
328     invalid.each do |hour|
329       get :changes, :hours => hour
330       assert_response :bad_request, "Problem with the hour: #{hour}"
331       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}."
332     end
333   end
334
335   def test_changes_hours_valid
336     1.upto(24) do |hour|
337       get :changes, :hours => hour
338       assert_response :success
339     end
340   end
341
342   def test_changes_start_end_invalid
343     get :changes, :start => "2010-04-03 10:55:00", :end => "2010-04-03 09:55:00"
344     assert_response :bad_request
345     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"
346   end
347
348   def test_changes_start_end_valid
349     get :changes, :start => "2010-04-03 09:55:00", :end => "2010-04-03 10:55:00"
350     assert_response :success
351   end
352
353   def test_capabilities
354     get :capabilities
355     assert_response :success
356     assert_select "osm[version='#{API_VERSION}'][generator='#{GENERATOR}']", :count => 1 do
357       assert_select "api", :count => 1 do
358         assert_select "version[minimum='#{API_VERSION}'][maximum='#{API_VERSION}']", :count => 1
359         assert_select "area[maximum='#{MAX_REQUEST_AREA}']", :count => 1
360         assert_select "tracepoints[per_page='#{TRACEPOINTS_PER_PAGE}']", :count => 1
361         assert_select "changesets[maximum_elements='#{Changeset::MAX_ELEMENTS}']", :count => 1
362         assert_select "status[database='online']", :count => 1
363         assert_select "status[api='online']", :count => 1
364         assert_select "status[gpx='online']", :count => 1
365       end
366     end
367   end
368
369   def test_permissions_anonymous
370     get :permissions
371     assert_response :success
372     assert_select "osm > permissions", :count => 1 do
373       assert_select "permission", :count => 0
374     end
375   end
376
377   def test_permissions_basic_auth
378     basic_authorization(users(:normal_user).email, "test")
379     get :permissions
380     assert_response :success
381     assert_select "osm > permissions", :count => 1 do
382       assert_select "permission", :count => ClientApplication.all_permissions.size
383       ClientApplication.all_permissions.each do |p|
384         assert_select "permission[name='#{p}']", :count => 1
385       end
386     end
387   end
388
389   def test_permissions_oauth
390     @request.env["oauth.token"] = AccessToken.new do |token|
391       # Just to test a few
392       token.allow_read_prefs = true
393       token.allow_write_api = true
394       token.allow_read_gpx = false
395     end
396     get :permissions
397     assert_response :success
398     assert_select "osm > permissions", :count => 1 do
399       assert_select "permission", :count => 2
400       assert_select "permission[name='allow_read_prefs']", :count => 1
401       assert_select "permission[name='allow_write_api']", :count => 1
402       assert_select "permission[name='allow_read_gpx']", :count => 0
403     end
404   end
405 end