base_test.rb 7.7 KB

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