base_test.rb 5.8 KB

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