From 62d767b69511e2f77d181b5c8f45b3369ec8ecd1 Mon Sep 17 00:00:00 2001 From: Rick Barrette Date: Fri, 20 Nov 2026 21:50:22 -0500 Subject: [PATCH] Estimates QuickBooks can't read raise an ordinary error EstimateService had no build_qbo_remote, so a failed read fell back to ServiceBase's NotImplementedError, which rescue StandardError misses (e.g. saving an appointment with an estimate while QuickBooks was down gave a 500). Tests keep refund emails off redmine_qbo_reminders' SMTP server. Co-Authored-By: Claude Opus 5.5 --- app/services/estimate_service.rb | 10 ++++++++++ test/test_helper.rb | 8 ++++++++ test/unit/estimate_service_test.rb | 32 ++++++++++++++++++++++++++++++ 3 files changed, 50 insertions(+) create mode 100644 test/unit/estimate_service_test.rb diff --git a/app/services/estimate_service.rb b/app/services/estimate_service.rb index c41cda9..a8bc350 100644 --- a/app/services/estimate_service.rb +++ b/app/services/estimate_service.rb @@ -9,5 +9,15 @@ #THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. class EstimateService < ServiceBase + # Raised when an estimate can't be read from QuickBooks (e.g. QuickBooks is down or disconnected) + class FetchError < StandardError; end + private + + # Estimates are only read here (EstimateWriter saves them). When the read fails, raise an ordinary error + # rather than ServiceBase's NotImplementedError, which callers' `rescue StandardError` would miss; nothing + # is cached, so the next read tries QuickBooks again. + def build_qbo_remote + raise FetchError, "Could not read estimate ##{@local.id} from QuickBooks" + end end \ No newline at end of file diff --git a/test/test_helper.rb b/test/test_helper.rb index 74d7403..7bf5acc 100644 --- a/test/test_helper.rb +++ b/test/test_helper.rb @@ -12,6 +12,14 @@ require File.expand_path('../../../test/test_helper', __dir__) require File.expand_path('qbo_fake_service', __dir__) require File.expand_path('qbo_editor_test_helper', __dir__) +# With redmine_qbo_reminders installed, refund emails go out through its SMTP server +# (smtp-relay.gmail.com by default). Keep tests on Redmine's own (test) delivery. +if defined?(RedmineQboReminders::Settings) + ActiveSupport::TestCase.setup do + Setting.plugin_redmine_qbo_reminders = { 'delivery' => 'redmine' } + end +end + module QboRefundTestHelper CUSTOMER_ID = 9101 diff --git a/test/unit/estimate_service_test.rb b/test/unit/estimate_service_test.rb new file mode 100644 index 0000000..474c883 --- /dev/null +++ b/test/unit/estimate_service_test.rb @@ -0,0 +1,32 @@ +#The MIT License (MIT) +# +#Copyright (c) 2026 rick barrette +# +#Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions: +# +#The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software. +# +#THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. + +require File.expand_path('../test_helper', __dir__) + +class EstimateServiceTest < ActiveSupport::TestCase + def setup + Estimate.where(id: 9601).delete_all + @estimate = Estimate.create!(id: 9601, doc_number: '1068', customer_id: 9101) + end + + def test_an_estimate_quickbooks_cannot_read_raises_an_ordinary_error + QboConnectionService.stubs(:with_qbo_service).raises(RuntimeError, 'QBO not connected') + error = assert_raises(EstimateService::FetchError) { @estimate.details } + assert_kind_of StandardError, error + assert_match(/9601/, error.message) + end + + def test_labor_hours_are_zero_while_quickbooks_is_down + skip 'redmine_qbo_calendar is not installed' unless defined?(CustomerAppointment) + + QboConnectionService.stubs(:with_qbo_service).raises(RuntimeError, 'QBO not connected') + assert_equal 0, CustomerAppointment.estimate_labor_hours(@estimate) + end +end