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
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.rbindex 2f5f728f23..9c004a2ec2 100644--- a/api/app/models/spree/webhooks/subscriber.rb+++ b/api/app/models/spree/webhooks/subscriber.rb@@ -3,7 +3,21 @@ module Spreeclass Subscriber < Spree::Webhooks::Basevalidates :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?+ endendendenddiff --git a/api/spec/models/spree/webhooks/subscriber_spec.rb b/api/spec/models/spree/webhooks/subscriber_spec.rbindex 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 dodescribe 'validations' do- context 'url format' do+ context 'url format (UrlValidator)' doit 'is invalid with an invalid url' doendpoint = described_class.new(url: 'google.com')expect(endpoint.valid?).to be(false)endit '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)endend++ 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+ endendenddiff --git a/core/app/validators/spree/url_validator.rb b/core/app/validators/spree/url_validator.rbindex 63bc79bc28..559167973c 100644--- a/core/app/validators/spree/url_validator.rb+++ b/core/app/validators/spree/url_validator.rb@@ -1,6 +1,7 @@module Spreeclass UrlValidator < ActiveModel::EachValidatordef 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))enddiff --git a/core/spec/validators/spree/url_validator_spec.rb b/core/spec/validators/spree/url_validator_spec.rbindex 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 Spreemodule Test- class Product < ActiveRecord::Base- self.table_name = 'test_products'+ Product = Struct.new(:url) do+ include ActiveModel::Validationsvalidates :url, 'spree/url': trueend@@ -11,16 +11,6 @@ module Spreeenddescribe 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' docontext 'is invalid' doit { expect(Spree::Test::Product.new(url: nil).valid?).to eq(false) }