Skip to content

armgit / sure

Public Imported from GitHub
0

The personal finance app for everyone (by everyone)

Ruby

Stop showing connections with rejected credentials as Connected (#3854)

Guillem Arias Fausteaeb8c1954619
diff --git a/app/helpers/settings_helper.rb b/app/helpers/settings_helper.rb
index 981b28cece..cb765cb198 100644
--- a/app/helpers/settings_helper.rb
+++ b/app/helpers/settings_helper.rb
@@ -253,9 +253,9 @@ module SettingsHelper
return { status: :err, meta: t("settings.providers.meta.sync_error"), last_synced_at: nil } if health[:error]
- valid_items = @enable_banking_items&.select(&:session_valid?) || []
+ valid_items = @enable_banking_items&.reject(&:needs_authorization?) || []
- # All items have expired/missing sessions — need re-authorization
+ # All items have expired, missing or rejected sessions — need re-authorization
if valid_items.empty?
return { status: :warn, meta: t("settings.providers.meta.reconsent_required"), last_synced_at: last_synced_at }
end
diff --git a/app/models/brex_item/importer.rb b/app/models/brex_item/importer.rb
index a053c16e26..4d982dee7c 100644
--- a/app/models/brex_item/importer.rb
+++ b/app/models/brex_item/importer.rb
@@ -43,6 +43,8 @@ class BrexItem::Importer
rescue Provider::Brex::BrexError => e
mark_requires_update_if_credentials_error(e)
Rails.logger.error "BrexItem::Importer - Brex API error: #{e.message} trace_id=#{e.trace_id}"
+ # A rejected token fails the sync instead of completing it with nothing imported
+ raise if credentials_error?(e)
nil
rescue JSON::ParserError => e
Rails.logger.error "BrexItem::Importer - Failed to parse Brex API response: #{e.message}"
@@ -123,6 +125,8 @@ class BrexItem::Importer
transactions_failed += 1
end
rescue => e
+ raise if credentials_error?(e)
+
transactions_failed += 1
Rails.logger.error "BrexItem::Importer - Failed to fetch/store transactions for account #{brex_account.account_id}: #{e.message}"
end
@@ -155,6 +159,8 @@ class BrexItem::Importer
rescue Provider::Brex::BrexError => e
mark_requires_update_if_credentials_error(e)
Rails.logger.error "BrexItem::Importer - Brex API error for account #{brex_account.account_id}: #{e.message} trace_id=#{e.trace_id}"
+ # Also when the token can list accounts but not read their transactions
+ raise if credentials_error?(e)
{ success: false, transactions_count: 0, error: e.message }
rescue JSON::ParserError => e
Rails.logger.error "BrexItem::Importer - Failed to parse transaction response for account #{brex_account.account_id}: #{e.message}"
@@ -223,8 +229,12 @@ class BrexItem::Importer
end
end
+ def credentials_error?(error)
+ error.is_a?(Provider::Brex::BrexError) && error.error_type.in?([ :unauthorized, :access_forbidden ])
+ end
+
def mark_requires_update_if_credentials_error(error)
- return unless error.error_type.in?([ :unauthorized, :access_forbidden ])
+ return unless credentials_error?(error)
brex_item.update!(status: :requires_update)
rescue => update_error
diff --git a/app/models/enable_banking_item.rb b/app/models/enable_banking_item.rb
index 130ecfbae7..34fc03e37b 100644
--- a/app/models/enable_banking_item.rb
+++ b/app/models/enable_banking_item.rb
@@ -58,8 +58,10 @@ class EnableBankingItem < ApplicationRecord
session_id.present? && session_expires_at.present? && session_expires_at <= Time.current
end
+ # A session-level 401/404 flags the item requires_update while its session
+ # can still look unexpired.
def needs_authorization?
- !session_valid?
+ !session_valid? || requires_update?
end
validate :psu_type_in_aspsp_types
diff --git a/app/models/lunchflow_item/importer.rb b/app/models/lunchflow_item/importer.rb
index f3b4a91006..10de598f42 100644
--- a/app/models/lunchflow_item/importer.rb
+++ b/app/models/lunchflow_item/importer.rb
@@ -189,6 +189,8 @@ class LunchflowItem::Importer
rescue => update_error
Rails.logger.error "LunchflowItem::Importer - Failed to update item status: #{update_error.message}"
end
+ # Fail the sync instead of completing it with nothing imported
+ raise
end
Rails.logger.error "LunchflowItem::Importer - Lunch flow API error: #{e.message}"
return nil
diff --git a/app/models/mercury_item/importer.rb b/app/models/mercury_item/importer.rb
index f507f1ad6c..efa424df48 100644
--- a/app/models/mercury_item/importer.rb
+++ b/app/models/mercury_item/importer.rb
@@ -122,6 +122,8 @@ class MercuryItem::Importer
rescue => update_error
Rails.logger.error "MercuryItem::Importer - Failed to update item status: #{update_error.message}"
end
+ # Fail the sync instead of completing it with nothing imported
+ raise
end
Rails.logger.error "MercuryItem::Importer - Mercury API error: #{e.message}"
return nil
diff --git a/app/models/wise_item/importer.rb b/app/models/wise_item/importer.rb
index b8a693e59d..db307561d8 100644
--- a/app/models/wise_item/importer.rb
+++ b/app/models/wise_item/importer.rb
@@ -59,6 +59,8 @@ class WiseItem::Importer
end
mark_requires_update_if_credentials_error(e)
Rails.logger.error "WiseItem::Importer - Failed to fetch balances: #{e.message}"
+ # A rejected token fails the sync instead of completing it with nothing imported
+ raise if credentials_error?(e)
nil
rescue => e
Rails.logger.error "WiseItem::Importer - Unexpected error fetching balances: #{e.class} - #{e.message}"
@@ -595,8 +597,12 @@ class WiseItem::Importer
Time.current
end
+ def credentials_error?(error)
+ error.is_a?(Provider::Wise::WiseError) && error.error_type.in?([ :unauthorized, :access_forbidden ])
+ end
+
def mark_requires_update_if_credentials_error(error)
- return unless error.is_a?(Provider::Wise::WiseError) && error.error_type.in?([ :unauthorized, :access_forbidden ])
+ return unless credentials_error?(error)
wise_item.update!(status: :requires_update)
rescue => e
diff --git a/test/helpers/settings_helper_test.rb b/test/helpers/settings_helper_test.rb
index 20b39d1ee3..8e60e61092 100644
--- a/test/helpers/settings_helper_test.rb
+++ b/test/helpers/settings_helper_test.rb
@@ -37,6 +37,15 @@ class SettingsHelperTest < ActionView::TestCase
assert_equal({ status: :off }, provider_summary("trading212"))
end
+ test "provider_summary for enable_banking asks for re-consent when the bank rejected an unexpired session" do
+ @enable_banking_items = [ EnableBankingItem.new(session_id: "session", session_expires_at: 30.days.from_now, status: :requires_update) ]
+
+ summary = provider_summary("enable_banking")
+
+ assert_equal :warn, summary[:status]
+ assert_equal I18n.t("settings.providers.meta.reconsent_required"), summary[:meta]
+ end
+
# A key provider_summary doesn't handle falls through to { status: :off },
# which lists a connected provider under Available. Wise and CoinSpot
# shipped that way. One key at a time, so a branch that reads another
diff --git a/test/models/brex_item/importer_test.rb b/test/models/brex_item/importer_test.rb
index ed64f5e812..5911fbd046 100644
--- a/test/models/brex_item/importer_test.rb
+++ b/test/models/brex_item/importer_test.rb
@@ -239,15 +239,16 @@ class BrexItem::ImporterTest < ActiveSupport::TestCase
assert_equal "snapshot failed", error.message
end
- test "marks item as requiring update on authorization errors" do
+ test "marks item as requiring update and raises on authorization errors" do
provider = mock("brex_provider")
provider.expects(:get_accounts).raises(
Provider::Brex::BrexError.new("Access forbidden", :access_forbidden, http_status: 403, trace_id: "trace_123")
)
- result = BrexItem::Importer.new(@brex_item, brex_provider: provider).import
+ assert_raises(Provider::Brex::BrexError) do
+ BrexItem::Importer.new(@brex_item, brex_provider: provider).import
+ end
- refute result[:success]
assert @brex_item.reload.requires_update?
end
diff --git a/test/models/brex_item/syncer_test.rb b/test/models/brex_item/syncer_test.rb
index ce192c5a58..e5c5bbe555 100644
--- a/test/models/brex_item/syncer_test.rb
+++ b/test/models/brex_item/syncer_test.rb
@@ -98,6 +98,35 @@ class BrexItem::SyncerTest < ActiveSupport::TestCase
assert_equal I18n.t("brex_items.syncer.credentials_invalid"), error.message
end
+ test "a rejected token fails the sync and flags the connection" do
+ Provider::Brex.any_instance.stubs(:get_accounts)
+ .raises(Provider::Brex::BrexError.new("Unauthorized", :unauthorized, http_status: 401))
+ sync = Sync.create!(syncable: @brex_item)
+
+ sync.perform
+
+ assert sync.reload.failed?, "expected the sync to fail, was #{sync.status}"
+ assert_equal I18n.t("brex_items.syncer.credentials_invalid"), sync.error
+ assert @brex_item.reload.requires_update?
+ end
+
+ # Brex scopes accounts and transactions separately, so a token can list the
+ # accounts and still be refused their transactions.
+ test "a token refused transactions fails the sync and flags the connection" do
+ account = @brex_item.family.accounts.create!(name: "Linked Brex Checking", balance: 0, currency: "USD", accountable: Depository.new)
+ AccountProvider.create!(account: account, provider: brex_accounts(:checking_account))
+ Provider::Brex.any_instance.stubs(:get_accounts).returns(accounts: [])
+ Provider::Brex.any_instance.stubs(:get_cash_transactions)
+ .raises(Provider::Brex::BrexError.new("Access forbidden", :access_forbidden, http_status: 403))
+ sync = Sync.create!(syncable: @brex_item)
+
+ sync.perform
+
+ assert sync.reload.failed?, "expected the sync to fail, was #{sync.status}"
+ assert_equal I18n.t("brex_items.syncer.credentials_invalid"), sync.error
+ assert @brex_item.reload.requires_update?
+ end
+
private
def mock_sync(window_start_date:)
diff --git a/test/models/lunchflow_item/syncer_test.rb b/test/models/lunchflow_item/syncer_test.rb
index e3a5e7a27c..e3f9b5a8f4 100644
--- a/test/models/lunchflow_item/syncer_test.rb
+++ b/test/models/lunchflow_item/syncer_test.rb
@@ -91,6 +91,18 @@ class LunchflowItem::SyncerTest < ActiveSupport::TestCase
assert_equal "sync_error", error["category"]
end
+ test "a rejected API key fails the sync and flags the connection" do
+ Provider::Lunchflow.any_instance.stubs(:get_accounts)
+ .raises(Provider::Lunchflow::LunchflowError.new("Invalid API key", :unauthorized, status: 401))
+ sync = Sync.create!(syncable: @lunchflow_item)
+
+ sync.perform
+
+ assert sync.reload.failed?, "expected the sync to fail, was #{sync.status}"
+ assert_equal "Invalid API key", sync.error
+ assert @lunchflow_item.reload.requires_update?
+ end
+
private
def recording_sync
diff --git a/test/models/mercury_item/importer_test.rb b/test/models/mercury_item/importer_test.rb
index e42b9389e6..ba0d24f2a3 100644
--- a/test/models/mercury_item/importer_test.rb
+++ b/test/models/mercury_item/importer_test.rb
@@ -113,12 +113,12 @@ class MercuryItem::ImporterTest < ActiveSupport::TestCase
# auth error handling
# ---------------------------------------------------------------------------
- test "marks item requires_update on 401 from Mercury API" do
+ test "marks item requires_update and raises on 401 from Mercury API" do
@provider.stubs(:get_accounts).raises(
Provider::Mercury::MercuryError.new("Unauthorized", :unauthorized)
)
- run_import
+ assert_raises(Provider::Mercury::MercuryError) { run_import }
assert @item.reload.requires_update?
end
diff --git a/test/models/mercury_item/syncer_test.rb b/test/models/mercury_item/syncer_test.rb
new file mode 100644
index 0000000000..43a672f4a7
--- /dev/null
+++ b/test/models/mercury_item/syncer_test.rb
@@ -0,0 +1,18 @@
+# frozen_string_literal: true
+
+require "test_helper"
+
+class MercuryItem::SyncerTest < ActiveSupport::TestCase
+ test "a rejected token fails the sync and flags the connection" do
+ mercury_item = mercury_items(:one)
+ Provider::Mercury.any_instance.stubs(:get_accounts)
+ .raises(Provider::Mercury::MercuryError.new("Invalid API token", :unauthorized))
+ sync = Sync.create!(syncable: mercury_item)
+
+ sync.perform
+
+ assert sync.reload.failed?, "expected the sync to fail, was #{sync.status}"
+ assert_equal "Invalid API token", sync.error
+ assert mercury_item.reload.requires_update?
+ end
+end
diff --git a/test/models/wise_item/importer_test.rb b/test/models/wise_item/importer_test.rb
index bbbd1fb0a2..154e4f4b88 100644
--- a/test/models/wise_item/importer_test.rb
+++ b/test/models/wise_item/importer_test.rb
@@ -527,7 +527,7 @@ class WiseItem::ImporterTest < ActiveSupport::TestCase
# Returns failed result when balances fetch fails
test "returns failed result when STANDARD balances fetch fails" do
- provider = FakeWiseProvider.new(raise_on: { get_balances: "unauthorized" })
+ provider = FakeWiseProvider.new(raise_on: { get_balances: "server error" })
result = WiseItem::Importer.new(@wise_item, wise_provider: provider).import
diff --git a/test/models/wise_item/syncer_test.rb b/test/models/wise_item/syncer_test.rb
new file mode 100644
index 0000000000..ce65ad95f5
--- /dev/null
+++ b/test/models/wise_item/syncer_test.rb
@@ -0,0 +1,18 @@
+# frozen_string_literal: true
+
+require "test_helper"
+
+class WiseItem::SyncerTest < ActiveSupport::TestCase
+ test "a rejected token fails the sync and flags the connection" do
+ wise_item = wise_items(:one)
+ Provider::Wise.any_instance.stubs(:get_balances)
+ .raises(Provider::Wise::WiseError.new("Invalid API token", :unauthorized))
+ sync = Sync.create!(syncable: wise_item)
+
+ sync.perform
+
+ assert sync.reload.failed?, "expected the sync to fail, was #{sync.status}"
+ assert_equal I18n.t("wise_items.syncer.credentials_invalid"), sync.error
+ assert wise_item.reload.requires_update?
+ end
+end