Fix crypto subtype for trades api (#1022)

* fix: crypto subtype not persisted by permitting :subtype in CryptosController

* Backfill crypto subtype for existig accounts so Trades API works

* fix: backfill only unlinked cryptos; use raw SQL in migration; deterministic redirect in test

* Update schema.rb for BackfillcryptoSubtypeForTrades migration

---------

Signed-off-by: dataCenter430 <161712630+dataCenter430@users.noreply.github.com>
This commit is contained in:
dataCenter430
2026-02-19 11:51:42 -07:00
committed by GitHub
parent 356d9ebf3a
commit cfadff641f
5 changed files with 81 additions and 2 deletions

View File

@@ -6,5 +6,48 @@ class CryptosControllerTest < ActionDispatch::IntegrationTest
setup do
sign_in @user = users(:family_admin)
@account = accounts(:crypto)
@family = @user.family
end
test "create persists subtype so account supports trades" do
Family.any_instance.stubs(:get_link_token).returns("test-link-token")
assert_difference "@family.accounts.count", 1 do
post cryptos_path, params: {
account: {
name: "Crypto Exchange Account",
balance: 0,
currency: @family.currency,
accountable_type: "Crypto",
accountable_attributes: { subtype: "exchange", tax_treatment: "taxable" }
}
}
end
assert_response :redirect
created = Account.find(URI(response.location).path.split("/").last)
assert_redirected_to created
assert_equal "exchange", created.accountable.subtype, "subtype must be persisted for trades API"
assert created.supports_trades?, "exchange crypto account must support trades"
end
test "update persists subtype" do
@account.accountable.update_column(:subtype, nil)
assert_nil @account.reload.accountable.subtype
refute @account.supports_trades?
patch crypto_path(@account), params: {
account: {
name: @account.name,
balance: @account.balance,
currency: @account.currency,
accountable_attributes: { id: @account.accountable_id, subtype: "exchange", tax_treatment: "taxable" }
}
}
assert_redirected_to @account
@account.reload
assert_equal "exchange", @account.accountable.subtype
assert @account.supports_trades?
end
end

View File

@@ -27,4 +27,10 @@ class CryptoTest < ActiveSupport::TestCase
assert crypto.tax_deferred?
assert_not crypto.tax_exempt?
end
test "supports_trades? is true only for exchange subtype" do
assert Crypto.new(subtype: "exchange").supports_trades?
refute Crypto.new(subtype: "wallet").supports_trades?
refute Crypto.new(subtype: nil).supports_trades?
end
end