From d3e8966e6522bc3a8a1ef4e515053db5acd65839 Mon Sep 17 00:00:00 2001 From: Rob Harrington Date: Thu, 2 Jul 2015 16:25:52 +0800 Subject: [PATCH] Update User Invoices job does not run unless necessary global settings have been configured --- app/jobs/update_user_invoices.rb | 4 ++ spec/jobs/update_user_invoices_spec.rb | 67 +++++++++++++++++++++----- 2 files changed, 59 insertions(+), 12 deletions(-) diff --git a/app/jobs/update_user_invoices.rb b/app/jobs/update_user_invoices.rb index 34c54469a4..a96aa5ba82 100644 --- a/app/jobs/update_user_invoices.rb +++ b/app/jobs/update_user_invoices.rb @@ -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 diff --git a/spec/jobs/update_user_invoices_spec.rb b/spec/jobs/update_user_invoices_spec.rb index fd9d970d67..8ca1421244 100644 --- a/spec/jobs/update_user_invoices_spec.rb +++ b/spec/jobs/update_user_invoices_spec.rb @@ -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