From ec3aee2ac401241abe08667307633c888f54c677 Mon Sep 17 00:00:00 2001 From: Martin Schaflitzl Date: Thu, 14 Nov 2024 13:50:26 +0100 Subject: [PATCH 1/2] Make email steps show the attachments --- lib/spreewald_support/mail_finder.rb | 12 +- spec/steps/show_me_the_mails_spec.rb | 22 +++ .../app/controllers/emails_controller.rb | 9 +- tests/shared/app/models/mailer.rb | 139 ++++++------------ .../features/shared/email_steps.feature | 18 +++ 5 files changed, 94 insertions(+), 106 deletions(-) diff --git a/lib/spreewald_support/mail_finder.rb b/lib/spreewald_support/mail_finder.rb index 2873350..de7baa1 100644 --- a/lib/spreewald_support/mail_finder.rb +++ b/lib/spreewald_support/mail_finder.rb @@ -24,7 +24,6 @@ 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]), @@ -32,7 +31,7 @@ def find(raw_data, type = '') 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 @@ -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 = '') @@ -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 diff --git a/spec/steps/show_me_the_mails_spec.rb b/spec/steps/show_me_the_mails_spec.rb index 6bfddd5..186c72b 100644 --- a/spec/steps/show_me_the_mails_spec.rb +++ b/spec/steps/show_me_the_mails_spec.rb @@ -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 diff --git a/tests/shared/app/controllers/emails_controller.rb b/tests/shared/app/controllers/emails_controller.rb index 38db723..e04c432 100644 --- a/tests/shared/app/controllers/emails_controller.rb +++ b/tests/shared/app/controllers/emails_controller.rb @@ -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 diff --git a/tests/shared/app/models/mailer.rb b/tests/shared/app/models/mailer.rb index 2fb7a76..ee1bd10 100644 --- a/tests/shared/app/models/mailer.rb +++ b/tests/shared/app/models/mailer.rb @@ -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 diff --git a/tests/shared/features/shared/email_steps.feature b/tests/shared/features/shared/email_steps.feature index c8ce7fb..0eeb234 100644 --- a/tests/shared/features/shared/email_steps.feature +++ b/tests/shared/features/shared/email_steps.feature @@ -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" From 32d8a6f047de617d670b5a525c6d2c38568dd778 Mon Sep 17 00:00:00 2001 From: Martin Schaflitzl Date: Thu, 14 Nov 2024 13:57:22 +0100 Subject: [PATCH 2/2] Bump version to 4.6.3 --- CHANGELOG.md | 3 +++ Gemfile.ruby266.lock | 2 +- Gemfile.ruby320.lock | 2 +- lib/spreewald_support/version.rb | 2 +- tests/rails-7_capybara-3/Gemfile.lock | 2 +- 5 files changed, 7 insertions(+), 4 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 1789a46..cf47ee1 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -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)) diff --git a/Gemfile.ruby266.lock b/Gemfile.ruby266.lock index fd594f3..27ae212 100644 --- a/Gemfile.ruby266.lock +++ b/Gemfile.ruby266.lock @@ -1,7 +1,7 @@ PATH remote: . specs: - spreewald (4.6.2) + spreewald (4.6.3) capybara cucumber cucumber_priority (>= 0.3.0) diff --git a/Gemfile.ruby320.lock b/Gemfile.ruby320.lock index 37b08d2..03b82c8 100644 --- a/Gemfile.ruby320.lock +++ b/Gemfile.ruby320.lock @@ -1,7 +1,7 @@ PATH remote: . specs: - spreewald (4.6.2) + spreewald (4.6.3) capybara cucumber cucumber_priority (>= 0.3.0) diff --git a/lib/spreewald_support/version.rb b/lib/spreewald_support/version.rb index 151bf71..3890259 100644 --- a/lib/spreewald_support/version.rb +++ b/lib/spreewald_support/version.rb @@ -1,3 +1,3 @@ module Spreewald - VERSION = '4.6.2' + VERSION = '4.6.3' end diff --git a/tests/rails-7_capybara-3/Gemfile.lock b/tests/rails-7_capybara-3/Gemfile.lock index 05c9f92..284fcf6 100644 --- a/tests/rails-7_capybara-3/Gemfile.lock +++ b/tests/rails-7_capybara-3/Gemfile.lock @@ -1,7 +1,7 @@ PATH remote: ../.. specs: - spreewald (4.6.2) + spreewald (4.6.3) capybara cucumber cucumber_priority (>= 0.3.0)