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.
21 # -------------------------------------
22 # Test reading a bounding box.
23 # -------------------------------------
26 node = current_nodes(:used_node_1)
27 # Need to split the min/max lat/lon out into their own variables here
28 # so that we can test they are returned later.
33 bbox = "#{minlon},#{minlat},#{maxlon},#{maxlat}"
34 get :map, :bbox => bbox
36 print @request.to_yaml
39 assert_response :success, "Expected scucess with the map call"
40 assert_select "osm[version='#{API_VERSION}'][generator='#{GENERATOR}']:root", :count => 1 do
41 assert_select "bounds[minlon=#{minlon}][minlat=#{minlat}][maxlon=#{maxlon}][maxlat=#{maxlat}]", :count => 1
42 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
43 # This should really be more generic
44 assert_select "tag[k='test'][v='yes']"
46 # Should also test for the ways and relation
50 # This differs from the above test in that we are making the bbox exactly
51 # the same as the node we are looking at
52 def test_map_inclusive
53 node = current_nodes(:used_node_1)
54 bbox = "#{node.lon},#{node.lat},#{node.lon},#{node.lat}"
55 get :map, :bbox => bbox
57 assert_response :success, "The map call should have succeeded"
58 assert_select "osm[version='#{API_VERSION}'][generator='#{GENERATOR}']:root:empty", :count => 1
62 point = gpx_files(:public_trace_file)
63 minlon = point.longitude-0.1
64 minlat = point.latitude-0.1
65 maxlon = point.longitude+0.1
66 maxlat = point.latitude+0.1
67 bbox = "#{minlon},#{minlat},#{maxlon},#{maxlat}"
68 get :trackpoints, :bbox => bbox
70 assert_response :success
71 assert_select "gpx[version=1.0][creator=OpenStreetMap.org][xmlns=http://www.topografix.com/GPX/1/0/]:root", :count => 1 do
72 assert_select "trk" do
73 assert_select "trkseg"
78 def test_map_without_bbox
79 ["trackpoints", "map"].each do |tq|
81 assert_response :bad_request
82 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"
86 def test_traces_page_less_than_0
88 get :trackpoints, :page => i, :bbox => "-0.1,-0.1,0.1,0.1"
89 assert_response :bad_request
90 assert_equal "Page number must be greater than or equal to 0", @response.body, "The page number was #{i}"
93 get :trackpoints, :page => i, :bbox => "-0.1,-0.1,0.1,0.1"
94 assert_response :success, "The page number was #{i} and should have been accepted"
99 @badbigbbox.each do |bbox|
100 [ "trackpoints", "map" ].each do |tq|
101 get tq, :bbox => bbox
102 assert_response :bad_request, "The bbox:#{bbox} was expected to be too big"
103 assert_equal "The maximum bbox size is #{APP_CONFIG['max_request_area']}, and your request was too large. Either request a smaller area, or use planet.osm", @response.body, "bbox: #{bbox}"
108 def test_bbox_malformed
109 @badmalformedbbox.each do |bbox|
110 [ "trackpoints", "map" ].each do |tq|
111 get tq, :bbox => bbox
112 assert_response :bad_request, "The bbox:#{bbox} was expected to be malformed"
113 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}"
118 def test_bbox_lon_mixedup
119 @badlonmixedbbox.each do |bbox|
120 [ "trackpoints", "map" ].each do |tq|
121 get tq, :bbox => bbox
122 assert_response :bad_request, "The bbox:#{bbox} was expected to have the longitude mixed up"
123 assert_equal "The minimum longitude must be less than the maximum longitude, but it wasn't", @response.body, "bbox: #{bbox}"
128 def test_bbox_lat_mixedup
129 @badlatmixedbbox.each do |bbox|
130 ["trackpoints", "map"].each do |tq|
131 get tq, :bbox => bbox
132 assert_response :bad_request, "The bbox:#{bbox} was expected to have the latitude mixed up"
133 assert_equal "The minimum latitude must be less than the maximum latitude, but it wasn't", @response.body, "bbox: #{bbox}"
138 # We can't actually get an out of bounds error, as the bbox is sanitised.
139 #def test_latlon_outofbounds
140 # @badlatlonoutboundsbbox.each do |bbox|
141 # [ "trackpoints", "map" ].each do |tq|
142 # get tq, :bbox => bbox
143 # #print @request.to_yaml
144 # assert_response :bad_request, "The bbox #{bbox} was expected to be out of range"
145 # assert_equal "The latitudes must be between -90 an 90, and longitudes between -180 and 180", @response.body, "bbox: #{bbox}"
150 # MySQL and Postgres require that the C based functions are installed for
151 # this test to work. More information is available from:
152 # http://wiki.openstreetmap.org/index.php/Rails#Installing_the_quadtile_functions
153 # or by looking at the readme in db/README
154 def test_changes_simple
156 assert_response :success
157 #print @response.body
158 # As we have loaded the fixtures, we can assume that there are no
160 now = Time.now.getutc
161 hourago = now - 1.hour
162 # Note that this may fail on a very slow machine, so isn't a great test
163 assert_select "osm[version='#{API_VERSION}'][generator='#{GENERATOR}']:root", :count => 1 do
164 assert_select "changes[starttime='#{hourago.xmlschema}'][endtime='#{now.xmlschema}']", :count => 1
168 def test_changes_zoom_invalid
169 zoom_to_test = %w{ p -1 0 17 one two }
170 zoom_to_test.each do |zoom|
171 get :changes, :zoom => zoom
172 assert_response :bad_request
173 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"
177 def test_changes_zoom_valid
179 get :changes, :zoom => zoom
180 assert_response :success
181 now = Time.now.getutc
182 hourago = now - 1.hour
183 # Note that this may fail on a very slow machine, so isn't a great test
184 assert_select "osm[version='#{API_VERSION}'][generator='#{GENERATOR}']:root", :count => 1 do
185 assert_select "changes[starttime='#{hourago.xmlschema}'][endtime='#{now.xmlschema}']", :count => 1
190 def test_start_end_time_invalid
194 def test_start_end_time_invalid
198 def test_hours_invalid
199 invalid = %w{ -21 335 -1 0 25 26 100 one two three ping pong : }
200 invalid.each do |hour|
201 get :changes, :hours => hour
202 assert_response :bad_request, "Problem with the hour: #{hour}"
203 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}."
209 get :changes, :hours => hour
210 assert_response :success
214 def test_capabilities
216 assert_response :success
217 assert_select "osm:root[version='#{API_VERSION}'][generator='#{GENERATOR}']", :count => 1 do
218 assert_select "api", :count => 1 do
219 assert_select "version[minimum=#{API_VERSION}][maximum=#{API_VERSION}]", :count => 1
220 assert_select "area[maximum=#{APP_CONFIG['max_request_area']}]", :count => 1
221 assert_select "tracepoints[per_page=#{APP_CONFIG['tracepoints_per_page']}]", :count => 1
222 assert_select "changesets[maximum_elements=#{Changeset::MAX_ELEMENTS}]", :count => 1