base_test.rb 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112
  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/admin', ShopifyAPI::Base.site.to_s
  14. assert_equal 'https://shop1.myshopify.com/admin', 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 site and headers from Base' do
  20. ShopifyAPI::Base.activate_session @session1
  21. ShopifyAPI::Base.clear_session
  22. assert_nil ActiveResource::Base.site
  23. assert_nil ShopifyAPI::Base.site
  24. assert_nil ShopifyAPI::Shop.site
  25. assert_nil ActiveResource::Base.headers['X-Shopify-Access-Token']
  26. assert_nil ShopifyAPI::Base.headers['X-Shopify-Access-Token']
  27. assert_nil ShopifyAPI::Shop.headers['X-Shopify-Access-Token']
  28. end
  29. test '#activate_session with one session, then clearing and activating with another session should send request to correct shop' do
  30. ShopifyAPI::Base.activate_session @session1
  31. ShopifyAPI::Base.clear_session
  32. ShopifyAPI::Base.activate_session @session2
  33. assert_nil ActiveResource::Base.site
  34. assert_equal 'https://shop2.myshopify.com/admin', ShopifyAPI::Base.site.to_s
  35. assert_equal 'https://shop2.myshopify.com/admin', ShopifyAPI::Shop.site.to_s
  36. assert_nil ActiveResource::Base.headers['X-Shopify-Access-Token']
  37. assert_equal 'token2', ShopifyAPI::Base.headers['X-Shopify-Access-Token']
  38. assert_equal 'token2', ShopifyAPI::Shop.headers['X-Shopify-Access-Token']
  39. end
  40. test '#activate_session with nil raises an InvalidSessionError' do
  41. assert_raises ShopifyAPI::Base::InvalidSessionError do
  42. ShopifyAPI::Base.activate_session nil
  43. end
  44. end
  45. test "#delete should send custom headers with request" do
  46. ShopifyAPI::Base.activate_session @session1
  47. ShopifyAPI::Base.headers['X-Custom'] = 'abc'
  48. ShopifyAPI::Base.connection.expects(:delete).with('/admin/bases/1.json', has_entry('X-Custom', 'abc'))
  49. ShopifyAPI::Base.delete "1"
  50. end
  51. test "#headers includes the User-Agent" do
  52. assert_not_includes ActiveResource::Base.headers.keys, 'User-Agent'
  53. assert_includes ShopifyAPI::Base.headers.keys, 'User-Agent'
  54. thread = Thread.new do
  55. assert_includes ShopifyAPI::Base.headers.keys, 'User-Agent'
  56. end
  57. thread.join
  58. end
  59. if ActiveResource::VERSION::MAJOR >= 4
  60. test "#headers propagates changes to subclasses" do
  61. ShopifyAPI::Base.headers['X-Custom'] = "the value"
  62. assert_equal "the value", ShopifyAPI::Base.headers['X-Custom']
  63. assert_equal "the value", ShopifyAPI::Product.headers['X-Custom']
  64. end
  65. test "#headers clears changes to subclasses" do
  66. ShopifyAPI::Base.headers['X-Custom'] = "the value"
  67. assert_equal "the value", ShopifyAPI::Product.headers['X-Custom']
  68. ShopifyAPI::Base.headers['X-Custom'] = nil
  69. assert_nil ShopifyAPI::Product.headers['X-Custom']
  70. end
  71. end
  72. if ActiveResource::VERSION::MAJOR >= 5 || (ActiveResource::VERSION::MAJOR >= 4 && ActiveResource::VERSION::PRE == "threadsafe")
  73. test "#headers set in the main thread affect spawned threads" do
  74. ShopifyAPI::Base.headers['X-Custom'] = "the value"
  75. Thread.new do
  76. assert_equal "the value", ShopifyAPI::Base.headers['X-Custom']
  77. end.join
  78. end
  79. test "#headers set in spawned threads do not affect the main thread" do
  80. Thread.new do
  81. ShopifyAPI::Base.headers['X-Custom'] = "the value"
  82. end.join
  83. assert_nil ShopifyAPI::Base.headers['X-Custom']
  84. end
  85. end
  86. def clear_header(header)
  87. [ActiveResource::Base, ShopifyAPI::Base, ShopifyAPI::Product].each do |klass|
  88. klass.headers.delete(header)
  89. end
  90. end
  91. end