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 def basic_authorization(user, pass)
22 @request.env["HTTP_AUTHORIZATION"] = "Basic %s" % Base64.encode64("#{user}:#{pass}")
25 # -------------------------------------
26 # Test reading a bounding box.
27 # -------------------------------------
30 node = current_nodes(:used_node_1)
31 # Need to split the min/max lat/lon out into their own variables here
32 # so that we can test they are returned later.
37 bbox = "#{minlon},#{minlat},#{maxlon},#{maxlat}"
38 get :map, :bbox => bbox
40 print @request.to_yaml
43 assert_response :success
44 assert_select "osm[version='#{API_VERSION}'][generator='#{GENERATOR}']:root", :count => 1 do
45 assert_select "bounds[minlon=#{minlon}][minlat=#{minlat}][maxlon=#{maxlon}][maxlat=#{maxlat}]", :count => 1
46 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
47 # This should really be more generic
48 assert_select "tag[k=test][v=1]"
50 # Should also test for the ways and relation
55 node = gpx_files(:first_trace_file)
56 minlon = node.longitude-0.1
57 minlat = node.latitude-0.1
58 maxlon = node.longitude+0.1
59 maxlat = node.latitude+0.1
60 bbox = "#{minlon},#{minlat},#{maxlon},#{maxlat}"
61 get :trackpoints, :bbox => bbox
63 assert_response :success
64 assert_select "gpx[version=1.0][creator=OpenStreetMap.org][xmlns=http://www.topografix.com/GPX/1/0/]:root", :count => 1 do
65 assert_select "trk" do
66 assert_select "trkseg"
71 def test_map_without_bbox
72 ["trackpoints", "map"].each do |tq|
74 assert_response :bad_request
75 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"
79 def test_traces_page_less_than_0
81 get :trackpoints, :page => i, :bbox => "-0.1,-0.1,0.1,0.1"
82 assert_response :bad_request
83 assert_equal "Page number must be greater than or equal to 0", @response.body, "The page number was #{i}"
86 get :trackpoints, :page => i, :bbox => "-0.1,-0.1,0.1,0.1"
87 assert_response :success, "The page number was #{i} and should have been accepted"
92 @badbigbbox.each do |bbox|
93 [ "trackpoints", "map" ].each do |tq|
95 assert_response :bad_request, "The bbox:#{bbox} was expected to be too big"
96 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}"
101 def test_bbox_malformed
102 @badmalformedbbox.each do |bbox|
103 [ "trackpoints", "map" ].each do |tq|
104 get tq, :bbox => bbox
105 assert_response :bad_request, "The bbox:#{bbox} was expected to be malformed"
106 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}"
111 def test_bbox_lon_mixedup
112 @badlonmixedbbox.each do |bbox|
113 [ "trackpoints", "map" ].each do |tq|
114 get tq, :bbox => bbox
115 assert_response :bad_request, "The bbox:#{bbox} was expected to have the longitude mixed up"
116 assert_equal "The minimum longitude must be less than the maximum longitude, but it wasn't", @response.body, "bbox: #{bbox}"
121 def test_bbox_lat_mixedup
122 @badlatmixedbbox.each do |bbox|
123 ["trackpoints", "map"].each do |tq|
124 get tq, :bbox => bbox
125 assert_response :bad_request, "The bbox:#{bbox} was expected to have the latitude mixed up"
126 assert_equal "The minimum latitude must be less than the maximum latitude, but it wasn't", @response.body, "bbox: #{bbox}"
131 # We can't actually get an out of bounds error, as the bbox is sanitised.
132 #def test_latlon_outofbounds
133 # @badlatlonoutboundsbbox.each do |bbox|
134 # [ "trackpoints", "map" ].each do |tq|
135 # get tq, :bbox => bbox
136 # #print @request.to_yaml
137 # assert_response :bad_request, "The bbox #{bbox} was expected to be out of range"
138 # assert_equal "The latitudes must be between -90 an 90, and longitudes between -180 and 180", @response.body, "bbox: #{bbox}"
143 def test_capabilities
145 assert_response :success
146 assert_select "osm:root[version='#{API_VERSION}'][generator='#{GENERATOR}']", :count => 1 do
147 assert_select "api", :count => 1 do
148 assert_select "version[minimum=#{API_VERSION}][maximum=#{API_VERSION}]", :count => 1
149 assert_select "area[maximum=#{APP_CONFIG['max_request_area']}]", :count => 1
150 assert_select "tracepoints[per_page=#{APP_CONFIG['tracepoints_per_page']}]", :count => 1