From 8d4a77d14d2b025d9b1f65efbbfee911f9bc9d15 Mon Sep 17 00:00:00 2001 From: luciajanikova <19lucia99@gmail.com> Date: Fri, 31 Jan 2025 15:21:54 +0100 Subject: [PATCH] Do not raise error if XML does not match any FS form --- .env.test | 2 ++ app/lib/fs/api.rb | 2 +- app/models/fs/message_draft.rb | 4 ++-- test/fixtures/files/fs/random_xml.xml | 3 +++ test/models/fs/message_draft_test.rb | 17 +++++++++++++++++ 5 files changed, 25 insertions(+), 3 deletions(-) create mode 100644 test/fixtures/files/fs/random_xml.xml diff --git a/.env.test b/.env.test index d32d7f7b..e128ecfb 100644 --- a/.env.test +++ b/.env.test @@ -12,5 +12,7 @@ ACTIVE_RECORD_ENCRYPTION_PRIMARY_KEY=1 ACTIVE_RECORD_ENCRYPTION_DETERMINISTIC_KEY=2 ACTIVE_RECORD_ENCRYPTION_KEY_DERIVATION_SALT=3 +FS_API_URL=https://fsapi.test + GROVER_NO_SANDBOX=true # must be true for running chromium as root in Docker container PDF_DISPLAY_URL= diff --git a/app/lib/fs/api.rb b/app/lib/fs/api.rb index 23c0d8c0..5d673627 100644 --- a/app/lib/fs/api.rb +++ b/app/lib/fs/api.rb @@ -117,7 +117,7 @@ def request_url(method, path, *args, accept_negative: false) raise StandardError.new(error.response) if error.respond_to?(:response) && error.response raise error else - raise StandardError.new(response.body) unless accept_negative || response.status < 400 + raise StandardError.new(response.body) if !accept_negative && response.status != 404 && response.status > 400 return { status: response.status, body: structure, diff --git a/app/models/fs/message_draft.rb b/app/models/fs/message_draft.rb index c18c0978..b067e664 100644 --- a/app/models/fs/message_draft.rb +++ b/app/models/fs/message_draft.rb @@ -32,8 +32,8 @@ def self.create_and_validate_with_fs_form(form_files: [], author:, fs_client: Fs form_files.each do |form_file| form_content = form_file.read.force_encoding("UTF-8") form_information = fs_client.api.parse_form(form_content) - dic = form_information['subject']&.strip - fs_form_identifier = form_information['form_identifier'] + dic = form_information&.dig('subject')&.strip + fs_form_identifier = form_information&.dig('form_identifier') box = author.tenant.boxes.with_enabled_message_drafts_import.find_by("settings ->> 'dic' = ?", dic) fs_form = Fs::Form.find_by(identifier: fs_form_identifier) diff --git a/test/fixtures/files/fs/random_xml.xml b/test/fixtures/files/fs/random_xml.xml new file mode 100644 index 00000000..0415abe8 --- /dev/null +++ b/test/fixtures/files/fs/random_xml.xml @@ -0,0 +1,3 @@ + + + diff --git a/test/models/fs/message_draft_test.rb b/test/models/fs/message_draft_test.rb index 8787de5a..df256d58 100644 --- a/test/models/fs/message_draft_test.rb +++ b/test/models/fs/message_draft_test.rb @@ -44,4 +44,21 @@ class Fs::MessageDraftTest < ActiveSupport::TestCase message_draft = Fs::MessageDraft.last assert message_draft.thread.box.eql?(boxes(:fs_accountants)) end + + test "create_and_validate_with_fs_form method does not raise if XML does not match any FS form" do + author = users(:accountants_basic) + + fs_api_handler = Minitest::Mock.new + fs_api_handler.expect :public_send, OpenStruct.new(status: 404, headers: nil, body: "null"), [:post, 'https://fsapi.test/api/v1/forms/parse', {:content=> Base64.strict_encode64(file_fixture("fs/random_xml.xml").read) }] + + fs_api_handler_options = Minitest::Mock.new + fs_api_handler.expect :options, fs_api_handler_options, [] + fs_api_handler_options.expect :timeout=, nil, [900000] + + assert_nothing_raised do + FsEnvironment.fs_client.stub :api, Fs::Api.new(ENV.fetch('FS_API_URL'), handler: fs_api_handler ) do + Fs::MessageDraft.create_and_validate_with_fs_form(form_files: [fixture_file_upload("fs/random_xml.xml", "application/xml")], author: author) + end + end + end end