Skip to content

Commit

Permalink
Merge pull request #452 from slovensko-digital/GO-109/get_fs_forms_in…
Browse files Browse the repository at this point in the history
…fo_from_api

GO-109 Get FS forms info from FS API
  • Loading branch information
luciajanikova authored Jul 12, 2024
2 parents a984116 + 82e99ce commit 63dc9b4
Show file tree
Hide file tree
Showing 4 changed files with 77 additions and 40 deletions.
4 changes: 4 additions & 0 deletions app/lib/fs/api.rb
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,10 @@ def fetch_forms(**args)
request(:get, "forms", **args)
end

def parse_form(content)
request(:post, "forms/parse", { content: Base64.strict_encode64(content) })[:body]
end

def get_public_key(**args)
@fs_public_key ||= request(:get, "public-key")[:body]["public_key_b64"]
end
Expand Down
9 changes: 6 additions & 3 deletions app/models/fs/message_draft.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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(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)
Expand Down Expand Up @@ -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
Expand Down
11 changes: 10 additions & 1 deletion test/models/fs/message_draft_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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"
},
[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
93 changes: 57 additions & 36 deletions test/system/fs/message_drafts_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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"
},
[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

Expand All @@ -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"
},
[file_fixture("fs/dic1122334455_fs3055_781__sprava_dani_2023.xml").read]
fs_api.expect :parse_form, {
"subject" => "1122334455",
"form_identifier" => "2682_712"
},
[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
Expand Down

0 comments on commit 63dc9b4

Please sign in to comment.