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
35 node = create(:node, :lat => 7, :lon => 7)
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"
104 # -------------------------------------
105 # Test reading a bounding box.
106 # -------------------------------------
109 node = create(:node, :lat => 7, :lon => 7)
110 tag = create(:node_tag, :node => node)
111 way1 = create(:way_node, :node => node).way
112 way2 = create(:way_node, :node => node).way
113 relation = create(:relation_member, :member => node).relation
115 # Need to split the min/max lat/lon out into their own variables here
116 # so that we can test they are returned later.
117 minlon = node.lon - 0.1
118 minlat = node.lat - 0.1
119 maxlon = node.lon + 0.1
120 maxlat = node.lat + 0.1
121 bbox = "#{minlon},#{minlat},#{maxlon},#{maxlat}"
122 get :index, :params => { :bbox => bbox }
124 print @request.to_yaml
127 assert_response :success, "Expected scucess with the map call"
128 assert_select "osm[version='#{Settings.api_version}'][generator='#{Settings.generator}']", :count => 1 do
129 assert_select "bounds[minlon='#{format('%.7f', minlon)}'][minlat='#{format('%.7f', minlat)}'][maxlon='#{format('%.7f', maxlon)}'][maxlat='#{format('%.7f', maxlat)}']", :count => 1
130 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
131 # This should really be more generic
132 assert_select "tag[k='#{tag.k}'][v='#{tag.v}']"
134 assert_select "way", :count => 2
135 assert_select "way[id='#{way1.id}']", :count => 1
136 assert_select "way[id='#{way2.id}']", :count => 1
137 assert_select "relation", :count => 1
138 assert_select "relation[id='#{relation.id}']", :count => 1
143 node = create(:node, :lat => 7, :lon => 7)
144 tag = create(:node_tag, :node => node)
145 way1 = create(:way_node, :node => node).way
146 way2 = create(:way_node, :node => node).way
147 relation = create(:relation_member, :member => node).relation
149 # Need to split the min/max lat/lon out into their own variables here
150 # so that we can test they are returned later.
151 minlon = node.lon - 0.1
152 minlat = node.lat - 0.1
153 maxlon = node.lon + 0.1
154 maxlat = node.lat + 0.1
155 bbox = "#{minlon},#{minlat},#{maxlon},#{maxlat}"
156 get :index, :params => { :bbox => bbox, :format => "json" }
158 print @request.to_yaml
161 assert_response :success, "Expected success with the map call"
162 js = ActiveSupport::JSON.decode(@response.body)
165 assert_equal Settings.api_version, js["version"]
166 assert_equal Settings.generator, js["generator"]
167 assert_equal GeoRecord::Coord.new(minlon), js["bounds"]["minlon"]
168 assert_equal GeoRecord::Coord.new(minlat), js["bounds"]["minlat"]
169 assert_equal GeoRecord::Coord.new(maxlon), js["bounds"]["maxlon"]
170 assert_equal GeoRecord::Coord.new(maxlat), js["bounds"]["maxlat"]
172 result_nodes = js["elements"].select { |a| a["type"] == "node" }
173 .select { |a| a["id"] == node.id }
174 .select { |a| a["lat"] == GeoRecord::Coord.new(node.lat) }
175 .select { |a| a["lon"] == GeoRecord::Coord.new(node.lon) }
176 .select { |a| a["version"] == node.version }
177 .select { |a| a["changeset"] == node.changeset_id }
178 .select { |a| a["timestamp"] == node.timestamp.xmlschema }
179 assert_equal result_nodes.count, 1
180 result_node = result_nodes.first
182 assert_equal result_node["tags"], tag.k => tag.v
183 assert_equal 2, (js["elements"].count { |a| a["type"] == "way" })
184 assert_equal 1, (js["elements"].count { |a| a["type"] == "way" && a["id"] == way1.id })
185 assert_equal 1, (js["elements"].count { |a| a["type"] == "way" && a["id"] == way2.id })
186 assert_equal 1, (js["elements"].count { |a| a["type"] == "relation" })
187 assert_equal 1, (js["elements"].count { |a| a["type"] == "relation" && a["id"] == relation.id })
190 # This differs from the above test in that we are making the bbox exactly
191 # the same as the node we are looking at
192 def test_map_inclusive
193 node = create(:node, :lat => 7, :lon => 7)
194 tag = create(:node_tag, :node => node)
195 way1 = create(:way_node, :node => node).way
196 way2 = create(:way_node, :node => node).way
197 relation = create(:relation_member, :member => node).relation
199 bbox = "#{node.lon},#{node.lat},#{node.lon},#{node.lat}"
200 get :index, :params => { :bbox => bbox }
201 assert_response :success, "The map call should have succeeded"
202 assert_select "osm[version='#{Settings.api_version}'][generator='#{Settings.generator}']", :count => 1 do
203 assert_select "bounds[minlon='#{node.lon}'][minlat='#{node.lat}'][maxlon='#{node.lon}'][maxlat='#{node.lat}']", :count => 1
204 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
205 # This should really be more generic
206 assert_select "tag[k='#{tag.k}'][v='#{tag.v}']"
208 assert_select "way", :count => 2
209 assert_select "way[id='#{way1.id}']", :count => 1
210 assert_select "way[id='#{way2.id}']", :count => 1
211 assert_select "relation", :count => 1
212 assert_select "relation[id='#{relation.id}']", :count => 1
216 def test_map_complete_way
217 node = create(:node, :lat => 7, :lon => 7)
218 # create a couple of nodes well outside of the bbox
219 node2 = create(:node, :lat => 45, :lon => 45)
220 node3 = create(:node, :lat => 10, :lon => 10)
221 way1 = create(:way_node, :node => node).way
222 create(:way_node, :way => way1, :node => node2, :sequence_id => 2)
223 way2 = create(:way_node, :node => node).way
224 create(:way_node, :way => way2, :node => node3, :sequence_id => 2)
225 relation = create(:relation_member, :member => way1).relation
227 bbox = "#{node.lon},#{node.lat},#{node.lon},#{node.lat}"
228 get :index, :params => { :bbox => bbox }
229 assert_response :success, "The map call should have succeeded"
230 assert_select "osm[version='#{Settings.api_version}'][generator='#{Settings.generator}']", :count => 1 do
231 assert_select "bounds[minlon='#{node.lon}'][minlat='#{node.lat}'][maxlon='#{node.lon}'][maxlat='#{node.lat}']", :count => 1
232 assert_select "node", :count => 3
233 assert_select "node[id='#{node.id}']", :count => 1
234 assert_select "node[id='#{node2.id}']", :count => 1
235 assert_select "node[id='#{node3.id}']", :count => 1
236 assert_select "way", :count => 2
237 assert_select "way[id='#{way1.id}']", :count => 1
238 assert_select "way[id='#{way2.id}']", :count => 1
239 assert_select "relation", :count => 1
240 assert_select "relation[id='#{relation.id}']", :count => 1
245 get :index, :params => { :bbox => "179.998,89.998,179.999.1,89.999" }
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='179.9980000'][minlat='89.9980000'][maxlon='179.9990000'][maxlat='89.9990000']", :count => 1
249 assert_select "node", :count => 0
250 assert_select "way", :count => 0
251 assert_select "relation", :count => 0
255 def test_map_without_bbox
257 assert_response :bad_request
258 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"
261 def test_bbox_too_big
262 @badbigbbox.each do |bbox|
263 get :index, :params => { :bbox => bbox }
264 assert_response :bad_request, "The bbox:#{bbox} was expected to be too big"
265 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}"
269 def test_bbox_malformed
270 @badmalformedbbox.each do |bbox|
271 get :index, :params => { :bbox => bbox }
272 assert_response :bad_request, "The bbox:#{bbox} was expected to be malformed"
273 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}"
277 def test_bbox_lon_mixedup
278 @badlonmixedbbox.each do |bbox|
279 get :index, :params => { :bbox => bbox }
280 assert_response :bad_request, "The bbox:#{bbox} was expected to have the longitude mixed up"
281 assert_equal "The minimum longitude must be less than the maximum longitude, but it wasn't", @response.body, "bbox: #{bbox}"
285 def test_bbox_lat_mixedup
286 @badlatmixedbbox.each do |bbox|
287 get :index, :params => { :bbox => bbox }
288 assert_response :bad_request, "The bbox:#{bbox} was expected to have the latitude mixed up"
289 assert_equal "The minimum latitude must be less than the maximum latitude, but it wasn't", @response.body, "bbox: #{bbox}"
293 # We can't actually get an out of bounds error, as the bbox is sanitised.
294 # def test_latlon_outofbounds
295 # @badlatlonoutboundsbbox.each do |bbox|
296 # [ "trackpoints", "map" ].each do |tq|
297 # get tq, :bbox => bbox
298 # #print @request.to_yaml
299 # assert_response :bad_request, "The bbox #{bbox} was expected to be out of range"
300 # assert_equal "The latitudes must be between -90 an 90, and longitudes between -180 and 180", @response.body, "bbox: #{bbox}"