mirror of
https://github.com/openfoodfoundation/openfoodnetwork
synced 2026-01-27 21:06:49 +00:00
When using a voucher with tax not incluced in price, we need to delete both the voucher adjustment and the tax adjustment
122 lines
3.5 KiB
Ruby
122 lines
3.5 KiB
Ruby
# frozen_string_literal: true
|
|
|
|
require 'spec_helper'
|
|
|
|
describe VoucherAdjustmentsController, type: :request do
|
|
let(:user) { order.user }
|
|
let(:address) { create(:address) }
|
|
let(:distributor) { create(:distributor_enterprise, with_payment_and_shipping: true) }
|
|
let(:order_cycle) { create(:order_cycle, distributors: [distributor]) }
|
|
let(:exchange) { order_cycle.exchanges.outgoing.first }
|
|
let(:order) do
|
|
create(
|
|
:order_with_line_items,
|
|
line_items_count: 1,
|
|
distributor: distributor,
|
|
order_cycle: order_cycle,
|
|
bill_address: address,
|
|
ship_address: address
|
|
)
|
|
end
|
|
let(:shipping_method) { distributor.shipping_methods.first }
|
|
let(:voucher) { create(:voucher, code: 'some_code', enterprise: distributor) }
|
|
|
|
before do
|
|
order.update!(created_by: user)
|
|
|
|
order.select_shipping_method shipping_method.id
|
|
OrderWorkflow.new(order).advance_to_payment
|
|
|
|
sign_in user
|
|
end
|
|
|
|
describe "POST voucher_adjustments" do
|
|
let(:params) { { order: { voucher_code: voucher.code } } }
|
|
|
|
it "adds a voucher to the user's current order" do
|
|
post "/voucher_adjustments", params: params
|
|
|
|
expect(response).to be_successful
|
|
expect(order.reload.voucher_adjustments.length).to eq(1)
|
|
end
|
|
|
|
context "when voucher doesn't exist" do
|
|
let(:params) { { order: { voucher_code: "non_voucher" } } }
|
|
|
|
it "returns 422 and an error message" do
|
|
post "/voucher_adjustments", params: params
|
|
|
|
expect(response).to be_unprocessable
|
|
expect(flash[:error]).to match "Voucher code Not found"
|
|
end
|
|
end
|
|
|
|
context "when adding fails" do
|
|
it "returns 422 and an error message" do
|
|
# Create a non valid adjustment
|
|
bad_adjustment = build(:adjustment, label: nil)
|
|
allow(voucher).to receive(:create_adjustment).and_return(bad_adjustment)
|
|
allow(Voucher).to receive(:find_by).and_return(voucher)
|
|
|
|
post "/voucher_adjustments", params: params
|
|
|
|
expect(response).to be_unprocessable
|
|
expect(flash[:error]).to match(
|
|
"There was an error while adding the voucher and Label can't be blank"
|
|
)
|
|
end
|
|
end
|
|
end
|
|
|
|
describe "DELETE voucher_adjustments/:id" do
|
|
let!(:adjustment) { voucher.create_adjustment(voucher.code, order) }
|
|
|
|
it "deletes the voucher adjustment" do
|
|
delete "/voucher_adjustments/#{adjustment.id}"
|
|
|
|
expect(order.voucher_adjustments.reload.length).to eq(0)
|
|
end
|
|
|
|
it "render a success response" do
|
|
delete "/voucher_adjustments/#{adjustment.id}"
|
|
|
|
expect(response).to be_successful
|
|
end
|
|
|
|
context "when adjustment doesn't exits" do
|
|
it "does nothing" do
|
|
delete "/voucher_adjustments/-1"
|
|
|
|
expect(order.voucher_adjustments.reload.length).to eq(1)
|
|
end
|
|
|
|
it "render a success response" do
|
|
delete "/voucher_adjustments/-1"
|
|
|
|
expect(response).to be_successful
|
|
end
|
|
end
|
|
|
|
context "when tax excluded from price" do
|
|
it "deletes all voucher adjustment" do
|
|
# Add a tax adjustment
|
|
adjustment_attributes = {
|
|
amount: 2.00,
|
|
originator: adjustment.originator,
|
|
order: order,
|
|
label: "Tax #{adjustment.label}",
|
|
mandatory: false,
|
|
state: 'closed',
|
|
tax_category: nil,
|
|
included_tax: 0
|
|
}
|
|
order.adjustments.create(adjustment_attributes)
|
|
|
|
delete "/voucher_adjustments/#{adjustment.id}"
|
|
|
|
expect(order.voucher_adjustments.reload.length).to eq(0)
|
|
end
|
|
end
|
|
end
|
|
end
|