4 class MapControllerTest < 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/0.6/map", :method => :get },
24 { :controller => "api/map", :action => "index" }
27 { :path => "/api/0.6/map.json", :method => :get },
28 { :controller => "api/map", :action => "index", :format => "json" }
33 # test http accept headers
34 def test_http_accept_header
37 minlon = node.lon - 0.1
38 minlat = node.lat - 0.1
39 maxlon = node.lon + 0.1
40 maxlat = node.lat + 0.1
41 bbox = "#{minlon},#{minlat},#{maxlon},#{maxlat}"
43 # Accept: XML format -> use XML
44 http_accept_format("text/xml")
45 get :index, :params => { :bbox => bbox }
46 assert_response :success, "Expected success with the map call"
47 assert_equal "application/xml; charset=utf-8", @response.header["Content-Type"]
49 # Accept: Any format -> use XML
50 http_accept_format("*/*")
51 get :index, :params => { :bbox => bbox }
52 assert_response :success, "Expected success with the map call"
53 assert_equal "application/xml; charset=utf-8", @response.header["Content-Type"]
55 # Accept: Any format, .json URL suffix -> use json
56 http_accept_format("*/*")
57 get :index, :params => { :bbox => bbox, :format => "json" }
58 assert_response :success, "Expected success with the map call"
59 assert_equal "application/json; charset=utf-8", @response.header["Content-Type"]
61 # Accept: Firefox header -> use XML
62 http_accept_format("text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,*/*;q=0.8")
63 get :index, :params => { :bbox => bbox }
64 assert_response :success, "Expected success with the map call"
65 assert_equal "application/xml; charset=utf-8", @response.header["Content-Type"]
67 # Accept: JOSM header text/html, image/gif, image/jpeg, *; q=.2, */*; q=.2 -> use XML
68 # Note: JOSM's header does not comply with RFC 7231, section 5.3.1
69 http_accept_format("text/html, image/gif, image/jpeg, *; q=.2, */*; q=.2")
70 get :index, :params => { :bbox => bbox }
71 assert_response :success, "Expected success with the map call"
72 assert_equal "application/xml; charset=utf-8", @response.header["Content-Type"]
74 # Accept: text/plain, */* -> use XML
75 http_accept_format("text/plain, */*")
76 get :index, :params => { :bbox => bbox }
77 assert_response :success, "Expected success with the map call"
78 assert_equal "application/xml; charset=utf-8", @response.header["Content-Type"]
80 # Accept: text/* -> use XML
81 http_accept_format("text/*")
82 get :index, :params => { :bbox => bbox }
83 assert_response :success, "Expected success with the map call"
84 assert_equal "application/xml; charset=utf-8", @response.header["Content-Type"]
86 # Accept: json, */* format -> use json
87 http_accept_format("application/json, */*")
88 get :index, :params => { :bbox => bbox }
89 assert_response :success, "Expected success with the map call"
90 assert_equal "application/json; charset=utf-8", @response.header["Content-Type"]
92 # Accept: json format -> use json
93 http_accept_format("application/json")
94 get :index, :params => { :bbox => bbox }
95 assert_response :success, "Expected success with the map call"
96 assert_equal "application/json; charset=utf-8", @response.header["Content-Type"]
98 # text/json is in invalid format, ActionController::UnknownFormat error is expected
99 http_accept_format("text/json")
100 get :index, :params => { :bbox => bbox }
101 assert_response :internal_server_error, "text/json should fail"
103 # image/jpeg is a format which we don't support, ActionController::UnknownFormat error is expected
104 # HTTP 406 Not acceptable would be the correct response error code. That's outside of our control though.
105 http_accept_format("image/jpeg")
106 get :index, :params => { :bbox => bbox }
107 assert_response :internal_server_error, "text/json should fail"
110 # -------------------------------------
111 # Test reading a bounding box.
112 # -------------------------------------
115 node = create(:node, :lat => 7, :lon => 7)
116 tag = create(:node_tag, :node => node)
117 way1 = create(:way_node, :node => node).way
118 way2 = create(:way_node, :node => node).way
119 relation = create(:relation_member, :member => node).relation
121 # Need to split the min/max lat/lon out into their own variables here
122 # so that we can test they are returned later.
123 minlon = node.lon - 0.1
124 minlat = node.lat - 0.1
125 maxlon = node.lon + 0.1
126 maxlat = node.lat + 0.1
127 bbox = "#{minlon},#{minlat},#{maxlon},#{maxlat}"
128 get :index, :params => { :bbox => bbox }
130 print @request.to_yaml
133 assert_response :success, "Expected scucess with the map call"
134 assert_select "osm[version='#{Settings.api_version}'][generator='#{Settings.generator}']", :count => 1 do
135 assert_select "bounds[minlon='#{format('%.7f', minlon)}'][minlat='#{format('%.7f', minlat)}'][maxlon='#{format('%.7f', maxlon)}'][maxlat='#{format('%.7f', maxlat)}']", :count => 1
136 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
137 # This should really be more generic
138 assert_select "tag[k='#{tag.k}'][v='#{tag.v}']"
140 assert_select "way", :count => 2
141 assert_select "way[id='#{way1.id}']", :count => 1
142 assert_select "way[id='#{way2.id}']", :count => 1
143 assert_select "relation", :count => 1
144 assert_select "relation[id='#{relation.id}']", :count => 1
149 node = create(:node, :lat => 7, :lon => 7)
150 tag = create(:node_tag, :node => node)
151 way1 = create(:way_node, :node => node).way
152 way2 = create(:way_node, :node => node).way
153 relation = create(:relation_member, :member => node).relation
155 # Need to split the min/max lat/lon out into their own variables here
156 # so that we can test they are returned later.
157 minlon = node.lon - 0.1
158 minlat = node.lat - 0.1
159 maxlon = node.lon + 0.1
160 maxlat = node.lat + 0.1
161 bbox = "#{minlon},#{minlat},#{maxlon},#{maxlat}"
162 get :index, :params => { :bbox => bbox, :format => "json" }
164 print @request.to_yaml
167 assert_response :success, "Expected success with the map call"
168 js = ActiveSupport::JSON.decode(@response.body)
171 assert_equal Settings.api_version, js["version"]
172 assert_equal Settings.generator, js["generator"]
173 assert_equal GeoRecord::Coord.new(minlon), js["bounds"]["minlon"]
174 assert_equal GeoRecord::Coord.new(minlat), js["bounds"]["minlat"]
175 assert_equal GeoRecord::Coord.new(maxlon), js["bounds"]["maxlon"]
176 assert_equal GeoRecord::Coord.new(maxlat), js["bounds"]["maxlat"]
178 result_nodes = js["elements"].select { |a| a["type"] == "node" }
179 .select { |a| a["id"] == node.id }
180 .select { |a| a["lat"] == GeoRecord::Coord.new(node.lat) }
181 .select { |a| a["lon"] == GeoRecord::Coord.new(node.lon) }
182 .select { |a| a["version"] == node.version }
183 .select { |a| a["changeset"] == node.changeset_id }
184 .select { |a| a["timestamp"] == node.timestamp.xmlschema }
185 assert_equal result_nodes.count, 1
186 result_node = result_nodes.first
188 assert_equal result_node["tags"], tag.k => tag.v
189 assert_equal 2, (js["elements"].count { |a| a["type"] == "way" })
190 assert_equal 1, (js["elements"].count { |a| a["type"] == "way" && a["id"] == way1.id })
191 assert_equal 1, (js["elements"].count { |a| a["type"] == "way" && a["id"] == way2.id })
192 assert_equal 1, (js["elements"].count { |a| a["type"] == "relation" })
193 assert_equal 1, (js["elements"].count { |a| a["type"] == "relation" && a["id"] == relation.id })
196 # This differs from the above test in that we are making the bbox exactly
197 # the same as the node we are looking at
198 def test_map_inclusive
199 node = create(:node, :lat => 7, :lon => 7)
200 tag = create(:node_tag, :node => node)
201 way1 = create(:way_node, :node => node).way
202 way2 = create(:way_node, :node => node).way
203 relation = create(:relation_member, :member => node).relation
205 bbox = "#{node.lon},#{node.lat},#{node.lon},#{node.lat}"
206 get :index, :params => { :bbox => bbox }
207 assert_response :success, "The map call should have succeeded"
208 assert_select "osm[version='#{Settings.api_version}'][generator='#{Settings.generator}']", :count => 1 do
209 assert_select "bounds[minlon='#{node.lon}'][minlat='#{node.lat}'][maxlon='#{node.lon}'][maxlat='#{node.lat}']", :count => 1
210 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
211 # This should really be more generic
212 assert_select "tag[k='#{tag.k}'][v='#{tag.v}']"
214 assert_select "way", :count => 2
215 assert_select "way[id='#{way1.id}']", :count => 1
216 assert_select "way[id='#{way2.id}']", :count => 1
217 assert_select "relation", :count => 1
218 assert_select "relation[id='#{relation.id}']", :count => 1
222 def test_map_complete_way
223 node = create(:node, :lat => 7, :lon => 7)
224 # create a couple of nodes well outside of the bbox
225 node2 = create(:node, :lat => 45, :lon => 45)
226 node3 = create(:node, :lat => 10, :lon => 10)
227 way1 = create(:way_node, :node => node).way
228 create(:way_node, :way => way1, :node => node2, :sequence_id => 2)
229 way2 = create(:way_node, :node => node).way
230 create(:way_node, :way => way2, :node => node3, :sequence_id => 2)
231 relation = create(:relation_member, :member => way1).relation
233 bbox = "#{node.lon},#{node.lat},#{node.lon},#{node.lat}"
234 get :index, :params => { :bbox => bbox }
235 assert_response :success, "The map call should have succeeded"
236 assert_select "osm[version='#{Settings.api_version}'][generator='#{Settings.generator}']", :count => 1 do
237 assert_select "bounds[minlon='#{node.lon}'][minlat='#{node.lat}'][maxlon='#{node.lon}'][maxlat='#{node.lat}']", :count => 1
238 assert_select "node", :count => 3
239 assert_select "node[id='#{node.id}']", :count => 1
240 assert_select "node[id='#{node2.id}']", :count => 1
241 assert_select "node[id='#{node3.id}']", :count => 1
242 assert_select "way", :count => 2
243 assert_select "way[id='#{way1.id}']", :count => 1
244 assert_select "way[id='#{way2.id}']", :count => 1
245 assert_select "relation", :count => 1
246 assert_select "relation[id='#{relation.id}']", :count => 1
251 get :index, :params => { :bbox => "179.998,89.998,179.999.1,89.999" }
252 assert_response :success, "The map call should have succeeded"
253 assert_select "osm[version='#{Settings.api_version}'][generator='#{Settings.generator}']", :count => 1 do
254 assert_select "bounds[minlon='179.9980000'][minlat='89.9980000'][maxlon='179.9990000'][maxlat='89.9990000']", :count => 1
255 assert_select "node", :count => 0
256 assert_select "way", :count => 0
257 assert_select "relation", :count => 0
261 def test_map_without_bbox
263 assert_response :bad_request
264 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"
267 def test_bbox_too_big
268 @badbigbbox.each do |bbox|
269 get :index, :params => { :bbox => bbox }
270 assert_response :bad_request, "The bbox:#{bbox} was expected to be too big"
271 assert_equal "The maximum bbox size is #{Settings.max_request_area}, and your request was too large. Either request a smaller area, or use planet.osm", @response.body, "bbox: #{bbox}"
275 def test_bbox_malformed
276 @badmalformedbbox.each do |bbox|
277 get :index, :params => { :bbox => bbox }
278 assert_response :bad_request, "The bbox:#{bbox} was expected to be malformed"
279 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}"
283 def test_bbox_lon_mixedup
284 @badlonmixedbbox.each do |bbox|
285 get :index, :params => { :bbox => bbox }
286 assert_response :bad_request, "The bbox:#{bbox} was expected to have the longitude mixed up"
287 assert_equal "The minimum longitude must be less than the maximum longitude, but it wasn't", @response.body, "bbox: #{bbox}"
291 def test_bbox_lat_mixedup
292 @badlatmixedbbox.each do |bbox|
293 get :index, :params => { :bbox => bbox }
294 assert_response :bad_request, "The bbox:#{bbox} was expected to have the latitude mixed up"
295 assert_equal "The minimum latitude must be less than the maximum latitude, but it wasn't", @response.body, "bbox: #{bbox}"
299 # We can't actually get an out of bounds error, as the bbox is sanitised.
300 # def test_latlon_outofbounds
301 # @badlatlonoutboundsbbox.each do |bbox|
302 # [ "trackpoints", "map" ].each do |tq|
303 # get tq, :bbox => bbox
304 # #print @request.to_yaml
305 # assert_response :bad_request, "The bbox #{bbox} was expected to be out of range"
306 # assert_equal "The latitudes must be between -90 an 90, and longitudes between -180 and 180", @response.body, "bbox: #{bbox}"