Skip to content

Commit

Permalink
Update FoiAttachment factory
Browse files Browse the repository at this point in the history
Simplify the after build callback by ensuring there will be a `body`,
`content_type` and `filename`.
  • Loading branch information
gbp committed Sep 27, 2023
1 parent c7203f0 commit f38f19f
Show file tree
Hide file tree
Showing 2 changed files with 6 additions and 5 deletions.
7 changes: 3 additions & 4 deletions spec/factories/foi_attchments.rb
Original file line number Diff line number Diff line change
Expand Up @@ -4,17 +4,16 @@
sequence(:url_part_number) { |n| n + 1 }
display_size { '0K' }
masked_at { 1.day.ago }
content_type { 'text/plain' }
filename { 'attachment.txt' }

transient do
body { 'hereisthemaskedtext' }
end

after(:build) do |foi_attachment, evaluator|
body = evaluator.body
foi_attachment.hexdigest = Digest::MD5.hexdigest(body) if body

next unless body && foi_attachment.filename && foi_attachment.content_type

foi_attachment.hexdigest = Digest::MD5.hexdigest(body)
foi_attachment.file.attach(
io: StringIO.new(body),
filename: foi_attachment.filename,
Expand Down
4 changes: 3 additions & 1 deletion spec/models/foi_attachment_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -233,10 +233,12 @@

context 'when attachment file is unattached' do
let(:foi_attachment) do
FactoryBot.create(:body_text, filename: nil)
FactoryBot.create(:body_text)
end

it 'raises missing attachment exception' do
foi_attachment.file.purge

expect { unmasked_body }.to raise_error(
FoiAttachment::MissingAttachment,
"file not attached (ID=#{foi_attachment.id})"
Expand Down

0 comments on commit f38f19f

Please sign in to comment.