From cab988903c63a0c11ae1b2359b5b30e121ea59b1 Mon Sep 17 00:00:00 2001
From: Alex Reisner <alex@alexreisner.com>
Date: Fri, 21 Dec 2012 14:07:10 -0500
Subject: [PATCH] Raise exception on invalid MaxMind key.

---
 lib/geocoder/exceptions.rb            |  3 +++
 lib/geocoder/lookups/maxmind.rb       | 12 +++++++-----
 test/fixtures/maxmind_invalid_key.txt |  1 +
 test/services_test.rb                 | 10 ++++++++++
 test/test_helper.rb                   |  1 +
 5 files changed, 22 insertions(+), 5 deletions(-)
 create mode 100644 test/fixtures/maxmind_invalid_key.txt

diff --git a/lib/geocoder/exceptions.rb b/lib/geocoder/exceptions.rb
index 0ed0eb0d..7445c662 100644
--- a/lib/geocoder/exceptions.rb
+++ b/lib/geocoder/exceptions.rb
@@ -15,4 +15,7 @@ module Geocoder
   class InvalidRequest < Error
   end
 
+  class InvalidApiKey < Error
+  end
+
 end
diff --git a/lib/geocoder/lookups/maxmind.rb b/lib/geocoder/lookups/maxmind.rb
index 2619c627..9ca10504 100644
--- a/lib/geocoder/lookups/maxmind.rb
+++ b/lib/geocoder/lookups/maxmind.rb
@@ -14,14 +14,16 @@ module Geocoder::Lookup
     def results(query)
       # don't look up a loopback address, just return the stored result
       return [reserved_result] if query.loopback_ip_address?
-      begin
-        doc = fetch_data(query)
-        if doc && doc.size == 10
+      doc = fetch_data(query)
+      if doc and doc.is_a?(Array)
+        if doc.size == 10
           return [doc]
-        else
-          return []
+        elsif doc.size > 10 and doc[10] == "INVALID_LICENSE_KEY"
+          raise_error(Geocoder::InvalidApiKey) ||
+            warn("Invalid MaxMind API key.")
         end
       end
+      return []
     end
 
     def parse_raw_data(raw_data)
diff --git a/test/fixtures/maxmind_invalid_key.txt b/test/fixtures/maxmind_invalid_key.txt
new file mode 100644
index 00000000..e2165865
--- /dev/null
+++ b/test/fixtures/maxmind_invalid_key.txt
@@ -0,0 +1 @@
+,,,,,,,,,,INVALID_LICENSE_KEY
diff --git a/test/services_test.rb b/test/services_test.rb
index aed9b355..200e10e7 100644
--- a/test/services_test.rb
+++ b/test/services_test.rb
@@ -156,6 +156,16 @@ class ServicesTest < Test::Unit::TestCase
     assert_equal "Plano, TX 75093, US", result.address
   end
 
+  def test_maxmind_raises_exception_on_invalid_key
+    Geocoder.configure(
+      :always_raise => [Geocoder::InvalidApiKey]
+    )
+    l = Geocoder::Lookup.get(:maxmind)
+    assert_raises Geocoder::InvalidApiKey do
+      l.send(:results, Geocoder::Query.new("invalid key"))
+    end
+  end
+
 
   # --- Bing ---
 
diff --git a/test/test_helper.rb b/test/test_helper.rb
index 0ab1614b..95b7dedb 100644
--- a/test/test_helper.rb
+++ b/test/test_helper.rb
@@ -168,6 +168,7 @@ module Geocoder
         raise SocketError if query.text == "socket_error"
         file = case query.text
           when "no results";  :no_results
+          when "invalid key"; :invalid_key
           else                "74_200_247_59"
         end
         read_fixture "maxmind_#{file}.txt"
-- 
GitLab