<commit_before>require 'spec_helper'

feature 'Promotion with option value rule' do
  stub_authorization!

  given(:variant) { create :variant }
  given!(:product) { variant.product }
  given!(:option_value) { variant.option_values.first }

  given(:promotion) { create :promotion }

  background do
    visit spree.edit_admin_promotion_path(promotion)
  end

  scenario "adding an option value rule", js: true do
    select2 "Option Value(s)", from: "Add rule of type"
    within("#rules_container") { click_button "Add" }

    within("#rules_container .promotion-block") do
      expect(page).to have_content("Product")
      expect(page).to have_content("Option Values")

      click_button "Add"
    end

    within('.promo-rule-option-value') do
      targetted_select2_search product.name, from: '.js-promo-rule-option-value-product-select'
      targetted_select2_search option_value.name, from: '.js-promo-rule-option-value-option-values-select'
    end

    within('#rules_container') { click_button "Update" }

    first_rule = promotion.rules(true).first
    expect(first_rule.class).to eq Spree::Promotion::Rules::OptionValue
    expect(first_rule.preferred_eligible_values).to eq Hash[product.id => [option_value.id]]
  end

  context "with an attempted XSS" do
    let(:xss_string) { %(<script>throw("XSS")</script>) }
    before do
      option_value.update!(name: xss_string)
    end
    scenario "adding an option value rule", js: true do
      select2 "Option Value(s)", from: "Add rule of type"
      within("#rules_container") { click_button "Add" }

      within("#rules_container .promotion-block") do
        click_button "Add"
      end

      within('.promo-rule-option-value') do
        targetted_select2_search product.name, from: '.js-promo-rule-option-value-product-select'
        targetted_select2_search option_value.name, from: '.js-promo-rule-option-value-option-values-select'
      end
    end
  end

  context "with an existing option value rule" do
    given(:variant1) { create :variant }
    given(:variant2) { create :variant }
    background do
      rule = Spree::Promotion::Rules::OptionValue.new
      rule.promotion = promotion
      rule.preferred_eligible_values = Hash[
        variant1.product_id => variant1.option_values.pluck(:id),
        variant2.product_id => variant2.option_values.pluck(:id)
      ]
      rule.save!

      visit spree.edit_admin_promotion_path(promotion)
    end

    scenario "deleting a product", js: true do
      within(".promo-rule-option-value:last-child") do
        find(".remove").click
      end

      within('#rules_container') { click_button "Update" }

      first_rule = promotion.rules(true).first
      expect(first_rule.preferred_eligible_values).to eq Hash[variant1.product_id => variant1.option_values.pluck(:id)]
    end
  end
end<commit_msg>Use assoc.reload instead of assoc(true)<commit_after>require 'spec_helper'

feature 'Promotion with option value rule' do
  stub_authorization!

  given(:variant) { create :variant }
  given!(:product) { variant.product }
  given!(:option_value) { variant.option_values.first }

  given(:promotion) { create :promotion }

  background do
    visit spree.edit_admin_promotion_path(promotion)
  end

  scenario "adding an option value rule", js: true do
    select2 "Option Value(s)", from: "Add rule of type"
    within("#rules_container") { click_button "Add" }

    within("#rules_container .promotion-block") do
      expect(page).to have_content("Product")
      expect(page).to have_content("Option Values")

      click_button "Add"
    end

    within('.promo-rule-option-value') do
      targetted_select2_search product.name, from: '.js-promo-rule-option-value-product-select'
      targetted_select2_search option_value.name, from: '.js-promo-rule-option-value-option-values-select'
    end

    within('#rules_container') { click_button "Update" }

    first_rule = promotion.rules.reload.first
    expect(first_rule.class).to eq Spree::Promotion::Rules::OptionValue
    expect(first_rule.preferred_eligible_values).to eq Hash[product.id => [option_value.id]]
  end

  context "with an attempted XSS" do
    let(:xss_string) { %(<script>throw("XSS")</script>) }
    before do
      option_value.update!(name: xss_string)
    end
    scenario "adding an option value rule", js: true do
      select2 "Option Value(s)", from: "Add rule of type"
      within("#rules_container") { click_button "Add" }

      within("#rules_container .promotion-block") do
        click_button "Add"
      end

      within('.promo-rule-option-value') do
        targetted_select2_search product.name, from: '.js-promo-rule-option-value-product-select'
        targetted_select2_search option_value.name, from: '.js-promo-rule-option-value-option-values-select'
      end
    end
  end

  context "with an existing option value rule" do
    given(:variant1) { create :variant }
    given(:variant2) { create :variant }
    background do
      rule = Spree::Promotion::Rules::OptionValue.new
      rule.promotion = promotion
      rule.preferred_eligible_values = Hash[
        variant1.product_id => variant1.option_values.pluck(:id),
        variant2.product_id => variant2.option_values.pluck(:id)
      ]
      rule.save!

      visit spree.edit_admin_promotion_path(promotion)
    end

    scenario "deleting a product", js: true do
      within(".promo-rule-option-value:last-child") do
        find(".remove").click
      end

      within('#rules_container') { click_button "Update" }

      first_rule = promotion.rules.reload.first
      expect(first_rule.preferred_eligible_values).to eq Hash[variant1.product_id => variant1.option_values.pluck(:id)]
    end
  end
end
{'fraction_non_alphanumeric': 0.11790469516468115, 'fraction_numerical': 0.004905395935529082, 'mean_word_length': 3.5672, 'pattern_counts': {'":': 0, '<': 7, '<?xml version=': 0, '>': 15, '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': '828096', 'n_tokens_mistral': 1782, 'n_tokens_neox': 1744, 'n_words': 373}