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