|
| 1 | +require 'rails_helper' |
| 2 | +require 'net/smtp' |
| 3 | + |
| 4 | +RSpec.describe MailConnectivityCheck do |
| 5 | + subject(:check) { described_class.new } |
| 6 | + |
| 7 | + let(:smtp_settings) do |
| 8 | + { |
| 9 | + address: 'smtp.example.com', |
| 10 | + port: 587, |
| 11 | + domain: 'example.com', |
| 12 | + user_name: 'user', |
| 13 | + password: 'password', |
| 14 | + authentication: 'plain', |
| 15 | + tls: true |
| 16 | + } |
| 17 | + end |
| 18 | + |
| 19 | + before do |
| 20 | + allow(ActionMailer::Base).to receive(:smtp_settings).and_return(smtp_settings) |
| 21 | + allow(Rails.logger).to receive(:warn) |
| 22 | + end |
| 23 | + |
| 24 | + describe '#check' do |
| 25 | + context 'when SMTP connection succeeds' do |
| 26 | + before do |
| 27 | + allow(Net::SMTP).to receive(:start).and_yield |
| 28 | + end |
| 29 | + |
| 30 | + it 'marks the check as successful' do |
| 31 | + check.check |
| 32 | + |
| 33 | + expect(check.success?).to be(true) |
| 34 | + expect(check.message).to eq('Connection for smtp successful') |
| 35 | + end |
| 36 | + end |
| 37 | + |
| 38 | + context 'when authentication fails' do |
| 39 | + before do |
| 40 | + allow(Net::SMTP).to receive(:start) |
| 41 | + .and_raise(Net::SMTPAuthenticationError.new('auth failed')) |
| 42 | + end |
| 43 | + |
| 44 | + it 'marks failure and logs authentication error' do |
| 45 | + check.check |
| 46 | + |
| 47 | + expect(check.success?).to be(false) |
| 48 | + expect(check.message) |
| 49 | + .to eq('SMTP Error: Authentication failed. Check logs for more details') |
| 50 | + expect(Rails.logger) |
| 51 | + .to have_received(:warn).with(/SMTP authentication error/) |
| 52 | + end |
| 53 | + end |
| 54 | + |
| 55 | + context 'when SMTP protocol errors occur' do |
| 56 | + [ |
| 57 | + Net::SMTPServerBusy, |
| 58 | + Net::SMTPSyntaxError, |
| 59 | + Net::SMTPFatalError, |
| 60 | + Net::SMTPUnknownError |
| 61 | + ].each do |error_class| |
| 62 | + it "handles #{error_class.name}" do |
| 63 | + allow(Net::SMTP).to receive(:start) |
| 64 | + .and_raise(error_class.new('smtp error')) |
| 65 | + |
| 66 | + check.check |
| 67 | + |
| 68 | + expect(check.success?).to be(false) |
| 69 | + expect(check.message) |
| 70 | + .to eq('SMTP error. Check logs for more details') |
| 71 | + expect(Rails.logger) |
| 72 | + .to have_received(:warn).with(/SMTP Error/) |
| 73 | + end |
| 74 | + end |
| 75 | + end |
| 76 | + |
| 77 | + context 'when a timeout occurs' do |
| 78 | + [IOError, Net::ReadTimeout].each do |error_class| |
| 79 | + it "handles #{error_class.name}" do |
| 80 | + allow(Net::SMTP).to receive(:start) |
| 81 | + .and_raise(error_class.new('timeout')) |
| 82 | + |
| 83 | + check.check |
| 84 | + |
| 85 | + expect(check.success?).to be(false) |
| 86 | + expect(check.message) |
| 87 | + .to eq('SMTP Connection error: Timeout. Check logs for more details') |
| 88 | + expect(Rails.logger) |
| 89 | + .to have_received(:warn).with(/SMTP Timeout/) |
| 90 | + end |
| 91 | + end |
| 92 | + end |
| 93 | + |
| 94 | + context 'when an unexpected error occurs' do |
| 95 | + before do |
| 96 | + allow(Net::SMTP).to receive(:start) |
| 97 | + .and_raise(StandardError.new('failed')) |
| 98 | + end |
| 99 | + |
| 100 | + it 'marks failure and logs standard error' do |
| 101 | + check.check |
| 102 | + |
| 103 | + expect(check.success?).to be(false) |
| 104 | + expect(check.message) |
| 105 | + .to eq('SMTP ERROR: Could not connect. Check logs for more details') |
| 106 | + expect(Rails.logger) |
| 107 | + .to have_received(:warn).with(/SMTP standard error/) |
| 108 | + end |
| 109 | + end |
| 110 | + end |
| 111 | +end |
0 commit comments