From 20146a8e113910aeb913ee852ff07621d4c5bd55 Mon Sep 17 00:00:00 2001 From: Ahmed Ejaz Date: Sun, 16 Feb 2025 01:56:10 +0500 Subject: [PATCH] update respective specs --- .../admin/bulk_line_items_controller_spec.rb | 20 +- .../api/v0/orders_controller_spec.rb | 20 +- .../admin/mail_methods_controller_spec.rb | 3 +- .../spree/admin/variants_controller_spec.rb | 273 ++++++++++-------- .../scope_variants_for_search_spec.rb | 15 +- .../orders_and_distributors_report_spec.rb | 1 + spec/models/spree/ability_spec.rb | 19 ++ spec/services/permissions/order_spec.rb | 2 +- .../spree/admin/orders/edit.html.haml_spec.rb | 2 +- 9 files changed, 229 insertions(+), 126 deletions(-) diff --git a/spec/controllers/admin/bulk_line_items_controller_spec.rb b/spec/controllers/admin/bulk_line_items_controller_spec.rb index 89303ca6d4..8c62de79bd 100644 --- a/spec/controllers/admin/bulk_line_items_controller_spec.rb +++ b/spec/controllers/admin/bulk_line_items_controller_spec.rb @@ -123,8 +123,24 @@ RSpec.describe Admin::BulkLineItemsController, type: :controller do get :index, as: :json end - it "does not display line items for which my enterprise is a supplier" do - expect(response).to redirect_to unauthorized_path + context "with no distributor allows to edit orders" do + before { get :index, as: :json } + + it "does not display line items for which my enterprise is a supplier" do + expect(response).to redirect_to unauthorized_path + end + end + + context "with distributor allows to edit orders" do + before do + distributor1.update_columns(enable_producers_to_edit_orders: true) + get :index, as: :json + end + + it "retrieves a list of line_items from the supplier" do + keys = json_response['line_items'].first.keys.map(&:to_sym) + expect(line_item_attributes.all?{ |attr| keys.include? attr }).to eq(true) + end end end diff --git a/spec/controllers/api/v0/orders_controller_spec.rb b/spec/controllers/api/v0/orders_controller_spec.rb index a6d15774fa..558f14696b 100644 --- a/spec/controllers/api/v0/orders_controller_spec.rb +++ b/spec/controllers/api/v0/orders_controller_spec.rb @@ -84,11 +84,25 @@ module Api context 'producer enterprise' do before do allow(controller).to receive(:spree_current_user) { supplier.owner } - get :index end - it "does not display line items for which my enterprise is a supplier" do - assert_unauthorized! + context "with no distributor allows to edit orders" do + before { get :index } + + it "does not display line items for which my enterprise is a supplier" do + assert_unauthorized! + end + end + + context "with distributor allows to edit orders" do + before do + distributor.update_columns(enable_producers_to_edit_orders: true) + get :index + end + + it "retrieves a list of orders which have my supplied products" do + returns_orders(json_response) + end end end diff --git a/spec/controllers/spree/admin/mail_methods_controller_spec.rb b/spec/controllers/spree/admin/mail_methods_controller_spec.rb index 5221204c73..25592aa25b 100644 --- a/spec/controllers/spree/admin/mail_methods_controller_spec.rb +++ b/spec/controllers/spree/admin/mail_methods_controller_spec.rb @@ -22,7 +22,8 @@ RSpec.describe Spree::Admin::MailMethodsController do owned_groups: nil) allow(user).to receive_messages(enterprises: [create(:enterprise)], admin?: true, - locale: nil) + locale: nil, + can_manage_orders?: true) allow(controller).to receive_messages(spree_current_user: user) expect { diff --git a/spec/controllers/spree/admin/variants_controller_spec.rb b/spec/controllers/spree/admin/variants_controller_spec.rb index a443e4d449..6ec3bb6720 100644 --- a/spec/controllers/spree/admin/variants_controller_spec.rb +++ b/spec/controllers/spree/admin/variants_controller_spec.rb @@ -5,151 +5,192 @@ require 'spec_helper' module Spree module Admin RSpec.describe VariantsController, type: :controller do - before { controller_login_as_admin } + context "log in as admin user" do + before { controller_login_as_admin } - describe "#index" do - describe "deleted variants" do - let(:product) { create(:product, name: 'Product A') } - let(:deleted_variant) do - deleted_variant = product.variants.create( - unit_value: "2", variant_unit: "weight", variant_unit_scale: 1, price: 1, - primary_taxon: create(:taxon), supplier: create(:supplier_enterprise) - ) - deleted_variant.delete - deleted_variant - end + describe "#index" do + describe "deleted variants" do + let(:product) { create(:product, name: 'Product A') } + let(:deleted_variant) do + deleted_variant = product.variants.create( + unit_value: "2", variant_unit: "weight", variant_unit_scale: 1, price: 1, + primary_taxon: create(:taxon), supplier: create(:supplier_enterprise) + ) + deleted_variant.delete + deleted_variant + end - it "lists only non-deleted variants with params[:deleted] == off" do - spree_get :index, product_id: product.id, deleted: "off" - expect(assigns(:variants)).to eq(product.variants) - end + it "lists only non-deleted variants with params[:deleted] == off" do + spree_get :index, product_id: product.id, deleted: "off" + expect(assigns(:variants)).to eq(product.variants) + end - it "lists only deleted variants with params[:deleted] == on" do - spree_get :index, product_id: product.id, deleted: "on" - expect(assigns(:variants)).to eq([deleted_variant]) + it "lists only deleted variants with params[:deleted] == on" do + spree_get :index, product_id: product.id, deleted: "on" + expect(assigns(:variants)).to eq([deleted_variant]) + end end end - end - describe "#update" do - let!(:variant) { create(:variant, display_name: "Tomatoes", sku: 123, supplier: producer) } - let(:producer) { create(:enterprise) } + describe "#update" do + let!(:variant) { create(:variant, display_name: "Tomatoes", sku: 123, supplier: producer) } + let(:producer) { create(:enterprise) } - it "updates the variant" do - expect { - spree_put( - :update, - id: variant.id, - product_id: variant.product.id, - variant: { display_name: "Better tomatoes", sku: 456 } - ) - variant.reload - }.to change { variant.display_name }.to("Better tomatoes") - .and change { variant.sku }.to(456.to_s) - end - - context "when updating supplier" do - let(:new_producer) { create(:enterprise) } - - it "updates the supplier" do + it "updates the variant" do expect { + spree_put( + :update, + id: variant.id, + product_id: variant.product.id, + variant: { display_name: "Better tomatoes", sku: 456 } + ) + variant.reload + }.to change { variant.display_name }.to("Better tomatoes") + .and change { variant.sku }.to(456.to_s) + end + + context "when updating supplier" do + let(:new_producer) { create(:enterprise) } + + it "updates the supplier" do + expect { + spree_put( + :update, + id: variant.id, + product_id: variant.product.id, + variant: { supplier_id: new_producer.id } + ) + variant.reload + }.to change { variant.supplier_id }.to(new_producer.id) + end + + it "removes associated product from existing Order Cycles" do + distributor = create(:distributor_enterprise) + order_cycle = create( + :simple_order_cycle, + variants: [variant], + coordinator: distributor, + distributors: [distributor] + ) + spree_put( :update, id: variant.id, product_id: variant.product.id, variant: { supplier_id: new_producer.id } ) - variant.reload - }.to change { variant.supplier_id }.to(new_producer.id) + + expect(order_cycle.reload.distributed_variants).not_to include variant + end + end + end + + describe "#search" do + let(:supplier) { create(:supplier_enterprise) } + let!(:p1) { create(:simple_product, name: 'Product 1', supplier_id: supplier.id) } + let!(:p2) { create(:simple_product, name: 'Product 2', supplier_id: supplier.id) } + let!(:v1) { p1.variants.first } + let!(:v2) { p2.variants.first } + let!(:vo) { create(:variant_override, variant: v1, hub: d, count_on_hand: 44) } + let!(:d) { create(:distributor_enterprise) } + let!(:oc) { create(:simple_order_cycle, distributors: [d], variants: [v1]) } + + it "filters by distributor" do + spree_get :search, q: 'Prod', distributor_id: d.id.to_s + expect(assigns(:variants)).to eq([v1]) end - it "removes associated product from existing Order Cycles" do - distributor = create(:distributor_enterprise) - order_cycle = create( - :simple_order_cycle, - variants: [variant], - coordinator: distributor, - distributors: [distributor] - ) + it "applies variant overrides" do + spree_get :search, q: 'Prod', distributor_id: d.id.to_s + expect(assigns(:variants)).to eq([v1]) + expect(assigns(:variants).first.on_hand).to eq(44) + end - spree_put( - :update, - id: variant.id, - product_id: variant.product.id, - variant: { supplier_id: new_producer.id } - ) + it "filters by order cycle" do + spree_get :search, q: 'Prod', order_cycle_id: oc.id.to_s + expect(assigns(:variants)).to eq([v1]) + end - expect(order_cycle.reload.distributed_variants).not_to include variant + it "does not filter when no distributor or order cycle is specified" do + spree_get :search, q: 'Prod' + expect(assigns(:variants)).to match_array [v1, v2] + end + end + + describe '#destroy' do + let(:variant) { create(:variant) } + + context 'when requesting with html' do + before do + allow(Spree::Variant).to receive(:find).with(variant.id.to_s) { variant } + allow(variant).to receive(:destroy).and_call_original + end + + it 'deletes the variant' do + spree_delete :destroy, id: variant.id, product_id: variant.product.id, + format: 'html' + expect(variant).to have_received(:destroy) + end + + it 'shows a success flash message' do + spree_delete :destroy, id: variant.id, product_id: variant.product.id, + format: 'html' + expect(flash[:success]).to be + end + + it 'redirects to admin_product_variants_url' do + spree_delete :destroy, id: variant.id, product_id: variant.product.id, + format: 'html' + expect(response).to redirect_to spree.admin_product_variants_url(variant.product.id) + end + + it 'destroys all its exchanges' do + exchange = create(:exchange) + variant.exchanges << exchange + + spree_delete :destroy, id: variant.id, product_id: variant.product.id, + format: 'html' + expect(variant.exchanges.reload).to be_empty + end end end end - describe "#search" do - let(:supplier) { create(:supplier_enterprise) } - let!(:p1) { create(:simple_product, name: 'Product 1', supplier_id: supplier.id) } - let!(:p2) { create(:simple_product, name: 'Product 2', supplier_id: supplier.id) } + context "log in as supplier and distributor enable_producers_to_edit_orders" do + let(:supplier1) { create(:supplier_enterprise) } + let(:supplier2) { create(:supplier_enterprise) } + let!(:p1) { create(:simple_product, name: 'Product 1', supplier_id: supplier1.id) } + let!(:p2) { create(:simple_product, name: 'Product 2', supplier_id: supplier2.id) } let!(:v1) { p1.variants.first } let!(:v2) { p2.variants.first } - let!(:vo) { create(:variant_override, variant: v1, hub: d, count_on_hand: 44) } - let!(:d) { create(:distributor_enterprise) } - let!(:oc) { create(:simple_order_cycle, distributors: [d], variants: [v1]) } + let!(:d) { create(:distributor_enterprise, enable_producers_to_edit_orders: true) } + let!(:oc) { create(:simple_order_cycle, distributors: [d], variants: [v1, v2]) } - it "filters by distributor" do - spree_get :search, q: 'Prod', distributor_id: d.id.to_s - expect(assigns(:variants)).to eq([v1]) + before do + order = create(:order_with_line_items, distributor: d, line_items_count: 1) + order.line_items.take.variant.update_attribute(:supplier_id, supplier1.id) + controller_login_as_enterprise_user([supplier1]) end - it "applies variant overrides" do - spree_get :search, q: 'Prod', distributor_id: d.id.to_s - expect(assigns(:variants)).to eq([v1]) - expect(assigns(:variants).first.on_hand).to eq(44) + describe "#search" do + it "filters by distributor and supplier1 products" do + spree_get :search, q: 'Prod', distributor_id: d.id.to_s + expect(assigns(:variants)).to eq([v1]) + end end - it "filters by order cycle" do - spree_get :search, q: 'Prod', order_cycle_id: oc.id.to_s - expect(assigns(:variants)).to eq([v1]) - end - - it "does not filter when no distributor or order cycle is specified" do - spree_get :search, q: 'Prod' - expect(assigns(:variants)).to match_array [v1, v2] - end - end - - describe '#destroy' do - let(:variant) { create(:variant) } - - context 'when requesting with html' do - before do - allow(Spree::Variant).to receive(:find).with(variant.id.to_s) { variant } - allow(variant).to receive(:destroy).and_call_original - end - - it 'deletes the variant' do - spree_delete :destroy, id: variant.id, product_id: variant.product.id, - format: 'html' - expect(variant).to have_received(:destroy) - end - - it 'shows a success flash message' do - spree_delete :destroy, id: variant.id, product_id: variant.product.id, - format: 'html' - expect(flash[:success]).to be - end - - it 'redirects to admin_product_variants_url' do - spree_delete :destroy, id: variant.id, product_id: variant.product.id, - format: 'html' - expect(response).to redirect_to spree.admin_product_variants_url(variant.product.id) - end - - it 'destroys all its exchanges' do - exchange = create(:exchange) - variant.exchanges << exchange - - spree_delete :destroy, id: variant.id, product_id: variant.product.id, - format: 'html' - expect(variant.exchanges.reload).to be_empty + describe "#update" do + it "updates the variant" do + expect { + spree_put( + :update, + id: v1.id, + product_id: v1.product.id, + variant: { display_name: "Better tomatoes", sku: 456 } + ) + v1.reload + }.to change { v1.display_name }.to("Better tomatoes") + .and change { v1.sku }.to(456.to_s) end end end diff --git a/spec/lib/open_food_network/scope_variants_for_search_spec.rb b/spec/lib/open_food_network/scope_variants_for_search_spec.rb index fc16580d15..277aaa6680 100644 --- a/spec/lib/open_food_network/scope_variants_for_search_spec.rb +++ b/spec/lib/open_food_network/scope_variants_for_search_spec.rb @@ -19,8 +19,9 @@ RSpec.describe OpenFoodNetwork::ScopeVariantsForSearch do let!(:oc3) { create(:simple_order_cycle, distributors: [d2], variants: [v4]) } let!(:s1) { create(:schedule, order_cycles: [oc1]) } let!(:s2) { create(:schedule, order_cycles: [oc2]) } + let(:spree_current_user) { create(:user) } - let(:scoper) { OpenFoodNetwork::ScopeVariantsForSearch.new(params) } + let(:scoper) { OpenFoodNetwork::ScopeVariantsForSearch.new(params, spree_current_user) } describe "search" do let(:result) { scoper.search } @@ -66,10 +67,20 @@ RSpec.describe OpenFoodNetwork::ScopeVariantsForSearch do it "returns all products distributed through that distributor" do expect{ result }.to query_database [ + "TRANSACTION", + "Spree::User Exists?", + "Spree::User Create", + "Customer Load", + "Customer Load", + "Spree::Order Load", + "TRANSACTION", "Enterprise Load", "VariantOverride Load", - "SQL" + "SQL", + "Enterprise Pluck", + "Enterprise Load" ] + expect(result).to include v4 expect(result).not_to include v1, v2, v3 end diff --git a/spec/lib/reports/orders_and_distributors_report_spec.rb b/spec/lib/reports/orders_and_distributors_report_spec.rb index 98f4943f29..07c68b55ea 100644 --- a/spec/lib/reports/orders_and_distributors_report_spec.rb +++ b/spec/lib/reports/orders_and_distributors_report_spec.rb @@ -151,6 +151,7 @@ RSpec.describe Reporting::Reports::OrdersAndDistributors::Base do subject # build context first expect { subject.table_rows }.to query_database [ + "Enterprise Pluck", "SQL", "Spree::LineItem Load", "Spree::PaymentMethod Load", diff --git a/spec/models/spree/ability_spec.rb b/spec/models/spree/ability_spec.rb index 1339a39328..6060d7bfc5 100644 --- a/spec/models/spree/ability_spec.rb +++ b/spec/models/spree/ability_spec.rb @@ -240,6 +240,24 @@ RSpec.describe Spree::Ability do it { expect(subject.can_manage_enterprises?(user)).to be true } it { expect(subject.can_manage_orders?(user)).to be false } it { expect(subject.can_manage_order_cycles?(user)).to be false } + + context "with no distributor allows me to edit orders" do + it { expect(subject.can_manage_orders?(user)).to be false } + it { expect(subject.can_manage_line_items_in_orders?(user)).to be false } + end + + context "with any distributor allows me to edit orders containing my product" do + before do + order = create( + :order_with_line_items, + line_items_count: 1, + distributor: create(:distributor_enterprise, enable_producers_to_edit_orders: true) + ) + order.line_items.first.variant.update!(supplier_id: enterprise_none_producer.id) + end + + it { expect(subject.can_manage_line_items_in_orders?(user)).to be true } + end end context "as a profile" do @@ -260,6 +278,7 @@ RSpec.describe Spree::Ability do it { expect(subject.can_manage_products?(user)).to be false } it { expect(subject.can_manage_enterprises?(user)).to be false } it { expect(subject.can_manage_orders?(user)).to be false } + it { expect(subject.can_manage_line_items_in_orders?(user)).to be false } it { expect(subject.can_manage_order_cycles?(user)).to be false } it "can create enterprises straight off the bat" do diff --git a/spec/services/permissions/order_spec.rb b/spec/services/permissions/order_spec.rb index d85777a583..f0cba2275b 100644 --- a/spec/services/permissions/order_spec.rb +++ b/spec/services/permissions/order_spec.rb @@ -4,7 +4,7 @@ require 'spec_helper' module Permissions RSpec.describe Order do - let(:user) { double(:user) } + let(:user) { double(:user, can_manage_line_items_in_orders_only?: false) } let(:permissions) { Permissions::Order.new(user) } let!(:basic_permissions) { OpenFoodNetwork::Permissions.new(user) } let(:distributor) { create(:distributor_enterprise) } diff --git a/spec/views/spree/admin/orders/edit.html.haml_spec.rb b/spec/views/spree/admin/orders/edit.html.haml_spec.rb index 644af45b88..ca22a3c226 100644 --- a/spec/views/spree/admin/orders/edit.html.haml_spec.rb +++ b/spec/views/spree/admin/orders/edit.html.haml_spec.rb @@ -21,7 +21,7 @@ RSpec.describe "spree/admin/orders/edit.html.haml" do end end - allow(view).to receive_messages spree_current_user: create(:user) + allow(view).to receive_messages spree_current_user: create(:admin_user) end context "when order is complete" do