Fix spec to work with the internal payment method

This commit is contained in:
Gaetan Craig-Riou
2026-02-24 13:25:38 +11:00
parent d95aac333b
commit 628810eb33
11 changed files with 53 additions and 20 deletions

View File

@@ -162,9 +162,13 @@ module Admin
end
def destroy
if @object.destroy
@object.transaction do
# Destroy linked payment methods so we remove the default customer credit payment methods
@object.distributor_payment_methods.destroy_all
@object.destroy!
flash.now[:success] = flash_message_for(@object, :successfully_removed)
else
rescue StandardError
Rails.logger.error @object.errors.full_messages.to_sentence
flash.now[:error] = @object.errors.full_messages.to_sentence
end
@@ -176,7 +180,7 @@ module Admin
protected
def delete_custom_tab
@object.custom_tab.destroy if @object.custom_tab.present?
@object.custom_tab.presence&.destroy
enterprise_params.delete(:custom_tab_attributes)
end
@@ -239,9 +243,7 @@ module Admin
enterprises = OpenFoodNetwork::OrderCyclePermissions.new(spree_current_user, @order_cycle)
.visible_enterprises
if enterprises.present?
enterprises.includes(supplied_products: [:variants, :image])
end
enterprises.presence&.includes(supplied_products: [:variants, :image])
when :index
if spree_current_user.admin?
OpenFoodNetwork::Permissions.new(spree_current_user).

View File

@@ -9,6 +9,9 @@ namespace :ofn do
desc 'remove the specified enterprise'
task :remove_enterprise, [:enterprise_id] => :environment do |_task, args|
enterprise = Enterprise.find(args.enterprise_id)
# Enterprise have two internal credit payment methods by default, we destroy the link to allow
# removing the enterprise
enterprise.distributor_payment_methods.destroy_all
enterprise.destroy
end

View File

