base_test.rb 7.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214
  1. require 'test_helper'
  2. require "active_support/log_subscriber/test_helper"
  3. class BaseTest < Test::Unit::TestCase
  4. def setup
  5. super
  6. @session1 = ShopifyAPI::Session.new(domain: 'shop1.myshopify.com', token: 'token1', api_version: '2019-01')
  7. @session2 = ShopifyAPI::Session.new(domain: 'shop2.myshopify.com', token: 'token2', api_version: '2019-01')
  8. end
  9. def teardown
  10. super
  11. clear_header('X-Custom')
  12. end
  13. test '#activate_session should set site and headers for given session' do
  14. ShopifyAPI::Base.activate_session @session1
  15. assert_nil ActiveResource::Base.site
  16. assert_equal 'https://shop1.myshopify.com', ShopifyAPI::Base.site.to_s
  17. assert_equal 'https://shop1.myshopify.com', ShopifyAPI::Shop.site.to_s
  18. assert_nil ActiveResource::Base.headers['X-Shopify-Access-Token']
  19. assert_equal 'token1', ShopifyAPI::Base.headers['X-Shopify-Access-Token']
  20. assert_equal 'token1', ShopifyAPI::Shop.headers['X-Shopify-Access-Token']
  21. end
  22. test '#clear_session should clear base site settings from Base' do
  23. ShopifyAPI::Base.site = "https://foo:bar@www.zombo.com"
  24. assert_equal "foo", ShopifyAPI::Base.user
  25. assert_equal "bar", ShopifyAPI::Base.password
  26. ShopifyAPI::Base.clear_session
  27. assert_nil ShopifyAPI::Base.user
  28. assert_nil ShopifyAPI::Base.password
  29. assert_nil ShopifyAPI::Base.site
  30. end
  31. # test to check session reset api version remains the same after session reset
  32. test '#clear_session should not change the api_version' do
  33. ShopifyAPI::Base.site = "https://zoo:lion@www.zoo.com"
  34. assert_equal "zoo", ShopifyAPI::Base.user
  35. assert_equal "lion", ShopifyAPI::Base.password
  36. ShopifyAPI::Base.clear_session
  37. assert_nil ShopifyAPI::Base.user
  38. assert_nil ShopifyAPI::Base.password
  39. assert_nil ShopifyAPI::Base.site
  40. assert_equal ShopifyAPI::Base.api_version,@session1.api_version
  41. end
  42. test '#clear_session should clear site and headers from Base' do
  43. ShopifyAPI::Base.activate_session @session1
  44. ShopifyAPI::Base.clear_session
  45. assert_nil ActiveResource::Base.site
  46. assert_nil ShopifyAPI::Base.site
  47. assert_nil ShopifyAPI::Shop.site
  48. assert_nil ActiveResource::Base.headers['X-Shopify-Access-Token']
  49. assert_nil ShopifyAPI::Base.headers['X-Shopify-Access-Token']
  50. assert_nil ShopifyAPI::Shop.headers['X-Shopify-Access-Token']
  51. end
  52. test '#activate_session with one session, then clearing and activating with another session should send request to correct shop' do
  53. ShopifyAPI::Base.activate_session @session1
  54. ShopifyAPI::Base.clear_session
  55. ShopifyAPI::Base.activate_session @session2
  56. assert_nil ActiveResource::Base.site
  57. assert_equal 'https://shop2.myshopify.com', ShopifyAPI::Base.site.to_s
  58. assert_equal 'https://shop2.myshopify.com', ShopifyAPI::Shop.site.to_s
  59. assert_nil ActiveResource::Base.headers['X-Shopify-Access-Token']
  60. assert_equal 'token2', ShopifyAPI::Base.headers['X-Shopify-Access-Token']
  61. assert_equal 'token2', ShopifyAPI::Shop.headers['X-Shopify-Access-Token']
  62. end
  63. test '#activate_session with nil raises an InvalidSessionError' do
  64. assert_raises ShopifyAPI::Base::InvalidSessionError do
  65. ShopifyAPI::Base.activate_session nil
  66. end
  67. end
  68. test "#delete should send custom headers with request" do
  69. ShopifyAPI::Base.activate_session @session1
  70. ShopifyAPI::Base.headers['X-Custom'] = 'abc'
  71. ShopifyAPI::Base.connection
  72. .expects(:delete)
  73. .with('/admin/api/2019-01/bases/1.json', has_entry('X-Custom', 'abc'))
  74. ShopifyAPI::Base.delete "1"
  75. end
  76. test "#headers includes the User-Agent" do
  77. assert_not_includes ActiveResource::Base.headers.keys, 'User-Agent'
  78. assert_includes ShopifyAPI::Base.headers.keys, 'User-Agent'
  79. thread = Thread.new do
  80. assert_includes ShopifyAPI::Base.headers.keys, 'User-Agent'
  81. end
  82. thread.join
  83. end
  84. test "prefix= will forward to resource when the value does not start with admin" do
  85. session = ShopifyAPI::Session.new(domain: 'shop1.myshopify.com', token: 'token1', api_version: '2019-01')
  86. ShopifyAPI::Base.activate_session(session)
  87. silence_warnings do
  88. TestResource.prefix = 'a/regular/path/'
  89. end
  90. assert_equal('/admin/api/2019-01/a/regular/path/', TestResource.prefix)
  91. end
  92. test "prefix= will raise an error if value starts with with /admin" do
  93. assert_raises ArgumentError do
  94. TestResource.prefix = '/admin/old/prefix/structure/'
  95. end
  96. end
  97. test "#headers propagates changes to subclasses" do
  98. ShopifyAPI::Base.headers['X-Custom'] = "the value"
  99. assert_equal "the value", ShopifyAPI::Base.headers['X-Custom']
  100. assert_equal "the value", ShopifyAPI::Product.headers['X-Custom']
  101. end
  102. test "#headers clears changes to subclasses" do
  103. ShopifyAPI::Base.headers['X-Custom'] = "the value"
  104. assert_equal "the value", ShopifyAPI::Product.headers['X-Custom']
  105. ShopifyAPI::Base.headers['X-Custom'] = nil
  106. assert_nil ShopifyAPI::Product.headers['X-Custom']
  107. end
  108. test "#headers set in the main thread affect spawned threads" do
  109. ShopifyAPI::Base.headers['X-Custom'] = "the value"
  110. Thread.new do
  111. assert_equal "the value", ShopifyAPI::Base.headers['X-Custom']
  112. end.join
  113. end
  114. test "#headers set in spawned threads do not affect the main thread" do
  115. Thread.new do
  116. ShopifyAPI::Base.headers['X-Custom'] = "the value"
  117. end.join
  118. assert_nil ShopifyAPI::Base.headers['X-Custom']
  119. end
  120. test "using a different version changes the url" do
  121. release_2019_01 = ShopifyAPI::Session.new(domain: 'shop1.myshopify.com', token: 'token1', api_version: '2019-01')
  122. unstable_version = ShopifyAPI::Session.new(domain: 'shop2.myshopify.com', token: 'token2', api_version: :unstable)
  123. fake(
  124. "shop",
  125. url: "https://shop1.myshopify.com/admin/api/2019-01/shop.json",
  126. method: :get,
  127. status: 201,
  128. body: '{ "shop": { "id": 1 } }'
  129. )
  130. fake(
  131. "shop",
  132. url: "https://shop2.myshopify.com/admin/api/unstable/shop.json",
  133. method: :get,
  134. status: 201,
  135. body: '{ "shop": { "id": 2 } }'
  136. )
  137. ShopifyAPI::Base.activate_session(release_2019_01)
  138. assert_equal 1, ShopifyAPI::Shop.current.id
  139. ShopifyAPI::Base.activate_session(unstable_version)
  140. assert_equal 2, ShopifyAPI::Shop.current.id
  141. end
  142. test "#api_version= should set ApiVersion" do
  143. ShopifyAPI::Base.api_version = '2019-04'
  144. assert_equal '2019-04', ShopifyAPI::Base.api_version.to_s
  145. end
  146. test "#api_version= nil should set ApiVersion to ShopifyAPI::ApiVersion::NullVersion" do
  147. ShopifyAPI::Base.api_version = nil
  148. assert_equal ShopifyAPI::ApiVersion::NullVersion, ShopifyAPI::Base.api_version
  149. end
  150. test "#api_version= ShopifyAPI::ApiVersion::NullVersion should set ApiVersion to ShopifyAPI::ApiVersion::NullVersion" do
  151. ShopifyAPI::Base.api_version = ShopifyAPI::ApiVersion::NullVersion
  152. assert_equal ShopifyAPI::ApiVersion::NullVersion, ShopifyAPI::Base.api_version
  153. end
  154. test "#version_validation! does not raise is api_version is newer or equal to minimum supported version" do
  155. ShopifyAPI::Base.api_version = '2020-01'
  156. assert_nil ShopifyAPI::Base::version_validation!('2020-01')
  157. end
  158. test "#version_validation! raises NotImplemetedError if api_version is older than minimum supported version" do
  159. ShopifyAPI::Base.api_version = '2019-10'
  160. exception = assert_raises NotImplementedError do
  161. ShopifyAPI::Base::version_validation!('2020-01')
  162. end
  163. assert_equal 'The minimum supported version is 2020-01.', exception.message
  164. end
  165. def clear_header(header)
  166. [ActiveResource::Base, ShopifyAPI::Base, ShopifyAPI::Product].each do |klass|
  167. klass.headers.delete(header)
  168. end
  169. end
  170. class TestResource < ShopifyAPI::Base
  171. end
  172. end