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 Webhooks::Subscriber check_uri_path validation.

Sebastian Palma746feb2395bd
diff --git a/api/app/models/spree/webhooks/subscriber.rb b/api/app/models/spree/webhooks/subscriber.rb
index 2f5f728f23..9c004a2ec2 100644
--- a/api/app/models/spree/webhooks/subscriber.rb
+++ b/api/app/models/spree/webhooks/subscriber.rb
@@ -3,7 +3,21 @@ module Spree
class Subscriber < Spree::Webhooks::Base
validates :url, 'spree/url': true, presence: true
+ validate :check_uri_path
+
scope :active, -> { where(active: true) }
+
+ private
+
+ def check_uri_path
+ uri = begin
+ URI.parse(url)
+ rescue URI::InvalidURIError
+ return false
+ end
+
+ errors.add(:url, 'the URL must have a path') if uri.blank? || uri.path.blank?
+ end
end
end
end
diff --git a/api/spec/models/spree/webhooks/subscriber_spec.rb b/api/spec/models/spree/webhooks/subscriber_spec.rb
index 1bf2dfed77..4ae897297c 100644
--- a/api/spec/models/spree/webhooks/subscriber_spec.rb
+++ b/api/spec/models/spree/webhooks/subscriber_spec.rb
@@ -2,16 +2,24 @@ require 'spec_helper'
describe Spree::Webhooks::Subscriber do
describe 'validations' do
- context 'url format' do
+ context 'url format (UrlValidator)' do
it 'is invalid with an invalid url' do
endpoint = described_class.new(url: 'google.com')
expect(endpoint.valid?).to be(false)
end
it 'is valid with a valid url' do
- endpoint = described_class.new(url: 'http://google.com')
+ endpoint = described_class.new(url: 'http://google.com/')
expect(endpoint.valid?).to be(true)
end
end
+
+ context 'url path' do
+ it 'is invalid a url without path' do
+ endpoint = described_class.new(url: 'http://google.com')
+ expect(endpoint.valid?).to be(false)
+ expect(endpoint.errors.messages).to eq(url: ['the URL must have a path'])
+ end
+ end
end
end
diff --git a/core/app/validators/spree/url_validator.rb b/core/app/validators/spree/url_validator.rb
index 63bc79bc28..559167973c 100644
--- a/core/app/validators/spree/url_validator.rb
+++ b/core/app/validators/spree/url_validator.rb
@@ -1,6 +1,7 @@
module Spree
class UrlValidator < ActiveModel::EachValidator
def validate_each(record, attribute, value)
+ value = value[:url] if value.is_a?(Hash)
unless url_valid?(value)
record.errors.add(attribute, (options[:message] || ERROR_MESSAGE))
end
diff --git a/core/spec/validators/spree/url_validator_spec.rb b/core/spec/validators/spree/url_validator_spec.rb
index 97789da492..14f298323f 100644
--- a/core/spec/validators/spree/url_validator_spec.rb
+++ b/core/spec/validators/spree/url_validator_spec.rb
@@ -2,8 +2,8 @@ require 'spec_helper'
module Spree
module Test
- class Product < ActiveRecord::Base
- self.table_name = 'test_products'
+ Product = Struct.new(:url) do
+ include ActiveModel::Validations
validates :url, 'spree/url': true
end
@@ -11,16 +11,6 @@ module Spree
end
describe Spree::UrlValidator do
- before(:all) do
- ActiveRecord::Base.connection.create_table :test_products, force: true do |t|
- t.string :url
- end
- end
-
- after(:all) do
- ActiveRecord::Base.connection.drop_table :test_products, if_exists: true
- end
-
describe 'validating the given URL' do
context 'is invalid' do
it { expect(Spree::Test::Product.new(url: nil).valid?).to eq(false) }