1 # frozen_string_literal: true
6 class MapsControllerTest < ActionDispatch::IntegrationTest
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 # really 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/0.6/map", :method => :get },
26 { :controller => "api/maps", :action => "show" }
29 { :path => "/api/0.6/map.json", :method => :get },
30 { :controller => "api/maps", :action => "show", :format => "json" }
35 # test http accept headers
36 def test_http_accept_header
39 minlon = node.lon - 0.1
40 minlat = node.lat - 0.1
41 maxlon = node.lon + 0.1
42 maxlat = node.lat + 0.1
43 bbox = "#{minlon},#{minlat},#{maxlon},#{maxlat}"
45 # Accept: XML format -> use XML
46 accept_header = accept_format_header("text/xml")
47 get api_map_path(:bbox => bbox), :headers => accept_header
48 assert_response :success, "Expected success with the map call"
49 assert_equal "application/xml; charset=utf-8", @response.header["Content-Type"]
51 # Accept: Any format -> use XML
52 accept_header = accept_format_header("*/*")
53 get api_map_path(:bbox => bbox), :headers => accept_header
54 assert_response :success, "Expected success with the map call"
55 assert_equal "application/xml; charset=utf-8", @response.header["Content-Type"]
57 # Accept: Any format, .json URL suffix -> use json
58 accept_header = accept_format_header("*/*")
59 get api_map_path(:bbox => bbox, :format => "json"), :headers => accept_header
60 assert_response :success, "Expected success with the map call"
61 assert_equal "application/json; charset=utf-8", @response.header["Content-Type"]
63 # Accept: Firefox header -> use XML
64 accept_header = accept_format_header("text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,*/*;q=0.8")
65 get api_map_path(:bbox => bbox), :headers => accept_header
66 assert_response :success, "Expected success with the map call"
67 assert_equal "application/xml; charset=utf-8", @response.header["Content-Type"]
69 # Accept: JOSM header text/html, image/gif, image/jpeg, *; q=.2, */*; q=.2 -> use XML
70 # Note: JOSM's header does not comply with RFC 7231, section 5.3.1
71 accept_header = accept_format_header("text/html, image/gif, image/jpeg, *; q=.2, */*; q=.2")
72 get api_map_path(:bbox => bbox), :headers => accept_header
73 assert_response :success, "Expected success with the map call"
74 assert_equal "application/xml; charset=utf-8", @response.header["Content-Type"]
76 # Accept: text/plain, */* -> use XML
77 accept_header = accept_format_header("text/plain, */*")
78 get api_map_path(:bbox => bbox), :headers => accept_header
79 assert_response :success, "Expected success with the map call"
80 assert_equal "application/xml; charset=utf-8", @response.header["Content-Type"]
82 # Accept: text/* -> use XML
83 accept_header = accept_format_header("text/*")
84 get api_map_path(:bbox => bbox), :headers => accept_header
85 assert_response :success, "Expected success with the map call"
86 assert_equal "application/xml; charset=utf-8", @response.header["Content-Type"]
88 # Accept: json, */* format -> use json
89 accept_header = accept_format_header("application/json, */*")
90 get api_map_path(:bbox => bbox), :headers => accept_header
91 assert_response :success, "Expected success with the map call"
92 assert_equal "application/json; charset=utf-8", @response.header["Content-Type"]
94 # Accept: json format -> use json
95 accept_header = accept_format_header("application/json")
96 get api_map_path(:bbox => bbox), :headers => accept_header
97 assert_response :success, "Expected success with the map call"
98 assert_equal "application/json; charset=utf-8", @response.header["Content-Type"]
100 # text/json is in invalid format, return HTTP 406 Not acceptable
101 accept_header = accept_format_header("text/json")
102 get api_map_path(:bbox => bbox), :headers => accept_header
103 assert_response :not_acceptable, "text/json should fail"
105 # image/jpeg is a format which we don't support, return HTTP 406 Not acceptable
106 accept_header = accept_format_header("image/jpeg")
107 get api_map_path(:bbox => bbox), :headers => accept_header
108 assert_response :not_acceptable, "text/json should fail"
111 # -------------------------------------
112 # Test reading a bounding box.
113 # -------------------------------------
116 node = create(:node, :lat => 7, :lon => 7)
117 tag = create(:node_tag, :node => node)
118 way1 = create(:way_node, :node => node).way
119 way2 = create(:way_node, :node => node).way
120 relation = create(:relation_member, :member => node).relation
122 # Need to split the min/max lat/lon out into their own variables here
123 # so that we can test they are returned later.
124 minlon = node.lon - 0.1
125 minlat = node.lat - 0.1
126 maxlon = node.lon + 0.1
127 maxlat = node.lat + 0.1
128 bbox = "#{minlon},#{minlat},#{maxlon},#{maxlat}"
129 get api_map_path(:bbox => bbox)
130 assert_response :success, "Expected success with the map call"
131 assert_select "osm[version='#{Settings.api_version}'][generator='#{Settings.generator}']", :count => 1 do
132 assert_select "bounds[minlon='#{format('%<lon>.7f', :lon => minlon)}']" \
133 "[minlat='#{format('%<lat>.7f', :lat => minlat)}']" \
134 "[maxlon='#{format('%<lon>.7f', :lon => maxlon)}']" \
135 "[maxlat='#{format('%<lat>.7f', :lat => maxlat)}']", :count => 1
136 assert_select "node[id='#{node.id}']" \
137 "[lat='#{format('%<lat>.7f', :lat => node.lat)}']" \
138 "[lon='#{format('%<lon>.7f', :lon => node.lon)}']" \
139 "[version='#{node.version}']" \
140 "[changeset='#{node.changeset_id}']" \
141 "[visible='#{node.visible}']" \
142 "[timestamp='#{node.timestamp.xmlschema}']", :count => 1 do
143 # This should really be more generic
144 assert_select "tag[k='#{tag.k}'][v='#{tag.v}']"
146 assert_select "way", :count => 2
147 assert_select "way[id='#{way1.id}']", :count => 1
148 assert_select "way[id='#{way2.id}']", :count => 1
149 assert_select "relation", :count => 1
150 assert_select "relation[id='#{relation.id}']", :count => 1
155 node = create(:node, :lat => 7, :lon => 7)
156 tag = create(:node_tag, :node => node)
157 way1 = create(:way_node, :node => node).way
158 way2 = create(:way_node, :node => node).way
159 relation = create(:relation_member, :member => node).relation
161 # Need to split the min/max lat/lon out into their own variables here
162 # so that we can test they are returned later.
163 minlon = node.lon - 0.1
164 minlat = node.lat - 0.1
165 maxlon = node.lon + 0.1
166 maxlat = node.lat + 0.1
167 bbox = "#{minlon},#{minlat},#{maxlon},#{maxlat}"
168 get api_map_path(:bbox => bbox, :format => "json")
169 assert_response :success, "Expected success with the map call"
170 js = ActiveSupport::JSON.decode(@response.body)
173 assert_equal Settings.api_version, js["version"]
174 assert_equal Settings.generator, js["generator"]
175 assert_equal GeoRecord::Coord.new(minlon), js["bounds"]["minlon"]
176 assert_equal GeoRecord::Coord.new(minlat), js["bounds"]["minlat"]
177 assert_equal GeoRecord::Coord.new(maxlon), js["bounds"]["maxlon"]
178 assert_equal GeoRecord::Coord.new(maxlat), js["bounds"]["maxlat"]
180 result_nodes = js["elements"].select { |a| a["type"] == "node" }
181 .select { |a| a["id"] == node.id }
182 .select { |a| a["lat"] == GeoRecord::Coord.new(node.lat) }
183 .select { |a| a["lon"] == GeoRecord::Coord.new(node.lon) }
184 .select { |a| a["version"] == node.version }
185 .select { |a| a["changeset"] == node.changeset_id }
186 .select { |a| a["timestamp"] == node.timestamp.xmlschema }
187 assert_equal(1, result_nodes.count)
188 result_node = result_nodes.first
190 assert_equal result_node["tags"], tag.k => tag.v
191 assert_equal(2, js["elements"].count { |a| a["type"] == "way" })
192 assert_equal(1, js["elements"].count { |a| a["type"] == "way" && a["id"] == way1.id })
193 assert_equal(1, js["elements"].count { |a| a["type"] == "way" && a["id"] == way2.id })
194 assert_equal(1, js["elements"].count { |a| a["type"] == "relation" })
195 assert_equal(1, js["elements"].count { |a| a["type"] == "relation" && a["id"] == relation.id })
198 # This differs from the above test in that we are making the bbox exactly
199 # the same as the node we are looking at
200 def test_map_inclusive
201 node = create(:node, :lat => 7, :lon => 7)
202 tag = create(:node_tag, :node => node)
203 way1 = create(:way_node, :node => node).way
204 way2 = create(:way_node, :node => node).way
205 relation = create(:relation_member, :member => node).relation
207 bbox = "#{node.lon},#{node.lat},#{node.lon},#{node.lat}"
208 get api_map_path(:bbox => bbox)
209 assert_response :success, "The map call should have succeeded"
210 assert_select "osm[version='#{Settings.api_version}'][generator='#{Settings.generator}']", :count => 1 do
211 assert_select "bounds[minlon='#{node.lon}']" \
212 "[minlat='#{node.lat}']" \
213 "[maxlon='#{node.lon}']" \
214 "[maxlat='#{node.lat}']", :count => 1
215 assert_select "node[id='#{node.id}']" \
216 "[lat='#{format('%<lat>.7f', :lat => node.lat)}']" \
217 "[lon='#{format('%<lon>.7f', :lon => node.lon)}']" \
218 "[version='#{node.version}']" \
219 "[changeset='#{node.changeset_id}']" \
220 "[visible='#{node.visible}']" \
221 "[timestamp='#{node.timestamp.xmlschema}']", :count => 1 do
222 # This should really be more generic
223 assert_select "tag[k='#{tag.k}'][v='#{tag.v}']"
225 assert_select "way", :count => 2
226 assert_select "way[id='#{way1.id}']", :count => 1
227 assert_select "way[id='#{way2.id}']", :count => 1
228 assert_select "relation", :count => 1
229 assert_select "relation[id='#{relation.id}']", :count => 1
233 def test_map_complete_way
234 node = create(:node, :lat => 7, :lon => 7)
235 # create a couple of nodes well outside of the bbox
236 node2 = create(:node, :lat => 45, :lon => 45)
237 node3 = create(:node, :lat => 10, :lon => 10)
238 way1 = create(:way_node, :node => node).way
239 create(:way_node, :way => way1, :node => node2, :sequence_id => 2)
240 way2 = create(:way_node, :node => node).way
241 create(:way_node, :way => way2, :node => node3, :sequence_id => 2)
242 relation = create(:relation_member, :member => way1).relation
244 bbox = "#{node.lon},#{node.lat},#{node.lon},#{node.lat}"
245 get api_map_path(:bbox => bbox)
246 assert_response :success, "The map call should have succeeded"
247 assert_select "osm[version='#{Settings.api_version}'][generator='#{Settings.generator}']", :count => 1 do
248 assert_select "bounds[minlon='#{node.lon}'][minlat='#{node.lat}'][maxlon='#{node.lon}'][maxlat='#{node.lat}']", :count => 1
249 assert_select "node", :count => 3
250 assert_select "node[id='#{node.id}']", :count => 1
251 assert_select "node[id='#{node2.id}']", :count => 1
252 assert_select "node[id='#{node3.id}']", :count => 1
253 assert_select "way", :count => 2
254 assert_select "way[id='#{way1.id}']", :count => 1
255 assert_select "way[id='#{way2.id}']", :count => 1
256 assert_select "relation", :count => 1
257 assert_select "relation[id='#{relation.id}']", :count => 1
262 get api_map_path(:bbox => "179.998,89.998,179.999.1,89.999")
263 assert_response :success, "The map call should have succeeded"
264 assert_select "osm[version='#{Settings.api_version}'][generator='#{Settings.generator}']", :count => 1 do
265 assert_select "bounds[minlon='179.9980000'][minlat='89.9980000'][maxlon='179.9990000'][maxlat='89.9990000']", :count => 1
266 assert_select "node", :count => 0
267 assert_select "way", :count => 0
268 assert_select "relation", :count => 0
272 def test_map_without_bbox
274 assert_response :bad_request
275 assert_equal "The parameter bbox is required", @response.body, "A bbox param was expected"
278 def test_bbox_too_big
279 @badbigbbox.each do |bbox|
280 get api_map_path(:bbox => bbox)
281 assert_response :bad_request, "The bbox:#{bbox} was expected to be too big"
282 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}"
286 def test_bbox_malformed
287 @badmalformedbbox.each do |bbox|
288 get api_map_path(:bbox => bbox)
289 assert_response :bad_request, "The bbox:#{bbox} was expected to be malformed"
290 assert_equal "The parameter bbox must be of the form min_lon,min_lat,max_lon,max_lat", @response.body, "bbox: #{bbox}"
294 def test_bbox_lon_mixedup
295 @badlonmixedbbox.each do |bbox|
296 get api_map_path(:bbox => bbox)
297 assert_response :bad_request, "The bbox:#{bbox} was expected to have the longitude mixed up"
298 assert_equal "The minimum longitude must be less than the maximum longitude, but it wasn't", @response.body, "bbox: #{bbox}"
302 def test_bbox_lat_mixedup
303 @badlatmixedbbox.each do |bbox|
304 get api_map_path(:bbox => bbox)
305 assert_response :bad_request, "The bbox:#{bbox} was expected to have the latitude mixed up"
306 assert_equal "The minimum latitude must be less than the maximum latitude, but it wasn't", @response.body, "bbox: #{bbox}"
310 # We can't actually get an out of bounds error, as the bbox is sanitised.
311 # def test_latlon_outofbounds
312 # @badlatlonoutboundsbbox.each do |bbox|
313 # [ "trackpoints", "map" ].each do |tq|
314 # get tq, :bbox => bbox
315 # #print @request.to_yaml
316 # assert_response :bad_request, "The bbox #{bbox} was expected to be out of range"
317 # assert_equal "The latitudes must be between -90 an 90, and longitudes between -180 and 180", @response.body, "bbox: #{bbox}"