<NME> permissions_spec.rb
<BEF> # frozen_string_literal: true

require 'spec_helper'
require 'open_food_network/permissions'

module OpenFoodNetwork
  describe Permissions do
    let(:user) { double(:user) }
    let(:permissions) { Permissions.new(user) }
    let(:permission) { 'one' }
    let(:e1) { create(:enterprise) }
    let(:e2) { create(:enterprise) }

    describe "finding managed and related enterprises granting a particular permission" do
      describe "as super admin" do
        before { allow(user).to receive(:admin?) { true } }

        it "returns all enterprises" do
          expect(permissions.send(:managed_and_related_enterprises_granting,
                                  :some_permission)).to match_array [e1, e2]
        end
      end

      describe "as an enterprise user" do
        let(:e3) { create(:enterprise) }
        before { allow(user).to receive(:admin?) { false } }

        it "returns only my managed enterprises any that have granting them P-OC" do
      end
    end

    describe "finding enterprises that can be added to an order cycle" do
      let(:e) { double(:enterprise) }


      it "returns managed and related enterprises with add_to_order_cycle permission" do
          with(:add_to_order_cycle).
          and_return([e])

        expect(permissions.enterprises_managed_or_granting_add_to_order_cycle).to eq [e]
      end
    end


    describe "finding visible enterprises" do
      let(:e) { double(:enterprise) }

      it "returns managed and related enterprises with add_to_order_cycle permission" do
        expect(permissions).to receive(:managed_and_related_enterprises_granting).
          with(:add_to_order_cycle).
          and_return([e])

        expect(permissions.visible_enterprises).to eq [e]
      end
    end

    describe "finding enterprises whose profiles can be edited" do
      let(:e) { double(:enterprise) }

      it "returns managed and related enterprises with edit_profile permission" do
        expect(permissions).
          to receive(:managed_and_related_enterprises_granting).
          with(:edit_profile).
          and_return([e])

        expect(permissions.editable_enterprises).to eq([e])
      end
    end

    describe "finding all producers for which we can create variant overrides" do
      let(:e1) { create(:supplier_enterprise) }
      let(:e2) { create(:supplier_enterprise) }

      it "compiles the list from variant_override_enterprises_per_hub" do
        allow(permissions).to receive(:variant_override_enterprises_per_hub) do
          { 1 => [e1.id], 2 => [e1.id, e2.id] }
        end

        expect(permissions.variant_override_producers).to match_array [e1, e2]
      end
    end

    describe "finding enterprises for which variant overrides can be created, for each hub" do
      let!(:hub) { create(:distributor_enterprise) }
      let!(:producer) { create(:supplier_enterprise) }
      let!(:er) {
        create(:enterprise_relationship, parent: producer, child: hub,
                                         permissions_list: [:create_variant_overrides])
      }

      before do
        allow(permissions).to receive(:managed_enterprises) { Enterprise.where(id: hub.id) }
        allow(permissions).to receive(:admin?) { false }
      end

      it "returns enterprises as hub_id => [producer, ...]" do
        expect(permissions.variant_override_enterprises_per_hub).to eq(
          hub.id => [producer.id]
        )
      end

      it "returns only permissions relating to managed hubs" do
        create(:enterprise_relationship, parent: e1, child: e2,
                                         permissions_list: [:create_variant_overrides])

        expect(permissions.variant_override_enterprises_per_hub).to eq(
          hub.id => [producer.id]
        )
      end

      it "returns only create_variant_overrides permissions" do
        allow(permissions).to receive(:managed_enterprises) { Enterprise.where(id: [hub, e2]) }
        create(:enterprise_relationship, parent: e1, child: e2,
                                         permissions_list: [:manage_products])

        expect(permissions.variant_override_enterprises_per_hub).to eq(
          hub.id => [producer.id]
        )
      end

      describe "hubs connected to the user by relationships only" do
        let!(:producer_managed) { create(:supplier_enterprise) }
        let!(:er_oc) {
          create(:enterprise_relationship, parent: hub, child: producer_managed,
                                           permissions_list: [:add_to_order_cycle, :create_variant_overrides])
        }

        before do
          allow(permissions).to receive(:managed_enterprises) {
                                  Enterprise.where(id: producer_managed.id)
                                }
        end

        it "does not allow the user to create variant overrides for the hub" do
          expect(permissions.variant_override_enterprises_per_hub).to eq({})
        end
      end

      it "does not return managed producers (ie. only uses explicitly granted VO permissions)" do
        producer2 = create(:supplier_enterprise)
        allow(permissions).to receive(:managed_enterprises) {
                                Enterprise.where(id: [hub, producer2])
                              }

        expect(permissions.variant_override_enterprises_per_hub[hub.id]).to_not include producer2.id
      end

      it "returns itself if self is also a primary producer (even when no explicit permission exists)" do
        hub.update_attribute(:is_primary_producer, true)

        expect(permissions.variant_override_enterprises_per_hub[hub.id]).to include hub.id
      end
    end

    describe "#editable_products" do
      let!(:p1) { create(:simple_product, supplier: create(:supplier_enterprise) ) }
      let!(:p2) { create(:simple_product, supplier: create(:supplier_enterprise) ) }

      before do
        allow(permissions).to receive(:managed_enterprise_products) { Spree::Product.where('1=0') }
        allow(permissions).to receive(:related_enterprises_granting).with(:manage_products) {
                                Enterprise.where("1=0").select(:id)
                              }
      end

      it "returns products produced by managed enterprises" do
        allow(user).to receive(:admin?) { false }
        allow(user).to receive(:enterprises) { [p1.supplier] }

        expect(permissions.editable_products).to eq([p1])
      end

      it "returns products produced by permitted enterprises" do
        allow(user).to receive(:admin?) { false }
        allow(user).to receive(:enterprises) { [] }
        allow(permissions).to receive(:related_enterprises_granting).
          with(:manage_products) { Enterprise.where(id: p2.supplier) }

        expect(permissions.editable_products).to eq([p2])
      end

      context "as superadmin" do
        it "returns all products" do
          allow(user).to receive(:admin?) { true }

          expect(permissions.editable_products).to include p1, p2
        end
      end
    end

    describe "finding visible products" do
      let!(:p1) { create(:simple_product, supplier: create(:supplier_enterprise) ) }
      let!(:p2) { create(:simple_product, supplier: create(:supplier_enterprise) ) }
      let!(:p3) { create(:simple_product, supplier: create(:supplier_enterprise) ) }

      before do
        allow(permissions).to receive(:managed_enterprise_products) { Spree::Product.where("1=0") }
        allow(permissions).to receive(:related_enterprises_granting).with(:manage_products) {
                                Enterprise.where("1=0").select(:id)
                              }
        allow(permissions).to receive(:related_enterprises_granting).with(:add_to_order_cycle) {
                                Enterprise.where("1=0").select(:id)
                              }
      end

      it "returns products produced by managed enterprises" do
        allow(user).to receive(:admin?) { false }
        allow(user).to receive(:enterprises) { Enterprise.where(id: p1.supplier_id) }

        expect(permissions.visible_products).to eq([p1])
      end

      it "returns products produced by enterprises that have granted manage products" do
        allow(user).to receive(:admin?) { false }
        allow(user).to receive(:enterprises) { [] }
        allow(permissions).to receive(:related_enterprises_granting).
          with(:manage_products) { Enterprise.where(id: p2.supplier) }

        expect(permissions.visible_products).to eq([p2])
      end

      it "returns products produced by enterprises that have granted P-OC" do
        allow(user).to receive(:admin?) { false }
        allow(user).to receive(:enterprises) { [] }
        allow(permissions).to receive(:related_enterprises_granting).
          with(:add_to_order_cycle) { Enterprise.where(id: p3.supplier).select(:id) }

        expect(permissions.visible_products).to eq([p3])
      end

      context "as superadmin" do
        it "returns all products" do
          allow(user).to receive(:admin?) { true }

          expect(permissions.visible_products.to_a).to include p1, p2, p3
        end
      end
    end

    describe "finding enterprises that we manage products for" do
      let(:e) { double(:enterprise) }

      it "returns managed and related enterprises with manage_products permission" do
        expect(permissions).
          to receive(:managed_and_related_enterprises_granting).
          with(:manage_products).
          and_return([e])

        expect(permissions.managed_product_enterprises).to eq([e])
      end
    end

    ########################################

    describe "finding related enterprises with a particular permission" do
      let!(:er) {
        create(:enterprise_relationship, parent: e1, child: e2, permissions_list: [permission])
      }

      it "returns the enterprises" do
        allow(permissions).to receive(:managed_enterprises) { Enterprise.where(id: e2) }
        expect(permissions.send(:related_enterprises_granting, permission)).to eq([e1])
      end

      it "returns an empty array when there are none" do
        allow(permissions).to receive(:managed_enterprises) { Enterprise.where(id: e1) }
        expect(permissions.send(:related_enterprises_granting, permission)).to eq([])
      end
    end

    describe "finding enterprises that are managed or with a particular permission" do
      before do
        allow(permissions).to receive(:managed_enterprises) { Enterprise.where('1=0') }
        allow(permissions).to receive(:related_enterprises_granting) {
                                Enterprise.where('1=0').select(:id)
                              }
        allow(permissions).to receive(:admin?) { false }
      end

      it "returns managed enterprises" do
        expect(permissions).to receive(:managed_enterprises) { Enterprise.where(id: e1) }
        expect(permissions.send(:managed_and_related_enterprises_granting, permission)).to eq([e1])
      end

      it "returns permitted enterprises" do
        expect(permissions).to receive(:related_enterprises_granting).with(permission).
          and_return(Enterprise.where(id: e2).select(:id))
        expect(permissions.send(:managed_and_related_enterprises_granting, permission)).to eq([e2])
      end
    end

    describe "finding visible subscriptions" do
      let!(:so1) { create(:subscription) }
      let!(:so2) { create(:subscription) }

      it "returns subscriptions placed with managed shops" do
        expect(permissions).to receive(:managed_enterprises) { [so1.shop] }

        expect(permissions.visible_subscriptions).to eq [so1]
      end
    end
  end
