<commit_before>require "spec_helper"

feature %q{
    As an admin
    I want to set a supplier and distributor(s) for a product
} do
  include AuthenticationWorkflow
  include WebHelper
  let!(:taxon) { create(:taxon) }

  background do
    @supplier = create(:supplier_enterprise, :name => 'New supplier')
    @distributors = (1..3).map { create(:distributor_enterprise) }
    @enterprise_fees = (0..2).map { |i| create(:enterprise_fee, enterprise: @distributors[i]) }
  end

  describe "creating a product" do
    scenario "assigning important attributes", js: true do
      tax_category = create(:tax_category, name: 'Test Tax Category')
      login_to_admin_section

      click_link 'Products'
      click_link 'New Product'

      select 'Test Tax Category', from: 'product_tax_category_id'
      select 'New supplier', from: 'product_supplier_id'
      fill_in 'product_name', with: 'A new product !!!'
      select "Weight (kg)", from: 'product_variant_unit_with_scale'
      fill_in 'product_unit_value_with_description', with: 5
      select taxon.name, from: "product_primary_taxon_id"
      fill_in 'product_price', with: '19.99'
      fill_in 'product_on_hand', with: 5
      fill_in 'product_description', with: "A description..."

      click_button 'Create'

      flash_message.should == 'Product "A new product !!!" has been successfully created!'
      product = Spree::Product.find_by_name('A new product !!!')
      product.tax_category_id.should == tax_category.id
      product.supplier.should == @supplier
      product.variant_unit.should == 'weight'
      product.variant_unit_scale.should == 1000
      product.unit_value.should == 5000
      product.unit_description.should == ""
      product.variant_unit_name.should == ""
      product.primary_taxon_id.should == taxon.id
      product.price.to_s.should == '19.99'
      product.on_hand.should == 5
      product.description.should == "A description..."
      product.group_buy.should be_false
      product.master.option_values.map(&:name).should == ['5kg']
      product.master.options_text.should == "5kg"

      # Distributors
      visit spree.product_distributions_admin_product_path(product)

      check @distributors[0].name
      select2_select @enterprise_fees[0].name, :from => 'product_product_distributions_attributes_0_enterprise_fee_id'
      check @distributors[2].name
      select2_select @enterprise_fees[2].name, :from => 'product_product_distributions_attributes_2_enterprise_fee_id'

      click_button 'Update'
      
      product.reload
      product.distributors.sort.should == [@distributors[0], @distributors[2]].sort


      product.product_distributions.map { |pd| pd.enterprise_fee }.sort.should == [@enterprise_fees[0], @enterprise_fees[2]].sort
    end

    scenario "making a product into a group buy product" do
      product = create(:simple_product, name: 'group buy product')

      login_to_admin_section

      visit spree.edit_admin_product_path(product)

      choose 'product_group_buy_1'
      fill_in 'Group buy unit size', :with => '10'

      click_button 'Update'

      flash_message.should == 'Product "group buy product" has been successfully updated!'
      product.reload
      product.group_buy.should be_true
      product.group_buy_unit_size.should == 10.0
    end
  end

  context "as an enterprise user" do

    before do
      @new_user = create_enterprise_user
      @supplier2 = create(:supplier_enterprise, name: 'Another Supplier')
      @supplier_permitted = create(:supplier_enterprise, name: 'Permitted Supplier')
      @new_user.enterprise_roles.build(enterprise: @supplier2).save
      @new_user.enterprise_roles.build(enterprise: @distributors[0]).save
      create(:enterprise_relationship, parent: @supplier_permitted, child: @supplier2,
             permissions_list: [:manage_products])

      login_to_admin_as @new_user
    end


    context "additional fields" do
      it "should have a notes field" do
        product = create(:simple_product, supplier: @supplier2)
        visit spree.edit_admin_product_path product
        page.should have_content "Notes"
      end
    end

    scenario "creating a new product" do
      click_link 'Products'
      click_link 'New Product'

      fill_in 'product_name', :with => 'A new product !!!'
      fill_in 'product_price', :with => '19.99'

      page.should have_selector('#product_supplier_id')
      select 'Another Supplier', :from => 'product_supplier_id'
      select taxon.name, from: "product_primary_taxon_id"
      page.should have_select 'product_tax_category_id' if Spree::TaxCategory.any?

      # Should only have suppliers listed which the user can manage
      page.should have_select 'product_supplier_id', with_options: [@supplier2.name, @supplier_permitted.name]
      page.should_not have_select 'product_supplier_id', with_options: [@supplier.name]

      click_button 'Create'

      flash_message.should == 'Product "A new product !!!" has been successfully created!'
      product = Spree::Product.find_by_name('A new product !!!')
      product.supplier.should == @supplier2
    end

    scenario "editing a product" do
      product = create(:simple_product, name: 'a product', supplier: @supplier2)

      visit spree.edit_admin_product_path product

      page.should have_select 'product_tax_category_id'
      select 'Permitted Supplier', from: 'product_supplier_id'
      click_button 'Update'
      flash_message.should == 'Product "a product" has been successfully updated!'
      product.reload
      product.supplier.should == @supplier_permitted
    end

    scenario "editing product distributions" do
      product = create(:simple_product, supplier: @supplier2)

      visit spree.edit_admin_product_path product
      within('#sidebar') { click_link 'Product Distributions' }

      check @distributors[0].name
      select @enterprise_fees[0].name, :from => 'product_product_distributions_attributes_0_enterprise_fee_id'

      # Should only have distributors listed which the user can manage
      within "#product_product_distributions_field" do
        page.should_not have_content @distributors[1].name
        page.should_not have_content @distributors[2].name
      end

      click_button 'Update'
      flash_message.should == "Product \"#{product.name}\" has been successfully updated!"

      product.distributors.should == [@distributors[0]]
    end


    scenario "deleting product properties", js: true do
      # Given a product with a property
      p = create(:simple_product, supplier: @supplier)
      p.set_property('fooprop', 'fooval')
