Files
sure/test/models/indexa_capital_account_test.rb
David Gil ba442d5f26 Implement Indexa Capital provider with real API integration (#933)
* Add Indexa Capital provider scaffold

Generate Indexa Capital provider scaffolding and align credential fields with the API authentication requirements.

* Fix PR 926 lint and schema CI failures

* Implement Indexa Capital provider with real API integration

- Rewrite all broken view templates (were meta-ERB from code generator)
- Create missing select_accounts.html.erb template
- Implement real API calls: list_accounts via /users/me, get_holdings
  via /accounts/{number}/fiscal-results, get_account_balance via
  /accounts/{number}/performance
- Add API token auth support (stored token > env token > credentials)
- Add api_token column with encryption support
- Redesign settings panel: API token prominent, credentials collapsible
- Fix account balances display using performance endpoint portfolios
- Fix accounts index empty-state guard missing indexa_capital_items
- Simplify activities fetch job (no activities API endpoint exists)
- Fix i18n interpolation (%%{ -> %{) throughout locale file

* Add tests for Indexa Capital provider integration

- IndexaCapitalItemTest: validations, credentials, scopes, sync status
- IndexaCapitalAccountTest: upsert, holdings, account provider linking
- Provider::IndexaCapitalTest: auth modes, API stubs, error handling
- IndexaCapitalItemsControllerTest: CRUD, setup, linking, authorization
- Fixtures for items (token + credentials) and accounts (mutual + pension)

52 tests, 98 assertions, 0 failures

* Address code review feedback from PR #933

- Fix zero balance bug: use `nil?` instead of `present?` so 0 is stored
- Fix has_indexa_capital_credentials? to check api_token (was ignored)
- Fix build_provider to delegate to Provided concern (was ignoring token)
- Fix IndexaCapital section outside encryption_error guard in settings
- Add account_number sanitization to prevent path traversal in API URLs
- Replace all skipped processor tests with real working tests
- Add zero-balance and path-traversal test coverage

61 tests, 107 assertions, 0 failures

* Address code review round 2: credentials validation, RuboCop, test quality

- Fix RuboCop SpaceInsideArrayLiteralBrackets in credentials check
- Chain where.not calls so all three username/document/password must be present
- Require all three credentials (||) instead of any one (&&) in validate_configuration!
- Move attr_reader to private to avoid exposing credentials publicly
- Parse dates with Date.parse in extract_balance for robustness
- Remove stale TODO and Crypto from supported_account_types
- Order build_provider query deterministically by created_at
- Replace no-op holdings assertion with meaningful assert_difference

* Address code review round 3: JSON parse safety and test precision

- Rescue JSON::ParserError on 2xx responses for clearer error messages
- Fix weak balance assertion: set balance to 0 before processing, assert
  expected value (27093.01 = sum of holdings amounts)

* Include Indexa Capital in automatic family sync

Add indexa_capital_items to Family::Syncer#child_syncables so balances
and holdings refresh on daily auto-sync and login sync, not only on
manual sync button clicks.

---------

Signed-off-by: Juan José Mata <juanjo.mata@gmail.com>
Co-authored-by: Juan José Mata <jjmata@jjmata.com>
Co-authored-by: Juan José Mata <juanjo.mata@gmail.com>
2026-02-08 18:19:37 +01:00

127 lines
3.6 KiB
Ruby

# frozen_string_literal: true
require "test_helper"
class IndexaCapitalAccountTest < ActiveSupport::TestCase
setup do
@family = families(:dylan_family)
@item = indexa_capital_items(:configured_with_token)
@account = indexa_capital_accounts(:mutual_fund)
end
test "belongs to indexa_capital_item" do
assert_equal @item, @account.indexa_capital_item
end
test "validates presence of name" do
@account.name = nil
assert_not @account.valid?
end
test "validates presence of currency" do
@account.currency = nil
assert_not @account.valid?
end
test "upsert_from_indexa_capital! updates from API data" do
data = {
account_number: "NEWACCT1",
name: "New Account",
type: "mutual",
status: "active",
currency: "EUR",
current_balance: 12345.67
}
new_account = @item.indexa_capital_accounts.create!(
name: "Placeholder", currency: "EUR",
indexa_capital_account_id: "NEWACCT1"
)
new_account.upsert_from_indexa_capital!(data)
new_account.reload
assert_equal "NEWACCT1", new_account.indexa_capital_account_id
assert_equal "New Account", new_account.name
assert_equal "mutual", new_account.account_type
assert_equal "active", new_account.account_status
assert_equal 12345.67, new_account.current_balance.to_f
end
test "upsert_from_indexa_capital! without balance does not overwrite existing" do
assert_equal 38905.2136, @account.current_balance.to_f
data = {
account_number: "LPYH3MCQ",
name: "Updated Name",
type: "mutual",
status: "active",
currency: "EUR"
# No current_balance
}
@account.upsert_from_indexa_capital!(data)
@account.reload
assert_equal "Updated Name", @account.name
assert_equal 38905.2136, @account.current_balance.to_f
end
test "upsert_from_indexa_capital! stores zero balance correctly" do
data = {
account_number: "LPYH3MCQ",
name: "Zero Balance Account",
type: "mutual",
status: "active",
currency: "EUR",
current_balance: 0
}
@account.upsert_from_indexa_capital!(data)
@account.reload
assert_equal 0, @account.current_balance.to_f
end
test "upsert_holdings_snapshot! stores holdings data" do
holdings = [ { instrument: { identifier: "IE00BFPM9V94" }, titles: 32, price: 506.32, amount: 16333.96 } ]
@account.upsert_holdings_snapshot!(holdings)
@account.reload
assert_equal 1, @account.raw_holdings_payload.size
assert_not_nil @account.last_holdings_sync
end
test "upsert_holdings_snapshot! skips when empty" do
@account.update!(last_holdings_sync: 1.day.ago)
original_sync = @account.last_holdings_sync
@account.upsert_holdings_snapshot!([])
@account.reload
assert_equal original_sync, @account.last_holdings_sync
end
test "ensure_account_provider! creates link" do
linked_account = Account.create!(
family: @family, name: "My Fund", balance: 1000, currency: "EUR",
accountable: Investment.new
)
assert_nil @account.account_provider
@account.ensure_account_provider!(linked_account)
assert_not_nil @account.account_provider
assert_equal linked_account, @account.account
end
test "ensure_account_provider! is idempotent" do
linked_account = Account.create!(
family: @family, name: "My Fund", balance: 1000, currency: "EUR",
accountable: Investment.new
)
@account.ensure_account_provider!(linked_account)
assert_no_difference "AccountProvider.count" do
@account.ensure_account_provider!(linked_account)
end
end
end