Files
sure/test/controllers/trading212_controller_test.rb
jdcdp 849578a84a Add support for trading212 integration in investments sync (#2513)
* Add support for trading212 integration in investments sync

* Respect Trading 212 history rate limits

* Replace hex colors with design tokens

* Changed withdrawal to withdraw

* Address PR review feedback for Trading 212 integration

- Replace all Rails.logger calls with DebugLogEntry.capture across
  syncer, importer, processor, provider, controller, and unlinking
- Fix destroy action to surface unlink errors via DebugLogEntry
- Replace hardcoded 'GBP' currency fallback with Current.family.currency
- Replace hardcoded English status strings in Syncer with i18n
- Add config/initializers/trading212.rb with DEBUG_RAW ENV toggle
- Add comprehensive test coverage: item, account, data_helpers,
  holdings_processor, activities_processor, importer, syncer,
  and controller tests
- Rewrite syncer and controller tests to match actual interfaces

* Fix tests and bugs found during local test execution

- Fix standard_ticker: empty string caused nil.upcase via [].first
- Fix parse_date: DateTime < Date, so when Date caught DateTime first
- Fix ActivitiesProcessor/HoldingsProcessor: missing instruments_map
  in DataHelpers caused NameError when processing dividends
- Fix test: security fixture ticker collision with test data
- Fix test: sync_status_summary i18n matching in assertions
- Fix test: trading212_provider ConfigurationError test path
- Fix test: controller invalid params needs Turbo-Frame header
- Fix test: syncer test uses stubs instead of strict expects

116 tests, 257 assertions, 0 failures, 0 errors

* Keep raw provider response bodies out of exception messages.

* Fix Secrets leak into page HTML, prevents the API key/secret from appearing in the HTML source while keeping the "leave blank to keep existing" UX.

* Address review findings: env gate, sync test, uniqueness test, destroy flow

- Gate TRADING212_DEBUG_RAW behind Rails.env.local? so staging/production
  cannot accidentally enable raw payload logging
- Assert SyncJob enqueue in sync controller test, not just redirect
- Fix cross-item uniqueness test to actually use two different items
- Remove rescue in destroy so unlink failures stop the flow (matching
  Brex/Akahu pattern) instead of proceeding to destroy_later silently

* Add Trading212 tables to db/schema.rb for CI test database

CI runs db:test:prepare which loads db/schema.rb, not migrations.
Without these table definitions, fixture loading fails with
PG::UndefinedTable: relation "trading212_accounts" does not exist.

* Removed lint complaint

* Register Trading212 in ProviderConnectionStatus::PROVIDERS

Fixes CI failure: test_provider_registry_covers_syncable_family_provider_item_associations

* Fixed bad merge

---------

Signed-off-by: jdcdp <47483528+jdcdp@users.noreply.github.com>
Co-authored-by: jdcdp <jdcdp@cdm4.net>
2026-07-25 04:54:30 +02:00

146 lines
4.0 KiB
Ruby

require "test_helper"
class Trading212ItemsControllerTest < ActionDispatch::IntegrationTest
setup do
sign_in @user = users(:family_admin)
@item = trading212_items(:configured_item)
end
# === create ===
test "create redirects to accounts on success" do
assert_difference "Trading212Item.count", 1 do
post trading212_items_url, params: {
trading212_item: {
api_key: "new_api_key",
api_secret: "new_api_secret",
environment: "live",
currency: "USD"
}
}
end
assert_redirected_to accounts_path
end
test "create renders error on invalid params" do
assert_no_difference "Trading212Item.count" do
post trading212_items_url, params: {
trading212_item: {
api_key: "",
api_secret: "",
environment: "live"
}
}, headers: { "Turbo-Frame" => "modal" }
end
assert_response :unprocessable_entity
end
# === update ===
test "update redirects to accounts on success" do
patch trading212_item_url(@item), params: {
trading212_item: {
api_key: "",
api_secret: "",
environment: "demo"
}
}
assert_redirected_to accounts_path
assert_equal "demo", @item.reload.environment
end
# === destroy ===
test "destroy schedules item for deletion" do
delete trading212_item_url(@item)
assert_redirected_to settings_providers_path
assert @item.reload.scheduled_for_deletion?
end
# === sync ===
test "sync triggers sync job" do
@item.update!(status: :good)
assert_enqueued_with(job: SyncJob) do
post sync_trading212_item_url(@item)
end
assert_response :redirect
end
# === complete_account_setup ===
test "complete_account_setup creates investment account and provider link" do
t212_account = trading212_accounts(:main_account)
assert_difference "Account.count", 1 do
assert_difference "AccountProvider.count", 1 do
post complete_account_setup_trading212_item_url(@item), params: {
account_ids: [ t212_account.id ]
}
end
end
created_account = Account.order(created_at: :desc).first
assert_equal "Investment", created_account.accountable_type
assert_redirected_to accounts_path
t212_account.reload
assert_equal created_account, t212_account.current_account
end
test "complete_account_setup returns alert when no accounts selected" do
post complete_account_setup_trading212_item_url(@item), params: {
account_ids: []
}
assert_redirected_to setup_accounts_trading212_item_path(@item)
assert_not_nil flash[:alert]
end
# === link_existing_account ===
test "link_existing_account links manual investment account" do
account = accounts(:investment)
assert_difference "AccountProvider.count", 1 do
post link_existing_account_trading212_items_url, params: {
account_id: account.id,
trading212_account_id: trading212_accounts(:main_account).id
}
end
assert_redirected_to account_path(account)
trading212_accounts(:main_account).reload
assert_equal account, trading212_accounts(:main_account).current_account
end
test "link_existing_account rejects already linked trading212 account" do
original_account = accounts(:investment)
t212_account = trading212_accounts(:main_account)
AccountProvider.create!(account: original_account, provider: t212_account)
assert_no_difference "AccountProvider.count" do
post link_existing_account_trading212_items_url, params: {
account_id: original_account.id,
trading212_account_id: t212_account.id
}
end
assert_redirected_to account_path(original_account)
end
# === select_existing_account ===
test "select_existing_account renders available trading212 accounts" do
get select_existing_account_trading212_items_url, params: { account_id: accounts(:investment).id }
assert_response :success
assert_includes response.body, trading212_accounts(:main_account).name
end
end