4 class MapControllerTest < ActionDispatch::IntegrationTest
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 accept_header = accept_format_header("text/xml")
45 get map_path(:bbox => bbox), :headers => accept_header
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 accept_header = accept_format_header("*/*")
51 get map_path(:bbox => bbox), :headers => accept_header
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 accept_header = accept_format_header("*/*")
57 get map_path(:bbox => bbox, :format => "json"), :headers => accept_header
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 accept_header = accept_format_header("text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,*/*;q=0.8")
63 get map_path(:bbox => bbox), :headers => accept_header
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 accept_header = accept_format_header("text/html, image/gif, image/jpeg, *; q=.2, */*; q=.2")
70 get map_path(:bbox => bbox), :headers => accept_header
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 accept_header = accept_format_header("text/plain, */*")
76 get map_path(:bbox => bbox), :headers => accept_header
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 accept_header = accept_format_header("text/*")
82 get map_path(:bbox => bbox), :headers => accept_header
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 accept_header = accept_format_header("application/json, */*")
88 get map_path(:bbox => bbox), :headers => accept_header
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 accept_header = accept_format_header("application/json")
94 get map_path(:bbox => bbox), :headers => accept_header
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, return HTTP 406 Not acceptable
99 accept_header = accept_format_header("text/json")
100 get map_path(:bbox => bbox), :headers => accept_header
101 assert_response :not_acceptable, "text/json should fail"
103 # image/jpeg is a format which we don't support, return HTTP 406 Not acceptable
104 accept_header = accept_format_header("image/jpeg")
105 get map_path(:bbox => bbox), :headers => accept_header
106 assert_response :not_acceptable, "text/json should fail"
109 # -------------------------------------
110 # Test reading a bounding box.
111 # -------------------------------------
114 node = create(:node, :lat => 7, :lon => 7)
115 tag = create(:node_tag, :node => node)
116 way1 = create(:way_node, :node => node).way
117 way2 = create(:way_node, :node => node).way
118 relation = create(:relation_member, :member => node).relation
120 # Need to split the min/max lat/lon out into their own variables here
121 # so that we can test they are returned later.
122 minlon = node.lon - 0.1
123 minlat = node.lat - 0.1
124 maxlon = node.lon + 0.1
125 maxlat = node.lat + 0.1
126 bbox = "#{minlon},#{minlat},#{maxlon},#{maxlat}"
127 get map_path(:bbox => bbox)
129 print @request.to_yaml
132 assert_response :success, "Expected scucess with the map call"
133 assert_select "osm[version='#{Settings.api_version}'][generator='#{Settings.generator}']", :count => 1 do
134 assert_select "bounds[minlon='#{format('%.7f', minlon)}'][minlat='#{format('%.7f', minlat)}'][maxlon='#{format('%.7f', maxlon)}'][maxlat='#{format('%.7f', maxlat)}']", :count => 1
135 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
136 # This should really be more generic
137 assert_select "tag[k='#{tag.k}'][v='#{tag.v}']"
139 assert_select "way", :count => 2
140 assert_select "way[id='#{way1.id}']", :count => 1
141 assert_select "way[id='#{way2.id}']", :count => 1
142 assert_select "relation", :count => 1
143 assert_select "relation[id='#{relation.id}']", :count => 1
148 node = create(:node, :lat => 7, :lon => 7)
149 tag = create(:node_tag, :node => node)
150 way1 = create(:way_node, :node => node).way
151 way2 = create(:way_node, :node => node).way
152 relation = create(:relation_member, :member => node).relation
154 # Need to split the min/max lat/lon out into their own variables here
155 # so that we can test they are returned later.
156 minlon = node.lon - 0.1
157 minlat = node.lat - 0.1
158 maxlon = node.lon + 0.1
159 maxlat = node.lat + 0.1
160 bbox = "#{minlon},#{minlat},#{maxlon},#{maxlat}"
161 get map_path(:bbox => bbox, :format => "json")
163 print @request.to_yaml
166 assert_response :success, "Expected success with the map call"
167 js = ActiveSupport::JSON.decode(@response.body)
170 assert_equal Settings.api_version, js["version"]
171 assert_equal Settings.generator, js["generator"]
172 assert_equal GeoRecord::Coord.new(minlon), js["bounds"]["minlon"]
173 assert_equal GeoRecord::Coord.new(minlat), js["bounds"]["minlat"]
174 assert_equal GeoRecord::Coord.new(maxlon), js["bounds"]["maxlon"]
175 assert_equal GeoRecord::Coord.new(maxlat), js["bounds"]["maxlat"]
177 result_nodes = js["elements"].select { |a| a["type"] == "node" }
178 .select { |a| a["id"] == node.id }
179 .select { |a| a["lat"] == GeoRecord::Coord.new(node.lat) }
180 .select { |a| a["lon"] == GeoRecord::Coord.new(node.lon) }
181 .select { |a| a["version"] == node.version }
182 .select { |a| a["changeset"] == node.changeset_id }
183 .select { |a| a["timestamp"] == node.timestamp.xmlschema }
184 assert_equal result_nodes.count, 1
185 result_node = result_nodes.first
187 assert_equal result_node["tags"], tag.k => tag.v
188 assert_equal 2, (js["elements"].count { |a| a["type"] == "way" })
189 assert_equal 1, (js["elements"].count { |a| a["type"] == "way" && a["id"] == way1.id })
190 assert_equal 1, (js["elements"].count { |a| a["type"] == "way" && a["id"] == way2.id })
191 assert_equal 1, (js["elements"].count { |a| a["type"] == "relation" })
192 assert_equal 1, (js["elements"].count { |a| a["type"] == "relation" && a["id"] == relation.id })
195 # This differs from the above test in that we are making the bbox exactly
196 # the same as the node we are looking at
197 def test_map_inclusive
198 node = create(:node, :lat => 7, :lon => 7)
199 tag = create(:node_tag, :node => node)
200 way1 = create(:way_node, :node => node).way
201 way2 = create(:way_node, :node => node).way
202 relation = create(:relation_member, :member => node).relation
204 bbox = "#{node.lon},#{node.lat},#{node.lon},#{node.lat}"
205 get map_path(:bbox => bbox)
206 assert_response :success, "The map call should have succeeded"
207 assert_select "osm[version='#{Settings.api_version}'][generator='#{Settings.generator}']", :count => 1 do
208 assert_select "bounds[minlon='#{node.lon}'][minlat='#{node.lat}'][maxlon='#{node.lon}'][maxlat='#{node.lat}']", :count => 1
209 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
210 # This should really be more generic
211 assert_select "tag[k='#{tag.k}'][v='#{tag.v}']"
213 assert_select "way", :count => 2
214 assert_select "way[id='#{way1.id}']", :count => 1
215 assert_select "way[id='#{way2.id}']", :count => 1
216 assert_select "relation", :count => 1
217 assert_select "relation[id='#{relation.id}']", :count => 1
221 def test_map_complete_way
222 node = create(:node, :lat => 7, :lon => 7)
223 # create a couple of nodes well outside of the bbox
224 node2 = create(:node, :lat => 45, :lon => 45)
225 node3 = create(:node, :lat => 10, :lon => 10)
226 way1 = create(:way_node, :node => node).way
227 create(:way_node, :way => way1, :node => node2, :sequence_id => 2)
228 way2 = create(:way_node, :node => node).way
229 create(:way_node, :way => way2, :node => node3, :sequence_id => 2)
230 relation = create(:relation_member, :member => way1).relation
232 bbox = "#{node.lon},#{node.lat},#{node.lon},#{node.lat}"
233 get map_path(:bbox => bbox)
234 assert_response :success, "The map call should have succeeded"
235 assert_select "osm[version='#{Settings.api_version}'][generator='#{Settings.generator}']", :count => 1 do
236 assert_select "bounds[minlon='#{node.lon}'][minlat='#{node.lat}'][maxlon='#{node.lon}'][maxlat='#{node.lat}']", :count => 1
237 assert_select "node", :count => 3
238 assert_select "node[id='#{node.id}']", :count => 1
239 assert_select "node[id='#{node2.id}']", :count => 1
240 assert_select "node[id='#{node3.id}']", :count => 1
241 assert_select "way", :count => 2
242 assert_select "way[id='#{way1.id}']", :count => 1
243 assert_select "way[id='#{way2.id}']", :count => 1
244 assert_select "relation", :count => 1
245 assert_select "relation[id='#{relation.id}']", :count => 1
250 get map_path(:bbox => "179.998,89.998,179.999.1,89.999")
251 assert_response :success, "The map call should have succeeded"
252 assert_select "osm[version='#{Settings.api_version}'][generator='#{Settings.generator}']", :count => 1 do
253 assert_select "bounds[minlon='179.9980000'][minlat='89.9980000'][maxlon='179.9990000'][maxlat='89.9990000']", :count => 1
254 assert_select "node", :count => 0
255 assert_select "way", :count => 0
256 assert_select "relation", :count => 0
260 def test_map_without_bbox
262 assert_response :bad_request
263 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"
266 def test_bbox_too_big
267 @badbigbbox.each do |bbox|
268 get map_path(:bbox => bbox)
269 assert_response :bad_request, "The bbox:#{bbox} was expected to be too big"
270 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}"
274 def test_bbox_malformed
275 @badmalformedbbox.each do |bbox|
276 get map_path(:bbox => bbox)
277 assert_response :bad_request, "The bbox:#{bbox} was expected to be malformed"
278 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}"
282 def test_bbox_lon_mixedup
283 @badlonmixedbbox.each do |bbox|
284 get map_path(:bbox => bbox)
285 assert_response :bad_request, "The bbox:#{bbox} was expected to have the longitude mixed up"
286 assert_equal "The minimum longitude must be less than the maximum longitude, but it wasn't", @response.body, "bbox: #{bbox}"
290 def test_bbox_lat_mixedup
291 @badlatmixedbbox.each do |bbox|
292 get map_path(:bbox => bbox)
293 assert_response :bad_request, "The bbox:#{bbox} was expected to have the latitude mixed up"
294 assert_equal "The minimum latitude must be less than the maximum latitude, but it wasn't", @response.body, "bbox: #{bbox}"
298 # We can't actually get an out of bounds error, as the bbox is sanitised.
299 # def test_latlon_outofbounds
300 # @badlatlonoutboundsbbox.each do |bbox|
301 # [ "trackpoints", "map" ].each do |tq|
302 # get tq, :bbox => bbox
303 # #print @request.to_yaml
304 # assert_response :bad_request, "The bbox #{bbox} was expected to be out of range"
305 # assert_equal "The latitudes must be between -90 an 90, and longitudes between -180 and 180", @response.body, "bbox: #{bbox}"