From b957168ca5987dd68ea2d18e503259f0c890839b Mon Sep 17 00:00:00 2001 From: Jean-Francis Bastien Date: Tue, 26 Aug 2025 09:34:18 -0400 Subject: [PATCH 1/2] Create evelope to validate mail --- .../rails_cache_delivery_method.rb | 4 ++ .../rails_cache_delivery_method_spec.rb | 44 +++++++++++++++++++ 2 files changed, 48 insertions(+) diff --git a/lib/gravity_mailbox/rails_cache_delivery_method.rb b/lib/gravity_mailbox/rails_cache_delivery_method.rb index 2ed61f3..cd694c0 100644 --- a/lib/gravity_mailbox/rails_cache_delivery_method.rb +++ b/lib/gravity_mailbox/rails_cache_delivery_method.rb @@ -14,6 +14,10 @@ def initialize(settings) end def deliver!(mail) + # https://github.com/mikel/mail/blob/d1d65b370b109b98e673a934e8b70a0c1f58cc59/lib/mail/network/delivery_methods/test_mailer.rb#L39 + # Create the envelope to validate it + Mail::SmtpEnvelope.new(mail) + key = "#{KEY_PREFIX}#{mail.message_id}" Rails.cache.write(key, mail.encoded, expires_in: 1.week) # TODO: setting for expiration actual_list = self.class.mail_keys diff --git a/spec/gravity_mailbox/rails_cache_delivery_method_spec.rb b/spec/gravity_mailbox/rails_cache_delivery_method_spec.rb index c27fa5f..720f309 100644 --- a/spec/gravity_mailbox/rails_cache_delivery_method_spec.rb +++ b/spec/gravity_mailbox/rails_cache_delivery_method_spec.rb @@ -7,6 +7,8 @@ let(:mail) do Mail.new.tap do |m| m.message_id = '123' + m.from = 'sender@example.com' + m.to = 'recipient@example.com' end end @@ -26,6 +28,48 @@ ["gravity_mailbox/#{mail.message_id}"] ) end + + context 'envelope validation' do + context 'when mail has no sender' do + let(:mail) do + Mail.new.tap do |m| + m.message_id = '123' + m.to = 'recipient@example.com' + end + end + + it 'raises an error' do + expect { delivering }.to raise_error(ArgumentError, /SMTP From address may not be blank/) + end + end + + context 'when mail has no recipients' do + let(:mail) do + Mail.new.tap do |m| + m.message_id = '123' + m.from = 'sender@example.com' + end + end + + it 'raises an error' do + expect { delivering }.to raise_error(ArgumentError, /SMTP To address may not be blank/) + end + end + + context 'when mail has invalid from address' do + let(:mail) do + Mail.new.tap do |m| + m.message_id = '123' + m.from = '' + m.to = 'recipient@example.com' + end + end + + it 'raises an error' do + expect { delivering }.to raise_error(ArgumentError, /SMTP From address may not be blank/) + end + end + end end describe '#mails' do From f5230d8bcfe42720fca3999d77e896a9655092ef Mon Sep 17 00:00:00 2001 From: Jean-Francis Bastien Date: Tue, 26 Aug 2025 09:36:05 -0400 Subject: [PATCH 2/2] --- spec/gravity_mailbox/rails_cache_delivery_method_spec.rb | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/spec/gravity_mailbox/rails_cache_delivery_method_spec.rb b/spec/gravity_mailbox/rails_cache_delivery_method_spec.rb index 720f309..fc15af2 100644 --- a/spec/gravity_mailbox/rails_cache_delivery_method_spec.rb +++ b/spec/gravity_mailbox/rails_cache_delivery_method_spec.rb @@ -29,7 +29,7 @@ ) end - context 'envelope validation' do + describe 'envelope validation' do context 'when mail has no sender' do let(:mail) do Mail.new.tap do |m|