From 15eee8175e33ce95f596f9bee1c0550696681bf3 Mon Sep 17 00:00:00 2001 From: Gaetan Craig-Riou Date: Fri, 5 May 2023 15:43:04 +1000 Subject: [PATCH 1/7] Add amount to voucher Change value to amount to be more consistent --- app/models/voucher.rb | 9 +++------ .../split_checkout/_voucher_section.cable_ready.haml | 2 +- db/migrate/20230418030646_add_amount_to_vouchers.rb | 5 +++++ db/schema.rb | 1 + spec/models/voucher_spec.rb | 10 ++++++---- spec/system/consumer/split_checkout_spec.rb | 10 +++++++--- spec/system/consumer/split_checkout_tax_incl_spec.rb | 4 ++-- .../consumer/split_checkout_tax_not_incl_spec.rb | 3 +-- 8 files changed, 26 insertions(+), 18 deletions(-) create mode 100644 db/migrate/20230418030646_add_amount_to_vouchers.rb diff --git a/app/models/voucher.rb b/app/models/voucher.rb index 376c1979dd6..2f8fd3c1372 100644 --- a/app/models/voucher.rb +++ b/app/models/voucher.rb @@ -11,13 +11,10 @@ class Voucher < ApplicationRecord dependent: :nullify validates :code, presence: true, uniqueness: { scope: :enterprise_id } - - def value - 10 - end + validates :amount, presence: true, numericality: { greater_than: 0 } def display_value - Spree::Money.new(value) + Spree::Money.new(amount) end # Ideally we would use `include CalculatedAdjustments` to be consistent with other adjustments, @@ -44,6 +41,6 @@ def create_adjustment(label, order) # We limit adjustment to the maximum amount needed to cover the order, ie if the voucher # covers more than the order.total we only need to create an adjustment covering the order.total def compute_amount(order) - -value.clamp(0, order.total) + -amount.clamp(0, order.total) end end diff --git a/app/views/split_checkout/_voucher_section.cable_ready.haml b/app/views/split_checkout/_voucher_section.cable_ready.haml index 786eff72884..a005b5698b9 100644 --- a/app/views/split_checkout/_voucher_section.cable_ready.haml +++ b/app/views/split_checkout/_voucher_section.cable_ready.haml @@ -10,7 +10,7 @@ = t("split_checkout.step2.voucher.voucher", voucher_amount: voucher_adjustment.originator.display_value) = link_to t("split_checkout.step2.voucher.remove_code"), voucher_adjustment_path(id: voucher_adjustment.id), method: "delete", data: { confirm: t("split_checkout.step2.voucher.confirm_delete") } - # This might not be true, ie payment method including a fee which wouldn't be covered by voucher or tax implication raising total to be bigger than the voucher amount ? - - if voucher_adjustment.originator.value > order.total + - if voucher_adjustment.originator.amount > order.total .checkout-input %span.formError.standalone = t("split_checkout.step2.voucher.warning_forfeit_remaining_amount") diff --git a/db/migrate/20230418030646_add_amount_to_vouchers.rb b/db/migrate/20230418030646_add_amount_to_vouchers.rb new file mode 100644 index 00000000000..2542ab26c39 --- /dev/null +++ b/db/migrate/20230418030646_add_amount_to_vouchers.rb @@ -0,0 +1,5 @@ +class AddAmountToVouchers < ActiveRecord::Migration[7.0] + def change + add_column :vouchers, :amount, :decimal, precision: 10, scale: 2, null: false, default: 0 + end +end diff --git a/db/schema.rb b/db/schema.rb index f0935b6e160..7aa41d0e3c5 100644 --- a/db/schema.rb +++ b/db/schema.rb @@ -1197,6 +1197,7 @@ t.datetime "updated_at", null: false t.bigint "enterprise_id" t.datetime "deleted_at", precision: nil + t.decimal "amount", precision: 10, scale: 2, default: "0.0", null: false t.index ["code", "enterprise_id"], name: "index_vouchers_on_code_and_enterprise_id", unique: true t.index ["deleted_at"], name: "index_vouchers_on_deleted_at" t.index ["enterprise_id"], name: "index_vouchers_on_enterprise_id" diff --git a/spec/models/voucher_spec.rb b/spec/models/voucher_spec.rb index 3ef8792b1c7..57dcac881c9 100644 --- a/spec/models/voucher_spec.rb +++ b/spec/models/voucher_spec.rb @@ -15,10 +15,12 @@ it { is_expected.to validate_presence_of(:code) } it { is_expected.to validate_uniqueness_of(:code).scoped_to(:enterprise_id) } + it { is_expected.to validate_presence_of(:amount) } + it { is_expected.to validate_numericality_of(:amount).is_greater_than(0) } end describe '#compute_amount' do - subject { Voucher.create(code: 'new_code', enterprise: enterprise) } + subject { Voucher.create(code: 'new_code', enterprise: enterprise, amount: 10) } let(:order) { create(:order_with_totals) } @@ -39,11 +41,11 @@ describe '#create_adjustment' do subject(:adjustment) { voucher.create_adjustment(voucher.code, order) } - let(:voucher) { Voucher.create(code: 'new_code', enterprise: enterprise) } - let(:order) { create(:order_with_line_items, line_items_count: 1, distributor: enterprise) } + let(:voucher) { Voucher.create(code: 'new_code', enterprise: enterprise, amount: 25) } + let(:order) { create(:order_with_line_items, line_items_count: 3, distributor: enterprise) } it 'includes the full voucher amount' do - expect(adjustment.amount.to_f).to eq(-10.0) + expect(adjustment.amount.to_f).to eq(-25.0) end it 'has no included_tax' do diff --git a/spec/system/consumer/split_checkout_spec.rb b/spec/system/consumer/split_checkout_spec.rb index f0e5fb6fc68..87741fcb8ed 100644 --- a/spec/system/consumer/split_checkout_spec.rb +++ b/spec/system/consumer/split_checkout_spec.rb @@ -720,7 +720,8 @@ end context "with voucher available" do - let!(:voucher) { Voucher.create(code: 'some_code', enterprise: distributor) } + let!(:voucher) { Voucher.create(code: 'some_code', enterprise: distributor, amount: amount) } + let(:amount) { 15 } before do visit checkout_step_path(:payment) @@ -738,7 +739,7 @@ end it "adds a voucher to the order" do - expect(page).to have_content("$10.00 Voucher") + expect(page).to have_content("$15.00 Voucher") expect(order.reload.voucher_adjustments.length).to eq(1) end end @@ -1111,13 +1112,15 @@ end describe "vouchers" do - let(:voucher) { Voucher.create(code: 'some_code', enterprise: distributor) } + let(:voucher) { Voucher.create(code: 'some_code', enterprise: distributor, amount: 6) } before do # Add voucher to the order voucher.create_adjustment(voucher.code, order) + # Update order so voucher adjustment is properly taken into account order.update_order! + VoucherAdjustmentsService.calculate(order) visit checkout_step_path(:summary) end @@ -1125,6 +1128,7 @@ it "shows the applied voucher" do within ".summary-right" do expect(page).to have_content "some_code" + expect(page).to have_content "-6" end end end diff --git a/spec/system/consumer/split_checkout_tax_incl_spec.rb b/spec/system/consumer/split_checkout_tax_incl_spec.rb index 5cb07f67bbd..ea51694aae5 100644 --- a/spec/system/consumer/split_checkout_tax_incl_spec.rb +++ b/spec/system/consumer/split_checkout_tax_incl_spec.rb @@ -136,7 +136,7 @@ end context "when using a voucher" do - let!(:voucher) { Voucher.create(code: 'some_code', enterprise: distributor) } + let!(:voucher) { Voucher.create(code: 'some_code', enterprise: distributor, amount: 10) } it "will include a tax included amount on the voucher adjustment" do visit checkout_step_path(:details) @@ -149,7 +149,7 @@ fill_in "Enter voucher code", with: voucher.code click_button("Apply") - # Choose payment ?? + # Choose payment click_on "Next - Order summary" click_on "Complete order" diff --git a/spec/system/consumer/split_checkout_tax_not_incl_spec.rb b/spec/system/consumer/split_checkout_tax_not_incl_spec.rb index 4fa0f0531a8..d99dd604c73 100644 --- a/spec/system/consumer/split_checkout_tax_not_incl_spec.rb +++ b/spec/system/consumer/split_checkout_tax_not_incl_spec.rb @@ -144,7 +144,7 @@ end context "when using a voucher" do - let!(:voucher) { Voucher.create(code: 'some_code', enterprise: distributor) } + let!(:voucher) { Voucher.create(code: 'some_code', enterprise: distributor, amount: 10) } it "will include a tax included amount on the voucher adjustment" do visit checkout_step_path(:details) @@ -156,7 +156,6 @@ fill_in "Enter voucher code", with: voucher.code click_button("Apply") - # Choose payment ?? click_on "Next - Order summary" click_on "Complete order" From b427e420ce9a3a5d143a7ca25e9629c0e7e650da Mon Sep 17 00:00:00 2001 From: Gaetan Craig-Riou Date: Tue, 18 Apr 2023 15:39:37 +1000 Subject: [PATCH 2/7] Update admin pages to allow entering an amount --- app/controllers/admin/vouchers_controller.rb | 4 +- app/views/admin/vouchers/new.html.haml | 2 +- .../admin/vouchers_controller_spec.rb | 52 +++++++++++++++++++ spec/system/admin/vouchers_spec.rb | 8 +-- 4 files changed, 60 insertions(+), 6 deletions(-) create mode 100644 spec/requests/admin/vouchers_controller_spec.rb diff --git a/app/controllers/admin/vouchers_controller.rb b/app/controllers/admin/vouchers_controller.rb index 70bf0c75a18..66908db7ff6 100644 --- a/app/controllers/admin/vouchers_controller.rb +++ b/app/controllers/admin/vouchers_controller.rb @@ -26,11 +26,11 @@ def create private def load_enterprise - @enterprise = Enterprise.find_by permalink: params[:enterprise_id] + @enterprise = Enterprise.find_by(permalink: params[:enterprise_id]) end def permitted_resource_params - params.require(:voucher).permit(:code) + params.require(:voucher).permit(:code, :amount) end end end diff --git a/app/views/admin/vouchers/new.html.haml b/app/views/admin/vouchers/new.html.haml index 806d49856bf..ec6d88ee052 100644 --- a/app/views/admin/vouchers/new.html.haml +++ b/app/views/admin/vouchers/new.html.haml @@ -20,4 +20,4 @@ = f.label :amount, t('.voucher_amount') .omega.eight.columns = Spree::Money.currency_symbol - = f.text_field :amount, value: @voucher.value, disabled: true + = f.text_field :amount, value: @voucher.amount diff --git a/spec/requests/admin/vouchers_controller_spec.rb b/spec/requests/admin/vouchers_controller_spec.rb new file mode 100644 index 00000000000..e794a42af1e --- /dev/null +++ b/spec/requests/admin/vouchers_controller_spec.rb @@ -0,0 +1,52 @@ +# frozen_string_literal: true + +require "spec_helper" + +describe Admin::VouchersController, type: :request do + let(:enterprise) { create(:supplier_enterprise, name: "Feedme") } + let(:enterprise_user) { create(:user, enterprise_limit: 1) } + + before do + Flipper.enable(:vouchers) + + enterprise_user.enterprise_roles.build(enterprise: enterprise).save + sign_in enterprise_user + end + + describe "GET /admin/enterprises/:enterprise_id/vouchers/new" do + it "loads the new voucher page" do + get new_admin_enterprise_voucher_path(enterprise) + + expect(response).to render_template("admin/vouchers/new") + end + end + + describe "POST /admin/enterprises/:enterprise_id/vouchers" do + subject(:create_voucher) { post admin_enterprise_vouchers_path(enterprise), params: params } + + let(:params) do + { + voucher: { + code: code, + amount: amount + } + } + end + let(:code) { "new_code" } + let(:amount) { 15 } + + it "creates a new voucher" do + expect { create_voucher }.to change(Voucher, :count).by(1) + + voucher = Voucher.last + expect(voucher.code).to eq(code) + expect(voucher.amount).to eq(amount) + end + + it "redirects to admin enterprise setting page, voucher panel" do + create_voucher + + expect(response).to redirect_to("#{edit_admin_enterprise_path(enterprise)}#vouchers_panel") + end + end +end diff --git a/spec/system/admin/vouchers_spec.rb b/spec/system/admin/vouchers_spec.rb index 7b18eec578c..fb85ba50728 100644 --- a/spec/system/admin/vouchers_spec.rb +++ b/spec/system/admin/vouchers_spec.rb @@ -11,6 +11,7 @@ let(:enterprise) { create(:supplier_enterprise, name: 'Feedme') } let(:voucher_code) { 'awesomevoucher' } + let(:amount) { 25 } let(:enterprise_user) { create(:user, enterprise_limit: 1) } before do @@ -22,7 +23,7 @@ it 'lists enterprise vouchers' do # Given an enterprise with vouchers - Voucher.create!(enterprise: enterprise, code: voucher_code) + Voucher.create!(enterprise: enterprise, code: voucher_code, amount: amount) # When I go to the enterprise voucher tab visit edit_admin_enterprise_path(enterprise) @@ -31,7 +32,7 @@ # Then I see a list of vouchers expect(page).to have_content voucher_code - expect(page).to have_content "10" + expect(page).to have_content amount end it 'creates a voucher' do @@ -46,12 +47,13 @@ # And I fill in the fields for a new voucher click save fill_in 'voucher_code', with: voucher_code + fill_in 'voucher_amount', with: amount click_button 'Save' # Then I should get redirect to the entreprise voucher tab and see the created voucher expect(page).to have_selector '.success', text: 'Voucher has been successfully created!' expect(page).to have_content voucher_code - expect(page).to have_content "10" + expect(page).to have_content amount voucher = Voucher.where(enterprise: enterprise, code: voucher_code).first From 2d8fa2486208d65046f8df3982c0478d5b665df6 Mon Sep 17 00:00:00 2001 From: Gaetan Craig-Riou Date: Mon, 1 May 2023 13:32:33 +1000 Subject: [PATCH 3/7] Add a voucher factory As vouchers are getting more complicated, it makes sense to use a factory to simplify writing test. --- spec/controllers/split_checkout_controller_spec.rb | 2 +- spec/factories/voucher_factory.rb | 8 ++++++++ spec/models/spree/order_spec.rb | 2 +- spec/models/voucher_spec.rb | 6 +++--- spec/requests/voucher_adjustments_spec.rb | 2 +- spec/services/voucher_adjustments_service_spec.rb | 2 +- spec/system/admin/vouchers_spec.rb | 2 +- spec/system/consumer/split_checkout_spec.rb | 7 ++++--- spec/system/consumer/split_checkout_tax_incl_spec.rb | 4 +++- spec/system/consumer/split_checkout_tax_not_incl_spec.rb | 4 +++- 10 files changed, 26 insertions(+), 13 deletions(-) create mode 100644 spec/factories/voucher_factory.rb diff --git a/spec/controllers/split_checkout_controller_spec.rb b/spec/controllers/split_checkout_controller_spec.rb index 774be4ca763..c57710a2bbe 100644 --- a/spec/controllers/split_checkout_controller_spec.rb +++ b/spec/controllers/split_checkout_controller_spec.rb @@ -237,7 +237,7 @@ end describe "Vouchers" do - let(:voucher) { Voucher.create(code: 'some_code', enterprise: distributor) } + let(:voucher) { create(:voucher, code: 'some_code', enterprise: distributor) } describe "adding a voucher" do let(:checkout_params) do diff --git a/spec/factories/voucher_factory.rb b/spec/factories/voucher_factory.rb new file mode 100644 index 00000000000..864e8d53317 --- /dev/null +++ b/spec/factories/voucher_factory.rb @@ -0,0 +1,8 @@ +# frozen_string_literal: true + +FactoryBot.define do + factory :voucher do + enterprise { create(:distributor_enterprise) } + amount { rand(200.0) } + end +end diff --git a/spec/models/spree/order_spec.rb b/spec/models/spree/order_spec.rb index 5b3d0ef71a8..2ed17bb0838 100644 --- a/spec/models/spree/order_spec.rb +++ b/spec/models/spree/order_spec.rb @@ -1432,7 +1432,7 @@ def advance_to_delivery_state(order) end describe "#voucher_adjustments" do - let(:voucher) { Voucher.create(code: 'new_code', enterprise: order.distributor) } + let(:voucher) { create(:voucher, code: 'new_code', enterprise: order.distributor) } context "when no voucher adjustment" do it 'returns an empty array' do diff --git a/spec/models/voucher_spec.rb b/spec/models/voucher_spec.rb index 57dcac881c9..d600913964b 100644 --- a/spec/models/voucher_spec.rb +++ b/spec/models/voucher_spec.rb @@ -11,7 +11,7 @@ end describe 'validations' do - subject { Voucher.new(code: 'new_code', enterprise: enterprise) } + subject { build(:voucher, code: 'new_code', enterprise: enterprise) } it { is_expected.to validate_presence_of(:code) } it { is_expected.to validate_uniqueness_of(:code).scoped_to(:enterprise_id) } @@ -20,7 +20,7 @@ end describe '#compute_amount' do - subject { Voucher.create(code: 'new_code', enterprise: enterprise, amount: 10) } + subject { create(:voucher, code: 'new_code', enterprise: enterprise, amount: 10) } let(:order) { create(:order_with_totals) } @@ -41,7 +41,7 @@ describe '#create_adjustment' do subject(:adjustment) { voucher.create_adjustment(voucher.code, order) } - let(:voucher) { Voucher.create(code: 'new_code', enterprise: enterprise, amount: 25) } + let(:voucher) { create(:voucher, code: 'new_code', enterprise: enterprise, amount: 25) } let(:order) { create(:order_with_line_items, line_items_count: 3, distributor: enterprise) } it 'includes the full voucher amount' do diff --git a/spec/requests/voucher_adjustments_spec.rb b/spec/requests/voucher_adjustments_spec.rb index 056526ee67d..e663c1a6a9d 100644 --- a/spec/requests/voucher_adjustments_spec.rb +++ b/spec/requests/voucher_adjustments_spec.rb @@ -6,7 +6,7 @@ let(:user) { order.user } let(:distributor) { create(:distributor_enterprise, with_payment_and_shipping: true) } let(:order) { create( :order_with_line_items, line_items_count: 1, distributor: distributor) } - let(:voucher) { Voucher.create(code: 'some_code', enterprise: distributor) } + let(:voucher) { create(:voucher, code: 'some_code', enterprise: distributor) } let!(:adjustment) { voucher.create_adjustment(voucher.code, order) } before do diff --git a/spec/services/voucher_adjustments_service_spec.rb b/spec/services/voucher_adjustments_service_spec.rb index d2b95cdb78b..0fc08d7260d 100644 --- a/spec/services/voucher_adjustments_service_spec.rb +++ b/spec/services/voucher_adjustments_service_spec.rb @@ -5,7 +5,7 @@ describe VoucherAdjustmentsService do describe '.calculate' do let(:enterprise) { build(:enterprise) } - let(:voucher) { Voucher.create(code: 'new_code', enterprise: enterprise) } + let(:voucher) { create(:voucher, code: 'new_code', enterprise: enterprise, amount: 10) } context 'when voucher covers the order total' do subject { order.voucher_adjustments.first } diff --git a/spec/system/admin/vouchers_spec.rb b/spec/system/admin/vouchers_spec.rb index fb85ba50728..08acad4ccbf 100644 --- a/spec/system/admin/vouchers_spec.rb +++ b/spec/system/admin/vouchers_spec.rb @@ -23,7 +23,7 @@ it 'lists enterprise vouchers' do # Given an enterprise with vouchers - Voucher.create!(enterprise: enterprise, code: voucher_code, amount: amount) + create(:voucher, enterprise: enterprise, code: voucher_code, amount: amount) # When I go to the enterprise voucher tab visit edit_admin_enterprise_path(enterprise) diff --git a/spec/system/consumer/split_checkout_spec.rb b/spec/system/consumer/split_checkout_spec.rb index 87741fcb8ed..3b379b8aa58 100644 --- a/spec/system/consumer/split_checkout_spec.rb +++ b/spec/system/consumer/split_checkout_spec.rb @@ -720,8 +720,9 @@ end context "with voucher available" do - let!(:voucher) { Voucher.create(code: 'some_code', enterprise: distributor, amount: amount) } - let(:amount) { 15 } + let!(:voucher) do + create(:voucher, code: 'some_code', enterprise: distributor, amount: 15) + end before do visit checkout_step_path(:payment) @@ -1112,7 +1113,7 @@ end describe "vouchers" do - let(:voucher) { Voucher.create(code: 'some_code', enterprise: distributor, amount: 6) } + let(:voucher) { create(:voucher, code: 'some_code', enterprise: distributor, amount: 6) } before do # Add voucher to the order diff --git a/spec/system/consumer/split_checkout_tax_incl_spec.rb b/spec/system/consumer/split_checkout_tax_incl_spec.rb index ea51694aae5..b4935aca7c6 100644 --- a/spec/system/consumer/split_checkout_tax_incl_spec.rb +++ b/spec/system/consumer/split_checkout_tax_incl_spec.rb @@ -136,7 +136,9 @@ end context "when using a voucher" do - let!(:voucher) { Voucher.create(code: 'some_code', enterprise: distributor, amount: 10) } + let!(:voucher) do + create(:voucher, code: 'some_code', enterprise: distributor, amount: 10) + end it "will include a tax included amount on the voucher adjustment" do visit checkout_step_path(:details) diff --git a/spec/system/consumer/split_checkout_tax_not_incl_spec.rb b/spec/system/consumer/split_checkout_tax_not_incl_spec.rb index d99dd604c73..2b734adfe42 100644 --- a/spec/system/consumer/split_checkout_tax_not_incl_spec.rb +++ b/spec/system/consumer/split_checkout_tax_not_incl_spec.rb @@ -144,7 +144,9 @@ end context "when using a voucher" do - let!(:voucher) { Voucher.create(code: 'some_code', enterprise: distributor, amount: 10) } + let!(:voucher) do + create(:voucher, code: 'some_code', enterprise: distributor, amount: 10) + end it "will include a tax included amount on the voucher adjustment" do visit checkout_step_path(:details) From b585f77be4ed53a161c8511dcb793da2a3bf9e54 Mon Sep 17 00:00:00 2001 From: Gaetan Craig-Riou Date: Fri, 5 May 2023 16:14:21 +1000 Subject: [PATCH 4/7] Make enterprise mandatory for a voucher It should have already been the case... --- app/models/voucher.rb | 2 +- spec/models/spree/order_spec.rb | 3 ++- spec/models/voucher_spec.rb | 2 +- 3 files changed, 4 insertions(+), 3 deletions(-) diff --git a/app/models/voucher.rb b/app/models/voucher.rb index 2f8fd3c1372..94743fc7316 100644 --- a/app/models/voucher.rb +++ b/app/models/voucher.rb @@ -3,7 +3,7 @@ class Voucher < ApplicationRecord acts_as_paranoid - belongs_to :enterprise + belongs_to :enterprise, optional: false has_many :adjustments, as: :originator, diff --git a/spec/models/spree/order_spec.rb b/spec/models/spree/order_spec.rb index 2ed17bb0838..5c250f970e8 100644 --- a/spec/models/spree/order_spec.rb +++ b/spec/models/spree/order_spec.rb @@ -1432,6 +1432,8 @@ def advance_to_delivery_state(order) end describe "#voucher_adjustments" do + let(:distributor) { create(:distributor_enterprise) } + let(:order) { create(:order, user: user, distributor: distributor) } let(:voucher) { create(:voucher, code: 'new_code', enterprise: order.distributor) } context "when no voucher adjustment" do @@ -1441,7 +1443,6 @@ def advance_to_delivery_state(order) end it "returns an array of voucher adjusment" do - order.save! expected_adjustments = Array.new(2) { voucher.create_adjustment(voucher.code, order) } expect(order.voucher_adjustments).to eq(expected_adjustments) diff --git a/spec/models/voucher_spec.rb b/spec/models/voucher_spec.rb index d600913964b..c5bbcdabe54 100644 --- a/spec/models/voucher_spec.rb +++ b/spec/models/voucher_spec.rb @@ -6,7 +6,7 @@ let(:enterprise) { build(:enterprise) } describe 'associations' do - it { is_expected.to belong_to(:enterprise) } + it { is_expected.to belong_to(:enterprise).required } it { is_expected.to have_many(:adjustments) } end From 355af9e818e4f0157a9b35c1e0a74d19f95b244a Mon Sep 17 00:00:00 2001 From: Gaetan Craig-Riou Date: Fri, 5 May 2023 16:16:01 +1000 Subject: [PATCH 5/7] Per review comment, build enterprise instead of create Speed up testing by removing unnecessarily created record --- spec/factories/voucher_factory.rb | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/spec/factories/voucher_factory.rb b/spec/factories/voucher_factory.rb index 864e8d53317..66ea8bfc0e0 100644 --- a/spec/factories/voucher_factory.rb +++ b/spec/factories/voucher_factory.rb @@ -2,7 +2,7 @@ FactoryBot.define do factory :voucher do - enterprise { create(:distributor_enterprise) } + enterprise { build(:distributor_enterprise) } amount { rand(200.0) } end end From abf2105df7a3bdda2ade26fb75117bda218d9658 Mon Sep 17 00:00:00 2001 From: Gaetan Craig-Riou Date: Fri, 5 May 2023 16:21:21 +1000 Subject: [PATCH 6/7] Admin::VoucherController check enterprise permission --- app/controllers/admin/vouchers_controller.rb | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/app/controllers/admin/vouchers_controller.rb b/app/controllers/admin/vouchers_controller.rb index 66908db7ff6..efd9ac9b7d8 100644 --- a/app/controllers/admin/vouchers_controller.rb +++ b/app/controllers/admin/vouchers_controller.rb @@ -26,7 +26,10 @@ def create private def load_enterprise - @enterprise = Enterprise.find_by(permalink: params[:enterprise_id]) + @enterprise = OpenFoodNetwork::Permissions + .new(spree_current_user) + .editable_enterprises + .find_by(permalink: params[:enterprise_id]) end def permitted_resource_params From 9d7316c553f29a5f05643e0e955e84b97fb1df1f Mon Sep 17 00:00:00 2001 From: David Cook Date: Fri, 19 May 2023 17:19:54 +1000 Subject: [PATCH 7/7] Use a fixed value for voucher factory --- spec/factories/voucher_factory.rb | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/spec/factories/voucher_factory.rb b/spec/factories/voucher_factory.rb index 66ea8bfc0e0..c02de3fb52d 100644 --- a/spec/factories/voucher_factory.rb +++ b/spec/factories/voucher_factory.rb @@ -3,6 +3,6 @@ FactoryBot.define do factory :voucher do enterprise { build(:distributor_enterprise) } - amount { rand(200.0) } + amount { 15 } end end