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 = gpx_files(:public_trace_file)
136 minlon = point.longitude - 0.001
137 minlat = point.latitude - 0.001
138 maxlon = point.longitude + 0.001
139 maxlat = point.latitude + 0.001
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']", :count => 1 do
144 assert_select "trk" do
145 assert_select "trkseg"
150 def test_tracepoints_trackable
151 point = gpx_files(:trackable_trace_file)
152 minlon = point.longitude - 0.002
153 minlat = point.latitude - 0.002
154 maxlon = point.longitude + 0.002
155 maxlat = point.latitude + 0.002
156 bbox = "#{minlon},#{minlat},#{maxlon},#{maxlat}"
157 get :trackpoints, :bbox => bbox
158 assert_response :success
159 assert_select "gpx[version='1.0'][creator='OpenStreetMap.org']", :count => 1 do
160 assert_select "trk", :count => 1 do
161 assert_select "trk > trkseg", :count => 2 do |trksegs|
162 trksegs.each do |trkseg|
163 assert_select trkseg, "trkpt", :count => 1 do |trkpt|
164 assert_select trkpt[0], "time", :count => 1
172 def test_tracepoints_identifiable
173 point = gpx_files(:identifiable_trace_file)
174 minlon = point.longitude - 0.002
175 minlat = point.latitude - 0.002
176 maxlon = point.longitude + 0.002
177 maxlat = point.latitude + 0.002
178 bbox = "#{minlon},#{minlat},#{maxlon},#{maxlat}"
179 get :trackpoints, :bbox => bbox
180 assert_response :success
181 assert_select "gpx[version='1.0'][creator='OpenStreetMap.org']", :count => 1 do
182 assert_select "trk", :count => 1 do
183 assert_select "trk>name", :count => 1
184 assert_select "trk>desc", :count => 1
185 assert_select "trk>url", :count => 1
186 assert_select "trkseg", :count => 1 do
187 assert_select "trkpt", :count => 1 do
188 assert_select "time", :count => 1
195 def test_map_without_bbox
196 %w(trackpoints map).each do |tq|
198 assert_response :bad_request
199 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"
203 def test_traces_page_less_than_0
205 get :trackpoints, :page => i, :bbox => "-0.1,-0.1,0.1,0.1"
206 assert_response :bad_request
207 assert_equal "Page number must be greater than or equal to 0", @response.body, "The page number was #{i}"
210 get :trackpoints, :page => i, :bbox => "-0.1,-0.1,0.1,0.1"
211 assert_response :success, "The page number was #{i} and should have been accepted"
215 def test_bbox_too_big
216 @badbigbbox.each do |bbox|
217 %w(trackpoints map).each do |tq|
218 get tq, :bbox => bbox
219 assert_response :bad_request, "The bbox:#{bbox} was expected to be too big"
220 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}"
225 def test_bbox_malformed
226 @badmalformedbbox.each do |bbox|
227 %w(trackpoints map).each do |tq|
228 get tq, :bbox => bbox
229 assert_response :bad_request, "The bbox:#{bbox} was expected to be malformed"
230 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}"
235 def test_bbox_lon_mixedup
236 @badlonmixedbbox.each do |bbox|
237 %w(trackpoints map).each do |tq|
238 get tq, :bbox => bbox
239 assert_response :bad_request, "The bbox:#{bbox} was expected to have the longitude mixed up"
240 assert_equal "The minimum longitude must be less than the maximum longitude, but it wasn't", @response.body, "bbox: #{bbox}"
245 def test_bbox_lat_mixedup
246 @badlatmixedbbox.each do |bbox|
247 %w(trackpoints map).each do |tq|
248 get tq, :bbox => bbox
249 assert_response :bad_request, "The bbox:#{bbox} was expected to have the latitude mixed up"
250 assert_equal "The minimum latitude must be less than the maximum latitude, but it wasn't", @response.body, "bbox: #{bbox}"
255 # We can't actually get an out of bounds error, as the bbox is sanitised.
256 # def test_latlon_outofbounds
257 # @badlatlonoutboundsbbox.each do |bbox|
258 # [ "trackpoints", "map" ].each do |tq|
259 # get tq, :bbox => bbox
260 # #print @request.to_yaml
261 # assert_response :bad_request, "The bbox #{bbox} was expected to be out of range"
262 # assert_equal "The latitudes must be between -90 an 90, and longitudes between -180 and 180", @response.body, "bbox: #{bbox}"
267 # MySQL and Postgres require that the C based functions are installed for
268 # this test to work. More information is available from:
269 # http://wiki.openstreetmap.org/wiki/Rails#Installing_the_quadtile_functions
270 # or by looking at the readme in db/README
271 def test_changes_simple
272 Timecop.freeze(Time.parse("2010-04-03 10:55:00"))
274 assert_response :success
275 # print @response.body
276 # As we have loaded the fixtures, we can assume that there are no
277 # changes at the time we have frozen at
278 now = Time.now.getutc
279 hourago = now - 1.hour
280 assert_select "osm[version='#{API_VERSION}'][generator='#{GENERATOR}']", :count => 1 do
281 assert_select "changes[starttime='#{hourago.xmlschema}'][endtime='#{now.xmlschema}']", :count => 1 do
282 assert_select "tile", :count => 0
287 Timecop.freeze(Time.parse("2007-01-01 00:30:00"))
289 assert_response :success
290 # print @response.body
291 # As we have loaded the fixtures, we can assume that there are some
292 # changes at the time we have frozen at
293 now = Time.now.getutc
294 hourago = now - 1.hour
295 assert_select "osm[version='#{API_VERSION}'][generator='#{GENERATOR}']", :count => 1 do
296 assert_select "changes[starttime='#{hourago.xmlschema}'][endtime='#{now.xmlschema}']", :count => 1 do
297 assert_select "tile", :count => 10
303 def test_changes_zoom_invalid
304 zoom_to_test = %w(p -1 0 17 one two)
305 zoom_to_test.each do |zoom|
306 get :changes, :zoom => zoom
307 assert_response :bad_request
308 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"
312 def test_changes_zoom_valid
314 get :changes, :zoom => zoom
315 assert_response :success
316 # NOTE: there was a test here for the timing, but it was too sensitive to be a good test
317 # and it was annoying.
318 assert_select "osm[version='#{API_VERSION}'][generator='#{GENERATOR}']", :count => 1 do
319 assert_select "changes", :count => 1
324 def test_changes_hours_invalid
325 invalid = %w(-21 335 -1 0 25 26 100 one two three ping pong :)
326 invalid.each do |hour|
327 get :changes, :hours => hour
328 assert_response :bad_request, "Problem with the hour: #{hour}"
329 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}."
333 def test_changes_hours_valid
335 get :changes, :hours => hour
336 assert_response :success
340 def test_changes_start_end_invalid
341 get :changes, :start => "2010-04-03 10:55:00", :end => "2010-04-03 09:55:00"
342 assert_response :bad_request
343 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"
346 def test_changes_start_end_valid
347 get :changes, :start => "2010-04-03 09:55:00", :end => "2010-04-03 10:55:00"
348 assert_response :success
351 def test_capabilities
353 assert_response :success
354 assert_select "osm[version='#{API_VERSION}'][generator='#{GENERATOR}']", :count => 1 do
355 assert_select "api", :count => 1 do
356 assert_select "version[minimum='#{API_VERSION}'][maximum='#{API_VERSION}']", :count => 1
357 assert_select "area[maximum='#{MAX_REQUEST_AREA}']", :count => 1
358 assert_select "tracepoints[per_page='#{TRACEPOINTS_PER_PAGE}']", :count => 1
359 assert_select "changesets[maximum_elements='#{Changeset::MAX_ELEMENTS}']", :count => 1
360 assert_select "status[database='online']", :count => 1
361 assert_select "status[api='online']", :count => 1
362 assert_select "status[gpx='online']", :count => 1
367 def test_permissions_anonymous
369 assert_response :success
370 assert_select "osm > permissions", :count => 1 do
371 assert_select "permission", :count => 0
375 def test_permissions_basic_auth
376 basic_authorization(users(:normal_user).email, "test")
378 assert_response :success
379 assert_select "osm > permissions", :count => 1 do
380 assert_select "permission", :count => ClientApplication.all_permissions.size
381 ClientApplication.all_permissions.each do |p|
382 assert_select "permission[name='#{p}']", :count => 1
387 def test_permissions_oauth
388 @request.env["oauth.token"] = AccessToken.new do |token|
390 token.allow_read_prefs = true
391 token.allow_write_api = true
392 token.allow_read_gpx = false
395 assert_response :success
396 assert_select "osm > permissions", :count => 1 do
397 assert_select "permission", :count => 2
398 assert_select "permission[name='allow_read_prefs']", :count => 1
399 assert_select "permission[name='allow_write_api']", :count => 1
400 assert_select "permission[name='allow_read_gpx']", :count => 0