Skip to content

Commit

Permalink
asp: clarify the filename attribute and fix file_saved? for payments
Browse files Browse the repository at this point in the history
  • Loading branch information
freesteph committed Apr 5, 2024
1 parent dc94cc6 commit 852eaed
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 10 deletions.
16 changes: 8 additions & 8 deletions app/services/asp/file_handler.rb
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,13 @@ module ASP
class FileHandler
include Errors

attr_reader :filepath, :filename
attr_reader :filepath, :filename_noext

FILE_TYPES = %i[rejects integrations payments].freeze

def initialize(filepath)
@filepath = filepath
@filename = File.basename(filepath, ".*")
@filename_noext = File.basename(filepath, ".*")
end

def parse!
Expand All @@ -21,13 +21,13 @@ def parse!
begin
reader.process!
rescue StandardError => e
raise ResponseFileParsingError, "couldn't parse #{filename}: #{e}"
raise ResponseFileParsingError, "couldn't parse #{filename_noext}: #{e}"
end
end

def file_saved?
if payments_file?
ASP::PaymentReturn.exists?(filename: filename)
ASP::PaymentReturn.exists?(filename: "#{filename_noext}.xml")
else
target_attachment.attached?
end
Expand All @@ -42,7 +42,7 @@ def file_saved?
end

def kind
case filename
case filename_noext
when /^rejets_integ_idp/
:rejects
when /^identifiants_generes/
Expand All @@ -56,9 +56,9 @@ def original_filename
return if payments_file?

name = if rejects_file?
filename.split("integ_idp_").last
filename_noext.split("integ_idp_").last
elsif integrations_file?
filename.split("generes_").last
filename_noext.split("generes_").last
end

"#{name}.xml"
Expand Down Expand Up @@ -89,7 +89,7 @@ def persist_file!
end

def persist_payment_file!
ASP::PaymentReturn.create_with_file!(io: File.read(filepath), filename: "#{filename}.xml")
ASP::PaymentReturn.create_with_file!(io: File.read(filepath), filename: "#{filename_noext}.xml")
end

def attach_to_request!
Expand Down
11 changes: 9 additions & 2 deletions spec/services/asp/file_handler_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -33,10 +33,14 @@

expect(request.send("#{type}_file")).to be_attached
end

it "can tell the file was attached correctly" do
expect { reader.parse! }.to change(reader, :file_saved?).from(false).to(true)
end
end
# rubocop:enable Rspec/MultipleMemoizedHelpers

%i[rejects integrations].each do |type|
%i[integrations rejects].each do |type|
context "when the file is #{type} file" do
it_behaves_like "a reader for the ASP integration process", type
end
Expand All @@ -60,6 +64,9 @@

expect(ASP::PaymentReturn.last.filename).to eq basename
end

it "knows whether the file was saved" do
expect { reader.parse! }.to change(reader, :file_saved?).from(false).to(true)
end
end
end
# frozen_string_literal: true

0 comments on commit 852eaed

Please sign in to comment.