Skip to content

Commit

Permalink
Make email steps show the attachments
Browse files Browse the repository at this point in the history
  • Loading branch information
martinschaflitzl1 committed Nov 14, 2024
1 parent 7b67a92 commit ec3aee2
Show file tree
Hide file tree
Showing 5 changed files with 94 additions and 106 deletions.
12 changes: 10 additions & 2 deletions lib/spreewald_support/mail_finder.rb
Original file line number Diff line number Diff line change
Expand Up @@ -24,15 +24,14 @@ def find(raw_data, type = '')
body = [header, body].join("\n\n")
end

filename_method = Rails::VERSION::MAJOR < 3 ? 'original_filename' : 'filename'
matching_header = ActionMailer::Base.deliveries.select do |mail|
[ conditions[:to].nil? || mail.to.include?(resolve_email conditions[:to]),
conditions[:cc].nil? || mail.cc && mail.cc.include?(resolve_email conditions[:cc]),
conditions[:bcc].nil? || mail.bcc && mail.bcc.include?(resolve_email conditions[:bcc]),
conditions[:from].nil? || mail.from.include?(resolve_email conditions[:from]),
conditions[:reply_to].nil? || mail.reply_to.include?(resolve_email conditions[:reply_to]),
conditions[:subject].nil? || mail.subject.include?(conditions[:subject]),
conditions[:attachments].nil? || conditions[:attachments].split(/\s*,\s*/).sort == Array(mail.attachments).collect(&:"#{filename_method}").sort
conditions[:attachments].nil? || conditions[:attachments].split(/\s*,\s*/).sort == attachment_filenames(mail)
].all?
end

Expand Down Expand Up @@ -64,6 +63,9 @@ def header_representation(mail)
header << "CC: #{mail.cc.join(', ')}\n" if mail.cc
header << "BCC: #{mail.bcc.join(', ')}\n" if mail.bcc
header << "Subject: #{mail.subject}\n"
header << "Attachments: #{attachment_filenames(mail).join(', ')}\n" if mail.attachments.any?

header
end

def email_text_body(mail, type = '')
Expand Down Expand Up @@ -94,6 +96,12 @@ def expected_body_regex(expected_body)
Regexp.new(expected)
end

private

def attachment_filenames(mail)
Array(mail.attachments).collect(&:filename).sort
end

end
end

Expand Down
22 changes: 22 additions & 0 deletions spec/steps/show_me_the_mails_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -93,4 +93,26 @@
expect { step.run }.to output(expected_output).to_stdout
end
end

context "with attachments" do
it 'includes the filenames of the attachments' do
mail = Mail.new do
attachments['attached_file.pdf'] = File.open("tests/shared/public/fixture_files/attachment.pdf") {}
attachments['other_attached_file.pdf'] = File.open("tests/shared/public/fixture_files/attachment.pdf") {}
end

expected_output = <<~TXT
E-Mail #0
--------------------------------------------------------------------------------
From:
Subject:
Attachments: attached_file.pdf, other_attached_file.pdf
--------------------------------------------------------------------------------
TXT
step = Spreewald::Steps::ShowMeTheMails.new([mail], true)
expect { step.run }.to output(expected_output).to_stdout
end
end

end
9 changes: 1 addition & 8 deletions tests/shared/app/controllers/emails_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -67,14 +67,7 @@ def send_text_email_for_failed_test_without_header
private

def deliver(method_name)
case
when Rails.version.to_i >= 5
SpreewaldMailer.send(method_name).deliver
when Rails.version.to_i >= 3
Mailer.public_send(method_name).deliver
else
Mailer.public_send("deliver_#{method_name}")
end
SpreewaldMailer.send(method_name).deliver
end

end
139 changes: 43 additions & 96 deletions tests/shared/app/models/mailer.rb
Original file line number Diff line number Diff line change
Expand Up @@ -7,113 +7,60 @@ class Mailer < ActionMailer::Base
FROM = "from@example.com"
SUBJECT = "SUBJECT"

if Rails.version >= "3"

def email
attachments['attached_file.pdf'] = File.open("#{Rails.root}/public/fixture_files/attachment.pdf", "w") {}
mail(
:from => FROM,
:reply_to => REPLY_TO,
:to => TO,
:cc => CC,
:bcc => BCC,
:subject => SUBJECT
)
end

def email_crlf
email
end

def email_with_umlauts
email
end

def html_email_with_links
email
end

def text_email_with_links
email
end

def html_email_with_linebreaks
email
end

def html_email_with_specific_line
email
end

def text_email_with_specific_line
email
end

def html_email_for_successful_test_without_header
email
end

def text_email_for_successful_test_without_header
email
end

def html_email_for_failed_test_without_header
email
end

def text_email_for_failed_test_without_header
email
end

else
def email
attachments['attached_file.pdf'] = File.open("#{Rails.root}/public/fixture_files/attachment.pdf") {}
mail(
:from => FROM,
:reply_to => REPLY_TO,
:to => TO,
:cc => CC,
:bcc => BCC,
:subject => SUBJECT
)
end

def email
attachments['attached_file.pdf'] = File.open("#{Rails.root}/public/fixture_files/attachment.pdf", "w") {}
recipients TO
reply_to REPLY_TO
from FROM
cc CC
bcc BCC
subject SUBJECT
body BODY
end
def email_crlf
email
end

def email_crlf
email
end
def email_with_umlauts
email
end

def html_email_with_links
email
end
def html_email_with_links
email
end

def text_email_with_links
email
end
def text_email_with_links
email
end

def html_email_with_specific_line
email
end
def html_email_with_linebreaks
email
end

def text_email_with_specific_line
email
end
def html_email_with_specific_line
email
end

def html_email_for_successful_test_without_header
email
end
def text_email_with_specific_line
email
end

def text_email_for_successful_test_without_header
email
end
def html_email_for_successful_test_without_header
email
end

def html_email_for_failed_test_without_header
email
end
def text_email_for_successful_test_without_header
email
end

def text_email_for_failed_test_without_header
email
end
def html_email_for_failed_test_without_header
email
end

def text_email_for_failed_test_without_header
email
end

end
18 changes: 18 additions & 0 deletions tests/shared/features/shared/email_steps.feature
Original file line number Diff line number Diff line change
Expand Up @@ -297,6 +297,24 @@ Feature: Test Spreewald's email steps
'''
"""

Then the following multiline step should succeed:
"""
Then an email should have been sent with:
'''
From: from@example.com
Attachments: attached_file.pdf
'''
"""

Then the following multiline step should fail:
"""
Then an email should have been sent with:
'''
From: from@example.com
Attachments: not_attached_file.pdf
'''
"""

When I clear my emails
And I go to "/emails/send_crlf_email"

Expand Down

0 comments on commit ec3aee2

Please sign in to comment.