Skip to content

Commit

Permalink
Merge pull request #542 from slovensko-digital/GO-197/do_not_raise_if…
Browse files Browse the repository at this point in the history
…_fs_xml_does_not_match_any_fs_form

GO-197 Do not raise error if XML does not match any FS form
  • Loading branch information
luciajanikova authored Jan 31, 2025
2 parents bb6d09f + 8d4a77d commit dae3dd3
Show file tree
Hide file tree
Showing 5 changed files with 25 additions and 3 deletions.
2 changes: 2 additions & 0 deletions .env.test
Original file line number Diff line number Diff line change
Expand Up @@ -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=
2 changes: 1 addition & 1 deletion app/lib/fs/api.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
4 changes: 2 additions & 2 deletions app/models/fs/message_draft.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
3 changes: 3 additions & 0 deletions test/fixtures/files/fs/random_xml.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
<randomXML>
<hello />
</randomXML>
17 changes: 17 additions & 0 deletions test/models/fs/message_draft_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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

0 comments on commit dae3dd3

Please sign in to comment.