From: Tom Hughes Date: Sun, 1 Mar 2015 23:05:18 +0000 (+0000) Subject: Testing, testing, testing... X-Git-Tag: live~4167 X-Git-Url: https://git.openstreetmap.org/rails.git/commitdiff_plain/1891efef06872740184a2abc9ecfdb9f263c9ba5 Testing, testing, testing... --- diff --git a/app/controllers/geocoder_controller.rb b/app/controllers/geocoder_controller.rb index 5a59c9eeb..11460f8c0 100644 --- a/app/controllers/geocoder_controller.rb +++ b/app/controllers/geocoder_controller.rb @@ -185,9 +185,9 @@ class GeocoderController < ApplicationController end render :action => "results" - # rescue StandardError => ex - # @error = "Error contacting nominatim.openstreetmap.org: #{ex.to_s}" - # render :action => "error" + rescue StandardError => ex + @error = "Error contacting nominatim.openstreetmap.org: #{ex}" + render :action => "error" end def search_geonames @@ -217,7 +217,7 @@ class GeocoderController < ApplicationController render :action => "results" rescue StandardError => ex - @error = "Error contacting ws.geonames.org: #{ex}" + @error = "Error contacting api.geonames.org: #{ex}" render :action => "error" end @@ -279,7 +279,7 @@ class GeocoderController < ApplicationController render :action => "results" rescue StandardError => ex - @error = "Error contacting ws.geonames.org: #{ex}" + @error = "Error contacting api.geonames.org: #{ex}" render :action => "error" end diff --git a/config/example.application.yml b/config/example.application.yml index 5fd287f7f..2e41adbfc 100644 --- a/config/example.application.yml +++ b/config/example.application.yml @@ -100,3 +100,5 @@ production: test: <<: *defaults + # Geonames credentials for testing + geonames_username: "dummy" diff --git a/test/controllers/browse_controller_test.rb b/test/controllers/browse_controller_test.rb index a84138184..72bb68937 100644 --- a/test/controllers/browse_controller_test.rb +++ b/test/controllers/browse_controller_test.rb @@ -71,6 +71,7 @@ class BrowseControllerTest < ActionController::TestCase def test_read_changeset browse_check "changeset", changesets(:normal_user_first_change).id, "browse/changeset" + browse_check "changeset", changesets(:public_user_first_change).id, "browse/changeset" end def test_read_note @@ -150,6 +151,16 @@ class BrowseControllerTest < ActionController::TestCase get type, :id => -10 # we won't have an id that's negative end + get type, :id => 0 + assert_response :not_found + assert_template "browse/not_found" + assert_template :layout => "map" + + xhr :get, type, :id => 0 + assert_response :not_found + assert_template "browse/not_found" + assert_template :layout => "xhr" + get type, :id => id assert_response :success assert_template template diff --git a/test/controllers/geocoder_controller_test.rb b/test/controllers/geocoder_controller_test.rb index 7479b3e9d..483c37dba 100644 --- a/test/controllers/geocoder_controller_test.rb +++ b/test/controllers/geocoder_controller_test.rb @@ -272,42 +272,242 @@ class GeocoderControllerTest < ActionController::TestCase ## # Test identification fall through to the default case def test_identify_default - search_check "foo bar baz", ["osm_nominatim"] + search_check "foo bar baz", %w(osm_nominatim geonames) + end + + ## + # Test the builtin latitude+longitude search + def test_search_latlon + xhr :get, :search_latlon, :lat => 1.23, :lon => 4.56, :zoom => 16 + results_check :name => "1.23, 4.56", :lat => 1.23, :lon => 4.56, :zoom => 16 + + xhr :get, :search_latlon, :lat => -91.23, :lon => 4.56, :zoom => 16 + results_check_error "Latitude -91.23 out of range" + + xhr :get, :search_latlon, :lat => 91.23, :lon => 4.56, :zoom => 16 + results_check_error "Latitude 91.23 out of range" + + xhr :get, :search_latlon, :lat => 1.23, :lon => -180.23, :zoom => 16 + results_check_error "Longitude -180.23 out of range" + + xhr :get, :search_latlon, :lat => 1.23, :lon => 180.23, :zoom => 16 + results_check_error "Longitude 180.23 out of range" + end + + ## + # Test the US postcode search + def test_search_us_postcode + with_http_stubs "geocoder_us" do + xhr :get, :search_us_postcode, + :query => "90210", :zoom => 10, + :minlon => -0.559, :minlat => 51.217, + :maxlon => 0.836, :maxlat => 51.766 + results_check :prefix => "Beverly Hills, CA,", :name => "90210", + :lat => 34.088808, :lon => -118.40612 + + xhr :get, :search_us_postcode, + :query => "00000", :zoom => 10, + :minlon => -0.559, :minlat => 51.217, + :maxlon => 0.836, :maxlat => 51.766 + results_check + end + end + + ## + # Test the UK postcode search + def test_search_uk_postcode + with_http_stubs "npemap" do + xhr :get, :search_uk_postcode, + :query => "CV4 7AL", :zoom => 10, + :minlon => -0.559, :minlat => 51.217, + :maxlon => 0.836, :maxlat => 51.766 + results_check :name => "CV4 7AL", :lat => 52.381748701968, :lon => -1.56176420939232 + + xhr :get, :search_uk_postcode, + :query => "XX9 9XX", :zoom => 10, + :minlon => -0.559, :minlat => 51.217, + :maxlon => 0.836, :maxlat => 51.766 + results_check + end + end + + ## + # Test the Canadian postcode search + def test_search_ca_postcode + with_http_stubs "geocoder_ca" do + xhr :get, :search_ca_postcode, + :query => "A1B 2C3", :zoom => 10, + :minlon => -0.559, :minlat => 51.217, + :maxlon => 0.836, :maxlat => 51.766 + results_check :name => "A1B 2C3", :lat => "47.172520", :lon => "-55.440515" + + xhr :get, :search_ca_postcode, + :query => "k1a 0b1", :zoom => 10, + :minlon => -0.559, :minlat => 51.217, + :maxlon => 0.836, :maxlat => 51.766 + results_check :name => "K1A 0B1", :lat => "45.375437", :lon => "-75.691041" + + xhr :get, :search_ca_postcode, + :query => "Q0Q 0Q0", :zoom => 10, + :minlon => -0.559, :minlat => 51.217, + :maxlon => 0.836, :maxlat => 51.766 + results_check + end + end + + ## + # Test the nominatim forward search + def test_search_osm_nominatim + with_http_stubs "nominatim" do + xhr :get, :search_osm_nominatim, + :query => "Hoddesdon", :zoom => 10, + :minlon => -0.559, :minlat => 51.217, + :maxlon => 0.836, :maxlat => 51.766 + results_check "name" => "Hoddesdon, Hertfordshire, East of England, England, United Kingdom", + "min-lat" => 51.7216709, "max-lat" => 51.8016709, + "min-lon" => -0.0512898, "max-lon" => 0.0287102, + "type" => "node", "id" => 18007599 + + xhr :get, :search_osm_nominatim, + :query => "Broxbourne", :zoom => 10, + :minlon => -0.559, :minlat => 51.217, + :maxlon => 0.836, :maxlat => 51.766 + results_check({ "prefix" => "Suburb", + "name" => "Broxbourne, Hertfordshire, East of England, England, United Kingdom", + "min-lat" => 51.7265723, "max-lat" => 51.7665723, + "min-lon" => -0.0390782, "max-lon" => 0.0009218, + "type" => "node", "id" => 28825933 }, + { "prefix" => "City Boundary", + "name" => "Broxbourne, Hertfordshire, East of England, England, United Kingdom", + "min-lat" => 51.6808751, "max-lat" => 51.7806237, + "min-lon" => -0.114204, "max-lon" => 0.0145267, + "type" => "relation", "id" => 2677978 }, + { "prefix" => "Railway Station", + "name" => "Broxbourne, Stafford Drive, Broxbourne, Hertfordshire, East of England, England, United Kingdom", + "min-lat" => 51.7418469, "max-lat" => 51.7518469, + "min-lon" => -0.0156773, "max-lon" => -0.0056773, + "type" => "node", "id" => 17044599 }) + end + end + + ## + # Test the geonames forward search + def test_search_geonames + with_http_stubs "geonames" do + xhr :get, :search_geonames, + :query => "Hoddesdon", :zoom => 10, + :minlon => -0.559, :minlat => 51.217, + :maxlon => 0.836, :maxlat => 51.766 + results_check :name => "Hoddesdon", :lat => 51.76148, :lon => -0.01144 + + xhr :get, :search_geonames, + :query => "Broxbourne", :zoom => 10, + :minlon => -0.559, :minlat => 51.217, + :maxlon => 0.836, :maxlat => 51.766 + results_check({ :name => "Broxbourne", :lat => 51.74712, :lon => -0.01923 }, + { :name => "Broxbourne District", :lat => 51.73026, :lon => -0.04821 }, + { :name => "Cheshunt", :lat => 51.70791, :lon => -0.03739 }, + { :name => "Hoddesdon", :lat => 51.76148, :lon => -0.01144 }, + { :name => "Waltham Cross", :lat => 51.68905, :lon => -0.0333 }, + { :name => "Goffs Oak", :lat => 51.71015, :lon => -0.0872 }, + { :name => "Wormley", :lat => 51.7324, :lon => -0.0242 }, + { :name => "Broxbourne", :lat => -27.50314, :lon => 151.378 }, + { :name => "Lee Valley White Water Centre", :lat => 51.68814, :lon => -0.01682 }, + { :name => "Cheshunt Railway Station", :lat => 51.703, :lon => -0.024 }, + { :name => "Theobalds Grove Railway Station", :lat => 51.692, :lon => -0.035 }, + { :name => "Waltham Cross Railway Station", :lat => 51.685, :lon => -0.027 }, + { :name => "Rye House Station", :lat => 51.76938, :lon => 0.00562 }, + { :name => "Broxbourne Station", :lat => 51.74697, :lon => -0.01105 }, + { :name => "Broxbornebury Park", :lat => 51.75252, :lon => -0.03839 }, + { :name => "Marriott Cheshunt", :lat => 51.7208, :lon => -0.0324 }, + { :name => "Cheshunt Community Hospital", :lat => 51.68396, :lon => -0.03951 }) + end + end + + ## + # Test the nominatim reverse search + def test_search_osm_nominatim_reverse + with_http_stubs "nominatim" do + xhr :get, :search_osm_nominatim_reverse, :lat => 51.7632, :lon => -0.0076, :zoom => 15 + results_check :name => "Broxbourne, Hertfordshire, East of England, England, United Kingdom", + :lat => 51.7465723, :lon => -0.0190782, + :type => "node", :id => 28825933, :zoom => 15 + + xhr :get, :search_osm_nominatim_reverse, :lat => 51.7632, :lon => -0.0076, :zoom => 17 + results_check :name => "Dinant Link Road, Broxbourne, Hertfordshire, East of England, England, EN11 8HX, United Kingdom", + :lat => 51.7634883, :lon => -0.0088373, + :type => "way", :id => 3489841, :zoom => 17 + end + end + + ## + # Test the geonames reverse search + def test_search_geonames_reverse + with_http_stubs "geonames" do + xhr :get, :search_geonames_reverse, :lat => 51.7632, :lon => -0.0076, :zoom => 15 + results_check :name => "England", :suffix => ", United Kingdom", + :lat => 51.7632, :lon => -0.0076 + end end private def latlon_check(query, lat, lon) - post :search, :query => query + get :search, :query => query assert_response :success - assert_template "search" + assert_template :search assert_template :layout => "map" - assert_equal %w(latlon osm_nominatim_reverse), assigns(:sources) + assert_equal %w(latlon osm_nominatim_reverse geonames_reverse), assigns(:sources) assert_nil @controller.params[:query] assert_in_delta lat, @controller.params[:lat] assert_in_delta lon, @controller.params[:lon] - xhr :post, :search, :query => query + xhr :get, :search, :query => query assert_response :success - assert_template "search" + assert_template :search assert_template :layout => "xhr" - assert_equal %w(latlon osm_nominatim_reverse), assigns(:sources) + assert_equal %w(latlon osm_nominatim_reverse geonames_reverse), assigns(:sources) assert_nil @controller.params[:query] assert_in_delta lat, @controller.params[:lat] assert_in_delta lon, @controller.params[:lon] end def search_check(query, sources) - post :search, :query => query + get :search, :query => query assert_response :success - assert_template "search" + assert_template :search assert_template :layout => "map" assert_equal sources, assigns(:sources) - xhr :post, :search, :query => query + xhr :get, :search, :query => query assert_response :success - assert_template "search" + assert_template :search assert_template :layout => "xhr" assert_equal sources, assigns(:sources) end + + def results_check(*results) + assert_response :success + assert_template :results + assert_template :layout => nil + if results.empty? + assert_select "ul.results-list", 0 + else + assert_select "ul.results-list", 1 do + assert_select "p.search_results_entry", results.count + + results.each do |result| + attrs = result.collect { |k, v| "[data-#{k}='#{v}']" }.join("") + assert_select "p.search_results_entry a.set_position#{attrs}", result[:name] + end + end + end + end + + def results_check_error(error) + assert_response :success + assert_template :error + assert_template :layout => nil + assert_select "p.search_results_error", error + end end diff --git a/test/controllers/trace_controller_test.rb b/test/controllers/trace_controller_test.rb index d4f58c872..b2f7c01b0 100644 --- a/test/controllers/trace_controller_test.rb +++ b/test/controllers/trace_controller_test.rb @@ -728,8 +728,26 @@ class TraceControllerTest < ActionController::TestCase file.rewind # Now authenticated, with the legacy public flag - assert_not_equal "private", users(:public_user).preferences.where(:k => "gps.trace.visibility").first.v + assert_not_equal "public", users(:public_user).preferences.where(:k => "gps.trace.visibility").first.v basic_authorization(users(:public_user).display_name, "test") + post :api_create, :file => file, :description => "New Trace", :tags => "new,trace", :public => 1 + assert_response :success + trace = Trace.find(response.body.to_i) + assert_equal "1.gpx", trace.name + assert_equal "New Trace", trace.description + assert_equal "new, trace", trace.tagstring + assert_equal "public", trace.visibility + assert_equal false, trace.inserted + assert_equal File.new(gpx_files(:public_trace_file).trace_name).read, File.new(trace.trace_name).read + trace.destroy + assert_equal "public", users(:public_user).preferences.where(:k => "gps.trace.visibility").first.v + + # Rewind the file + file.rewind + + # Now authenticated, with the legacy private flag + assert_nil users(:second_public_user).preferences.where(:k => "gps.trace.visibility").first + basic_authorization(users(:second_public_user).display_name, "test") post :api_create, :file => file, :description => "New Trace", :tags => "new,trace", :public => 0 assert_response :success trace = Trace.find(response.body.to_i) @@ -740,7 +758,7 @@ class TraceControllerTest < ActionController::TestCase assert_equal false, trace.inserted assert_equal File.new(gpx_files(:public_trace_file).trace_name).read, File.new(trace.trace_name).read trace.destroy - assert_equal "private", users(:public_user).preferences.where(:k => "gps.trace.visibility").first.v + assert_equal "private", users(:second_public_user).preferences.where(:k => "gps.trace.visibility").first.v end # Check updating a trace through the api diff --git a/test/http/geocoder_ca.yml b/test/http/geocoder_ca.yml new file mode 100644 index 000000000..754885a5b --- /dev/null +++ b/test/http/geocoder_ca.yml @@ -0,0 +1,48 @@ +/?geoit=XML&postal=A1B+2C3: | + + + 47.172520 + -55.440515 + A1B2C3 + + 1 + + ST. JOHN&'S + NL + 0.9 + + + +/?geoit=XML&postal=k1a+0b1: | + + + 45.375437 + -75.691041 + K1A0B1 + + 1 + + OTTAWA + ON + 0.9 + + + +/?geoit=XML&postal=Q0Q+0Q0: | + + + + 008 + Your request did not produce any results. Check your spelling and try again. + + + - + Q0Q0Q0 + + 1 + + + + 0.9 + + diff --git a/test/http/geocoder_us.yml b/test/http/geocoder_us.yml new file mode 100644 index 000000000..1e451d60a --- /dev/null +++ b/test/http/geocoder_us.yml @@ -0,0 +1,2 @@ +/service/csv?zip=90210: "34.088808, -118.40612, Beverly Hills, CA, 90210" +/service/csv?zip=00000: "1: couldn't find this zip code: 00000! sorry" diff --git a/test/http/geonames.yml b/test/http/geonames.yml new file mode 100644 index 000000000..3527e3ced --- /dev/null +++ b/test/http/geonames.yml @@ -0,0 +1,222 @@ +/search?lang=en&maxRows=20&q=Hoddesdon&username=dummy: | + + + 1 + + Hoddesdon + Hoddesdon + 51.76148 + -0.01144 + 2646807 + GB + United Kingdom + P + PPL + + + +/search?lang=en&maxRows=20&q=Broxbourne&username=dummy: | + + + 17 + + Broxbourne + Broxbourne + 51.74712 + -0.01923 + 2654481 + GB + United Kingdom + P + PPL + + + Broxbourne District + Broxbourne District + 51.73026 + -0.04821 + 7290563 + GB + United Kingdom + A + ADM3 + + + Cheshunt + Cheshunt + 51.70791 + -0.03739 + 2653232 + GB + United Kingdom + P + PPL + + + Hoddesdon + Hoddesdon + 51.76148 + -0.01144 + 2646807 + GB + United Kingdom + P + PPL + + + Waltham Cross + Waltham Cross + 51.68905 + -0.0333 + 2634842 + GB + United Kingdom + P + PPL + + + Goffs Oak + Goffs Oak + 51.71015 + -0.0872 + 2648362 + GB + United Kingdom + P + PPL + + + Wormley + Wormley + 51.7324 + -0.0242 + 2633535 + GB + United Kingdom + P + PPL + + + Broxbourne + Broxbourne + -27.50314 + 151.378 + 8792801 + AU + Australia + S + HMSD + + + Lee Valley White Water Centre + Lee Valley White Water Centre + 51.68814 + -0.01682 + 7670551 + GB + United Kingdom + S + FCL + + + Cheshunt Railway Station + Cheshunt Railway Station + 51.703 + -0.024 + 6952282 + GB + United Kingdom + S + RSTN + + + Theobalds Grove Railway Station + Theobalds Grove Railway Station + 51.692 + -0.035 + 6953715 + GB + United Kingdom + S + RSTN + + + Waltham Cross Railway Station + Waltham Cross Railway Station + 51.685 + -0.027 + 6953801 + GB + United Kingdom + S + RSTN + + + Rye House Station + Rye House Station + 51.76938 + 0.00562 + 6691700 + GB + United Kingdom + S + RSTN + + + Broxbourne Station + Broxbourne Station + 51.74697 + -0.01105 + 6691701 + GB + United Kingdom + S + RSTN + + + Broxbornebury Park + Broxbornebury Park + 51.75252 + -0.03839 + 6286417 + GB + United Kingdom + S + CSTL + + + Marriott Cheshunt + Marriott Cheshunt + 51.7208 + -0.0324 + 6512481 + GB + United Kingdom + S + HTL + + + Cheshunt Community Hospital + Cheshunt Community Hospital + 51.68396 + -0.03951 + 6289233 + GB + United Kingdom + S + HSP + + + +/countrySubdivision?lang=en&lat=51.7632&lng=-0.0076&username=dummy: | + + + + GB + United Kingdom + ENG + England + ENG + 0.0 + + diff --git a/test/http/nominatim.yml b/test/http/nominatim.yml new file mode 100644 index 000000000..443149346 --- /dev/null +++ b/test/http/nominatim.yml @@ -0,0 +1,45 @@ +/search?accept-language=&format=xml&q=Hoddesdon&viewbox=-0.559%2C51.766%2C0.836%2C51.217: | + + + + + +/search?accept-language=&format=xml&q=Broxbourne&viewbox=-0.559%2C51.766%2C0.836%2C51.217: | + + + + + + + +/reverse?accept-language=&lat=51.7632&lon=-0.0076&zoom=15: | + + + Broxbourne, Hertfordshire, East of England, England, United Kingdom + + Broxbourne + Broxbourne + Hertfordshire + East of England + England + United Kingdom + gb + + + +/reverse?accept-language=&lat=51.7632&lon=-0.0076&zoom=17: | + + + Dinant Link Road, Broxbourne, Hertfordshire, East of England, England, EN11 8HX, United Kingdom + + Dinant Link Road + Broxbourne + Broxbourne + Hertfordshire + East of England + England + EN11 8HX + United Kingdom + gb + + diff --git a/test/http/npemap.yml b/test/http/npemap.yml new file mode 100644 index 000000000..39c585a13 --- /dev/null +++ b/test/http/npemap.yml @@ -0,0 +1,6 @@ +/cgi/geocoder.fcgi?format=text&postcode=CV4+7AL: | + # Easting,Northing,Matched Postcode,Latitude,Longitude + 429926,276058,'CV4 7AL',52.381748701968,-1.56176420939232 + +/cgi/geocoder.fcgi?format=text&postcode=XX9+9XX: | + Error: Postcode area 'XX' not found, postcode probably invalid diff --git a/test/test_helper.rb b/test/test_helper.rb index 4696958f8..4bd228f14 100644 --- a/test/test_helper.rb +++ b/test/test_helper.rb @@ -117,18 +117,25 @@ module ActiveSupport assert_equal a.tags, b.tags, "tags on node #{a.id}" end + ## + # set request headers for HTTP basic authentication def basic_authorization(user, pass) @request.env["HTTP_AUTHORIZATION"] = format("Basic %s", Base64.encode64("#{user}:#{pass}")) end + ## + # set request readers to ask for a particular error format def error_format(format) @request.env["HTTP_X_ERROR_FORMAT"] = format end + ## + # set the raw body to be sent with a POST request def content(c) @request.env["RAW_POST_DATA"] = c.to_s end + ## # Used to check that the error header and the forbidden responses are given # when the owner of the changset has their data not marked as public def assert_require_public_data(msg = "Shouldn't be able to use API when the user's data is not public") @@ -136,14 +143,39 @@ module ActiveSupport assert_equal @response.headers["Error"], "You must make your edits public to upload new data", "Wrong error message" end + ## # Not sure this is the best response we could give def assert_inactive_user(msg = "an inactive user shouldn't be able to access the API") assert_response :unauthorized, msg # assert_equal @response.headers['Error'], "" end + ## + # Check for missing translations in an HTML response def assert_no_missing_translations(msg = "") assert_select "span[class=translation_missing]", false, "Missing translation #{msg}" end + + ## + # execute a block with a given set of HTTP responses stubbed + def with_http_stubs(stubs_file) + http_client_save = OSM.http_client + + begin + stubs = YAML.load_file(File.expand_path("../http/#{stubs_file}.yml", __FILE__)) + + OSM.http_client = Faraday.new do |builder| + builder.adapter :test do |stub| + stubs.each do |url, body| + stub.get(url) { |_env| [200, {}, body] } + end + end + end + + yield + ensure + OSM.http_client = http_client_save + end + end end end