1 require File.dirname(__FILE__) + '/../test_helper'
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.
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 # Should also test for the ways and relation
79 # This differs from the above test in that we are making the bbox exactly
80 # the same as the node we are looking at
81 def test_map_inclusive
82 node = current_nodes(:used_node_1)
83 bbox = "#{node.lon},#{node.lat},#{node.lon},#{node.lat}"
84 get :map, :bbox => bbox
85 assert_response :success, "The map call should have succeeded"
86 assert_select "osm[version='#{API_VERSION}'][generator='#{GENERATOR}']", :count => 1 do
87 assert_select "bounds[minlon=#{node.lon}][minlat=#{node.lat}][maxlon=#{node.lon}][maxlat=#{node.lat}]", :count => 1
88 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
89 # This should really be more generic
90 assert_select "tag[k='test'][v='yes']"
92 # Should also test for the ways and relation
97 point = gpx_files(:public_trace_file)
98 minlon = point.longitude-0.001
99 minlat = point.latitude-0.001
100 maxlon = point.longitude+0.001
101 maxlat = point.latitude+0.001
102 bbox = "#{minlon},#{minlat},#{maxlon},#{maxlat}"
103 get :trackpoints, :bbox => bbox
104 assert_response :success
105 assert_select "gpx[version=1.0][creator=OpenStreetMap.org][xmlns=http://www.topografix.com/GPX/1/0]", :count => 1 do
106 assert_select "trk" do
107 assert_select "trkseg"
112 def test_tracepoints_trackable
113 point = gpx_files(:trackable_trace_file)
114 minlon = point.longitude-0.002
115 minlat = point.latitude-0.002
116 maxlon = point.longitude+0.002
117 maxlat = point.latitude+0.002
118 bbox = "#{minlon},#{minlat},#{maxlon},#{maxlat}"
119 get :trackpoints, :bbox => bbox
120 assert_response :success
121 assert_select "gpx[version=1.0][creator=OpenStreetMap.org][xmlns=http://www.topografix.com/GPX/1/0]", :count => 1 do
122 assert_select "trk", :count => 1 do
123 assert_select "trk > trkseg", :count => 2 do |trksegs|
124 trksegs.each do |trkseg|
125 assert_select trkseg, "trkpt", :count => 1 do |trkpt|
126 assert_select trkpt[0], "time", :count => 1
134 def test_tracepoints_identifiable
135 point = gpx_files(:identifiable_trace_file)
136 minlon = point.longitude-0.002
137 minlat = point.latitude-0.002
138 maxlon = point.longitude+0.002
139 maxlat = point.latitude+0.002
140 bbox = "#{minlon},#{minlat},#{maxlon},#{maxlat}"
141 get :trackpoints, :bbox => bbox
142 assert_response :success
143 assert_select "gpx[version=1.0][creator=OpenStreetMap.org][xmlns=http://www.topografix.com/GPX/1/0]", :count => 1 do
144 assert_select "trk", :count => 1 do
145 assert_select "trk>name", :count => 1
146 assert_select "trk>desc", :count => 1
147 assert_select "trk>url", :count => 1
148 assert_select "trkseg", :count => 1 do
149 assert_select "trkpt", :count => 1 do
150 assert_select "time", :count => 1
157 def test_map_without_bbox
158 ["trackpoints", "map"].each do |tq|
160 assert_response :bad_request
161 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"
165 def test_traces_page_less_than_0
167 get :trackpoints, :page => i, :bbox => "-0.1,-0.1,0.1,0.1"
168 assert_response :bad_request
169 assert_equal "Page number must be greater than or equal to 0", @response.body, "The page number was #{i}"
172 get :trackpoints, :page => i, :bbox => "-0.1,-0.1,0.1,0.1"
173 assert_response :success, "The page number was #{i} and should have been accepted"
177 def test_bbox_too_big
178 @badbigbbox.each do |bbox|
179 [ "trackpoints", "map" ].each do |tq|
180 get tq, :bbox => bbox
181 assert_response :bad_request, "The bbox:#{bbox} was expected to be too big"
182 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}"
187 def test_bbox_malformed
188 @badmalformedbbox.each do |bbox|
189 [ "trackpoints", "map" ].each do |tq|
190 get tq, :bbox => bbox
191 assert_response :bad_request, "The bbox:#{bbox} was expected to be malformed"
192 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}"
197 def test_bbox_lon_mixedup
198 @badlonmixedbbox.each do |bbox|
199 [ "trackpoints", "map" ].each do |tq|
200 get tq, :bbox => bbox
201 assert_response :bad_request, "The bbox:#{bbox} was expected to have the longitude mixed up"
202 assert_equal "The minimum longitude must be less than the maximum longitude, but it wasn't", @response.body, "bbox: #{bbox}"
207 def test_bbox_lat_mixedup
208 @badlatmixedbbox.each do |bbox|
209 ["trackpoints", "map"].each do |tq|
210 get tq, :bbox => bbox
211 assert_response :bad_request, "The bbox:#{bbox} was expected to have the latitude mixed up"
212 assert_equal "The minimum latitude must be less than the maximum latitude, but it wasn't", @response.body, "bbox: #{bbox}"
217 # We can't actually get an out of bounds error, as the bbox is sanitised.
218 #def test_latlon_outofbounds
219 # @badlatlonoutboundsbbox.each do |bbox|
220 # [ "trackpoints", "map" ].each do |tq|
221 # get tq, :bbox => bbox
222 # #print @request.to_yaml
223 # assert_response :bad_request, "The bbox #{bbox} was expected to be out of range"
224 # assert_equal "The latitudes must be between -90 an 90, and longitudes between -180 and 180", @response.body, "bbox: #{bbox}"
229 # MySQL and Postgres require that the C based functions are installed for
230 # this test to work. More information is available from:
231 # http://wiki.openstreetmap.org/wiki/Rails#Installing_the_quadtile_functions
232 # or by looking at the readme in db/README
233 def test_changes_simple
234 Timecop.freeze(Time.parse('2010-04-03 10:55:00'))
236 assert_response :success
237 #print @response.body
238 # As we have loaded the fixtures, we can assume that there are no
239 # changes at the time we have frozen at
240 now = Time.now.getutc
241 hourago = now - 1.hour
242 assert_select "osm[version='#{API_VERSION}'][generator='#{GENERATOR}']", :count => 1 do
243 assert_select "changes[starttime='#{hourago.xmlschema}'][endtime='#{now.xmlschema}']", :count => 1
248 def test_changes_zoom_invalid
249 zoom_to_test = %w{ p -1 0 17 one two }
250 zoom_to_test.each do |zoom|
251 get :changes, :zoom => zoom
252 assert_response :bad_request
253 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"
257 def test_changes_zoom_valid
259 get :changes, :zoom => zoom
260 assert_response :success
261 # NOTE: there was a test here for the timing, but it was too sensitive to be a good test
262 # and it was annoying.
263 assert_select "osm[version='#{API_VERSION}'][generator='#{GENERATOR}']", :count => 1 do
264 assert_select "changes", :count => 1
269 def test_hours_invalid
270 invalid = %w{ -21 335 -1 0 25 26 100 one two three ping pong : }
271 invalid.each do |hour|
272 get :changes, :hours => hour
273 assert_response :bad_request, "Problem with the hour: #{hour}"
274 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}."
280 get :changes, :hours => hour
281 assert_response :success
285 def test_capabilities
287 assert_response :success
288 assert_select "osm[version='#{API_VERSION}'][generator='#{GENERATOR}']", :count => 1 do
289 assert_select "api", :count => 1 do
290 assert_select "version[minimum=#{API_VERSION}][maximum=#{API_VERSION}]", :count => 1
291 assert_select "area[maximum=#{MAX_REQUEST_AREA}]", :count => 1
292 assert_select "tracepoints[per_page=#{TRACEPOINTS_PER_PAGE}]", :count => 1
293 assert_select "changesets[maximum_elements=#{Changeset::MAX_ELEMENTS}]", :count => 1
294 assert_select "status[database=online]", :count => 1
295 assert_select "status[api=online]", :count => 1
296 assert_select "status[gpx=online]", :count => 1
301 def test_permissions_anonymous
303 assert_response :success
304 assert_select "osm > permissions", :count => 1 do
305 assert_select "permission", :count => 0
309 def test_permissions_basic_auth
310 basic_authorization(users(:normal_user).email, "test")
312 assert_response :success
313 assert_select "osm > permissions", :count => 1 do
314 assert_select "permission", :count => ClientApplication.all_permissions.size
315 ClientApplication.all_permissions.each do |p|
316 assert_select "permission[name=#{p}]", :count => 1
321 def test_permissions_oauth
322 @request.env["oauth.token"] = AccessToken.new do |token|
324 token.allow_read_prefs = true
325 token.allow_write_api = true
326 token.allow_read_gpx = false
329 assert_response :success
330 assert_select "osm > permissions", :count => 1 do
331 assert_select "permission", :count => 2
332 assert_select "permission[name=allow_read_prefs]", :count => 1
333 assert_select "permission[name=allow_write_api]", :count => 1
334 assert_select "permission[name=allow_read_gpx]", :count => 0