]> git.openstreetmap.org Git - rails.git/blob - test/controllers/api_controller_test.rb
Refactor a few tests to use changeset factories.
[rails.git] / test / controllers / api_controller_test.rb
1 require "test_helper"
2 require "api_controller"
3
4 class ApiControllerTest < ActionController::TestCase
5   api_fixtures
6
7   def setup
8     super
9     @badbigbbox = %w(-0.1,-0.1,1.1,1.1 10,10,11,11)
10     @badmalformedbbox = %w(-0.1 hello
11                            10N2W10.1N2.1W)
12     @badlatmixedbbox = %w(0,0.1,0.1,0 -0.1,80,0.1,70 0.24,54.34,0.25,54.33)
13     @badlonmixedbbox = %w(80,-0.1,70,0.1 54.34,0.24,54.33,0.25)
14     # @badlatlonoutboundsbbox = %w{ 191,-0.1,193,0.1  -190.1,89.9,-190,90 }
15     @goodbbox = %w(-0.1,-0.1,0.1,0.1 51.1,-0.1,51.2,0
16                    -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)
17     # That last item in the goodbbox really shouldn't be there, as the API should
18     # reall reject it, however this is to test to see if the api changes.
19   end
20
21   ##
22   # test all routes which lead to this controller
23   def test_routes
24     assert_routing(
25       { :path => "/api/capabilities", :method => :get },
26       { :controller => "api", :action => "capabilities" }
27     )
28     assert_recognizes(
29       { :controller => "api", :action => "capabilities" },
30       { :path => "/api/0.6/capabilities", :method => :get }
31     )
32     assert_routing(
33       { :path => "/api/0.6/permissions", :method => :get },
34       { :controller => "api", :action => "permissions" }
35     )
36     assert_routing(
37       { :path => "/api/0.6/map", :method => :get },
38       { :controller => "api", :action => "map" }
39     )
40     assert_routing(
41       { :path => "/api/0.6/trackpoints", :method => :get },
42       { :controller => "api", :action => "trackpoints" }
43     )
44     assert_routing(
45       { :path => "/api/0.6/changes", :method => :get },
46       { :controller => "api", :action => "changes" }
47     )
48   end
49
50   # -------------------------------------
51   # Test reading a bounding box.
52   # -------------------------------------
53
54   def test_map
55     node = create(:node, :lat => 7, :lon => 7)
56     tag = create(:node_tag, :node => node)
57     way1 = create(:way_node, :node => node).way
58     way2 = create(:way_node, :node => node).way
59     relation = create(:relation_member, :member => node).relation
60
61     # Need to split the min/max lat/lon out into their own variables here
62     # so that we can test they are returned later.
63     minlon = node.lon - 0.1
64     minlat = node.lat - 0.1
65     maxlon = node.lon + 0.1
66     maxlat = node.lat + 0.1
67     bbox = "#{minlon},#{minlat},#{maxlon},#{maxlat}"
68     get :map, :bbox => bbox
69     if $VERBOSE
70       print @request.to_yaml
71       print @response.body
72     end
73     assert_response :success, "Expected scucess with the map call"
74     assert_select "osm[version='#{API_VERSION}'][generator='#{GENERATOR}']", :count => 1 do
75       assert_select "bounds[minlon='#{minlon}'][minlat='#{minlat}'][maxlon='#{maxlon}'][maxlat='#{maxlat}']", :count => 1
76       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
77         # This should really be more generic
78         assert_select "tag[k='#{tag.k}'][v='#{tag.v}']"
79       end
80       assert_select "way", :count => 2
81       assert_select "way[id='#{way1.id}']", :count => 1
82       assert_select "way[id='#{way2.id}']", :count => 1
83       assert_select "relation", :count => 1
84       assert_select "relation[id='#{relation.id}']", :count => 1
85     end
86   end
87
88   # This differs from the above test in that we are making the bbox exactly
89   # the same as the node we are looking at
90   def test_map_inclusive
91     node = create(:node, :lat => 7, :lon => 7)
92     tag = create(:node_tag, :node => node)
93     way1 = create(:way_node, :node => node).way
94     way2 = create(:way_node, :node => node).way
95     relation = create(:relation_member, :member => node).relation
96
97     bbox = "#{node.lon},#{node.lat},#{node.lon},#{node.lat}"
98     get :map, :bbox => bbox
99     assert_response :success, "The map call should have succeeded"
100     assert_select "osm[version='#{API_VERSION}'][generator='#{GENERATOR}']", :count => 1 do
101       assert_select "bounds[minlon='#{node.lon}'][minlat='#{node.lat}'][maxlon='#{node.lon}'][maxlat='#{node.lat}']", :count => 1
102       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
103         # This should really be more generic
104         assert_select "tag[k='#{tag.k}'][v='#{tag.v}']"
105       end
106       assert_select "way", :count => 2
107       assert_select "way[id='#{way1.id}']", :count => 1
108       assert_select "way[id='#{way2.id}']", :count => 1
109       assert_select "relation", :count => 1
110       assert_select "relation[id='#{relation.id}']", :count => 1
111     end
112   end
113
114   def test_map_complete_way
115     node = create(:node, :lat => 7, :lon => 7)
116     # create a couple of nodes well outside of the bbox
117     node2 = create(:node, :lat => 45, :lon => 45)
118     node3 = create(:node, :lat => 10, :lon => 10)
119     way1 = create(:way_node, :node => node).way
120     create(:way_node, :way => way1, :node => node2, :sequence_id => 2)
121     way2 = create(:way_node, :node => node).way
122     create(:way_node, :way => way2, :node => node3, :sequence_id => 2)
123     relation = create(:relation_member, :member => way1).relation
124
125     bbox = "#{node.lon},#{node.lat},#{node.lon},#{node.lat}"
126     get :map, :bbox => bbox
127     assert_response :success, "The map call should have succeeded"
128     assert_select "osm[version='#{API_VERSION}'][generator='#{GENERATOR}']", :count => 1 do
129       assert_select "bounds[minlon='#{node.lon}'][minlat='#{node.lat}'][maxlon='#{node.lon}'][maxlat='#{node.lat}']", :count => 1
130       assert_select "node", :count => 3
131       assert_select "node[id='#{node.id}']", :count => 1
132       assert_select "node[id='#{node2.id}']", :count => 1
133       assert_select "node[id='#{node3.id}']", :count => 1
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
139     end
140   end
141
142   def test_map_empty
143     get :map, :bbox => "179.998,89.998,179.999.1,89.999"
144     assert_response :success, "The map call should have succeeded"
145     assert_select "osm[version='#{API_VERSION}'][generator='#{GENERATOR}']", :count => 1 do
146       assert_select "bounds[minlon='179.998'][minlat='89.998'][maxlon='179.999'][maxlat='89.999']", :count => 1
147       assert_select "node", :count => 0
148       assert_select "way", :count => 0
149       assert_select "relation", :count => 0
150     end
151   end
152
153   def test_tracepoints
154     point = create(:trace, :visibility => "public", :latitude => 1, :longitude => 1) do |trace|
155       create(:tracepoint, :trace => trace, :latitude => 1 * GeoRecord::SCALE, :longitude => 1 * GeoRecord::SCALE)
156     end
157     minlon = point.longitude - 0.001
158     minlat = point.latitude - 0.001
159     maxlon = point.longitude + 0.001
160     maxlat = point.latitude + 0.001
161     bbox = "#{minlon},#{minlat},#{maxlon},#{maxlat}"
162     get :trackpoints, :bbox => bbox
163     assert_response :success
164     assert_select "gpx[version='1.0'][creator='OpenStreetMap.org']", :count => 1 do
165       assert_select "trk" do
166         assert_select "trkseg"
167       end
168     end
169   end
170
171   def test_tracepoints_trackable
172     point = create(:trace, :visibility => "trackable", :latitude => 51.51, :longitude => -0.14) do |trace|
173       create(:tracepoint, :trace => trace, :trackid => 1, :latitude => (51.510 * GeoRecord::SCALE).to_i, :longitude => (-0.140 * GeoRecord::SCALE).to_i)
174       create(:tracepoint, :trace => trace, :trackid => 2, :latitude => (51.511 * GeoRecord::SCALE).to_i, :longitude => (-0.141 * GeoRecord::SCALE).to_i)
175     end
176     minlon = point.longitude - 0.002
177     minlat = point.latitude - 0.002
178     maxlon = point.longitude + 0.002
179     maxlat = point.latitude + 0.002
180     bbox = "#{minlon},#{minlat},#{maxlon},#{maxlat}"
181     get :trackpoints, :bbox => bbox
182     assert_response :success
183     assert_select "gpx[version='1.0'][creator='OpenStreetMap.org']", :count => 1 do
184       assert_select "trk", :count => 1 do
185         assert_select "trk > trkseg", :count => 2 do |trksegs|
186           trksegs.each do |trkseg|
187             assert_select trkseg, "trkpt", :count => 1 do |trkpt|
188               assert_select trkpt[0], "time", :count => 1
189             end
190           end
191         end
192       end
193     end
194   end
195
196   def test_tracepoints_identifiable
197     point = create(:trace, :visibility => "identifiable", :latitude => 51.512, :longitude => 0.142) do |trace|
198       create(:tracepoint, :trace => trace, :latitude => (51.512 * GeoRecord::SCALE).to_i, :longitude => (0.142 * GeoRecord::SCALE).to_i)
199     end
200     minlon = point.longitude - 0.002
201     minlat = point.latitude - 0.002
202     maxlon = point.longitude + 0.002
203     maxlat = point.latitude + 0.002
204     bbox = "#{minlon},#{minlat},#{maxlon},#{maxlat}"
205     get :trackpoints, :bbox => bbox
206     assert_response :success
207     assert_select "gpx[version='1.0'][creator='OpenStreetMap.org']", :count => 1 do
208       assert_select "trk", :count => 1 do
209         assert_select "trk>name", :count => 1
210         assert_select "trk>desc", :count => 1
211         assert_select "trk>url", :count => 1
212         assert_select "trkseg", :count => 1 do
213           assert_select "trkpt", :count => 1 do
214             assert_select "time", :count => 1
215           end
216         end
217       end
218     end
219   end
220
221   def test_map_without_bbox
222     %w(trackpoints map).each do |tq|
223       get tq
224       assert_response :bad_request
225       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"
226     end
227   end
228
229   def test_traces_page_less_than_0
230     -10.upto(-1) do |i|
231       get :trackpoints, :page => i, :bbox => "-0.1,-0.1,0.1,0.1"
232       assert_response :bad_request
233       assert_equal "Page number must be greater than or equal to 0", @response.body, "The page number was #{i}"
234     end
235     0.upto(10) do |i|
236       get :trackpoints, :page => i, :bbox => "-0.1,-0.1,0.1,0.1"
237       assert_response :success, "The page number was #{i} and should have been accepted"
238     end
239   end
240
241   def test_bbox_too_big
242     @badbigbbox.each do |bbox|
243       %w(trackpoints map).each do |tq|
244         get tq, :bbox => bbox
245         assert_response :bad_request, "The bbox:#{bbox} was expected to be too big"
246         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}"
247       end
248     end
249   end
250
251   def test_bbox_malformed
252     @badmalformedbbox.each do |bbox|
253       %w(trackpoints map).each do |tq|
254         get tq, :bbox => bbox
255         assert_response :bad_request, "The bbox:#{bbox} was expected to be malformed"
256         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}"
257       end
258     end
259   end
260
261   def test_bbox_lon_mixedup
262     @badlonmixedbbox.each do |bbox|
263       %w(trackpoints map).each do |tq|
264         get tq, :bbox => bbox
265         assert_response :bad_request, "The bbox:#{bbox} was expected to have the longitude mixed up"
266         assert_equal "The minimum longitude must be less than the maximum longitude, but it wasn't", @response.body, "bbox: #{bbox}"
267       end
268     end
269   end
270
271   def test_bbox_lat_mixedup
272     @badlatmixedbbox.each do |bbox|
273       %w(trackpoints map).each do |tq|
274         get tq, :bbox => bbox
275         assert_response :bad_request, "The bbox:#{bbox} was expected to have the latitude mixed up"
276         assert_equal "The minimum latitude must be less than the maximum latitude, but it wasn't", @response.body, "bbox: #{bbox}"
277       end
278     end
279   end
280
281   # We can't actually get an out of bounds error, as the bbox is sanitised.
282   # def test_latlon_outofbounds
283   #  @badlatlonoutboundsbbox.each do |bbox|
284   #    [ "trackpoints", "map" ].each do |tq|
285   #      get tq, :bbox => bbox
286   #      #print @request.to_yaml
287   #      assert_response :bad_request, "The bbox #{bbox} was expected to be out of range"
288   #      assert_equal "The latitudes must be between -90 an 90, and longitudes between -180 and 180", @response.body, "bbox: #{bbox}"
289   #    end
290   #  end
291   # end
292
293   # MySQL and Postgres require that the C based functions are installed for
294   # this test to work. More information is available from:
295   # http://wiki.openstreetmap.org/wiki/Rails#Installing_the_quadtile_functions
296   # or by looking at the readme in db/README
297   def test_changes_simple
298     Timecop.freeze(Time.utc(2010, 4, 3, 10, 55, 0))
299     get :changes
300     assert_response :success
301     # print @response.body
302     # As we have loaded the fixtures, we can assume that there are no
303     # changes at the time we have frozen at
304     now = Time.now.getutc
305     hourago = now - 1.hour
306     assert_select "osm[version='#{API_VERSION}'][generator='#{GENERATOR}']", :count => 1 do
307       assert_select "changes[starttime='#{hourago.xmlschema}'][endtime='#{now.xmlschema}']", :count => 1 do
308         assert_select "tile", :count => 0
309       end
310     end
311     Timecop.return
312
313     Timecop.freeze(Time.utc(2007, 1, 1, 0, 30, 0))
314     get :changes
315     assert_response :success
316     # print @response.body
317     # As we have loaded the fixtures, we can assume that there are some
318     # changes at the time we have frozen at
319     now = Time.now.getutc
320     hourago = now - 1.hour
321     assert_select "osm[version='#{API_VERSION}'][generator='#{GENERATOR}']", :count => 1 do
322       assert_select "changes[starttime='#{hourago.xmlschema}'][endtime='#{now.xmlschema}']", :count => 1 do
323         assert_select "tile", :count => 10
324       end
325     end
326     Timecop.return
327   end
328
329   def test_changes_zoom_invalid
330     zoom_to_test = %w(p -1 0 17 one two)
331     zoom_to_test.each do |zoom|
332       get :changes, :zoom => zoom
333       assert_response :bad_request
334       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"
335     end
336   end
337
338   def test_changes_zoom_valid
339     1.upto(16) do |zoom|
340       get :changes, :zoom => zoom
341       assert_response :success
342       # NOTE: there was a test here for the timing, but it was too sensitive to be a good test
343       # and it was annoying.
344       assert_select "osm[version='#{API_VERSION}'][generator='#{GENERATOR}']", :count => 1 do
345         assert_select "changes", :count => 1
346       end
347     end
348   end
349
350   def test_changes_hours_invalid
351     invalid = %w(-21 335 -1 0 25 26 100 one two three ping pong :)
352     invalid.each do |hour|
353       get :changes, :hours => hour
354       assert_response :bad_request, "Problem with the hour: #{hour}"
355       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}."
356     end
357   end
358
359   def test_changes_hours_valid
360     1.upto(24) do |hour|
361       get :changes, :hours => hour
362       assert_response :success
363     end
364   end
365
366   def test_changes_start_end_invalid
367     get :changes, :start => "2010-04-03 10:55:00", :end => "2010-04-03 09:55:00"
368     assert_response :bad_request
369     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"
370   end
371
372   def test_changes_start_end_valid
373     get :changes, :start => "2010-04-03 09:55:00", :end => "2010-04-03 10:55:00"
374     assert_response :success
375   end
376
377   def test_capabilities
378     get :capabilities
379     assert_response :success
380     assert_select "osm[version='#{API_VERSION}'][generator='#{GENERATOR}']", :count => 1 do
381       assert_select "api", :count => 1 do
382         assert_select "version[minimum='#{API_VERSION}'][maximum='#{API_VERSION}']", :count => 1
383         assert_select "area[maximum='#{MAX_REQUEST_AREA}']", :count => 1
384         assert_select "note_area[maximum='#{MAX_NOTE_REQUEST_AREA}']", :count => 1
385         assert_select "tracepoints[per_page='#{TRACEPOINTS_PER_PAGE}']", :count => 1
386         assert_select "changesets[maximum_elements='#{Changeset::MAX_ELEMENTS}']", :count => 1
387         assert_select "status[database='online']", :count => 1
388         assert_select "status[api='online']", :count => 1
389         assert_select "status[gpx='online']", :count => 1
390       end
391     end
392   end
393
394   def test_permissions_anonymous
395     get :permissions
396     assert_response :success
397     assert_select "osm > permissions", :count => 1 do
398       assert_select "permission", :count => 0
399     end
400   end
401
402   def test_permissions_basic_auth
403     basic_authorization(create(:user).email, "test")
404     get :permissions
405     assert_response :success
406     assert_select "osm > permissions", :count => 1 do
407       assert_select "permission", :count => ClientApplication.all_permissions.size
408       ClientApplication.all_permissions.each do |p|
409         assert_select "permission[name='#{p}']", :count => 1
410       end
411     end
412   end
413
414   def test_permissions_oauth
415     @request.env["oauth.token"] = AccessToken.new do |token|
416       # Just to test a few
417       token.allow_read_prefs = true
418       token.allow_write_api = true
419       token.allow_read_gpx = false
420     end
421     get :permissions
422     assert_response :success
423     assert_select "osm > permissions", :count => 1 do
424       assert_select "permission", :count => 2
425       assert_select "permission[name='allow_read_prefs']", :count => 1
426       assert_select "permission[name='allow_write_api']", :count => 1
427       assert_select "permission[name='allow_read_gpx']", :count => 0
428     end
429   end
430 end