]> git.openstreetmap.org Git - rails.git/blob - test/controllers/api_controller_test.rb
Move the trackpoints call into its own controller (and rename to tracepoints)
[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/permissions", :method => :get },
23       { :controller => "api", :action => "permissions" }
24     )
25     assert_routing(
26       { :path => "/api/0.6/map", :method => :get },
27       { :controller => "api", :action => "map" }
28     )
29     assert_routing(
30       { :path => "/api/0.6/changes", :method => :get },
31       { :controller => "api", :action => "changes" }
32     )
33   end
34
35   # -------------------------------------
36   # Test reading a bounding box.
37   # -------------------------------------
38
39   def test_map
40     node = create(:node, :lat => 7, :lon => 7)
41     tag = create(:node_tag, :node => node)
42     way1 = create(:way_node, :node => node).way
43     way2 = create(:way_node, :node => node).way
44     relation = create(:relation_member, :member => node).relation
45
46     # Need to split the min/max lat/lon out into their own variables here
47     # so that we can test they are returned later.
48     minlon = node.lon - 0.1
49     minlat = node.lat - 0.1
50     maxlon = node.lon + 0.1
51     maxlat = node.lat + 0.1
52     bbox = "#{minlon},#{minlat},#{maxlon},#{maxlat}"
53     get :map, :params => { :bbox => bbox }
54     if $VERBOSE
55       print @request.to_yaml
56       print @response.body
57     end
58     assert_response :success, "Expected scucess with the map call"
59     assert_select "osm[version='#{API_VERSION}'][generator='#{GENERATOR}']", :count => 1 do
60       assert_select "bounds[minlon='#{format('%.7f', minlon)}'][minlat='#{format('%.7f', minlat)}'][maxlon='#{format('%.7f', maxlon)}'][maxlat='#{format('%.7f', maxlat)}']", :count => 1
61       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
62         # This should really be more generic
63         assert_select "tag[k='#{tag.k}'][v='#{tag.v}']"
64       end
65       assert_select "way", :count => 2
66       assert_select "way[id='#{way1.id}']", :count => 1
67       assert_select "way[id='#{way2.id}']", :count => 1
68       assert_select "relation", :count => 1
69       assert_select "relation[id='#{relation.id}']", :count => 1
70     end
71   end
72
73   # This differs from the above test in that we are making the bbox exactly
74   # the same as the node we are looking at
75   def test_map_inclusive
76     node = create(:node, :lat => 7, :lon => 7)
77     tag = create(:node_tag, :node => node)
78     way1 = create(:way_node, :node => node).way
79     way2 = create(:way_node, :node => node).way
80     relation = create(:relation_member, :member => node).relation
81
82     bbox = "#{node.lon},#{node.lat},#{node.lon},#{node.lat}"
83     get :map, :params => { :bbox => bbox }
84     assert_response :success, "The map call should have succeeded"
85     assert_select "osm[version='#{API_VERSION}'][generator='#{GENERATOR}']", :count => 1 do
86       assert_select "bounds[minlon='#{node.lon}'][minlat='#{node.lat}'][maxlon='#{node.lon}'][maxlat='#{node.lat}']", :count => 1
87       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
88         # This should really be more generic
89         assert_select "tag[k='#{tag.k}'][v='#{tag.v}']"
90       end
91       assert_select "way", :count => 2
92       assert_select "way[id='#{way1.id}']", :count => 1
93       assert_select "way[id='#{way2.id}']", :count => 1
94       assert_select "relation", :count => 1
95       assert_select "relation[id='#{relation.id}']", :count => 1
96     end
97   end
98
99   def test_map_complete_way
100     node = create(:node, :lat => 7, :lon => 7)
101     # create a couple of nodes well outside of the bbox
102     node2 = create(:node, :lat => 45, :lon => 45)
103     node3 = create(:node, :lat => 10, :lon => 10)
104     way1 = create(:way_node, :node => node).way
105     create(:way_node, :way => way1, :node => node2, :sequence_id => 2)
106     way2 = create(:way_node, :node => node).way
107     create(:way_node, :way => way2, :node => node3, :sequence_id => 2)
108     relation = create(:relation_member, :member => way1).relation
109
110     bbox = "#{node.lon},#{node.lat},#{node.lon},#{node.lat}"
111     get :map, :params => { :bbox => bbox }
112     assert_response :success, "The map call should have succeeded"
113     assert_select "osm[version='#{API_VERSION}'][generator='#{GENERATOR}']", :count => 1 do
114       assert_select "bounds[minlon='#{node.lon}'][minlat='#{node.lat}'][maxlon='#{node.lon}'][maxlat='#{node.lat}']", :count => 1
115       assert_select "node", :count => 3
116       assert_select "node[id='#{node.id}']", :count => 1
117       assert_select "node[id='#{node2.id}']", :count => 1
118       assert_select "node[id='#{node3.id}']", :count => 1
119       assert_select "way", :count => 2
120       assert_select "way[id='#{way1.id}']", :count => 1
121       assert_select "way[id='#{way2.id}']", :count => 1
122       assert_select "relation", :count => 1
123       assert_select "relation[id='#{relation.id}']", :count => 1
124     end
125   end
126
127   def test_map_empty
128     get :map, :params => { :bbox => "179.998,89.998,179.999.1,89.999" }
129     assert_response :success, "The map call should have succeeded"
130     assert_select "osm[version='#{API_VERSION}'][generator='#{GENERATOR}']", :count => 1 do
131       assert_select "bounds[minlon='179.9980000'][minlat='89.9980000'][maxlon='179.9990000'][maxlat='89.9990000']", :count => 1
132       assert_select "node", :count => 0
133       assert_select "way", :count => 0
134       assert_select "relation", :count => 0
135     end
136   end
137
138   def test_map_without_bbox
139     get :map
140     assert_response :bad_request
141     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"
142   end
143
144   def test_bbox_too_big
145     @badbigbbox.each do |bbox|
146       get :map, :params => { :bbox => bbox }
147       assert_response :bad_request, "The bbox:#{bbox} was expected to be too big"
148       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}"
149     end
150   end
151
152   def test_bbox_malformed
153     @badmalformedbbox.each do |bbox|
154       get :map, :params => { :bbox => bbox }
155       assert_response :bad_request, "The bbox:#{bbox} was expected to be malformed"
156       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}"
157     end
158   end
159
160   def test_bbox_lon_mixedup
161     @badlonmixedbbox.each do |bbox|
162       get :map, :params => { :bbox => bbox }
163       assert_response :bad_request, "The bbox:#{bbox} was expected to have the longitude mixed up"
164       assert_equal "The minimum longitude must be less than the maximum longitude, but it wasn't", @response.body, "bbox: #{bbox}"
165     end
166   end
167
168   def test_bbox_lat_mixedup
169     @badlatmixedbbox.each do |bbox|
170       get :map, :params => { :bbox => bbox }
171       assert_response :bad_request, "The bbox:#{bbox} was expected to have the latitude mixed up"
172       assert_equal "The minimum latitude must be less than the maximum latitude, but it wasn't", @response.body, "bbox: #{bbox}"
173     end
174   end
175
176   # We can't actually get an out of bounds error, as the bbox is sanitised.
177   # def test_latlon_outofbounds
178   #  @badlatlonoutboundsbbox.each do |bbox|
179   #    [ "trackpoints", "map" ].each do |tq|
180   #      get tq, :bbox => bbox
181   #      #print @request.to_yaml
182   #      assert_response :bad_request, "The bbox #{bbox} was expected to be out of range"
183   #      assert_equal "The latitudes must be between -90 an 90, and longitudes between -180 and 180", @response.body, "bbox: #{bbox}"
184   #    end
185   #  end
186   # end
187
188   # MySQL and Postgres require that the C based functions are installed for
189   # this test to work. More information is available from:
190   # http://wiki.openstreetmap.org/wiki/Rails#Installing_the_quadtile_functions
191   # or by looking at the readme in db/README
192   def test_changes_simple
193     # create a selection of nodes
194     (1..5).each do |n|
195       create(:node, :timestamp => Time.utc(2007, 1, 1, 0, 0, 0), :lat => n, :lon => n)
196     end
197     # deleted nodes should also be counted
198     create(:node, :deleted, :timestamp => Time.utc(2007, 1, 1, 0, 0, 0), :lat => 6, :lon => 6)
199     # nodes in the same tile won't change the total
200     create(:node, :timestamp => Time.utc(2007, 1, 1, 0, 0, 0), :lat => 6, :lon => 6)
201     # nodes with a different timestamp should be ignored
202     create(:node, :timestamp => Time.utc(2008, 1, 1, 0, 0, 0), :lat => 7, :lon => 7)
203
204     travel_to Time.utc(2010, 4, 3, 10, 55, 0) do
205       get :changes
206       assert_response :success
207       now = Time.now.getutc
208       hourago = now - 1.hour
209       assert_select "osm[version='#{API_VERSION}'][generator='#{GENERATOR}']", :count => 1 do
210         assert_select "changes[starttime='#{hourago.xmlschema}'][endtime='#{now.xmlschema}']", :count => 1 do
211           assert_select "tile", :count => 0
212         end
213       end
214     end
215
216     travel_to Time.utc(2007, 1, 1, 0, 30, 0) do
217       get :changes
218       assert_response :success
219       # print @response.body
220       # As we have loaded the fixtures, we can assume that there are some
221       # changes at the time we have frozen at
222       now = Time.now.getutc
223       hourago = now - 1.hour
224       assert_select "osm[version='#{API_VERSION}'][generator='#{GENERATOR}']", :count => 1 do
225         assert_select "changes[starttime='#{hourago.xmlschema}'][endtime='#{now.xmlschema}']", :count => 1 do
226           assert_select "tile", :count => 6
227         end
228       end
229     end
230   end
231
232   def test_changes_zoom_invalid
233     zoom_to_test = %w[p -1 0 17 one two]
234     zoom_to_test.each do |zoom|
235       get :changes, :params => { :zoom => zoom }
236       assert_response :bad_request
237       assert_equal @response.body, "Requested zoom is invalid, or the supplied start is after the end time, or the start duration is more than 24 hours"
238     end
239   end
240
241   def test_changes_zoom_valid
242     1.upto(16) do |zoom|
243       get :changes, :params => { :zoom => zoom }
244       assert_response :success
245       # NOTE: there was a test here for the timing, but it was too sensitive to be a good test
246       # and it was annoying.
247       assert_select "osm[version='#{API_VERSION}'][generator='#{GENERATOR}']", :count => 1 do
248         assert_select "changes", :count => 1
249       end
250     end
251   end
252
253   def test_changes_hours_invalid
254     invalid = %w[-21 335 -1 0 25 26 100 one two three ping pong :]
255     invalid.each do |hour|
256       get :changes, :params => { :hours => hour }
257       assert_response :bad_request, "Problem with the hour: #{hour}"
258       assert_equal @response.body, "Requested zoom is invalid, or the supplied start is after the end time, or the start duration is more than 24 hours", "Problem with the hour: #{hour}."
259     end
260   end
261
262   def test_changes_hours_valid
263     1.upto(24) do |hour|
264       get :changes, :params => { :hours => hour }
265       assert_response :success
266     end
267   end
268
269   def test_changes_start_end_invalid
270     get :changes, :params => { :start => "2010-04-03 10:55:00", :end => "2010-04-03 09:55:00" }
271     assert_response :bad_request
272     assert_equal @response.body, "Requested zoom is invalid, or the supplied start is after the end time, or the start duration is more than 24 hours"
273   end
274
275   def test_changes_start_end_valid
276     get :changes, :params => { :start => "2010-04-03 09:55:00", :end => "2010-04-03 10:55:00" }
277     assert_response :success
278   end
279
280   def test_permissions_anonymous
281     get :permissions
282     assert_response :success
283     assert_select "osm > permissions", :count => 1 do
284       assert_select "permission", :count => 0
285     end
286   end
287
288   def test_permissions_basic_auth
289     basic_authorization create(:user).email, "test"
290     get :permissions
291     assert_response :success
292     assert_select "osm > permissions", :count => 1 do
293       assert_select "permission", :count => ClientApplication.all_permissions.size
294       ClientApplication.all_permissions.each do |p|
295         assert_select "permission[name='#{p}']", :count => 1
296       end
297     end
298   end
299
300   def test_permissions_oauth
301     @request.env["oauth.token"] = AccessToken.new do |token|
302       # Just to test a few
303       token.allow_read_prefs = true
304       token.allow_write_api = true
305       token.allow_read_gpx = false
306     end
307     get :permissions
308     assert_response :success
309     assert_select "osm > permissions", :count => 1 do
310       assert_select "permission", :count => 2
311       assert_select "permission[name='allow_read_prefs']", :count => 1
312       assert_select "permission[name='allow_write_api']", :count => 1
313       assert_select "permission[name='allow_read_gpx']", :count => 0
314     end
315   end
316 end