]> git.openstreetmap.org Git - rails.git/blob - test/controllers/geocoder_controller_test.rb
Require a valid session token to resend a confirmation
[rails.git] / test / controllers / geocoder_controller_test.rb
1 # coding: utf-8
2
3 require "test_helper"
4 require "geocoder_controller"
5
6 class GeocoderControllerTest < ActionController::TestCase
7   ##
8   # test all routes which lead to this controller
9   def test_routes
10     assert_routing(
11       { :path => "/search", :method => :get },
12       { :controller => "geocoder", :action => "search" }
13     )
14     assert_routing(
15       { :path => "/geocoder/search_latlon", :method => :get },
16       { :controller => "geocoder", :action => "search_latlon" }
17     )
18     assert_routing(
19       { :path => "/geocoder/search_us_postcode", :method => :get },
20       { :controller => "geocoder", :action => "search_us_postcode" }
21     )
22     assert_routing(
23       { :path => "/geocoder/search_uk_postcode", :method => :get },
24       { :controller => "geocoder", :action => "search_uk_postcode" }
25     )
26     assert_routing(
27       { :path => "/geocoder/search_ca_postcode", :method => :get },
28       { :controller => "geocoder", :action => "search_ca_postcode" }
29     )
30     assert_routing(
31       { :path => "/geocoder/search_osm_nominatim", :method => :get },
32       { :controller => "geocoder", :action => "search_osm_nominatim" }
33     )
34     assert_routing(
35       { :path => "/geocoder/search_geonames", :method => :get },
36       { :controller => "geocoder", :action => "search_geonames" }
37     )
38     assert_routing(
39       { :path => "/geocoder/search_osm_nominatim_reverse", :method => :get },
40       { :controller => "geocoder", :action => "search_osm_nominatim_reverse" }
41     )
42     assert_routing(
43       { :path => "/geocoder/search_geonames_reverse", :method => :get },
44       { :controller => "geocoder", :action => "search_geonames_reverse" }
45     )
46   end
47
48   ##
49   # Test identification of basic lat/lon pairs
50   def test_identify_latlon_basic
51     [
52       "50.06773 14.37742",
53       "50.06773, 14.37742",
54       "+50.06773 +14.37742",
55       "+50.06773, +14.37742"
56     ].each do |code|
57       latlon_check code, 50.06773, 14.37742
58     end
59   end
60
61   ##
62   # Test identification of lat/lon pairs using N/E with degrees
63   def test_identify_latlon_ne_d
64     [
65       "N50.06773 E14.37742",
66       "N50.06773, E14.37742",
67       "50.06773N 14.37742E",
68       "50.06773N, 14.37742E"
69     ].each do |code|
70       latlon_check code, 50.06773, 14.37742
71     end
72   end
73
74   ##
75   # Test identification of lat/lon pairs using N/W with degrees
76   def test_identify_latlon_nw_d
77     [
78       "N50.06773 W14.37742",
79       "N50.06773, W14.37742",
80       "50.06773N 14.37742W",
81       "50.06773N, 14.37742W"
82     ].each do |code|
83       latlon_check code, 50.06773, -14.37742
84     end
85   end
86
87   ##
88   # Test identification of lat/lon pairs using S/E with degrees
89   def test_identify_latlon_se_d
90     [
91       "S50.06773 E14.37742",
92       "S50.06773, E14.37742",
93       "50.06773S 14.37742E",
94       "50.06773S, 14.37742E"
95     ].each do |code|
96       latlon_check code, -50.06773, 14.37742
97     end
98   end
99
100   ##
101   # Test identification of lat/lon pairs using S/W with degrees
102   def test_identify_latlon_sw_d
103     [
104       "S50.06773 W14.37742",
105       "S50.06773, W14.37742",
106       "50.06773S 14.37742W",
107       "50.06773S, 14.37742W"
108     ].each do |code|
109       latlon_check code, -50.06773, -14.37742
110     end
111   end
112
113   ##
114   # Test identification of lat/lon pairs using N/E with degrees/mins
115   def test_identify_latlon_ne_dm
116     [
117       "N 50° 04.064 E 014° 22.645",
118       "N 50° 04.064' E 014° 22.645",
119       "N 50° 04.064', E 014° 22.645'",
120       "N50° 04.064 E14° 22.645",
121       "N 50 04.064 E 014 22.645",
122       "N50 4.064 E14 22.645",
123       "50° 04.064' N, 014° 22.645' E"
124     ].each do |code|
125       latlon_check code, 50.06773, 14.37742
126     end
127   end
128
129   ##
130   # Test identification of lat/lon pairs using N/W with degrees/mins
131   def test_identify_latlon_nw_dm
132     [
133       "N 50° 04.064 W 014° 22.645",
134       "N 50° 04.064' W 014° 22.645",
135       "N 50° 04.064', W 014° 22.645'",
136       "N50° 04.064 W14° 22.645",
137       "N 50 04.064 W 014 22.645",
138       "N50 4.064 W14 22.645",
139       "50° 04.064' N, 014° 22.645' W"
140     ].each do |code|
141       latlon_check code, 50.06773, -14.37742
142     end
143   end
144
145   ##
146   # Test identification of lat/lon pairs using S/E with degrees/mins
147   def test_identify_latlon_se_dm
148     [
149       "S 50° 04.064 E 014° 22.645",
150       "S 50° 04.064' E 014° 22.645",
151       "S 50° 04.064', E 014° 22.645'",
152       "S50° 04.064 E14° 22.645",
153       "S 50 04.064 E 014 22.645",
154       "S50 4.064 E14 22.645",
155       "50° 04.064' S, 014° 22.645' E"
156     ].each do |code|
157       latlon_check code, -50.06773, 14.37742
158     end
159   end
160
161   ##
162   # Test identification of lat/lon pairs using S/W with degrees/mins
163   def test_identify_latlon_sw_dm
164     [
165       "S 50° 04.064 W 014° 22.645",
166       "S 50° 04.064' W 014° 22.645",
167       "S 50° 04.064', W 014° 22.645'",
168       "S50° 04.064 W14° 22.645",
169       "S 50 04.064 W 014 22.645",
170       "S50 4.064 W14 22.645",
171       "50° 04.064' S, 014° 22.645' W"
172     ].each do |code|
173       latlon_check code, -50.06773, -14.37742
174     end
175   end
176
177   ##
178   # Test identification of lat/lon pairs using N/E with degrees/mins/secs
179   def test_identify_latlon_ne_dms
180     [
181       "N 50° 4' 03.828\" E 14° 22' 38.712\"",
182       "N 50° 4' 03.828\", E 14° 22' 38.712\"",
183       "N 50° 4′ 03.828″, E 14° 22′ 38.712″",
184       "N50 4 03.828 E14 22 38.712",
185       "N50 4 03.828, E14 22 38.712",
186       "50°4'3.828\"N 14°22'38.712\"E"
187     ].each do |code|
188       latlon_check code, 50.06773, 14.37742
189     end
190   end
191
192   ##
193   # Test identification of lat/lon pairs using N/W with degrees/mins/secs
194   def test_identify_latlon_nw_dms
195     [
196       "N 50° 4' 03.828\" W 14° 22' 38.712\"",
197       "N 50° 4' 03.828\", W 14° 22' 38.712\"",
198       "N 50° 4′ 03.828″, W 14° 22′ 38.712″",
199       "N50 4 03.828 W14 22 38.712",
200       "N50 4 03.828, W14 22 38.712",
201       "50°4'3.828\"N 14°22'38.712\"W"
202     ].each do |code|
203       latlon_check code, 50.06773, -14.37742
204     end
205   end
206
207   ##
208   # Test identification of lat/lon pairs using S/E with degrees/mins/secs
209   def test_identify_latlon_se_dms
210     [
211       "S 50° 4' 03.828\" E 14° 22' 38.712\"",
212       "S 50° 4' 03.828\", E 14° 22' 38.712\"",
213       "S 50° 4′ 03.828″, E 14° 22′ 38.712″",
214       "S50 4 03.828 E14 22 38.712",
215       "S50 4 03.828, E14 22 38.712",
216       "50°4'3.828\"S 14°22'38.712\"E"
217     ].each do |code|
218       latlon_check code, -50.06773, 14.37742
219     end
220   end
221
222   ##
223   # Test identification of lat/lon pairs using S/W with degrees/mins/secs
224   def test_identify_latlon_sw_dms
225     [
226       "S 50° 4' 03.828\" W 14° 22' 38.712\"",
227       "S 50° 4' 03.828\", W 14° 22' 38.712\"",
228       "S 50° 4′ 03.828″, W 14° 22′ 38.712″",
229       "S50 4 03.828 W14 22 38.712",
230       "S50 4 03.828, W14 22 38.712",
231       "50°4'3.828\"S 14°22'38.712\"W"
232     ].each do |code|
233       latlon_check code, -50.06773, -14.37742
234     end
235   end
236
237   ##
238   # Test identification of US zipcodes
239   def test_identify_us_postcode
240     [
241       "12345",
242       "12345-6789"
243     ].each do |code|
244       post :search, :query => code
245       assert_response :success
246       assert_equal %w(us_postcode osm_nominatim), assigns(:sources)
247     end
248   end
249
250   ##
251   # Test identification of UK postcodes using examples from
252   # http://en.wikipedia.org/wiki/Postcodes_in_the_United_Kingdom
253   def test_identify_uk_postcode
254     [
255       "EC1A 1BB",
256       "W1A 1HQ",
257       "M1 1AA",
258       "B33 8TH",
259       "CR2 6XH",
260       "DN55 1PT"
261     ].each do |code|
262       search_check code, %w(uk_postcode osm_nominatim)
263     end
264   end
265
266   ##
267   # Test identification of Canadian postcodes
268   def test_identify_ca_postcode
269     search_check "A1B 2C3", %w(ca_postcode osm_nominatim)
270   end
271
272   ##
273   # Test identification fall through to the default case
274   def test_identify_default
275     search_check "foo bar baz", %w(osm_nominatim geonames)
276   end
277
278   ##
279   # Test the builtin latitude+longitude search
280   def test_search_latlon
281     xhr :get, :search_latlon, :lat => 1.23, :lon => 4.56, :zoom => 16
282     results_check :name => "1.23, 4.56", :lat => 1.23, :lon => 4.56, :zoom => 16
283
284     xhr :get, :search_latlon, :lat => -91.23, :lon => 4.56, :zoom => 16
285     results_check_error "Latitude -91.23 out of range"
286
287     xhr :get, :search_latlon, :lat => 91.23, :lon => 4.56, :zoom => 16
288     results_check_error "Latitude 91.23 out of range"
289
290     xhr :get, :search_latlon, :lat => 1.23, :lon => -180.23, :zoom => 16
291     results_check_error "Longitude -180.23 out of range"
292
293     xhr :get, :search_latlon, :lat => 1.23, :lon => 180.23, :zoom => 16
294     results_check_error "Longitude 180.23 out of range"
295   end
296
297   ##
298   # Test the US postcode search
299   def test_search_us_postcode
300     with_http_stubs "geocoder_us" do
301       xhr :get, :search_us_postcode,
302           :query => "90210", :zoom => 10,
303           :minlon => -0.559, :minlat => 51.217,
304           :maxlon => 0.836, :maxlat => 51.766
305       results_check :prefix => "Beverly Hills, CA,", :name => "90210",
306                     :lat => 34.088808, :lon => -118.40612
307
308       xhr :get, :search_us_postcode,
309           :query => "00000", :zoom => 10,
310           :minlon => -0.559, :minlat => 51.217,
311           :maxlon => 0.836, :maxlat => 51.766
312       results_check
313     end
314   end
315
316   ##
317   # Test the UK postcode search
318   def test_search_uk_postcode
319     with_http_stubs "npemap" do
320       xhr :get, :search_uk_postcode,
321           :query => "CV4 7AL", :zoom => 10,
322           :minlon => -0.559, :minlat => 51.217,
323           :maxlon => 0.836, :maxlat => 51.766
324       results_check :name => "CV4 7AL", :lat => 52.381748701968, :lon => -1.56176420939232
325
326       xhr :get, :search_uk_postcode,
327           :query => "XX9 9XX", :zoom => 10,
328           :minlon => -0.559, :minlat => 51.217,
329           :maxlon => 0.836, :maxlat => 51.766
330       results_check
331     end
332   end
333
334   ##
335   # Test the Canadian postcode search
336   def test_search_ca_postcode
337     with_http_stubs "geocoder_ca" do
338       xhr :get, :search_ca_postcode,
339           :query => "A1B 2C3", :zoom => 10,
340           :minlon => -0.559, :minlat => 51.217,
341           :maxlon => 0.836, :maxlat => 51.766
342       results_check :name => "A1B 2C3", :lat => "47.172520", :lon => "-55.440515"
343
344       xhr :get, :search_ca_postcode,
345           :query => "k1a 0b1", :zoom => 10,
346           :minlon => -0.559, :minlat => 51.217,
347           :maxlon => 0.836, :maxlat => 51.766
348       results_check :name => "K1A 0B1", :lat => "45.375437", :lon => "-75.691041"
349
350       xhr :get, :search_ca_postcode,
351           :query => "Q0Q 0Q0", :zoom => 10,
352           :minlon => -0.559, :minlat => 51.217,
353           :maxlon => 0.836, :maxlat => 51.766
354       results_check
355     end
356   end
357
358   ##
359   # Test the nominatim forward search
360   def test_search_osm_nominatim
361     with_http_stubs "nominatim" do
362       xhr :get, :search_osm_nominatim,
363           :query => "Hoddesdon", :zoom => 10,
364           :minlon => -0.559, :minlat => 51.217,
365           :maxlon => 0.836, :maxlat => 51.766
366       results_check "name" => "Hoddesdon, Hertfordshire, East of England, England, United Kingdom",
367                     "min-lat" => 51.7216709, "max-lat" => 51.8016709,
368                     "min-lon" => -0.0512898, "max-lon" => 0.0287102,
369                     "type" => "node", "id" => 18007599
370
371       xhr :get, :search_osm_nominatim,
372           :query => "Broxbourne", :zoom => 10,
373           :minlon => -0.559, :minlat => 51.217,
374           :maxlon => 0.836, :maxlat => 51.766
375       results_check({ "prefix" => "Suburb",
376                       "name" => "Broxbourne, Hertfordshire, East of England, England, United Kingdom",
377                       "min-lat" => 51.7265723, "max-lat" => 51.7665723,
378                       "min-lon" => -0.0390782, "max-lon" => 0.0009218,
379                       "type" => "node", "id" => 28825933 },
380                     { "prefix" => "City Boundary",
381                       "name" => "Broxbourne, Hertfordshire, East of England, England, United Kingdom",
382                       "min-lat" => 51.6808751, "max-lat" => 51.7806237,
383                       "min-lon" => -0.114204, "max-lon" => 0.0145267,
384                       "type" => "relation", "id" => 2677978 },
385                     { "prefix" => "Railway Station",
386                       "name" => "Broxbourne, Stafford Drive, Broxbourne, Hertfordshire, East of England, England, United Kingdom",
387                       "min-lat" => 51.7418469, "max-lat" => 51.7518469,
388                       "min-lon" => -0.0156773, "max-lon" => -0.0056773,
389                       "type" => "node", "id" => 17044599 })
390     end
391   end
392
393   ##
394   # Test the geonames forward search
395   def test_search_geonames
396     with_http_stubs "geonames" do
397       xhr :get, :search_geonames,
398           :query => "Hoddesdon", :zoom => 10,
399           :minlon => -0.559, :minlat => 51.217,
400           :maxlon => 0.836, :maxlat => 51.766
401       results_check :name => "Hoddesdon", :lat => 51.76148, :lon => -0.01144
402
403       xhr :get, :search_geonames,
404           :query => "Broxbourne", :zoom => 10,
405           :minlon => -0.559, :minlat => 51.217,
406           :maxlon => 0.836, :maxlat => 51.766
407       results_check({ :name => "Broxbourne", :lat => 51.74712, :lon => -0.01923 },
408                     { :name => "Broxbourne District", :lat => 51.73026, :lon => -0.04821 },
409                     { :name => "Cheshunt", :lat => 51.70791, :lon => -0.03739 },
410                     { :name => "Hoddesdon", :lat => 51.76148, :lon => -0.01144 },
411                     { :name => "Waltham Cross", :lat => 51.68905, :lon => -0.0333 },
412                     { :name => "Goffs Oak", :lat => 51.71015, :lon => -0.0872 },
413                     { :name => "Wormley", :lat => 51.7324, :lon => -0.0242 },
414                     { :name => "Broxbourne", :lat => -27.50314, :lon => 151.378 },
415                     { :name => "Lee Valley White Water Centre", :lat => 51.68814, :lon => -0.01682 },
416                     { :name => "Cheshunt Railway Station", :lat => 51.703, :lon => -0.024 },
417                     { :name => "Theobalds Grove Railway Station", :lat => 51.692, :lon => -0.035 },
418                     { :name => "Waltham Cross Railway Station", :lat => 51.685, :lon => -0.027 },
419                     { :name => "Rye House Station", :lat => 51.76938, :lon => 0.00562 },
420                     { :name => "Broxbourne Station", :lat => 51.74697, :lon => -0.01105 },
421                     { :name => "Broxbornebury Park", :lat => 51.75252, :lon => -0.03839 },
422                     { :name => "Marriott Cheshunt", :lat => 51.7208, :lon => -0.0324 },
423                     { :name => "Cheshunt Community Hospital", :lat => 51.68396, :lon => -0.03951 })
424     end
425   end
426
427   ##
428   # Test the nominatim reverse search
429   def test_search_osm_nominatim_reverse
430     with_http_stubs "nominatim" do
431       xhr :get, :search_osm_nominatim_reverse, :lat => 51.7632, :lon => -0.0076, :zoom => 15
432       results_check :name => "Broxbourne, Hertfordshire, East of England, England, United Kingdom",
433                     :lat => 51.7465723, :lon => -0.0190782,
434                     :type => "node", :id => 28825933, :zoom => 15
435
436       xhr :get, :search_osm_nominatim_reverse, :lat => 51.7632, :lon => -0.0076, :zoom => 17
437       results_check :name => "Dinant Link Road, Broxbourne, Hertfordshire, East of England, England, EN11 8HX, United Kingdom",
438                     :lat => 51.7634883, :lon => -0.0088373,
439                     :type => "way", :id => 3489841, :zoom  => 17
440     end
441   end
442
443   ##
444   # Test the geonames reverse search
445   def test_search_geonames_reverse
446     with_http_stubs "geonames" do
447       xhr :get, :search_geonames_reverse, :lat => 51.7632, :lon => -0.0076, :zoom => 15
448       results_check :name => "England", :suffix => ", United Kingdom",
449                     :lat => 51.7632, :lon => -0.0076
450     end
451   end
452
453   private
454
455   def latlon_check(query, lat, lon)
456     get :search, :query => query
457     assert_response :success
458     assert_template :search
459     assert_template :layout => "map"
460     assert_equal %w(latlon osm_nominatim_reverse geonames_reverse), assigns(:sources)
461     assert_nil @controller.params[:query]
462     assert_in_delta lat, @controller.params[:lat]
463     assert_in_delta lon, @controller.params[:lon]
464
465     xhr :get, :search, :query => query
466     assert_response :success
467     assert_template :search
468     assert_template :layout => "xhr"
469     assert_equal %w(latlon osm_nominatim_reverse geonames_reverse), assigns(:sources)
470     assert_nil @controller.params[:query]
471     assert_in_delta lat, @controller.params[:lat]
472     assert_in_delta lon, @controller.params[:lon]
473   end
474
475   def search_check(query, sources)
476     get :search, :query => query
477     assert_response :success
478     assert_template :search
479     assert_template :layout => "map"
480     assert_equal sources, assigns(:sources)
481
482     xhr :get, :search, :query => query
483     assert_response :success
484     assert_template :search
485     assert_template :layout => "xhr"
486     assert_equal sources, assigns(:sources)
487   end
488
489   def results_check(*results)
490     assert_response :success
491     assert_template :results
492     assert_template :layout => nil
493     if results.empty?
494       assert_select "ul.results-list", 0
495     else
496       assert_select "ul.results-list", 1 do
497         assert_select "p.search_results_entry", results.count
498
499         results.each do |result|
500           attrs = result.collect { |k, v| "[data-#{k}='#{v}']" }.join("")
501           assert_select "p.search_results_entry a.set_position#{attrs}", result[:name]
502         end
503       end
504     end
505   end
506
507   def results_check_error(error)
508     assert_response :success
509     assert_template :error
510     assert_template :layout => nil
511     assert_select "p.search_results_error", error
512   end
513 end