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