]> git.openstreetmap.org Git - rails.git/blob - test/controllers/api/maps_controller_test.rb
Merge remote-tracking branch 'upstream/pull/6215'
[rails.git] / test / controllers / api / maps_controller_test.rb
1 require "test_helper"
2
3 module Api
4   class MapsControllerTest < ActionDispatch::IntegrationTest
5     def setup
6       super
7       @badbigbbox = %w[-0.1,-0.1,1.1,1.1 10,10,11,11]
8       @badmalformedbbox = %w[-0.1 hello
9                              10N2W10.1N2.1W]
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       # really reject it, however this is to test to see if the api changes.
17     end
18
19     ##
20     # test all routes which lead to this controller
21     def test_routes
22       assert_routing(
23         { :path => "/api/0.6/map", :method => :get },
24         { :controller => "api/maps", :action => "show" }
25       )
26       assert_routing(
27         { :path => "/api/0.6/map.json", :method => :get },
28         { :controller => "api/maps", :action => "show", :format => "json" }
29       )
30     end
31
32     ##
33     # test http accept headers
34     def test_http_accept_header
35       node = create(:node)
36
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}"
42
43       # Accept: XML format -> use XML
44       accept_header = accept_format_header("text/xml")
45       get api_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"]
48
49       # Accept: Any format -> use XML
50       accept_header = accept_format_header("*/*")
51       get api_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"]
54
55       # Accept: Any format, .json URL suffix -> use json
56       accept_header = accept_format_header("*/*")
57       get api_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"]
60
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 api_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"]
66
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 api_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"]
73
74       # Accept: text/plain, */* -> use XML
75       accept_header = accept_format_header("text/plain, */*")
76       get api_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"]
79
80       # Accept: text/* -> use XML
81       accept_header = accept_format_header("text/*")
82       get api_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"]
85
86       # Accept: json, */* format -> use json
87       accept_header = accept_format_header("application/json, */*")
88       get api_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"]
91
92       # Accept: json format -> use json
93       accept_header = accept_format_header("application/json")
94       get api_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"]
97
98       # text/json is in invalid format, return HTTP 406 Not acceptable
99       accept_header = accept_format_header("text/json")
100       get api_map_path(:bbox => bbox), :headers => accept_header
101       assert_response :not_acceptable, "text/json should fail"
102
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 api_map_path(:bbox => bbox), :headers => accept_header
106       assert_response :not_acceptable, "text/json should fail"
107     end
108
109     # -------------------------------------
110     # Test reading a bounding box.
111     # -------------------------------------
112
113     def test_map
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
119
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 api_map_path(:bbox => bbox)
128       assert_response :success, "Expected success with the map call"
129       assert_select "osm[version='#{Settings.api_version}'][generator='#{Settings.generator}']", :count => 1 do
130         assert_select "bounds[minlon='#{format('%<lon>.7f', :lon => minlon)}']" \
131                       "[minlat='#{format('%<lat>.7f', :lat => minlat)}']" \
132                       "[maxlon='#{format('%<lon>.7f', :lon => maxlon)}']" \
133                       "[maxlat='#{format('%<lat>.7f', :lat => maxlat)}']", :count => 1
134         assert_select "node[id='#{node.id}']" \
135                       "[lat='#{format('%<lat>.7f', :lat => node.lat)}']" \
136                       "[lon='#{format('%<lon>.7f', :lon => node.lon)}']" \
137                       "[version='#{node.version}']" \
138                       "[changeset='#{node.changeset_id}']" \
139                       "[visible='#{node.visible}']" \
140                       "[timestamp='#{node.timestamp.xmlschema}']", :count => 1 do
141           # This should really be more generic
142           assert_select "tag[k='#{tag.k}'][v='#{tag.v}']"
143         end
144         assert_select "way", :count => 2
145         assert_select "way[id='#{way1.id}']", :count => 1
146         assert_select "way[id='#{way2.id}']", :count => 1
147         assert_select "relation", :count => 1
148         assert_select "relation[id='#{relation.id}']", :count => 1
149       end
150     end
151
152     def test_map_json
153       node = create(:node, :lat => 7, :lon => 7)
154       tag = create(:node_tag, :node => node)
155       way1 = create(:way_node, :node => node).way
156       way2 = create(:way_node, :node => node).way
157       relation = create(:relation_member, :member => node).relation
158
159       # Need to split the min/max lat/lon out into their own variables here
160       # so that we can test they are returned later.
161       minlon = node.lon - 0.1
162       minlat = node.lat - 0.1
163       maxlon = node.lon + 0.1
164       maxlat = node.lat + 0.1
165       bbox = "#{minlon},#{minlat},#{maxlon},#{maxlat}"
166       get api_map_path(:bbox => bbox, :format => "json")
167       assert_response :success, "Expected success with the map call"
168       js = ActiveSupport::JSON.decode(@response.body)
169       assert_not_nil js
170
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"]
177
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(1, result_nodes.count)
186       result_node = result_nodes.first
187
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 })
194     end
195
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
204
205       bbox = "#{node.lon},#{node.lat},#{node.lon},#{node.lat}"
206       get api_map_path(: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}']" \
210                       "[minlat='#{node.lat}']" \
211                       "[maxlon='#{node.lon}']" \
212                       "[maxlat='#{node.lat}']", :count => 1
213         assert_select "node[id='#{node.id}']" \
214                       "[lat='#{format('%<lat>.7f', :lat => node.lat)}']" \
215                       "[lon='#{format('%<lon>.7f', :lon => node.lon)}']" \
216                       "[version='#{node.version}']" \
217                       "[changeset='#{node.changeset_id}']" \
218                       "[visible='#{node.visible}']" \
219                       "[timestamp='#{node.timestamp.xmlschema}']", :count => 1 do
220           # This should really be more generic
221           assert_select "tag[k='#{tag.k}'][v='#{tag.v}']"
222         end
223         assert_select "way", :count => 2
224         assert_select "way[id='#{way1.id}']", :count => 1
225         assert_select "way[id='#{way2.id}']", :count => 1
226         assert_select "relation", :count => 1
227         assert_select "relation[id='#{relation.id}']", :count => 1
228       end
229     end
230
231     def test_map_complete_way
232       node = create(:node, :lat => 7, :lon => 7)
233       # create a couple of nodes well outside of the bbox
234       node2 = create(:node, :lat => 45, :lon => 45)
235       node3 = create(:node, :lat => 10, :lon => 10)
236       way1 = create(:way_node, :node => node).way
237       create(:way_node, :way => way1, :node => node2, :sequence_id => 2)
238       way2 = create(:way_node, :node => node).way
239       create(:way_node, :way => way2, :node => node3, :sequence_id => 2)
240       relation = create(:relation_member, :member => way1).relation
241
242       bbox = "#{node.lon},#{node.lat},#{node.lon},#{node.lat}"
243       get api_map_path(:bbox => bbox)
244       assert_response :success, "The map call should have succeeded"
245       assert_select "osm[version='#{Settings.api_version}'][generator='#{Settings.generator}']", :count => 1 do
246         assert_select "bounds[minlon='#{node.lon}'][minlat='#{node.lat}'][maxlon='#{node.lon}'][maxlat='#{node.lat}']", :count => 1
247         assert_select "node", :count => 3
248         assert_select "node[id='#{node.id}']", :count => 1
249         assert_select "node[id='#{node2.id}']", :count => 1
250         assert_select "node[id='#{node3.id}']", :count => 1
251         assert_select "way", :count => 2
252         assert_select "way[id='#{way1.id}']", :count => 1
253         assert_select "way[id='#{way2.id}']", :count => 1
254         assert_select "relation", :count => 1
255         assert_select "relation[id='#{relation.id}']", :count => 1
256       end
257     end
258
259     def test_map_empty
260       get api_map_path(:bbox => "179.998,89.998,179.999.1,89.999")
261       assert_response :success, "The map call should have succeeded"
262       assert_select "osm[version='#{Settings.api_version}'][generator='#{Settings.generator}']", :count => 1 do
263         assert_select "bounds[minlon='179.9980000'][minlat='89.9980000'][maxlon='179.9990000'][maxlat='89.9990000']", :count => 1
264         assert_select "node", :count => 0
265         assert_select "way", :count => 0
266         assert_select "relation", :count => 0
267       end
268     end
269
270     def test_map_without_bbox
271       get api_map_path
272       assert_response :bad_request
273       assert_equal "The parameter bbox is required", @response.body, "A bbox param was expected"
274     end
275
276     def test_bbox_too_big
277       @badbigbbox.each do |bbox|
278         get api_map_path(:bbox => bbox)
279         assert_response :bad_request, "The bbox:#{bbox} was expected to be too big"
280         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}"
281       end
282     end
283
284     def test_bbox_malformed
285       @badmalformedbbox.each do |bbox|
286         get api_map_path(:bbox => bbox)
287         assert_response :bad_request, "The bbox:#{bbox} was expected to be malformed"
288         assert_equal "The parameter bbox must be of the form min_lon,min_lat,max_lon,max_lat", @response.body, "bbox: #{bbox}"
289       end
290     end
291
292     def test_bbox_lon_mixedup
293       @badlonmixedbbox.each do |bbox|
294         get api_map_path(:bbox => bbox)
295         assert_response :bad_request, "The bbox:#{bbox} was expected to have the longitude mixed up"
296         assert_equal "The minimum longitude must be less than the maximum longitude, but it wasn't", @response.body, "bbox: #{bbox}"
297       end
298     end
299
300     def test_bbox_lat_mixedup
301       @badlatmixedbbox.each do |bbox|
302         get api_map_path(:bbox => bbox)
303         assert_response :bad_request, "The bbox:#{bbox} was expected to have the latitude mixed up"
304         assert_equal "The minimum latitude must be less than the maximum latitude, but it wasn't", @response.body, "bbox: #{bbox}"
305       end
306     end
307
308     # We can't actually get an out of bounds error, as the bbox is sanitised.
309     # def test_latlon_outofbounds
310     #  @badlatlonoutboundsbbox.each do |bbox|
311     #    [ "trackpoints", "map" ].each do |tq|
312     #      get tq, :bbox => bbox
313     #      #print @request.to_yaml
314     #      assert_response :bad_request, "The bbox #{bbox} was expected to be out of range"
315     #      assert_equal "The latitudes must be between -90 an 90, and longitudes between -180 and 180", @response.body, "bbox: #{bbox}"
316     #    end
317     #  end
318     # end
319   end
320 end