@@ -31,6 +31,8 @@ RSpec.describe Enterprise do
u.enterprise_roles.build(enterprise: e).save!
role = e.enterprise_roles.first
# Delete the default customer credit payment method link
e.distributor_payment_methods.destroy_all
e.destroy
expect(EnterpriseRole.where(id: role.id)).to be_empty
end
@@ -51,6 +53,8 @@ RSpec.describe Enterprise do
er1 = create(:enterprise_relationship, parent: e, child: e_other)
er2 = create(:enterprise_relationship, child: e, parent: e_other)
# Delete the default customer credit payment method link
e.distributor_payment_methods.destroy_all
e.destroy
expect(EnterpriseRelationship.where(id: [er1, er2])).to be_empty
@@ -85,6 +89,8 @@ RSpec.describe Enterprise do
create_list(:distributor_shipping_method, 2, distributor: enterprise)
expect do
# Delete the default customer credit payment method link
enterprise.distributor_payment_methods.destroy_all
enterprise.destroy
expect(enterprise.errors.full_messages).to eq(
["Cannot delete record because dependent distributor shipping methods exist"]
@@ -111,6 +117,8 @@ RSpec.describe Enterprise do
end
expect do
# Delete the default customer credit payment method link
enterprise.distributor_payment_methods.destroy_all
enterprise.destroy
expect(enterprise.errors.full_messages).to eq(
["Cannot delete record because dependent vouchers exist"]

View File

@@ -6,7 +6,7 @@ RSpec.describe "CustomerAccountTransactions", swagger_doc: "v1.yaml", feature: :
let!(:enterprise) { create(:enterprise) }
# Paymnent method will be created when the enterprise is created
let(:payment_method) {
Spree::PaymentMethod.find_by(name: Rails.application.config.api_payment_method[:name])
Spree::PaymentMethod.internal.find_by(name: Rails.application.config.api_payment_method[:name])
}
let(:customer) { create(:customer) }

View File

@@ -223,8 +223,10 @@ RSpec.describe OrderCycles::FormService do
let!(:distributor){ create(:distributor_enterprise) }
let!(:payment_method){ create(:payment_method, distributors: [distributor]) }
let!(:payment_method2){ create(:payment_method, distributors: [distributor]) }
let!(:distributor_payment_method){ distributor.distributor_payment_methods.first.id }
let!(:distributor_payment_method2){ distributor.distributor_payment_methods.second.id }
let!(:distributor_payment_method){
distributor.distributor_payment_methods.second_to_last.id
}
let!(:distributor_payment_method2){ distributor.distributor_payment_methods.last.id }
let!(:supplier){ create(:supplier_enterprise) }
context "the submitter is a coordinator" do

View File

@@ -82,14 +82,14 @@ RSpec.describe Orders::AvailablePaymentMethodsService do
order_cycle = create(:distributor_order_cycle,
distributors: [distributor_i, distributor_ii])
order_cycle.selected_distributor_payment_methods << [
distributor_i.distributor_payment_methods.first,
distributor_ii.distributor_payment_methods.first,
distributor_i.distributor_payment_methods.last,
distributor_ii.distributor_payment_methods.last
]
order = build(:order, distributor: distributor_i, order_cycle:)
available_payment_methods = Orders::AvailablePaymentMethodsService.new(order).to_a
expect(available_payment_methods).to eq [payment_method_i]
expect(available_payment_methods).to eq [payment_method_ii]
end
end
end
@@ -242,14 +242,14 @@ RSpec.describe Orders::AvailablePaymentMethodsService do
d1.payment_methods << payment_method2
d2.payment_methods << payment_method
d2.payment_methods << payment_method2
oc.selected_distributor_payment_methods << d1.distributor_payment_methods.first
oc.selected_distributor_payment_methods << d1.distributor_payment_methods.second
oc.selected_distributor_payment_methods << d2.distributor_payment_methods.first
oc.selected_distributor_payment_methods << d1.distributor_payment_methods.second_to_last
oc.selected_distributor_payment_methods << d1.distributor_payment_methods.last
oc.selected_distributor_payment_methods << d2.distributor_payment_methods.last
}
it do
order = build(:order, distributor: d2, order_cycle: oc)
order_available_payment_methods = Orders::AvailablePaymentMethodsService.new(order).to_a
expect(order_available_payment_methods).to eq([d2.payment_methods.first])
expect(order_available_payment_methods).to eq([d2.payment_methods.last])
end
end
end

View File

@@ -103,11 +103,14 @@ RSpec.describe Orders::CustomerCreditService do
context "when credit payment method is missing" do
before do
# Add credit
payment_method = order.customer.enterprise.payment_methods.internal.find_by(
name: Rails.application.config.api_payment_method[:name]
)
create(
:customer_account_transaction,
amount: 5.00,
customer: order.customer,
payment_method: credit_payment_method
payment_method:
)
credit_payment_method.destroy!
end

View File

@@ -32,6 +32,10 @@ RSpec.describe Sets::ModelSet do
it "destroys deleted models" do
e1 = create(:enterprise)
e2 = create(:enterprise)
# Enterprise have 2 internal credit payment method, we need to destroy the link for
# the enterprise to be deletable
e1.distributor_payment_methods.destroy_all
e2.distributor_payment_methods.destroy_all
attributes = { collection_attributes: { '1' => { id: e1.id, name: 'deleteme' },
'2' => { id: e2.id, name: 'e2' } } }

View File

@@ -248,6 +248,7 @@ RSpec.describe '
expect(page).to have_selector 'td.shops', text: distributor_unmanaged.name
end
# TODO
it "creating a new order cycle" do
distributor_managed.update_attribute(:enable_subscriptions, true)
visit admin_order_cycles_path
@@ -923,9 +924,13 @@ RSpec.describe '
private
def expect_payment_methods_to_be_checked_for(distributor)
distributor.distributor_payment_method_ids.each do |distributor_payment_method_id|
# Exclude internal payment method
distributor_payment_methods = distributor.distributor_payment_methods.reject { |dpm|
dpm.payment_method.nil?
}
distributor_payment_methods.each do |distributor_payment_method|
expect(page).to have_checked_field(
"order_cycle_selected_distributor_payment_method_ids_#{distributor_payment_method_id}"
"order_cycle_selected_distributor_payment_method_ids_#{distributor_payment_method.id}"
)
end
end

View File

@@ -113,7 +113,12 @@ RSpec.describe '
end
it "checking a single distributor is checked by default" do
2.times.each { Enterprise.last.destroy }
2.times.each do
enterprise = Enterprise.last
# Delete the default customer credit payment method
enterprise.distributor_payment_methods.destroy_all
enterprise.destroy!
end
login_as_admin
visit spree.new_admin_payment_method_path
expect(page).to have_field "payment_method_distributor_ids_#{@distributors[0].id}",

View File

@@ -388,6 +388,7 @@ RSpec.describe "As a consumer, I want to checkout my order" do
let(:payment_amount) { 10.00 }
before do
create(:payment_method, distributors: [distributor])
create(
:customer_account_transaction,
amount: 100,