The personal finance app for everyone (by everyone)
Ruby
Skip email notifications for inactive rules (#4211)
jaysbeekay0f801e47bf47
diff --git a/app/models/rule/action_executor/send_email_notification.rb b/app/models/rule/action_executor/send_email_notification.rbindex e979160f83..020a370435 100644--- a/app/models/rule/action_executor/send_email_notification.rb+++ b/app/models/rule/action_executor/send_email_notification.rb@@ -6,6 +6,11 @@ class Rule::ActionExecutor::SendEmailNotification < Rule::ActionExecutor# rule_run is accepted for interface compatibility but unused: the digest email# is fire-and-forget and is not tracked as part of RuleRun accounting.def execute(transaction_scope, value: nil, ignore_attribute_locks: false, rule_run: nil)+ # "Apply all" runs inactive rules too, for their transaction changes. A rule+ # the user has switched off must not email, and its matches stay unrecorded+ # so they are handled when the rule is switched back on.+ return 0 unless rule.active?+candidate_ids = transaction_scope.pluck(:id)# record_for atomically inserts and returns ONLY the ids this run actuallydiff --git a/test/jobs/apply_all_rules_job_test.rb b/test/jobs/apply_all_rules_job_test.rbindex 3210f4efe3..472b8a9e9e 100644--- a/test/jobs/apply_all_rules_job_test.rb+++ b/test/jobs/apply_all_rules_job_test.rb@@ -28,15 +28,78 @@ class ApplyAllRulesJobTest < ActiveJob::TestCaseassert_equal "scheduled", rule.rule_runs.last.execution_typeend+ test "does not email for an inactive rule" do+ rule = create_email_rule(active: false)++ assert_no_difference -> { NotificationDelivery.where(rule: rule).count } do+ assert_no_enqueued_jobs only: RuleEmailNotificationJob do+ ApplyAllRulesJob.perform_now(@family)+ end+ end+ end++ test "applies an inactive rule's category action but does not email" do+ rule = create_email_rule(active: false, actions: [ category_action, email_action ])+ assert_nil @new_match.reload.category++ assert_no_difference -> { NotificationDelivery.where(rule: rule).count } do+ assert_no_enqueued_jobs only: RuleEmailNotificationJob do+ ApplyAllRulesJob.perform_now(@family)+ end+ end++ assert_equal @groceries_category, @new_match.reload.category+ end++ test "emails new matches for an active rule" do+ rule = create_email_rule(active: true)++ assert_difference -> { NotificationDelivery.where(rule: rule).count }, 1 do+ assert_enqueued_with(job: RuleEmailNotificationJob, args: [ rule.id, [ @new_match.id ] ]) do+ ApplyAllRulesJob.perform_now(@family)+ end+ end++ assert_enqueued_jobs 1, only: RuleEmailNotificationJob+ end++ test "does not re-send an active rule's already recorded matches" do+ rule = create_email_rule(active: true)+ NotificationDelivery.record_for(rule_id: rule.id, transaction_ids: [ @new_match.id ])++ assert_no_difference -> { NotificationDelivery.where(rule: rule).count } do+ assert_no_enqueued_jobs only: RuleEmailNotificationJob do+ ApplyAllRulesJob.perform_now(@family)+ end+ end+ end+private- def create_rule(active:)+ def category_action+ Rule::Action.new(action_type: "set_transaction_category", value: @groceries_category.id)+ end++ def email_action+ Rule::Action.new(action_type: "send_email_notification")+ end++ # An email rule whose existing match is already recorded, as its baseline+ # leaves it, followed by one new match (@new_match) it has not seen.+ def create_email_rule(active:, actions: [ email_action ])+ rule = create_rule(active: active, actions: actions)+ NotificationDelivery.record_for(rule_id: rule.id, transaction_ids: [ @transaction.id ])+ @new_match = create_transaction(account: @account, name: "Whole Foods").transaction+ rule+ end++ def create_rule(active:, actions: [ category_action ])Rule.create!(family: @family,resource_type: "transaction",active: active,effective_date: 1.day.ago.to_date,conditions: [ Rule::Condition.new(condition_type: "transaction_name", operator: "like", value: "Whole Foods") ],- actions: [ Rule::Action.new(action_type: "set_transaction_category", value: @groceries_category.id) ]+ actions: actions)endenddiff --git a/test/models/rule/action_executor/send_email_notification_test.rb b/test/models/rule/action_executor/send_email_notification_test.rbindex f024ab821f..f7fa97ba14 100644--- a/test/models/rule/action_executor/send_email_notification_test.rb+++ b/test/models/rule/action_executor/send_email_notification_test.rb@@ -6,6 +6,9 @@ class Rule::ActionExecutor::SendEmailNotificationTest < ActiveSupport::TestCasesetup do@family = families(:dylan_family)@rule = rules(:one)+ # The fixture rule is inactive by the column default, and an inactive rule+ # sends nothing. Switch it on so these tests exercise the sending path.+ @rule.update_columns(active: true)@account = @family.accounts.create!(name: "Notify test", balance: 1000, currency: "USD", accountable: Depository.new)@txn1 = create_transaction(date: Date.current, account: @account, amount: 100, name: "Coffee").transaction@scope = @account.transactions@@ -58,12 +61,26 @@ class Rule::ActionExecutor::SendEmailNotificationTest < ActiveSupport::TestCaseassert_equal 2, recorded.sizeend+ test "an inactive rule records and enqueues nothing" do+ @rule.update_columns(active: false)++ result = nil+ assert_no_difference -> { NotificationDelivery.where(rule: @rule).count } do+ assert_no_enqueued_jobs only: RuleEmailNotificationJob do+ result = action.apply(@scope)+ end+ end++ assert_equal 0, result+ end+test "pre-seed watermark records existing matches without sending so history never emails" do# after_create_commit does not fire under transactional tests (the wrapping# transaction never commits), so invoke the seeding path directly to verify# the watermark behavior the callback performs in production.rule = @family.rules.create!(resource_type: "transaction",+ active: true,actions_attributes: [ { action_type: "send_email_notification" } ])seed_action = rule.actions.first