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
|
2023-03-06 10:13:43 +01:00
|
|
|
let(:smtp_error) { Net::SMTPSyntaxError.new('400 unexpected recipients: want atleast 1, got 0') }
|
2020-07-13 15:21:12 +02:00
|
|
|
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
|
|
|
|
2023-02-03 15:42:46 +01:00
|
|
|
describe 'dealing with Dolist API error' do
|
|
|
|
let(:dossier) { create(:dossier, procedure: create(:simple_procedure)) }
|
|
|
|
subject { DossierMailer.with(dossier:).notify_new_draft.deliver_now }
|
2023-04-03 16:25:45 +02:00
|
|
|
context 'not ignored error' do
|
|
|
|
before do
|
|
|
|
ActionMailer::Base.delivery_method = :dolist_api
|
|
|
|
api_error_response = { "ResponseStatus": { "ErrorCode": "Forbidden", "Message": "Blocked non authorized request", "Errors": [] } }
|
|
|
|
allow_any_instance_of(Dolist::API).to receive(:send_email).and_return(api_error_response)
|
|
|
|
end
|
|
|
|
|
|
|
|
it 'raise classic error to retry' do
|
2023-06-19 15:24:10 +02:00
|
|
|
expect { subject }.to raise_error(RuntimeError)
|
|
|
|
expect(EmailEvent.pending.count).to eq(1)
|
2023-04-03 16:25:45 +02:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
context 'ignored error' do
|
|
|
|
before do
|
|
|
|
ActionMailer::Base.delivery_method = :dolist_api
|
|
|
|
api_error_response = { "ResponseStatus" => { "ErrorCode" => "458", "Message" => "The contact is disabled.", "Errors" => [] } }
|
|
|
|
allow_any_instance_of(Dolist::API).to receive(:send_email).and_return(api_error_response)
|
|
|
|
allow_any_instance_of(Dolist::API).to receive(:fetch_contact_status).with(dossier.user.email).and_return("7")
|
|
|
|
end
|
2023-02-03 15:42:46 +01:00
|
|
|
|
2023-04-03 16:25:45 +02:00
|
|
|
it 'does not raise' do
|
|
|
|
expect { subject }.not_to raise_error
|
|
|
|
end
|
2023-02-03 15:42:46 +01:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2023-02-03 15:40:16 +01:00
|
|
|
describe 'dealing with Dolist API success' do
|
|
|
|
let(:dossier) { create(:dossier, procedure: create(:simple_procedure)) }
|
|
|
|
let(:message_id) { "29d9b692-0374-4084-8434-d9cddbced205" }
|
|
|
|
before do
|
|
|
|
ActionMailer::Base.delivery_method = :dolist_api
|
|
|
|
api_success_response = { "Result" => message_id }
|
|
|
|
allow_any_instance_of(Dolist::API).to receive(:send_email).and_return(api_success_response)
|
|
|
|
end
|
|
|
|
subject { DossierMailer.with(dossier:).notify_new_draft.deliver_now }
|
|
|
|
|
|
|
|
it 'forward message ID to observer and EmailEvent.create_from_message!' do
|
|
|
|
expect { subject }.to change { EmailEvent.dolist_api.dispatched.where(message_id:).count }.to eq(1)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2023-01-09 17:05:38 +01:00
|
|
|
describe 'EmailDeliveryObserver is invoked' do
|
|
|
|
let(:user1) { create(:user) }
|
2023-01-10 15:47:00 +01:00
|
|
|
let(:user2) { create(:user, email: "your@email.com") }
|
2023-01-09 17:05:38 +01:00
|
|
|
|
|
|
|
it 'creates a new EmailEvent record with the correct information' do
|
2023-06-19 15:24:10 +02:00
|
|
|
expect { UserMailer.ask_for_merge(user1, user2.email).deliver_now }.to change { EmailEvent.count }.by(2)
|
2023-01-09 17:05:38 +01:00
|
|
|
event = EmailEvent.last
|
2023-06-19 15:24:10 +02:00
|
|
|
expect(EmailEvent.first.status).to eq('pending')
|
2023-01-09 17:05:38 +01:00
|
|
|
|
2023-01-10 15:47:00 +01:00
|
|
|
expect(event.to).to eq("your@email.com")
|
2023-01-09 17:05:38 +01:00
|
|
|
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-06-19 15:24:10 +02:00
|
|
|
end
|
|
|
|
|
|
|
|
context 'EmailDeliveringInterceptor is invoked' do
|
|
|
|
let(:user1) { create(:user) }
|
|
|
|
let(:user2) { create(:user, email: "your@email.com") }
|
2023-01-09 23:16:14 +01:00
|
|
|
|
2023-01-16 11:26:05 +01:00
|
|
|
context "when there is an error and email are not sent" do
|
|
|
|
subject { UserMailer.ask_for_merge(user1, user2.email) }
|
2023-01-09 23:16:14 +01:00
|
|
|
|
|
|
|
before do
|
|
|
|
allow_any_instance_of(Mail::Message)
|
2023-06-19 15:24:10 +02:00
|
|
|
.to receive(:do_delivery)
|
2023-01-09 23:16:14 +01:00
|
|
|
.and_raise(smtp_error)
|
|
|
|
end
|
|
|
|
|
|
|
|
context "smtp server busy" do
|
2023-03-06 10:13:43 +01:00
|
|
|
let(:smtp_error) { Net::SMTPServerBusy.new('451 4.7.500 Server busy') }
|
2023-01-09 23:16:14 +01:00
|
|
|
|
2023-06-19 15:24:10 +02:00
|
|
|
it "catches the smtp error" do
|
|
|
|
expect { subject.deliver_now }.not_to raise_error
|
|
|
|
expect(EmailEvent.pending.count).to eq(1)
|
2023-01-09 23:16:14 +01:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2023-06-19 15:24:10 +02:00
|
|
|
context "does not catches other error" do
|
2023-01-10 00:40:16 +01:00
|
|
|
let(:smtp_error) { Net::OpenTimeout.new }
|
2023-01-09 23:16:14 +01:00
|
|
|
|
2023-01-16 11:26:05 +01:00
|
|
|
it "re-raise an error and creates an event" do
|
2023-06-19 15:24:10 +02:00
|
|
|
expect { subject.deliver_now }.to raise_error(Net::OpenTimeout)
|
|
|
|
expect(EmailEvent.pending.count).to eq(1)
|
2023-01-09 23:16:14 +01:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
2023-01-09 17:05:38 +01:00
|
|
|
end
|
2019-10-31 16:31:40 +01:00
|
|
|
end
|