Non far crashare il reset password se SMTP rifiuta il destinatario.

Un account di test su dominio .test faceva 500; ora l'invio fallito viene loggato e l'API resta ok.

Co-authored-by: Cursor <cursoragent@cursor.com>
This commit is contained in:
2026-08-31 12:00:22 +02:00
co-authored by Cursor
parent 645807b853
commit 79850dfc2c
2 changed files with 18 additions and 0 deletions
@@ -1,3 +1,6 @@
require "net/smtp"
require "openssl"
module MatchLiveTv module MatchLiveTv
class << self class << self
def jwt_secret def jwt_secret
@@ -102,6 +105,7 @@ module MatchLiveTv
end end
# In produzione senza SMTP non blocca il flusso (es. collaudo): la mail si può inviare a mano. # In produzione senza SMTP non blocca il flusso (es. collaudo): la mail si può inviare a mano.
# Errori SMTP (dominio invalido, EOF Aruba, porta chiusa) non devono far crashare la request.
def deliver_mail(mail) def deliver_mail(mail)
if Rails.env.production? && !smtp_configured? if Rails.env.production? && !smtp_configured?
Rails.logger.info("[mail] skip (SMTP assente): #{mail.subject}") Rails.logger.info("[mail] skip (SMTP assente): #{mail.subject}")
@@ -110,6 +114,9 @@ module MatchLiveTv
mail.deliver_now mail.deliver_now
true true
rescue EOFError, Net::SMTPError, Errno::ECONNREFUSED, Errno::ETIMEDOUT, SocketError, OpenSSL::SSL::SSLError => e
Rails.logger.warn("[mail] delivery failed: #{mail.subject} #{e.class}: #{e.message}")
false
end end
def privacy_controller_name def privacy_controller_name
+11
View File
@@ -120,6 +120,17 @@ RSpec.describe "Account API", type: :request do
expect(user.reload.password_reset_digest).to be_present expect(user.reload.password_reset_digest).to be_present
end end
it "non va in 500 se SMTP rifiuta il destinatario" do
allow_any_instance_of(ActionMailer::MessageDelivery).to receive(:deliver_now)
.and_raise(Net::SMTPFatalError.new("556 5.1.10 invalid destination domain"))
expect {
post "/api/v1/auth/password/forgot", params: { email: user.email }
}.not_to raise_error
expect(response).to have_http_status(:ok)
expect(user.reload.password_reset_digest).to be_present
end
it "returns the same message for unknown emails" do it "returns the same message for unknown emails" do
expect { expect {
post "/api/v1/auth/password/forgot", params: { email: "nobody@example.com" } post "/api/v1/auth/password/forgot", params: { email: "nobody@example.com" }