base_test.rb 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125
  1. require 'test_helper'
  2. class BaseTest < Test::Unit::TestCase
  3. def setup
  4. @session1 = ShopifyAPI::Session.new('shop1.myshopify.com', 'token1')
  5. @session2 = ShopifyAPI::Session.new('shop2.myshopify.com', 'token2')
  6. end
  7. def teardown
  8. clear_header('X-Custom')
  9. end
  10. test '#activate_session should set site and headers for given session' do
  11. ShopifyAPI::Base.activate_session @session1
  12. assert_nil ActiveResource::Base.site
  13. assert_equal 'https://shop1.myshopify.com', ShopifyAPI::Base.site.to_s
  14. assert_equal 'https://shop1.myshopify.com', ShopifyAPI::Shop.site.to_s
  15. assert_nil ActiveResource::Base.headers['X-Shopify-Access-Token']
  16. assert_equal 'token1', ShopifyAPI::Base.headers['X-Shopify-Access-Token']
  17. assert_equal 'token1', ShopifyAPI::Shop.headers['X-Shopify-Access-Token']
  18. end
  19. test '#clear_session should clear base site settings from Base' do
  20. ShopifyAPI::Base.site = "https://foo:bar@www.zombo.com"
  21. assert_equal "foo", ShopifyAPI::Base.user
  22. assert_equal "bar", ShopifyAPI::Base.password
  23. ShopifyAPI::Base.clear_session
  24. assert_equal nil, ShopifyAPI::Base.user
  25. assert_equal nil, ShopifyAPI::Base.password
  26. assert_equal nil, ShopifyAPI::Base.site
  27. end
  28. test '#clear_session should clear site and headers from Base' do
  29. ShopifyAPI::Base.activate_session @session1
  30. ShopifyAPI::Base.clear_session
  31. assert_nil ActiveResource::Base.site
  32. assert_nil ShopifyAPI::Base.site
  33. assert_nil ShopifyAPI::Shop.site
  34. assert_nil ActiveResource::Base.headers['X-Shopify-Access-Token']
  35. assert_nil ShopifyAPI::Base.headers['X-Shopify-Access-Token']
  36. assert_nil ShopifyAPI::Shop.headers['X-Shopify-Access-Token']
  37. end
  38. test '#activate_session with one session, then clearing and activating with another session should send request to correct shop' do
  39. ShopifyAPI::Base.activate_session @session1
  40. ShopifyAPI::Base.clear_session
  41. ShopifyAPI::Base.activate_session @session2
  42. assert_nil ActiveResource::Base.site
  43. assert_equal 'https://shop2.myshopify.com', ShopifyAPI::Base.site.to_s
  44. assert_equal 'https://shop2.myshopify.com', ShopifyAPI::Shop.site.to_s
  45. assert_nil ActiveResource::Base.headers['X-Shopify-Access-Token']
  46. assert_equal 'token2', ShopifyAPI::Base.headers['X-Shopify-Access-Token']
  47. assert_equal 'token2', ShopifyAPI::Shop.headers['X-Shopify-Access-Token']
  48. end
  49. test '#activate_session with nil raises an InvalidSessionError' do
  50. assert_raises ShopifyAPI::Base::InvalidSessionError do
  51. ShopifyAPI::Base.activate_session nil
  52. end
  53. end
  54. test "#delete should send custom headers with request" do
  55. ShopifyAPI::Base.activate_session @session1
  56. ShopifyAPI::Base.headers['X-Custom'] = 'abc'
  57. ShopifyAPI::Base.connection.expects(:delete).with('/admin/bases/1.json', has_entry('X-Custom', 'abc'))
  58. ShopifyAPI::Base.delete "1"
  59. end
  60. test "#headers includes the User-Agent" do
  61. assert_not_includes ActiveResource::Base.headers.keys, 'User-Agent'
  62. assert_includes ShopifyAPI::Base.headers.keys, 'User-Agent'
  63. thread = Thread.new do
  64. assert_includes ShopifyAPI::Base.headers.keys, 'User-Agent'
  65. end
  66. thread.join
  67. end
  68. if ActiveResource::VERSION::MAJOR >= 4
  69. test "#headers propagates changes to subclasses" do
  70. ShopifyAPI::Base.headers['X-Custom'] = "the value"
  71. assert_equal "the value", ShopifyAPI::Base.headers['X-Custom']
  72. assert_equal "the value", ShopifyAPI::Product.headers['X-Custom']
  73. end
  74. test "#headers clears changes to subclasses" do
  75. ShopifyAPI::Base.headers['X-Custom'] = "the value"
  76. assert_equal "the value", ShopifyAPI::Product.headers['X-Custom']
  77. ShopifyAPI::Base.headers['X-Custom'] = nil
  78. assert_nil ShopifyAPI::Product.headers['X-Custom']
  79. end
  80. end
  81. if ActiveResource::VERSION::MAJOR >= 5 || (ActiveResource::VERSION::MAJOR >= 4 && ActiveResource::VERSION::PRE == "threadsafe")
  82. test "#headers set in the main thread affect spawned threads" do
  83. ShopifyAPI::Base.headers['X-Custom'] = "the value"
  84. Thread.new do
  85. assert_equal "the value", ShopifyAPI::Base.headers['X-Custom']
  86. end.join
  87. end
  88. test "#headers set in spawned threads do not affect the main thread" do
  89. Thread.new do
  90. ShopifyAPI::Base.headers['X-Custom'] = "the value"
  91. end.join
  92. assert_nil ShopifyAPI::Base.headers['X-Custom']
  93. end
  94. end
  95. def clear_header(header)
  96. [ActiveResource::Base, ShopifyAPI::Base, ShopifyAPI::Product].each do |klass|
  97. klass.headers.delete(header)
  98. end
  99. end
  100. end