mirror of
https://github.com/openfoodfoundation/openfoodnetwork
synced 2026-01-11 18:26:50 +00:00
remove delete methods from products reflex
This commit is contained in:
@@ -21,34 +21,6 @@ class ProductsReflex < ApplicationReflex
|
||||
fetch_and_render_products_with_flash
|
||||
end
|
||||
|
||||
def delete_product
|
||||
id = current_id_from_element(element)
|
||||
product = product_finder(id).find_product
|
||||
authorize! :delete, product
|
||||
|
||||
if product.destroy
|
||||
flash[:success] = I18n.t('admin.products_v3.delete_product.success')
|
||||
else
|
||||
flash[:error] = I18n.t('admin.products_v3.delete_product.error')
|
||||
end
|
||||
|
||||
fetch_and_render_products_with_flash
|
||||
end
|
||||
|
||||
def delete_variant
|
||||
id = current_id_from_element(element)
|
||||
variant = Spree::Variant.active.find(id)
|
||||
authorize! :delete, variant
|
||||
|
||||
if VariantDeleter.new.delete(variant)
|
||||
flash[:success] = I18n.t('admin.products_v3.delete_variant.success')
|
||||
else
|
||||
flash[:error] = I18n.t('admin.products_v3.delete_variant.error')
|
||||
end
|
||||
|
||||
fetch_and_render_products_with_flash
|
||||
end
|
||||
|
||||
private
|
||||
|
||||
def init_filters_params
|
||||
@@ -203,11 +175,4 @@ class ProductsReflex < ApplicationReflex
|
||||
.to_h.with_indifferent_access
|
||||
end
|
||||
|
||||
def product_finder(id)
|
||||
ProductScopeQuery.new(current_user, { id: })
|
||||
end
|
||||
|
||||
def current_id_from_element(element)
|
||||
element.dataset.current_id
|
||||
end
|
||||
end
|
||||
|
||||
@@ -1,100 +0,0 @@
|
||||
# frozen_string_literal: true
|
||||
|
||||
require "reflex_helper"
|
||||
|
||||
RSpec.describe ProductsReflex, type: :reflex, feature: :admin_style_v3 do
|
||||
let(:current_user) { create(:admin_user) } # todo: set up an enterprise user to test permissions
|
||||
let(:context) {
|
||||
{ url: admin_products_url, connection: { current_user: } }
|
||||
}
|
||||
let(:flash) { {} }
|
||||
|
||||
before do
|
||||
pending "fix spec"
|
||||
# Mock flash, because stimulus_reflex_testing doesn't support sessions
|
||||
allow_any_instance_of(described_class).to receive(:flash).and_return(flash)
|
||||
end
|
||||
|
||||
describe '#delete_product' do
|
||||
let(:product) { create(:simple_product) }
|
||||
let(:action_name) { :delete_product }
|
||||
|
||||
subject { build_reflex(method_name: action_name, **context) }
|
||||
|
||||
before { subject.element.dataset.current_id = product.id }
|
||||
|
||||
context 'given that the current user is admin' do
|
||||
let(:current_user) { create(:admin_user) }
|
||||
|
||||
it 'should successfully delete the product' do
|
||||
subject.run(action_name)
|
||||
product.reload
|
||||
expect(product.deleted_at).not_to be_nil
|
||||
expect(flash[:success]).to eq('Successfully deleted the product')
|
||||
end
|
||||
|
||||
it 'should be failed to delete the product' do
|
||||
# mock db query failure
|
||||
allow_any_instance_of(Spree::Product).to receive(:destroy).and_return(false)
|
||||
subject.run(action_name)
|
||||
product.reload
|
||||
expect(product.deleted_at).to be_nil
|
||||
expect(flash[:error]).to eq('Unable to delete the product')
|
||||
end
|
||||
end
|
||||
|
||||
context 'given that the current user is not admin' do
|
||||
let(:current_user) { create(:user) }
|
||||
|
||||
it 'should raise the access denied exception' do
|
||||
expect { subject.run(action_name) }.to raise_exception(CanCan::AccessDenied)
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
describe '#delete_variant' do
|
||||
let(:variant) { create(:variant) }
|
||||
let(:action_name) { :delete_variant }
|
||||
|
||||
subject { build_reflex(method_name: action_name, **context) }
|
||||
|
||||
before { subject.element.dataset.current_id = variant.id }
|
||||
|
||||
context 'given that the current user is admin' do
|
||||
let(:current_user) { create(:admin_user) }
|
||||
|
||||
it 'should successfully delete the variant' do
|
||||
subject.run(action_name)
|
||||
variant.reload
|
||||
expect(variant.deleted_at).not_to be_nil
|
||||
expect(flash[:success]).to eq('Successfully deleted the variant')
|
||||
end
|
||||
|
||||
it 'should be failed to delete the product' do
|
||||
# mock db query failure
|
||||
allow_any_instance_of(Spree::Variant).to receive(:destroy).and_return(false)
|
||||
subject.run(action_name)
|
||||
variant.reload
|
||||
expect(variant.deleted_at).to be_nil
|
||||
expect(flash[:error]).to eq('Unable to delete the variant')
|
||||
end
|
||||
end
|
||||
|
||||
context 'given that the current user is not admin' do
|
||||
let(:current_user) { create(:user) }
|
||||
|
||||
it 'should raise the access denied exception' do
|
||||
expect { subject.run(action_name) }.to raise_exception(CanCan::AccessDenied)
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
# Build and run a reflex using the context
|
||||
# Parameters can be added with params: option
|
||||
# For more options see https://github.com/podia/stimulus_reflex_testing#usage
|
||||
def run_reflex(method_name, opts = {})
|
||||
build_reflex(method_name:, **context.merge(opts)).tap{ |reflex|
|
||||
reflex.run(method_name)
|
||||
}
|
||||
end
|
||||
Reference in New Issue
Block a user