test: add smtp sender test for MX lookup timeouts

This commit is contained in:
Adam Cooke
2024-03-01 21:41:46 +00:00
parent 0c1f9250d4
commit e4638a0c40
+29
View File
@@ -46,6 +46,7 @@ RSpec.describe SMTPSender do
describe "#start" do describe "#start" do
context "when no servers are provided to the class and there are no SMTP relays" do context "when no servers are provided to the class and there are no SMTP relays" do
context "when there are MX records" do
before do before do
allow(DNSResolver.local).to receive(:mx).and_return([[5, "mx1.example.com"], [10, "mx2.example.com"]]) allow(DNSResolver.local).to receive(:mx).and_return([[5, "mx1.example.com"], [10, "mx2.example.com"]])
allow(DNSResolver.local).to receive(:a).with("mx1.example.com").and_return(["1.2.3.4"]) allow(DNSResolver.local).to receive(:a).with("mx1.example.com").and_return(["1.2.3.4"])
@@ -62,6 +63,34 @@ RSpec.describe SMTPSender do
end end
end end
context "when there are no MX records" do
before do
allow(DNSResolver.local).to receive(:mx).and_return([])
allow(DNSResolver.local).to receive(:a).with("example.com").and_return(["1.2.3.4"])
end
it "attempts to create an SMTP connection for the domain itself" do
endpoint = sender.start
expect(endpoint).to be_a SMTPClient::Endpoint
expect(endpoint).to have_attributes(
ip_address: "1.2.3.4",
server: have_attributes(hostname: "example.com", port: 25, ssl_mode: SMTPClient::SSLModes::AUTO)
)
end
end
context "when the MX lookup times out" do
before do
allow(DNSResolver.local).to receive(:mx).and_raise(Resolv::ResolvError.new("DNS resolv timeout: example.com"))
allow(DNSResolver.local).to receive(:a).with("example.com").and_return(["1.2.3.4"])
end
it "raises an error" do
expect { sender.start }.to raise_error Resolv::ResolvError
end
end
end
context "when there are no servers provided to the class but there are SMTP relays" do context "when there are no servers provided to the class but there are SMTP relays" do
before do before do
allow(SMTPSender).to receive(:smtp_relays).and_return([SMTPClient::Server.new("relay.example.com", port: 2525, ssl_mode: SMTPClient::SSLModes::TLS)]) allow(SMTPSender).to receive(:smtp_relays).and_return([SMTPClient::Server.new("relay.example.com", port: 2525, ssl_mode: SMTPClient::SSLModes::TLS)])