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.
Ruby b2b-commercee-commerceecommerceecommerce-apiecommerce-frameworkecommerce-platformheadlessheadless-commerceheadless-ecommercemarketplacemulti-tenantmulti-vendormulti-vendor-ecommerceopen-sourcespree-commerce
Update log messages.
Sebastian Palma5a2a8375bc2e
diff --git a/api/app/jobs/spree/webhooks/endpoints/make_request_job.rb b/api/app/jobs/spree/webhooks/endpoints/make_request_job.rbindex 5a3f49b766..f94f183291 100644--- a/api/app/jobs/spree/webhooks/endpoints/make_request_job.rb+++ b/api/app/jobs/spree/webhooks/endpoints/make_request_job.rb@@ -4,8 +4,8 @@ module Spreeclass MakeRequestJob < Spree::BaseJobqueue_as :spree_webhooks- def perform(body, url)- Spree::Webhooks::Endpoints::MakeRequest.new(body: body, url: url).call+ def perform(body, event, url)+ Spree::Webhooks::Endpoints::MakeRequest.new(body: body, event: event, url: url).callendendenddiff --git a/api/app/services/spree/webhooks/endpoints/make_request.rb b/api/app/services/spree/webhooks/endpoints/make_request.rbindex b272744133..8ec794873c 100644--- a/api/app/services/spree/webhooks/endpoints/make_request.rb+++ b/api/app/services/spree/webhooks/endpoints/make_request.rb@@ -4,39 +4,37 @@ module Spreemodule Webhooksmodule Endpointsclass MakeRequest- def initialize(body:, url:)+ def initialize(body:, event:, url:)@body = body+ @event = event@url = urlenddef callreturn if body == ''- Rails.logger.debug("[SPREE WEBHOOKS] #{object_class_name} #{event_name} sending to #{url}")- Rails.logger.debug("[SPREE WEBHOOKS] body: #{body}")+ Rails.logger.debug(webhooks_log("sending to '#{url}'"))+ Rails.logger.debug(webhooks_log("body: #{body}"))if unprocessable_uri?- Rails.logger.warn(UNPROCESSABLE_MSG)+ Rails.logger.warn(webhooks_log("can not make a request to '#{url}'"))returnendif failed_request?- Rails.logger.warn(FAIL_MSG)+ Rails.logger.warn(webhooks_log("failed for '#{url}'"))returnend- Rails.logger.debug(SUCCESS_MSG)+ Rails.logger.debug(webhooks_log("success for URL '#{url}'"))endprivate- attr_reader :body, :url+ attr_reader :body, :event, :url- FAIL_MSG = '[SPREE WEBHOOKS] #{object_class_name} #{event_name} failed for #{url}'HEADERS = { 'Content-Type' => 'application/json' }.freeze- SUCCESS_MSG = "[SPREE WEBHOOKS] #{object_class_name} #{event_name} success for URL #{url}"- UNPROCESSABLE_MSG = 'Can not make a request to the given URL'- private_constant :FAIL_MSG, :HEADERS, :SUCCESS_MSG, :UNPROCESSABLE_MSG+ private_constant :HEADERSdelegate :host, :path, :port, to: :uri, prefix: true@@ -63,6 +61,10 @@ module Spreedef uriURI(url)end++ def webhooks_log(msg)+ "[SPREE WEBHOOKS] '#{event}' #{msg}"+ endendendenddiff --git a/api/app/services/spree/webhooks/endpoints/queue_requests.rb b/api/app/services/spree/webhooks/endpoints/queue_requests.rbindex 269f1617d0..89bb40c11d 100644--- a/api/app/services/spree/webhooks/endpoints/queue_requests.rb+++ b/api/app/services/spree/webhooks/endpoints/queue_requests.rb@@ -6,7 +6,7 @@ module Spreedef call(body:, event:)urls_subscribed_to(event).each do |url|- Spree::Webhooks::Endpoints::MakeRequestJob.perform_later(body, url)+ Spree::Webhooks::Endpoints::MakeRequestJob.perform_later(body, event, url)endenddiff --git a/api/spec/jobs/spree/webhooks/endpoints/make_request_job_spec.rb b/api/spec/jobs/spree/webhooks/endpoints/make_request_job_spec.rbindex dd1f6741c9..85190bf671 100644--- a/api/spec/jobs/spree/webhooks/endpoints/make_request_job_spec.rb+++ b/api/spec/jobs/spree/webhooks/endpoints/make_request_job_spec.rb@@ -1,12 +1,12 @@require 'spec_helper'-describe Spree::Webhooks::Endpoints::MakeRequestJob, :job do- let(:url) { 'http://url.com/' }+describe Spree::Webhooks::Endpoints::MakeRequestJob dolet(:body) { {} }+ let(:event) { 'order.cancel' }+ let(:queue) { 'spree_webhooks' }+ let(:url) { 'http://url.com/' }- it 'enqueues a HTTP request using Spree::Webhooks::Endpoints::MakeRequest' do- expect { described_class.perform_later(body, url) }.to(- have_enqueued_job.on_queue('spree_webhooks')- )+ it 'enqueues a HTTP request using Spree::Webhooks::Endpoints::MakeRequest', :job do+ expect { described_class.perform_later(body, event, url) }.to have_enqueued_job.on_queue(queue)endenddiff --git a/api/spec/services/spree/webhooks/endpoints/make_request_spec.rb b/api/spec/services/spree/webhooks/endpoints/make_request_spec.rbindex cd19eb7a76..07b62d964f 100644--- a/api/spec/services/spree/webhooks/endpoints/make_request_spec.rb+++ b/api/spec/services/spree/webhooks/endpoints/make_request_spec.rb@@ -2,39 +2,39 @@ require 'spec_helper'describe Spree::Webhooks::Endpoints::MakeRequest dodescribe '#call' do- subject { described_class.new(body: body, url: url).call }+ subject { described_class.new(body: body, event: event, url: url).call }+ let(:event) { 'order.cancel' }let(:url) { 'http://google.com/' }let(:body) { { foo: :bar }.to_json }- shared_examples 'returns a failure without making a request' do- it 'does not instantiate a Net::HTTP::Post to make a request' do+ shared_examples 'returns nil without making a request' do+ it 'does not instantiate Net::HTTP::Post to make a request' doexpect(Net::HTTP::Post).not_to receive(:new)subjectend- it 'returns nil' do- expect(subject).to eq(nil)- end+ it { expect(subject).to eq(nil) }endcontext 'with an unexpected body' dolet(:body) { '' }- include_examples 'returns a failure without making a request'+ include_examples 'returns nil without making a request'endcontext 'with an expected body' docontext 'without a valid URL' dolet(:url) { '' }- include_examples 'returns a failure without making a request'-it 'debug logs before the request' doallow(Rails.logger).to receive(:warn)subject- expect(Rails.logger).to have_received(:warn).with('Can not make a request to the given URL')+ message = "[SPREE WEBHOOKS] 'order.cancel' can not make a request to ''"+ expect(Rails.logger).to have_received(:warn).with(message)end++ include_examples 'returns nil without making a request'endcontext 'with a valid URL' do@@ -42,15 +42,63 @@ describe Spree::Webhooks::Endpoints::MakeRequest dobefore { stub_request(:post, url) }- it 'makes a post HTTP request to the given url and body' do- subject- expect(WebMock).to have_requested(:post, url).with(body: body, headers: headers).once- end-it 'debug logs before the request' doallow(Rails.logger).to receive(:debug)subject- expect(Rails.logger).to have_received(:debug).with("Making a request to #{url} with body #{body}")+ message_fst = "[SPREE WEBHOOKS] 'order.cancel' sending to 'http://google.com/'"+ message_snd = "[SPREE WEBHOOKS] 'order.cancel' body: #{body}"+ expect(Rails.logger).to have_received(:debug).with(message_fst)+ expect(Rails.logger).to have_received(:debug).with(message_snd)+ end++ describe 'ssl usage' do+ let(:uri) { URI(url) }+ let(:http) { Net::HTTP.new(uri.host, uri.port) }++ shared_examples 'makes the request without setting use_ssl' do+ it 'does not set use_ssl' do+ expect(Net::HTTP).to receive(:new).with(uri.host, uri.port).and_return(http)+ expect(http).to_not receive(:use_ssl=)+ subject+ end++ it 'makes a post HTTP request to the given url and body' do+ subject+ expect(WebMock).to have_requested(:post, url).with(body: body, headers: headers).once+ end+ end++ context 'with development environment' do+ before { allow(Rails).to receive_message_chain(:env, :development?).and_return(true) }++ include_examples 'makes the request without setting use_ssl'+ end++ context 'with test environment' do+ include_examples 'makes the request without setting use_ssl'+ end++ context 'without test and/or development environment' do+ before do+ allow(Rails).to receive_message_chain(:env, :development?).and_return(false)+ allow(Rails).to receive_message_chain(:env, :test?).and_return(false)+ end++ let(:url) { 'http://google.com/' }++ it 'does set use_ssl' do+ expect(Net::HTTP).to receive(:new).and_return(http)+ expect(http).to receive(:use_ssl=).with(true)+ subject+ end++ it 'makes a post HTTP request to the given url and body' do+ allow(Net::HTTP).to receive(:new).and_return(http)+ allow(http).to receive(:use_ssl=).with(true)+ subject+ expect(WebMock).to have_requested(:post, url).with(body: body, headers: headers).once+ end+ endendcontext 'when request code_type is not Net::HTTPOK' do@@ -69,17 +117,17 @@ describe Spree::Webhooks::Endpoints::MakeRequest doit 'warn logs after the request' doallow(Rails.logger).to receive(:warn)subject- expect(Rails.logger).to have_received(:warn).with('Webhook request finished with errors')+ message = "[SPREE WEBHOOKS] 'order.cancel' failed for 'http://google.com/'"+ expect(Rails.logger).to have_received(:warn).with(message)end- it 'returns nil' do- expect(subject).to eq(nil)- end+ it { expect(subject).to eq(nil) }endcontext 'when request code_type is Net::HTTPOK' doit 'debug logs after the request and returns its value' do- expect(subject).to eq(Rails.logger.debug('Webhook request completed successfully'))+ message = "[SPREE WEBHOOKS] 'order.cancel' success for URL 'http://google.com/'"+ expect(subject).to eq(Rails.logger.debug(message))endendenddiff --git a/api/spec/services/spree/webhooks/endpoints/queue_requests_spec.rb b/api/spec/services/spree/webhooks/endpoints/queue_requests_spec.rbindex b0a5114859..78630f449b 100644--- a/api/spec/services/spree/webhooks/endpoints/queue_requests_spec.rb+++ b/api/spec/services/spree/webhooks/endpoints/queue_requests_spec.rb@@ -29,7 +29,7 @@ describe Spree::Webhooks::Endpoints::QueueRequests, :job, :spree_webhooks doit 'queues a job to make a request' doexpect { subject }.to(- have_enqueued_job(make_request_job).with(body, endpoint.url).on_queue(queue)+ have_enqueued_job(make_request_job).with(body, event, endpoint.url).on_queue(queue))endend@@ -47,7 +47,7 @@ describe Spree::Webhooks::Endpoints::QueueRequests, :job, :spree_webhooks doit 'queues a job to make a request' doexpect { subject }.to(- have_enqueued_job(make_request_job).with(body, endpoint.url).on_queue(queue)+ have_enqueued_job(make_request_job).with(body, event, endpoint.url).on_queue(queue))endend@@ -62,9 +62,7 @@ describe Spree::Webhooks::Endpoints::QueueRequests, :job, :spree_webhooks doendit 'does not queue a job to make a request' do- expect { subject }.not_to(- have_enqueued_job(make_request_job).with(body, endpoint.url).on_queue(queue)- )+ expect { subject }.not_to have_enqueued_job(make_request_job).on_queue(queue)endend@@ -78,9 +76,7 @@ describe Spree::Webhooks::Endpoints::QueueRequests, :job, :spree_webhooks doendit 'does not queue a job to make a request' do- expect { subject }.not_to(- have_enqueued_job(make_request_job).with(body, endpoint.url).on_queue(queue)- )+ expect { subject }.not_to have_enqueued_job(make_request_job).on_queue(queue)endendend