Update User Invoices job does not run unless necessary global settings have been configured

This commit is contained in:
Rob Harrington
2015-07-02 16:25:52 +08:00
parent e6f6a3ad81
commit d3e8966e65
2 changed files with 59 additions and 12 deletions

View File

@@ -1,5 +1,9 @@
UpdateUserInvoices = Struct.new("UpdateUserInvoices") do
def perform
return unless accounts_distributor = Enterprise.find_by_id(Spree::Config.accounts_distributor_id)
return unless accounts_distributor.payment_methods.find_by_id(Spree::Config.default_accounts_payment_method_id)
return unless accounts_distributor.shipping_methods.find_by_id(Spree::Config.default_accounts_shipping_method_id)
# If it is the first of the month, update invoices for the previous month up until midnight last night
# Otherwise, update invoices for the current month
start_date = (Time.now - 1.day).beginning_of_month

View File

@@ -16,27 +16,70 @@ describe UpdateUserInvoices do
let!(:billable_period2) { create(:billable_period, owner: user, begins_at: start_of_july + 12.days, ends_at: start_of_july + 20.days) }
describe "perform" do
let(:accounts_distributor) { double(:accounts_distributor) }
before do
allow(Enterprise).to receive(:find_by_id) { accounts_distributor }
allow(accounts_distributor).to receive(:payment_methods) { double(:payment_methods, find_by_id: true) }
allow(accounts_distributor).to receive(:shipping_methods) { double(:shipping_methods, find_by_id: true) }
allow(updater).to receive(:update_invoice_for)
end
context "on the first of the month" do
travel_to(3.hours)
context "when necessary global config setting have not been set" do
travel_to(20.days)
it "updates the user's current invoice with billable_periods from the previous month" do
updater.perform
expect(updater).to have_received(:update_invoice_for).once
.with(user, [old_billable_period])
context "when accounts_distributor has been set" do
before do
allow(Enterprise).to receive(:find_by_id) { false }
updater.perform
end
it "doesn't run" do
expect(updater).to_not have_received(:update_invoice_for)
end
end
context "when default payment method has been set" do
before do
allow(accounts_distributor).to receive(:payment_methods) { double(:payment_methods, find_by_id: false) }
updater.perform
end
it "doesn't run" do
expect(updater).to_not have_received(:update_invoice_for)
end
end
context "when default shipping method has been set" do
before do
allow(accounts_distributor).to receive(:shipping_methods) { double(:shipping_methods, find_by_id: false) }
updater.perform
end
it "doesn't run" do
expect(updater).to_not have_received(:update_invoice_for)
end
end
end
context "on other days" do
travel_to(20.days)
context "when necessary global config setting have been set" do
context "on the first of the month" do
travel_to(3.hours)
it "updates the user's current invoice with billable_periods from the current month" do
updater.perform
expect(updater).to have_received(:update_invoice_for).once
.with(user, [billable_period1, billable_period2])
it "updates the user's current invoice with billable_periods from the previous month" do
updater.perform
expect(updater).to have_received(:update_invoice_for).once
.with(user, [old_billable_period])
end
end
context "on other days" do
travel_to(20.days)
it "updates the user's current invoice with billable_periods from the current month" do
updater.perform
expect(updater).to have_received(:update_invoice_for).once
.with(user, [billable_period1, billable_period2])
end
end
end
end