Files
sure/test/controllers/imports_controller_test.rb
Serge L 57199d6eb9 Feat: Add QIF (Quicken Interchange Format) import functionality (#1074)
* Feat: Add QIF (Quicken Interchange Format) import functionality
- Add the ability to import QIF files for users coming from Quicken
- Includes categories and tags
- Comprehensive tests for QifImport, including parsing, row generation, and import functionality.
- Ensure handling of hierarchical categories (ex "Home:Home Improvement" is imported as Parent:Child)

* Fix QIF import issues raised in code review

- Fix two-digit year windowing in QIF date parser (e.g. '99 → 1999, not 2099)
- Fix ArgumentError from invalid `undef: :raise` encoding option
- Nil-safe `leaf_category_name` with blank guard and `.to_s` coercion
- Memoize `qif_account_type` to avoid re-parsing the full QIF file
- Add strong parameters (`selection_params`) to QifCategorySelectionsController
- Wrap all mutations in DB transactions in uploads and category-selections controllers
- Skip unchanged tag rows (only write rows where tags actually differ)
- Replace hardcoded strings with i18n keys across QIF views and nav
- Fix potentially colliding checkbox/label IDs in category selection view
- Improve keyboard accessibility: use semantic `<label>` for file picker area

Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>

* Fix QIF import test count and Brakeman mass assignment warning

- Update ImportsControllerTest to expect 4 disabled import options (was 3),
  accounting for the new QIF import type added in this branch
- Remove :account_id from upload_params permit list; it was never accessed
  through strong params (always via params.dig with Current.family scope),
  so this resolves the Brakeman high-confidence mass assignment warning

Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>

* Fix: QIF import security, safety, and i18n issues raised in code review
- Added french, spanish and german translations for newly added i18n keys
- Replace params.dig(:import, :account_id) with a proper strong-params
  accessor (import_account_id) in UploadsController to satisfy Rails
  parameter filtering requirements
- Guard ImportsController#show against QIF imports reaching the publish
  screen before a file has been uploaded, preventing an unrescued error
  on publish
- Gate the QIF "Clean" nav step link on import.uploaded? to prevent
  routing to CleansController with an unconfigured import (which would
  raise "Unknown import type: QifImport" via ImportsHelper)
- Replace hard-coded "txn" pluralize calls in the category/tag selection
  view with t(".txn_count") and add pluralization keys to the locale file
- Localize all hard-coded strings in the QIF upload section of
  uploads/show.html.erb and add corresponding en.yml keys
- Convert the CSV upload drop zone from a clickable <div> (JS-only) to
  a semantic <label> element, making it keyboard-accessible without
  JavaScript

* Fix: missing translations keys

* Add icon mapping and random color assignment to new categories

* fix a lint issue

* Add a warning about splits and some plumbing for future support.
Updated locales.

---------

Co-authored-by: Claude Sonnet 4.6 <noreply@anthropic.com>
2026-03-14 20:22:39 +01:00

139 lines
4.0 KiB
Ruby

require "test_helper"
class ImportsControllerTest < ActionDispatch::IntegrationTest
setup do
sign_in @user = users(:family_admin)
end
test "gets index" do
get imports_url
assert_response :success
@user.family.imports.ordered.each do |import|
assert_select "#" + dom_id(import), count: 1
end
end
test "gets new" do
get new_import_url
assert_response :success
assert_select "turbo-frame#modal"
end
test "shows disabled account-dependent imports when family has no accounts" do
sign_in users(:empty)
get new_import_url
assert_response :success
assert_select "button", text: "Import accounts"
assert_select "button", text: "Import transactions", count: 0
assert_select "button", text: "Import investments", count: 0
assert_select "button", text: "Import from Mint", count: 0
assert_select "button", text: "Import from Quicken (QIF)", count: 0
assert_select "span", text: "Import accounts first to unlock this option.", count: 4
assert_select "div[aria-disabled=true]", count: 4
end
test "creates import" do
assert_difference "Import.count", 1 do
post imports_url, params: {
import: {
type: "TransactionImport"
}
}
end
assert_redirected_to import_upload_url(Import.all.ordered.first)
end
test "uploads supported non-pdf document for vector store without creating import" do
adapter = mock("vector_store_adapter")
adapter.stubs(:supported_extensions).returns(%w[.csv .pdf])
VectorStore::Registry.stubs(:adapter).returns(adapter)
family_document = family_documents(:tax_return)
Family.any_instance.expects(:upload_document).with do |file_content:, filename:, **|
assert_not_empty file_content
assert_equal "valid.csv", filename
true
end.returns(family_document)
assert_no_difference "Import.count" do
post imports_url, params: {
import: {
type: "DocumentImport",
import_file: file_fixture_upload("imports/valid.csv", "text/csv")
}
}
end
assert_redirected_to new_import_url
assert_equal I18n.t("imports.create.document_uploaded"), flash[:notice]
end
test "uploads pdf document as PdfImport when using DocumentImport option" do
adapter = mock("vector_store_adapter")
adapter.stubs(:supported_extensions).returns(%w[.pdf .txt])
VectorStore::Registry.stubs(:adapter).returns(adapter)
@user.family.expects(:upload_document).never
assert_difference "Import.count", 1 do
post imports_url, params: {
import: {
type: "DocumentImport",
import_file: file_fixture_upload("imports/sample_bank_statement.pdf", "application/pdf")
}
}
end
created_import = Import.order(:created_at).last
assert_equal "PdfImport", created_import.type
assert_redirected_to import_url(created_import)
assert_equal I18n.t("imports.create.pdf_processing"), flash[:notice]
end
test "rejects unsupported document type for DocumentImport option" do
adapter = mock("vector_store_adapter")
adapter.stubs(:supported_extensions).returns(%w[.pdf .txt])
VectorStore::Registry.stubs(:adapter).returns(adapter)
assert_no_difference "Import.count" do
post imports_url, params: {
import: {
type: "DocumentImport",
import_file: file_fixture_upload("profile_image.png", "image/png")
}
}
end
assert_redirected_to new_import_url
assert_equal I18n.t("imports.create.invalid_document_file_type"), flash[:alert]
end
test "publishes import" do
import = imports(:transaction)
TransactionImport.any_instance.expects(:publish_later).once
post publish_import_url(import)
assert_equal "Your import has started in the background.", flash[:notice]
assert_redirected_to import_path(import)
end
test "destroys import" do
import = imports(:transaction)
assert_difference "Import.count", -1 do
delete import_url(import)
end
assert_redirected_to imports_path
end
end