base_test.rb 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174
  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 '#clear_session should clear site and headers from Base' do
  32. ShopifyAPI::Base.activate_session @session1
  33. ShopifyAPI::Base.clear_session
  34. assert_nil ActiveResource::Base.site
  35. assert_nil ShopifyAPI::Base.site
  36. assert_nil ShopifyAPI::Shop.site
  37. assert_nil ActiveResource::Base.headers['X-Shopify-Access-Token']
  38. assert_nil ShopifyAPI::Base.headers['X-Shopify-Access-Token']
  39. assert_nil ShopifyAPI::Shop.headers['X-Shopify-Access-Token']
  40. end
  41. test '#activate_session with one session, then clearing and activating with another session should send request to correct shop' do
  42. ShopifyAPI::Base.activate_session @session1
  43. ShopifyAPI::Base.clear_session
  44. ShopifyAPI::Base.activate_session @session2
  45. assert_nil ActiveResource::Base.site
  46. assert_equal 'https://shop2.myshopify.com', ShopifyAPI::Base.site.to_s
  47. assert_equal 'https://shop2.myshopify.com', ShopifyAPI::Shop.site.to_s
  48. assert_nil ActiveResource::Base.headers['X-Shopify-Access-Token']
  49. assert_equal 'token2', ShopifyAPI::Base.headers['X-Shopify-Access-Token']
  50. assert_equal 'token2', ShopifyAPI::Shop.headers['X-Shopify-Access-Token']
  51. end
  52. test '#activate_session with nil raises an InvalidSessionError' do
  53. assert_raises ShopifyAPI::Base::InvalidSessionError do
  54. ShopifyAPI::Base.activate_session nil
  55. end
  56. end
  57. test "#delete should send custom headers with request" do
  58. ShopifyAPI::Base.activate_session @session1
  59. ShopifyAPI::Base.headers['X-Custom'] = 'abc'
  60. ShopifyAPI::Base.connection
  61. .expects(:delete)
  62. .with('/admin/api/2019-01/bases/1.json', has_entry('X-Custom', 'abc'))
  63. ShopifyAPI::Base.delete "1"
  64. end
  65. test "#headers includes the User-Agent" do
  66. assert_not_includes ActiveResource::Base.headers.keys, 'User-Agent'
  67. assert_includes ShopifyAPI::Base.headers.keys, 'User-Agent'
  68. thread = Thread.new do
  69. assert_includes ShopifyAPI::Base.headers.keys, 'User-Agent'
  70. end
  71. thread.join
  72. end
  73. test "prefix= will forward to resource when the value does not start with admin" do
  74. session = ShopifyAPI::Session.new(domain: 'shop1.myshopify.com', token: 'token1', api_version: '2019-01')
  75. ShopifyAPI::Base.activate_session(session)
  76. silence_warnings do
  77. TestResource.prefix = 'a/regular/path/'
  78. end
  79. assert_equal('/admin/api/2019-01/a/regular/path/', TestResource.prefix)
  80. end
  81. test "prefix= will raise an error if value starts with with /admin" do
  82. assert_raises ArgumentError do
  83. TestResource.prefix = '/admin/old/prefix/structure/'
  84. end
  85. end
  86. test "#headers propagates changes to subclasses" do
  87. ShopifyAPI::Base.headers['X-Custom'] = "the value"
  88. assert_equal "the value", ShopifyAPI::Base.headers['X-Custom']
  89. assert_equal "the value", ShopifyAPI::Product.headers['X-Custom']
  90. end
  91. test "#headers clears changes to subclasses" do
  92. ShopifyAPI::Base.headers['X-Custom'] = "the value"
  93. assert_equal "the value", ShopifyAPI::Product.headers['X-Custom']
  94. ShopifyAPI::Base.headers['X-Custom'] = nil
  95. assert_nil ShopifyAPI::Product.headers['X-Custom']
  96. end
  97. test "#headers set in the main thread affect spawned threads" do
  98. ShopifyAPI::Base.headers['X-Custom'] = "the value"
  99. Thread.new do
  100. assert_equal "the value", ShopifyAPI::Base.headers['X-Custom']
  101. end.join
  102. end
  103. test "#headers set in spawned threads do not affect the main thread" do
  104. Thread.new do
  105. ShopifyAPI::Base.headers['X-Custom'] = "the value"
  106. end.join
  107. assert_nil ShopifyAPI::Base.headers['X-Custom']
  108. end
  109. test "using a different version changes the url" do
  110. release_2019_01 = ShopifyAPI::Session.new(domain: 'shop1.myshopify.com', token: 'token1', api_version: '2019-01')
  111. unstable_version = ShopifyAPI::Session.new(domain: 'shop2.myshopify.com', token: 'token2', api_version: :unstable)
  112. fake(
  113. "shop",
  114. url: "https://shop1.myshopify.com/admin/api/2019-01/shop.json",
  115. method: :get,
  116. status: 201,
  117. body: '{ "shop": { "id": 1 } }'
  118. )
  119. fake(
  120. "shop",
  121. url: "https://shop2.myshopify.com/admin/api/unstable/shop.json",
  122. method: :get,
  123. status: 201,
  124. body: '{ "shop": { "id": 2 } }'
  125. )
  126. ShopifyAPI::Base.activate_session(release_2019_01)
  127. assert_equal 1, ShopifyAPI::Shop.current.id
  128. ShopifyAPI::Base.activate_session(unstable_version)
  129. assert_equal 2, ShopifyAPI::Shop.current.id
  130. end
  131. test "#api_version should set ApiVersion" do
  132. ShopifyAPI::Base.api_version = '2019-04'
  133. assert_equal '2019-04', ShopifyAPI::Base.api_version.to_s
  134. end
  135. def clear_header(header)
  136. [ActiveResource::Base, ShopifyAPI::Base, ShopifyAPI::Product].each do |klass|
  137. klass.headers.delete(header)
  138. end
  139. end
  140. class TestResource < ShopifyAPI::Base
  141. end
  142. end