The personal finance app for everyone (by everyone)
Ruby
Honor the chosen country when adding an Enable Banking connection (#5334)
Rajkamal075419658f64
diff --git a/app/controllers/enable_banking_items_controller.rb b/app/controllers/enable_banking_items_controller.rbindex 26f4ce6909..2bb682e3ad 100644--- a/app/controllers/enable_banking_items_controller.rb+++ b/app/controllers/enable_banking_items_controller.rb@@ -73,10 +73,11 @@ class EnableBankingItemsController < ApplicationControllerend@new_connection = params[:new_connection] == "true"+ @country_code = requested_country_codebeginprovider = @enable_banking_item.enable_banking_provider- response = provider.get_aspsps(country: @enable_banking_item.country_code)+ response = provider.get_aspsps(country: @country_code)raw_aspsps = response[:aspsps] || response["aspsps"] || []# Sort: non-beta alphabetically, then beta alphabetically@@ -93,6 +94,7 @@ class EnableBankingItemsController < ApplicationController# Initiate authorization for a selected bankdef authorizeaspsp_name = params[:aspsp_name]+ country_code = requested_country_codepsu_type = params[:psu_type].presence || "personal"unless aspsp_name.present?@@ -101,12 +103,12 @@ class EnableBankingItemsController < ApplicationControllerendbegin- pending_connection = pending_connection_for(aspsp_name) if params[:new_connection] == "true"+ pending_connection = pending_connection_for(aspsp_name, country_code) if params[:new_connection] == "true"target_item = if params[:new_connection] == "true"pending_connection || Current.family.enable_banking_items.create!(name: "Enable Banking Connection",aspsp_name: aspsp_name,- country_code: @enable_banking_item.country_code,+ country_code: country_code,application_id: @enable_banking_item.application_id,client_certificate: @enable_banking_item.client_certificate)@@ -206,7 +208,7 @@ class EnableBankingItemsController < ApplicationController# Does NOT create a new item - that happens in authorize when user selects a bankdef new_connection# Redirect to select_bank with a flag indicating this is for a new connection- redirect_to select_bank_enable_banking_item_path(@enable_banking_item, new_connection: true), data: { turbo_frame: "modal" }+ redirect_to select_bank_enable_banking_item_path(@enable_banking_item, new_connection: true, country_code: requested_country_code), data: { turbo_frame: "modal" }end# Re-authorize an expired session@@ -547,14 +549,14 @@ class EnableBankingItemsController < ApplicationController# reused instead. Other banks keep their own connection. Only attempts# started in this browser session qualify: two browsers of one family# sharing a pending connection could both complete on it.- def pending_connection_for(aspsp_name)+ def pending_connection_for(aspsp_name, country_code)Current.family.enable_banking_items.active.where(id: pending_oauth_states.values).where(aspsp_name: aspsp_name,session_id: nil,application_id: @enable_banking_item.application_id,- country_code: @enable_banking_item.country_code,+ country_code: country_code,client_certificate: @enable_banking_item.client_certificate).where.missing(:enable_banking_accounts)@@ -609,6 +611,18 @@ class EnableBankingItemsController < ApplicationControllerpending.is_a?(Hash) ? pending.to_h : {}end+ # A new connection can live in a different country than the item whose+ # credentials it copies, so the caller may choose one. Unknown values fall+ # back to the item's own country.+ # Only a new connection can use another country. Existing-item flows keep the+ # item's own country, since they authorize against that item.+ def requested_country_code+ return @enable_banking_item.country_code unless action_name == "new_connection" || params[:new_connection] == "true"++ code = params[:country_code].to_s.strip.upcase+ EnableBankingItem::COUNTRY_CODES.include?(code) ? code : @enable_banking_item.country_code+ end+def set_enable_banking_item@enable_banking_item = Current.family.enable_banking_items.find(params[:id])enddiff --git a/app/javascript/controllers/enable_banking_country_controller.js b/app/javascript/controllers/enable_banking_country_controller.jsnew file mode 100644index 0000000000..d03546325e--- /dev/null+++ b/app/javascript/controllers/enable_banking_country_controller.js@@ -0,0 +1,14 @@+import { Controller } from "@hotwired/stimulus";++// Carries the country picked in the Enable Banking settings form (saved or+// not) into the "Add Connection" request.+export default class extends Controller {+ static targets = ["select"];++ applyCountry(event) {+ if (!this.hasSelectTarget || !this.selectTarget.value) return;++ const field = event.target.querySelector('input[name="country_code"]');+ if (field) field.value = this.selectTarget.value;+ }+}diff --git a/app/models/enable_banking_item.rb b/app/models/enable_banking_item.rbindex 9c1cee3ac1..ff6d5b6386 100644--- a/app/models/enable_banking_item.rb+++ b/app/models/enable_banking_item.rb@@ -1,6 +1,41 @@class EnableBankingItem < ApplicationRecordinclude Syncable, Provided, Unlinking, Encryptable, DestroyableLater+ COUNTRIES = [+ [ "Austria (AT)", "AT" ],+ [ "Belgium (BE)", "BE" ],+ [ "Bulgaria (BG)", "BG" ],+ [ "Croatia (HR)", "HR" ],+ [ "Cyprus (CY)", "CY" ],+ [ "Czech Republic (CZ)", "CZ" ],+ [ "Denmark (DK)", "DK" ],+ [ "Estonia (EE)", "EE" ],+ [ "Finland (FI)", "FI" ],+ [ "France (FR)", "FR" ],+ [ "Germany (DE)", "DE" ],+ [ "Greece (GR)", "GR" ],+ [ "Hungary (HU)", "HU" ],+ [ "Iceland (IS)", "IS" ],+ [ "Ireland (IE)", "IE" ],+ [ "Italy (IT)", "IT" ],+ [ "Latvia (LV)", "LV" ],+ [ "Liechtenstein (LI)", "LI" ],+ [ "Lithuania (LT)", "LT" ],+ [ "Luxembourg (LU)", "LU" ],+ [ "Malta (MT)", "MT" ],+ [ "Netherlands (NL)", "NL" ],+ [ "Norway (NO)", "NO" ],+ [ "Poland (PL)", "PL" ],+ [ "Portugal (PT)", "PT" ],+ [ "Romania (RO)", "RO" ],+ [ "Slovakia (SK)", "SK" ],+ [ "Slovenia (SI)", "SI" ],+ [ "Spain (ES)", "ES" ],+ [ "Sweden (SE)", "SE" ],+ [ "United Kingdom (GB)", "GB" ]+ ].freeze+ COUNTRY_CODES = COUNTRIES.map(&:last).freeze+enum :status, { good: "good", requires_update: "requires_update" }, default: :good# Encrypt sensitive credentials and raw payloads if ActiveRecord encryption is configureddiff --git a/app/views/enable_banking_items/select_bank.html.erb b/app/views/enable_banking_items/select_bank.html.erbindex 330490f6ea..480c9a279a 100644--- a/app/views/enable_banking_items/select_bank.html.erb+++ b/app/views/enable_banking_items/select_bank.html.erb@@ -30,7 +30,7 @@<div data-bank-search-target="item" data-bank-search="<%= [ aspsp[:name], aspsp[:bic] ].compact_blank.join(" ").downcase(:fold) %>"><%= button_to authorize_enable_banking_item_path(@enable_banking_item),method: :post,- params: { aspsp_name: aspsp[:name], new_connection: @new_connection },+ params: { aspsp_name: aspsp[:name], new_connection: @new_connection, country_code: @country_code },class: "w-full flex items-center gap-4 p-3 rounded-lg border border-primary bg-container hover:bg-subtle transition-colors text-left",data: { turbo: false } do %><% if aspsp[:logo].present? %>diff --git a/app/views/settings/providers/_enable_banking_panel.html.erb b/app/views/settings/providers/_enable_banking_panel.html.erbindex f6ec9d938a..601af9a70a 100644--- a/app/views/settings/providers/_enable_banking_panel.html.erb+++ b/app/views/settings/providers/_enable_banking_panel.html.erb@@ -1,4 +1,4 @@-<div id="enable_banking-providers-panel" class="space-y-4">+<div id="enable_banking-providers-panel" class="space-y-4" data-controller="enable-banking-country"><%eb_link = link_to("Enable Banking", "https://enablebanking.com", target: "_blank", rel: "noopener noreferrer", class: "text-link underline")%>@@ -33,41 +33,9 @@class: "space-y-3" do |form| %><%= form.select :country_code,- options_for_select([- ["Austria (AT)", "AT"],- ["Belgium (BE)", "BE"],- ["Bulgaria (BG)", "BG"],- ["Croatia (HR)", "HR"],- ["Cyprus (CY)", "CY"],- ["Czech Republic (CZ)", "CZ"],- ["Denmark (DK)", "DK"],- ["Estonia (EE)", "EE"],- ["Finland (FI)", "FI"],- ["France (FR)", "FR"],- ["Germany (DE)", "DE"],- ["Greece (GR)", "GR"],- ["Hungary (HU)", "HU"],- ["Iceland (IS)", "IS"],- ["Ireland (IE)", "IE"],- ["Italy (IT)", "IT"],- ["Latvia (LV)", "LV"],- ["Liechtenstein (LI)", "LI"],- ["Lithuania (LT)", "LT"],- ["Luxembourg (LU)", "LU"],- ["Malta (MT)", "MT"],- ["Netherlands (NL)", "NL"],- ["Norway (NO)", "NO"],- ["Poland (PL)", "PL"],- ["Portugal (PT)", "PT"],- ["Romania (RO)", "RO"],- ["Slovakia (SK)", "SK"],- ["Slovenia (SI)", "SI"],- ["Spain (ES)", "ES"],- ["Sweden (SE)", "SE"],- ["United Kingdom (GB)", "GB"]- ], enable_banking_item.country_code),+ options_for_select(EnableBankingItem::COUNTRIES, enable_banking_item.country_code),{ label: true, include_blank: t("settings.providers.enable_banking_panel.select_country") },- { label: t("settings.providers.enable_banking_panel.country_label"), class: "form-field__input" } %>+ { label: t("settings.providers.enable_banking_panel.country_label"), class: "form-field__input", data: { enable_banking_country_target: "select" } } %><% if credentials_locked %><%= render DS::Alert.new(@@ -194,6 +162,8 @@text: t("settings.providers.enable_banking_panel.add_connection"),icon: "plus",href: new_connection_enable_banking_item_path(item_for_new_connection),+ params: { country_code: enable_banking_item.country_code },+ form: { data: { action: "submit->enable-banking-country#applyCountry" } },variant: :primary,data: { turbo_method: :post, turbo_frame: "modal" }) %>diff --git a/test/controllers/enable_banking_items_controller_test.rb b/test/controllers/enable_banking_items_controller_test.rbindex 94db7311d8..e40959165b 100644--- a/test/controllers/enable_banking_items_controller_test.rb+++ b/test/controllers/enable_banking_items_controller_test.rb@@ -35,6 +35,75 @@ class EnableBankingItemsControllerTest < ActionDispatch::IntegrationTestassert_no_match(/DE89370400440532013000/, @response.body, "Only the last four digits should be shown") # pipelock:ignore IBANend+ test "select_bank lists banks for the requested country" do+ Provider::EnableBanking.any_instance.expects(:get_aspsps).with(country: "AT").returns(aspsps: [])++ get select_bank_enable_banking_item_url(@item, new_connection: true, country_code: "at")++ assert_response :success+ end++ test "select_bank ignores the requested country unless it is for a new connection" do+ Provider::EnableBanking.any_instance.expects(:get_aspsps).with(country: "DE").returns(aspsps: [])++ get select_bank_enable_banking_item_url(@item, country_code: "AT")++ assert_response :success+ end++ test "authorize keeps the item's country when it is not for a new connection" do+ Provider::EnableBanking.any_instance.stubs(:get_aspsps).returns(aspsps: [ { name: "Test Bank", country: "DE" } ])++ get select_bank_enable_banking_item_url(@item)++ assert_select "form[action='#{authorize_enable_banking_item_path(@item)}'] input[name=country_code][value=DE]"+ end++ test "select_bank falls back to the item's country for an unknown country" do+ Provider::EnableBanking.any_instance.expects(:get_aspsps).with(country: "DE").returns(aspsps: [])++ get select_bank_enable_banking_item_url(@item, country_code: "ZZ")++ assert_response :success+ end++ test "select_bank passes the requested country on to authorize" do+ Provider::EnableBanking.any_instance.stubs(:get_aspsps).returns(aspsps: [ { name: "Test Bank", country: "AT" } ])++ get select_bank_enable_banking_item_url(@item, new_connection: true, country_code: "AT")++ assert_select "form[action='#{authorize_enable_banking_item_path(@item)}'] input[name=country_code][value=AT]"+ end++ test "new_connection forwards the requested country to select_bank" do+ post new_connection_enable_banking_item_url(@item), params: { country_code: "AT" }++ assert_redirected_to select_bank_enable_banking_item_url(@item, new_connection: true, country_code: "AT")+ end++ test "authorize creates a new connection in the requested country" do+ stub_authorization_start++ assert_difference -> { @family.enable_banking_items.count }, 1 do+ post authorize_enable_banking_item_url(@item),+ params: { aspsp_name: "Test Bank", psu_type: "personal", new_connection: "true", country_code: "AT" }+ end++ created = @family.enable_banking_items.where.not(id: @item.id).order(:created_at).last+ assert_equal "AT", created.country_code+ assert_equal "DE", @item.reload.country_code+ end++ test "authorize keeps the item's country when the requested country is invalid" do+ stub_authorization_start++ post authorize_enable_banking_item_url(@item),+ params: { aspsp_name: "Test Bank", psu_type: "personal", new_connection: "true", country_code: "ZZ" }++ created = @family.enable_banking_items.where.not(id: @item.id).order(:created_at).last+ assert_equal "DE", created.country_code+ end+test "select_bank exposes ASPSP BIC in the searchable data attribute" doProvider::EnableBanking.any_instance.stubs(:get_aspsps).returns(aspsps: [diff --git a/test/controllers/settings/provider_panels_test.rb b/test/controllers/settings/provider_panels_test.rbindex 2d8a4a4ed9..9e8a1ddd45 100644--- a/test/controllers/settings/provider_panels_test.rb+++ b/test/controllers/settings/provider_panels_test.rb@@ -66,6 +66,22 @@ class Settings::ProviderPanelsTest < ActionDispatch::IntegrationTestendend+ test "Enable Banking Add Connection carries the country chosen in the form" do+ family = families(:dylan_family)+ EnableBankingItem.create!(family: family, name: "Finland", country_code: "FI", application_id: "app",+ client_certificate: "cert", session_id: "fi", session_expires_at: 1.day.from_now)+ EnableBankingItem.create!(family: family, name: "Sweden", country_code: "SE", application_id: "app",+ client_certificate: "cert", session_id: "se", session_expires_at: 1.day.from_now)++ get connect_form_settings_providers_path(provider_key: "enable_banking")++ assert_response :success+ selected = css_select("select[data-enable-banking-country-target=select] option[selected]").first["value"]+ assert_includes %w[FI SE], selected+ assert_select "form[action*='new_connection'] input[name=country_code][value=#{selected}]"+ assert_select "form[action*='new_connection'][data-action='submit->enable-banking-country#applyCountry']"+ end+private# DS::Alert prefixes its text with the variant for screen readers ("Error: …").def titled_alert(variant, title_key)