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/map", :method => :get },
34 { :controller => "api", :action => "map" }
37 { :path => "/api/0.6/trackpoints", :method => :get },
38 { :controller => "api", :action => "trackpoints" }
41 { :path => "/api/0.6/changes", :method => :get },
42 { :controller => "api", :action => "changes" }
46 # -------------------------------------
47 # Test reading a bounding box.
48 # -------------------------------------
51 node = current_nodes(:used_node_1)
52 # Need to split the min/max lat/lon out into their own variables here
53 # so that we can test they are returned later.
58 bbox = "#{minlon},#{minlat},#{maxlon},#{maxlat}"
59 get :map, :bbox => bbox
61 print @request.to_yaml
64 assert_response :success, "Expected scucess with the map call"
65 assert_select "osm[version='#{API_VERSION}'][generator='#{GENERATOR}']", :count => 1 do
66 assert_select "bounds[minlon=#{minlon}][minlat=#{minlat}][maxlon=#{maxlon}][maxlat=#{maxlat}]", :count => 1
67 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
68 # This should really be more generic
69 assert_select "tag[k='test'][v='yes']"
71 # Should also test for the ways and relation
75 # This differs from the above test in that we are making the bbox exactly
76 # the same as the node we are looking at
77 def test_map_inclusive
78 node = current_nodes(:used_node_1)
79 bbox = "#{node.lon},#{node.lat},#{node.lon},#{node.lat}"
80 get :map, :bbox => bbox
81 assert_response :success, "The map call should have succeeded"
82 assert_select "osm[version='#{API_VERSION}'][generator='#{GENERATOR}']", :count => 1 do
83 assert_select "bounds[minlon=#{node.lon}][minlat=#{node.lat}][maxlon=#{node.lon}][maxlat=#{node.lat}]", :count => 1
84 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
85 # This should really be more generic
86 assert_select "tag[k='test'][v='yes']"
88 # Should also test for the ways and relation
93 point = gpx_files(:public_trace_file)
94 minlon = point.longitude-0.001
95 minlat = point.latitude-0.001
96 maxlon = point.longitude+0.001
97 maxlat = point.latitude+0.001
98 bbox = "#{minlon},#{minlat},#{maxlon},#{maxlat}"
99 get :trackpoints, :bbox => bbox
100 assert_response :success
101 assert_select "gpx[version=1.0][creator=OpenStreetMap.org][xmlns=http://www.topografix.com/GPX/1/0]", :count => 1 do
102 assert_select "trk" do
103 assert_select "trkseg"
108 def test_tracepoints_trackable
109 point = gpx_files(:trackable_trace_file)
110 minlon = point.longitude-0.002
111 minlat = point.latitude-0.002
112 maxlon = point.longitude+0.002
113 maxlat = point.latitude+0.002
114 bbox = "#{minlon},#{minlat},#{maxlon},#{maxlat}"
115 get :trackpoints, :bbox => bbox
116 assert_response :success
117 assert_select "gpx[version=1.0][creator=OpenStreetMap.org][xmlns=http://www.topografix.com/GPX/1/0]", :count => 1 do
118 assert_select "trk", :count => 1 do
119 assert_select "trk > trkseg", :count => 2 do |trksegs|
120 trksegs.each do |trkseg|
121 assert_select trkseg, "trkpt", :count => 1 do |trkpt|
122 assert_select trkpt[0], "time", :count => 1
130 def test_tracepoints_identifiable
131 point = gpx_files(:identifiable_trace_file)
132 minlon = point.longitude-0.002
133 minlat = point.latitude-0.002
134 maxlon = point.longitude+0.002
135 maxlat = point.latitude+0.002
136 bbox = "#{minlon},#{minlat},#{maxlon},#{maxlat}"
137 get :trackpoints, :bbox => bbox
138 assert_response :success
139 assert_select "gpx[version=1.0][creator=OpenStreetMap.org][xmlns=http://www.topografix.com/GPX/1/0]", :count => 1 do
140 assert_select "trk", :count => 1 do
141 assert_select "trk>name", :count => 1
142 assert_select "trk>desc", :count => 1
143 assert_select "trk>url", :count => 1
144 assert_select "trkseg", :count => 1 do
145 assert_select "trkpt", :count => 1 do
146 assert_select "time", :count => 1
153 def test_map_without_bbox
154 ["trackpoints", "map"].each do |tq|
156 assert_response :bad_request
157 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"
161 def test_traces_page_less_than_0
163 get :trackpoints, :page => i, :bbox => "-0.1,-0.1,0.1,0.1"
164 assert_response :bad_request
165 assert_equal "Page number must be greater than or equal to 0", @response.body, "The page number was #{i}"
168 get :trackpoints, :page => i, :bbox => "-0.1,-0.1,0.1,0.1"
169 assert_response :success, "The page number was #{i} and should have been accepted"
173 def test_bbox_too_big
174 @badbigbbox.each do |bbox|
175 [ "trackpoints", "map" ].each do |tq|
176 get tq, :bbox => bbox
177 assert_response :bad_request, "The bbox:#{bbox} was expected to be too big"
178 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}"
183 def test_bbox_malformed
184 @badmalformedbbox.each do |bbox|
185 [ "trackpoints", "map" ].each do |tq|
186 get tq, :bbox => bbox
187 assert_response :bad_request, "The bbox:#{bbox} was expected to be malformed"
188 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}"
193 def test_bbox_lon_mixedup
194 @badlonmixedbbox.each do |bbox|
195 [ "trackpoints", "map" ].each do |tq|
196 get tq, :bbox => bbox
197 assert_response :bad_request, "The bbox:#{bbox} was expected to have the longitude mixed up"
198 assert_equal "The minimum longitude must be less than the maximum longitude, but it wasn't", @response.body, "bbox: #{bbox}"
203 def test_bbox_lat_mixedup
204 @badlatmixedbbox.each do |bbox|
205 ["trackpoints", "map"].each do |tq|
206 get tq, :bbox => bbox
207 assert_response :bad_request, "The bbox:#{bbox} was expected to have the latitude mixed up"
208 assert_equal "The minimum latitude must be less than the maximum latitude, but it wasn't", @response.body, "bbox: #{bbox}"
213 # We can't actually get an out of bounds error, as the bbox is sanitised.
214 #def test_latlon_outofbounds
215 # @badlatlonoutboundsbbox.each do |bbox|
216 # [ "trackpoints", "map" ].each do |tq|
217 # get tq, :bbox => bbox
218 # #print @request.to_yaml
219 # assert_response :bad_request, "The bbox #{bbox} was expected to be out of range"
220 # assert_equal "The latitudes must be between -90 an 90, and longitudes between -180 and 180", @response.body, "bbox: #{bbox}"
225 # MySQL and Postgres require that the C based functions are installed for
226 # this test to work. More information is available from:
227 # http://wiki.openstreetmap.org/wiki/Rails#Installing_the_quadtile_functions
228 # or by looking at the readme in db/README
229 def test_changes_simple
230 Timecop.freeze(Time.parse('2010-04-03 10:55:00'))
232 assert_response :success
233 #print @response.body
234 # As we have loaded the fixtures, we can assume that there are no
235 # changes at the time we have frozen at
236 now = Time.now.getutc
237 hourago = now - 1.hour
238 assert_select "osm[version='#{API_VERSION}'][generator='#{GENERATOR}']", :count => 1 do
239 assert_select "changes[starttime='#{hourago.xmlschema}'][endtime='#{now.xmlschema}']", :count => 1
244 def test_changes_zoom_invalid
245 zoom_to_test = %w{ p -1 0 17 one two }
246 zoom_to_test.each do |zoom|
247 get :changes, :zoom => zoom
248 assert_response :bad_request
249 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"
253 def test_changes_zoom_valid
255 get :changes, :zoom => zoom
256 assert_response :success
257 # NOTE: there was a test here for the timing, but it was too sensitive to be a good test
258 # and it was annoying.
259 assert_select "osm[version='#{API_VERSION}'][generator='#{GENERATOR}']", :count => 1 do
260 assert_select "changes", :count => 1
265 def test_start_end_time_invalid
269 def test_start_end_time_invalid
273 def test_hours_invalid
274 invalid = %w{ -21 335 -1 0 25 26 100 one two three ping pong : }
275 invalid.each do |hour|
276 get :changes, :hours => hour
277 assert_response :bad_request, "Problem with the hour: #{hour}"
278 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}."
284 get :changes, :hours => hour
285 assert_response :success
289 def test_capabilities
291 assert_response :success
292 assert_select "osm[version='#{API_VERSION}'][generator='#{GENERATOR}']", :count => 1 do
293 assert_select "api", :count => 1 do
294 assert_select "version[minimum=#{API_VERSION}][maximum=#{API_VERSION}]", :count => 1
295 assert_select "area[maximum=#{MAX_REQUEST_AREA}]", :count => 1
296 assert_select "tracepoints[per_page=#{TRACEPOINTS_PER_PAGE}]", :count => 1
297 assert_select "changesets[maximum_elements=#{Changeset::MAX_ELEMENTS}]", :count => 1
302 def test_permissions_anonymous
304 assert_response :success
305 assert_select "osm > permissions", :count => 1 do
306 assert_select "permission", :count => 0
310 def test_permissions_basic_auth
311 basic_authorization(users(:normal_user).email, "test")
313 assert_response :success
314 assert_select "osm > permissions", :count => 1 do
315 assert_select "permission", :count => ClientApplication.all_permissions.size
316 ClientApplication.all_permissions.each do |p|
317 assert_select "permission[name=#{p}]", :count => 1
322 def test_permissions_oauth
323 @request.env["oauth.token"] = AccessToken.new do |token|
325 token.allow_read_prefs = true
326 token.allow_write_api = true
327 token.allow_read_gpx = false
330 assert_response :success
331 assert_select "osm > permissions", :count => 1 do
332 assert_select "permission", :count => 2
333 assert_select "permission[name=allow_read_prefs]", :count => 1
334 assert_select "permission[name=allow_write_api]", :count => 1
335 assert_select "permission[name=allow_read_gpx]", :count => 0