Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add attachements to email steps output #213

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,9 @@ All notable changes to this project will be documented in this file.

This project adheres to [Semantic Versioning](http://semver.org/spec/v2.0.0.html).

## 4.6.3
- Fix email steps not showing the attachments

## 4.6.2
- Fix uninitialized constant `XPath::HTML` when using `When I fill in "..." with "..." inside any "..."` ([#210](https://github.com/makandra/spreewald/issues/210))

Expand Down
2 changes: 1 addition & 1 deletion Gemfile.ruby266.lock
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
PATH
remote: .
specs:
spreewald (4.6.2)
spreewald (4.6.3)
capybara
cucumber
cucumber_priority (>= 0.3.0)
Expand Down
2 changes: 1 addition & 1 deletion Gemfile.ruby320.lock
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
PATH
remote: .
specs:
spreewald (4.6.2)
spreewald (4.6.3)
capybara
cucumber
cucumber_priority (>= 0.3.0)
Expand Down
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
2 changes: 1 addition & 1 deletion lib/spreewald_support/version.rb
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
module Spreewald
VERSION = '4.6.2'
VERSION = '4.6.3'
end
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
2 changes: 1 addition & 1 deletion tests/rails-7_capybara-3/Gemfile.lock
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
PATH
remote: ../..
specs:
spreewald (4.6.2)
spreewald (4.6.3)
capybara
cucumber
cucumber_priority (>= 0.3.0)
Expand Down
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 = "[email protected]"
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: [email protected]
Attachments: attached_file.pdf
'''
"""

Then the following multiline step should fail:
"""
Then an email should have been sent with:
'''
From: [email protected]
Attachments: not_attached_file.pdf
'''
"""

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

Expand Down
Loading