Skip to content

armgit / sure

Public Imported from GitHub
0

The personal finance app for everyone (by everyone)

Ruby

fix(imports): normalize CSV upload encoding when validations are skipped (#2299)

Augusto Xavier844cba2fd387
diff --git a/app/models/import.rb b/app/models/import.rb
index fe9cf38f73..80b14ab261 100644
--- a/app/models/import.rb
+++ b/app/models/import.rb
@@ -45,6 +45,7 @@ class Import < ApplicationRecord
before_validation :set_default_number_format
before_validation :ensure_utf8_encoding
+ before_save :ensure_utf8_encoding
normalizes :client_chunk_id, with: ->(value) { value.strip.presence }
scope :ordered, -> { order(created_at: :desc) }
diff --git a/test/models/import_encoding_test.rb b/test/models/import_encoding_test.rb
index 7770da9923..14e084475b 100644
--- a/test/models/import_encoding_test.rb
+++ b/test/models/import_encoding_test.rb
@@ -56,6 +56,27 @@ class ImportEncodingTest < ActiveSupport::TestCase
assert import.rows.any? { |row| row.name&.include?("Café") }, "Extended Latin characters should be preserved"
end
+ test "normalizes encoding on save even when validations are skipped" do
+ file_path = Rails.root.join("test/fixtures/files/imports/windows1250.csv")
+ csv_content = File.binread(file_path)
+ assert_equal Encoding::ASCII_8BIT, csv_content.encoding
+
+ import = @family.imports.create!(
+ type: "TransactionImport",
+ account: @account,
+ date_format: "%Y-%m-%d"
+ )
+
+ import.assign_attributes(raw_file_str: csv_content)
+
+ assert_nothing_raised do
+ import.save!(validate: false)
+ end
+
+ assert_equal Encoding::UTF_8, import.reload.raw_file_str.encoding
+ assert import.raw_file_str.valid_encoding?, "Converted string should be valid UTF-8"
+ end
+
test "handles UTF-8 files without modification" do
# Test that valid UTF-8 files are not modified
file_path = Rails.root.join("test/fixtures/files/imports/transactions.csv")