Skip to content

armgit / sure

Public Imported from GitHub
0

The personal finance app for everyone (by everyone)

Ruby

Open Binance account setup from Add Account instead of returning to Settings (#4259)

alexcastgonzfba7a4b607af
diff --git a/app/controllers/binance_items_controller.rb b/app/controllers/binance_items_controller.rb
index 093c744291..aaf71a0166 100644
--- a/app/controllers/binance_items_controller.rb
+++ b/app/controllers/binance_items_controller.rb
@@ -58,8 +58,33 @@ class BinanceItemsController < ApplicationController
end
end
+ # Add Account -> Crypto -> Binance lands here. The connection's sync is what
+ # finds the accounts, so open that connection's account setup, as Kraken does.
def select_accounts
- redirect_to settings_providers_path
+ binance_items = Current.family.binance_items.active.ordered.select(&:credentials_configured?)
+ binance_item = if params[:binance_item_id].present?
+ binance_items.find { |item| item.id.to_s == params[:binance_item_id].to_s }
+ elsif binance_items.one?
+ binance_items.first
+ end
+
+ unless binance_item
+ message_key = if params[:binance_item_id].present? && binance_items.any?
+ ".connection_unavailable"
+ elsif binance_items.many?
+ ".select_connection"
+ else
+ ".no_credentials_configured"
+ end
+ if turbo_frame_request?
+ render_frame_redirect(settings_providers_path, alert: t(message_key))
+ else
+ redirect_to settings_providers_path, alert: t(message_key)
+ end
+ return
+ end
+
+ redirect_to setup_accounts_binance_item_path(binance_item, return_to: safe_return_to(params[:return_to])), status: :see_other
end
def link_accounts
@@ -161,6 +186,9 @@ class BinanceItemsController < ApplicationController
end
def setup_accounts
+ # Where Add Account was opened from, carried through the form so finishing
+ # or cancelling goes back there.
+ @return_to = safe_return_to(params[:return_to])
@binance_accounts = @binance_item.binance_accounts
.left_joins(:account_provider)
.where(account_providers: { id: nil })
@@ -245,7 +273,7 @@ class BinanceItemsController < ApplicationController
)
] + Array(flash_notification_stream_items)
else
- redirect_to accounts_path, status: :see_other
+ redirect_to safe_return_to(params[:return_to]) || accounts_path, status: :see_other
end
end
@@ -262,4 +290,16 @@ class BinanceItemsController < ApplicationController
def complete_account_setup_params
params.permit(:sync_start_date, selected_accounts: [])
end
+
+ # Add Account opens select_accounts inside the "modal" turbo frame, and a
+ # plain redirect would be followed within it, losing the page and its
+ # alert. Answer with the frame carrying a redirect stream action instead,
+ # which performs a full-page visit (as PlaidItemsController does).
+ def render_frame_redirect(path, alert:)
+ flash[:alert] = alert
+
+ render html: helpers.turbo_frame_tag(turbo_frame_request_id) {
+ helpers.turbo_stream.action(:redirect, path)
+ }
+ end
end
diff --git a/app/views/binance_items/setup_accounts.html.erb b/app/views/binance_items/setup_accounts.html.erb
index 323f2fb702..1f48170cbb 100644
--- a/app/views/binance_items/setup_accounts.html.erb
+++ b/app/views/binance_items/setup_accounts.html.erb
@@ -20,6 +20,7 @@
turbo_frame: "_top"
},
class: "space-y-6" do |form| %>
+ <%= hidden_field_tag :return_to, @return_to if @return_to %>
<div class="space-y-4">
<div class="bg-surface border border-primary p-4 rounded-lg">
@@ -108,7 +109,7 @@
<%= render DS::Link.new(
text: t(".cancel"),
variant: "secondary",
- href: accounts_path
+ href: @return_to || accounts_path
) %>
</div>
<% end %>
diff --git a/config/locales/views/binance_items/en.yml b/config/locales/views/binance_items/en.yml
index b3cad536a3..c5b566ad12 100644
--- a/config/locales/views/binance_items/en.yml
+++ b/config/locales/views/binance_items/en.yml
@@ -8,6 +8,10 @@ en:
success: Successfully updated Binance configuration.
destroy:
success: Scheduled Binance connection for deletion.
+ select_accounts:
+ connection_unavailable: That Binance connection isn't available. Choose one in Provider Settings.
+ no_credentials_configured: Connect Binance in Provider Settings before adding its accounts.
+ select_connection: Choose a Binance connection in Provider Settings.
setup_accounts:
title: Import Binance Account
subtitle: Select which portfolios to track
diff --git a/test/controllers/binance_items_controller_test.rb b/test/controllers/binance_items_controller_test.rb
index a063b3a9f4..11c1a4d0c6 100644
--- a/test/controllers/binance_items_controller_test.rb
+++ b/test/controllers/binance_items_controller_test.rb
@@ -48,6 +48,82 @@ class BinanceItemsControllerTest < ActionDispatch::IntegrationTest
assert @binance_item.reload.syncing?
end
+ # Add Account -> Crypto -> Binance used to bounce back to Settings even when
+ # the connection's sync had already found accounts waiting to be set up.
+ test "select_accounts opens the account setup of the family's Binance connection" do
+ only_binance_item(@binance_item)
+
+ get select_accounts_binance_items_url
+
+ assert_redirected_to setup_accounts_binance_item_path(@binance_item)
+ assert_response :see_other
+ end
+
+ test "select_accounts sends the user to Settings when no connection has credentials" do
+ @family.binance_items.update_all(scheduled_for_deletion: true)
+
+ get select_accounts_binance_items_url
+
+ assert_redirected_to settings_providers_path
+ assert_equal I18n.t("binance_items.select_accounts.no_credentials_configured"), flash[:alert]
+ end
+
+ test "select_accounts leaves the Add Account modal for Settings at the top level" do
+ @family.binance_items.update_all(scheduled_for_deletion: true)
+
+ get select_accounts_binance_items_url, headers: { "Turbo-Frame" => "modal" }
+
+ # A redirect would be followed inside the frame and the flash lost.
+ assert_response :success
+ assert_select "turbo-frame#modal turbo-stream[action=redirect][target=?]", settings_providers_path
+ assert_equal I18n.t("binance_items.select_accounts.no_credentials_configured"), flash[:alert]
+ end
+
+ test "select_accounts asks which connection when there are several, unless one is named" do
+ only_binance_item(@binance_item)
+ other_item = BinanceItem.create!(family: @family, name: "Second Binance", api_key: "key_2", api_secret: "secret_2")
+
+ get select_accounts_binance_items_url
+
+ assert_redirected_to settings_providers_path
+ assert_equal I18n.t("binance_items.select_accounts.select_connection"), flash[:alert]
+
+ get select_accounts_binance_items_url(binance_item_id: other_item.id)
+
+ assert_redirected_to setup_accounts_binance_item_path(other_item)
+ end
+
+ test "select_accounts says the named connection is unavailable when another one exists" do
+ only_binance_item(@binance_item)
+ get select_accounts_binance_items_url(binance_item_id: SecureRandom.uuid)
+
+ assert_redirected_to settings_providers_path
+ assert_equal I18n.t("binance_items.select_accounts.connection_unavailable"), flash[:alert]
+ end
+
+ test "select_accounts carries a safe return_to into setup and drops an unsafe one" do
+ only_binance_item(@binance_item)
+ get select_accounts_binance_items_url(return_to: "/imports/123/confirm")
+ assert_redirected_to setup_accounts_binance_item_path(@binance_item, return_to: "/imports/123/confirm")
+
+ get select_accounts_binance_items_url(return_to: "//evil.example")
+ assert_redirected_to setup_accounts_binance_item_path(@binance_item)
+ end
+
+ test "setup_accounts keeps return_to for finishing and cancelling" do
+ get setup_accounts_binance_item_url(@binance_item, return_to: "/imports/123/confirm")
+
+ assert_response :success
+ assert_select "input[type=hidden][name=return_to][value=?]", "/imports/123/confirm"
+ assert_select "a[href=?]", "/imports/123/confirm", text: I18n.t("binance_items.setup_accounts.cancel")
+ end
+
+ test "complete_account_setup returns to return_to" do
+ post complete_account_setup_binance_item_url(@binance_item), params: { return_to: "/imports/123/confirm" }
+
+ assert_redirected_to "/imports/123/confirm"
+ end
+
test "should show setup_accounts page" do
get setup_accounts_binance_item_url(@binance_item)
assert_response :success
@@ -255,4 +331,10 @@ class BinanceItemsControllerTest < ActionDispatch::IntegrationTest
get select_existing_account_binance_items_url, params: { account_id: account.id }
assert_response :success
end
+
+ private
+ # The binance_items fixtures add two more credentialed connections.
+ def only_binance_item(item)
+ @family.binance_items.where.not(id: item.id).update_all(scheduled_for_deletion: true)
+ end
end