Skip to content

armgit / sure

Public Imported from GitHub
0

The personal finance app for everyone (by everyone)

Ruby

Reuse shared frame redirects in provider controllers and generator (#4281)

Sure Admin (bot)df06937c9824
diff --git a/app/controllers/binance_items_controller.rb b/app/controllers/binance_items_controller.rb
index 9dbc8d5bb1..2fc3ec7e8b 100644
--- a/app/controllers/binance_items_controller.rb
+++ b/app/controllers/binance_items_controller.rb
@@ -76,11 +76,7 @@ class BinanceItemsController < ApplicationController
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
+ frame_redirect_to(settings_providers_path, alert: t(message_key))
return
end
@@ -290,16 +286,4 @@ 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/controllers/coinbase_items_controller.rb b/app/controllers/coinbase_items_controller.rb
index 75002f591a..535d6a0144 100644
--- a/app/controllers/coinbase_items_controller.rb
+++ b/app/controllers/coinbase_items_controller.rb
@@ -84,11 +84,7 @@ class CoinbaseItemsController < ApplicationController
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
+ frame_redirect_to(settings_providers_path, alert: t(message_key))
return
end
@@ -319,16 +315,4 @@ 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/controllers/coinspot_items_controller.rb b/app/controllers/coinspot_items_controller.rb
index f64778d744..5c8b3cbf43 100644
--- a/app/controllers/coinspot_items_controller.rb
+++ b/app/controllers/coinspot_items_controller.rb
@@ -70,11 +70,7 @@ class CoinspotItemsController < ApplicationController
unless coinspot_item
message = coinspot_item_selection_message(account_flow[:credentialed_items])
- if turbo_frame_request?
- render_frame_redirect(settings_providers_path, alert: message)
- else
- redirect_to settings_providers_path, alert: message
- end
+ frame_redirect_to(settings_providers_path, alert: message)
return
end
@@ -334,16 +330,4 @@ class CoinspotItemsController < ApplicationController
encoded_second_character = return_to[1, 3]
%w[%2F %5C].include?(encoded_second_character)
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/controllers/kraken_items_controller.rb b/app/controllers/kraken_items_controller.rb
index f344629ede..d85a69bcd4 100644
--- a/app/controllers/kraken_items_controller.rb
+++ b/app/controllers/kraken_items_controller.rb
@@ -47,11 +47,7 @@ class KrakenItemsController < ApplicationController
unless kraken_item
message = kraken_item_selection_message(account_flow[:credentialed_items])
- if turbo_frame_request?
- render_frame_redirect(settings_providers_path, alert: message)
- else
- redirect_to settings_providers_path, alert: message
- end
+ frame_redirect_to(settings_providers_path, alert: message)
return
end
@@ -221,16 +217,4 @@ class KrakenItemsController < ApplicationController
rescue URI::InvalidURIError
nil
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/controllers/plaid_items_controller.rb b/app/controllers/plaid_items_controller.rb
index 21f1978f0e..d437884926 100644
--- a/app/controllers/plaid_items_controller.rb
+++ b/app/controllers/plaid_items_controller.rb
@@ -284,28 +284,12 @@ class PlaidItemsController < ApplicationController
respond_to do |format|
format.html do
- if turbo_frame_request?
- render_frame_redirect(accounts_path, alert: alert)
- else
- redirect_to accounts_path, alert: alert
- end
+ frame_redirect_to(accounts_path, alert: alert)
end
format.turbo_stream { stream_redirect_to(accounts_path, alert: alert) }
end
end
- # The Link flow is opened inside the "modal" turbo frame. A plain redirect
- # is followed within that frame, so the flash is spent on a response whose
- # body is discarded and the modal just closes. Answer with the frame itself
- # carrying a redirect stream action, which performs a full-page visit.
- 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
-
def safe_parse_plaid_error(error)
JSON.parse(error.response_body.to_s)
rescue JSON::ParserError
diff --git a/lib/generators/provider/family/templates/controller.rb.tt b/lib/generators/provider/family/templates/controller.rb.tt
index 653806539e..60af7c76d6 100644
--- a/lib/generators/provider/family/templates/controller.rb.tt
+++ b/lib/generators/provider/family/templates/controller.rb.tt
@@ -60,7 +60,7 @@ class <%= class_name %>ItemsController < ApplicationController
# Trigger a sync to fetch accounts from the provider
<%= file_name %>_item = Current.family.<%= file_name %>_items.first
unless <%= file_name %>_item&.credentials_configured?
- redirect_to settings_providers_path, alert: t(".no_credentials_configured")
+ frame_redirect_to settings_providers_path, alert: t(".no_credentials_configured")
return
end
@@ -74,7 +74,7 @@ class <%= class_name %>ItemsController < ApplicationController
<%= file_name %>_item = Current.family.<%= file_name %>_items.first
unless <%= file_name %>_item&.credentials_configured?
- redirect_to settings_providers_path, alert: t(".no_credentials_configured")
+ frame_redirect_to settings_providers_path, alert: t(".no_credentials_configured")
return
end
@@ -135,7 +135,7 @@ class <%= class_name %>ItemsController < ApplicationController
@<%= file_name %>_item = Current.family.<%= file_name %>_items.first
unless @<%= file_name %>_item&.credentials_configured?
- redirect_to settings_providers_path, alert: t(".no_credentials_configured")
+ frame_redirect_to settings_providers_path, alert: t(".no_credentials_configured")
return
end
diff --git a/test/lib/generators/provider/family_generator_test.rb b/test/lib/generators/provider/family_generator_test.rb
index fd274d2353..5d4460de2a 100644
--- a/test/lib/generators/provider/family_generator_test.rb
+++ b/test/lib/generators/provider/family_generator_test.rb
@@ -6,6 +6,9 @@ require "generators/provider/family/family_generator"
# SyntaxError in data_enrichment.rb with nothing pointing back at the generator. So each
# case asserts the result actually PARSES, not just that it looks right.
class Provider::FamilyGeneratorTest < ActiveSupport::TestCase
+ module GeneratedControllers
+ end
+
def append(content)
Provider::FamilyGenerator.append_source_enum_entry(content, "gocardless")
end
@@ -170,6 +173,49 @@ class Provider::FamilyGeneratorTest < ActiveSupport::TestCase
assert_match(/if @gocardless_item\.save\n\s+redirect_to settings_providers_path, notice:/, rendered)
end
+ test "generated account entry points escape frames and preserve ordinary redirects" do
+ rendered = render_template("controller.rb.tt", class_name: "Gocardless", file_name: "gocardless",
+ table_name: "gocardless_items", parsed_fields: [])
+ # Evaluate the actual scaffold in an isolated namespace, without installing a
+ # provider or altering the application's routes/models.
+ namespace = GeneratedControllers
+ namespace.module_eval(rendered)
+ controller_class = namespace.const_get(:GocardlessItemsController)
+ family = mock("family")
+ items = mock("items")
+ accounts = mock("accounts")
+ family.stubs(:gocardless_items).returns(items)
+ family.stubs(:accounts).returns(accounts)
+ items.stubs(:first).returns(nil)
+ accounts.stubs(:find).with("account-id").returns(accounts(:depository))
+ Current.stubs(:family).returns(family)
+
+ [ :preload_accounts, :select_accounts, :select_existing_account ].each do |action|
+ [ nil, "modal", "account-picker" ].each do |frame_id|
+ controller = controller_class.new
+ request = ActionDispatch::TestRequest.create
+ request.headers["Turbo-Frame"] = frame_id if frame_id
+ controller.set_request!(request)
+ controller.set_response!(ActionDispatch::TestResponse.new)
+ controller.params = { account_id: "account-id" }
+ controller.stubs(:settings_providers_path).returns("/settings/providers")
+ controller.stubs(:t).with(".no_credentials_configured").returns("Configure a connection first.")
+
+ controller.public_send(action)
+
+ assert_equal "Configure a connection first.", controller.flash[:alert]
+ if frame_id
+ assert_equal 200, controller.response.status
+ html = Nokogiri::HTML.fragment(controller.response.body)
+ assert html.at_css("turbo-frame##{frame_id} turbo-stream[action=redirect][target='/settings/providers']")
+ else
+ assert_equal 302, controller.response.status
+ assert_equal "http://test.host/settings/providers", controller.response.location
+ end
+ end
+ end
+ end
+
# Bank sync builds its connection rows and drawers from FAMILY_PANELS, so a
# section injected into show.html.erb would render outside both.
test "adds the provider to the family panels of the real providers controller" do
diff --git a/test/system/provider_frame_redirect_test.rb b/test/system/provider_frame_redirect_test.rb
new file mode 100644
index 0000000000..0d742811df
--- /dev/null
+++ b/test/system/provider_frame_redirect_test.rb
@@ -0,0 +1,31 @@
+require "application_system_test_case"
+
+class ProviderFrameRedirectTest < ApplicationSystemTestCase
+ test "an ambiguous Kraken connection leaves the modal and shows its alert" do
+ family = families(:dylan_family)
+ family.update!(auto_sync_on_login: false)
+ family.kraken_items.create!(
+ name: "Second Kraken",
+ api_key: "second-key",
+ api_secret: "second-secret"
+ )
+ sign_in users(:family_admin)
+ visit settings_preferences_path
+
+ # Open the actual provider endpoint with the same frame target as Add Account.
+ execute_script(<<~JS)
+ const link = document.createElement("a");
+ link.id = "open-kraken-setup";
+ link.href = #{select_accounts_kraken_items_path.to_json};
+ link.dataset.turboFrame = "modal";
+ link.textContent = "Open Kraken setup";
+ link.style.cssText = "position: fixed; top: 0; left: 0; z-index: 100;";
+ document.body.append(link);
+ JS
+
+ find("#open-kraken-setup").click
+
+ assert_current_path settings_providers_path
+ assert_selector "#notification-tray", text: I18n.t("kraken_items.select_accounts.select_connection")
+ end
+end