base_test.rb 2.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657
  1. require 'test_helper'
  2. class BaseTest < Test::Unit::TestCase
  3. def setup
  4. @session1 = ShopifyAPI::Session.new('shop1.myshopify.com', 'token1')
  5. @session2 = ShopifyAPI::Session.new('shop2.myshopify.com', 'token2')
  6. end
  7. test '#activate_session should set site and headers for given session' do
  8. ShopifyAPI::Base.activate_session @session1
  9. assert_nil ActiveResource::Base.site
  10. assert_equal 'https://shop1.myshopify.com/admin', ShopifyAPI::Base.site.to_s
  11. assert_equal 'https://shop1.myshopify.com/admin', ShopifyAPI::Shop.site.to_s
  12. assert_nil ActiveResource::Base.headers['X-Shopify-Access-Token']
  13. assert_equal 'token1', ShopifyAPI::Base.headers['X-Shopify-Access-Token']
  14. assert_equal 'token1', ShopifyAPI::Shop.headers['X-Shopify-Access-Token']
  15. end
  16. test '#clear_session should clear site and headers from Base' do
  17. ShopifyAPI::Base.activate_session @session1
  18. ShopifyAPI::Base.clear_session
  19. assert_nil ActiveResource::Base.site
  20. assert_nil ShopifyAPI::Base.site
  21. assert_nil ShopifyAPI::Shop.site
  22. assert_nil ActiveResource::Base.headers['X-Shopify-Access-Token']
  23. assert_nil ShopifyAPI::Base.headers['X-Shopify-Access-Token']
  24. assert_nil ShopifyAPI::Shop.headers['X-Shopify-Access-Token']
  25. end
  26. test '#activate_session with one session, then clearing and activating with another session should send request to correct shop' do
  27. ShopifyAPI::Base.activate_session @session1
  28. ShopifyAPI::Base.clear_session
  29. ShopifyAPI::Base.activate_session @session2
  30. assert_nil ActiveResource::Base.site
  31. assert_equal 'https://shop2.myshopify.com/admin', ShopifyAPI::Base.site.to_s
  32. assert_equal 'https://shop2.myshopify.com/admin', ShopifyAPI::Shop.site.to_s
  33. assert_nil ActiveResource::Base.headers['X-Shopify-Access-Token']
  34. assert_equal 'token2', ShopifyAPI::Base.headers['X-Shopify-Access-Token']
  35. assert_equal 'token2', ShopifyAPI::Shop.headers['X-Shopify-Access-Token']
  36. end
  37. test "#delete should send custom headers with request" do
  38. ShopifyAPI::Base.activate_session @session1
  39. ShopifyAPI::Base.expects(:headers).returns({'X-Custom' => 'abc'})
  40. ShopifyAPI::Base.connection.expects(:delete).with('/admin/bases/1.json', {'X-Custom' => 'abc'})
  41. ShopifyAPI::Base.delete "1"
  42. end
  43. end