Skip to content

armgit / sure

Public Imported from GitHub
0

The personal finance app for everyone (by everyone)

Ruby

Only fetch Plaid liabilities the user consented to (#4524)

Wickes Wong9240cc6bf09c
diff --git a/app/models/plaid_item.rb b/app/models/plaid_item.rb
index 388ee02930..b2ffdba184 100644
--- a/app/models/plaid_item.rb
+++ b/app/models/plaid_item.rb
@@ -162,6 +162,12 @@ class PlaidItem < ApplicationRecord
supported_products.include?(product)
end
+ # available_products only says the institution offers a product. Calling one
+ # the user did not consent to in Link fails with ADDITIONAL_CONSENT_REQUIRED.
+ def consented_to_product?(product)
+ consented_products.include?(product)
+ end
+
private
def transactions_refresh_cache_key
"plaid_item:#{id}:transactions_refresh_requested"
@@ -213,8 +219,15 @@ class PlaidItem < ApplicationRecord
# the transactions fetch entirely and those accounts imported balances and
# liabilities but no transactions at all.
def supported_products
+ (available_products + consented_products).map(&:to_s).uniq
+ end
+
+ # Link's initial `products` are billed when the item is created; products
+ # granted through `additional_consented_products` stay in consented_products
+ # until their first call bills them.
+ def consented_products
consented = raw_payload.is_a?(Hash) ? Array(raw_payload["consented_products"]) : []
- (available_products + billed_products + consented).map(&:to_s).uniq
+ (billed_products + consented).map(&:to_s).uniq
end
end
diff --git a/app/models/plaid_item/accounts_snapshot.rb b/app/models/plaid_item/accounts_snapshot.rb
index a3e3f86c3a..27f03ad4ce 100644
--- a/app/models/plaid_item/accounts_snapshot.rb
+++ b/app/models/plaid_item/accounts_snapshot.rb
@@ -94,7 +94,7 @@ class PlaidItem::AccountsSnapshot
end
def can_fetch_liabilities?
- plaid_item.supports_product?("liabilities") &&
+ plaid_item.consented_to_product?("liabilities") &&
accounts.any? do |a|
# Plaid's `credit` type has two subtypes: "credit card" and "paypal".
# Liabilities covers both, so match on the type rather than allow-listing
diff --git a/test/models/plaid_item/accounts_snapshot_test.rb b/test/models/plaid_item/accounts_snapshot_test.rb
index 108a6c4529..d2988d75c8 100644
--- a/test/models/plaid_item/accounts_snapshot_test.rb
+++ b/test/models/plaid_item/accounts_snapshot_test.rb
@@ -141,7 +141,7 @@ class PlaidItem::AccountsSnapshotTest < ActiveSupport::TestCase
end
test "fetches liabilities data if item supports liabilities and liabilities accounts present" do
- @plaid_item.update!(available_products: [ "liabilities" ], billed_products: [])
+ @plaid_item.update!(available_products: [], billed_products: [ "liabilities" ])
@snapshot.expects(:accounts).returns([
OpenStruct.new(
@@ -159,7 +159,7 @@ class PlaidItem::AccountsSnapshotTest < ActiveSupport::TestCase
end
test "does not fetch liabilities if no liabilities accounts" do
- @plaid_item.update!(available_products: [ "liabilities" ], billed_products: [])
+ @plaid_item.update!(available_products: [], billed_products: [ "liabilities" ])
@snapshot.expects(:accounts).returns([]).at_least_once
@@ -169,4 +169,58 @@ class PlaidItem::AccountsSnapshotTest < ActiveSupport::TestCase
@snapshot.get_account_data("123")
end
+
+ test "fetches liabilities for a credit account when liabilities was an additionally consented product" do
+ @plaid_item.update!(
+ available_products: [],
+ billed_products: [],
+ raw_payload: { "consented_products" => [ "liabilities" ] }
+ )
+
+ @snapshot.expects(:accounts).returns([
+ OpenStruct.new(
+ account_id: "123",
+ type: "credit",
+ subtype: "credit card"
+ )
+ ]).at_least_once
+
+ @plaid_provider.expects(:get_transactions).never
+ @plaid_provider.expects(:get_item_investments).never
+ @plaid_provider.expects(:get_item_liabilities).with(@plaid_item.access_token).once
+
+ @snapshot.get_account_data("123")
+ end
+
+ test "does not fetch liabilities the institution offers but the user did not consent to" do
+ # A Depository link consents to transactions and investments only. Plaid
+ # still lists liabilities in available_products when the institution
+ # offers it, but /liabilities/get then fails with ADDITIONAL_CONSENT_REQUIRED.
+ @plaid_item.update!(
+ available_products: [ "liabilities" ],
+ billed_products: [ "transactions" ],
+ raw_payload: { "consented_products" => [ "transactions", "investments" ] }
+ )
+
+ @snapshot.expects(:accounts).returns([
+ OpenStruct.new(
+ account_id: "123",
+ type: "credit",
+ subtype: "credit card"
+ )
+ ]).at_least_once
+
+ @plaid_provider.expects(:get_transactions).with(@plaid_item.access_token, next_cursor: nil).returns(
+ OpenStruct.new(
+ added: [],
+ modified: [],
+ removed: [],
+ cursor: "test_cursor_1"
+ )
+ ).once
+ @plaid_provider.expects(:get_item_investments).never
+ @plaid_provider.expects(:get_item_liabilities).never
+
+ @snapshot.get_account_data("123")
+ end
end