The personal finance app for everyone (by everyone)
Ruby
fix(plaid): show Link errors raised inside the modal frame (#3849)
Darrell99bd9140866b
diff --git a/app/controllers/plaid_items_controller.rb b/app/controllers/plaid_items_controller.rbindex e2214af8dc..21f1978f0e 100644--- a/app/controllers/plaid_items_controller.rb+++ b/app/controllers/plaid_items_controller.rb@@ -283,11 +283,29 @@ class PlaidItemsController < ApplicationControlleralert = friendly_link_token_alert(error_code, error_body["error_message"])respond_to do |format|- format.html { redirect_to accounts_path, alert: alert }+ format.html do+ if turbo_frame_request?+ render_frame_redirect(accounts_path, alert: alert)+ else+ redirect_to accounts_path, alert: alert+ end+ endformat.turbo_stream { stream_redirect_to(accounts_path, alert: alert) }endend+ # 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::ParserErrordiff --git a/test/controllers/plaid_items_controller_test.rb b/test/controllers/plaid_items_controller_test.rbindex e1e0f6450e..e46746822d 100644--- a/test/controllers/plaid_items_controller_test.rb+++ b/test/controllers/plaid_items_controller_test.rb@@ -42,6 +42,26 @@ class PlaidItemsControllerTest < ActionDispatch::IntegrationTestassert_equal I18n.t("plaid_items.errors.link_token_generic"), flash[:alert]end+ test "new breaks out of the modal frame so the alert is shown" do+ plaid_provider = mock+ Provider::Registry.stubs(:plaid_provider_for_region).with(:us).returns(plaid_provider)++ error_body = {+ "error_code" => "INVALID_PRODUCT",+ "error_message" => "Your account is not enabled for the following products: [\"transactions\"]."+ }.to_json+ plaid_provider.expects(:get_link_token).raises(+ Plaid::ApiError.new(code: 400, response_body: error_body)+ )++ get new_plaid_item_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=?]", accounts_path+ assert_match(/not enabled for the following products/, flash[:alert])+ end+test "edit redirects with friendly alert when Plaid rejects update link_token request" doplaid_item = plaid_items(:one)error_body = {@@ -58,6 +78,23 @@ class PlaidItemsControllerTest < ActionDispatch::IntegrationTestassert_match(/not enabled for the following products/, flash[:alert])end+ test "edit breaks out of the modal frame so the alert is shown" do+ plaid_item = plaid_items(:one)+ error_body = {+ "error_code" => "INVALID_PRODUCT",+ "error_message" => "Your account is not enabled for the following products: [\"transactions\"]."+ }.to_json+ PlaidItem.any_instance.expects(:get_update_link_token).raises(+ Plaid::ApiError.new(code: 400, response_body: error_body)+ )++ get edit_plaid_item_url(plaid_item), headers: { "Turbo-Frame" => "modal" }++ assert_response :success+ assert_select "turbo-frame#modal turbo-stream[action=redirect][target=?]", accounts_path+ assert_match(/not enabled for the following products/, flash[:alert])+ end+test "edit enables account selection when adding accounts" doplaid_item = plaid_items(:one)PlaidItem.any_instance.expects(:get_update_link_token).with(