2019-10-31 16:31:40 +01:00
|
|
|
RSpec.describe ApplicationMailer, type: :mailer do
|
|
|
|
describe 'dealing with invalid emails' do
|
2021-11-19 13:48:52 +01:00
|
|
|
let(:dossier) { create(:dossier, procedure: create(:simple_procedure)) }
|
2022-12-21 17:18:19 +01:00
|
|
|
subject { DossierMailer.with(dossier:).notify_new_draft }
|
2019-10-31 16:31:40 +01:00
|
|
|
|
2020-02-20 11:39:33 +01:00
|
|
|
describe 'invalid emails are not sent' do
|
|
|
|
before do
|
|
|
|
allow_any_instance_of(DossierMailer)
|
|
|
|
.to receive(:notify_new_draft)
|
2020-07-13 15:21:12 +02:00
|
|
|
.and_raise(smtp_error)
|
2020-02-20 11:39:33 +01:00
|
|
|
end
|
|
|
|
|
2020-07-13 15:21:12 +02:00
|
|
|
context 'when the server handles invalid emails with Net::SMTPSyntaxError' do
|
|
|
|
let(:smtp_error) { Net::SMTPSyntaxError.new }
|
|
|
|
it { expect(subject.message).to be_an_instance_of(ActionMailer::Base::NullMail) }
|
|
|
|
end
|
|
|
|
|
|
|
|
context 'when the server handles invalid emails with Net::SMTPServerBusy' do
|
|
|
|
let(:smtp_error) { Net::SMTPServerBusy.new('400 unexpected recipients: want atleast 1, got 0') }
|
|
|
|
it { expect(subject.message).to be_an_instance_of(ActionMailer::Base::NullMail) }
|
|
|
|
end
|
2020-02-20 11:39:33 +01:00
|
|
|
end
|
|
|
|
|
2019-10-31 16:31:40 +01:00
|
|
|
describe 'valid emails are sent' do
|
|
|
|
it { expect(subject.message).not_to be_an_instance_of(ActionMailer::Base::NullMail) }
|
|
|
|
end
|
|
|
|
end
|
2023-01-09 17:05:38 +01:00
|
|
|
|
|
|
|
describe 'EmailDeliveryObserver is invoked' do
|
|
|
|
let(:user1) { create(:user) }
|
|
|
|
let(:user2) { create(:user, email: "thisisyour@email.com") }
|
|
|
|
|
|
|
|
it 'creates a new EmailEvent record with the correct information' do
|
|
|
|
expect { UserMailer.ask_for_merge(user1, user2.email).deliver_now }.to change { EmailEvent.count }.by(1)
|
|
|
|
event = EmailEvent.last
|
|
|
|
|
|
|
|
expect(event.to).to eq("th*******r@email.com")
|
|
|
|
expect(event.method).to eq("test")
|
|
|
|
expect(event.subject).to eq('Fusion de compte')
|
|
|
|
expect(event.processed_at).to be_within(1.second).of(Time.zone.now)
|
|
|
|
expect(event.status).to eq('dispatched')
|
|
|
|
end
|
2023-01-09 23:16:14 +01:00
|
|
|
|
|
|
|
context "when email is not sent" do
|
|
|
|
subject(:send_email) { UserMailer.ask_for_merge(user1, user2.email).deliver_now }
|
|
|
|
|
|
|
|
before do
|
|
|
|
allow_any_instance_of(Mail::Message)
|
|
|
|
.to receive(:deliver)
|
|
|
|
.and_raise(smtp_error)
|
|
|
|
end
|
|
|
|
|
|
|
|
context "smtp server busy" do
|
|
|
|
let(:smtp_error) { Net::SMTPServerBusy.new }
|
|
|
|
|
|
|
|
it "creates an event" do
|
|
|
|
expect { send_email }.to change { EmailEvent.count }.by(1)
|
|
|
|
expect(EmailEvent.last.status).to eq('dispatch_error')
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
context "smtp unknown error" do
|
|
|
|
let(:smtp_error) { Net::SMTPUnknownError.new }
|
|
|
|
|
|
|
|
it "creates an event" do
|
|
|
|
expect { send_email }.to change { EmailEvent.count }.by(1)
|
|
|
|
expect(EmailEvent.last.status).to eq('dispatch_error')
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
2023-01-09 17:05:38 +01:00
|
|
|
end
|
2019-10-31 16:31:40 +01:00
|
|
|
end
|