Skip to content

armgit / sure

Public Imported from GitHub
0

The personal finance app for everyone (by everyone)

Ruby

fixes Trading212 bugs reported in #3087 (#3090)

Neeraj Vermac3ef3d7dd82d
diff --git a/app/components/trading212/account_editor_component.html.erb b/app/components/trading212/account_editor_component.html.erb
new file mode 100644
index 0000000000..d43355fee5
--- /dev/null
+++ b/app/components/trading212/account_editor_component.html.erb
@@ -0,0 +1,107 @@
+<% items.each do |item| %>
+ <%= render DS::Disclosure.new(variant: :card) do |disclosure| %>
+ <% disclosure.with_summary_content do %>
+ <div class="flex items-center justify-between gap-3 w-full">
+ <div class="min-w-0">
+ <p class="font-medium text-primary truncate"><%= item.name %></p>
+ <p class="text-xs text-secondary">
+ <% if item.syncing? %>
+ <%= translation("syncing") %>
+ <% else %>
+ <%= sync_status_summary_for(item) %>
+ <% end %>
+ </p>
+ </div>
+ </div>
+ <% end %>
+
+ <div class="mt-4 space-y-4">
+ <div class="flex flex-wrap justify-end gap-2">
+ <%= render DS::Button.new(
+ text: translation("sync"),
+ icon: "refresh-cw",
+ variant: :outline,
+ href: helpers.sync_trading212_item_path(item),
+ method: :post,
+ disabled: item.syncing?
+ ) %>
+
+ <%= render DS::Button.new(
+ icon: "trash-2",
+ variant: :outline_destructive,
+ href: helpers.trading212_item_path(item),
+ method: :delete,
+ confirm: translation("disconnect_confirm"),
+ aria_label: translation("disconnect")
+ ) %>
+ </div>
+
+ <%= helpers.styled_form_with model: item,
+ url: helpers.trading212_item_path(item),
+ scope: :trading212_item,
+ method: :patch,
+ data: { turbo: true },
+ class: "space-y-3" do |form| %>
+ <%= form.text_field :api_key,
+ label: translation("api_key_label"),
+ placeholder: translation("api_key_placeholder_existing"),
+ type: :password,
+ value: nil %>
+
+ <%= form.text_field :api_secret,
+ label: translation("api_secret_label"),
+ placeholder: translation("api_secret_placeholder_existing"),
+ type: :password,
+ value: nil %>
+
+ <%= form.select :environment,
+ environment_options,
+ { label: translation("environment_label"), selected: item.environment || "live" } %>
+
+ <%= form.select :currency,
+ currency_options,
+ { label: translation("currency_label"), selected: item.currency.presence || @family.currency } %>
+
+ <div class="flex flex-wrap justify-end gap-2">
+ <%= form.submit translation("update_configuration") %>
+ </div>
+ <% end %>
+ </div>
+ <% end %>
+<% end %>
+
+<% if items.any? %>
+ <h3 class="flex items-center gap-2 text-sm font-medium text-primary mt-4">
+ <%= helpers.icon "plus", size: "sm" %>
+ <%= translation("add_account") %>
+ </h3>
+<% end %>
+
+<%= helpers.styled_form_with model: new_item,
+ url: helpers.trading212_items_path,
+ scope: :trading212_item,
+ method: :post,
+ data: { turbo: true },
+ class: "space-y-3" do |form| %>
+ <%= form.text_field :api_key,
+ label: translation("api_key_label"),
+ placeholder: translation("api_key_placeholder_new"),
+ type: :password %>
+
+ <%= form.text_field :api_secret,
+ label: translation("api_secret_label"),
+ placeholder: translation("api_secret_placeholder_new"),
+ type: :password %>
+
+ <%= form.select :environment,
+ environment_options,
+ { label: translation("environment_label"), selected: "live" } %>
+
+ <%= form.select :currency,
+ currency_options,
+ { label: translation("currency_label"), selected: @family.currency } %>
+
+ <div class="flex flex-wrap justify-end gap-2">
+ <%= form.submit translation("save_configuration") %>
+ </div>
+<% end %>
diff --git a/app/components/trading212/account_editor_component.rb b/app/components/trading212/account_editor_component.rb
new file mode 100644
index 0000000000..c5b07014b5
--- /dev/null
+++ b/app/components/trading212/account_editor_component.rb
@@ -0,0 +1,64 @@
+class Trading212::AccountEditorComponent < ApplicationComponent
+ def initialize(items: nil, family:)
+ @items = items || family.trading212_items.ordered
+ @family = family
+ @account_counts_by_item_id = preload_account_counts
+ end
+
+ attr_reader :items
+
+ def new_item
+ @new_item ||= @family.trading212_items.build
+ end
+
+ def sync_status_summary_for(item)
+ counts = @account_counts_by_item_id.fetch(item.id, { total: 0, linked: 0, unlinked: 0 })
+
+ if counts[:total].zero?
+ I18n.t("trading212_items.sync_status.no_accounts")
+ elsif counts[:unlinked].zero?
+ I18n.t("trading212_items.sync_status.all_linked", count: counts[:linked])
+ else
+ I18n.t("trading212_items.sync_status.partial", linked: counts[:linked], unlinked: counts[:unlinked])
+ end
+ end
+
+ def environment_options
+ [
+ [ translation("environment_live"), "live" ],
+ [ translation("environment_demo"), "demo" ]
+ ]
+ end
+
+ def currency_options
+ Money::Currency.as_options.map { |currency| [ "#{currency.name} (#{currency.iso_code})", currency.iso_code ] }
+ end
+
+ def translation(key, **options)
+ helpers.t("settings.providers.trading212_panel.#{key}", **options)
+ end
+
+ private
+
+ def preload_account_counts
+ item_ids = items.map(&:id).compact
+ return {} if item_ids.empty?
+
+ Trading212Account
+ .left_joins(:account_provider)
+ .where(trading212_item_id: item_ids)
+ .group(:trading212_item_id)
+ .pluck(
+ :trading212_item_id,
+ Arel.sql("COUNT(trading212_accounts.id)"),
+ Arel.sql("COUNT(account_providers.id)")
+ )
+ .each_with_object({}) do |(item_id, total, linked), counts|
+ counts[item_id] = {
+ total: total,
+ linked: linked,
+ unlinked: total - linked
+ }
+ end
+ end
+end
diff --git a/app/controllers/accounts_controller.rb b/app/controllers/accounts_controller.rb
index 05748dbfa5..0bc1359fa8 100644
--- a/app/controllers/accounts_controller.rb
+++ b/app/controllers/accounts_controller.rb
@@ -34,6 +34,7 @@ class AccountsController < ApplicationController
)
@binance_items = visible_provider_items(family.binance_items.ordered.with_attached_logo.includes(:binance_accounts, :accounts))
@kraken_items = visible_provider_items(family.kraken_items.ordered.with_attached_logo.includes(:kraken_accounts, :accounts))
+ @trading212_items = visible_provider_items(family.trading212_items.ordered.with_attached_logo.includes(:trading212_accounts)).sort_by(&:created_at)
@questrade_items = visible_provider_items(family.questrade_items.ordered.with_attached_logo.includes(:accounts, questrade_accounts: :account_provider))
@wise_items = visible_provider_items(family.wise_items.ordered.includes(:wise_accounts, :accounts))
@trade_republic_items = visible_provider_items(
@@ -370,6 +371,7 @@ class AccountsController < ApplicationController
@sophtron_items,
@binance_items,
@kraken_items,
+ @trading212_items,
@questrade_items,
@wise_items,
@trade_republic_items,
diff --git a/app/controllers/settings/providers_controller.rb b/app/controllers/settings/providers_controller.rb
index 2162fcfc93..0e755ebd24 100644
--- a/app/controllers/settings/providers_controller.rb
+++ b/app/controllers/settings/providers_controller.rb
@@ -314,7 +314,7 @@ class Settings::ProvidersController < ApplicationController
@coinbase_items = Current.family.coinbase_items.ordered # Coinbase panel needs name and sync info for status display
@snaptrade_items = Current.family.snaptrade_items.ordered
@ibkr_items = Current.family.ibkr_items.ordered.select(:id)
- @trading212_items = Current.family.trading212_items.ordered.select(:id)
+ @trading212_items = Current.family.trading212_items.ordered
@trade_republic_items = Current.family.trade_republic_items.ordered.select(:id)
@indexa_capital_items = Current.family.indexa_capital_items.ordered.select(:id)
@binance_items = Current.family.binance_items.active.ordered
diff --git a/app/controllers/trading212_items_controller.rb b/app/controllers/trading212_items_controller.rb
index 7d5b6ed09e..db7a41d815 100644
--- a/app/controllers/trading212_items_controller.rb
+++ b/app/controllers/trading212_items_controller.rb
@@ -4,7 +4,7 @@ class Trading212ItemsController < ApplicationController
def create
@trading212_item = Current.family.trading212_items.build(trading212_item_params)
- @trading212_item.name ||= t("trading212_items.defaults.name")
+ @trading212_item.name ||= t("settings.providers.trading212_panel.default_account_name")
@trading212_item.currency ||= Current.family.currency
if @trading212_item.save
diff --git a/app/helpers/settings_helper.rb b/app/helpers/settings_helper.rb
index 4fa638fa55..16d2e388c9 100644
--- a/app/helpers/settings_helper.rb
+++ b/app/helpers/settings_helper.rb
@@ -99,10 +99,12 @@ module SettingsHelper
when "onchain_wallet"
return { status: :off } unless @onchain_wallet_items&.any?
sync_based_summary(key)
+ when "trading212"
+ return { status: :off } unless @trading212_items&.any?
+ sync_based_summary(key)
when "snaptrade"
configured_item = @snaptrade_items&.find(&:oauth_configured?)
return { status: :off } unless configured_item
-
sync_based_summary(key)
when "ibkr"
return { status: :off } unless @ibkr_items&.any?
diff --git a/app/models/provider/trading212.rb b/app/models/provider/trading212.rb
index 5a62cf10ad..bb13e9f33f 100644
--- a/app/models/provider/trading212.rb
+++ b/app/models/provider/trading212.rb
@@ -18,6 +18,7 @@ class Provider::Trading212
LIVE_BASE_URI = "https://live.trading212.com/api/v0".freeze
DEMO_BASE_URI = "https://demo.trading212.com/api/v0".freeze
+ API_PATH_PREFIX = "/api/v0".freeze
MAX_PAGES = 200
PAGE_LIMIT = 50
@@ -86,7 +87,8 @@ class Provider::Trading212
end
def get(path, query: {})
- url = "#{base_uri}#{path}"
+ request_path = path.delete_prefix(API_PATH_PREFIX)
+ url = "#{base_uri}#{request_path}"
response = with_retries(path) do
self.class.get(url, headers: auth_headers, query: query.compact)
end
@@ -95,23 +97,17 @@ class Provider::Trading212
def fetch_all_pages(path)
items = []
- cursor = nil
+ next_page_path = nil
pages_fetched = 0
loop do
- query = { limit: PAGE_LIMIT }
- query[:cursor] = cursor if cursor
-
- data = get(path, query: query)
+ data = next_page_path ? get(next_page_path) : get(path, query: { limit: PAGE_LIMIT })
items.concat(Array(data["items"]))
- next_page = data["nextPagePath"]
+ next_page_path = data["nextPagePath"]
pages_fetched += 1
- break if next_page.nil? || pages_fetched >= MAX_PAGES
-
- cursor = extract_cursor(next_page)
- break if cursor.nil?
+ break if next_page_path.nil? || pages_fetched >= MAX_PAGES
sleep(10) # 6 req/min limit on history endpoint
end
@@ -119,14 +115,6 @@ class Provider::Trading212
items
end
- def extract_cursor(next_page_path)
- uri = URI.parse("https://placeholder#{next_page_path}")
- params = URI.decode_www_form(uri.query.to_s).to_h
- params["cursor"]
- rescue URI::InvalidURIError
- nil
- end
-
def handle_response(response)
case response.code
when 200, 201
diff --git a/app/models/trading212_account/activities_processor.rb b/app/models/trading212_account/activities_processor.rb
index 75647e9b0d..4c2c8aa5ec 100644
--- a/app/models/trading212_account/activities_processor.rb
+++ b/app/models/trading212_account/activities_processor.rb
@@ -4,7 +4,7 @@ class Trading212Account::ActivitiesProcessor
# T212 transaction types that map to cash flows
DEPOSIT_TYPE = "DEPOSIT".freeze
WITHDRAW_TYPE = "WITHDRAW".freeze
- INTEREST_TYPE = "INTEREST".freeze
+ INTEREST_TYPES = %w[INTEREST INTEREST_ON_FREE_CASH].freeze
FEE_TYPE = "FEE".freeze
def initialize(trading212_account)
@@ -203,7 +203,7 @@ class Trading212Account::ActivitiesProcessor
[ "Contribution", -amount.abs ]
when WITHDRAW_TYPE
[ "Withdrawal", amount.abs ]
- when INTEREST_TYPE
+ when *INTEREST_TYPES
[ "Interest", -amount.abs ]
when FEE_TYPE
[ "Fee", amount.abs ]
diff --git a/app/models/trading212_item.rb b/app/models/trading212_item.rb
index e02f17d5ce..4a4c9fa228 100644
--- a/app/models/trading212_item.rb
+++ b/app/models/trading212_item.rb
@@ -11,6 +11,7 @@ class Trading212Item < ApplicationRecord
end
belongs_to :family
+ has_one_attached :logo, dependent: :purge_later
has_many :trading212_accounts, dependent: :destroy
validates :api_key, presence: true, on: :create
diff --git a/app/views/accounts/index.html.erb b/app/views/accounts/index.html.erb
index cd1d97bcfb..f7cd93f5da 100644
--- a/app/views/accounts/index.html.erb
+++ b/app/views/accounts/index.html.erb
@@ -10,7 +10,7 @@
) %>
<% end %>
-<% if @manual_accounts.empty? && @plaid_items.empty? && @simplefin_items.empty? && @lunchflow_items.empty? && @redbark_items.empty? && @akahu_items.empty? && @up_items.empty? && @monobank_items.empty? && @enable_banking_items.empty? && @coinstats_items.empty? && @coinbase_items.empty? && @mercury_items.empty? && @brex_items.empty? && @ibkr_items.empty? && @snaptrade_items.empty? && @indexa_capital_items.empty? && @sophtron_items.empty? && @binance_items.empty? && @kraken_items.empty? && @questrade_items.empty? && @wise_items.empty? && @onchain_wallet_items.empty? && @trade_republic_items.empty? %>
+<% if @manual_accounts.empty? && @plaid_items.empty? && @simplefin_items.empty? && @lunchflow_items.empty? && @redbark_items.empty? && @akahu_items.empty? && @up_items.empty? && @monobank_items.empty? && @enable_banking_items.empty? && @coinstats_items.empty? && @coinbase_items.empty? && @mercury_items.empty? && @brex_items.empty? && @ibkr_items.empty? && @snaptrade_items.empty? && @indexa_capital_items.empty? && @sophtron_items.empty? && @binance_items.empty? && @kraken_items.empty? && @questrade_items.empty? && @wise_items.empty? && @onchain_wallet_items.empty? && @trade_republic_items.empty? && @trading212_items.empty? %>
<%= render "empty" %>
<% else %>
<div class="space-y-2">
@@ -84,6 +84,10 @@
<%= render @kraken_items.sort_by(&:created_at) %>
<% end %>
+ <% if @trading212_items.any? %>
+ <%= render @trading212_items %>
+ <% end %>
+
<% if @wise_items.any? %>
<%= render @wise_items.sort_by(&:created_at) %>
<% end %>
diff --git a/app/views/settings/providers/_trading212_panel.html.erb b/app/views/settings/providers/_trading212_panel.html.erb
index 0d0a5eae5f..0ab1877531 100644
--- a/app/views/settings/providers/_trading212_panel.html.erb
+++ b/app/views/settings/providers/_trading212_panel.html.erb
@@ -11,72 +11,8 @@
t(".steps.step_3")
] %>
- <%
- trading212_item = Current.family.trading212_items.first_or_initialize
- is_new_record = trading212_item.new_record?
- %>
-
- <% if trading212_item.persisted? %>
- <div class="flex flex-wrap justify-end gap-2">
- <%= button_to sync_trading212_item_path(trading212_item),
- method: :post,
- class: "inline-flex items-center gap-1 px-3 py-1.5 text-sm font-medium text-secondary hover:text-primary border border-secondary rounded-lg hover:border-primary",
- disabled: trading212_item.syncing? do %>
- <%= icon "refresh-cw", size: "sm" %>
- <%= t(".sync") %>
- <% end %>
-
- <%= button_to trading212_item_path(trading212_item),
- method: :delete,
- class: "inline-flex items-center gap-1 px-3 py-1.5 text-sm font-medium text-destructive hover:bg-destructive/10 rounded-lg",
- data: { turbo_confirm: t(".disconnect_confirm") } do %>
- <%= icon "trash-2", size: "sm" %>
- <% end %>
- </div>
- <% end %>
-
- <%= styled_form_with model: trading212_item,
- url: is_new_record ? trading212_items_path : trading212_item_path(trading212_item),
- scope: :trading212_item,
- method: is_new_record ? :post : :patch,
- data: { turbo: true },
- class: "space-y-3" do |form| %>
- <%= form.text_field :api_key,
- label: t(".api_key_label"),
- placeholder: is_new_record ? t(".api_key_placeholder_new") : t(".api_key_placeholder_existing"),
- type: :password,
- value: nil %>
-
- <%= form.text_field :api_secret,
- label: t(".api_secret_label"),
- placeholder: is_new_record ? t(".api_secret_placeholder_new") : t(".api_secret_placeholder_existing"),
- type: :password,
- value: nil %>
-
- <%= form.select :environment,
- [ [ t(".environment_live"), "live" ], [ t(".environment_demo"), "demo" ] ],
- { label: t(".environment_label"), selected: trading212_item.environment || "live" } %>
-
- <%= form.select :currency,
- Money::Currency.as_options.map { |c| [ "#{c.name} (#{c.iso_code})", c.iso_code ] },
- { label: t(".currency_label"), selected: trading212_item.currency.presence || Current.family.currency } %>
-
- <div class="flex flex-wrap justify-end gap-2">
- <%= form.submit(is_new_record ? t(".save_configuration") : t(".update_configuration")) %>
- </div>
- <% end %>
-
- <div class="flex items-center gap-2">
- <% if trading212_item.persisted? && trading212_item.credentials_configured? %>
- <div class="w-2 h-2 bg-success rounded-full"></div>
- <p class="text-sm text-secondary">
- <%= t(".status_configured_prefix", summary: trading212_item.sync_status_summary) %>
- <%= link_to t(".accounts_tab"), accounts_path, class: "link" %>
- <%= t(".status_configured_suffix") %>
- </p>
- <% else %>
- <div class="w-2 h-2 bg-surface-inset rounded-full"></div>
- <p class="text-sm text-secondary"><%= t(".not_configured") %></p>
- <% end %>
- </div>
+ <%= render Trading212::AccountEditorComponent.new(
+ items: local_assigns[:trading212_items] || @trading212_items,
+ family: Current.family
+ ) %>
</div>
diff --git a/app/views/trading212_items/_trading212_item.html.erb b/app/views/trading212_items/_trading212_item.html.erb
index 70888978ae..62f26dde76 100644
--- a/app/views/trading212_items/_trading212_item.html.erb
+++ b/app/views/trading212_items/_trading212_item.html.erb
@@ -83,8 +83,9 @@
<% unless trading212_item.scheduled_for_deletion? %>
<div class="space-y-4 mt-4">
- <% if trading212_item.accounts.any? %>
- <%= render "accounts/index/account_groups", accounts: trading212_item.accounts %>
+ <% visible_accounts = trading212_item.accounts & Current.user.accessible_accounts.to_a %>
+ <% if visible_accounts.any? %>
+ <%= render "accounts/index/account_groups", accounts: visible_accounts %>
<% end %>
<% stats = trading212_item.syncs.ordered.first&.sync_stats || {} %>
diff --git a/config/locales/views/settings/de.yml b/config/locales/views/settings/de.yml
index dafaa7cee7..76f3a2a53a 100644
--- a/config/locales/views/settings/de.yml
+++ b/config/locales/views/settings/de.yml
@@ -490,7 +490,9 @@ de:
api_secret_placeholder_existing: Leer lassen, um den bisherigen Secret Key zu behalten
api_secret_placeholder_new: Trading-212-Secret-Key einfügen
currency_label: Kontowährung
+ disconnect: Trading-212-Konto trennen
disconnect_confirm: Trading 212 trennen?
+ default_account_name: Trading-212-Konto
environment_demo: Demokonto
environment_label: Umgebung
environment_live: Echtes Konto
@@ -503,6 +505,8 @@ de:
step_2: Erzeug einen neuen API-Key – Trading 212 zeigt dir eine API ID und einen Secret Key.
step_3: Füg beides unten ein, wähl Umgebung und Kontowährung und speichere.
sync: Sync
+ syncing: Wird synchronisiert...
+ add_account: Weiteres Konto hinzufügen
update_configuration: Konfiguration aktualisieren
trade_republic_panel:
encryption_warning:
diff --git a/config/locales/views/settings/en.yml b/config/locales/views/settings/en.yml
index 32ad09e9f7..3ac6a230a4 100644
--- a/config/locales/views/settings/en.yml
+++ b/config/locales/views/settings/en.yml
@@ -738,7 +738,11 @@ en:
save_configuration: Save Configuration
update_configuration: Update Configuration
sync: Sync
+ syncing: Syncing...
+ disconnect: Disconnect Trading 212 account
disconnect_confirm: Disconnect Trading 212?
+ default_account_name: Trading 212 Account
+ add_account: Add another account
status_configured_prefix: "Configured. Visit the"
accounts_tab: Accounts
status_configured_suffix: tab to manage discovered accounts.
diff --git a/config/locales/views/settings/zh-TW.yml b/config/locales/views/settings/zh-TW.yml
index 6b50cfe05e..f6b88a787e 100644
--- a/config/locales/views/settings/zh-TW.yml
+++ b/config/locales/views/settings/zh-TW.yml
@@ -624,7 +624,11 @@ zh-TW:
save_configuration: 儲存設定
update_configuration: 更新設定
sync: 同步
+ syncing: 同步中...
+ disconnect: 中斷 Trading 212 帳戶連線
disconnect_confirm: 要中斷 Trading 212 連線嗎?
+ default_account_name: Trading 212 帳戶
+ add_account: 新增其他帳戶
status_configured_prefix: 已設定。請前往
accounts_tab: 帳戶管理
status_configured_suffix: 分頁管理已偵測到的帳戶。
diff --git a/test/components/trading212/account_editor_component_test.rb b/test/components/trading212/account_editor_component_test.rb
new file mode 100644
index 0000000000..648f023c07
--- /dev/null
+++ b/test/components/trading212/account_editor_component_test.rb
@@ -0,0 +1,28 @@
+require "test_helper"
+
+class Trading212::AccountEditorComponentTest < ViewComponent::TestCase
+ test "precomputes sync status summaries for all items" do
+ item = trading212_items(:configured_item)
+ trading212_accounts(:main_account).ensure_account_provider!(accounts(:investment))
+ item.trading212_accounts.create!(
+ name: "Trading 212 unlinked account",
+ trading212_account_id: "t212_unlinked_component_test",
+ currency: "USD",
+ current_balance: 1000,
+ cash_balance: 100,
+ raw_positions_payload: [],
+ raw_orders_payload: [],
+ raw_dividends_payload: [],
+ raw_transactions_payload: []
+ )
+
+ component = Trading212::AccountEditorComponent.new(items: [ item ], family: item.family)
+
+ queries = capture_sql_queries do
+ assert_equal I18n.t("trading212_items.sync_status.partial", linked: 1, unlinked: 1),
+ component.sync_status_summary_for(item)
+ end
+
+ assert_empty queries
+ end
+end
diff --git a/test/controllers/accounts_controller_test.rb b/test/controllers/accounts_controller_test.rb
index 9831291baf..43a5f8a3db 100644
--- a/test/controllers/accounts_controller_test.rb
+++ b/test/controllers/accounts_controller_test.rb
@@ -100,6 +100,51 @@ class AccountsControllerTest < ActionDispatch::IntegrationTest
unregister_fake_chain!
end
+ test "index renders trading212 items" do
+ trading212_item = trading212_items(:configured_item)
+ get accounts_url
+ assert_response :success
+ assert_select "##{dom_id(trading212_item)}"
+ end
+
+ test "index renders only accessible accounts for a visible trading212 item" do
+ accessible_account = accounts(:depository)
+ inaccessible_account = accounts(:investment)
+ trading212_item = trading212_items(:configured_item)
+ trading212_accounts(:main_account).ensure_account_provider!(accessible_account)
+ trading212_item.trading212_accounts.create!(
+ name: "Private Trading 212 Account",
+ trading212_account_id: "t212_private_123",
+ currency: "USD",
+ current_balance: 1000,
+ cash_balance: 100,
+ raw_positions_payload: [],
+ raw_orders_payload: [],
+ raw_dividends_payload: [],
+ raw_transactions_payload: []
+ ).ensure_account_provider!(inaccessible_account)
+ sign_in users(:family_member)
+
+ get accounts_url
+
+ assert_response :success
+ assert_select "##{dom_id(trading212_item)}"
+ assert_select "turbo-frame##{dom_id(accessible_account)}", count: 1
+ assert_select "turbo-frame##{dom_id(inaccessible_account)}", count: 0
+ end
+
+ test "index renders only trading212 items with accessible accounts for members" do
+ shared_account = accounts(:credit_card)
+ trading212_accounts(:main_account).ensure_account_provider!(shared_account)
+ sign_in users(:family_member)
+
+ get accounts_url
+
+ assert_response :success
+ assert_select "##{dom_id(trading212_items(:configured_item))}"
+ assert_select "##{dom_id(trading212_items(:pending_setup_item))}", count: 0
+ end
+
test "should get show" do
get account_url(@account)
assert_response :success
diff --git a/test/controllers/trading212_controller_test.rb b/test/controllers/trading212_controller_test.rb
index 2308f0d36f..4d7979fca1 100644
--- a/test/controllers/trading212_controller_test.rb
+++ b/test/controllers/trading212_controller_test.rb
@@ -23,6 +23,20 @@ class Trading212ItemsControllerTest < ActionDispatch::IntegrationTest
assert_redirected_to accounts_path
end
+ test "create uses the settings panel default name when no name is submitted" do
+ post trading212_items_url, params: {
+ trading212_item: {
+ api_key: "new_api_key",
+ api_secret: "new_api_secret",
+ environment: "live",
+ currency: "USD"
+ }
+ }
+
+ assert_redirected_to accounts_path
+ assert_equal I18n.t("settings.providers.trading212_panel.default_account_name"), Trading212Item.order(:created_at).last.name
+ end
+
test "create renders error on invalid params" do
assert_no_difference "Trading212Item.count" do
post trading212_items_url, params: {
diff --git a/test/helpers/settings_helper_test.rb b/test/helpers/settings_helper_test.rb
index 3ee9c41d81..d077f220ed 100644
--- a/test/helpers/settings_helper_test.rb
+++ b/test/helpers/settings_helper_test.rb
@@ -23,4 +23,17 @@ class SettingsHelperTest < ActionView::TestCase
assert_equal({ status: :ok, last_synced_at: nil }, provider_summary("snaptrade"))
end
+
+ test "provider_summary for trading212 reports sync-based status when connected" do
+ @trading212_items = [ OpenStruct.new ]
+ @provider_sync_health = {}
+
+ assert_equal({ status: :ok, last_synced_at: nil }, provider_summary("trading212"))
+ end
+
+ test "provider_summary for trading212 is off without connections" do
+ @trading212_items = []
+
+ assert_equal({ status: :off }, provider_summary("trading212"))
+ end
end
diff --git a/test/models/provider/trading212_test.rb b/test/models/provider/trading212_test.rb
new file mode 100644
index 0000000000..5b75520ae6
--- /dev/null
+++ b/test/models/provider/trading212_test.rb
@@ -0,0 +1,23 @@
+require "test_helper"
+
+class Provider::Trading212Test < ActiveSupport::TestCase
+ test "pagination follows next page path with all query parameters" do
+ provider = Provider::Trading212.new(api_key: "key", api_secret: "secret")
+ next_page_path = "/api/v0/equity/history/transactions?limit=50&cursor=cursor-2&time=2026-09-10T12%3A00%3A00Z"
+
+ provider.expects(:get).with("/equity/history/transactions", query: { limit: 50 }).returns({
+ "items" => [ { "id" => "transaction-1" } ],
+ "nextPagePath" => next_page_path
+ })
+ provider.expects(:get).with(next_page_path).returns({
+ "items" => [ { "id" => "transaction-2" } ],
+ "nextPagePath" => nil
+ })
+ provider.stubs(:sleep)
+
+ assert_equal [
+ { "id" => "transaction-1" },
+ { "id" => "transaction-2" }
+ ], provider.fetch_all_transactions
+ end
+end
diff --git a/test/models/trading212_activities_processor_test.rb b/test/models/trading212_activities_processor_test.rb
index c56834171a..ee1c538229 100644
--- a/test/models/trading212_activities_processor_test.rb
+++ b/test/models/trading212_activities_processor_test.rb
@@ -274,6 +274,29 @@ class Trading212ActivitiesProcessorTest < ActiveSupport::TestCase
assert_equal "Interest", entry.entryable.investment_activity_label
end
+ test "processes INTEREST_ON_FREE_CASH as Interest" do
+ @trading212_account.update!(
+ raw_transactions_payload: [
+ {
+ "reference" => "txn_003_free_cash",
+ "type" => "INTEREST_ON_FREE_CASH",
+ "amount" => "0.03",
+ "currency" => "EUR",
+ "dateTime" => "2026-09-09T01:13:26.766Z"
+ }
+ ]
+ )
+
+ processor = Trading212Account::ActivitiesProcessor.new(@trading212_account)
+ result = processor.process
+
+ assert_equal 1, result[:transactions]
+ entry = @account.entries.find_by(external_id: "trading212_transaction_txn_003_free_cash")
+ assert_not_nil entry
+ assert_equal "Interest", entry.entryable.investment_activity_label
+ assert entry.amount.negative?
+ end
+
test "processes FEE as Fee" do
@trading212_account.update!(
raw_transactions_payload: [