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
fixed custom domain URL validation, added controller specs, use permitted attributes
Damian Legawiece58e352ee2cf
diff --git a/admin/app/controllers/spree/admin/custom_domains_controller.rb b/admin/app/controllers/spree/admin/custom_domains_controller.rbindex f71fddd41b..2edc04175b 100644--- a/admin/app/controllers/spree/admin/custom_domains_controller.rb+++ b/admin/app/controllers/spree/admin/custom_domains_controller.rb@@ -16,6 +16,10 @@ module Spreedef location_after_savespree.admin_custom_domains_pathend++ def permitted_resource_params+ params.require(:custom_domain).permit(Spree::PermittedAttributes.custom_domain_attributes)+ endendendenddiff --git a/admin/spec/controllers/spree/admin/custom_domains_controller_spec.rb b/admin/spec/controllers/spree/admin/custom_domains_controller_spec.rbnew file mode 100644index 0000000000..ad73c356a4--- /dev/null+++ b/admin/spec/controllers/spree/admin/custom_domains_controller_spec.rb@@ -0,0 +1,114 @@+require 'spec_helper'++describe Spree::Admin::CustomDomainsController, type: :controller do+ stub_authorization!++ render_views++ let(:store) { Spree::Store.default }++ describe '#new' do+ subject { get :new, format: :html }++ it 'renders new template' do+ subject+ expect(response).to have_http_status(:ok)+ expect(response).to render_template(:new)+ end+ end++ describe '#create' do+ subject { post :create, params: params, format: :html }++ let(:params) do+ {+ custom_domain: {+ url: 'test-domain.com',+ }+ }+ end++ context 'with valid params' do+ it 'creates a new custom domain' do+ expect { subject }.to change(Spree::CustomDomain, :count).by(1)++ expect(response).to redirect_to(spree.admin_custom_domains_path)++ custom_domain = Spree::CustomDomain.last+ expect(custom_domain.url).to eq('test-domain.com')+ expect(custom_domain.store).to eq(store)+ expect(custom_domain.default).to be_truthy+ end+ end++ context 'with invalid params' do+ let(:params) do+ {+ custom_domain: {+ url: ''+ }+ }+ end++ it 'does not create a custom domain' do+ expect { subject }.not_to change(Spree::CustomDomain, :count)++ expect(response).to have_http_status(:unprocessable_entity)+ expect(response).to render_template(:new)+ end+ end+ end++ describe '#edit' do+ subject { get :edit, params: { id: custom_domain.id } }++ let!(:custom_domain) { create(:custom_domain, store: store) }++ it 'renders edit template' do+ subject+ expect(response).to have_http_status(:ok)+ expect(response).to render_template(:edit)+ end+ end++ describe '#update' do+ subject { put :update, params: params }++ let!(:custom_domain) { create(:custom_domain, store: store) }++ let(:params) do+ {+ id: custom_domain.id,+ custom_domain: {+ url: 'updated-domain.com'+ }+ }+ end++ context 'with valid params' do+ it 'updates the custom domain' do+ expect { subject }.to change { custom_domain.reload.url }.to('updated-domain.com')++ expect(response).to redirect_to(spree.admin_custom_domains_path)+ end+ end++ context 'with invalid params' do+ let(:params) do+ {+ id: custom_domain.id,+ custom_domain: {+ url: ''+ }+ }+ end++ it 'does not update the custom domain' do+ expect { subject }.not_to change { custom_domain.reload.url }++ expect(response).to have_http_status(:unprocessable_entity)+ expect(response).to render_template(:edit)+ end+ end+ end+enddiff --git a/core/app/models/spree/custom_domain.rb b/core/app/models/spree/custom_domain.rbindex 427aa31972..3af4720da2 100644--- a/core/app/models/spree/custom_domain.rb+++ b/core/app/models/spree/custom_domain.rb@@ -14,7 +14,7 @@ module Spree# Validations#validates :url, presence: true, uniqueness: true, format: {- with: %r{\A[a-z][a-z0-9-]*[a-z0-9]\z}i+ with: %r{\A(?:[a-z0-9](?:[a-z0-9-]*[a-z0-9])?\.)+[a-z0-9](?:[a-z0-9-]*[a-z0-9])?\z}i}, length: { in: 1..63 }validate :url_is_valid@@ -26,6 +26,7 @@ module Spreeafter_validation :ensure_default, on: :createdef url_is_valid+ return if url.blank?parts = url.split('.')errors.add(:url, 'use domain or subdomain') if (parts[0] != 'www' && parts.size > 3) || (parts[0] == 'www' && parts.size > 4) || parts.size < 2diff --git a/core/lib/spree/permitted_attributes.rb b/core/lib/spree/permitted_attributes.rbindex ead3298de2..3374b350a4 100644--- a/core/lib/spree/permitted_attributes.rb+++ b/core/lib/spree/permitted_attributes.rb@@ -7,6 +7,7 @@ module Spree:classification_attributes,:cms_page_attributes,:cms_section_attributes,+ :custom_domain_attributes,:customer_return_attributes,:digital_attributes,:digital_link_attributes,@@ -63,6 +64,8 @@ module Spree@@cms_section_attributes = [:name, :cms_page_id, :fit, :destination, { content: {}, settings: {} }]+ @@custom_domain_attributes = [:url, :default]+@@customer_return_attributes = [:stock_location_id, {return_items_attributes: [:id, :inventory_unit_id, :return_authorization_id, :returned, :pre_tax_amount,:acceptance_status, :exchange_variant_id, :resellable]