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 # Need to split the min/max lat/lon out into their own variables here
57 # so that we can test they are returned later.
58 minlon = node.lon - 0.1
59 minlat = node.lat - 0.1
60 maxlon = node.lon + 0.1
61 maxlat = node.lat + 0.1
62 bbox = "#{minlon},#{minlat},#{maxlon},#{maxlat}"
63 get :map, :bbox => bbox
65 print @request.to_yaml
68 assert_response :success, "Expected scucess with the map call"
69 assert_select "osm[version='#{API_VERSION}'][generator='#{GENERATOR}']", :count => 1 do
70 assert_select "bounds[minlon='#{minlon}'][minlat='#{minlat}'][maxlon='#{maxlon}'][maxlat='#{maxlat}']", :count => 1
71 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
72 # This should really be more generic
73 assert_select "tag[k='test'][v='yes']"
75 assert_select "way", :count => 2
76 assert_select "way[id='1']", :count => 1
77 assert_select "way[id='3']", :count => 1
78 assert_select "relation", :count => 1
79 assert_select "relation[id='1']", :count => 1
83 # This differs from the above test in that we are making the bbox exactly
84 # the same as the node we are looking at
85 def test_map_inclusive
86 node = current_nodes(:used_node_1)
87 bbox = "#{node.lon},#{node.lat},#{node.lon},#{node.lat}"
88 get :map, :bbox => bbox
89 assert_response :success, "The map call should have succeeded"
90 assert_select "osm[version='#{API_VERSION}'][generator='#{GENERATOR}']", :count => 1 do
91 assert_select "bounds[minlon='#{node.lon}'][minlat='#{node.lat}'][maxlon='#{node.lon}'][maxlat='#{node.lat}']", :count => 1
92 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
93 # This should really be more generic
94 assert_select "tag[k='test'][v='yes']"
96 assert_select "way", :count => 2
97 assert_select "way[id='1']", :count => 1
98 assert_select "way[id='3']", :count => 1
99 assert_select "relation", :count => 1
100 assert_select "relation[id='1']", :count => 1
104 def test_map_complete_way
105 node = current_nodes(:used_node_2)
106 bbox = "#{node.lon},#{node.lat},#{node.lon},#{node.lat}"
107 get :map, :bbox => bbox
108 assert_response :success, "The map call should have succeeded"
109 assert_select "osm[version='#{API_VERSION}'][generator='#{GENERATOR}']", :count => 1 do
110 assert_select "bounds[minlon='#{node.lon}'][minlat='#{node.lat}'][maxlon='#{node.lon}'][maxlat='#{node.lat}']", :count => 1
111 assert_select "node", :count => 3
112 assert_select "node[id='4']", :count => 1
113 assert_select "node[id='11']", :count => 1
114 assert_select "node[id='15']", :count => 1
115 assert_select "way", :count => 2
116 assert_select "way[id='5']", :count => 1
117 assert_select "way[id='7']", :count => 1
118 assert_select "relation", :count => 1
119 assert_select "relation[id='8']", :count => 1
124 get :map, :bbox => "179.998,89.998,179.999.1,89.999"
125 assert_response :success, "The map call should have succeeded"
126 assert_select "osm[version='#{API_VERSION}'][generator='#{GENERATOR}']", :count => 1 do
127 assert_select "bounds[minlon='179.998'][minlat='89.998'][maxlon='179.999'][maxlat='89.999']", :count => 1
128 assert_select "node", :count => 0
129 assert_select "way", :count => 0
130 assert_select "relation", :count => 0
135 point = create(:trace, :visibility => "public", :latitude => 1, :longitude => 1) do |trace|
136 create(:tracepoint, :trace => trace, :latitude => 1 * GeoRecord::SCALE, :longitude => 1 * GeoRecord::SCALE)
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 = create(:trace, :visibility => "trackable", :latitude => 51.51, :longitude => -0.14) do |trace|
154 create(:tracepoint, :trace => trace, :trackid => 1, :latitude => (51.510 * GeoRecord::SCALE).to_i, :longitude => (-0.140 * GeoRecord::SCALE).to_i)
155 create(:tracepoint, :trace => trace, :trackid => 2, :latitude => (51.511 * GeoRecord::SCALE).to_i, :longitude => (-0.141 * GeoRecord::SCALE).to_i)
157 minlon = point.longitude - 0.002
158 minlat = point.latitude - 0.002
159 maxlon = point.longitude + 0.002
160 maxlat = point.latitude + 0.002
161 bbox = "#{minlon},#{minlat},#{maxlon},#{maxlat}"
162 get :trackpoints, :bbox => bbox
163 assert_response :success
164 assert_select "gpx[version='1.0'][creator='OpenStreetMap.org']", :count => 1 do
165 assert_select "trk", :count => 1 do
166 assert_select "trk > trkseg", :count => 2 do |trksegs|
167 trksegs.each do |trkseg|
168 assert_select trkseg, "trkpt", :count => 1 do |trkpt|
169 assert_select trkpt[0], "time", :count => 1
177 def test_tracepoints_identifiable
178 point = create(:trace, :visibility => "identifiable", :latitude => 51.512, :longitude => 0.142) do |trace|
179 create(:tracepoint, :trace => trace, :latitude => (51.512 * GeoRecord::SCALE).to_i, :longitude => (0.142 * GeoRecord::SCALE).to_i)
181 minlon = point.longitude - 0.002
182 minlat = point.latitude - 0.002
183 maxlon = point.longitude + 0.002
184 maxlat = point.latitude + 0.002
185 bbox = "#{minlon},#{minlat},#{maxlon},#{maxlat}"
186 get :trackpoints, :bbox => bbox
187 assert_response :success
188 assert_select "gpx[version='1.0'][creator='OpenStreetMap.org']", :count => 1 do
189 assert_select "trk", :count => 1 do
190 assert_select "trk>name", :count => 1
191 assert_select "trk>desc", :count => 1
192 assert_select "trk>url", :count => 1
193 assert_select "trkseg", :count => 1 do
194 assert_select "trkpt", :count => 1 do
195 assert_select "time", :count => 1
202 def test_map_without_bbox
203 %w(trackpoints map).each do |tq|
205 assert_response :bad_request
206 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"
210 def test_traces_page_less_than_0
212 get :trackpoints, :page => i, :bbox => "-0.1,-0.1,0.1,0.1"
213 assert_response :bad_request
214 assert_equal "Page number must be greater than or equal to 0", @response.body, "The page number was #{i}"
217 get :trackpoints, :page => i, :bbox => "-0.1,-0.1,0.1,0.1"
218 assert_response :success, "The page number was #{i} and should have been accepted"
222 def test_bbox_too_big
223 @badbigbbox.each do |bbox|
224 %w(trackpoints map).each do |tq|
225 get tq, :bbox => bbox
226 assert_response :bad_request, "The bbox:#{bbox} was expected to be too big"
227 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}"
232 def test_bbox_malformed
233 @badmalformedbbox.each do |bbox|
234 %w(trackpoints map).each do |tq|
235 get tq, :bbox => bbox
236 assert_response :bad_request, "The bbox:#{bbox} was expected to be malformed"
237 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}"
242 def test_bbox_lon_mixedup
243 @badlonmixedbbox.each do |bbox|
244 %w(trackpoints map).each do |tq|
245 get tq, :bbox => bbox
246 assert_response :bad_request, "The bbox:#{bbox} was expected to have the longitude mixed up"
247 assert_equal "The minimum longitude must be less than the maximum longitude, but it wasn't", @response.body, "bbox: #{bbox}"
252 def test_bbox_lat_mixedup
253 @badlatmixedbbox.each do |bbox|
254 %w(trackpoints map).each do |tq|
255 get tq, :bbox => bbox
256 assert_response :bad_request, "The bbox:#{bbox} was expected to have the latitude mixed up"
257 assert_equal "The minimum latitude must be less than the maximum latitude, but it wasn't", @response.body, "bbox: #{bbox}"
262 # We can't actually get an out of bounds error, as the bbox is sanitised.
263 # def test_latlon_outofbounds
264 # @badlatlonoutboundsbbox.each do |bbox|
265 # [ "trackpoints", "map" ].each do |tq|
266 # get tq, :bbox => bbox
267 # #print @request.to_yaml
268 # assert_response :bad_request, "The bbox #{bbox} was expected to be out of range"
269 # assert_equal "The latitudes must be between -90 an 90, and longitudes between -180 and 180", @response.body, "bbox: #{bbox}"
274 # MySQL and Postgres require that the C based functions are installed for
275 # this test to work. More information is available from:
276 # http://wiki.openstreetmap.org/wiki/Rails#Installing_the_quadtile_functions
277 # or by looking at the readme in db/README
278 def test_changes_simple
279 Timecop.freeze(Time.utc(2010, 4, 3, 10, 55, 0))
281 assert_response :success
282 # print @response.body
283 # As we have loaded the fixtures, we can assume that there are no
284 # changes at the time we have frozen at
285 now = Time.now.getutc
286 hourago = now - 1.hour
287 assert_select "osm[version='#{API_VERSION}'][generator='#{GENERATOR}']", :count => 1 do
288 assert_select "changes[starttime='#{hourago.xmlschema}'][endtime='#{now.xmlschema}']", :count => 1 do
289 assert_select "tile", :count => 0
294 Timecop.freeze(Time.utc(2007, 1, 1, 0, 30, 0))
296 assert_response :success
297 # print @response.body
298 # As we have loaded the fixtures, we can assume that there are some
299 # changes at the time we have frozen at
300 now = Time.now.getutc
301 hourago = now - 1.hour
302 assert_select "osm[version='#{API_VERSION}'][generator='#{GENERATOR}']", :count => 1 do
303 assert_select "changes[starttime='#{hourago.xmlschema}'][endtime='#{now.xmlschema}']", :count => 1 do
304 assert_select "tile", :count => 10
310 def test_changes_zoom_invalid
311 zoom_to_test = %w(p -1 0 17 one two)
312 zoom_to_test.each do |zoom|
313 get :changes, :zoom => zoom
314 assert_response :bad_request
315 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"
319 def test_changes_zoom_valid
321 get :changes, :zoom => zoom
322 assert_response :success
323 # NOTE: there was a test here for the timing, but it was too sensitive to be a good test
324 # and it was annoying.
325 assert_select "osm[version='#{API_VERSION}'][generator='#{GENERATOR}']", :count => 1 do
326 assert_select "changes", :count => 1
331 def test_changes_hours_invalid
332 invalid = %w(-21 335 -1 0 25 26 100 one two three ping pong :)
333 invalid.each do |hour|
334 get :changes, :hours => hour
335 assert_response :bad_request, "Problem with the hour: #{hour}"
336 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}."
340 def test_changes_hours_valid
342 get :changes, :hours => hour
343 assert_response :success
347 def test_changes_start_end_invalid
348 get :changes, :start => "2010-04-03 10:55:00", :end => "2010-04-03 09:55:00"
349 assert_response :bad_request
350 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 def test_changes_start_end_valid
354 get :changes, :start => "2010-04-03 09:55:00", :end => "2010-04-03 10:55:00"
355 assert_response :success
358 def test_capabilities
360 assert_response :success
361 assert_select "osm[version='#{API_VERSION}'][generator='#{GENERATOR}']", :count => 1 do
362 assert_select "api", :count => 1 do
363 assert_select "version[minimum='#{API_VERSION}'][maximum='#{API_VERSION}']", :count => 1
364 assert_select "area[maximum='#{MAX_REQUEST_AREA}']", :count => 1
365 assert_select "tracepoints[per_page='#{TRACEPOINTS_PER_PAGE}']", :count => 1
366 assert_select "changesets[maximum_elements='#{Changeset::MAX_ELEMENTS}']", :count => 1
367 assert_select "status[database='online']", :count => 1
368 assert_select "status[api='online']", :count => 1
369 assert_select "status[gpx='online']", :count => 1
374 def test_permissions_anonymous
376 assert_response :success
377 assert_select "osm > permissions", :count => 1 do
378 assert_select "permission", :count => 0
382 def test_permissions_basic_auth
383 basic_authorization(users(:normal_user).email, "test")
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
394 def test_permissions_oauth
395 @request.env["oauth.token"] = AccessToken.new do |token|
397 token.allow_read_prefs = true
398 token.allow_write_api = true
399 token.allow_read_gpx = false
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