Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Allow producer to edit their products on hubs' orders #13113

Open
wants to merge 22 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 1 commit
Commits
Show all changes
22 commits
Select commit Hold shift + click to select a range
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 18 additions & 2 deletions spec/controllers/admin/bulk_line_items_controller_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -123,8 +123,24 @@
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

Expand Down
20 changes: 17 additions & 3 deletions spec/controllers/api/v0/orders_controller_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
3 changes: 2 additions & 1 deletion spec/controllers/spree/admin/mail_methods_controller_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,8 @@
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 {
Expand Down
275 changes: 158 additions & 117 deletions spec/controllers/spree/admin/variants_controller_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -5,151 +5,192 @@
module Spree
module Admin
RSpec.describe VariantsController, type: :controller 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

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])
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

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])
end
end
end
end

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) }
describe "#update" do
let!(:variant) { create(:variant, display_name: "Tomatoes", sku: 123, supplier: producer) }

Check warning on line 36 in spec/controllers/spree/admin/variants_controller_spec.rb

View workflow job for this annotation

GitHub Actions / runner / rubocop

[rubocop] reported by reviewdog 🐶 Line is too long. [101/100] Raw Output: spec/controllers/spree/admin/variants_controller_spec.rb:36:101: C: Layout/LineLength: Line is too long. [101/100]
let(: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: { supplier_id: new_producer.id }
variant: { display_name: "Better tomatoes", sku: 456 }
)
variant.reload
}.to change { variant.supplier_id }.to(new_producer.id)
}.to change { variant.display_name }.to("Better tomatoes")
.and change { variant.sku }.to(456.to_s)
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 }
)

expect(order_cycle.reload.distributed_variants).not_to include variant
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 }
)

expect(order_cycle.reload.distributed_variants).not_to include variant
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) }
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]) }
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 "filters by distributor" do
spree_get :search, q: 'Prod', distributor_id: d.id.to_s
expect(assigns(:variants)).to eq([v1])
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)
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)
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 "filters by order cycle" do
spree_get :search, q: 'Prod', order_cycle_id: oc.id.to_s
expect(assigns(:variants)).to eq([v1])
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

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]
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 '#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
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!(:d) { create(:distributor_enterprise, enable_producers_to_edit_orders: true) }
let!(:oc) { create(:simple_order_cycle, distributors: [d], variants: [v1, v2]) }

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
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 '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)
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 '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
Expand Down
Loading
Loading