Files
sure/test/models/account_provider_test.rb
soky srm 4fb0a3856e Providers factory (#250)
* Implement providers factory

* Multiple providers sync support

- Proper Multi-Provider Syncing: When you click sync on an account with multiple providers (e.g., both Plaid and SimpleFin), all provider items are synced
- Better API: The existing account.providers method already returns all providers, and account.provider returns the first one for backward compatibility
- Correct Holdings Deletion Logic: Holdings can only be deleted if ALL providers allow it, preventing accidental deletions that would be recreated on next sync
TODO: validate this is the way we want to go? We would need to check holdings belong to which account, and then check provider allows deletion. More complex
- Database Constraints: The existing validations ensure an account can have at most one provider of each type (one PlaidAccount, one SimplefinAccount, etc.)

* Add generic provider_import_adapter

* Finish unified import strategy

* Update app/models/plaid_account.rb

Co-authored-by: coderabbitai[bot] <136622811+coderabbitai[bot]@users.noreply.github.com>
Signed-off-by: soky srm <sokysrm@gmail.com>

* Update app/models/provider/factory.rb

Co-authored-by: coderabbitai[bot] <136622811+coderabbitai[bot]@users.noreply.github.com>
Signed-off-by: soky srm <sokysrm@gmail.com>

* Fix account linked by plaid_id instead of external_id

* Parse numerics to BigDecimal

Parse numerics to BigDecimal before computing amount; guard nils.

Avoid String * String and float drift; also normalize date.

* Fix incorrect usage of assert_raises.

* Fix linter

* Fix processor test.

* Update current_balance_manager.rb

* Test fixes

* Fix plaid linked account test

* Add support for holding per account_provider

* Fix proper account access

Also fix account deletion for simpefin too

* FIX match tests for consistency

* Some more factory updates

* Fix account schema for multipe providers

  Can do:
  - Account #1 → PlaidAccount + SimplefinAccount (multiple different providers)
  - Account #2 → PlaidAccount only
  - Account #3 → SimplefinAccount only

  Cannot do:
  - Account #1 → PlaidAccount + PlaidAccount (duplicate provider type)
  - PlaidAccount #123 → Account #1 + Account #2 (provider linked to multiple accounts)

* Fix account setup

- An account CAN have multiple providers (the schema shows account_providers with unique index on [account_id, provider_type])
  - Each provider should maintain its own separate entries
  - We should NOT update one provider's entry when another provider syncs

* Fix linter and guard migration

* FIX linter issues.

* Fixes

- Remove duplicated index
- Pass account_provider_id
- Guard holdings call to avoid NoMethodError

* Update schema and provider import fix

* Plaid doesn't allow holdings deletion

* Use ClimateControl for proper env setup

* No need for this in .git

---------

Signed-off-by: soky srm <sokysrm@gmail.com>
Co-authored-by: coderabbitai[bot] <136622811+coderabbitai[bot]@users.noreply.github.com>
Co-authored-by: Juan José Mata <juanjo.mata@gmail.com>
2025-10-28 19:32:27 +01:00

133 lines
3.5 KiB
Ruby

require "test_helper"
class AccountProviderTest < ActiveSupport::TestCase
setup do
@account = accounts(:depository)
@family = families(:dylan_family)
# Create provider items
@plaid_item = PlaidItem.create!(
family: @family,
plaid_id: "test_plaid_item",
access_token: "test_token",
name: "Test Bank"
)
@simplefin_item = SimplefinItem.create!(
family: @family,
name: "Test SimpleFin Bank",
access_url: "https://example.com/access"
)
# Create provider accounts
@plaid_account = PlaidAccount.create!(
plaid_item: @plaid_item,
name: "Plaid Checking",
plaid_id: "plaid_123",
plaid_type: "depository",
currency: "USD",
current_balance: 1000
)
@simplefin_account = SimplefinAccount.create!(
simplefin_item: @simplefin_item,
name: "SimpleFin Checking",
account_id: "sf_123",
account_type: "checking",
currency: "USD",
current_balance: 2000
)
end
test "allows an account to have multiple different provider types" do
# Should be able to link both Plaid and SimpleFin to same account
plaid_provider = AccountProvider.create!(
account: @account,
provider: @plaid_account
)
simplefin_provider = AccountProvider.create!(
account: @account,
provider: @simplefin_account
)
assert_equal 2, @account.account_providers.count
assert_includes @account.account_providers, plaid_provider
assert_includes @account.account_providers, simplefin_provider
end
test "prevents duplicate provider type for same account" do
# Create first PlaidAccount link
AccountProvider.create!(
account: @account,
provider: @plaid_account
)
# Create another PlaidAccount
another_plaid_account = PlaidAccount.create!(
plaid_item: @plaid_item,
name: "Another Plaid Account",
plaid_id: "plaid_456",
plaid_type: "savings",
currency: "USD",
current_balance: 5000
)
# Should not be able to link another PlaidAccount to same account
duplicate_provider = AccountProvider.new(
account: @account,
provider: another_plaid_account
)
assert_not duplicate_provider.valid?
assert_includes duplicate_provider.errors[:account_id], "has already been taken"
end
test "prevents same provider account from linking to multiple accounts" do
# Link provider to first account
AccountProvider.create!(
account: @account,
provider: @plaid_account
)
# Try to link same provider to another account
another_account = accounts(:investment)
duplicate_link = AccountProvider.new(
account: another_account,
provider: @plaid_account
)
assert_not duplicate_link.valid?
assert_includes duplicate_link.errors[:provider_id], "has already been taken"
end
test "adapter method returns correct adapter" do
provider = AccountProvider.create!(
account: @account,
provider: @plaid_account
)
adapter = provider.adapter
assert_kind_of Provider::PlaidAdapter, adapter
assert_equal "plaid", adapter.provider_name
assert_equal @account, adapter.account
end
test "provider_name delegates to adapter" do
plaid_provider = AccountProvider.create!(
account: @account,
provider: @plaid_account
)
simplefin_provider = AccountProvider.create!(
account: accounts(:investment),
provider: @simplefin_account
)
assert_equal "plaid", plaid_provider.provider_name
assert_equal "simplefin", simplefin_provider.provider_name
end
end