The personal finance app for everyone (by everyone)
Ruby
require "test_helper"
class CoinstatsItemsControllerTest < ActionDispatch::IntegrationTest
setup do
sign_in @user = users(:family_admin)
@family = families(:dylan_family)
@coinstats_item = CoinstatsItem.create!(
family: @family,
name: "Test CoinStats Connection",
api_key: "test_api_key_123"
)
ensure_tailwind_build
end
# Helper to wrap data in Provider::Response
def success_response(data)
Provider::Response.new(success?: true, data: data, error: nil)
end
def error_response(message)
Provider::Response.new(success?: false, data: nil, error: Provider::Error.new(message))
end
test "should get new" do
get new_coinstats_item_url
assert_response :success
end
test "should create coinstats item with valid api key" do
# Mock the API key validation
Provider::Coinstats.any_instance.expects(:get_blockchains).returns(success_response([])).once
assert_difference("CoinstatsItem.count", 1) do
post coinstats_items_url, params: {
coinstats_item: {
name: "New CoinStats Connection",
api_key: "valid_api_key"
}
}
end
end
test "should not create coinstats item with invalid api key" do
# Mock the API key validation to fail
Provider::Coinstats.any_instance.expects(:get_blockchains)
.returns(error_response("Invalid API key"))
assert_no_difference("CoinstatsItem.count") do
post coinstats_items_url, params: {
coinstats_item: {
name: "New CoinStats Connection",
api_key: "invalid_api_key"
}
}
end
end
test "invalid create outside a frame redirects to the providers page with a 303" do
assert_no_difference "CoinstatsItem.count" do
post coinstats_items_url, params: { coinstats_item: { name: "New CoinStats Connection", api_key: "" } }
end
assert_response :see_other
assert_redirected_to settings_providers_path
assert_match "can't be blank", flash[:alert]
end
# Redirecting back to Bank sync collapses the open connection row.
test "update from the page re-renders the panel in place" do
patch coinstats_item_url(@coinstats_item),
params: { coinstats_item: { name: "Renamed CoinStats" } },
as: :turbo_stream
assert_turbo_stream action: "replace", target: "coinstats-providers-panel"
assert_includes response.body, %(id="coinstats-providers-panel")
assert_equal "Renamed CoinStats", @coinstats_item.reload.name
end
test "the re-rendered panel leaves the API key field empty instead of echoing the stored key" do
patch coinstats_item_url(@coinstats_item), params: { coinstats_item: { name: "Renamed CoinStats" } }, as: :turbo_stream
assert_select "form[action=?] input[name=?]", coinstats_item_path(@coinstats_item), "coinstats_item[api_key]" do |inputs|
assert_nil inputs.first["value"]
assert_equal I18n.t("coinstats_items.new.api_key_placeholder_update"), inputs.first["placeholder"]
end
assert_not_includes response.body, "test_api_key_123"
end
test "update with the API key left blank changes the other fields and keeps the stored key" do
Provider::Coinstats.any_instance.expects(:get_blockchains).never
patch coinstats_item_url(@coinstats_item),
params: { coinstats_item: { name: "Renamed CoinStats", api_key: "" } },
as: :turbo_stream
assert_turbo_stream action: "replace", target: "coinstats-providers-panel"
@coinstats_item.reload
assert_equal "Renamed CoinStats", @coinstats_item.name
assert_equal "test_api_key_123", @coinstats_item.api_key
end
test "invalid create from the page shows the error in the panel" do
post coinstats_items_url,
params: { coinstats_item: { name: "New CoinStats Connection", api_key: "" } },
as: :turbo_stream
assert_turbo_stream status: :unprocessable_entity, action: "replace", target: "coinstats-providers-panel"
assert_includes response.body, ERB::Util.html_escape("can't be blank")
end
test "create from the page still reloads Bank sync" do
Provider::Coinstats.any_instance.expects(:get_blockchains).returns(success_response([])).once
post coinstats_items_url,
params: { coinstats_item: { name: "New CoinStats Connection", api_key: "valid_api_key" } },
as: :turbo_stream
assert_redirected_to settings_providers_path
end
test "should destroy coinstats item" do
# Schedules for deletion, doesn't actually delete immediately
assert_no_difference("CoinstatsItem.count") do
delete coinstats_item_url(@coinstats_item)
end
assert_redirected_to settings_providers_path
@coinstats_item.reload
assert @coinstats_item.scheduled_for_deletion?
end
test "should sync coinstats item" do
post sync_coinstats_item_url(@coinstats_item)
assert_redirected_to accounts_path
end
test "sync responds to json format" do
post sync_coinstats_item_url(@coinstats_item, format: :json)
assert_response :ok
end
test "should update coinstats item with valid api key" do
Provider::Coinstats.any_instance.expects(:get_blockchains).returns(success_response([])).once
patch coinstats_item_url(@coinstats_item), params: {
coinstats_item: {
name: "Updated Name",
api_key: "new_valid_api_key"
}
}
@coinstats_item.reload
assert_equal "Updated Name", @coinstats_item.name
assert_equal "new_valid_api_key", @coinstats_item.api_key
end
test "should not update coinstats item with invalid api key" do
Provider::Coinstats.any_instance.expects(:get_blockchains)
.returns(error_response("Invalid API key"))
original_name = @coinstats_item.name
patch coinstats_item_url(@coinstats_item), params: {
coinstats_item: {
name: "Updated Name",
api_key: "invalid_api_key"
}
}
@coinstats_item.reload
assert_equal original_name, @coinstats_item.name
end
test "link_wallet requires all parameters" do
post link_wallet_coinstats_items_url, params: {
coinstats_item_id: @coinstats_item.id,
address: "0x123"
# missing blockchain
}
assert_response :unprocessable_entity
end
test "link_wallet with valid params creates accounts" do
balance_data = [
{ coinId: "ethereum", name: "Ethereum", symbol: "ETH", amount: 1.5, price: 2000 }
]
bulk_response = [
{ blockchain: "ethereum", address: "0x123abc", connectionId: "ethereum", balances: balance_data }
]
Provider::Coinstats.any_instance.expects(:get_wallet_balances)
.with("ethereum:0x123abc")
.returns(success_response(bulk_response))
Provider::Coinstats.any_instance.expects(:extract_wallet_balance)
.with(bulk_response, "0x123abc", "ethereum")
.returns(balance_data)
assert_difference("Account.count", 1) do
assert_difference("CoinstatsAccount.count", 1) do
post link_wallet_coinstats_items_url, params: {
coinstats_item_id: @coinstats_item.id,
address: "0x123abc",
blockchain: "ethereum"
}
end
end
assert_redirected_to accounts_path
end
test "link_wallet handles provider errors" do
Provider::Coinstats.any_instance.expects(:get_wallet_balances)
.raises(Provider::Coinstats::Error.new("Invalid API key"))
post link_wallet_coinstats_items_url, params: {
coinstats_item_id: @coinstats_item.id,
address: "0x123abc",
blockchain: "ethereum"
}
assert_response :unprocessable_entity
end
test "link_wallet preserves no tokens found error in English" do
Provider::Coinstats.any_instance.expects(:get_wallet_balances)
.returns(success_response([]))
Provider::Coinstats.any_instance.expects(:extract_wallet_balance)
.returns([])
post link_wallet_coinstats_items_url, params: {
coinstats_item_id: @coinstats_item.id,
address: "0x123abc",
blockchain: "ethereum"
}
assert_response :unprocessable_entity
assert_includes response.body, "No tokens found for wallet"
end
test "link_wallet localizes no tokens found error in German" do
@user.update!(locale: "de")
Provider::Coinstats.any_instance.expects(:get_wallet_balances)
.returns(success_response([]))
Provider::Coinstats.any_instance.expects(:extract_wallet_balance)
.returns([])
post link_wallet_coinstats_items_url, params: {
coinstats_item_id: @coinstats_item.id,
address: "0x123abc",
blockchain: "ethereum"
}
assert_response :unprocessable_entity
assert_includes response.body, "Für dieses Krypto-Wallet wurden keine Token gefunden."
refute_includes response.body, "No tokens found for wallet"
assert_equal "Für dieses Krypto-Wallet wurden keine Token gefunden.",
I18n.t("models.coinstats_item.wallet_linker.no_tokens_found", locale: :de, fallback: false)
end
test "link_exchange filters unexpected connection fields" do
Provider::Coinstats.any_instance.expects(:get_exchanges).returns(success_response([
{
connectionId: "bitvavo",
name: "Bitvavo",
connectionFields: [
{ key: "apiKey", name: "API Key" },
{ key: "apiSecret", name: "API Secret" }
]
}
])).once
linker_result = CoinstatsItem::ExchangeLinker::Result.new(success?: true, created_count: 0, errors: [])
CoinstatsItem::ExchangeLinker.expects(:new).with(
@coinstats_item,
connection_id: "bitvavo",
connection_fields: { "apiKey" => "key", "apiSecret" => "secret" },
name: "Bitvavo"
).returns(stub(link: linker_result))
post link_exchange_coinstats_items_url, params: {
coinstats_item_id: @coinstats_item.id,
exchange_connection_id: "bitvavo",
exchange_connection_name: "Bitvavo",
connection_fields: {
apiKey: " key ",
apiSecret: " secret ",
unexpected: "should_not_be_forwarded"
}
}
assert_redirected_to accounts_path
end
end