<commit_msg>Tighten spec: setting tax category should succeed<commit_after>require "spec_helper"

feature %q{
    As an admin
    I want to set a supplier and distributor(s) for a product
} do
  include AuthenticationWorkflow
  include WebHelper


  let!(:taxon) { create(:taxon) }

  background do
    @supplier = create(:supplier_enterprise, :name => 'New supplier')
    @distributors = (1..3).map { create(:distributor_enterprise) }
    @enterprise_fees = (0..2).map { |i| create(:enterprise_fee, enterprise: @distributors[i]) }
  end

  describe "creating a product" do
    let!(:tax_category) { create(:tax_category, name: 'Test Tax Category') }

    scenario "assigning important attributes", js: true do
      login_to_admin_section

      click_link 'Products'
      click_link 'New Product'

      select 'Test Tax Category', from: 'product_tax_category_id'
      select 'New supplier', from: 'product_supplier_id'
      fill_in 'product_name', with: 'A new product !!!'
      select "Weight (kg)", from: 'product_variant_unit_with_scale'
      fill_in 'product_unit_value_with_description', with: 5
      select taxon.name, from: "product_primary_taxon_id"
      fill_in 'product_price', with: '19.99'
      fill_in 'product_on_hand', with: 5
      fill_in 'product_description', with: "A description..."

      click_button 'Create'

      flash_message.should == 'Product "A new product !!!" has been successfully created!'
      product = Spree::Product.find_by_name('A new product !!!')
      product.tax_category_id.should == tax_category.id
      product.supplier.should == @supplier
      product.variant_unit.should == 'weight'
      product.variant_unit_scale.should == 1000
      product.unit_value.should == 5000
      product.unit_description.should == ""
      product.variant_unit_name.should == ""
      product.primary_taxon_id.should == taxon.id
      product.price.to_s.should == '19.99'
      product.on_hand.should == 5
      product.description.should == "A description..."
      product.group_buy.should be_false
      product.master.option_values.map(&:name).should == ['5kg']
      product.master.options_text.should == "5kg"

      # Distributors
      visit spree.product_distributions_admin_product_path(product)

      check @distributors[0].name
      select2_select @enterprise_fees[0].name, :from => 'product_product_distributions_attributes_0_enterprise_fee_id'
      check @distributors[2].name
      select2_select @enterprise_fees[2].name, :from => 'product_product_distributions_attributes_2_enterprise_fee_id'

      click_button 'Update'
      
      product.reload
      product.distributors.sort.should == [@distributors[0], @distributors[2]].sort


      product.product_distributions.map { |pd| pd.enterprise_fee }.sort.should == [@enterprise_fees[0], @enterprise_fees[2]].sort
    end

    scenario "making a product into a group buy product" do
      product = create(:simple_product, name: 'group buy product')

      login_to_admin_section

      visit spree.edit_admin_product_path(product)

      choose 'product_group_buy_1'
      fill_in 'Group buy unit size', :with => '10'

      click_button 'Update'

      flash_message.should == 'Product "group buy product" has been successfully updated!'
      product.reload
      product.group_buy.should be_true
      product.group_buy_unit_size.should == 10.0
    end
  end

  context "as an enterprise user" do
    let!(:tax_category) { create(:tax_category) }

    before do
      @new_user = create_enterprise_user
      @supplier2 = create(:supplier_enterprise, name: 'Another Supplier')
      @supplier_permitted = create(:supplier_enterprise, name: 'Permitted Supplier')
      @new_user.enterprise_roles.build(enterprise: @supplier2).save
      @new_user.enterprise_roles.build(enterprise: @distributors[0]).save
      create(:enterprise_relationship, parent: @supplier_permitted, child: @supplier2,
             permissions_list: [:manage_products])

      login_to_admin_as @new_user
    end


    context "additional fields" do
      it "should have a notes field" do
        product = create(:simple_product, supplier: @supplier2)
        visit spree.edit_admin_product_path product
        page.should have_content "Notes"
      end
    end

    scenario "creating a new product" do
      click_link 'Products'
      click_link 'New Product'

      fill_in 'product_name', :with => 'A new product !!!'
      fill_in 'product_price', :with => '19.99'

      page.should have_selector('#product_supplier_id')
      select 'Another Supplier', :from => 'product_supplier_id'
      select taxon.name, from: "product_primary_taxon_id"
      select tax_category.name, from: "product_tax_category_id"

      # Should only have suppliers listed which the user can manage
      page.should have_select 'product_supplier_id', with_options: [@supplier2.name, @supplier_permitted.name]
      page.should_not have_select 'product_supplier_id', with_options: [@supplier.name]

      click_button 'Create'

      flash_message.should == 'Product "A new product !!!" has been successfully created!'
      product = Spree::Product.find_by_name('A new product !!!')
      product.supplier.should == @supplier2
      product.tax_category.should == tax_category
    end

    scenario "editing a product" do
      product = create(:simple_product, name: 'a product', supplier: @supplier2)

      visit spree.edit_admin_product_path product

      select 'Permitted Supplier', from: 'product_supplier_id'
      select tax_category.name, from: 'product_tax_category_id'
      click_button 'Update'
      flash_message.should == 'Product "a product" has been successfully updated!'
      product.reload
      product.supplier.should == @supplier_permitted
      product.tax_category.should == tax_category
    end

    scenario "editing product distributions" do
      product = create(:simple_product, supplier: @supplier2)

      visit spree.edit_admin_product_path product
      within('#sidebar') { click_link 'Product Distributions' }

      check @distributors[0].name
      select @enterprise_fees[0].name, :from => 'product_product_distributions_attributes_0_enterprise_fee_id'

      # Should only have distributors listed which the user can manage
      within "#product_product_distributions_field" do
        page.should_not have_content @distributors[1].name
        page.should_not have_content @distributors[2].name
      end

      click_button 'Update'
      flash_message.should == "Product \"#{product.name}\" has been successfully updated!"

      product.distributors.should == [@distributors[0]]
    end


    scenario "deleting product properties", js: true do
      # Given a product with a property
      p = create(:simple_product, supplier: @supplier)
      p.set_property('fooprop', 'fooval')

{'fraction_non_alphanumeric': 0.10040998881848677, 'fraction_numerical': 0.009243384271338055, 'mean_word_length': 3.6454293628808863, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 19, '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': '7540909', 'n_tokens_mistral': 4112, 'n_tokens_neox': 3936, 'n_words': 948}