Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

Update tests

Add tests
  • Loading branch information...
commit 1ec80eae58c8aba2884daaf57899fe1031921003 1 parent f6b03f2
@redronin redronin authored
View
10 lib/shopify_api/session.rb
@@ -17,16 +17,18 @@ def setup(params)
def temp(domain, token, &block)
session = new(domain, token)
-
- original_domain = URI.parse(ShopifyAPI::Base.site).host
+ begin
+ original_domain = URI.parse(ShopifyAPI::Base.site.to_s).host
+ rescue URI::InvalidURIError
+ end
original_token = ShopifyAPI::Base.headers['X-Shopify-Access-Token']
original_session = new(original_domain, original_token)
begin
- activate_session(session)
+ ShopifyAPI::Base.activate_session(session)
yield
ensure
- activate_session(original_session)
+ ShopifyAPI::Base.activate_session(original_session)
end
end
View
50 test/base_test.rb
@@ -0,0 +1,50 @@
+require 'test_helper'
+
+
+class BaseTest < Test::Unit::TestCase
+
+ def setup
+ @session1 = ShopifyAPI::Session.new('shop1.myshopify.com', 'token1')
+ @session2 = ShopifyAPI::Session.new('shop2.myshopify.com', 'token2')
+ end
+
+ test '#activate_session should set site and headers for given session' do
+ ShopifyAPI::Base.activate_session @session1
+
+ assert_nil ActiveResource::Base.site
+ assert_equal 'https://shop1.myshopify.com/admin', ShopifyAPI::Base.site.to_s
+ assert_equal 'https://shop1.myshopify.com/admin', ShopifyAPI::Shop.site.to_s
+
+ assert_nil ActiveResource::Base.headers['X-Shopify-Access-Token']
+ assert_equal 'token1', ShopifyAPI::Base.headers['X-Shopify-Access-Token']
+ assert_equal 'token1', ShopifyAPI::Shop.headers['X-Shopify-Access-Token']
+ end
+
+ test '#clear_session should clear site and headers from Base' do
+ ShopifyAPI::Base.activate_session @session1
+ ShopifyAPI::Base.clear_session
+
+ assert_nil ActiveResource::Base.site
+ assert_nil ShopifyAPI::Base.site
+ assert_nil ShopifyAPI::Shop.site
+
+ assert_nil ActiveResource::Base.headers['X-Shopify-Access-Token']
+ assert_nil ShopifyAPI::Base.headers['X-Shopify-Access-Token']
+ assert_nil ShopifyAPI::Shop.headers['X-Shopify-Access-Token']
+ end
+
+ test '#activate_session with one session, then clearing and activating with another session should send request to correct shop' do
+ ShopifyAPI::Base.activate_session @session1
+ ShopifyAPI::Base.clear_session
+ ShopifyAPI::Base.activate_session @session2
+
+ assert_nil ActiveResource::Base.site
+ assert_equal 'https://shop2.myshopify.com/admin', ShopifyAPI::Base.site.to_s
+ assert_equal 'https://shop2.myshopify.com/admin', ShopifyAPI::Shop.site.to_s
+
+ assert_nil ActiveResource::Base.headers['X-Shopify-Access-Token']
+ assert_equal 'token2', ShopifyAPI::Base.headers['X-Shopify-Access-Token']
+ assert_equal 'token2', ShopifyAPI::Shop.headers['X-Shopify-Access-Token']
+ end
+
+end
View
15 test/session_test.rb
@@ -35,20 +35,21 @@ class SessionTest < Test::Unit::TestCase
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
+ session1 = ShopifyAPI::Session.new('fakeshop.myshopify.com', 'token1')
+ ShopifyAPI::Base.activate_session(session1)
+
ShopifyAPI::Session.temp("testshop.myshopify.com", "any-token") {
- assigned_site = ShopifyAPI::Base.site
+ @assigned_site = ShopifyAPI::Base.site
}
- assert_equal 'https://:any-token@testshop.myshopify.com/admin', assigned_site.to_s
- assert_equal 'http://www.original.com', ShopifyAPI::Base.site.to_s
+ assert_equal 'https://testshop.myshopify.com/admin', @assigned_site.to_s
+ assert_equal 'https://fakeshop.myshopify.com/admin', ShopifyAPI::Base.site.to_s
end
should "return site for session" do
session = ShopifyAPI::Session.new("testshop.myshopify.com", "any-token")
- assert_equal "https://:any-token@testshop.myshopify.com/admin", session.site
+ assert_equal "https://testshop.myshopify.com/admin", session.site
end
end
end
Please sign in to comment.
Something went wrong with that request. Please try again.