]> git.openstreetmap.org Git - rails.git/blob - test/controllers/api/map_controller_test.rb
Rubocop fixes
[rails.git] / test / controllers / api / map_controller_test.rb
1 require "test_helper"
2
3 module Api
4   class MapControllerTest < ActionController::TestCase
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       # reall 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/map", :action => "index" }
25       )
26     end
27
28     # -------------------------------------
29     # Test reading a bounding box.
30     # -------------------------------------
31
32     def test_map
33       node = create(:node, :lat => 7, :lon => 7)
34       tag = create(:node_tag, :node => node)
35       way1 = create(:way_node, :node => node).way
36       way2 = create(:way_node, :node => node).way
37       relation = create(:relation_member, :member => node).relation
38
39       # Need to split the min/max lat/lon out into their own variables here
40       # so that we can test they are returned later.
41       minlon = node.lon - 0.1
42       minlat = node.lat - 0.1
43       maxlon = node.lon + 0.1
44       maxlat = node.lat + 0.1
45       bbox = "#{minlon},#{minlat},#{maxlon},#{maxlat}"
46       get :index, :params => { :bbox => bbox }
47       if $VERBOSE
48         print @request.to_yaml
49         print @response.body
50       end
51       assert_response :success, "Expected scucess with the map call"
52       assert_select "osm[version='#{API_VERSION}'][generator='#{GENERATOR}']", :count => 1 do
53         assert_select "bounds[minlon='#{format('%.7f', minlon)}'][minlat='#{format('%.7f', minlat)}'][maxlon='#{format('%.7f', maxlon)}'][maxlat='#{format('%.7f', maxlat)}']", :count => 1
54         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
55           # This should really be more generic
56           assert_select "tag[k='#{tag.k}'][v='#{tag.v}']"
57         end
58         assert_select "way", :count => 2
59         assert_select "way[id='#{way1.id}']", :count => 1
60         assert_select "way[id='#{way2.id}']", :count => 1
61         assert_select "relation", :count => 1
62         assert_select "relation[id='#{relation.id}']", :count => 1
63       end
64     end
65
66     # This differs from the above test in that we are making the bbox exactly
67     # the same as the node we are looking at
68     def test_map_inclusive
69       node = create(:node, :lat => 7, :lon => 7)
70       tag = create(:node_tag, :node => node)
71       way1 = create(:way_node, :node => node).way
72       way2 = create(:way_node, :node => node).way
73       relation = create(:relation_member, :member => node).relation
74
75       bbox = "#{node.lon},#{node.lat},#{node.lon},#{node.lat}"
76       get :index, :params => { :bbox => bbox }
77       assert_response :success, "The map call should have succeeded"
78       assert_select "osm[version='#{API_VERSION}'][generator='#{GENERATOR}']", :count => 1 do
79         assert_select "bounds[minlon='#{node.lon}'][minlat='#{node.lat}'][maxlon='#{node.lon}'][maxlat='#{node.lat}']", :count => 1
80         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
81           # This should really be more generic
82           assert_select "tag[k='#{tag.k}'][v='#{tag.v}']"
83         end
84         assert_select "way", :count => 2
85         assert_select "way[id='#{way1.id}']", :count => 1
86         assert_select "way[id='#{way2.id}']", :count => 1
87         assert_select "relation", :count => 1
88         assert_select "relation[id='#{relation.id}']", :count => 1
89       end
90     end
91
92     def test_map_complete_way
93       node = create(:node, :lat => 7, :lon => 7)
94       # create a couple of nodes well outside of the bbox
95       node2 = create(:node, :lat => 45, :lon => 45)
96       node3 = create(:node, :lat => 10, :lon => 10)
97       way1 = create(:way_node, :node => node).way
98       create(:way_node, :way => way1, :node => node2, :sequence_id => 2)
99       way2 = create(:way_node, :node => node).way
100       create(:way_node, :way => way2, :node => node3, :sequence_id => 2)
101       relation = create(:relation_member, :member => way1).relation
102
103       bbox = "#{node.lon},#{node.lat},#{node.lon},#{node.lat}"
104       get :index, :params => { :bbox => bbox }
105       assert_response :success, "The map call should have succeeded"
106       assert_select "osm[version='#{API_VERSION}'][generator='#{GENERATOR}']", :count => 1 do
107         assert_select "bounds[minlon='#{node.lon}'][minlat='#{node.lat}'][maxlon='#{node.lon}'][maxlat='#{node.lat}']", :count => 1
108         assert_select "node", :count => 3
109         assert_select "node[id='#{node.id}']", :count => 1
110         assert_select "node[id='#{node2.id}']", :count => 1
111         assert_select "node[id='#{node3.id}']", :count => 1
112         assert_select "way", :count => 2
113         assert_select "way[id='#{way1.id}']", :count => 1
114         assert_select "way[id='#{way2.id}']", :count => 1
115         assert_select "relation", :count => 1
116         assert_select "relation[id='#{relation.id}']", :count => 1
117       end
118     end
119
120     def test_map_empty
121       get :index, :params => { :bbox => "179.998,89.998,179.999.1,89.999" }
122       assert_response :success, "The map call should have succeeded"
123       assert_select "osm[version='#{API_VERSION}'][generator='#{GENERATOR}']", :count => 1 do
124         assert_select "bounds[minlon='179.9980000'][minlat='89.9980000'][maxlon='179.9990000'][maxlat='89.9990000']", :count => 1
125         assert_select "node", :count => 0
126         assert_select "way", :count => 0
127         assert_select "relation", :count => 0
128       end
129     end
130
131     def test_map_without_bbox
132       get :index
133       assert_response :bad_request
134       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"
135     end
136
137     def test_bbox_too_big
138       @badbigbbox.each do |bbox|
139         get :index, :params => { :bbox => bbox }
140         assert_response :bad_request, "The bbox:#{bbox} was expected to be too big"
141         assert_equal "The maximum bbox size is #{MAX_REQUEST_AREA}, and your request was too large. Either request a smaller area, or use planet.osm", @response.body, "bbox: #{bbox}"
142       end
143     end
144
145     def test_bbox_malformed
146       @badmalformedbbox.each do |bbox|
147         get :index, :params => { :bbox => bbox }
148         assert_response :bad_request, "The bbox:#{bbox} was expected to be malformed"
149         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}"
150       end
151     end
152
153     def test_bbox_lon_mixedup
154       @badlonmixedbbox.each do |bbox|
155         get :index, :params => { :bbox => bbox }
156         assert_response :bad_request, "The bbox:#{bbox} was expected to have the longitude mixed up"
157         assert_equal "The minimum longitude must be less than the maximum longitude, but it wasn't", @response.body, "bbox: #{bbox}"
158       end
159     end
160
161     def test_bbox_lat_mixedup
162       @badlatmixedbbox.each do |bbox|
163         get :index, :params => { :bbox => bbox }
164         assert_response :bad_request, "The bbox:#{bbox} was expected to have the latitude mixed up"
165         assert_equal "The minimum latitude must be less than the maximum latitude, but it wasn't", @response.body, "bbox: #{bbox}"
166       end
167     end
168
169     # We can't actually get an out of bounds error, as the bbox is sanitised.
170     # def test_latlon_outofbounds
171     #  @badlatlonoutboundsbbox.each do |bbox|
172     #    [ "trackpoints", "map" ].each do |tq|
173     #      get tq, :bbox => bbox
174     #      #print @request.to_yaml
175     #      assert_response :bad_request, "The bbox #{bbox} was expected to be out of range"
176     #      assert_equal "The latitudes must be between -90 an 90, and longitudes between -180 and 180", @response.body, "bbox: #{bbox}"
177     #    end
178     #  end
179     # end
180   end
181 end