diff --git a/app/mailbox_utils.py b/app/mailbox_utils.py index 194995679..bc7f86b6e 100644 --- a/app/mailbox_utils.py +++ b/app/mailbox_utils.py @@ -37,8 +37,9 @@ def __init__(self): class CannotVerifyError(MailboxError): - def __init__(self, msg: str): + def __init__(self, msg: str, deleted_activation_code: bool = False): self.msg = msg + self.deleted_activation_code = deleted_activation_code MAX_ACTIVATION_TRIES = 3 @@ -196,7 +197,10 @@ def verify_mailbox_code(user: User, mailbox_id: int, code: str) -> Mailbox: if activation.tries >= MAX_ACTIVATION_TRIES: LOG.i(f"User {user} failed to verify mailbox {mailbox_id} more than 3 times") clear_activation_codes_for_mailbox(mailbox) - raise CannotVerifyError("Invalid activation code. Please request another code.") + raise CannotVerifyError( + "Invalid activation code. Please request another code.", + deleted_activation_code=True, + ) if activation.created_at < arrow.now().shift(minutes=-15): LOG.i( f"User {user} failed to verify mailbox {mailbox_id} because code is too old" diff --git a/tests/test_mailbox_utils.py b/tests/test_mailbox_utils.py index 030bf1301..d716b3135 100644 --- a/tests/test_mailbox_utils.py +++ b/tests/test_mailbox_utils.py @@ -314,10 +314,13 @@ def test_verify_too_may(): output = mailbox_utils.create_mailbox(user, random_email()) output.activation.tries = mailbox_utils.MAX_ACTIVATION_TRIES Session.commit() - with pytest.raises(mailbox_utils.CannotVerifyError): + try: mailbox_utils.verify_mailbox_code( user, output.mailbox.id, output.activation.code ) + assert False + except mailbox_utils.CannotVerifyError as e: + assert e.deleted_activation_code @mail_sender.store_emails_test_decorator