diff --git a/app/senders/smtp_sender.rb b/app/senders/smtp_sender.rb index 897282e4..7b19225d 100644 --- a/app/senders/smtp_sender.rb +++ b/app/senders/smtp_sender.rb @@ -244,11 +244,11 @@ def smtp_relays relays = Postal::Config.postal.smtp_relays return nil if relays.nil? - relays.map do |relay| + relays = relays.map do |relay| next unless relay.host.present? - SMTPClient::Server.new(relay.host, relay.port, ssl_mode: relay.ssl_mode) - end.compact + SMTPClient::Server.new(relay.host, port: relay.port, ssl_mode: relay.ssl_mode) + end @smtp_relays = relays.empty? ? nil : relays end diff --git a/spec/senders/smtp_sender_spec.rb b/spec/senders/smtp_sender_spec.rb index 4d44e306..6008a265 100644 --- a/spec/senders/smtp_sender_spec.rb +++ b/spec/senders/smtp_sender_spec.rb @@ -524,4 +524,41 @@ expect(sender.endpoints).to all have_received(:finish_smtp_session).at_least(:once) end end + + describe ".smtp_relays" do + before do + if described_class.instance_variable_defined?("@smtp_relays") + described_class.remove_instance_variable("@smtp_relays") + end + end + + it "returns nil if smtp relays is nil" do + allow(Postal::Config.postal).to receive(:smtp_relays).and_return(nil) + expect(described_class.smtp_relays).to be nil + end + + it "returns nil if there are no smtp relays" do + allow(Postal::Config.postal).to receive(:smtp_relays).and_return([]) + expect(described_class.smtp_relays).to be nil + end + + it "does not return relays where the host is nil" do + allow(Postal::Config.postal).to receive(:smtp_relays).and_return([ + Hashie::Mash.new(host: nil, port: 25, ssl_mode: "Auto"), + Hashie::Mash.new(host: "test.example.com", port: 25, ssl_mode: "Auto"), + ]) + expect(described_class.smtp_relays).to match [kind_of(SMTPClient::Server)] + end + + it "returns relays with options" do + allow(Postal::Config.postal).to receive(:smtp_relays).and_return([ + Hashie::Mash.new(host: "test.example.com", port: 25, ssl_mode: "Auto"), + Hashie::Mash.new(host: "test2.example.com", port: 2525, ssl_mode: "TLS"), + ]) + expect(described_class.smtp_relays).to match [ + have_attributes(hostname: "test.example.com", port: 25, ssl_mode: "Auto"), + have_attributes(hostname: "test2.example.com", port: 2525, ssl_mode: "TLS"), + ] + end + end end