Skip to content

Commit 00c6464

Browse files
committed
Fix validation order - validate file format before encoding
1 parent 1d12945 commit 00c6464

File tree

3 files changed

+19
-1
lines changed

3 files changed

+19
-1
lines changed

lib/active_admin_import/model.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@ module CONST
3737
validate :file_contents_present, if: ->(me) { me.file.present? }
3838

3939
before_validation :unzip_file, if: ->(me) { me.archive? && me.allow_archive? }
40-
before_validation :encode_file, if: ->(me) { me.force_encoding? && me.file.present? }
40+
after_validation :encode_file, if: ->(me) { me.force_encoding? && me.file.present? }
4141

4242
attr_reader :attributes
4343

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
Name,Last name,Birthday
2+
John,Doe,1986-05-01

spec/import_spec.rb

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -610,4 +610,20 @@ def upload_file!(name, ext = 'csv')
610610
end.not_to change { Author.count }
611611
end
612612
end
613+
614+
context 'when importing file with invalid format and auto force_encoding' do
615+
let(:options) { { template_object: ActiveAdminImport::Model.new(force_encoding: :auto) } }
616+
617+
before do
618+
add_author_resource(options)
619+
visit '/admin/authors/import'
620+
end
621+
622+
it 'should reject invalid file format before encoding' do
623+
expect do
624+
upload_file!(:author_invalid_format, 'txt')
625+
expect(page).to have_content I18n.t('active_admin_import.file_format_error')
626+
end.not_to change { Author.count }
627+
end
628+
end
613629
end

0 commit comments

Comments
 (0)