Skip to content

armgit / sure

Public Imported from GitHub
0

The personal finance app for everyone (by everyone)

Ruby

Open Coinbase wallet setup from Add Account instead of returning to Settings (#4257)

alexcastgonz3d51baf3a2db
diff --git a/app/controllers/coinbase_items_controller.rb b/app/controllers/coinbase_items_controller.rb
index 8326b26529..5089ad7f9b 100644
--- a/app/controllers/coinbase_items_controller.rb
+++ b/app/controllers/coinbase_items_controller.rb
@@ -66,8 +66,33 @@ class CoinbaseItemsController < ApplicationController
redirect_to settings_providers_path
end
+ # Add Account -> Crypto -> Coinbase lands here. The connection's sync is what
+ # finds the wallets, so open that connection's wallet setup, as Kraken does.
def select_accounts
- redirect_to settings_providers_path
+ coinbase_items = Current.family.coinbase_items.active.ordered.select(&:credentials_configured?)
+ coinbase_item = if params[:coinbase_item_id].present?
+ coinbase_items.find { |item| item.id.to_s == params[:coinbase_item_id].to_s }
+ elsif coinbase_items.one?
+ coinbase_items.first
+ end
+
+ unless coinbase_item
+ message_key = if params[:coinbase_item_id].present? && coinbase_items.any?
+ ".connection_unavailable"
+ elsif coinbase_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_coinbase_item_path(coinbase_item, return_to: safe_return_to(params[:return_to])), status: :see_other
end
def link_accounts
@@ -196,6 +221,9 @@ class CoinbaseItemsController < 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])
# Only show unlinked accounts
@coinbase_accounts = @coinbase_item.coinbase_accounts
.left_joins(:account_provider)
@@ -273,7 +301,7 @@ class CoinbaseItemsController < 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
@@ -291,4 +319,16 @@ class CoinbaseItemsController < ApplicationController
:api_secret
)
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/coinbase_items/setup_accounts.html.erb b/app/views/coinbase_items/setup_accounts.html.erb
index 40cfe5e0f1..39673d4926 100644
--- a/app/views/coinbase_items/setup_accounts.html.erb
+++ b/app/views/coinbase_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">
@@ -96,7 +97,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/coinbase_items/en.yml b/config/locales/views/coinbase_items/en.yml
index 3fec937e47..ed2c3dd23e 100644
--- a/config/locales/views/coinbase_items/en.yml
+++ b/config/locales/views/coinbase_items/en.yml
@@ -8,6 +8,10 @@ en:
success: Successfully updated Coinbase configuration.
destroy:
success: Scheduled Coinbase connection for deletion.
+ select_accounts:
+ connection_unavailable: That Coinbase connection isn't available. Choose one in Provider Settings.
+ no_credentials_configured: Connect Coinbase in Provider Settings before adding its wallets.
+ select_connection: Choose a Coinbase connection in Provider Settings.
setup_accounts:
title: Import Coinbase Wallets
subtitle: Select which wallets to track
diff --git a/test/controllers/coinbase_items_controller_test.rb b/test/controllers/coinbase_items_controller_test.rb
index 2ef13de731..5dc6411e4c 100644
--- a/test/controllers/coinbase_items_controller_test.rb
+++ b/test/controllers/coinbase_items_controller_test.rb
@@ -65,6 +65,77 @@ class CoinbaseItemsControllerTest < ActionDispatch::IntegrationTest
assert @coinbase_item.reload.syncing?
end
+ # Add Account -> Crypto -> Coinbase used to bounce back to Settings even when
+ # the connection's sync had already found wallets waiting to be set up.
+ test "select_accounts opens the wallet setup of the family's Coinbase connection" do
+ get select_accounts_coinbase_items_url
+
+ assert_redirected_to setup_accounts_coinbase_item_path(@coinbase_item)
+ assert_response :see_other
+ end
+
+ test "select_accounts sends the user to Settings when no connection has credentials" do
+ @coinbase_item.update!(scheduled_for_deletion: true)
+
+ get select_accounts_coinbase_items_url
+
+ assert_redirected_to settings_providers_path
+ assert_equal I18n.t("coinbase_items.select_accounts.no_credentials_configured"), flash[:alert]
+ end
+
+ test "select_accounts leaves the Add Account modal for Settings at the top level" do
+ @coinbase_item.update!(scheduled_for_deletion: true)
+
+ get select_accounts_coinbase_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("coinbase_items.select_accounts.no_credentials_configured"), flash[:alert]
+ end
+
+ test "select_accounts asks which connection when there are several, unless one is named" do
+ other_item = CoinbaseItem.create!(family: @family, name: "Second Coinbase", api_key: "key_2", api_secret: "secret_2")
+
+ get select_accounts_coinbase_items_url
+
+ assert_redirected_to settings_providers_path
+ assert_equal I18n.t("coinbase_items.select_accounts.select_connection"), flash[:alert]
+
+ get select_accounts_coinbase_items_url(coinbase_item_id: other_item.id)
+
+ assert_redirected_to setup_accounts_coinbase_item_path(other_item)
+ end
+
+ test "select_accounts says the named connection is unavailable when another one exists" do
+ get select_accounts_coinbase_items_url(coinbase_item_id: SecureRandom.uuid)
+
+ assert_redirected_to settings_providers_path
+ assert_equal I18n.t("coinbase_items.select_accounts.connection_unavailable"), flash[:alert]
+ end
+
+ test "select_accounts carries a safe return_to into setup and drops an unsafe one" do
+ get select_accounts_coinbase_items_url(return_to: "/imports/123/confirm")
+ assert_redirected_to setup_accounts_coinbase_item_path(@coinbase_item, return_to: "/imports/123/confirm")
+
+ get select_accounts_coinbase_items_url(return_to: "//evil.example")
+ assert_redirected_to setup_accounts_coinbase_item_path(@coinbase_item)
+ end
+
+ test "setup_accounts keeps return_to for finishing and cancelling" do
+ get setup_accounts_coinbase_item_url(@coinbase_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("coinbase_items.setup_accounts.cancel")
+ end
+
+ test "complete_account_setup returns to return_to" do
+ post complete_account_setup_coinbase_item_url(@coinbase_item), params: { return_to: "/imports/123/confirm" }
+
+ assert_redirected_to "/imports/123/confirm"
+ end
+
test "should show setup_accounts page" do
get setup_accounts_coinbase_item_url(@coinbase_item)
assert_response :success