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.

Ensure that we only fetch current Product resources

Damian Legawiec430f7f32d613
diff --git a/backend/app/controllers/concerns/spree/admin/product_concern.rb b/backend/app/controllers/concerns/spree/admin/product_concern.rb
new file mode 100644
index 0000000000..b47938dba2
--- /dev/null
+++ b/backend/app/controllers/concerns/spree/admin/product_concern.rb
@@ -0,0 +1,11 @@
+module Spree
+ module Admin
+ module ProductConcern
+ extend ActiveSupport::Concern
+
+ def product_scope
+ current_store.products.accessible_by(current_ability, :index)
+ end
+ end
+ end
+end
diff --git a/backend/app/controllers/spree/admin/base_controller.rb b/backend/app/controllers/spree/admin/base_controller.rb
index 05346d506c..af693f692b 100644
--- a/backend/app/controllers/spree/admin/base_controller.rb
+++ b/backend/app/controllers/spree/admin/base_controller.rb
@@ -38,7 +38,7 @@ module Spree
def flash_message_for(object, event_sym)
resource_desc = object.class.model_name.human
- resource_desc += " \"#{object.name}\"" if object.respond_to?(:name) && object.name.present? && !object.is_a?(Spree::Order)
+ resource_desc += " \"#{object.name}\"" if object.persisted? && object.respond_to?(:name) && object.name.present? && !object.is_a?(Spree::Order)
Spree.t(event_sym, resource: resource_desc)
end
diff --git a/backend/app/controllers/spree/admin/images_controller.rb b/backend/app/controllers/spree/admin/images_controller.rb
index 7d8ffb2c6b..cc43b7d382 100644
--- a/backend/app/controllers/spree/admin/images_controller.rb
+++ b/backend/app/controllers/spree/admin/images_controller.rb
@@ -1,8 +1,11 @@
module Spree
module Admin
class ImagesController < ResourceController
+ include Spree::Admin::ProductConcern
+
+ belongs_to 'spree/product', find_by: :slug
+
before_action :load_edit_data, except: :index
- before_action :load_index_data, only: :index
create.before :set_viewable
update.before :set_viewable
@@ -10,41 +13,24 @@ module Spree
private
def location_after_destroy
- admin_product_images_url(@product)
+ spree.admin_product_images_url(@arent)
end
def location_after_save
- admin_product_images_url(@product)
- end
-
- def load_index_data
- @product = Product.friendly.preload(*variant_index_includes).find(params[:product_id])
+ spree.admin_product_images_url(parent)
end
def load_edit_data
- @product = Product.friendly.preload(*variant_edit_includes).find(params[:product_id])
- @variants = @product.variants.map do |variant|
+ @variants = parent.variants.map do |variant|
[variant.sku_and_options_text, variant.id]
end
- @variants.insert(0, [Spree.t(:all), @product.master.id])
+ @variants.insert(0, [Spree.t(:all), parent.master.id])
end
def set_viewable
@image.viewable_type = 'Spree::Variant'
@image.viewable_id = params[:image][:viewable_id]
end
-
- def variant_index_includes
- [
- variant_images: [viewable: { option_values: :option_type }]
- ]
- end
-
- def variant_edit_includes
- [
- variants_including_master: { option_values: :option_type, images: :viewable }
- ]
- end
end
end
end
diff --git a/backend/app/controllers/spree/admin/prices_controller.rb b/backend/app/controllers/spree/admin/prices_controller.rb
index 3b9584e2e1..944df5e4d8 100644
--- a/backend/app/controllers/spree/admin/prices_controller.rb
+++ b/backend/app/controllers/spree/admin/prices_controller.rb
@@ -8,7 +8,7 @@ module Spree
params[:vp].each do |variant_id, prices|
next unless variant_id
- variant = Spree::Variant.find(variant_id)
+ variant = parent.variants_including_master.find(variant_id)
next unless variant
supported_currencies_for_all_stores.each do |currency|
diff --git a/backend/app/controllers/spree/admin/products_controller.rb b/backend/app/controllers/spree/admin/products_controller.rb
index 1e92f5bd1d..3449493229 100644
--- a/backend/app/controllers/spree/admin/products_controller.rb
+++ b/backend/app/controllers/spree/admin/products_controller.rb
@@ -1,6 +1,8 @@
module Spree
module Admin
class ProductsController < ResourceController
+ include Spree::Admin::ProductConcern
+
helper 'spree/products'
before_action :load_data, except: :index
@@ -45,7 +47,7 @@ module Spree
end
def destroy
- @product = Product.friendly.find(params[:id])
+ @product = product_scope.friendly.find(params[:id])
begin
# TODO: why is @product.destroy raising ActiveRecord::RecordNotDestroyed instead of failing with false result
@@ -92,12 +94,8 @@ module Spree
protected
- def scope
- current_store.products.accessible_by(current_ability, :index)
- end
-
def find_resource
- scope.with_deleted.friendly.find(params[:id])
+ product_scope.with_deleted.friendly.find(params[:id])
end
def location_after_save
@@ -120,7 +118,7 @@ module Spree
params[:q][:s] ||= 'name asc'
- @collection = scope
+ @collection = product_scope
# Don't delete params[:q][:deleted_at_null] here because it is used in view to check the
# checkbox for 'q[deleted_at_null]'. This also messed with pagination when deleted_at_null is checked.
diff --git a/backend/app/controllers/spree/admin/resource_controller.rb b/backend/app/controllers/spree/admin/resource_controller.rb
index fed8a8d942..8c7d5b1c5d 100644
--- a/backend/app/controllers/spree/admin/resource_controller.rb
+++ b/backend/app/controllers/spree/admin/resource_controller.rb
@@ -162,6 +162,10 @@ class Spree::Admin::ResourceController < Spree::Admin::BaseController
# Don't use `find_by_attribute_name` to workaround globalize/globalize#423 bug
send(:find_by, parent_data[:find_by].to_s => params["#{resource.model_name}_id"])
instance_variable_set("@#{resource.model_name}", @parent)
+
+ raise ActiveRecord::RecordNotFound if @parent.nil?
+
+ @parent
end
end
diff --git a/backend/app/controllers/spree/admin/variants_controller.rb b/backend/app/controllers/spree/admin/variants_controller.rb
index cb843fd144..aa76413a3f 100644
--- a/backend/app/controllers/spree/admin/variants_controller.rb
+++ b/backend/app/controllers/spree/admin/variants_controller.rb
@@ -1,6 +1,8 @@
module Spree
module Admin
class VariantsController < ResourceController
+ include Spree::Admin::ProductConcern
+
belongs_to 'spree/product', find_by: :slug
new_action.before :new_before
before_action :redirect_on_empty_option_values, only: [:new]
@@ -9,7 +11,7 @@ module Spree
# override the destroy method to set deleted_at value
# instead of actually deleting the product.
def destroy
- @variant = Variant.find(params[:id])
+ @variant = parent.variants.find(params[:id])
if @variant.destroy
flash[:success] = Spree.t('notice_messages.variant_deleted')
else
@@ -34,10 +36,6 @@ module Spree
@object.default_price = master.default_price.clone if master.default_price.present?
end
- def parent
- @product = Product.with_deleted.friendly.find(params[:product_id])
- end
-
def collection
return @collection if @collection.present?
diff --git a/backend/spec/controllers/spree/admin/images_controller_spec.rb b/backend/spec/controllers/spree/admin/images_controller_spec.rb
new file mode 100644
index 0000000000..9ac760f3ae
--- /dev/null
+++ b/backend/spec/controllers/spree/admin/images_controller_spec.rb
@@ -0,0 +1,65 @@
+require 'spec_helper'
+
+module Spree
+ module Admin
+ describe ImagesController, type: :controller do
+ stub_authorization!
+
+ let(:store) { Spree::Store.default }
+ let(:product) { create(:product, stores: [store]) }
+
+ describe '#index' do
+ let!(:image_1) { create(:image, viewable: product.master) }
+ let!(:image_2) { create(:image, viewable: product.master) }
+ let!(:image_3) { create(:image, viewable: create(:variant)) }
+
+ it 'assigns the images for a requested product' do
+ get :index, params: { product_id: product.slug }
+ expect(assigns(:collection)).to include image_1
+ expect(assigns(:collection)).to include image_2
+ expect(assigns(:collection)).not_to include image_3
+ end
+ end
+
+ describe '#destroy' do
+ subject(:send_request) do
+ delete :destroy, params: { product_id: product, id: image, format: :js }
+ end
+
+ let(:image) { create(:image, viewable: product.master) }
+
+ shared_examples 'correct response' do
+ it { expect(assigns(:image)).to eq(image) }
+ it { expect(response).to have_http_status(:ok) }
+ end
+
+ context 'will successfully destroy image' do
+ describe 'returns response' do
+ before { send_request }
+
+ it_behaves_like 'correct response'
+ it { expect(flash[:success]).to eq('Image has been successfully removed!') }
+ end
+ end
+
+ context 'cannot destroy image of other product' do
+ let(:other_product) { create(:product, stores: [store]) }
+ let(:image) { create(:image, viewable: other_product) }
+
+ it { expect(send_request).to redirect_to(spree.admin_product_images_path(product)) }
+
+ it do
+ send_request
+ expect(flash[:error]).to eq('Image is not found')
+ end
+ end
+
+ context 'cannot destroy image of product from different store' do
+ let(:product) { create(:product, stores: [create(:store)]) }
+
+ it { expect { send_request }.to raise_error(ActiveRecord::RecordNotFound) }
+ end
+ end
+ end
+ end
+end
diff --git a/backend/spec/controllers/spree/admin/products_controller_spec.rb b/backend/spec/controllers/spree/admin/products_controller_spec.rb
index 38833d2b57..e4cfb08c36 100644
--- a/backend/spec/controllers/spree/admin/products_controller_spec.rb
+++ b/backend/spec/controllers/spree/admin/products_controller_spec.rb
@@ -3,21 +3,32 @@ require 'spec_helper'
describe Spree::Admin::ProductsController, type: :controller do
stub_authorization!
+ let(:store) { Spree::Store.default }
+
context '#index' do
let(:ability_user) { stub_model(Spree::LegacyUser, has_spree_role?: true) }
# Regression test for #1259
it 'can find a product by SKU' do
- product = create(:product, sku: 'ABC123', stores: Spree::Store.all)
+ product = create(:product, sku: 'ABC123', stores: [store])
get :index, params: { q: { sku_start: 'ABC123' } }
expect(assigns[:collection]).not_to be_empty
expect(assigns[:collection]).to include(product)
end
+
+ it 'cannot find products from different stores' do
+ product = create(:product, sku: 'ABC123', stores: [create(:store)])
+ get :index, params: { q: { sku_start: 'ABC123' } }
+ expect(assigns[:collection]).to be_empty
+ expect(assigns[:collection]).not_to include(product)
+ end
end
+
+
# regression test for #1370
context 'adding properties to a product' do
- let!(:product) { create(:product, stores: Spree::Store.all) }
+ let!(:product) { create(:product, stores: [store]) }
specify do
put :update, params: {
@@ -101,6 +112,17 @@ describe Spree::Admin::ProductsController, type: :controller do
end
end
end
+
+ context 'cannot destroy product from different store' do
+ let(:product) { create(:product, stores: [create(:store)]) }
+
+ it { expect(send_request).to redirect_to(spree.admin_products_path) }
+
+ it do
+ send_request
+ expect(flash[:error]).to eq('Product is not found')
+ end
+ end
end
describe '#clone' do
@@ -147,14 +169,57 @@ describe Spree::Admin::ProductsController, type: :controller do
end
end
end
+
+ context 'cannot clone product from different store' do
+ let(:product) { create(:product, stores: [create(:store)]) }
+
+ it { expect(send_request).to redirect_to(spree.admin_products_path) }
+
+ it do
+ send_request
+ expect(flash[:error]).to eq('Product is not found')
+ end
+ end
end
- context 'stock' do
- let(:product) { create(:product, stores: Spree::Store.all) }
+ describe '#show' do
+ let(:product) { create(:product, stores: [store]) }
+ let(:send_request) { get :stock, params: { id: product } }
+
+ it { expect(send_request).to have_http_status(:ok) }
+
+ context 'cannot see product from different store' do
+ let(:product) { create(:product, stores: [create(:store)]) }
+
+ it { expect(send_request).to redirect_to(spree.admin_products_path) }
+
+ it do
+ send_request
+ expect(flash[:error]).to eq('Product is not found')
+ end
+ end
+ end
+
+ describe '#stock' do
+ let(:product) { create(:product, stores: [store]) }
+ let(:send_request) { get :stock, params: { id: product } }
it 'restricts stock location based on accessible attributes' do
expect(Spree::StockLocation).to receive(:accessible_by).and_return([])
- get :stock, params: { id: product }
+ send_request
+ end
+
+ it { expect(send_request).to have_http_status(:ok) }
+
+ context 'cannot see stock of product from different store' do
+ let(:product) { create(:product, stores: [create(:store)]) }
+
+ it { expect(send_request).to redirect_to(spree.admin_products_path) }
+
+ it do
+ send_request
+ expect(flash[:error]).to eq('Product is not found')
+ end
end
end
end
diff --git a/backend/spec/controllers/spree/admin/variants_controller_spec.rb b/backend/spec/controllers/spree/admin/variants_controller_spec.rb
index a76cd98b6e..b356c047d4 100644
--- a/backend/spec/controllers/spree/admin/variants_controller_spec.rb
+++ b/backend/spec/controllers/spree/admin/variants_controller_spec.rb
@@ -11,12 +11,14 @@ module Spree
describe '#index' do
let!(:variant_1) { create(:variant, product: product) }
let!(:variant_2) { create(:variant, product: product) }
+ let!(:variant_3) { create(:variant, product: create(:product)) }
context 'deleted is not requested' do
it 'assigns the variants for a requested product' do
get :index, params: { product_id: product.slug }
expect(assigns(:collection)).to include variant_1
expect(assigns(:collection)).to include variant_2
+ expect(assigns(:collection)).not_to include variant_3
end
end
@@ -37,8 +39,6 @@ module Spree
end
let(:variant) { create(:variant, product: product) }
- let(:variants) { double(ActiveRecord::Relation) }
- let(:products) { double(ActiveRecord::Relation) }
shared_examples 'correct response' do
it { expect(assigns(:variant)).to eq(variant) }
@@ -53,6 +53,24 @@ module Spree
it { expect(flash[:success]).to eq(Spree.t('notice_messages.variant_deleted')) }
end
end
+
+ context 'cannot destroy variant of other product' do
+ let(:other_product) { create(:product, stores: [store]) }
+ let(:variant) { create(:variant, product: other_product) }
+
+ it { expect(send_request).to redirect_to(spree.admin_product_variants_path(product)) }
+
+ it do
+ send_request
+ expect(flash[:error]).to eq('Variant is not found')
+ end
+ end
+
+ context 'cannot destroy variant of product from different store' do
+ let(:product) { create(:product, stores: [create(:store)]) }
+
+ it { expect { send_request }.to raise_error(ActiveRecord::RecordNotFound) }
+ end
end
end
end
diff --git a/backend/spec/features/admin/products/edit/images_spec.rb b/backend/spec/features/admin/products/edit/images_spec.rb
index 0f6c3078e4..5fe8f52a0b 100644
--- a/backend/spec/features/admin/products/edit/images_spec.rb
+++ b/backend/spec/features/admin/products/edit/images_spec.rb
@@ -5,6 +5,9 @@ describe 'Product Images', type: :feature, js: true do
let(:file_path) { Rails.root + '../../spec/support/ror_ringer.jpeg' }
+ let(:store) { Spree::Store.default }
+ let(:product) { create(:product, stores: [store]) }
+
before do
# Ensure attachment style keys are symbolized before running all tests
# Otherwise this would result in this error:
@@ -15,8 +18,7 @@ describe 'Product Images', type: :feature, js: true do
context 'uploading, editing, and deleting an image' do
it 'allows an admin to upload and edit an image for a product' do
- create(:product, stores: Spree::Store.all)
-
+ product
visit spree.admin_products_path
click_icon(:edit)
click_link 'Images'
@@ -42,9 +44,9 @@ describe 'Product Images', type: :feature, js: true do
# Regression test for #2228
it 'sees variant images', js: false do
- variant = create(:variant)
+ variant = create(:variant, product: product)
create_image(variant, File.open(file_path))
- visit spree.admin_product_images_path(variant.product)
+ visit spree.admin_product_images_path(product)
expect(page).not_to have_content('No Images Found.')
within('table.table') do
@@ -66,7 +68,6 @@ describe 'Product Images', type: :feature, js: true do
end
it 'does not see variant column when product has no variants', js: false do
- product = create(:product, stores: Spree::Store.all)
create_image(product, File.open(file_path))
visit spree.admin_product_images_path(product)
diff --git a/backend/spec/features/admin/products/variant_prices_spec.rb b/backend/spec/features/admin/products/variant_prices_spec.rb
index 33a104c304..446d2ac53a 100644
--- a/backend/spec/features/admin/products/variant_prices_spec.rb
+++ b/backend/spec/features/admin/products/variant_prices_spec.rb
@@ -2,8 +2,13 @@ require 'spec_helper'
describe 'Variant Prices', type: :feature, js: true do
stub_authorization!
- let!(:store) { create(:store, default: true, supported_currencies: 'USD,EUR') }
+ let(:store) { Spree::Store.default }
let!(:store2) { create(:store, default: false, default_currency: 'GBP') }
+
+ before do
+ store.update(supported_currencies: 'USD,EUR')
+ end
+
let!(:product) { create(:product, stores: Spree::Store.all) }
context 'with USD and EUR as currencies' do