From 1cf444e535fd6909c34b17b4b6bcac61c09baf88 Mon Sep 17 00:00:00 2001 From: luciajanikova <19lucia99@gmail.com> Date: Fri, 12 Jul 2024 10:43:58 +0200 Subject: [PATCH 1/4] Parse FS form information via FS API --- app/lib/fs/api.rb | 4 ++++ app/models/fs/message_draft.rb | 9 ++++++--- 2 files changed, 10 insertions(+), 3 deletions(-) diff --git a/app/lib/fs/api.rb b/app/lib/fs/api.rb index 280eb3aca..e95e5ba46 100644 --- a/app/lib/fs/api.rb +++ b/app/lib/fs/api.rb @@ -16,6 +16,10 @@ def fetch_forms(**args) request(:get, "forms", **args) end + def parse_form(encoded_content) + request(:post, "forms/parse", { content: encoded_content })[:body] + end + def get_public_key(**args) @fs_public_key ||= request(:get, "public-key")[:body]["public_key_b64"] end diff --git a/app/models/fs/message_draft.rb b/app/models/fs/message_draft.rb index f51ebc607..829b27453 100644 --- a/app/models/fs/message_draft.rb +++ b/app/models/fs/message_draft.rb @@ -26,11 +26,14 @@ def self.policy_class MessageDraftPolicy end - def self.create_and_validate_with_fs_form(form_files: [], author:) + def self.create_and_validate_with_fs_form(form_files: [], author:, fs_client: FsEnvironment.fs_client) messages = [] form_files.each do |form_file| - dic, fs_form_identifier = Utils::FsInformationParser.parse_info_from_filename(form_file.original_filename) + form_content = form_file.read.force_encoding("UTF-8") + form_information = fs_client.api.parse_form(Base64.strict_encode64(form_content)) + dic = form_information['subject'] + fs_form_identifier = form_information['form_identifier'] box = Fs::Box.find_by("settings ->> 'dic' = ?", dic) fs_form = Fs::Form.find_by(identifier: fs_form_identifier) @@ -80,7 +83,7 @@ def self.create_and_validate_with_fs_form(form_files: [], author:) MessageObjectDatum.create( message_object: form_object, - blob: form_file.read.force_encoding("UTF-8") + blob: form_content ) messages << message From 91b596dbefd9967bf42909d5501724afad957e6b Mon Sep 17 00:00:00 2001 From: luciajanikova <19lucia99@gmail.com> Date: Fri, 12 Jul 2024 12:01:33 +0200 Subject: [PATCH 2/4] Update tests --- test/system/fs/message_drafts_test.rb | 93 ++++++++++++++++----------- 1 file changed, 57 insertions(+), 36 deletions(-) diff --git a/test/system/fs/message_drafts_test.rb b/test/system/fs/message_drafts_test.rb index 4cf6e8dcf..a99871099 100644 --- a/test/system/fs/message_drafts_test.rb +++ b/test/system/fs/message_drafts_test.rb @@ -11,18 +11,26 @@ class Fs::MessageDraftsTest < ApplicationSystemTestCase click_button "Vytvoriť novú správu" click_link "Vytvoriť novú správu na finančnú správu" - attach_file "content[]", file_fixture("fs/dic1122334455_fs3055_781__sprava_dani_2023.xml") + fs_api = Minitest::Mock.new + fs_api.expect :parse_form, { + "subject" => "1122334455", + "form_identifier" => "3055_781" + }, + [Base64.strict_encode64(file_fixture("fs/dic1122334455_fs3055_781__sprava_dani_2023.xml").read)] - click_button "Nahrať správy" + FsEnvironment.fs_client.stub :api, fs_api do + attach_file "content[]", file_fixture("fs/dic1122334455_fs3055_781__sprava_dani_2023.xml") + click_button "Nahrať správy" - assert_text "Správy boli úspešne nahraté" + assert_text "Správy boli úspešne nahraté" - assert_content "Podanie pre FS (Správa daní) - platné od 1.4.2024" - assert_content "Rozpracované" + assert_content "Podanie pre FS (Správa daní) - platné od 1.4.2024" + assert_content "Rozpracované" - within_frame(find("iframe")) do - assert_text "Všeobecné podanie - správa daní" - assert_text "Daňové identifikačné číslo:\n1122334455" + within_frame(find("iframe")) do + assert_text "Všeobecné podanie - správa daní" + assert_text "Daňové identifikačné číslo:\n1122334455" + end end end @@ -32,37 +40,50 @@ class Fs::MessageDraftsTest < ApplicationSystemTestCase click_button "Vytvoriť novú správu" click_link "Vytvoriť novú správu na finančnú správu" - attach_file "content[]", [ - file_fixture("fs/dic1122334455_fs3055_781__sprava_dani_2023.xml"), - file_fixture("fs/dic1122334455_fs2682_712__v2py_2021.xml") - ] - - click_button "Nahrať správy" - - assert_text "Správy boli úspešne nahraté" - - Searchable::MessageThread.reindex_all - - visit message_threads_path - - message1 = Fs::MessageDraft.second_to_last - message2 = Fs::MessageDraft.last - - within_thread_in_listing(message1.thread) do - assert_text "Podanie pre FS (Správa daní) - platné od 1.4.2024" - assert_text "Finančná správa" - - within_tags do - assert_text "Rozpracované" + fs_api = Minitest::Mock.new + fs_api.expect :parse_form, { + "subject" => "1122334455", + "form_identifier" => "3055_781" + }, + [Base64.strict_encode64(file_fixture("fs/dic1122334455_fs3055_781__sprava_dani_2023.xml").read)] + fs_api.expect :parse_form, { + "subject" => "1122334455", + "form_identifier" => "2682_712" + }, + [Base64.strict_encode64(file_fixture("fs/dic1122334455_fs2682_712__v2py_2021.xml").read)] + + FsEnvironment.fs_client.stub :api, fs_api do + attach_file "content[]", [ + file_fixture("fs/dic1122334455_fs3055_781__sprava_dani_2023.xml"), + file_fixture("fs/dic1122334455_fs2682_712__v2py_2021.xml") + ] + click_button "Nahrať správy" + + assert_text "Správy boli úspešne nahraté" + + Searchable::MessageThread.reindex_all + + visit message_threads_path + + message1 = Fs::MessageDraft.second_to_last + message2 = Fs::MessageDraft.last + + within_thread_in_listing(message1.thread) do + assert_text "Podanie pre FS (Správa daní) - platné od 1.4.2024" + assert_text "Finančná správa" + + within_tags do + assert_text "Rozpracované" + end end - end - within_thread_in_listing(message2.thread) do - assert_text "Vyhlásenie o poukázaní sumy do výšky 2% (3%) zaplatenej dane za zdaňovacie obdobie 2021" - assert_text "Finančná správa" + within_thread_in_listing(message2.thread) do + assert_text "Vyhlásenie o poukázaní sumy do výšky 2% (3%) zaplatenej dane za zdaňovacie obdobie 2021" + assert_text "Finančná správa" - within_tags do - assert_text "Rozpracované" + within_tags do + assert_text "Rozpracované" + end end end end From be81e6144cd1f0acabd596a688bf2fb9afab893c Mon Sep 17 00:00:00 2001 From: luciajanikova <19lucia99@gmail.com> Date: Fri, 12 Jul 2024 12:24:10 +0200 Subject: [PATCH 3/4] Update test --- test/models/fs/message_draft_test.rb | 11 ++++++++++- 1 file changed, 10 insertions(+), 1 deletion(-) diff --git a/test/models/fs/message_draft_test.rb b/test/models/fs/message_draft_test.rb index a88ec807b..c7b36e3c0 100644 --- a/test/models/fs/message_draft_test.rb +++ b/test/models/fs/message_draft_test.rb @@ -7,8 +7,17 @@ class Fs::MessageDraftTest < ActiveSupport::TestCase include ActionDispatch::TestProcess::FixtureFile test "create_and_validate_with_fs_form method schedules Fs::ValidateMessageDraftJob" do + fs_api = Minitest::Mock.new + fs_api.expect :parse_form, { + "subject" => "1122334455", + "form_identifier" => "3055_781" + }, + [Base64.strict_encode64(file_fixture("fs/dic1122334455_fs3055_781__sprava_dani_2023.xml").read)] + + FsEnvironment.fs_client.stub :api, fs_api do assert_enqueued_with(job: Fs::ValidateMessageDraftJob) do - Fs::MessageDraft.create_and_validate_with_fs_form(form_files: [fixture_file_upload("fs/dic1122334455_fs3055_781__sprava_dani_2023.xml", "application/xml")], author: users(:accountants_basic)) + Fs::MessageDraft.create_and_validate_with_fs_form(form_files: [fixture_file_upload("fs/dic1122334455_fs3055_781__sprava_dani_2023.xml", "application/xml")], author: users(:accountants_basic)) + end end end end From 82e99ce248f63df25819eb68ea966c10e4cfbb02 Mon Sep 17 00:00:00 2001 From: luciajanikova <19lucia99@gmail.com> Date: Fri, 12 Jul 2024 13:11:45 +0200 Subject: [PATCH 4/4] Update parse_form method --- app/lib/fs/api.rb | 4 ++-- app/models/fs/message_draft.rb | 2 +- test/models/fs/message_draft_test.rb | 2 +- test/system/fs/message_drafts_test.rb | 6 +++--- 4 files changed, 7 insertions(+), 7 deletions(-) diff --git a/app/lib/fs/api.rb b/app/lib/fs/api.rb index e95e5ba46..da24d7051 100644 --- a/app/lib/fs/api.rb +++ b/app/lib/fs/api.rb @@ -16,8 +16,8 @@ def fetch_forms(**args) request(:get, "forms", **args) end - def parse_form(encoded_content) - request(:post, "forms/parse", { content: encoded_content })[:body] + def parse_form(content) + request(:post, "forms/parse", { content: Base64.strict_encode64(content) })[:body] end def get_public_key(**args) diff --git a/app/models/fs/message_draft.rb b/app/models/fs/message_draft.rb index 829b27453..a0c7164ed 100644 --- a/app/models/fs/message_draft.rb +++ b/app/models/fs/message_draft.rb @@ -31,7 +31,7 @@ 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(Base64.strict_encode64(form_content)) + form_information = fs_client.api.parse_form(form_content) dic = form_information['subject'] fs_form_identifier = form_information['form_identifier'] diff --git a/test/models/fs/message_draft_test.rb b/test/models/fs/message_draft_test.rb index c7b36e3c0..ed1bcccae 100644 --- a/test/models/fs/message_draft_test.rb +++ b/test/models/fs/message_draft_test.rb @@ -12,7 +12,7 @@ class Fs::MessageDraftTest < ActiveSupport::TestCase "subject" => "1122334455", "form_identifier" => "3055_781" }, - [Base64.strict_encode64(file_fixture("fs/dic1122334455_fs3055_781__sprava_dani_2023.xml").read)] + [file_fixture("fs/dic1122334455_fs3055_781__sprava_dani_2023.xml").read] FsEnvironment.fs_client.stub :api, fs_api do assert_enqueued_with(job: Fs::ValidateMessageDraftJob) do diff --git a/test/system/fs/message_drafts_test.rb b/test/system/fs/message_drafts_test.rb index a99871099..b1c0f3201 100644 --- a/test/system/fs/message_drafts_test.rb +++ b/test/system/fs/message_drafts_test.rb @@ -16,7 +16,7 @@ class Fs::MessageDraftsTest < ApplicationSystemTestCase "subject" => "1122334455", "form_identifier" => "3055_781" }, - [Base64.strict_encode64(file_fixture("fs/dic1122334455_fs3055_781__sprava_dani_2023.xml").read)] + [file_fixture("fs/dic1122334455_fs3055_781__sprava_dani_2023.xml").read] FsEnvironment.fs_client.stub :api, fs_api do attach_file "content[]", file_fixture("fs/dic1122334455_fs3055_781__sprava_dani_2023.xml") @@ -45,12 +45,12 @@ class Fs::MessageDraftsTest < ApplicationSystemTestCase "subject" => "1122334455", "form_identifier" => "3055_781" }, - [Base64.strict_encode64(file_fixture("fs/dic1122334455_fs3055_781__sprava_dani_2023.xml").read)] + [file_fixture("fs/dic1122334455_fs3055_781__sprava_dani_2023.xml").read] fs_api.expect :parse_form, { "subject" => "1122334455", "form_identifier" => "2682_712" }, - [Base64.strict_encode64(file_fixture("fs/dic1122334455_fs2682_712__v2py_2021.xml").read)] + [file_fixture("fs/dic1122334455_fs2682_712__v2py_2021.xml").read] FsEnvironment.fs_client.stub :api, fs_api do attach_file "content[]", [