detailed_log_subscriber_test.rb 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143
  1. # frozen_string_literal: true
  2. require 'test_helper'
  3. require "active_support/log_subscriber/test_helper"
  4. require 'json'
  5. class LogSubscriberTest < Test::Unit::TestCase
  6. include ActiveSupport::LogSubscriber::TestHelper
  7. attr_reader :request_headers
  8. def setup
  9. super
  10. @page = { page: { id: 1, title: 'Shopify API' } }.to_json
  11. @ua_header = "\"User-Agent\"=>\"ShopifyAPI/#{ShopifyAPI::VERSION} " \
  12. "ActiveResource/#{ActiveResource::VERSION::STRING} Ruby/#{RUBY_VERSION}\""
  13. @request_headers = "Headers: {\"Accept\"=>\"application/json\", " \
  14. "#{@ua_header}, \"X-Shopify-Access-Token\"=>\"access_token\"}"
  15. ShopifyAPI::Base.clear_session
  16. fake(
  17. "apis",
  18. url: "https://app.shopify.com/services/apis.json",
  19. method: :get,
  20. status: 200,
  21. api_version: :stub,
  22. body: load_fixture('apis')
  23. )
  24. ShopifyAPI::ApiVersion.fetch_known_versions
  25. session = ShopifyAPI::Session.new(
  26. domain: "https://this-is-my-test-shop.myshopify.com",
  27. token: "access_token",
  28. api_version: '2019-01'
  29. )
  30. ShopifyAPI::Base.activate_session(session)
  31. ActiveResource::LogSubscriber.attach_to(:active_resource)
  32. ActiveResource::DetailedLogSubscriber.attach_to(:active_resource_detailed)
  33. end
  34. def teardown
  35. super
  36. ShopifyAPI::ApiVersion.clear_known_versions
  37. ShopifyAPI::ApiVersion.version_lookup_mode = :raise_on_unknown
  38. end
  39. def set_logger(logger)
  40. ActiveResource::Base.logger = logger
  41. end
  42. test "logging on #find" do
  43. fake("pages/1", method: :get, body: @page)
  44. ShopifyAPI::Page.find(1)
  45. assert_equal(4, @logger.logged(:info).size)
  46. assert_equal(
  47. "GET https://this-is-my-test-shop.myshopify.com:443/admin/api/2019-01/pages/1.json",
  48. @logger.logged(:info)[0]
  49. )
  50. assert_match(/\-\-\> 200/, @logger.logged(:info)[1])
  51. assert_equal(request_headers, @logger.logged(:info)[2])
  52. assert_match(
  53. %r{(Response:\n\{\"page\"\:\{\"id\"\:1,\"title\"\:\"Shopify API\"\}\})|
  54. (Response:\n\{\"page\"\:\{\"title\"\:\"Shopify API\",\"id\"\:1\}\})},
  55. @logger.logged(:info)[3]
  56. )
  57. end
  58. test "logging on #find with an error" do
  59. fake("pages/2", method: :get, body: nil, status: 404)
  60. assert_raises(ActiveResource::ResourceNotFound) do
  61. ShopifyAPI::Page.find(2)
  62. end
  63. if ar_version_before?('5.1.0')
  64. assert_equal(4, @logger.logged(:info).size)
  65. assert_equal(
  66. "GET https://this-is-my-test-shop.myshopify.com:443/admin/api/2019-01/pages/2.json",
  67. @logger.logged(:info)[0]
  68. )
  69. assert_match(/\-\-\> 404/, @logger.logged(:info)[1])
  70. assert_equal(request_headers, @logger.logged(:info)[2])
  71. assert_equal("Response:", @logger.logged(:info)[3])
  72. else
  73. assert_equal(2, @logger.logged(:error).size)
  74. assert_equal(
  75. "GET https://this-is-my-test-shop.myshopify.com:443/admin/api/2019-01/pages/2.json",
  76. @logger.logged(:error)[0]
  77. )
  78. assert_match(/\-\-\> 404/, @logger.logged(:error)[1])
  79. assert_equal(2, @logger.logged(:info).size)
  80. assert_equal(request_headers, @logger.logged(:info)[0])
  81. assert_equal("Response:", @logger.logged(:info)[1])
  82. end
  83. end
  84. test "warns when the server responds with a x-shopify-api-deprecated-reason header" do
  85. fake(
  86. "pages/1",
  87. method: :get,
  88. body: @page,
  89. x_shopify_api_deprecated_reason: 'https://help.shopify.com/en/api/getting-started/api-deprecations'
  90. )
  91. ShopifyAPI::Page.find(1)
  92. assert_equal(1, @logger.logged(:warn).size)
  93. assert_match(
  94. %r{\[DEPRECATED\] ShopifyAPI made a call to GET /admin/api/2019-01/pages/1.json},
  95. @logger.logged(:warn).first
  96. )
  97. assert_match(
  98. %r{See https:\/\/help.shopify.com\/en\/api\/getting-started\/api-deprecations for more details.},
  99. @logger.logged(:warn).first
  100. )
  101. end
  102. test "warns when the server responds with a x-shopify-api-version-warning header" do
  103. fake(
  104. "pages/1",
  105. method: :get,
  106. body: @page,
  107. x_shopify_api_version_warning: 'https://shopify.dev/concepts/about-apis/versioning'
  108. )
  109. ShopifyAPI::Page.find(1)
  110. assert_equal(1, @logger.logged(:warn).size)
  111. assert_match(
  112. %r{\[API Version Warning\] ShopifyAPI made a call to GET /admin/api/2019-01/pages/1.json},
  113. @logger.logged(:warn).first
  114. )
  115. assert_match(
  116. %r{See https:\/\/shopify.dev\/concepts\/about-apis\/versioning for more details.},
  117. @logger.logged(:warn).first
  118. )
  119. end
  120. end