Clean up spec

This commit is contained in:
Gaetan Craig-Riou
2026-02-10 11:03:31 +11:00
parent cb6b1f2dd0
commit 5bdb6e6d69
9 changed files with 36 additions and 47 deletions

View File

@@ -5,8 +5,6 @@ require "spree/localized_number"
class CustomerAccountTransaction < ApplicationRecord
extend Spree::LocalizedNumber
DEFAULT_PAYMENT_METHOD_NAME = "api_payment_method.name"
localize_number :amount
belongs_to :customer
@@ -43,7 +41,9 @@ class CustomerAccountTransaction < ApplicationRecord
# Creates the first transaction with a 0 amount
def create_initial_transaction
api_payment_method = Spree::PaymentMethod.find_by!(name: DEFAULT_PAYMENT_METHOD_NAME)
api_payment_method = Spree::PaymentMethod.find_by!(
name: Rails.application.config.api_payment_method[:name]
)
CustomerAccountTransaction.create!(
customer: customer,
amount: 0.00,

View File

@@ -13,14 +13,10 @@ module Spree
customer = Customer.find_by(id: options[:customer_id])
return error_response("customer_not_found") if customer.nil?
return error_response("missing_payment") if options[:payment_id].nil?
payment_method = Spree::PaymentMethod.find_by(name: "credit_payment_method.name")
return error_response("credit_payment_method_missing") if payment_method.nil?
available_credit = customer.customer_account_transactions.last&.balance
return error_response("no_credit_available") if available_credit.nil?
return error_response("not_enough_credit_available") if calculated_amount > available_credit
@@ -57,6 +53,12 @@ module Spree
private
def payment_method
Spree::PaymentMethod.find_by(
name: Rails.application.config.credit_payment_method[:name]
)
end
def error_response(translation_key)
message = I18n.t(translation_key, scope: "credit_payment_method.errors")
ActiveMerchant::Billing::Response.new(false, message)

View File

@@ -5,6 +5,8 @@ FactoryBot.define do
customer { build(:customer) }
amount { 10.00 }
currency { "AUD" }
payment_method { build(:payment_method) }
payment_method {
build(:payment_method, name: Rails.application.config.api_payment_method[:name])
}
end
end

View File

@@ -3,10 +3,6 @@
require 'spec_helper'
RSpec.describe CustomerAccountTransaction do
let!(:payment_method) {
create(:payment_method, name: CustomerAccountTransaction::DEFAULT_PAYMENT_METHOD_NAME)
}
describe "validations" do
subject { build(:customer_account_transaction) }
@@ -60,12 +56,21 @@ RSpec.describe CustomerAccountTransaction do
end
context "when the default payment method is missing" do
let!(:payment_method) { nil }
around do |example|
# A Customer account transaction is linked to a customer which is linked to an enterprise.
# That means FactoryBot will create an enterprise, so we disable the after create callback
# so that credit payment are not created.
Enterprise.skip_callback(:create, :after, :add_credit_payment_method)
example.run
Enterprise.set_callback(:create, :after, :add_credit_payment_method)
end
it "raises an error" do
expect { create(:customer_account_transaction, amount: 12.00) }.to raise_error(
ActiveRecord::RecordNotFound
)
expect do
create(
:customer_account_transaction, amount: 12.00, payment_method: create(:payment_method)
)
end.to raise_error(ActiveRecord::RecordNotFound)
end
end
end

View File

@@ -8,9 +8,6 @@ RSpec.describe Spree::PaymentMethod::CustomerCredit do
describe "#purchase" do
let(:response) { subject.purchase(amount, nil, options) }
let!(:payment_method) {
create(:payment_method, name: CustomerAccountTransaction::DEFAULT_PAYMENT_METHOD_NAME)
}
let!(:credit_payment_method) {
create(:customer_credit_payment_method)
}
@@ -95,6 +92,15 @@ RSpec.describe Spree::PaymentMethod::CustomerCredit do
context "when credit payment method is not configured" do
let!(:credit_payment_method) { nil }
around do |example|
# Customer is needed to create a purchase and a customer which is linked to an enterprise.
# That means FactoryBot will create an enterprise, so we disable the after create callback
# so that credit payment methods are not created.
Enterprise.skip_callback(:create, :after, :add_credit_payment_method)
example.run
Enterprise.set_callback(:create, :after, :add_credit_payment_method)
end
it "returns an error" do
expect(response.success?).to be(false)
expect(response.message).to eq("Credit payment method is missing")

View File

@@ -212,15 +212,6 @@ RSpec.describe CustomersWithBalanceQuery do
end
context "with customer payments" do
# TODO should not be needed, need to add this to seed somehow
let!(:payment_method) {
create(
:payment_method,
name: CustomerAccountTransaction::DEFAULT_PAYMENT_METHOD_NAME,
distributors: [customer.enterprise]
)
}
it 'returns the customer available credit' do
create(:customer_account_transaction, customer:, amount: 10.00)
create(:customer_account_transaction, customer:, amount: -2.00)

View File

@@ -7,13 +7,6 @@ RSpec.describe Admin::CustomerAccountTransactionController do
let(:enterprise_user) { create(:user, enterprises: [enterprise]) }
let(:enterprise) { create(:enterprise) }
let(:customer) { create(:customer, enterprise:) }
let!(:payment_method) {
create(
:payment_method,
name: CustomerAccountTransaction::DEFAULT_PAYMENT_METHOD_NAME,
distributors: [enterprise]
)
}
before do
login_as enterprise_user

View File

@@ -4,12 +4,9 @@ require "swagger_helper"
RSpec.describe "CustomerAccountTransactions", swagger_doc: "v1.yaml", feature: :api_v1 do
let!(:enterprise) { create(:enterprise) }
# Paymnent method will be created when the enterprise is created
let(:payment_method) {
create(
:payment_method,
name: CustomerAccountTransaction::DEFAULT_PAYMENT_METHOD_NAME,
distributors: [enterprise]
)
Spree::PaymentMethod.find_by(name: Rails.application.config.api_payment_method[:name])
}
let(:customer) { create(:customer) }

View File

@@ -5,13 +5,6 @@ require 'spec_helper'
RSpec.describe CustomerAccountTransactions::DataLoaderService do
subject { described_class.new(user:, enterprise:) }
let!(:payment_method) {
create(
:payment_method,
name: CustomerAccountTransaction::DEFAULT_PAYMENT_METHOD_NAME,
distributors: [enterprise]
)
}
let(:user) { create(:user) }
let(:enterprise) { create(:distributor_enterprise) }