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.

Fixed permitted attributes for Posts, Post Categories and Page Sectio… (#12774)

Damian Legawiec79ba125f4718
diff --git a/admin/app/controllers/spree/admin/orders_controller.rb b/admin/app/controllers/spree/admin/orders_controller.rb
index 5151be5679..b35d2b0a02 100644
--- a/admin/app/controllers/spree/admin/orders_controller.rb
+++ b/admin/app/controllers/spree/admin/orders_controller.rb
@@ -66,7 +66,7 @@ module Spree
def order_params
params[:created_by_id] = try_spree_current_user.try(:id)
- params.permit(:created_by_id, :user_id, :store_id, :channel)
+ params.permit(:created_by_id, :user_id, :store_id, :channel, tag_list: [])
end
def load_order
diff --git a/admin/app/controllers/spree/admin/post_categories_controller.rb b/admin/app/controllers/spree/admin/post_categories_controller.rb
index 2a1c750c28..a66765f7af 100644
--- a/admin/app/controllers/spree/admin/post_categories_controller.rb
+++ b/admin/app/controllers/spree/admin/post_categories_controller.rb
@@ -4,6 +4,12 @@ module Spree
include StorefrontBreadcrumbConcern
add_breadcrumb Spree.t(:posts), :admin_posts_path
add_breadcrumb Spree.t(:categories), :admin_post_categories_path
+
+ private
+
+ def permitted_resource_params
+ params.require(:post_category).permit(permitted_post_category_attributes)
+ end
end
end
end
diff --git a/admin/app/controllers/spree/admin/posts_controller.rb b/admin/app/controllers/spree/admin/posts_controller.rb
index a98c844028..7764ee7e79 100644
--- a/admin/app/controllers/spree/admin/posts_controller.rb
+++ b/admin/app/controllers/spree/admin/posts_controller.rb
@@ -35,6 +35,10 @@ module Spree
add_breadcrumb @post.title, spree.edit_admin_post_path(@post)
end
+
+ def permitted_resource_params
+ params.require(:post).permit(permitted_post_attributes)
+ end
end
end
end
diff --git a/admin/app/controllers/spree/admin/shipments_controller.rb b/admin/app/controllers/spree/admin/shipments_controller.rb
index 11690b716f..a78c27edef 100644
--- a/admin/app/controllers/spree/admin/shipments_controller.rb
+++ b/admin/app/controllers/spree/admin/shipments_controller.rb
@@ -11,7 +11,7 @@ module Spree
before_action :refresh_shipping_rates, only: :edit
def update
- @result = Spree::Dependencies.shipment_update_service.constantize.call(shipment: @shipment, shipment_attributes: shipment_params)
+ @result = Spree::Dependencies.shipment_update_service.constantize.call(shipment: @shipment, shipment_attributes: permitted_resource_params)
flash[:success] = Spree.t(:successfully_updated, resource: Spree.t(:shipment)) if @result.success?
redirect_back fallback_location: spree.edit_admin_order_path(@order)
@@ -94,7 +94,7 @@ module Spree
@variant = current_store.variants.accessible_by(current_ability, :manage).find(params[:variant_id])
end
- def shipment_params
+ def permitted_resource_params
params.require(:shipment).permit(permitted_shipment_attributes)
end
end
diff --git a/admin/spec/controllers/spree/admin/page_sections_controller_spec.rb b/admin/spec/controllers/spree/admin/page_sections_controller_spec.rb
index 576e6eb27c..bc1dbddf91 100644
--- a/admin/spec/controllers/spree/admin/page_sections_controller_spec.rb
+++ b/admin/spec/controllers/spree/admin/page_sections_controller_spec.rb
@@ -4,7 +4,8 @@ RSpec.describe Spree::Admin::PageSectionsController, type: :controller do
stub_authorization!
render_views
- let(:parent_theme) { @default_store.default_theme }
+ let(:store) { @default_store }
+ let(:parent_theme) { create(:theme, store: store) }
let(:theme) { create(:theme, :preview, parent: parent_theme) }
let(:page) { create(:page, :preview, pageable: parent_theme) }
@@ -74,6 +75,26 @@ RSpec.describe Spree::Admin::PageSectionsController, type: :controller do
expect(page_section.reload.asset).to be_attached
end
+
+ context "with action text field" do
+ let(:page_section) { create(:announcement_bar_page_section, pageable: theme) }
+
+ it "persists page section action text" do
+ put :update, params: { id: page_section.id, page_section: { text: "Welcome to our amazing store" } }, format: :turbo_stream
+
+ expect(page_section.reload.text.to_plain_text).to eq("Welcome to our amazing store")
+ end
+ end
+
+ context "changing position" do
+ let(:page_section) { create(:featured_taxon_page_section, pageable: page, position: 1) }
+
+ it "updates the position" do
+ put :update, params: { id: page_section.id, page_section: { position: 2 } }, format: :turbo_stream
+
+ expect(page_section.reload.position).to eq(2)
+ end
+ end
end
describe '#destroy' do
@@ -89,7 +110,7 @@ RSpec.describe Spree::Admin::PageSectionsController, type: :controller do
let!(:page_section) { create(:header_page_section) }
it 'does not delete the page section' do
- expect { delete :destroy, params: { id: page_section.id }, format: :turbo_stream }.to change(Spree::PageSection, :count).by(0)
+ expect { delete :destroy, params: { id: page_section.id }, format: :turbo_stream }.not_to change(Spree::PageSection, :count)
end
end
end
diff --git a/admin/spec/controllers/spree/admin/post_categories_controller_spec.rb b/admin/spec/controllers/spree/admin/post_categories_controller_spec.rb
index 371ec34c6c..59cac00d47 100644
--- a/admin/spec/controllers/spree/admin/post_categories_controller_spec.rb
+++ b/admin/spec/controllers/spree/admin/post_categories_controller_spec.rb
@@ -17,6 +17,37 @@ RSpec.describe Spree::Admin::PostCategoriesController, type: :controller do
end
end
+ describe 'GET #new' do
+ subject(:new) { get :new }
+
+ it 'renders the new post category page' do
+ new
+ expect(response).to render_template(:new)
+ end
+ end
+
+ describe 'POST #create' do
+ subject(:create_category) { post :create, params: { post_category: post_category_params } }
+
+ let(:post_category_params) do
+ {
+ title: 'New Category',
+ description: 'This is a new category',
+ slug: 'new-category'
+ }
+ end
+
+ it 'creates a new post category' do
+ expect { create_category }.to change(Spree::PostCategory, :count).by(1)
+
+ category = Spree::PostCategory.last
+ expect(category.title).to eq('New Category')
+ expect(category.description.to_plain_text).to eq('This is a new category')
+ expect(category.slug).to eq('new-category')
+ expect(category.store).to eq(store)
+ end
+ end
+
describe 'GET #edit' do
subject(:edit) { get :edit, params: { id: post_category.id } }
@@ -27,4 +58,37 @@ RSpec.describe Spree::Admin::PostCategoriesController, type: :controller do
expect(response).to render_template(:edit)
end
end
+
+ describe 'PUT #update' do
+ subject(:update_category) { put :update, params: { id: post_category.id, post_category: post_category_params } }
+
+ let!(:post_category) { create(:post_category, store: store) }
+ let(:post_category_params) do
+ {
+ title: 'Updated Category',
+ description: 'This is an updated category',
+ slug: 'updated-category'
+ }
+ end
+
+ it 'updates the post category' do
+ update_category
+ post_category.reload
+
+ expect(post_category.title).to eq('Updated Category')
+ expect(post_category.description.to_plain_text).to eq('This is an updated category')
+ expect(post_category.slug).to eq('updated-category')
+ end
+ end
+
+ describe 'DELETE #destroy' do
+ subject(:destroy_category) { delete :destroy, params: { id: post_category.id } }
+
+ let!(:post_category) { create(:post_category, store: store) }
+
+ it 'destroys the post category' do
+ expect { destroy_category }.to change(Spree::PostCategory, :count).by(-1)
+ expect(response).to redirect_to(admin_post_categories_path)
+ end
+ end
end
diff --git a/admin/spec/controllers/spree/admin/posts_controller_spec.rb b/admin/spec/controllers/spree/admin/posts_controller_spec.rb
index 5f69ac872a..23bdf89be5 100644
--- a/admin/spec/controllers/spree/admin/posts_controller_spec.rb
+++ b/admin/spec/controllers/spree/admin/posts_controller_spec.rb
@@ -4,6 +4,10 @@ RSpec.describe Spree::Admin::PostsController, type: :controller do
stub_authorization!
render_views
+ let(:author_admin_user) { create(:admin_user) }
+ let(:post_category) { create(:post_category, store: store) }
+ let(:published_at) { '2025-05-24 00:00:00' }
+
let(:store) { @default_store }
describe 'GET #index' do
@@ -45,6 +49,44 @@ RSpec.describe Spree::Admin::PostsController, type: :controller do
end
end
+ describe 'POST #create' do
+ subject(:create_post) { post :create, params: { post: post_params } }
+
+ let(:post_params) do
+ {
+ title: 'New Post',
+ content: 'This is a new post',
+ excerpt: "Excerpt for SEO",
+ published_at: published_at,
+ author_id: author_admin_user.id,
+ image: fixture_file_upload('logo.png', 'image/png'),
+ meta_title: 'New Post for SEO',
+ meta_description: 'This is a new post for SEO',
+ tag_list: ['SEO', 'VIP'],
+ slug: "my-new-post",
+ post_category_id: post_category.id
+ }
+ end
+
+ it 'creates a new post' do
+ expect { create_post }.to change(Spree::Post, :count).by(1)
+
+ post = Spree::Post.last
+ expect(post.title).to eq('New Post')
+ expect(post.content.to_plain_text).to eq('This is a new post')
+ expect(post.excerpt.to_plain_text).to eq("Excerpt for SEO")
+ expect(post.published_at).to eq(published_at)
+ expect(post.author).to eq(author_admin_user)
+ expect(post.post_category).to eq(post_category)
+ expect(post.image).to be_attached
+ expect(post.meta_title).to eq('New Post for SEO')
+ expect(post.meta_description).to eq('This is a new post for SEO')
+ expect(post.tag_list).to eq(['SEO', 'VIP'])
+ expect(post.slug).to eq("my-new-post")
+ expect(post.post_category).to eq(post_category)
+ end
+ end
+
describe 'GET #edit' do
subject(:edit) { get :edit, params: { id: post.id } }
@@ -55,4 +97,55 @@ RSpec.describe Spree::Admin::PostsController, type: :controller do
expect(response).to render_template(:edit)
end
end
+
+ describe 'PUT #update' do
+ subject(:update_post) { put :update, params: { id: post.id, post: post_params } }
+
+ let!(:post) { create(:post, store: store) }
+ let(:post_params) do
+ {
+ title: 'Updated Post',
+ content: 'This is an updated post',
+ excerpt: "Updated excerpt for SEO",
+ published_at: published_at,
+ author_id: author_admin_user.id,
+ image: fixture_file_upload('logo.png', 'image/png'),
+ meta_title: 'Updated Post for SEO',
+ meta_description: 'This is an updated post for SEO',
+ tag_list: ['Updated', 'VIP'],
+ post_category_id: post_category.id
+ }
+ end
+
+ it 'updates the post' do
+ update_post
+ post.reload
+
+ expect(post.title).to eq('Updated Post')
+ expect(post.content.to_plain_text).to eq('This is an updated post')
+ expect(post.excerpt.to_plain_text).to eq("Updated excerpt for SEO")
+ expect(post.published_at).to eq(published_at)
+ expect(post.author).to eq(author_admin_user)
+ expect(post.post_category).to eq(post_category)
+ expect(post.image).to be_attached
+ expect(post.meta_title).to eq('Updated Post for SEO')
+ expect(post.meta_description).to eq('This is an updated post for SEO')
+ expect(post.tag_list).to eq(['Updated', 'VIP'])
+ expect(post.post_category).to eq(post_category)
+ end
+ end
+
+ describe 'DELETE #destroy' do
+ subject(:destroy_post) { delete :destroy, params: { id: post.id } }
+
+ let!(:post) { create(:post, store: store) }
+
+ it 'destroys the post' do
+ destroy_post
+
+ expect(post.reload.deleted_at).not_to be_nil
+
+ expect(response).to redirect_to(admin_posts_path)
+ end
+ end
end
diff --git a/admin/spec/controllers/spree/admin/shipments_controller_spec.rb b/admin/spec/controllers/spree/admin/shipments_controller_spec.rb
index b4f933f9e6..0a1b435216 100644
--- a/admin/spec/controllers/spree/admin/shipments_controller_spec.rb
+++ b/admin/spec/controllers/spree/admin/shipments_controller_spec.rb
@@ -29,7 +29,7 @@ RSpec.describe Spree::Admin::ShipmentsController, type: :controller do
end
end
- describe '#ships' do
+ describe '#ship' do
context 'when shipment can be shipped' do
it 'ships the shipment' do
post :ship, params: { order_id: order.number, id: shipment.number }
diff --git a/core/lib/spree/permitted_attributes.rb b/core/lib/spree/permitted_attributes.rb
index f972f32f31..9af2760bbc 100644
--- a/core/lib/spree/permitted_attributes.rb
+++ b/core/lib/spree/permitted_attributes.rb
@@ -27,6 +27,8 @@ module Spree
:page_section_attributes,
:payment_attributes,
:payment_method_attributes,
+ :post_attributes,
+ :post_category_attributes,
:product_attributes,
:promotion_attributes,
:promotion_rule_attributes,
@@ -125,16 +127,20 @@ module Spree
@@page_attributes = [:name, :slug, :meta_title, :meta_description, :meta_keywords]
- @@page_block_attributes = [:type, :name, :position]
+ @@page_block_attributes = [:type, :name, :text, :position]
@@page_link_attributes = [:linkable_id, :linkable_type, :position, :label, :url]
- @@page_section_attributes = [:type, :name, :position, :asset]
+ @@page_section_attributes = [:type, :name, :position, :asset, :text, :description]
@@payment_attributes = [:amount, :payment_method_id, :payment_method]
@@payment_method_attributes = [:name, :type, :description, :active, :display_on, :auto_capture, :position]
+ @@post_attributes = [:title, :meta_title, :meta_description, :slug, :author_id, :post_category_id, :published_at, :content, :excerpt, :image, tag_list: []]
+
+ @@post_category_attributes = [:title, :slug, :description]
+
@@product_properties_attributes = [:property_name, :property_id, :value, :position, :_destroy]
@@product_attributes = [
diff --git a/core/lib/spree/testing_support/factories/post_factory.rb b/core/lib/spree/testing_support/factories/post_factory.rb
index 6840c5ff5d..a1a3ec7952 100644
--- a/core/lib/spree/testing_support/factories/post_factory.rb
+++ b/core/lib/spree/testing_support/factories/post_factory.rb
@@ -1,6 +1,6 @@
FactoryBot.define do
factory :post, class: Spree::Post do
- post_category { create(:post_category) }
+ post_category { create(:post_category, store: Spree::Store.default || create(:store)) }
title { FFaker::Lorem.sentence }
content { FFaker::Lorem.paragraph }
published_at { Time.current }