1
0
Fork 0
mirror of https://github.com/retspen/webvirtcloud synced 2026-11-11 12:46:49 +00:00

fix(accounts): a failed OTP mail leaves the user without OTP

The admin's "Email OTP QR code" made the user's TOTP device and then sent
the mail. When the SMTP server refused it, the page said so, but the
device stayed: the user's next login asked for a code from a QR code they
never got. The device and the mail now share one transaction.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01Kk4h2TcUiaWmU5MUszYSea
This commit is contained in:
catborise 2026-11-10 07:58:46 +03:00
parent cb4fd4185b
commit fa0f7f79ac
2 changed files with 8 additions and 1 deletions

View file

@ -3,6 +3,7 @@ from unittest.mock import patch
from django.contrib.auth import get_user_model
from django.test import TestCase
from django.urls import reverse
from django_otp.plugins.otp_totp.models import TOTPDevice
class AdminEmailOtpTestCase(TestCase):
@ -31,6 +32,11 @@ class AdminEmailOtpTestCase(TestCase):
self.assertEqual(response.status_code, 302)
mock_send.assert_called_once()
@patch("accounts.views.send_email_with_otp", side_effect=ConnectionRefusedError)
def test_a_mail_that_did_not_go_leaves_the_user_without_otp(self, mock_send):
self.client.post(self.url)
self.assertFalse(TOTPDevice.objects.filter(user=self.user).exists())
def test_the_smtp_connection_waits_a_bounded_time(self):
from django.core.mail.backends.smtp import EmailBackend

View file

@ -380,7 +380,8 @@ def admin_email_otp(request, user_id):
subject = f"User {user.username}"
if user.email != "":
try:
with audit(request, None, subject, "mail OTP QR code"):
# a device made for a mail that did not go would ask for codes the user never got
with audit(request, None, subject, "mail OTP QR code"), transaction.atomic():
get_user_totp_device(user)
send_email_with_otp(request, user)
except OSError as err: # the SMTP server refused it, or did not answer within EMAIL_TIMEOUT