test_helper.rb 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124
  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. end
  32. end
  33. ShopifyAPI::Base.clear_session
  34. fake("apis",
  35. url: "https://app.shopify.com/services/apis.json",
  36. method: :get,
  37. status: 200,
  38. api_version: :stub,
  39. body: load_fixture('apis'))
  40. ShopifyAPI::ApiVersion.fetch_known_versions
  41. session = ShopifyAPI::Session.new(
  42. domain: "https://this-is-my-test-shop.myshopify.com",
  43. token: "token_test_helper",
  44. api_version: '2019-01',
  45. )
  46. ShopifyAPI::Base.activate_session(session)
  47. end
  48. def teardown
  49. ShopifyAPI::Base.clear_session
  50. ShopifyAPI::Base.site = nil
  51. ShopifyAPI::Base.password = nil
  52. ShopifyAPI::Base.user = nil
  53. ShopifyAPI::ApiVersion.clear_known_versions
  54. ShopifyAPI::ApiVersion.version_lookup_mode = :raise_on_unknown
  55. end
  56. # Custom Assertions
  57. def assert_not(expression)
  58. refute expression, "Expected <#{expression}> to be false!"
  59. end
  60. def assert_nothing_raised
  61. yield
  62. end
  63. def assert_not_includes(array, value)
  64. refute array.include?(value)
  65. end
  66. def assert_includes(array, value)
  67. assert array.include?(value)
  68. end
  69. def load_fixture(name, format=:json)
  70. File.read(File.dirname(__FILE__) + "/fixtures/#{name}.#{format}")
  71. end
  72. def assert_request_body(expected)
  73. assert_equal expected, WebMock.last_request.body
  74. end
  75. def fake(endpoint, options={})
  76. request_body = options.has_key?(:request_body) ? options.delete(:request_body) : nil
  77. body = options.has_key?(:body) ? options.delete(:body) : load_fixture(endpoint)
  78. format = options.delete(:format) || :json
  79. method = options.delete(:method) || :get
  80. api_version = options.delete(:api_version) || ShopifyAPI::ApiVersion.find_version('2019-01')
  81. extension = ".#{options.delete(:extension)||'json'}" unless options[:extension]==false
  82. status = options.delete(:status) || 200
  83. url = if options.has_key?(:url)
  84. options[:url]
  85. else
  86. "https://this-is-my-test-shop.myshopify.com#{api_version.construct_api_path("#{endpoint}#{extension}")}"
  87. end
  88. stubbing = WebMock.stub_request(method, url)
  89. stubbing = stubbing.with(body: request_body) if request_body
  90. stubbing.to_return(
  91. body: body, status: status, headers: { content_type: "text/#{format}", content_length: 1 }.merge(options)
  92. )
  93. end
  94. def ar_version_before?(version_string)
  95. Gem::Version.new(ActiveResource::VERSION::STRING) < Gem::Version.new(version_string)
  96. end
  97. def ar_version_after?(version_string)
  98. Gem::Version.new(version_string) < Gem::Version.new(ActiveResource::VERSION::STRING)
  99. end
  100. end
  101. end
  102. end