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 = create(:node, :lat => 7, :lon => 7)
56 tag = create(:node_tag, :node => node)
57 way1 = create(:way_node, :node => node).way
58 way2 = create(:way_node, :node => node).way
59 relation = create(:relation_member, :member => node).relation
61 # Need to split the min/max lat/lon out into their own variables here
62 # so that we can test they are returned later.
63 minlon = node.lon - 0.1
64 minlat = node.lat - 0.1
65 maxlon = node.lon + 0.1
66 maxlat = node.lat + 0.1
67 bbox = "#{minlon},#{minlat},#{maxlon},#{maxlat}"
68 get :map, :bbox => bbox
70 print @request.to_yaml
73 assert_response :success, "Expected scucess with the map call"
74 assert_select "osm[version='#{API_VERSION}'][generator='#{GENERATOR}']", :count => 1 do
75 assert_select "bounds[minlon='#{minlon}'][minlat='#{minlat}'][maxlon='#{maxlon}'][maxlat='#{maxlat}']", :count => 1
76 assert_select "node[id='#{node.id}'][lat='#{format('%.7f', node.lat)}'][lon='#{format('%.7f', node.lon)}'][version='#{node.version}'][changeset='#{node.changeset_id}'][visible='#{node.visible}'][timestamp='#{node.timestamp.xmlschema}']", :count => 1 do
77 # This should really be more generic
78 assert_select "tag[k='#{tag.k}'][v='#{tag.v}']"
80 assert_select "way", :count => 2
81 assert_select "way[id='#{way1.id}']", :count => 1
82 assert_select "way[id='#{way2.id}']", :count => 1
83 assert_select "relation", :count => 1
84 assert_select "relation[id='#{relation.id}']", :count => 1
88 # This differs from the above test in that we are making the bbox exactly
89 # the same as the node we are looking at
90 def test_map_inclusive
91 node = create(:node, :lat => 7, :lon => 7)
92 tag = create(:node_tag, :node => node)
93 way1 = create(:way_node, :node => node).way
94 way2 = create(:way_node, :node => node).way
95 relation = create(:relation_member, :member => node).relation
97 bbox = "#{node.lon},#{node.lat},#{node.lon},#{node.lat}"
98 get :map, :bbox => bbox
99 assert_response :success, "The map call should have succeeded"
100 assert_select "osm[version='#{API_VERSION}'][generator='#{GENERATOR}']", :count => 1 do
101 assert_select "bounds[minlon='#{node.lon}'][minlat='#{node.lat}'][maxlon='#{node.lon}'][maxlat='#{node.lat}']", :count => 1
102 assert_select "node[id='#{node.id}'][lat='#{format('%.7f', node.lat)}'][lon='#{format('%.7f', node.lon)}'][version='#{node.version}'][changeset='#{node.changeset_id}'][visible='#{node.visible}'][timestamp='#{node.timestamp.xmlschema}']", :count => 1 do
103 # This should really be more generic
104 assert_select "tag[k='#{tag.k}'][v='#{tag.v}']"
106 assert_select "way", :count => 2
107 assert_select "way[id='#{way1.id}']", :count => 1
108 assert_select "way[id='#{way2.id}']", :count => 1
109 assert_select "relation", :count => 1
110 assert_select "relation[id='#{relation.id}']", :count => 1
114 def test_map_complete_way
115 node = create(:node, :lat => 7, :lon => 7)
116 # create a couple of nodes well outside of the bbox
117 node2 = create(:node, :lat => 45, :lon => 45)
118 node3 = create(:node, :lat => 10, :lon => 10)
119 way1 = create(:way_node, :node => node).way
120 create(:way_node, :way => way1, :node => node2, :sequence_id => 2)
121 way2 = create(:way_node, :node => node).way
122 create(:way_node, :way => way2, :node => node3, :sequence_id => 2)
123 relation = create(:relation_member, :member => way1).relation
125 bbox = "#{node.lon},#{node.lat},#{node.lon},#{node.lat}"
126 get :map, :bbox => bbox
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='#{node.lon}'][minlat='#{node.lat}'][maxlon='#{node.lon}'][maxlat='#{node.lat}']", :count => 1
130 assert_select "node", :count => 3
131 assert_select "node[id='#{node.id}']", :count => 1
132 assert_select "node[id='#{node2.id}']", :count => 1
133 assert_select "node[id='#{node3.id}']", :count => 1
134 assert_select "way", :count => 2
135 assert_select "way[id='#{way1.id}']", :count => 1
136 assert_select "way[id='#{way2.id}']", :count => 1
137 assert_select "relation", :count => 1
138 assert_select "relation[id='#{relation.id}']", :count => 1
143 get :map, :bbox => "179.998,89.998,179.999.1,89.999"
144 assert_response :success, "The map call should have succeeded"
145 assert_select "osm[version='#{API_VERSION}'][generator='#{GENERATOR}']", :count => 1 do
146 assert_select "bounds[minlon='179.998'][minlat='89.998'][maxlon='179.999'][maxlat='89.999']", :count => 1
147 assert_select "node", :count => 0
148 assert_select "way", :count => 0
149 assert_select "relation", :count => 0
154 point = create(:trace, :visibility => "public", :latitude => 1, :longitude => 1) do |trace|
155 create(:tracepoint, :trace => trace, :latitude => 1 * GeoRecord::SCALE, :longitude => 1 * GeoRecord::SCALE)
157 minlon = point.longitude - 0.001
158 minlat = point.latitude - 0.001
159 maxlon = point.longitude + 0.001
160 maxlat = point.latitude + 0.001
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" do
166 assert_select "trkseg"
171 def test_tracepoints_trackable
172 point = create(:trace, :visibility => "trackable", :latitude => 51.51, :longitude => -0.14) do |trace|
173 create(:tracepoint, :trace => trace, :trackid => 1, :latitude => (51.510 * GeoRecord::SCALE).to_i, :longitude => (-0.140 * GeoRecord::SCALE).to_i)
174 create(:tracepoint, :trace => trace, :trackid => 2, :latitude => (51.511 * GeoRecord::SCALE).to_i, :longitude => (-0.141 * GeoRecord::SCALE).to_i)
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 > trkseg", :count => 2 do |trksegs|
186 trksegs.each do |trkseg|
187 assert_select trkseg, "trkpt", :count => 1 do |trkpt|
188 assert_select trkpt[0], "time", :count => 1
196 def test_tracepoints_identifiable
197 point = create(:trace, :visibility => "identifiable", :latitude => 51.512, :longitude => 0.142) do |trace|
198 create(:tracepoint, :trace => trace, :latitude => (51.512 * GeoRecord::SCALE).to_i, :longitude => (0.142 * GeoRecord::SCALE).to_i)
200 minlon = point.longitude - 0.002
201 minlat = point.latitude - 0.002
202 maxlon = point.longitude + 0.002
203 maxlat = point.latitude + 0.002
204 bbox = "#{minlon},#{minlat},#{maxlon},#{maxlat}"
205 get :trackpoints, :bbox => bbox
206 assert_response :success
207 assert_select "gpx[version='1.0'][creator='OpenStreetMap.org']", :count => 1 do
208 assert_select "trk", :count => 1 do
209 assert_select "trk>name", :count => 1
210 assert_select "trk>desc", :count => 1
211 assert_select "trk>url", :count => 1
212 assert_select "trkseg", :count => 1 do
213 assert_select "trkpt", :count => 1 do
214 assert_select "time", :count => 1
221 def test_map_without_bbox
222 %w(trackpoints map).each do |tq|
224 assert_response :bad_request
225 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"
229 def test_traces_page_less_than_0
231 get :trackpoints, :page => i, :bbox => "-0.1,-0.1,0.1,0.1"
232 assert_response :bad_request
233 assert_equal "Page number must be greater than or equal to 0", @response.body, "The page number was #{i}"
236 get :trackpoints, :page => i, :bbox => "-0.1,-0.1,0.1,0.1"
237 assert_response :success, "The page number was #{i} and should have been accepted"
241 def test_bbox_too_big
242 @badbigbbox.each do |bbox|
243 %w(trackpoints map).each do |tq|
244 get tq, :bbox => bbox
245 assert_response :bad_request, "The bbox:#{bbox} was expected to be too big"
246 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}"
251 def test_bbox_malformed
252 @badmalformedbbox.each do |bbox|
253 %w(trackpoints map).each do |tq|
254 get tq, :bbox => bbox
255 assert_response :bad_request, "The bbox:#{bbox} was expected to be malformed"
256 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}"
261 def test_bbox_lon_mixedup
262 @badlonmixedbbox.each do |bbox|
263 %w(trackpoints map).each do |tq|
264 get tq, :bbox => bbox
265 assert_response :bad_request, "The bbox:#{bbox} was expected to have the longitude mixed up"
266 assert_equal "The minimum longitude must be less than the maximum longitude, but it wasn't", @response.body, "bbox: #{bbox}"
271 def test_bbox_lat_mixedup
272 @badlatmixedbbox.each do |bbox|
273 %w(trackpoints map).each do |tq|
274 get tq, :bbox => bbox
275 assert_response :bad_request, "The bbox:#{bbox} was expected to have the latitude mixed up"
276 assert_equal "The minimum latitude must be less than the maximum latitude, but it wasn't", @response.body, "bbox: #{bbox}"
281 # We can't actually get an out of bounds error, as the bbox is sanitised.
282 # def test_latlon_outofbounds
283 # @badlatlonoutboundsbbox.each do |bbox|
284 # [ "trackpoints", "map" ].each do |tq|
285 # get tq, :bbox => bbox
286 # #print @request.to_yaml
287 # assert_response :bad_request, "The bbox #{bbox} was expected to be out of range"
288 # assert_equal "The latitudes must be between -90 an 90, and longitudes between -180 and 180", @response.body, "bbox: #{bbox}"
293 # MySQL and Postgres require that the C based functions are installed for
294 # this test to work. More information is available from:
295 # http://wiki.openstreetmap.org/wiki/Rails#Installing_the_quadtile_functions
296 # or by looking at the readme in db/README
297 def test_changes_simple
298 Timecop.freeze(Time.utc(2010, 4, 3, 10, 55, 0))
300 assert_response :success
301 # print @response.body
302 # As we have loaded the fixtures, we can assume that there are no
303 # changes at the time we have frozen at
304 now = Time.now.getutc
305 hourago = now - 1.hour
306 assert_select "osm[version='#{API_VERSION}'][generator='#{GENERATOR}']", :count => 1 do
307 assert_select "changes[starttime='#{hourago.xmlschema}'][endtime='#{now.xmlschema}']", :count => 1 do
308 assert_select "tile", :count => 0
313 Timecop.freeze(Time.utc(2007, 1, 1, 0, 30, 0))
315 assert_response :success
316 # print @response.body
317 # As we have loaded the fixtures, we can assume that there are some
318 # changes at the time we have frozen at
319 now = Time.now.getutc
320 hourago = now - 1.hour
321 assert_select "osm[version='#{API_VERSION}'][generator='#{GENERATOR}']", :count => 1 do
322 assert_select "changes[starttime='#{hourago.xmlschema}'][endtime='#{now.xmlschema}']", :count => 1 do
323 assert_select "tile", :count => 10
329 def test_changes_zoom_invalid
330 zoom_to_test = %w(p -1 0 17 one two)
331 zoom_to_test.each do |zoom|
332 get :changes, :zoom => zoom
333 assert_response :bad_request
334 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"
338 def test_changes_zoom_valid
340 get :changes, :zoom => zoom
341 assert_response :success
342 # NOTE: there was a test here for the timing, but it was too sensitive to be a good test
343 # and it was annoying.
344 assert_select "osm[version='#{API_VERSION}'][generator='#{GENERATOR}']", :count => 1 do
345 assert_select "changes", :count => 1
350 def test_changes_hours_invalid
351 invalid = %w(-21 335 -1 0 25 26 100 one two three ping pong :)
352 invalid.each do |hour|
353 get :changes, :hours => hour
354 assert_response :bad_request, "Problem with the hour: #{hour}"
355 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}."
359 def test_changes_hours_valid
361 get :changes, :hours => hour
362 assert_response :success
366 def test_changes_start_end_invalid
367 get :changes, :start => "2010-04-03 10:55:00", :end => "2010-04-03 09:55:00"
368 assert_response :bad_request
369 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"
372 def test_changes_start_end_valid
373 get :changes, :start => "2010-04-03 09:55:00", :end => "2010-04-03 10:55:00"
374 assert_response :success
377 def test_capabilities
379 assert_response :success
380 assert_select "osm[version='#{API_VERSION}'][generator='#{GENERATOR}']", :count => 1 do
381 assert_select "api", :count => 1 do
382 assert_select "version[minimum='#{API_VERSION}'][maximum='#{API_VERSION}']", :count => 1
383 assert_select "area[maximum='#{MAX_REQUEST_AREA}']", :count => 1
384 assert_select "note_area[maximum='#{MAX_NOTE_REQUEST_AREA}']", :count => 1
385 assert_select "tracepoints[per_page='#{TRACEPOINTS_PER_PAGE}']", :count => 1
386 assert_select "changesets[maximum_elements='#{Changeset::MAX_ELEMENTS}']", :count => 1
387 assert_select "status[database='online']", :count => 1
388 assert_select "status[api='online']", :count => 1
389 assert_select "status[gpx='online']", :count => 1
394 def test_permissions_anonymous
396 assert_response :success
397 assert_select "osm > permissions", :count => 1 do
398 assert_select "permission", :count => 0
402 def test_permissions_basic_auth
403 basic_authorization(create(:user).email, "test")
405 assert_response :success
406 assert_select "osm > permissions", :count => 1 do
407 assert_select "permission", :count => ClientApplication.all_permissions.size
408 ClientApplication.all_permissions.each do |p|
409 assert_select "permission[name='#{p}']", :count => 1
414 def test_permissions_oauth
415 @request.env["oauth.token"] = AccessToken.new do |token|
417 token.allow_read_prefs = true
418 token.allow_write_api = true
419 token.allow_read_gpx = false
422 assert_response :success
423 assert_select "osm > permissions", :count => 1 do
424 assert_select "permission", :count => 2
425 assert_select "permission[name='allow_read_prefs']", :count => 1
426 assert_select "permission[name='allow_write_api']", :count => 1
427 assert_select "permission[name='allow_read_gpx']", :count => 0