From b0849bff6ddc162c3e8c18ee52318ee7bcf10ad8 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Adri=C3=A0=20Casaj=C3=BAs?= Date: Fri, 2 Aug 2024 14:28:18 +0200 Subject: [PATCH] Allow to skip sending the mailbox verification email when creating a mailbox --- app/mailbox_utils.py | 37 ++++++++++++++++++++++--------------- tests/test_mailbox_utils.py | 28 +++++++++++++++++++++++++--- 2 files changed, 47 insertions(+), 18 deletions(-) diff --git a/app/mailbox_utils.py b/app/mailbox_utils.py index ad37b718..43072fdc 100644 --- a/app/mailbox_utils.py +++ b/app/mailbox_utils.py @@ -39,6 +39,7 @@ def create_mailbox( user: User, email: str, verified: bool = False, + send_email: bool = True, use_digit_codes: bool = False, send_link: bool = True, ) -> Mailbox: @@ -68,14 +69,15 @@ def create_mailbox( if verified: LOG.i(f"User {user} as created a pre-verified mailbox with {email}") - else: - LOG.i(f"User {user} has created mailbox with {email}") - send_verification_email( - user, - new_mailbox, - use_digit_code=use_digit_codes, - send_link=send_link, - ) + return new_mailbox + LOG.i(f"User {user} has created mailbox with {email}") + activation = generate_activation_code(new_mailbox, use_digit_code=use_digit_codes) + send_verification_email( + user, + new_mailbox, + activation=activation, + send_link=send_link, + ) return new_mailbox @@ -192,29 +194,34 @@ def verify_mailbox_code(user: User, mailbox_id: int, code: str) -> Mailbox: return mailbox -def send_verification_email( - user: User, mailbox: Mailbox, use_digit_code: bool = False, send_link: bool = True -): +def generate_activation_code( + mailbox: Mailbox, use_digit_code: bool = False +) -> MailboxActivation: clear_activation_codes_for_mailbox(mailbox) if use_digit_code: code = "{:06d}".format(random.randint(1, 999999)) else: code = secrets.token_urlsafe(16) - activation = MailboxActivation.create( + return MailboxActivation.create( mailbox_id=mailbox.id, code=code, tries=0, + commit=True, ) - Session.commit() + + +def send_verification_email( + user: User, mailbox: Mailbox, activation: MailboxActivation, send_link: bool = True +): LOG.i( - f"Sending mailbox verification email to {mailbox.email} with digit={use_digit_code} link={send_link}" + f"Sending mailbox verification email to {mailbox.email} with send link={send_link}" ) if send_link: verification_url = ( config.URL + "/dashboard/mailbox_verify" - + f"?mailbox_id={mailbox.id}&code={code}" + + f"?mailbox_id={mailbox.id}&code={activation.code}" ) else: verification_url = None diff --git a/tests/test_mailbox_utils.py b/tests/test_mailbox_utils.py index 1b37345d..8e8d97e1 100644 --- a/tests/test_mailbox_utils.py +++ b/tests/test_mailbox_utils.py @@ -107,10 +107,32 @@ def test_send_verification_email(): mailbox_utils.create_mailbox(user, email, use_digit_codes=True, send_link=False) mailbox = Mailbox.get_by(email=email) activation = MailboxActivation.get_by(mailbox_id=mailbox.id) - old_code = activation.code - mailbox_utils.send_verification_email(user, mailbox) + mail_sender.purge_stored_emails() + mailbox_utils.send_verification_email(user, mailbox, activation, send_link=False) + + assert 1 == len(mail_sender.get_stored_emails()) + mail_sent = mail_sender.get_stored_emails()[0] + mail_contents = str(mail_sent.msg) + assert mail_contents.find(activation.code) > 0 + assert mail_contents.find(config.URL) == -1 + assert mail_sent.envelope_to == email + + +@mail_sender.store_emails_test_decorator +def test_send_verification_email_with_link(): + email = random_email() + mailbox_utils.create_mailbox(user, email, use_digit_codes=True, send_link=False) + mailbox = Mailbox.get_by(email=email) activation = MailboxActivation.get_by(mailbox_id=mailbox.id) - assert activation.code != old_code + mail_sender.purge_stored_emails() + mailbox_utils.send_verification_email(user, mailbox, activation, send_link=True) + + assert 1 == len(mail_sender.get_stored_emails()) + mail_sent = mail_sender.get_stored_emails()[0] + mail_contents = str(mail_sent.msg) + assert mail_contents.find(activation.code) > 0 + assert mail_contents.find(config.URL) > -1 + assert mail_sent.envelope_to == email def test_delete_other_user_mailbox():