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 endpoint url format validator.
Sebastian Palma3d6b48d5b956
diff --git a/api/app/models/spree/webhooks/endpoint.rb b/api/app/models/spree/webhooks/endpoint.rbindex b047db4714..88f3801002 100644--- a/api/app/models/spree/webhooks/endpoint.rb+++ b/api/app/models/spree/webhooks/endpoint.rb@@ -1,7 +1,7 @@module Spreemodule Webhooksclass Endpoint < Spree::Webhooks::Base- validates :url, presence: true+ validates :url, :'spree/webhooks/validators/url' => trueendendenddiff --git a/api/app/models/spree/webhooks/validators/url_validator.rb b/api/app/models/spree/webhooks/validators/url_validator.rbnew file mode 100644index 0000000000..61ccc2a694--- /dev/null+++ b/api/app/models/spree/webhooks/validators/url_validator.rb@@ -0,0 +1,27 @@+module Spree+ module Webhooks+ module Validators+ class UrlValidator < ActiveModel::EachValidator+ def validate_each(record, attribute, value)+ unless url_valid?(value)+ record.errors.add(attribute, (options[:message] || ERROR_MESSAGE))+ end+ end++ private++ ERROR_MESSAGE = 'must be a valid URL'+ private_constant :ERROR_MESSAGE++ def url_valid?(url)+ uri = begin+ URI.parse(url)+ rescue URI::InvalidURIError+ return false+ end+ uri.host.present? && uri.kind_of?(URI::HTTP)+ end+ end+ end+ end+enddiff --git a/api/spec/models/spree/webhooks/endpoint_spec.rb b/api/spec/models/spree/webhooks/endpoint_spec.rbindex da2b045ac4..a5254ddca6 100644--- a/api/spec/models/spree/webhooks/endpoint_spec.rb+++ b/api/spec/models/spree/webhooks/endpoint_spec.rb@@ -13,5 +13,17 @@ describe Spree::Store doexpect(endpoint.valid?).to be(false)endend++ context 'url format' do+ it 'is invalid with an invalid url' do+ endpoint = Spree::Webhooks::Endpoint.new(url: 'google.com')+ expect(endpoint.valid?).to be(false)+ end++ it 'is valid with an valid url' do+ endpoint = Spree::Webhooks::Endpoint.new(url: 'http://google.com')+ expect(endpoint.valid?).to be(true)+ end+ endendenddiff --git a/api/spec/models/spree/webhooks/validators/url_validator_spec.rb b/api/spec/models/spree/webhooks/validators/url_validator_spec.rbnew file mode 100644index 0000000000..606b794ba5--- /dev/null+++ b/api/spec/models/spree/webhooks/validators/url_validator_spec.rb@@ -0,0 +1,67 @@+require 'spec_helper'++module Test+ class Product < ActiveRecord::Base+ self.table_name = 'test_products'++ validates :url, :'spree/webhooks/validators/url' => true+ end+end++describe Spree::Webhooks::Validators::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(Test::Product.new(url: nil).valid?).to eq(false) }+ it { expect(Test::Product.new(url: 'google.com').valid?).to eq(false) }+ it { expect(Test::Product.new(url: 'http:/google').valid?).to eq(false) }+ it { expect(Test::Product.new(url: 'www.google.com').valid?).to eq(false) }+ end++ context 'is valid' do+ it { expect(Test::Product.new(url: 'http://google.com').valid?).to eq(true) }+ it { expect(Test::Product.new(url: 'https://google.com').valid?).to eq(true) }+ it { expect(Test::Product.new(url: 'http://www.google.com').valid?).to eq(true) }+ it { expect(Test::Product.new(url: 'https://www.google.com').valid?).to eq(true) }+ # Due to simplicity, this is valid even without tld+ it { expect(Test::Product.new(url: 'http://google').valid?).to eq(true) }+ end+ end++ describe 'using a given message or a defined one' do+ let(:test_product) { Test::Product.new(url: nil) }++ context 'using the message option' do+ let(:message) { 'is needed' }++ it 'adds the given message to the record url errors array' do+ # Remove the validation set at the beginning+ Test::Product.clear_validators!+ Test::Product.validates(:url, :'spree/webhooks/validators/url' => { message: message })++ test_product.valid?+ expect(test_product.errors.messages).to eq(url: [message])++ # Add back the validation removed previously+ Test::Product.clear_validators!+ Test::Product.validates(:url, :'spree/webhooks/validators/url' => true)+ end+ end++ context 'without using the message option' do+ it 'adds a pre-defined message to the record url error array' do+ test_product.valid?+ expect(test_product.errors.messages).to eq(url: ['must be a valid URL'])+ end+ end+ end+end