Browse Source

ApiVersion.find_version raises if passed NullVersion as argument

Jon G 5 years ago
parent
commit
7270b6d739
2 changed files with 20 additions and 0 deletions
  1. 5 0
      lib/shopify_api/api_version.rb
  2. 15 0
      test/api_version_test.rb

+ 5 - 0
lib/shopify_api/api_version.rb

@@ -25,6 +25,7 @@ module ShopifyAPI
       end
 
       def find_version(version_or_handle)
+        raise ArgumentError, "NullVersion is not a valid version or version handle." if version_or_handle == NullVersion
         return version_or_handle if version_or_handle.is_a?(ApiVersion)
         handle = version_or_handle.to_s
         @versions ||= {}
@@ -174,6 +175,10 @@ module ShopifyAPI
 
     class NullVersion
       class << self
+        def new(*_args)
+          raise NoMethodError, "NullVersion is an abstract class and cannot be instantiated."
+        end
+
         def raise_not_set_error(*_args)
           raise ApiVersionNotSetError, "You must set ShopifyAPI::Base.api_version before making a request."
         end

+ 15 - 0
test/api_version_test.rb

@@ -50,6 +50,15 @@ class ApiVersionTest < Test::Unit::TestCase
     end
   end
 
+  test "find_version raises ArgumentError when given an ShopifyAPI::ApiVersion::NullVersion object" do
+    ShopifyAPI::ApiVersion.clear_known_versions
+    ShopifyAPI::ApiVersion.version_lookup_mode = :define_on_unknown
+    assert_equal :define_on_unknown, ShopifyAPI::ApiVersion.version_lookup_mode
+    assert_raises ArgumentError do
+      ShopifyAPI::ApiVersion.find_version(ShopifyAPI::ApiVersion::NullVersion)
+    end
+  end
+
   test 'two versions with the same version number are equal' do
     version_1 = ShopifyAPI::ApiVersion.new(handle: '2018-09')
     version_2 = ShopifyAPI::ApiVersion.new(handle: '2018-09')
@@ -125,6 +134,12 @@ class ApiVersionTest < Test::Unit::TestCase
     end
   end
 
+  test "NullVersion cannot be instantiated and raises NoMethodError if attempted" do
+    assert_raises(NoMethodError) do
+      ShopifyAPI::ApiVersion::NullVersion.new
+    end
+  end
+
   test "handle_to_date converts a version handle to a date" do
     version_1 = ShopifyAPI::ApiVersion.new(handle: '2019-01')
     version_2 = ShopifyAPI::ApiVersion.new(handle: 'unstable')