2 require "api_controller"
4 class ApiControllerTest < ActionController::TestCase
7 @badbigbbox = %w[-0.1,-0.1,1.1,1.1 10,10,11,11]
8 @badmalformedbbox = %w[-0.1 hello
10 @badlatmixedbbox = %w[0,0.1,0.1,0 -0.1,80,0.1,70 0.24,54.34,0.25,54.33]
11 @badlonmixedbbox = %w[80,-0.1,70,0.1 54.34,0.24,54.33,0.25]
12 # @badlatlonoutboundsbbox = %w{ 191,-0.1,193,0.1 -190.1,89.9,-190,90 }
13 @goodbbox = %w[-0.1,-0.1,0.1,0.1 51.1,-0.1,51.2,0
14 -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]
15 # That last item in the goodbbox really shouldn't be there, as the API should
16 # reall reject it, however this is to test to see if the api changes.
20 # test all routes which lead to this controller
23 { :path => "/api/capabilities", :method => :get },
24 { :controller => "api", :action => "capabilities" }
27 { :controller => "api", :action => "capabilities" },
28 { :path => "/api/0.6/capabilities", :method => :get }
31 { :path => "/api/0.6/permissions", :method => :get },
32 { :controller => "api", :action => "permissions" }
35 { :path => "/api/0.6/map", :method => :get },
36 { :controller => "api", :action => "map" }
39 { :path => "/api/0.6/trackpoints", :method => :get },
40 { :controller => "api", :action => "trackpoints" }
43 { :path => "/api/0.6/changes", :method => :get },
44 { :controller => "api", :action => "changes" }
48 # -------------------------------------
49 # Test reading a bounding box.
50 # -------------------------------------
53 node = create(:node, :lat => 7, :lon => 7)
54 tag = create(:node_tag, :node => node)
55 way1 = create(:way_node, :node => node).way
56 way2 = create(:way_node, :node => node).way
57 relation = create(:relation_member, :member => node).relation
59 # Need to split the min/max lat/lon out into their own variables here
60 # so that we can test they are returned later.
61 minlon = node.lon - 0.1
62 minlat = node.lat - 0.1
63 maxlon = node.lon + 0.1
64 maxlat = node.lat + 0.1
65 bbox = "#{minlon},#{minlat},#{maxlon},#{maxlat}"
66 get :map, :params => { :bbox => bbox }
68 print @request.to_yaml
71 assert_response :success, "Expected scucess with the map call"
72 assert_select "osm[version='#{API_VERSION}'][generator='#{GENERATOR}']", :count => 1 do
73 assert_select "bounds[minlon='#{format('%.7f', minlon)}'][minlat='#{format('%.7f', minlat)}'][maxlon='#{format('%.7f', maxlon)}'][maxlat='#{format('%.7f', maxlat)}']", :count => 1
74 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
75 # This should really be more generic
76 assert_select "tag[k='#{tag.k}'][v='#{tag.v}']"
78 assert_select "way", :count => 2
79 assert_select "way[id='#{way1.id}']", :count => 1
80 assert_select "way[id='#{way2.id}']", :count => 1
81 assert_select "relation", :count => 1
82 assert_select "relation[id='#{relation.id}']", :count => 1
86 # This differs from the above test in that we are making the bbox exactly
87 # the same as the node we are looking at
88 def test_map_inclusive
89 node = create(:node, :lat => 7, :lon => 7)
90 tag = create(:node_tag, :node => node)
91 way1 = create(:way_node, :node => node).way
92 way2 = create(:way_node, :node => node).way
93 relation = create(:relation_member, :member => node).relation
95 bbox = "#{node.lon},#{node.lat},#{node.lon},#{node.lat}"
96 get :map, :params => { :bbox => bbox }
97 assert_response :success, "The map call should have succeeded"
98 assert_select "osm[version='#{API_VERSION}'][generator='#{GENERATOR}']", :count => 1 do
99 assert_select "bounds[minlon='#{node.lon}'][minlat='#{node.lat}'][maxlon='#{node.lon}'][maxlat='#{node.lat}']", :count => 1
100 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
101 # This should really be more generic
102 assert_select "tag[k='#{tag.k}'][v='#{tag.v}']"
104 assert_select "way", :count => 2
105 assert_select "way[id='#{way1.id}']", :count => 1
106 assert_select "way[id='#{way2.id}']", :count => 1
107 assert_select "relation", :count => 1
108 assert_select "relation[id='#{relation.id}']", :count => 1
112 def test_map_complete_way
113 node = create(:node, :lat => 7, :lon => 7)
114 # create a couple of nodes well outside of the bbox
115 node2 = create(:node, :lat => 45, :lon => 45)
116 node3 = create(:node, :lat => 10, :lon => 10)
117 way1 = create(:way_node, :node => node).way
118 create(:way_node, :way => way1, :node => node2, :sequence_id => 2)
119 way2 = create(:way_node, :node => node).way
120 create(:way_node, :way => way2, :node => node3, :sequence_id => 2)
121 relation = create(:relation_member, :member => way1).relation
123 bbox = "#{node.lon},#{node.lat},#{node.lon},#{node.lat}"
124 get :map, :params => { :bbox => bbox }
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='#{node.lon}'][minlat='#{node.lat}'][maxlon='#{node.lon}'][maxlat='#{node.lat}']", :count => 1
128 assert_select "node", :count => 3
129 assert_select "node[id='#{node.id}']", :count => 1
130 assert_select "node[id='#{node2.id}']", :count => 1
131 assert_select "node[id='#{node3.id}']", :count => 1
132 assert_select "way", :count => 2
133 assert_select "way[id='#{way1.id}']", :count => 1
134 assert_select "way[id='#{way2.id}']", :count => 1
135 assert_select "relation", :count => 1
136 assert_select "relation[id='#{relation.id}']", :count => 1
141 get :map, :params => { :bbox => "179.998,89.998,179.999.1,89.999" }
142 assert_response :success, "The map call should have succeeded"
143 assert_select "osm[version='#{API_VERSION}'][generator='#{GENERATOR}']", :count => 1 do
144 assert_select "bounds[minlon='179.9980000'][minlat='89.9980000'][maxlon='179.9990000'][maxlat='89.9990000']", :count => 1
145 assert_select "node", :count => 0
146 assert_select "way", :count => 0
147 assert_select "relation", :count => 0
152 point = create(:trace, :visibility => "public", :latitude => 1, :longitude => 1) do |trace|
153 create(:tracepoint, :trace => trace, :latitude => 1 * GeoRecord::SCALE, :longitude => 1 * GeoRecord::SCALE)
155 minlon = point.longitude - 0.001
156 minlat = point.latitude - 0.001
157 maxlon = point.longitude + 0.001
158 maxlat = point.latitude + 0.001
159 bbox = "#{minlon},#{minlat},#{maxlon},#{maxlat}"
160 get :trackpoints, :params => { :bbox => bbox }
161 assert_response :success
162 assert_select "gpx[version='1.0'][creator='OpenStreetMap.org']", :count => 1 do
163 assert_select "trk" do
164 assert_select "trkseg"
169 def test_tracepoints_trackable
170 point = create(:trace, :visibility => "trackable", :latitude => 51.51, :longitude => -0.14) do |trace|
171 create(:tracepoint, :trace => trace, :trackid => 1, :latitude => (51.510 * GeoRecord::SCALE).to_i, :longitude => (-0.140 * GeoRecord::SCALE).to_i)
172 create(:tracepoint, :trace => trace, :trackid => 2, :latitude => (51.511 * GeoRecord::SCALE).to_i, :longitude => (-0.141 * GeoRecord::SCALE).to_i)
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, :params => { :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 > trkseg", :count => 2 do |trksegs|
184 trksegs.each do |trkseg|
185 assert_select trkseg, "trkpt", :count => 1 do |trkpt|
186 assert_select trkpt[0], "time", :count => 1
194 def test_tracepoints_identifiable
195 point = create(:trace, :visibility => "identifiable", :latitude => 51.512, :longitude => 0.142) do |trace|
196 create(:tracepoint, :trace => trace, :latitude => (51.512 * GeoRecord::SCALE).to_i, :longitude => (0.142 * GeoRecord::SCALE).to_i)
198 minlon = point.longitude - 0.002
199 minlat = point.latitude - 0.002
200 maxlon = point.longitude + 0.002
201 maxlat = point.latitude + 0.002
202 bbox = "#{minlon},#{minlat},#{maxlon},#{maxlat}"
203 get :trackpoints, :params => { :bbox => bbox }
204 assert_response :success
205 assert_select "gpx[version='1.0'][creator='OpenStreetMap.org']", :count => 1 do
206 assert_select "trk", :count => 1 do
207 assert_select "trk>name", :count => 1
208 assert_select "trk>desc", :count => 1
209 assert_select "trk>url", :count => 1
210 assert_select "trkseg", :count => 1 do
211 assert_select "trkpt", :count => 1 do
212 assert_select "time", :count => 1
219 def test_map_without_bbox
220 %w[trackpoints map].each do |tq|
222 assert_response :bad_request
223 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"
227 def test_traces_page_less_than_0
229 get :trackpoints, :params => { :page => i, :bbox => "-0.1,-0.1,0.1,0.1" }
230 assert_response :bad_request
231 assert_equal "Page number must be greater than or equal to 0", @response.body, "The page number was #{i}"
234 get :trackpoints, :params => { :page => i, :bbox => "-0.1,-0.1,0.1,0.1" }
235 assert_response :success, "The page number was #{i} and should have been accepted"
239 def test_bbox_too_big
240 @badbigbbox.each do |bbox|
241 %w[trackpoints map].each do |tq|
242 get tq, :params => { :bbox => bbox }
243 assert_response :bad_request, "The bbox:#{bbox} was expected to be too big"
244 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}"
249 def test_bbox_malformed
250 @badmalformedbbox.each do |bbox|
251 %w[trackpoints map].each do |tq|
252 get tq, :params => { :bbox => bbox }
253 assert_response :bad_request, "The bbox:#{bbox} was expected to be malformed"
254 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}"
259 def test_bbox_lon_mixedup
260 @badlonmixedbbox.each do |bbox|
261 %w[trackpoints map].each do |tq|
262 get tq, :params => { :bbox => bbox }
263 assert_response :bad_request, "The bbox:#{bbox} was expected to have the longitude mixed up"
264 assert_equal "The minimum longitude must be less than the maximum longitude, but it wasn't", @response.body, "bbox: #{bbox}"
269 def test_bbox_lat_mixedup
270 @badlatmixedbbox.each do |bbox|
271 %w[trackpoints map].each do |tq|
272 get tq, :params => { :bbox => bbox }
273 assert_response :bad_request, "The bbox:#{bbox} was expected to have the latitude mixed up"
274 assert_equal "The minimum latitude must be less than the maximum latitude, but it wasn't", @response.body, "bbox: #{bbox}"
279 # We can't actually get an out of bounds error, as the bbox is sanitised.
280 # def test_latlon_outofbounds
281 # @badlatlonoutboundsbbox.each do |bbox|
282 # [ "trackpoints", "map" ].each do |tq|
283 # get tq, :bbox => bbox
284 # #print @request.to_yaml
285 # assert_response :bad_request, "The bbox #{bbox} was expected to be out of range"
286 # assert_equal "The latitudes must be between -90 an 90, and longitudes between -180 and 180", @response.body, "bbox: #{bbox}"
291 # MySQL and Postgres require that the C based functions are installed for
292 # this test to work. More information is available from:
293 # http://wiki.openstreetmap.org/wiki/Rails#Installing_the_quadtile_functions
294 # or by looking at the readme in db/README
295 def test_changes_simple
296 # create a selection of nodes
298 create(:node, :timestamp => Time.utc(2007, 1, 1, 0, 0, 0), :lat => n, :lon => n)
300 # deleted nodes should also be counted
301 create(:node, :deleted, :timestamp => Time.utc(2007, 1, 1, 0, 0, 0), :lat => 6, :lon => 6)
302 # nodes in the same tile won't change the total
303 create(:node, :timestamp => Time.utc(2007, 1, 1, 0, 0, 0), :lat => 6, :lon => 6)
304 # nodes with a different timestamp should be ignored
305 create(:node, :timestamp => Time.utc(2008, 1, 1, 0, 0, 0), :lat => 7, :lon => 7)
307 travel_to Time.utc(2010, 4, 3, 10, 55, 0) do
309 assert_response :success
310 now = Time.now.getutc
311 hourago = now - 1.hour
312 assert_select "osm[version='#{API_VERSION}'][generator='#{GENERATOR}']", :count => 1 do
313 assert_select "changes[starttime='#{hourago.xmlschema}'][endtime='#{now.xmlschema}']", :count => 1 do
314 assert_select "tile", :count => 0
319 travel_to Time.utc(2007, 1, 1, 0, 30, 0) do
321 assert_response :success
322 # print @response.body
323 # As we have loaded the fixtures, we can assume that there are some
324 # changes at the time we have frozen at
325 now = Time.now.getutc
326 hourago = now - 1.hour
327 assert_select "osm[version='#{API_VERSION}'][generator='#{GENERATOR}']", :count => 1 do
328 assert_select "changes[starttime='#{hourago.xmlschema}'][endtime='#{now.xmlschema}']", :count => 1 do
329 assert_select "tile", :count => 6
335 def test_changes_zoom_invalid
336 zoom_to_test = %w[p -1 0 17 one two]
337 zoom_to_test.each do |zoom|
338 get :changes, :params => { :zoom => zoom }
339 assert_response :bad_request
340 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"
344 def test_changes_zoom_valid
346 get :changes, :params => { :zoom => zoom }
347 assert_response :success
348 # NOTE: there was a test here for the timing, but it was too sensitive to be a good test
349 # and it was annoying.
350 assert_select "osm[version='#{API_VERSION}'][generator='#{GENERATOR}']", :count => 1 do
351 assert_select "changes", :count => 1
356 def test_changes_hours_invalid
357 invalid = %w[-21 335 -1 0 25 26 100 one two three ping pong :]
358 invalid.each do |hour|
359 get :changes, :params => { :hours => hour }
360 assert_response :bad_request, "Problem with the hour: #{hour}"
361 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}."
365 def test_changes_hours_valid
367 get :changes, :params => { :hours => hour }
368 assert_response :success
372 def test_changes_start_end_invalid
373 get :changes, :params => { :start => "2010-04-03 10:55:00", :end => "2010-04-03 09:55:00" }
374 assert_response :bad_request
375 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"
378 def test_changes_start_end_valid
379 get :changes, :params => { :start => "2010-04-03 09:55:00", :end => "2010-04-03 10:55:00" }
380 assert_response :success
383 def test_capabilities
385 assert_response :success
386 assert_select "osm[version='#{API_VERSION}'][generator='#{GENERATOR}']", :count => 1 do
387 assert_select "api", :count => 1 do
388 assert_select "version[minimum='#{API_VERSION}'][maximum='#{API_VERSION}']", :count => 1
389 assert_select "area[maximum='#{MAX_REQUEST_AREA}']", :count => 1
390 assert_select "note_area[maximum='#{MAX_NOTE_REQUEST_AREA}']", :count => 1
391 assert_select "tracepoints[per_page='#{TRACEPOINTS_PER_PAGE}']", :count => 1
392 assert_select "changesets[maximum_elements='#{Changeset::MAX_ELEMENTS}']", :count => 1
393 assert_select "status[database='online']", :count => 1
394 assert_select "status[api='online']", :count => 1
395 assert_select "status[gpx='online']", :count => 1
400 def test_permissions_anonymous
402 assert_response :success
403 assert_select "osm > permissions", :count => 1 do
404 assert_select "permission", :count => 0
408 def test_permissions_basic_auth
409 basic_authorization(create(:user).email, "test")
411 assert_response :success
412 assert_select "osm > permissions", :count => 1 do
413 assert_select "permission", :count => ClientApplication.all_permissions.size
414 ClientApplication.all_permissions.each do |p|
415 assert_select "permission[name='#{p}']", :count => 1
420 def test_permissions_oauth
421 @request.env["oauth.token"] = AccessToken.new do |token|
423 token.allow_read_prefs = true
424 token.allow_write_api = true
425 token.allow_read_gpx = false
428 assert_response :success
429 assert_select "osm > permissions", :count => 1 do
430 assert_select "permission", :count => 2
431 assert_select "permission[name='allow_read_prefs']", :count => 1
432 assert_select "permission[name='allow_write_api']", :count => 1
433 assert_select "permission[name='allow_read_gpx']", :count => 0