The personal finance app for everyone (by everyone)
Ruby
Restore an account to disabled when its destroy fails (#4173)
jaysbeekayfbd90463e8c7
diff --git a/app/models/account.rb b/app/models/account.rbindex 9c0c40698a..c9e573a31d 100644--- a/app/models/account.rb+++ b/app/models/account.rb@@ -175,8 +175,11 @@ class Account < ApplicationRecordstate :disabledstate :pending_deletion+ # `timestamps: true` stamps disabled_at on entering disabled but never+ # clears it, so leaving disabled clears it here. A present disabled_at then+ # means the account was disabled, which the failed-deletion restore relies on.event :activate do- transitions from: [ :draft, :disabled ], to: :active+ transitions from: [ :draft, :disabled ], to: :active, after: :clear_disabled_atendevent :disable do@@ -184,11 +187,29 @@ class Account < ApplicationRecordendevent :enable do- transitions from: :disabled, to: :active+ transitions from: :disabled, to: :active, after: :clear_disabled_atend+ # An active or draft account can still carry a disabled_at from before enable+ # cleared it. Drop it here, so a failed deletion (below) stamps the failure+ # rather than reviving that stale date as the cutoff. A disabled account+ # keeps its date.event :mark_for_deletion do- transitions from: [ :draft, :active, :disabled ], to: :pending_deletion+ transitions from: [ :draft, :active ], to: :pending_deletion, after: :clear_disabled_at+ transitions from: :disabled, to: :pending_deletion+ end++ # The AASM way out of pending_deletion (destroy_later's enqueue failure+ # restores by update_column instead): Account#destroy fires it when the+ # destroy raises, so the account is visible again and can be re-deleted.+ # A separate event rather than widening `disable`, which would let every+ # `disable!`/`may_disable?` caller act on an account being deleted.+ #+ # disabled_at is the net-worth cutoff, so an account that was already+ # disabled keeps the date it was disabled; only one deleted from active or+ # draft is stamped at the failure.+ event :restore_after_failed_deletion do+ transitions from: :pending_deletion, to: :disabled, after: :keep_original_disabled_atendend@@ -672,9 +693,14 @@ class Account < ApplicationRecorddef destroysuperrescue => e- # If destruction fails, transition back to disabled state- # This provides a cleaner recovery path than the generic scheduled_for_deletion flag- disable! if may_disable?+ # If destruction fails, leave the account disabled so it stays visible and+ # can be deleted again. DestroyJob's scheduled_for_deletion reset is for+ # provider items; accounts track deletion in `status`.+ if may_restore_after_failed_deletion?+ restore_after_failed_deletion!+ elsif may_disable?+ disable!+ endraise eend@@ -871,6 +897,16 @@ class Account < ApplicationRecorderrors.add(:owner, :invalid, message: "must belong to the same family as the account")end+ # AASM transition callbacks run after the timestamp is set and before the+ # state is saved, so both land in the event's own save.+ def clear_disabled_at+ self.disabled_at = nil+ end++ def keep_original_disabled_at+ self.disabled_at = disabled_at_in_database if disabled_at_in_database.present?+ end+def capture_account_statement_ids_to_move@statement_ids_to_move = account_statements.idsenddiff --git a/test/jobs/destroy_job_test.rb b/test/jobs/destroy_job_test.rbindex 54633495e8..2122ff0cdd 100644--- a/test/jobs/destroy_job_test.rb+++ b/test/jobs/destroy_job_test.rb@@ -17,6 +17,37 @@ class DestroyJobTest < ActiveJob::TestCaseDestroyJob.perform_now(model)end+ test "a provider item's failed destroy still resets scheduled_for_deletion" do+ item = plaid_items(:one)+ item.update_column(:scheduled_for_deletion, true)+ item.stubs(:destroy).raises(StandardError, "destroy failed")++ DestroyJob.perform_now(item)++ assert_not item.reload.scheduled_for_deletion?+ end++ test "an account whose destroy raises is left disabled, not pending_deletion" do+ account = accounts(:depository)+ account.mark_for_deletion!+ account.stubs(:cleanup_transfers).raises(StandardError, "destroy failed")++ assert_no_difference "Account.count" do+ DestroyJob.perform_now(account)+ end++ assert_equal "disabled", account.reload.status+ end++ test "an account whose destroy succeeds is removed" do+ account = accounts(:depository)+ account.mark_for_deletion!++ assert_difference "Account.count", -1 do+ DestroyJob.perform_now(account)+ end+ end+test "inherits the deferred-enqueue policy from ApplicationJob" doaccount = accounts(:depository)enqueued_mid_transaction = nildiff --git a/test/models/account_test.rb b/test/models/account_test.rbindex c0df3dc108..5678823aa4 100644--- a/test/models/account_test.rb+++ b/test/models/account_test.rb@@ -16,6 +16,153 @@ class AccountTest < ActiveSupport::TestCaseendend+ test "a destroy that raises leaves a pending_deletion account disabled" do+ @account.mark_for_deletion!+ @account.stubs(:cleanup_transfers).raises(StandardError, "destroy failed")++ error = assert_raises(StandardError) { @account.destroy }++ assert_equal "destroy failed", error.message+ assert_equal "disabled", @account.reload.status+ end++ test "a disabled account can be scheduled for deletion again after a failed destroy" do+ @account.mark_for_deletion!+ @account.stubs(:cleanup_transfers).raises(StandardError, "destroy failed")+ assert_raises(StandardError) { @account.destroy }+ @account.unstub(:cleanup_transfers)+ @account.reload++ assert_enqueued_with(job: DestroyJob, args: [ @account ]) do+ @account.destroy_later+ end+ assert_equal "pending_deletion", @account.reload.status+ end++ test "a failed direct destroy still disables an active account" do+ assert @account.active?+ @account.stubs(:cleanup_transfers).raises(StandardError, "destroy failed")++ assert_raises(StandardError) { @account.destroy }++ assert_equal "disabled", @account.reload.status+ end++ test "disable from active is unchanged" do+ assert @account.active?++ @account.disable!++ assert_equal "disabled", @account.reload.status+ end++ test "no event other than the failed-deletion recovery leaves pending_deletion" do+ @account.mark_for_deletion!++ assert_not @account.may_activate?+ assert_not @account.may_enable?+ assert_not @account.may_disable?+ assert_not @account.may_mark_for_deletion?+ permitted = @account.aasm.events(permitted: true).map(&:name)+ assert_equal [ :restore_after_failed_deletion ], permitted+ end++ test "the failed-deletion recovery only fires from pending_deletion" do+ assert @account.active?+ assert_not @account.aasm.may_fire_event?(:restore_after_failed_deletion)++ @account.disable!+ assert_not @account.aasm.may_fire_event?(:restore_after_failed_deletion)+ end++ # disabled_at is the net-worth cutoff for a disabled account+ # (NetWorthSeriesBuilder, NetWorthBreakdownSeriesBuilder). A failed delete+ # must not move it to the failure, which would count the account's balances+ # for the whole time it was already disabled.+ test "a failed destroy of a disabled account keeps its original disabled_at" do+ disabled_at = 40.days.ago.change(usec: 0)+ @account.update_columns(status: "disabled", disabled_at: disabled_at)+ @account.mark_for_deletion!+ @account.stubs(:cleanup_transfers).raises(StandardError, "destroy failed")++ assert_raises(StandardError) { @account.destroy }++ @account.reload+ assert_equal "disabled", @account.status+ assert_equal disabled_at, @account.disabled_at+ end++ # The other side: an account deleted from active has no cutoff yet, so the+ # failure is when it stopped counting. One disabled and enabled again earlier+ # must not inherit that old cutoff.+ test "a failed destroy of an account that was active stamps disabled_at at the failure" do+ @account.disable!+ @account.enable!+ @account.mark_for_deletion!+ @account.stubs(:cleanup_transfers).raises(StandardError, "destroy failed")++ failed_at = 3.days.from_now.change(usec: 0)+ travel_to(failed_at) { assert_raises(StandardError) { @account.destroy } }++ @account.reload+ assert_equal "disabled", @account.status+ assert_equal failed_at, @account.disabled_at+ end++ # An account disabled and enabled again before enable cleared disabled_at+ # still carries that date while active or draft. Deleting it must drop the+ # stale date, so a failed destroy stamps the failure instead of reviving it+ # as the account's net-worth cutoff.+ test "a failed destroy of an active or draft account with a stale disabled_at stamps the failure" do+ stale = 1.year.ago.change(usec: 0)+ failed_at = 3.days.from_now.change(usec: 0)++ results = %w[active draft].index_with do |status|+ @account.update_columns(status: status, disabled_at: stale)+ @account.reload.mark_for_deletion!+ @account.stubs(:cleanup_transfers).raises(StandardError, "destroy failed")++ travel_to(failed_at) { assert_raises(StandardError) { @account.destroy } }+ @account.unstub(:cleanup_transfers)++ @account.reload+ [ @account.status, @account.disabled_at ]+ end++ assert_equal({ "active" => [ "disabled", failed_at ], "draft" => [ "disabled", failed_at ] }, results)+ end++ test "marking an active or draft account for deletion clears a stale disabled_at" do+ results = %w[active draft].index_with do |status|+ @account.update_columns(status: status, disabled_at: 1.year.ago)+ @account.reload.mark_for_deletion!+ @account.reload.disabled_at+ end++ assert_equal({ "active" => nil, "draft" => nil }, results)+ end++ test "marking a disabled account for deletion keeps its disabled_at" do+ disabled_at = 40.days.ago.change(usec: 0)+ @account.update_columns(status: "disabled", disabled_at: disabled_at)++ @account.reload.mark_for_deletion!++ assert_equal disabled_at, @account.reload.disabled_at+ end++ test "enabling an account clears disabled_at" do+ @account.disable!+ assert_not_nil @account.reload.disabled_at++ @account.enable!+ assert_nil @account.reload.disabled_at++ @account.disable!+ @account.activate!+ assert_nil @account.reload.disabled_at+ end+test "default owner prefers a family admin before a super admin" dofamily = families(:empty)admin = users(:empty)diff --git a/test/models/balance_sheet_test.rb b/test/models/balance_sheet_test.rbindex 1b5a8e8bbf..97883262dd 100644--- a/test/models/balance_sheet_test.rb+++ b/test/models/balance_sheet_test.rb@@ -97,6 +97,54 @@ class BalanceSheetTest < ActiveSupport::TestCaseassert_equal BalanceSheet.new(@family).net_worth, values_by_date.fetch(period.end_date).value.amountend+ # A failed delete restores the account to disabled. Its history must still+ # stop where it was disabled, not run on to the failure.+ test "a failed destroy of a disabled account keeps its net worth cutoff" do+ period = Period.custom(start_date: Date.current - 3.days, end_date: Date.current)+ active_account = create_account(balance: 1_000, accountable: Depository.new)+ disabled_account = create_account(balance: 0, accountable: Depository.new)+ create_balance(account: active_account, date: period.start_date, balance: 1_000)+ create_balance(account: active_account, date: period.end_date, balance: 1_000)+ create_balance(account: disabled_account, date: period.start_date, balance: 20_000)+ disabled_account.update_columns(status: "disabled", disabled_at: 10.days.ago)++ start_value = -> { BalanceSheet.new(@family).net_worth_series(period: period).values.index_by(&:date).fetch(period.start_date).value.amount }+ assert_equal 1_000, start_value.call++ disabled_account.mark_for_deletion!+ disabled_account.stubs(:cleanup_transfers).raises(StandardError, "destroy failed")+ assert_raises(StandardError) { disabled_account.destroy }+ assert disabled_account.reload.disabled?+ Rails.cache.clear++ assert_equal 1_000, start_value.call+ end++ # An active account can still carry a disabled_at from being disabled and+ # enabled again before enable cleared it. A failed delete must not turn that+ # stale date into its cutoff and drop the stretch in which it was active.+ test "a failed destroy of an active account with a stale disabled_at keeps its history" do+ period = Period.custom(start_date: Date.current - 3.days, end_date: Date.current)+ other_account = create_account(balance: 1_000, accountable: Depository.new)+ deleted_account = create_account(balance: 20_000, accountable: Depository.new)+ create_balance(account: other_account, date: period.start_date, balance: 1_000)+ create_balance(account: other_account, date: period.end_date, balance: 1_000)+ create_balance(account: deleted_account, date: period.start_date, balance: 20_000)+ deleted_account.update_columns(status: "active", disabled_at: 10.days.ago)++ start_value = -> { BalanceSheet.new(@family).net_worth_series(period: period).values.index_by(&:date).fetch(period.start_date).value.amount }+ before = start_value.call+ assert_equal 21_000, before++ deleted_account.reload.mark_for_deletion!+ deleted_account.stubs(:cleanup_transfers).raises(StandardError, "destroy failed")+ assert_raises(StandardError) { deleted_account.destroy }+ assert deleted_account.reload.disabled?+ Rails.cache.clear++ assert_equal before, start_value.call+ end+test "historical account scope respects shared-account finance settings" domember = users(:new_email)included_account = create_account(balance: 0, accountable: Depository.new)