From b8ce6ed0fc2e706e05e09d230e26326722df34a2 Mon Sep 17 00:00:00 2001 From: Rob Harrington Date: Wed, 8 Apr 2015 13:28:49 +1000 Subject: [PATCH] Coordinator can see any enterprises that are already in the order cycle --- lib/open_food_network/permissions.rb | 17 ++++----- .../lib/open_food_network/permissions_spec.rb | 35 +++++++++++++++++-- 2 files changed, 42 insertions(+), 10 deletions(-) diff --git a/lib/open_food_network/permissions.rb b/lib/open_food_network/permissions.rb index 03e5d491e4..9a98d7a750 100644 --- a/lib/open_food_network/permissions.rb +++ b/lib/open_food_network/permissions.rb @@ -24,20 +24,21 @@ module OpenFoodNetwork coordinator = order_cycle.coordinator if managed_enterprises.include? coordinator - coordinator_permitted = [] + coordinator_permitted = [coordinator] + all_active = [] - if coordinator.sells == "own" - # Coordinators that sell own can only see themselves in the OC interface - coordinator_permitted = [coordinator] - elsif coordinator.sells == "any" + if coordinator.sells == "any" # If I manage the coordinator (or possibly in the future, if coordinator has made order cycle a friends of friend OC) # Any hubs that have granted the coordinator P-OC (or any enterprises that have granted mine P-OC if we do friends of friends) # If the coordinator sells any, relationships come into play - coordinator_permitted = granting(:add_to_order_cycle, to: [coordinator]).pluck(:id) - coordinator_permitted = coordinator_permitted | [coordinator] + granting(:add_to_order_cycle, to: [coordinator]).pluck(:id).each do |enterprise_id| + coordinator_permitted << enterprise_id + end + + all_active = order_cycle.suppliers.pluck(:id) | order_cycle.distributors.pluck(:id) end - Enterprise.where(id: coordinator_permitted) + Enterprise.where(id: coordinator_permitted | all_active) else # Any enterprises that I manage directly, which have granted P-OC to the coordinator managed_permitted = granting(:add_to_order_cycle, to: [coordinator], scope: managed_enterprises_in(order_cycle) ).pluck(:id) diff --git a/spec/lib/open_food_network/permissions_spec.rb b/spec/lib/open_food_network/permissions_spec.rb index 31566cbe54..fb281300c3 100644 --- a/spec/lib/open_food_network/permissions_spec.rb +++ b/spec/lib/open_food_network/permissions_spec.rb @@ -38,15 +38,17 @@ module OpenFoodNetwork end context "as a manager of the coordinator" do - it "returns the coordinator itself" do + before do permissions.stub(:managed_enterprises) { Enterprise.where(id: [coordinator]) } + end + + it "returns the coordinator itself" do expect(permissions.order_cycle_enterprises_for(oc)).to include coordinator end context "where P-OC has been granted to the coordinator by other enterprises" do before do create(:enterprise_relationship, parent: hub, child: coordinator, permissions_list: [:add_to_order_cycle]) - permissions.stub(:managed_enterprises) { Enterprise.where(id: [coordinator]) } end context "where the coordinator sells any" do @@ -65,6 +67,35 @@ module OpenFoodNetwork end end end + + context "where P-OC has not been granted to the coordinator by other enterprises" do + context "where the other enterprise are already in the order cycle" do + let!(:ex_incoming) { create(:exchange, order_cycle: oc, sender: producer, receiver: coordinator, incoming: true) } + let!(:ex_outgoing) { create(:exchange, order_cycle: oc, sender: coordinator, receiver: hub, incoming: false) } + + context "where the coordinator sells any" do + it "returns enterprises which have granted P-OC to the coordinator" do + enterprises = permissions.order_cycle_enterprises_for(oc) + expect(enterprises).to include hub, producer + end + end + + context "where the coordinator sells 'own'" do + before { coordinator.stub(:sells) { 'own' } } + it "returns just the coordinator" do + enterprises = permissions.order_cycle_enterprises_for(oc) + expect(enterprises).to_not include hub, producer + end + end + end + + context "where the other enterprises are not in the order cycle" do + it "returns just the coordinator" do + enterprises = permissions.order_cycle_enterprises_for(oc) + expect(enterprises).to_not include hub, producer + end + end + end end context "as a manager of a hub" do