test_helper.rb 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125
  1. require 'rubygems'
  2. require 'minitest/autorun'
  3. require 'webmock/minitest'
  4. require_relative 'lib/webmock_extensions/last_request'
  5. require 'mocha/minitest'
  6. require 'pry'
  7. $LOAD_PATH.unshift(File.dirname(__FILE__))
  8. $LOAD_PATH.unshift(File.join(File.dirname(__FILE__), '..', 'lib'))
  9. WebMock.disable_net_connect!
  10. require 'shopify_api'
  11. # setup ShopifyAPI with fake api_key and secret
  12. module Test
  13. module Unit
  14. class TestCase < Minitest::Test
  15. def self.test(string, &block)
  16. define_method("test_#{string}", &block)
  17. end
  18. def self.should(string, &block)
  19. self.test("should_#{string}", &block)
  20. end
  21. def self.context(_string)
  22. yield
  23. end
  24. def setup
  25. ActiveResource::Base.format = :json
  26. ShopifyAPI.constants.each do |const|
  27. begin
  28. const = mod.const_get(const)
  29. const.format = :json if const.respond_to?(:format=)
  30. rescue NameError
  31. # Do nothing
  32. end
  33. end
  34. ShopifyAPI::Base.clear_session
  35. fake("apis",
  36. url: "https://app.shopify.com/services/apis.json",
  37. method: :get,
  38. status: 200,
  39. api_version: :stub,
  40. body: load_fixture('apis'))
  41. ShopifyAPI::ApiVersion.fetch_known_versions
  42. session = ShopifyAPI::Session.new(
  43. domain: "https://this-is-my-test-shop.myshopify.com",
  44. token: "token_test_helper",
  45. api_version: '2019-01',
  46. )
  47. ShopifyAPI::Base.activate_session(session)
  48. end
  49. def teardown
  50. ShopifyAPI::Base.clear_session
  51. ShopifyAPI::Base.site = nil
  52. ShopifyAPI::Base.password = nil
  53. ShopifyAPI::Base.user = nil
  54. ShopifyAPI::ApiVersion.clear_known_versions
  55. ShopifyAPI::ApiVersion.version_lookup_mode = :raise_on_unknown
  56. end
  57. # Custom Assertions
  58. def assert_not(expression)
  59. refute expression, "Expected <#{expression}> to be false!"
  60. end
  61. def assert_nothing_raised
  62. yield
  63. end
  64. def assert_not_includes(array, value)
  65. refute array.include?(value)
  66. end
  67. def assert_includes(array, value)
  68. assert array.include?(value)
  69. end
  70. def load_fixture(name, format=:json)
  71. File.read(File.dirname(__FILE__) + "/fixtures/#{name}.#{format}")
  72. end
  73. def assert_request_body(expected)
  74. assert_equal expected, WebMock.last_request.body
  75. end
  76. def fake(endpoint, options={})
  77. request_body = options.has_key?(:request_body) ? options.delete(:request_body) : nil
  78. body = options.has_key?(:body) ? options.delete(:body) : load_fixture(endpoint)
  79. format = options.delete(:format) || :json
  80. method = options.delete(:method) || :get
  81. api_version = options.delete(:api_version) || ShopifyAPI::ApiVersion.find_version('2019-01')
  82. extension = ".#{options.delete(:extension)||'json'}" unless options[:extension]==false
  83. status = options.delete(:status) || 200
  84. url = if options.has_key?(:url)
  85. options[:url]
  86. else
  87. "https://this-is-my-test-shop.myshopify.com#{api_version.construct_api_path("#{endpoint}#{extension}")}"
  88. end
  89. stubbing = WebMock.stub_request(method, url)
  90. stubbing = stubbing.with(body: request_body) if request_body
  91. stubbing.to_return(
  92. body: body, status: status, headers: { content_type: "text/#{format}", content_length: 1 }.merge(options)
  93. )
  94. end
  95. def ar_version_before?(version_string)
  96. Gem::Version.new(ActiveResource::VERSION::STRING) < Gem::Version.new(version_string)
  97. end
  98. def ar_version_after?(version_string)
  99. Gem::Version.new(version_string) < Gem::Version.new(ActiveResource::VERSION::STRING)
  100. end
  101. end
  102. end
  103. end