From bdeb053c14c8d089561e80f779c75d49634a6872 Mon Sep 17 00:00:00 2001 From: Martin Edenhofer Date: Sun, 22 Mar 2015 13:23:05 +0100 Subject: [PATCH] Added geoip tests. --- lib/geo_ip/zammad_geo_ip.rb | 6 ++++ test/integration/geo_ip_test.rb | 59 +++++++++++++++++++++++++++++++++ 2 files changed, 65 insertions(+) create mode 100644 test/integration/geo_ip_test.rb diff --git a/lib/geo_ip/zammad_geo_ip.rb b/lib/geo_ip/zammad_geo_ip.rb index ad85c5244..8eb6db120 100644 --- a/lib/geo_ip/zammad_geo_ip.rb +++ b/lib/geo_ip/zammad_geo_ip.rb @@ -28,6 +28,12 @@ class GeoIp::ZammadGeoIp end data = JSON.parse( response.body ) + + # compat. map + if data && data['country_code2'] + data['country_code'] = data['country_code2'] + end + Cache.write( cache_key, data, { :expires_in => 90.days } ) rescue => e puts "ERROR: #{host}#{url}: " + e.inspect diff --git a/test/integration/geo_ip_test.rb b/test/integration/geo_ip_test.rb new file mode 100644 index 000000000..821bc7dcf --- /dev/null +++ b/test/integration/geo_ip_test.rb @@ -0,0 +1,59 @@ +# encoding: utf-8 +require 'integration_test_helper' + +class GeoIpTest < ActiveSupport::TestCase + + # check + test 'check some results' do + + result = GeoIp.location( '127.0.0.0.1' ) + assert(result) + assert_equal(nil, result['country_name']) + assert_equal(nil, result['city_name']) + assert_equal(nil, result['country_code']) + assert_equal(nil, result['continent_code']) + assert_equal(nil, result['latitude']) + assert_equal(nil, result['longitude']) + + + result = GeoIp.location( '195.65.29.254' ) + assert(result) + assert_equal('Switzerland', result['country_name']) + assert_equal('Regensdorf', result['city_name']) + assert_equal('CH', result['country_code']) + assert_equal('EU', result['continent_code']) + assert_equal(47.4299, result['latitude']) + assert_equal(8.465100000000007, result['longitude']) + + + result = GeoIp.location( '134.109.140.74' ) + assert(result) + assert_equal('Germany', result['country_name']) + assert_equal('Chemnitz', result['city_name']) + assert_equal('DE', result['country_code']) + assert_equal('EU', result['continent_code']) + assert_equal(50.83330000000001, result['latitude']) + assert_equal(12.916699999999992, result['longitude']) + + + result = GeoIp.location( '46.253.55.170' ) + assert(result) + assert_equal('Germany', result['country_name']) + assert_equal('Halle', result['city_name']) + assert_equal('DE', result['country_code']) + assert_equal('EU', result['continent_code']) + assert_equal(51.5, result['latitude']) + assert_equal(12.0, result['longitude']) + + + result = GeoIp.location( '169.229.216.200' ) + assert(result) + assert_equal('United States', result['country_name']) + assert_equal('Berkeley', result['city_name']) + assert_equal('US', result['country_code']) + assert_equal('NA', result['continent_code']) + assert_equal(37.86680000000001, result['latitude']) + assert_equal(-122.2536, result['longitude']) + + end +end \ No newline at end of file