Allow to skip sending the mailbox verification email when creating a mailbox

This commit is contained in:
Adrià Casajús 2024-08-02 14:28:18 +02:00
parent 9b2e8c2e44
commit b0849bff6d
No known key found for this signature in database
GPG Key ID: F0033226A5AFC9B9
2 changed files with 47 additions and 18 deletions

View File

@ -39,6 +39,7 @@ def create_mailbox(
user: User, user: User,
email: str, email: str,
verified: bool = False, verified: bool = False,
send_email: bool = True,
use_digit_codes: bool = False, use_digit_codes: bool = False,
send_link: bool = True, send_link: bool = True,
) -> Mailbox: ) -> Mailbox:
@ -68,12 +69,13 @@ def create_mailbox(
if verified: if verified:
LOG.i(f"User {user} as created a pre-verified mailbox with {email}") LOG.i(f"User {user} as created a pre-verified mailbox with {email}")
else: return new_mailbox
LOG.i(f"User {user} has created mailbox with {email}") 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( send_verification_email(
user, user,
new_mailbox, new_mailbox,
use_digit_code=use_digit_codes, activation=activation,
send_link=send_link, send_link=send_link,
) )
return new_mailbox return new_mailbox
@ -192,29 +194,34 @@ def verify_mailbox_code(user: User, mailbox_id: int, code: str) -> Mailbox:
return mailbox return mailbox
def send_verification_email( def generate_activation_code(
user: User, mailbox: Mailbox, use_digit_code: bool = False, send_link: bool = True mailbox: Mailbox, use_digit_code: bool = False
): ) -> MailboxActivation:
clear_activation_codes_for_mailbox(mailbox) clear_activation_codes_for_mailbox(mailbox)
if use_digit_code: if use_digit_code:
code = "{:06d}".format(random.randint(1, 999999)) code = "{:06d}".format(random.randint(1, 999999))
else: else:
code = secrets.token_urlsafe(16) code = secrets.token_urlsafe(16)
activation = MailboxActivation.create( return MailboxActivation.create(
mailbox_id=mailbox.id, mailbox_id=mailbox.id,
code=code, code=code,
tries=0, tries=0,
commit=True,
) )
Session.commit()
def send_verification_email(
user: User, mailbox: Mailbox, activation: MailboxActivation, send_link: bool = True
):
LOG.i( 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: if send_link:
verification_url = ( verification_url = (
config.URL config.URL
+ "/dashboard/mailbox_verify" + "/dashboard/mailbox_verify"
+ f"?mailbox_id={mailbox.id}&code={code}" + f"?mailbox_id={mailbox.id}&code={activation.code}"
) )
else: else:
verification_url = None verification_url = None

View File

@ -107,10 +107,32 @@ def test_send_verification_email():
mailbox_utils.create_mailbox(user, email, use_digit_codes=True, send_link=False) mailbox_utils.create_mailbox(user, email, use_digit_codes=True, send_link=False)
mailbox = Mailbox.get_by(email=email) mailbox = Mailbox.get_by(email=email)
activation = MailboxActivation.get_by(mailbox_id=mailbox.id) activation = MailboxActivation.get_by(mailbox_id=mailbox.id)
old_code = activation.code mail_sender.purge_stored_emails()
mailbox_utils.send_verification_email(user, mailbox) 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) 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(): def test_delete_other_user_mailbox():