Files
sure/test/controllers/indexa_capital_items_controller_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

153 lines
4.4 KiB
Ruby

# frozen_string_literal: true
require "test_helper"
class IndexaCapitalItemsControllerTest < ActionDispatch::IntegrationTest
include ActiveJob::TestHelper
setup do
sign_in users(:family_admin)
@family = families(:dylan_family)
@item = indexa_capital_items(:configured_with_token)
end
test "should create indexa_capital_item with api_token" do
assert_difference("IndexaCapitalItem.count", 1) do
post indexa_capital_items_url, params: {
indexa_capital_item: { name: "New Connection", api_token: "new_token" }
}
end
assert_redirected_to settings_providers_path
end
test "should update indexa_capital_item" do
patch indexa_capital_item_url(@item), params: {
indexa_capital_item: { name: "Updated Name" }
}
assert_redirected_to settings_providers_path
@item.reload
assert_equal "Updated Name", @item.name
end
test "should destroy indexa_capital_item" do
assert_difference("IndexaCapitalItem.count", 0) do # doesn't delete immediately
delete indexa_capital_item_url(@item)
end
assert_redirected_to settings_providers_path
@item.reload
assert @item.scheduled_for_deletion?
end
test "should sync indexa_capital_item" do
post sync_indexa_capital_item_url(@item)
assert_response :redirect
end
test "should show setup_accounts page" do
get setup_accounts_indexa_capital_item_url(@item)
assert_response :success
end
test "complete_account_setup creates accounts for selected indexa_capital_accounts" do
ica = indexa_capital_accounts(:mutual_fund)
assert_difference "Account.count", 1 do
post complete_account_setup_indexa_capital_item_url(@item), params: {
accounts: {
ica.id => { account_type: "investment", subtype: "brokerage" }
}
}
end
assert_response :redirect
ica.reload
assert_not_nil ica.current_account
assert_equal "Investment", ica.current_account.accountable_type
end
test "complete_account_setup skips already linked accounts" do
ica = indexa_capital_accounts(:mutual_fund)
# Pre-link
account = Account.create!(
family: @family, name: "Existing Fund", balance: 1000, currency: "EUR",
accountable: Investment.new
)
AccountProvider.create!(account: account, provider: ica)
assert_no_difference "Account.count" do
post complete_account_setup_indexa_capital_item_url(@item), params: {
accounts: {
ica.id => { account_type: "investment" }
}
}
end
end
test "complete_account_setup with all skipped redirects to setup" do
ica = indexa_capital_accounts(:mutual_fund)
assert_no_difference "Account.count" do
post complete_account_setup_indexa_capital_item_url(@item), params: {
accounts: {
ica.id => { account_type: "skip" }
}
}
end
assert_redirected_to setup_accounts_indexa_capital_item_path(@item)
end
test "cannot access other family's indexa_capital_item" do
other_item = indexa_capital_items(:configured_with_credentials)
get setup_accounts_indexa_capital_item_url(other_item)
assert_response :not_found
end
test "link_existing_account links manual account to indexa_capital_account" do
manual_account = Account.create!(
family: @family, name: "Manual Investment", balance: 0, currency: "EUR",
accountable: Investment.new
)
ica = indexa_capital_accounts(:pension_plan)
assert_difference "AccountProvider.count", 1 do
post link_existing_account_indexa_capital_items_url, params: {
account_id: manual_account.id,
indexa_capital_account_id: ica.id
}
end
ica.reload
assert_equal manual_account, ica.current_account
end
test "link_existing_account rejects already linked provider account" do
ica = indexa_capital_accounts(:mutual_fund)
# Pre-link
account = Account.create!(
family: @family, name: "Linked Fund", balance: 1000, currency: "EUR",
accountable: Investment.new
)
AccountProvider.create!(account: account, provider: ica)
target_account = Account.create!(
family: @family, name: "Target", balance: 0, currency: "EUR",
accountable: Investment.new
)
assert_no_difference "AccountProvider.count" do
post link_existing_account_indexa_capital_items_url, params: {
account_id: target_account.id,
indexa_capital_account_id: ica.id
}
end
end
end