Files
openfoodnetwork/spec/models/enterprise_fee_spec.rb
Matt-Yorkley f5bc120fb1 Remove without_protection: true argument from #create calls
Needed for using Strong Parameters in Rails 4
2020-02-22 14:35:10 +01:00

141 lines
5.7 KiB
Ruby

require 'spec_helper'
describe EnterpriseFee do
describe "associations" do
it { is_expected.to belong_to(:enterprise) }
end
describe "validations" do
it { is_expected.to validate_presence_of(:name) }
end
describe "callbacks" do
let(:ef) { create(:enterprise_fee) }
it "removes itself from order cycle coordinator fees when destroyed" do
oc = create(:simple_order_cycle, coordinator_fees: [ef])
ef.destroy
expect(oc.reload.coordinator_fee_ids).to be_empty
end
it "removes itself from order cycle exchange fees when destroyed" do
oc = create(:simple_order_cycle)
ex = create(:exchange, order_cycle: oc, enterprise_fees: [ef])
ef.destroy
expect(ex.reload.exchange_fee_ids).to be_empty
end
describe "for tax_category" do
let(:tax_category) { create(:tax_category) }
let(:enterprise_fee) { create(:enterprise_fee, tax_category_id: nil, inherits_tax_category: true) }
it "maintains valid tax_category settings" do
# Changing just tax_category, when inheriting
# tax_category is changed, inherits.. set to false
enterprise_fee.assign_attributes(tax_category_id: tax_category.id)
enterprise_fee.save!
expect(enterprise_fee.tax_category).to eq tax_category
expect(enterprise_fee.inherits_tax_category).to be false
# Changing inherits_tax_category, when tax_category is set
# tax_category is dropped, inherits.. set to true
enterprise_fee.assign_attributes(inherits_tax_category: true)
enterprise_fee.save!
expect(enterprise_fee.tax_category).to be nil
expect(enterprise_fee.inherits_tax_category).to be true
# Changing both tax_category and inherits_tax_category
# tax_category is changed, but inherits.. changes are dropped
enterprise_fee.assign_attributes(tax_category_id: tax_category.id)
enterprise_fee.assign_attributes(inherits_tax_category: true)
enterprise_fee.save!
expect(enterprise_fee.tax_category).to eq tax_category
expect(enterprise_fee.inherits_tax_category).to be false
end
end
end
describe "scopes" do
describe "finding per-item enterprise fees" do
it "does not return fees with FlatRate, FlexiRate and PriceSack calculators" do
create(:enterprise_fee, calculator: Spree::Calculator::FlatRate.new)
create(:enterprise_fee, calculator: Spree::Calculator::FlexiRate.new)
create(:enterprise_fee, calculator: Spree::Calculator::PriceSack.new)
expect(EnterpriseFee.per_item).to be_empty
end
it "returns fees with any other calculator" do
ef1 = create(:enterprise_fee, calculator: Spree::Calculator::DefaultTax.new)
ef2 = create(:enterprise_fee, calculator: Calculator::FlatPercentPerItem.new)
ef3 = create(:enterprise_fee, calculator: Spree::Calculator::PerItem.new)
expect(EnterpriseFee.per_item).to match_array [ef1, ef2, ef3]
end
end
describe "finding per-order enterprise fees" do
it "returns fees with FlatRate, FlexiRate and PriceSack calculators" do
ef1 = create(:enterprise_fee, calculator: Spree::Calculator::FlatRate.new)
ef2 = create(:enterprise_fee, calculator: Spree::Calculator::FlexiRate.new)
ef3 = create(:enterprise_fee, calculator: Spree::Calculator::PriceSack.new)
expect(EnterpriseFee.per_order).to match_array [ef1, ef2, ef3]
end
it "does not return fees with any other calculator" do
ef1 = create(:enterprise_fee, calculator: Spree::Calculator::DefaultTax.new)
ef2 = create(:enterprise_fee, calculator: Calculator::FlatPercentPerItem.new)
ef3 = create(:enterprise_fee, calculator: Spree::Calculator::PerItem.new)
expect(EnterpriseFee.per_order).to be_empty
end
end
end
describe "clearing all enterprise fee adjustments on an order" do
it "clears adjustments from many fees and on all line items" do
order_cycle = create(:order_cycle)
order = create(:order, order_cycle: order_cycle)
line_item1 = create(:line_item, order: order, variant: order_cycle.variants.first)
line_item2 = create(:line_item, order: order, variant: order_cycle.variants.second)
order_cycle.coordinator_fees[0].create_adjustment('foo1', line_item1.order, line_item1, true)
order_cycle.coordinator_fees[0].create_adjustment('foo2', line_item2.order, line_item2, true)
order_cycle.exchanges[0].enterprise_fees[0].create_adjustment('foo3', line_item1.order, line_item1, true)
order_cycle.exchanges[0].enterprise_fees[0].create_adjustment('foo4', line_item2.order, line_item2, true)
expect do
EnterpriseFee.clear_all_adjustments_on_order order
end.to change(order.adjustments, :count).by(-4)
end
it "clears adjustments from per-order fees" do
order = create(:order)
enterprise_fee = create(:enterprise_fee)
enterprise_fee_aplicator = OpenFoodNetwork::EnterpriseFeeApplicator.new(enterprise_fee, nil, 'coordinator')
enterprise_fee_aplicator.create_order_adjustment(order)
expect do
EnterpriseFee.clear_all_adjustments_on_order order
end.to change(order.adjustments, :count).by(-1)
end
it "does not clear adjustments from another originator" do
order = create(:order)
tax_rate = create(:tax_rate, calculator: build(:calculator))
order.adjustments.create({ amount: 12.34,
source: order,
originator: tax_rate,
state: 'closed',
label: 'hello' })
expect do
EnterpriseFee.clear_all_adjustments_on_order order
end.to change(order.adjustments, :count).by(0)
end
end
end