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.

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.rb
index 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 Spree
class MakeRequestJob < Spree::BaseJob
queue_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).call
end
end
end
diff --git a/api/app/services/spree/webhooks/endpoints/make_request.rb b/api/app/services/spree/webhooks/endpoints/make_request.rb
index 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 Spree
module Webhooks
module Endpoints
class MakeRequest
- def initialize(body:, url:)
+ def initialize(body:, event:, url:)
@body = body
+ @event = event
@url = url
end
def call
return 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}'"))
return
end
if failed_request?
- Rails.logger.warn(FAIL_MSG)
+ Rails.logger.warn(webhooks_log("failed for '#{url}'"))
return
end
- Rails.logger.debug(SUCCESS_MSG)
+ Rails.logger.debug(webhooks_log("success for URL '#{url}'"))
end
private
- 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 :HEADERS
delegate :host, :path, :port, to: :uri, prefix: true
@@ -63,6 +61,10 @@ module Spree
def uri
URI(url)
end
+
+ def webhooks_log(msg)
+ "[SPREE WEBHOOKS] '#{event}' #{msg}"
+ end
end
end
end
diff --git a/api/app/services/spree/webhooks/endpoints/queue_requests.rb b/api/app/services/spree/webhooks/endpoints/queue_requests.rb
index 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 Spree
def 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)
end
end
diff --git a/api/spec/jobs/spree/webhooks/endpoints/make_request_job_spec.rb b/api/spec/jobs/spree/webhooks/endpoints/make_request_job_spec.rb
index 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 do
let(: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)
end
end
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 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 do
describe '#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' do
expect(Net::HTTP::Post).not_to receive(:new)
subject
end
- it 'returns nil' do
- expect(subject).to eq(nil)
- end
+ it { expect(subject).to eq(nil) }
end
context 'with an unexpected body' do
let(:body) { '' }
- include_examples 'returns a failure without making a request'
+ include_examples 'returns nil without making a request'
end
context 'with an expected body' do
context 'without a valid URL' do
let(:url) { '' }
- include_examples 'returns a failure without making a request'
-
it 'debug logs before the request' do
allow(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'
end
context 'with a valid URL' do
@@ -42,15 +42,63 @@ describe Spree::Webhooks::Endpoints::MakeRequest do
before { 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' do
allow(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
+ end
end
context 'when request code_type is not Net::HTTPOK' do
@@ -69,17 +117,17 @@ describe Spree::Webhooks::Endpoints::MakeRequest do
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')
+ 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) }
end
context 'when request code_type is Net::HTTPOK' do
it '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))
end
end
end
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 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 do
it 'queues a job to make a request' do
expect { 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)
)
end
end
@@ -47,7 +47,7 @@ describe Spree::Webhooks::Endpoints::QueueRequests, :job, :spree_webhooks do
it 'queues a job to make a request' do
expect { 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)
)
end
end
@@ -62,9 +62,7 @@ describe Spree::Webhooks::Endpoints::QueueRequests, :job, :spree_webhooks do
end
it '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)
end
end
@@ -78,9 +76,7 @@ describe Spree::Webhooks::Endpoints::QueueRequests, :job, :spree_webhooks do
end
it '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)
end
end
end