From e15b1283e5f9fde3be1bd4a3c8f3595116801dc3 Mon Sep 17 00:00:00 2001 From: Alex Sherman Date: Mon, 1 Jun 2020 14:36:16 +0500 Subject: [PATCH 1/2] Add error handling if legaldoc file not found Closes #1452 --- app/controllers/admin/legal_documents_controller.rb | 9 ++++++++- config/locales/admin/legal_documents.en.yml | 2 ++ config/locales/admin/legal_documents.et.yml | 2 ++ 3 files changed, 12 insertions(+), 1 deletion(-) create mode 100644 config/locales/admin/legal_documents.en.yml create mode 100644 config/locales/admin/legal_documents.et.yml diff --git a/app/controllers/admin/legal_documents_controller.rb b/app/controllers/admin/legal_documents_controller.rb index a07949875..70ebddded 100644 --- a/app/controllers/admin/legal_documents_controller.rb +++ b/app/controllers/admin/legal_documents_controller.rb @@ -5,7 +5,14 @@ module Admin def show @ld = LegalDocument.find(params[:id]) filename = @ld.path.split('/').last - send_data File.open(@ld.path).read, filename: filename + file = File.open(@ld.path).read + + if file + send_data file, filename: filename + else + flash[:notice] = I18n.t('legal_doc_not_found') + redirect_to [:admin, @ld.documentable] + end end end end diff --git a/config/locales/admin/legal_documents.en.yml b/config/locales/admin/legal_documents.en.yml new file mode 100644 index 000000000..f24d883a6 --- /dev/null +++ b/config/locales/admin/legal_documents.en.yml @@ -0,0 +1,2 @@ +en: + legal_doc_not_found: "There is an error downloading legal document: file not found" diff --git a/config/locales/admin/legal_documents.et.yml b/config/locales/admin/legal_documents.et.yml new file mode 100644 index 000000000..d3ebfa512 --- /dev/null +++ b/config/locales/admin/legal_documents.et.yml @@ -0,0 +1,2 @@ +et: + legal_doc_not_found: "Viga juriidilise dokumendi allalaadimisel: faili ei leitud" From e92afb59c1a5cd3c4894ff280491aac1dc3268d3 Mon Sep 17 00:00:00 2001 From: Alex Sherman Date: Mon, 1 Jun 2020 15:11:47 +0500 Subject: [PATCH 2/2] Add tests, fix error handling --- .../admin/legal_documents_controller.rb | 13 +++++------- .../admin_area/domains/legal_doc_test.rb | 21 +++++++++++++++++++ 2 files changed, 26 insertions(+), 8 deletions(-) create mode 100644 test/system/admin_area/domains/legal_doc_test.rb diff --git a/app/controllers/admin/legal_documents_controller.rb b/app/controllers/admin/legal_documents_controller.rb index 70ebddded..68b95877d 100644 --- a/app/controllers/admin/legal_documents_controller.rb +++ b/app/controllers/admin/legal_documents_controller.rb @@ -5,14 +5,11 @@ module Admin def show @ld = LegalDocument.find(params[:id]) filename = @ld.path.split('/').last - file = File.open(@ld.path).read - - if file - send_data file, filename: filename - else - flash[:notice] = I18n.t('legal_doc_not_found') - redirect_to [:admin, @ld.documentable] - end + file = File.open(@ld.path)&.read + send_data file, filename: filename + rescue Errno::ENOENT + flash[:notice] = I18n.t('legal_doc_not_found') + redirect_to [:admin, @ld.documentable] end end end diff --git a/test/system/admin_area/domains/legal_doc_test.rb b/test/system/admin_area/domains/legal_doc_test.rb new file mode 100644 index 000000000..00cc7cc3a --- /dev/null +++ b/test/system/admin_area/domains/legal_doc_test.rb @@ -0,0 +1,21 @@ +require 'application_system_test_case' + +class AdminAreaDomainsLegalDocTest < ApplicationSystemTestCase + setup do + sign_in users(:admin) + @domain = domains(:shop) + @document = LegalDocument.create( + document_type: 'pdf', + documentable_id: @domain.id, + documentable_type: 'Domain', + path: '\zzz\zzz' + ) + end + + def test_absent_doc_downloading_without_errors + visit admin_domain_url(@domain) + assert_nothing_raised do + click_on "#{@document.created_at}" + end + end +end