2 require "api_controller"
4 class ApiControllerTest < ActionController::TestCase
9 @badbigbbox = %w(-0.1,-0.1,1.1,1.1 10,10,11,11)
10 @badmalformedbbox = %w(-0.1 hello
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.
22 # test all routes which lead to this controller
25 { :path => "/api/capabilities", :method => :get },
26 { :controller => "api", :action => "capabilities" }
29 { :controller => "api", :action => "capabilities" },
30 { :path => "/api/0.6/capabilities", :method => :get }
33 { :path => "/api/0.6/permissions", :method => :get },
34 { :controller => "api", :action => "permissions" }
37 { :path => "/api/0.6/map", :method => :get },
38 { :controller => "api", :action => "map" }
41 { :path => "/api/0.6/trackpoints", :method => :get },
42 { :controller => "api", :action => "trackpoints" }
45 { :path => "/api/0.6/changes", :method => :get },
46 { :controller => "api", :action => "changes" }
50 # -------------------------------------
51 # Test reading a bounding box.
52 # -------------------------------------
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
66 print @request.to_yaml
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}']"
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
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}']"
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
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
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
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"
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
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
197 def test_map_without_bbox
198 %w(trackpoints map).each do |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"
205 def test_traces_page_less_than_0
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}"
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"
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}"
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}"
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}"
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}"
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}"
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))
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
289 Timecop.freeze(Time.utc(2007, 1, 1, 0, 30, 0))
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
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"
314 def test_changes_zoom_valid
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
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}."
335 def test_changes_hours_valid
337 get :changes, :hours => hour
338 assert_response :success
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"
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
353 def test_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
369 def test_permissions_anonymous
371 assert_response :success
372 assert_select "osm > permissions", :count => 1 do
373 assert_select "permission", :count => 0
377 def test_permissions_basic_auth
378 basic_authorization(users(:normal_user).email, "test")
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
389 def test_permissions_oauth
390 @request.env["oauth.token"] = AccessToken.new do |token|
392 token.allow_read_prefs = true
393 token.allow_write_api = true
394 token.allow_read_gpx = false
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