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