base_test.rb 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104
  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 "#delete should send custom headers with request" do
  41. ShopifyAPI::Base.activate_session @session1
  42. ShopifyAPI::Base.headers['X-Custom'] = 'abc'
  43. ShopifyAPI::Base.connection.expects(:delete).with('/admin/bases/1.json', has_entry('X-Custom', 'abc'))
  44. ShopifyAPI::Base.delete "1"
  45. end
  46. test "#headers includes the User-Agent" do
  47. assert_not_includes ActiveResource::Base.headers.keys, 'User-Agent'
  48. assert_includes ShopifyAPI::Base.headers.keys, 'User-Agent'
  49. thread = Thread.new do
  50. assert_includes ShopifyAPI::Base.headers.keys, 'User-Agent'
  51. end
  52. thread.join
  53. end
  54. if ActiveResource::VERSION::MAJOR >= 4
  55. test "#headers propagates changes to subclasses" do
  56. ShopifyAPI::Base.headers['X-Custom'] = "the value"
  57. assert_equal "the value", ShopifyAPI::Base.headers['X-Custom']
  58. assert_equal "the value", ShopifyAPI::Product.headers['X-Custom']
  59. end
  60. test "#headers clears changes to subclasses" do
  61. ShopifyAPI::Base.headers['X-Custom'] = "the value"
  62. assert_equal "the value", ShopifyAPI::Product.headers['X-Custom']
  63. ShopifyAPI::Base.headers['X-Custom'] = nil
  64. assert_nil ShopifyAPI::Product.headers['X-Custom']
  65. end
  66. test "#headers set in the main thread affect spawned threads" do
  67. ShopifyAPI::Base.headers['X-Custom'] = "the value"
  68. Thread.new do
  69. assert_equal "the value", ShopifyAPI::Base.headers['X-Custom']
  70. end.join
  71. end
  72. test "#headers set in spawned threads do not affect the main thread" do
  73. Thread.new do
  74. ShopifyAPI::Base.headers['X-Custom'] = "the value"
  75. end.join
  76. assert_nil ShopifyAPI::Base.headers['X-Custom']
  77. end
  78. end
  79. def clear_header(header)
  80. [ActiveResource::Base, ShopifyAPI::Base, ShopifyAPI::Product].each do |klass|
  81. klass.headers.delete(header)
  82. end
  83. end
  84. end