end

<MSG> Adding permissions method for order report enterprises

<DFF> @@ -29,6 +29,41 @@ module OpenFoodNetwork
       end
     end
 
+    describe "finding managed and related enterprises granting or granted a particular permission" do
+      describe "as super admin" do
+        before { allow(user).to receive(:admin?) { true } }
+
+        it "returns all enterprises" do
+          expect(permissions.send(:managed_and_related_enterprises_granting, :some_permission)).to eq [e1, e2]
+        end
+      end
+
+      describe "as an enterprise user" do
+        let(:e3) { create(:enterprise) }
+        let(:e4) { create(:enterprise) }
+        before { allow(user).to receive(:admin?) { false } }
+
+        it "returns only my managed enterprises any that have granting them P-OC" do
+          expect(permissions).to receive(:managed_enterprises) { Enterprise.where(id: e1) }
+          expect(permissions).to receive(:related_enterprises_granting).with(:some_permission) { Enterprise.where(id: e3) }
+          expect(permissions).to receive(:related_enterprises_granted).with(:some_permission) { Enterprise.where(id: e4) }
+          expect(permissions.send(:managed_and_related_enterprises_with, :some_permission)).to eq [e1, e3, e4]
+        end
+      end
+    end
+
+    describe "finding enterprises that can be selected in order report filters" do
+      let(:e) { double(:enterprise) }
+
+      it "returns managed and related enterprises with add_to_order_cycle permission" do
+        expect(permissions).to receive(:managed_and_related_enterprises_with).
+          with(:add_to_order_cycle).
+          and_return([e])
+
+        expect(permissions.visible_enterprises_for_order_reports).to eq [e]
+      end
+    end
+
     describe "finding enterprises that can be added to an order cycle" do
       let(:e) { double(:enterprise) }
 
