<NME> controller_helper.rb
<BEF> # frozen_string_literal: true
  module ControllerHelper
    def login_as_admin
      @admin_user ||= begin
                        user = create(:user)
                        user.spree_roles << Spree::Role.find_or_create_by_name!('admin')
                        user
                      end

      controller.stub spree_current_user: @admin_user
    end
  end
end

    def reset_controller_environment
      # Rails 5.0 introduced a bug in controller tests (fixed in 5.2) where the controller's
      # environment is essentially cached if multiple requests are made in the same `it` block,
      # meaning subsequent requests will not be handled well. This resets the environment.
      # This edge case is quite rare though; normally we only do one request per test.
      @request.env.delete("RAW_POST_DATA")
    end
  end
end

<MSG> Use controller login helpers

<DFF> @@ -2,12 +2,25 @@ module OpenFoodNetwork
   module ControllerHelper
     def login_as_admin
       @admin_user ||= begin
-                        user = create(:user)
-                        user.spree_roles << Spree::Role.find_or_create_by_name!('admin')
-                        user
-                      end
+        user = create(:user)
+        user.spree_roles << Spree::Role.find_or_create_by_name!('admin')
+        user
+      end
 
       controller.stub spree_current_user: @admin_user
     end
+
+    def login_as_enterprise_user(enterprises)
+      @enterprise_user ||= begin
+        user = create(:user)
+        user.spree_roles = []
+        enterprises.each do |enterprise|
+          enterprise.enterprise_roles.create!(user: user)
+        end
+        user
+      end
+
+      controller.stub spree_current_user: @enterprise_user
+    end
   end
 end

