Skip to content

armgit / spree

Public Imported from GitHub
0

Open Source Platform for DTC, B2B Commerce, Marketplaces & Omnichannel. REST APIs, TypeScript SDKs, and production-ready Next.js storefront. Self-host it. Own your data. No vendor lock-in. Zero platform fees.

Add logging before/after request.

Sebastian Palma38de89f99096
diff --git a/api/app/services/spree/webhooks/endpoints/make_request.rb b/api/app/services/spree/webhooks/endpoints/make_request.rb
index e7fac1130c..570e7da70d 100644
--- a/api/app/services/spree/webhooks/endpoints/make_request.rb
+++ b/api/app/services/spree/webhooks/endpoints/make_request.rb
@@ -19,17 +19,36 @@ module Spree
def make_request(body:, url:)
uri = URI(url)
+ Rails.logger.debug('logging')
if uri.path == '' && uri.host.nil? && uri.port.nil?
+ Rails.logger.debug('webhook request finished with errors')
failure(false)
else
- request = Net::HTTP::Post.new(uri.path, HEADERS)
- request.body = body
- http = Net::HTTP.new(uri.host, uri.port)
- http.use_ssl = true unless Rails.env.development? || Rails.env.test?
- code_type = http.request(request).code_type
- success(code_type == Net::HTTPOK)
+ if request_code_type(body, uri) == Net::HTTPOK
+ Rails.logger.debug('webhook sent successfully')
+ success(true)
+ else
+ Rails.logger.warn('webhook request finished with errors')
+ success(false)
+ end
end
end
+
+ def request_code_type(body, uri)
+ http = Net::HTTP.new(uri.host, uri.port)
+ http.use_ssl = true if ssl?
+ http.request(request(body, uri.path)).code_type
+ end
+
+ def request(body, uri_path)
+ req = Net::HTTP::Post.new(uri_path, HEADERS)
+ req.body = body
+ req
+ end
+
+ def ssl?
+ !(Rails.env.development? || Rails.env.test?)
+ end
end
end
end
diff --git a/api/spec/serializers/spree/api/v2/platform/menu_item_serializer_spec.rb b/api/spec/serializers/spree/api/v2/platform/menu_item_serializer_spec.rb
index b463ba11e4..631fc31953 100644
--- a/api/spec/serializers/spree/api/v2/platform/menu_item_serializer_spec.rb
+++ b/api/spec/serializers/spree/api/v2/platform/menu_item_serializer_spec.rb
@@ -5,18 +5,13 @@ describe Spree::Api::V2::Platform::MenuItemSerializer do
let(:menu) { create(:menu) }
let(:menu_item) { create(:menu_item, menu: menu, linked_resource: create(:taxon)) }
- let(:children) do
+ let!(:children) do
[
create(:menu_item, parent_id: menu_item.id, menu: menu),
create(:menu_item, parent_id: menu_item.id, menu: menu)
]
end
- before do
- allow_any_instance_of(Spree::Base).to receive(:queue_webhooks_requests!)
- children
- end
-
it { expect(subject.serializable_hash).to be_kind_of(Hash) }
it do
diff --git a/api/spec/serializers/spree/api/v2/platform/taxon_serializer_spec.rb b/api/spec/serializers/spree/api/v2/platform/taxon_serializer_spec.rb
index 6e863c87b7..5258e04c62 100644
--- a/api/spec/serializers/spree/api/v2/platform/taxon_serializer_spec.rb
+++ b/api/spec/serializers/spree/api/v2/platform/taxon_serializer_spec.rb
@@ -9,8 +9,6 @@ describe Spree::Api::V2::Platform::TaxonSerializer do
let(:taxon) { create(:taxon, products: create_list(:product, 2, stores: [store]), taxonomy: taxonomy) }
let!(:children) { [create(:taxon, parent: taxon, taxonomy: taxonomy), create(:taxon, parent: taxon, taxonomy: taxonomy)] }
- before { taxon.reload }
-
it { expect(subject.serializable_hash).to be_kind_of(Hash) }
context 'without products' do
diff --git a/api/spec/services/spree/webhooks/endpoints/make_request_spec.rb b/api/spec/services/spree/webhooks/endpoints/make_request_spec.rb
index 2419d7ae2c..5205a233c7 100644
--- a/api/spec/services/spree/webhooks/endpoints/make_request_spec.rb
+++ b/api/spec/services/spree/webhooks/endpoints/make_request_spec.rb
@@ -47,6 +47,42 @@ describe Spree::Webhooks::Endpoints::MakeRequest do
)
end
+ it 'debug logs before the request' do
+ allow(Rails.logger).to receive(:debug)
+ subject
+ expect(Rails.logger).to have_received(:debug).with('logging')
+ end
+
+ context 'logging after the request' do
+ context 'when fail' do
+ before do
+ http_double = instance_double(Net::HTTP)
+ allow(Net::HTTP).to receive(:new).and_return(http_double)
+ allow(http_double).to(
+ receive(:request).and_return(
+ double(:request).tap do |request|
+ allow(request).to receive(:code_type).and_return(Net::HTTPClientError)
+ end
+ )
+ )
+ end
+
+ it 'warn logs after the request' do
+ allow(Rails.logger).to receive(:warn)
+ subject
+ expect(Rails.logger).to have_received(:warn).with('webhook request finished with errors')
+ end
+ end
+
+ context 'when success' do
+ it 'debug logs after the request' do
+ allow(Rails.logger).to receive(:debug)
+ subject
+ expect(Rails.logger).to have_received(:debug).with('webhook sent successfully')
+ end
+ end
+ end
+
context 'when request code_type is Net::HTTPOK' do
it 'returns true wrapped into a success' do
expect(subject.value).to eq(true)
diff --git a/api/spec/services/spree/webhooks/endpoints/queue_requests_spec.rb b/api/spec/services/spree/webhooks/endpoints/queue_requests_spec.rb
index 76b3df84ae..b0a5114859 100644
--- a/api/spec/services/spree/webhooks/endpoints/queue_requests_spec.rb
+++ b/api/spec/services/spree/webhooks/endpoints/queue_requests_spec.rb
@@ -11,9 +11,7 @@ describe Spree::Webhooks::Endpoints::QueueRequests, :job, :spree_webhooks do
context 'without subscriptions for the given event' do
it 'does not queue a job to make a request' do
- expect { subject }.not_to(
- have_enqueued_job(Spree::Webhooks::Endpoints::MakeRequest).on_queue(queue)
- )
+ expect { subject }.not_to have_enqueued_job(make_request_job).on_queue(queue)
end
end