base_test.rb 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173
  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_equal nil, ShopifyAPI::Base.user
  28. assert_equal nil, ShopifyAPI::Base.password
  29. assert_equal 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. TestResource.prefix = 'a/regular/path/'
  77. assert_equal('/admin/api/2019-01/a/regular/path/', TestResource.prefix)
  78. end
  79. test "prefix= will raise an error if value starts with with /admin" do
  80. assert_raises ArgumentError do
  81. TestResource.prefix = '/admin/old/prefix/structure/'
  82. end
  83. end
  84. test "#headers propagates changes to subclasses" do
  85. ShopifyAPI::Base.headers['X-Custom'] = "the value"
  86. assert_equal "the value", ShopifyAPI::Base.headers['X-Custom']
  87. assert_equal "the value", ShopifyAPI::Product.headers['X-Custom']
  88. end
  89. test "#headers clears changes to subclasses" do
  90. ShopifyAPI::Base.headers['X-Custom'] = "the value"
  91. assert_equal "the value", ShopifyAPI::Product.headers['X-Custom']
  92. ShopifyAPI::Base.headers['X-Custom'] = nil
  93. assert_nil ShopifyAPI::Product.headers['X-Custom']
  94. end
  95. test "#headers set in the main thread affect spawned threads" do
  96. ShopifyAPI::Base.headers['X-Custom'] = "the value"
  97. Thread.new do
  98. assert_equal "the value", ShopifyAPI::Base.headers['X-Custom']
  99. end.join
  100. end
  101. test "#headers set in spawned threads do not affect the main thread" do
  102. Thread.new do
  103. ShopifyAPI::Base.headers['X-Custom'] = "the value"
  104. end.join
  105. assert_nil ShopifyAPI::Base.headers['X-Custom']
  106. end
  107. test "using a different version changes the url" do
  108. release_2019_01 = ShopifyAPI::Session.new(domain: 'shop1.myshopify.com', token: 'token1', api_version: '2019-01')
  109. unstable_version = ShopifyAPI::Session.new(domain: 'shop2.myshopify.com', token: 'token2', api_version: :unstable)
  110. fake(
  111. "shop",
  112. url: "https://shop1.myshopify.com/admin/api/2019-01/shop.json",
  113. method: :get,
  114. status: 201,
  115. body: '{ "shop": { "id": 1 } }'
  116. )
  117. fake(
  118. "shop",
  119. url: "https://shop2.myshopify.com/admin/api/unstable/shop.json",
  120. method: :get,
  121. status: 201,
  122. body: '{ "shop": { "id": 2 } }'
  123. )
  124. ShopifyAPI::Base.activate_session(release_2019_01)
  125. assert_equal 1, ShopifyAPI::Shop.current.id
  126. ShopifyAPI::Base.activate_session(unstable_version)
  127. assert_equal 2, ShopifyAPI::Shop.current.id
  128. end
  129. test "#api_version should set ApiVersion" do
  130. ShopifyAPI::Base.api_version = '2019-04'
  131. assert_equal '2019-04', ShopifyAPI::Base.api_version.to_s
  132. end
  133. def clear_header(header)
  134. [ActiveResource::Base, ShopifyAPI::Base, ShopifyAPI::Product].each do |klass|
  135. klass.headers.delete(header)
  136. end
  137. end
  138. class TestResource < ShopifyAPI::Base
  139. end
  140. end