<commit_before>require 'spec_helper'

describe Spree::Admin::ProductsController, type: :controller do
  describe 'bulk_update' do
    context "updating a product we do not have access to" do
      let(:s_managed) { create(:enterprise) }
      let(:s_unmanaged) { create(:enterprise) }
      let(:product) do
        create(:simple_product, supplier: s_unmanaged, name: 'Peas')
      end

      before do
        controller_login_as_enterprise_user [s_managed]
        spree_post :bulk_update,
                   "products" => [{ "id" => product.id, "name" => "Pine nuts" }]
      end

      it "denies access" do
        expect(response).to redirect_to unauthorized_path
      end

      it "does not update any product" do
        expect(product.reload.name).not_to eq("Pine nuts")
      end
    end

    context "when changing a product's variant_unit" do
      let(:producer) { create(:enterprise) }
      let!(:product) do
        create(
          :simple_product,
          supplier: producer,
          variant_unit: 'items',
          variant_unit_scale: nil,
          variant_unit_name: 'bunches',
          unit_value: nil,
          unit_description: 'some description'
        )
      end

      before { controller_login_as_enterprise_user([producer]) }

      it 'fails' do
        spree_post :bulk_update,
                   "products" => [
                     {
                       "id" => product.id,
                       "variant_unit" => "weight",
                       "variant_unit_scale" => 1
                     }
                   ]

        expect(response).to have_http_status(400)
      end

      it 'does not redirect to bulk_products' do
        spree_post :bulk_update,
                   "products" => [
                     {
                       "id" => product.id,
                       "variant_unit" => "weight",
                       "variant_unit_scale" => 1
                     }
                   ]

        expect(response).not_to redirect_to(
          '/api/products/bulk_products?page=1;per_page=500;'
        )
      end
    end

    context 'when passing empty variants_attributes' do
      let(:producer) { create(:enterprise) }
      let!(:product) do
        create(
          :simple_product,<commit_msg>update outdated spec<commit_after>require 'spec_helper'

describe Spree::Admin::ProductsController, type: :controller do
  describe 'bulk_update' do
    context "updating a product we do not have access to" do
      let(:s_managed) { create(:enterprise) }
      let(:s_unmanaged) { create(:enterprise) }
      let(:product) do
        create(:simple_product, supplier: s_unmanaged, name: 'Peas')
      end

      before do
        controller_login_as_enterprise_user [s_managed]
        spree_post :bulk_update,
                   "products" => [{ "id" => product.id, "name" => "Pine nuts" }]
      end

      it "denies access" do
        expect(response).to redirect_to unauthorized_path
      end

      it "does not update any product" do
        expect(product.reload.name).not_to eq("Pine nuts")
      end
    end

    context "when changing a product's variant_unit" do
      let(:producer) { create(:enterprise) }
      let!(:product) do
        create(
          :simple_product,
          supplier: producer,
          variant_unit: 'items',
          variant_unit_scale: nil,
          variant_unit_name: 'bunches',
          unit_value: nil,
          unit_description: 'some description'
        )
      end

      before { controller_login_as_enterprise_user([producer]) }

      it 'succeeds' do
        spree_post :bulk_update,
                   "products" => [
                     {
                       "id" => product.id,
                       "variant_unit" => "weight",
                       "variant_unit_scale" => 1
                     }
                   ]

        expect(response).to have_http_status(302)
      end

      it 'does not redirect to bulk_products' do
        spree_post :bulk_update,
                   "products" => [
                     {
                       "id" => product.id,
                       "variant_unit" => "weight",
                       "variant_unit_scale" => 1
                     }
                   ]

        expect(response).to redirect_to(
          '/api/products/bulk_products'
        )
      end
    end

    context 'when passing empty variants_attributes' do
      let(:producer) { create(:enterprise) }
      let!(:product) do
        create(
          :simple_product,
{'fraction_non_alphanumeric': 0.09653191959354981, 'fraction_numerical': 0.0030925557764523966, 'mean_word_length': 1.6557184750733138, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 25, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '6740327', 'n_tokens_mistral': 1297, 'n_tokens_neox': 1228, 'n_words': 287}