The personal finance app for everyone (by everyone)
Ruby
fix(sharing): scope import account selects to accessible_by (#1803) (#2194)
dripsmvcp1157ea8f2013
diff --git a/app/views/import/uploads/show.html.erb b/app/views/import/uploads/show.html.erbindex 7cf51d6514..7d19818c68 100644--- a/app/views/import/uploads/show.html.erb+++ b/app/views/import/uploads/show.html.erb@@ -56,7 +56,7 @@<%= styled_form_with model: @import, scope: :import, url: import_upload_path(@import), multipart: true, class: "space-y-4" do |form| %><%= form.select :account_id,- @import.family.accounts.visible.alphabetically.pluck(:name, :id),+ Current.user.accessible_accounts.visible.alphabetically.pluck(:name, :id),{ label: t(".qif_account_label"), include_blank: t(".qif_account_placeholder"), selected: @import.account_id },required: true %>@@ -112,7 +112,7 @@<%= form.select :col_sep, Import.separator_options, label: true %><% if @import.type == "TransactionImport" || @import.type == "TradeImport" %>- <%= form.select :account_id, @import.family.accounts.visible.alphabetically.pluck(:name, :id), { label: t(".account_optional_label"), include_blank: t(".multi_account_import"), selected: @import.account_id } %>+ <%= form.select :account_id, Current.user.accessible_accounts.visible.alphabetically.pluck(:name, :id), { label: t(".account_optional_label"), include_blank: t(".multi_account_import"), selected: @import.account_id } %><% end %><label for="import_import_file_csv" class="flex flex-col items-center justify-center w-full h-64 border border-secondary border-dashed rounded-xl cursor-pointer" data-controller="file-upload" data-file-upload-target="uploadArea">@@ -144,7 +144,7 @@<%= form.select :col_sep, Import.separator_options, label: true %><% if @import.type == "TransactionImport" || @import.type == "TradeImport" %>- <%= form.select :account_id, @import.family.accounts.visible.alphabetically.pluck(:name, :id), { label: t(".account_optional_label"), include_blank: t(".multi_account_import"), selected: @import.account_id } %>+ <%= form.select :account_id, Current.user.accessible_accounts.visible.alphabetically.pluck(:name, :id), { label: t(".account_optional_label"), include_blank: t(".multi_account_import"), selected: @import.account_id } %><% end %><%= form.text_area :raw_file_str,diff --git a/app/views/imports/_pdf_import.html.erb b/app/views/imports/_pdf_import.html.erbindex 4d82ce324f..06f2a6e0ef 100644--- a/app/views/imports/_pdf_import.html.erb+++ b/app/views/imports/_pdf_import.html.erb@@ -39,10 +39,10 @@<div class="space-y-2"><h2 class="font-medium text-primary"><%= t("imports.pdf_import.select_account", default: "Import to Account") %></h2>- <%= form_with model: import, url: import_path(import), method: :patch, class: "space-y-2" do |f| %>- <% accounts = import.family.accounts.manual.alphabetically %>+ <%= form_with model: import, scope: :import, url: import_path(import), method: :patch, class: "space-y-2" do |f| %>+ <% accounts = Current.user.accessible_accounts.manual.alphabetically.pluck(:name, :id) %><% if accounts.any? %>- <%= f.select :account_id, options_for_select(accounts.map { |a| [a.name, a.id] }, import.account_id), { include_blank: t("imports.pdf_import.select_account_placeholder", default: "Select an account...") }, class: "form-field__input" %>+ <%= f.select :account_id, options_for_select(accounts, import.account_id), { include_blank: t("imports.pdf_import.select_account_placeholder", default: "Select an account...") }, class: "form-field__input" %><% if import.account.nil? %><p class="text-xs text-secondary"><%= t("imports.pdf_import.select_account_hint", default: "Choose which account to import these transactions into.") %></p><% end %>diff --git a/test/controllers/import/uploads_controller_test.rb b/test/controllers/import/uploads_controller_test.rbindex 6aa3be8b63..1836ac95d6 100644--- a/test/controllers/import/uploads_controller_test.rb+++ b/test/controllers/import/uploads_controller_test.rb@@ -35,6 +35,18 @@ class Import::UploadsControllerTest < ActionDispatch::IntegrationTestassert_equal "CSV uploaded successfully.", flash[:notice]end+ test "account select does not leak unshared family accounts (#1803)" do+ sign_in users(:family_member)++ get import_upload_url(@import)++ assert_response :success+ assert_select 'select[name="import[account_id]"] option', text: "Checking Account"+ assert_select 'select[name="import[account_id]"] option', text: "Collectable Account", count: 0+ assert_select 'select[name="import[account_id]"] option', text: "IOU (personal debt to friend)", count: 0+ assert_select 'select[name="import[account_id]"] option', text: "Plaid Depository Account", count: 0+ end+test "invalid csv cannot be uploaded" dopatch import_upload_url(@import), params: {import: {diff --git a/test/controllers/imports_controller_test.rb b/test/controllers/imports_controller_test.rbindex 1b48bd9539..a92db9b1de 100644--- a/test/controllers/imports_controller_test.rb+++ b/test/controllers/imports_controller_test.rb@@ -363,4 +363,22 @@ class ImportsControllerTest < ActionDispatch::IntegrationTestassert_redirected_to imports_pathend++ test "PDF import account select does not leak unshared family accounts (#1803)" do+ sign_in users(:family_member)+ pdf_import = imports(:pdf_with_rows)+ # The fixture has no attached pdf_file and no statement, so+ # ImportsController#show would redirect to the upload page. The+ # partial under test only renders for an uploaded PDF — stub the+ # state so we exercise the actual account-select scoping path.+ PdfImport.any_instance.stubs(:pdf_uploaded?).returns(true)++ get import_url(pdf_import)++ assert_response :success+ assert_select 'select[name="import[account_id]"] option', text: "Checking Account"+ assert_select 'select[name="import[account_id]"] option', text: "Collectable Account", count: 0+ assert_select 'select[name="import[account_id]"] option', text: "IOU (personal debt to friend)", count: 0+ assert_select 'select[name="import[account_id]"] option', text: "Plaid Depository Account", count: 0+ endend