Browse Source

Add test for Session#temp and move into one function

Denis Odorcic 13 years ago
parent
commit
835bc4fe3a
2 changed files with 22 additions and 11 deletions
  1. 9 10
      lib/shopify_api/session.rb
  2. 13 1
      test/shopify_api_test.rb

+ 9 - 10
lib/shopify_api/session.rb

@@ -94,7 +94,15 @@ module ShopifyAPI
       end
       
       def temp(domain, token, &block)
-        ShopifyAPI::Session.new(domain, token).temp { yield }
+        session = new(domain, token)
+
+        original_site = ShopifyAPI::Base.site
+        begin
+          ShopifyAPI::Base.site = session.site
+          yield
+        ensure
+          ShopifyAPI::Base.site = original_site
+        end
       end
       
       def prepare_url(url)
@@ -112,15 +120,6 @@ module ShopifyAPI
     
     end
     
-    def temp(&block)
-      begin
-        ShopifyAPI::Base.site = site
-        yield
-      ensure 
-        ShopifyAPI::Base.site = nil
-      end
-    end
-    
     def initialize(url, token = nil, params = nil)
       self.url, self.token = url, token
 

+ 13 - 1
test/shopify_api_test.rb

@@ -39,5 +39,17 @@ class ShopifyApiTest < Test::Unit::TestCase
     should "use 'https' protocol by default for all sessions" do
       assert_equal 'https', ShopifyAPI::Session.protocol
     end
+
+    should "#temp reset ShopifyAPI::Base.site to original value" do
+      ShopifyAPI::Base.site = 'http://www.original.com'
+
+      ShopifyAPI::Session.setup(:api_key => "key", :secret => "secret")
+      assigned_site = nil
+      ShopifyAPI::Session.temp("testshop.myshopify.com", "any-token") {
+        assigned_site = ShopifyAPI::Base.site
+      }
+      assert_equal 'https://key:e56d5793b869753d87cf03ceb6bb5dfc@testshop.myshopify.com/admin', assigned_site.to_s
+      assert_equal 'http://www.original.com', ShopifyAPI::Base.site.to_s
+    end
   end
-end
+end