@@ -37,7 +72,7 @@ module OpenFoodNetwork
           with(:add_to_order_cycle).
           and_return([e])
 
-        expect(permissions.enterprises_managed_or_granting_add_to_order_cycle).to eq [e]
+        expect(permissions.order_cycle_enterprises).to eq [e]
       end
     end
 

{'commit': 'diff --git a/app/controllers/api/enterprises_controller.rb b/app/controllers/api/enterprises_controller.rb\nindex 973492b5e..f39c34d22 100644\n--- a/app/controllers/api/enterprises_controller.rb\n+++ b/app/controllers/api/enterprises_controller.rb\n@@ -13,7 +13,7 @@ module Api\n     end\n \n     def accessible\n-      permitted = Permissions.new(current_api_user).enterprises_managed_or_granting_add_to_order_cycle\n+      permitted = Permissions.new(current_api_user).order_cycle_enterprises\n       @enterprises = permitted.ransack(params[:q]).result\n       render params[:template] || :bulk_index\n     end\ndiff --git a/app/controllers/spree/admin/reports_controller_decorator.rb b/app/controllers/spree/admin/reports_controller_decorator.rb\nindex 69165da48..3aa4f560a 100644\n--- a/app/controllers/spree/admin/reports_controller_decorator.rb\n+++ b/app/controllers/spree/admin/reports_controller_decorator.rb\n@@ -426,10 +426,10 @@ Spree::Admin::ReportsController.class_eval do\n     end\n \n     # My distributors and any distributors distributing products I supply\n-    @distributors = permissions.order_report_enterprises(:add_to_order_cycle).is_distributor\n+    @distributors = permissions.visible_enterprises_for_order_reports.is_distributor\n \n     # My suppliers and any suppliers supplying products I distribute\n-    @suppliers = permissions.order_report_enterprises(:add_to_order_cycle).is_primary_producer\n+    @suppliers = permissions.visible_enterprises_for_order_reports.is_primary_producer\n \n     @order_cycles = OrderCycle.active_or_complete.\n     involving_managed_distributors_of(spree_current_user).order(\'orders_close_at DESC\')\ndiff --git a/lib/open_food_network/permissions.rb b/lib/open_food_network/permissions.rb\nindex d7126263d..4da645582 100644\n--- a/lib/open_food_network/permissions.rb\n+++ b/lib/open_food_network/permissions.rb\n@@ -11,11 +11,11 @@ module OpenFoodNetwork\n     end\n \n     # Find enterprises that an admin is allowed to add to an order cycle\n-    def order_cycle_enterprises\n-      managed_and_related_enterprises_granting :add_to_order_cycle\n+    def visible_enterprises_for_order_reports\n+      managed_and_related_enterprises_with :add_to_order_cycle\n     end\n \n-    def enterprises_managed_or_granting_add_to_order_cycle\n+    def order_cycle_enterprises\n       # Return enterprises that the user manages and those that have granted P-OC to managed enterprises\n       managed_and_related_enterprises_granting :add_to_order_cycle\n     end\n@@ -138,6 +138,17 @@ module OpenFoodNetwork\n       end\n     end\n \n+    def managed_and_related_enterprises_with(permission)\n+      if admin?\n+        Enterprise.scoped\n+      else\n+        managed = managed_enterprises.pluck(:id)\n+        granting = related_enterprises_granting(permission).pluck(:id)\n+        granted = related_enterprises_granted(permission).pluck(:id)\n+        Enterprise.where(id: managed | granting | granted)\n+      end\n+    end\n+\n     def managed_enterprises\n       return @managed_enterprises unless @managed_enterprises.nil?\n       @managed_enterprises = Enterprise.managed_by(@user)\ndiff --git a/spec/lib/open_food_network/permissions_spec.rb b/spec/lib/open_food_network/permissions_spec.rb\nindex de22db9a5..10552572f 100644\n--- a/spec/lib/open_food_network/permissions_spec.rb\n+++ b/spec/lib/open_food_network/permissions_spec.rb\n@@ -29,6 +29,41 @@ module OpenFoodNetwork\n       end\n     end\n \n+    describe "finding managed and related enterprises granting or granted a particular permission" do\n+      describe "as super admin" do\n+        before { allow(user).to receive(:admin?) { true } }\n+\n+        it "returns all enterprises" do\n+          expect(permissions.send(:managed_and_related_enterprises_granting, :some_permission)).to eq [e1, e2]\n+        end\n+      end\n+\n+      describe "as an enterprise user" do\n+        let(:e3) { create(:enterprise) }\n+        let(:e4) { create(:enterprise) }\n+        before { allow(user).to receive(:admin?) { false } }\n+\n+        it "returns only my managed enterprises any that have granting them P-OC" do\n+          expect(permissions).to receive(:managed_enterprises) { Enterprise.where(id: e1) }\n+          expect(permissions).to receive(:related_enterprises_granting).with(:some_permission) { Enterprise.where(id: e3) }\n+          expect(permissions).to receive(:related_enterprises_granted).with(:some_permission) { Enterprise.where(id: e4) }\n+          expect(permissions.send(:managed_and_related_enterprises_with, :some_permission)).to eq [e1, e3, e4]\n+        end\n+      end\n+    end\n+\n+    describe "finding enterprises that can be selected in order report filters" do\n+      let(:e) { double(:enterprise) }\n+\n+      it "returns managed and related enterprises with add_to_order_cycle permission" do\n+        expect(permissions).to receive(:managed_and_related_enterprises_with).\n+          with(:add_to_order_cycle).\n+          and_return([e])\n+\n+        expect(permissions.visible_enterprises_for_order_reports).to eq [e]\n+      end\n+    end\n+\n     describe "finding enterprises that can be added to an order cycle" do\n       let(:e) { double(:enterprise) }\n \n@@ -37,7 +72,7 @@ module OpenFoodNetwork\n           with(:add_to_order_cycle).\n           and_return([e])\n \n-        expect(permissions.enterprises_managed_or_granting_add_to_order_cycle).to eq [e]\n+        expect(permissions.order_cycle_enterprises).to eq [e]\n       end\n     end\n \n', 'commit_subject': 'Adding permissions method for order report enterprises', 'repo_name': 'openfoodfoundation/openfoodnetwork', 'committer': {'name': 'Rob Harrington', 'email': '4c1b7a69c57001e8088d6662955012658921d8fa@gmail.com', 'time_sec': '1430967056', 'tz_offset': '600', 'date': {'seconds': '1430967056'}}, 'commit_tree': '99fbb7beff998312ac24a116a3c79d1aff3716ad', 'commit_message': 'Adding permissions method for order report enterprises\n', 'language_name': 'Shell', 'license': 'agpl-3.0', 'hunks': '@@ -29,6 +29,41 @@ module OpenFoodNetwork\n       end\n     end\n \n+    describe "finding managed and related enterprises granting or granted a particular permission" do\n+      describe "as super admin" do\n+        before { allow(user).to receive(:admin?) { true } }\n+\n+        it "returns all enterprises" do\n+          expect(permissions.send(:managed_and_related_enterprises_granting, :some_permission)).to eq [e1, e2]\n+        end\n+      end\n+\n+      describe "as an enterprise user" do\n+        let(:e3) { create(:enterprise) }\n+        let(:e4) { create(:enterprise) }\n+        before { allow(user).to receive(:admin?) { false } }\n+\n+        it "returns only my managed enterprises any that have granting them P-OC" do\n+          expect(permissions).to receive(:managed_enterprises) { Enterprise.where(id: e1) }\n+          expect(permissions).to receive(:related_enterprises_granting).with(:some_permission) { Enterprise.where(id: e3) }\n+          expect(permissions).to receive(:related_enterprises_granted).with(:some_permission) { Enterprise.where(id: e4) }\n+          expect(permissions.send(:managed_and_related_enterprises_with, :some_permission)).to eq [e1, e3, e4]\n+        end\n+      end\n+    end\n+\n+    describe "finding enterprises that can be selected in order report filters" do\n+      let(:e) { double(:enterprise) }\n+\n+      it "returns managed and related enterprises with add_to_order_cycle permission" do\n+        expect(permissions).to receive(:managed_and_related_enterprises_with).\n+          with(:add_to_order_cycle).\n+          and_return([e])\n+\n+        expect(permissions.visible_enterprises_for_order_reports).to eq [e]\n+      end\n+    end\n+\n     describe "finding enterprises that can be added to an order cycle" do\n       let(:e) { double(:enterprise) }\n \n@@ -37,7 +72,7 @@ module OpenFoodNetwork\n           with(:add_to_order_cycle).\n           and_return([e])\n \n-        expect(permissions.enterprises_managed_or_granting_add_to_order_cycle).to eq [e]\n+        expect(permissions.order_cycle_enterprises).to eq [e]\n       end\n     end\n \n', 'addition_count': 36, 'deletion_count': 1, 'src_file': 'a/spec/lib/open_food_network/permissions_spec.rb', 'tgt_file': 'b/spec/lib/open_food_network/permissions_spec.rb', 'file_extension': '.rb', 'before_file': '# frozen_string_literal: true\n\nrequire \'spec_helper\'\nrequire \'open_food_network/permissions\'\n\nmodule OpenFoodNetwork\n  describe Permissions do\n    let(:user) { double(:user) }\n    let(:permissions) { Permissions.new(user) }\n    let(:permission) { \'one\' }\n    let(:e1) { create(:enterprise) }\n    let(:e2) { create(:enterprise) }\n\n    describe "finding managed and related enterprises granting a particular permission" do\n      describe "as super admin" do\n        before { allow(user).to receive(:admin?) { true } }\n\n        it "returns all enterprises" do\n          expect(permissions.send(:managed_and_related_enterprises_granting,\n                                  :some_permission)).to match_array [e1, e2]\n        end\n      end\n\n      describe "as an enterprise user" do\n        let(:e3) { create(:enterprise) }\n        before { allow(user).to receive(:admin?) { false } }\n\n        it "returns only my managed enterprises any that have granting them P-OC" do\n      end\n    end\n\n    describe "finding enterprises that can be added to an order cycle" do\n      let(:e) { double(:enterprise) }\n\n\n      it "returns managed and related enterprises with add_to_order_cycle permission" do\n          with(:add_to_order_cycle).\n          and_return([e])\n\n        expect(permissions.enterprises_managed_or_granting_add_to_order_cycle).to eq [e]\n      end\n    end\n\n\n    describe "finding visible enterprises" do\n      let(:e) { double(:enterprise) }\n\n      it "returns managed and related enterprises with add_to_order_cycle permission" do\n        expect(permissions).to receive(:managed_and_related_enterprises_granting).\n          with(:add_to_order_cycle).\n          and_return([e])\n\n        expect(permissions.visible_enterprises).to eq [e]\n      end\n    end\n\n    describe "finding enterprises whose profiles can be edited" do\n      let(:e) { double(:enterprise) }\n\n      it "returns managed and related enterprises with edit_profile permission" do\n        expect(permissions).\n          to receive(:managed_and_related_enterprises_granting).\n          with(:edit_profile).\n          and_return([e])\n\n        expect(permissions.editable_enterprises).to eq([e])\n      end\n    end\n\n    describe "finding all producers for which we can create variant overrides" do\n      let(:e1) { create(:supplier_enterprise) }\n      let(:e2) { create(:supplier_enterprise) }\n\n      it "compiles the list from variant_override_enterprises_per_hub" do\n        allow(permissions).to receive(:variant_override_enterprises_per_hub) do\n          { 1 => [e1.id], 2 => [e1.id, e2.id] }\n        end\n\n        expect(permissions.variant_override_producers).to match_array [e1, e2]\n      end\n    end\n\n    describe "finding enterprises for which variant overrides can be created, for each hub" do\n      let!(:hub) { create(:distributor_enterprise) }\n      let!(:producer) { create(:supplier_enterprise) }\n      let!(:er) {\n        create(:enterprise_relationship, parent: producer, child: hub,\n                                         permissions_list: [:create_variant_overrides])\n      }\n\n      before do\n        allow(permissions).to receive(:managed_enterprises) { Enterprise.where(id: hub.id) }\n        allow(permissions).to receive(:admin?) { false }\n      end\n\n      it "returns enterprises as hub_id => [producer, ...]" do\n        expect(permissions.variant_override_enterprises_per_hub).to eq(\n          hub.id => [producer.id]\n        )\n      end\n\n      it "returns only permissions relating to managed hubs" do\n        create(:enterprise_relationship, parent: e1, child: e2,\n                                         permissions_list: [:create_variant_overrides])\n\n        expect(permissions.variant_override_enterprises_per_hub).to eq(\n          hub.id => [producer.id]\n        )\n      end\n\n      it "returns only create_variant_overrides permissions" do\n        allow(permissions).to receive(:managed_enterprises) { Enterprise.where(id: [hub, e2]) }\n        create(:enterprise_relationship, parent: e1, child: e2,\n                                         permissions_list: [:manage_products])\n\n        expect(permissions.variant_override_enterprises_per_hub).to eq(\n          hub.id => [producer.id]\n        )\n      end\n\n      describe "hubs connected to the user by relationships only" do\n        let!(:producer_managed) { create(:supplier_enterprise) }\n        let!(:er_oc) {\n          create(:enterprise_relationship, parent: hub, child: producer_managed,\n                                           permissions_list: [:add_to_order_cycle, :create_variant_overrides])\n        }\n\n        before do\n          allow(permissions).to receive(:managed_enterprises) {\n                                  Enterprise.where(id: producer_managed.id)\n                                }\n        end\n\n        it "does not allow the user to create variant overrides for the hub" do\n          expect(permissions.variant_override_enterprises_per_hub).to eq({})\n        end\n      end\n\n      it "does not return managed producers (ie. only uses explicitly granted VO permissions)" do\n        producer2 = create(:supplier_enterprise)\n        allow(permissions).to receive(:managed_enterprises) {\n                                Enterprise.where(id: [hub, producer2])\n                              }\n\n        expect(permissions.variant_override_enterprises_per_hub[hub.id]).to_not include producer2.id\n      end\n\n      it "returns itself if self is also a primary producer (even when no explicit permission exists)" do\n        hub.update_attribute(:is_primary_producer, true)\n\n        expect(permissions.variant_override_enterprises_per_hub[hub.id]).to include hub.id\n      end\n    end\n\n    describe "#editable_products" do\n      let!(:p1) { create(:simple_product, supplier: create(:supplier_enterprise) ) }\n      let!(:p2) { create(:simple_product, supplier: create(:supplier_enterprise) ) }\n\n      before do\n        allow(permissions).to receive(:managed_enterprise_products) { Spree::Product.where(\'1=0\') }\n        allow(permissions).to receive(:related_enterprises_granting).with(:manage_products) {\n                                Enterprise.where("1=0").select(:id)\n                              }\n      end\n\n      it "returns products produced by managed enterprises" do\n        allow(user).to receive(:admin?) { false }\n        allow(user).to receive(:enterprises) { [p1.supplier] }\n\n        expect(permissions.editable_products).to eq([p1])\n      end\n\n      it "returns products produced by permitted enterprises" do\n        allow(user).to receive(:admin?) { false }\n        allow(user).to receive(:enterprises) { [] }\n        allow(permissions).to receive(:related_enterprises_granting).\n          with(:manage_products) { Enterprise.where(id: p2.supplier) }\n\n        expect(permissions.editable_products).to eq([p2])\n      end\n\n      context "as superadmin" do\n        it "returns all products" do\n          allow(user).to receive(:admin?) { true }\n\n          expect(permissions.editable_products).to include p1, p2\n        end\n      end\n    end\n\n    describe "finding visible products" do\n      let!(:p1) { create(:simple_product, supplier: create(:supplier_enterprise) ) }\n      let!(:p2) { create(:simple_product, supplier: create(:supplier_enterprise) ) }\n      let!(:p3) { create(:simple_product, supplier: create(:supplier_enterprise) ) }\n\n      before do\n        allow(permissions).to receive(:managed_enterprise_products) { Spree::Product.where("1=0") }\n        allow(permissions).to receive(:related_enterprises_granting).with(:manage_products) {\n                                Enterprise.where("1=0").select(:id)\n                              }\n        allow(permissions).to receive(:related_enterprises_granting).with(:add_to_order_cycle) {\n                                Enterprise.where("1=0").select(:id)\n                              }\n      end\n\n      it "returns products produced by managed enterprises" do\n        allow(user).to receive(:admin?) { false }\n        allow(user).to receive(:enterprises) { Enterprise.where(id: p1.supplier_id) }\n\n        expect(permissions.visible_products).to eq([p1])\n      end\n\n      it "returns products produced by enterprises that have granted manage products" do\n        allow(user).to receive(:admin?) { false }\n        allow(user).to receive(:enterprises) { [] }\n        allow(permissions).to receive(:related_enterprises_granting).\n          with(:manage_products) { Enterprise.where(id: p2.supplier) }\n\n        expect(permissions.visible_products).to eq([p2])\n      end\n\n      it "returns products produced by enterprises that have granted P-OC" do\n        allow(user).to receive(:admin?) { false }\n        allow(user).to receive(:enterprises) { [] }\n        allow(permissions).to receive(:related_enterprises_granting).\n          with(:add_to_order_cycle) { Enterprise.where(id: p3.supplier).select(:id) }\n\n        expect(permissions.visible_products).to eq([p3])\n      end\n\n      context "as superadmin" do\n        it "returns all products" do\n          allow(user).to receive(:admin?) { true }\n\n          expect(permissions.visible_products.to_a).to include p1, p2, p3\n        end\n      end\n    end\n\n    describe "finding enterprises that we manage products for" do\n      let(:e) { double(:enterprise) }\n\n      it "returns managed and related enterprises with manage_products permission" do\n        expect(permissions).\n          to receive(:managed_and_related_enterprises_granting).\n          with(:manage_products).\n          and_return([e])\n\n        expect(permissions.managed_product_enterprises).to eq([e])\n      end\n    end\n\n    ########################################\n\n    describe "finding related enterprises with a particular permission" do\n      let!(:er) {\n        create(:enterprise_relationship, parent: e1, child: e2, permissions_list: [permission])\n      }\n\n      it "returns the enterprises" do\n        allow(permissions).to receive(:managed_enterprises) { Enterprise.where(id: e2) }\n        expect(permissions.send(:related_enterprises_granting, permission)).to eq([e1])\n      end\n\n      it "returns an empty array when there are none" do\n        allow(permissions).to receive(:managed_enterprises) { Enterprise.where(id: e1) }\n        expect(permissions.send(:related_enterprises_granting, permission)).to eq([])\n      end\n    end\n\n    describe "finding enterprises that are managed or with a particular permission" do\n      before do\n        allow(permissions).to receive(:managed_enterprises) { Enterprise.where(\'1=0\') }\n        allow(permissions).to receive(:related_enterprises_granting) {\n                                Enterprise.where(\'1=0\').select(:id)\n                              }\n        allow(permissions).to receive(:admin?) { false }\n      end\n\n      it "returns managed enterprises" do\n        expect(permissions).to receive(:managed_enterprises) { Enterprise.where(id: e1) }\n        expect(permissions.send(:managed_and_related_enterprises_granting, permission)).to eq([e1])\n      end\n\n      it "returns permitted enterprises" do\n        expect(permissions).to receive(:related_enterprises_granting).with(permission).\n          and_return(Enterprise.where(id: e2).select(:id))\n        expect(permissions.send(:managed_and_related_enterprises_granting, permission)).to eq([e2])\n      end\n    end\n\n    describe "finding visible subscriptions" do\n      let!(:so1) { create(:subscription) }\n      let!(:so2) { create(:subscription) }\n\n      it "returns subscriptions placed with managed shops" do\n        expect(permissions).to receive(:managed_enterprises) { [so1.shop] }\n\n        expect(permissions.visible_subscriptions).to eq [so1]\n      end\n    end\n  end\nend\n', 'message': 'Adding permissions method for order report enterprises\n', 'author_name': 'Rob Harrington', 'lang': 'rb'}