test_helper.rb 3.8 KB

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