{'commit': 'diff --git a/spec/controllers/spree/admin/products_controller_spec.rb b/spec/controllers/spree/admin/products_controller_spec.rb\nindex aee2b6df5..f05be2bb6 100644\n--- a/spec/controllers/spree/admin/products_controller_spec.rb\n+++ b/spec/controllers/spree/admin/products_controller_spec.rb\n@@ -2,14 +2,8 @@ require \'spec_helper\'\n \n describe Spree::Admin::ProductsController do\n   context "Creating a new product" do\n-    let(:user) do\n-      user = create(:user)\n-      user.spree_roles << Spree::Role.find_or_create_by_name!(\'admin\')\n-      user\n-    end\n-\n     before do\n-      controller.stub spree_current_user: user\n+      login_as_admin\n     end\n \n     it "redirects to bulk_edit when the user hits \'create\'" do\n@@ -52,4 +46,4 @@ describe Spree::Admin::ProductsController do\n       response.should redirect_to "/admin/products/new"\n     end\n   end\n-end\n\\ No newline at end of file\n+end\ndiff --git a/spec/controllers/spree/admin/reports_controller_spec.rb b/spec/controllers/spree/admin/reports_controller_spec.rb\nindex 0afe63c63..432bc4e67 100644\n--- a/spec/controllers/spree/admin/reports_controller_spec.rb\n+++ b/spec/controllers/spree/admin/reports_controller_spec.rb\n@@ -56,16 +56,7 @@ describe Spree::Admin::ReportsController do\n   \n   # As a Distributor Enterprise user for d1  \n   context "Distributor Enterprise User" do\n-    let(:user) do\n-      user = create(:user)\n-      user.spree_roles = []\n-      d1.enterprise_roles.build(user: user).save\n-      user\n-    end\n-\n-    before :each do\n-      controller.stub :spree_current_user => user\n-    end\n+    before { login_as_enterprise_user [d1] }\n \n     describe \'Orders and Distributors\' do\n       it "only shows orders that I have access to" do\n@@ -117,16 +108,7 @@ describe Spree::Admin::ReportsController do\n \n   # As a Supplier Enterprise user for s1\n   context "Supplier" do\n-    let(:user) do\n-      user = create(:user)\n-      user.spree_roles = []\n-      s1.enterprise_roles.build(user: user).save\n-      user\n-    end\n-\n-    before :each do\n-      controller.stub :spree_current_user => user\n-    end\n+    before { login_as_enterprise_user [s1] }\n \n     describe \'Bulk Coop\' do\n       it "only shows product line items that I am supplying" do\n@@ -157,14 +139,7 @@ describe Spree::Admin::ReportsController do\n   end\n \n   context "Products & Inventory" do\n-    let(:user) do\n-      user = create(:user)\n-      user.spree_roles << Spree::Role.find_or_create_by_name!(\'admin\')\n-      user\n-    end\n-    before do\n-      controller.stub spree_current_user: user\n-    end\n+    before { login_as_admin }\n \n     it "should build distributors for the current user" do\n       spree_get :products_and_inventory\n@@ -188,24 +163,17 @@ describe Spree::Admin::ReportsController do\n \n     it "creates a ProductAndInventoryReport" do\n       OpenFoodNetwork::ProductsAndInventoryReport.should_receive(:new)\n-      .with(user, {"test"=>"foo", "controller"=>"spree/admin/reports", "action"=>"products_and_inventory"})\n-      .and_return(report = double(:report))\n+        .with(@admin_user, {"test" => "foo", "controller" => "spree/admin/reports", "action" => "products_and_inventory"})\n+        .and_return(report = double(:report))\n       report.stub(:header).and_return []\n       report.stub(:table).and_return []\n-      spree_get :products_and_inventory, :test => "foo"\n+      spree_get :products_and_inventory, test: "foo"\n       assigns(:report).should == report\n     end\n   end\n \n   context "My Customers" do\n-    let(:user) do\n-      user = create(:user)\n-      user.spree_roles << Spree::Role.find_or_create_by_name!(\'admin\')\n-      user\n-    end\n-    before do\n-      controller.stub spree_current_user: user\n-    end\n+    before { login_as_admin }\n \n     it "should have report types for customers" do\n       Spree::Admin::ReportsController::REPORT_TYPES[:customers].should == [\n@@ -236,13 +204,12 @@ describe Spree::Admin::ReportsController do\n \n     it "creates a CustomersReport" do\n       OpenFoodNetwork::CustomersReport.should_receive(:new)\n-      .with(user, {"test"=>"foo", "controller"=>"spree/admin/reports", "action"=>"customers"})\n-      .and_return(report = double(:report))\n+        .with(@admin_user, {"test" => "foo", "controller" => "spree/admin/reports", "action" => "customers"})\n+        .and_return(report = double(:report))\n       report.stub(:header).and_return []\n       report.stub(:table).and_return []\n-      spree_get :customers, :test => "foo"\n+      spree_get :customers, test: "foo"\n       assigns(:report).should == report\n     end\n   end\n-\n end\ndiff --git a/spec/support/controller_helper.rb b/spec/support/controller_helper.rb\nindex 79cb94e00..de0720283 100644\n--- a/spec/support/controller_helper.rb\n+++ b/spec/support/controller_helper.rb\n@@ -2,12 +2,25 @@ module OpenFoodNetwork\n   module ControllerHelper\n     def login_as_admin\n       @admin_user ||= begin\n-                        user = create(:user)\n-                        user.spree_roles << Spree::Role.find_or_create_by_name!(\'admin\')\n-                        user\n-                      end\n+        user = create(:user)\n+        user.spree_roles << Spree::Role.find_or_create_by_name!(\'admin\')\n+        user\n+      end\n \n       controller.stub spree_current_user: @admin_user\n     end\n+\n+    def login_as_enterprise_user(enterprises)\n+      @enterprise_user ||= begin\n+        user = create(:user)\n+        user.spree_roles = []\n+        enterprises.each do |enterprise|\n+          enterprise.enterprise_roles.create!(user: user)\n+        end\n+        user\n+      end\n+\n+      controller.stub spree_current_user: @enterprise_user\n+    end\n   end\n end\n', 'commit_subject': 'Use controller login helpers', 'repo_name': 'openfoodfoundation/openfoodnetwork', 'committer': {'name': 'Rohan Mitchell', 'email': '9671c73253cee9b18b8df367e3e627fab2863fe8@rohanmitchell.com', 'time_sec': '1405470598', 'tz_offset': '600', 'date': {'seconds': '1405470598'}}, 'commit_tree': 'ff737bc17de8ac3ba0a7667d5f3973cf0c66f440', 'commit_message': 'Use controller login helpers\n', 'language_name': 'Shell', 'license': 'agpl-3.0', 'hunks': "@@ -2,12 +2,25 @@ module OpenFoodNetwork\n   module ControllerHelper\n     def login_as_admin\n       @admin_user ||= begin\n-                        user = create(:user)\n-                        user.spree_roles << Spree::Role.find_or_create_by_name!('admin')\n-                        user\n-                      end\n+        user = create(:user)\n+        user.spree_roles << Spree::Role.find_or_create_by_name!('admin')\n+        user\n+      end\n \n       controller.stub spree_current_user: @admin_user\n     end\n+\n+    def login_as_enterprise_user(enterprises)\n+      @enterprise_user ||= begin\n+        user = create(:user)\n+        user.spree_roles = []\n+        enterprises.each do |enterprise|\n+          enterprise.enterprise_roles.create!(user: user)\n+        end\n+        user\n+      end\n+\n+      controller.stub spree_current_user: @enterprise_user\n+    end\n   end\n end\n", 'addition_count': 17, 'deletion_count': 4, 'src_file': 'a/spec/support/controller_helper.rb', 'tgt_file': 'b/spec/support/controller_helper.rb', 'file_extension': '.rb', 'before_file': '# frozen_string_literal: true\n  module ControllerHelper\n    def login_as_admin\n      @admin_user ||= begin\n                        user = create(:user)\n                        user.spree_roles << Spree::Role.find_or_create_by_name!(\'admin\')\n                        user\n                      end\n\n      controller.stub spree_current_user: @admin_user\n    end\n  end\nend\n\n    def reset_controller_environment\n      # Rails 5.0 introduced a bug in controller tests (fixed in 5.2) where the controller\'s\n      # environment is essentially cached if multiple requests are made in the same `it` block,\n      # meaning subsequent requests will not be handled well. This resets the environment.\n      # This edge case is quite rare though; normally we only do one request per test.\n      @request.env.delete("RAW_POST_DATA")\n    end\n  end\nend\n', 'message': 'Use controller login helpers\n', 'author_name': 'Rohan Mitchell', 'lang': 'rb'}