base_test.rb 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139
  1. require 'test_helper'
  2. require "active_support/log_subscriber/test_helper"
  3. class BaseTest < Test::Unit::TestCase
  4. def setup
  5. @session1 = ShopifyAPI::Session.new('shop1.myshopify.com', 'token1')
  6. @session2 = ShopifyAPI::Session.new('shop2.myshopify.com', 'token2')
  7. end
  8. def teardown
  9. clear_header('X-Custom')
  10. end
  11. test '#activate_session should set site and headers for given session' do
  12. ShopifyAPI::Base.activate_session @session1
  13. assert_nil ActiveResource::Base.site
  14. assert_equal 'https://shop1.myshopify.com', ShopifyAPI::Base.site.to_s
  15. assert_equal 'https://shop1.myshopify.com', ShopifyAPI::Shop.site.to_s
  16. assert_nil ActiveResource::Base.headers['X-Shopify-Access-Token']
  17. assert_equal 'token1', ShopifyAPI::Base.headers['X-Shopify-Access-Token']
  18. assert_equal 'token1', ShopifyAPI::Shop.headers['X-Shopify-Access-Token']
  19. end
  20. test '#clear_session should clear base site settings from Base' do
  21. ShopifyAPI::Base.site = "https://foo:bar@www.zombo.com"
  22. assert_equal "foo", ShopifyAPI::Base.user
  23. assert_equal "bar", ShopifyAPI::Base.password
  24. ShopifyAPI::Base.clear_session
  25. assert_equal nil, ShopifyAPI::Base.user
  26. assert_equal nil, ShopifyAPI::Base.password
  27. assert_equal nil, ShopifyAPI::Base.site
  28. end
  29. test '#clear_session should clear site and headers from Base' do
  30. ShopifyAPI::Base.activate_session @session1
  31. ShopifyAPI::Base.clear_session
  32. assert_nil ActiveResource::Base.site
  33. assert_nil ShopifyAPI::Base.site
  34. assert_nil ShopifyAPI::Shop.site
  35. assert_nil ActiveResource::Base.headers['X-Shopify-Access-Token']
  36. assert_nil ShopifyAPI::Base.headers['X-Shopify-Access-Token']
  37. assert_nil ShopifyAPI::Shop.headers['X-Shopify-Access-Token']
  38. end
  39. test '#activate_session with one session, then clearing and activating with another session should send request to correct shop' do
  40. ShopifyAPI::Base.activate_session @session1
  41. ShopifyAPI::Base.clear_session
  42. ShopifyAPI::Base.activate_session @session2
  43. assert_nil ActiveResource::Base.site
  44. assert_equal 'https://shop2.myshopify.com', ShopifyAPI::Base.site.to_s
  45. assert_equal 'https://shop2.myshopify.com', ShopifyAPI::Shop.site.to_s
  46. assert_nil ActiveResource::Base.headers['X-Shopify-Access-Token']
  47. assert_equal 'token2', ShopifyAPI::Base.headers['X-Shopify-Access-Token']
  48. assert_equal 'token2', ShopifyAPI::Shop.headers['X-Shopify-Access-Token']
  49. end
  50. test '#activate_session with nil raises an InvalidSessionError' do
  51. assert_raises ShopifyAPI::Base::InvalidSessionError do
  52. ShopifyAPI::Base.activate_session nil
  53. end
  54. end
  55. test "#delete should send custom headers with request" do
  56. ShopifyAPI::Base.activate_session @session1
  57. ShopifyAPI::Base.headers['X-Custom'] = 'abc'
  58. ShopifyAPI::Base.connection.expects(:delete).with('/admin/bases/1.json', has_entry('X-Custom', 'abc'))
  59. ShopifyAPI::Base.delete "1"
  60. end
  61. test "#headers includes the User-Agent" do
  62. assert_not_includes ActiveResource::Base.headers.keys, 'User-Agent'
  63. assert_includes ShopifyAPI::Base.headers.keys, 'User-Agent'
  64. thread = Thread.new do
  65. assert_includes ShopifyAPI::Base.headers.keys, 'User-Agent'
  66. end
  67. thread.join
  68. end
  69. test "prefix= will forward to resource when the value does not start with admin" do
  70. TestResource.prefix = 'a/regular/path/'
  71. assert_equal('/admin/a/regular/path/', TestResource.prefix)
  72. end
  73. test "prefix= will raise an error if value starts with with /admin" do
  74. assert_raises ArgumentError do
  75. TestResource.prefix = '/admin/old/prefix/structure/'
  76. end
  77. end
  78. if ActiveResource::VERSION::MAJOR >= 4
  79. test "#headers propagates changes to subclasses" do
  80. ShopifyAPI::Base.headers['X-Custom'] = "the value"
  81. assert_equal "the value", ShopifyAPI::Base.headers['X-Custom']
  82. assert_equal "the value", ShopifyAPI::Product.headers['X-Custom']
  83. end
  84. test "#headers clears changes to subclasses" do
  85. ShopifyAPI::Base.headers['X-Custom'] = "the value"
  86. assert_equal "the value", ShopifyAPI::Product.headers['X-Custom']
  87. ShopifyAPI::Base.headers['X-Custom'] = nil
  88. assert_nil ShopifyAPI::Product.headers['X-Custom']
  89. end
  90. end
  91. if ActiveResource::VERSION::MAJOR >= 5 || (ActiveResource::VERSION::MAJOR >= 4 && ActiveResource::VERSION::PRE == "threadsafe")
  92. test "#headers set in the main thread affect spawned threads" do
  93. ShopifyAPI::Base.headers['X-Custom'] = "the value"
  94. Thread.new do
  95. assert_equal "the value", ShopifyAPI::Base.headers['X-Custom']
  96. end.join
  97. end
  98. test "#headers set in spawned threads do not affect the main thread" do
  99. Thread.new do
  100. ShopifyAPI::Base.headers['X-Custom'] = "the value"
  101. end.join
  102. assert_nil ShopifyAPI::Base.headers['X-Custom']
  103. end
  104. end
  105. def clear_header(header)
  106. [ActiveResource::Base, ShopifyAPI::Base, ShopifyAPI::Product].each do |klass|
  107. klass.headers.delete(header)
  108. end
  109. end
  110. class TestResource < ShopifyAPI::Base
  111. end
  112. end