From 2478def834db717cc9d37fe4364f0a7a427255db Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Adri=C3=A0=20Casaj=C3=BAs?= Date: Thu, 1 Aug 2024 17:18:51 +0200 Subject: [PATCH] Allow to create pre-verified mailboxes --- app/mailbox_utils.py | 24 ++++++++------ tests/test_mailbox_utils.py | 31 ++++++++++++------- .../user_settings/test_set_default_mailbox.py | 6 ++-- 3 files changed, 38 insertions(+), 23 deletions(-) diff --git a/app/mailbox_utils.py b/app/mailbox_utils.py index c4a00ebe..47755223 100644 --- a/app/mailbox_utils.py +++ b/app/mailbox_utils.py @@ -28,8 +28,9 @@ MAX_ACTIVATION_TRIES = 3 def create_mailbox( user: User, email: str, + verified: bool = False, use_digit_codes: bool = False, - send_verification_link: bool = True, + send_link: bool = True, ) -> Mailbox: if not user.is_premium(): LOG.i( @@ -51,15 +52,20 @@ def create_mailbox( f"User {user} has tried to create mailbox with {email} but email is invalid" ) raise MailboxError("Invalid email") - new_mailbox = Mailbox.create(email=email, user_id=user.id, commit=True) - - 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_verification_link, + new_mailbox = Mailbox.create( + email=email, user_id=user.id, verified=verified, commit=True ) + + 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 diff --git a/tests/test_mailbox_utils.py b/tests/test_mailbox_utils.py index 03560edb..7e076955 100644 --- a/tests/test_mailbox_utils.py +++ b/tests/test_mailbox_utils.py @@ -68,12 +68,23 @@ def test_create_mailbox(): assert mail_sent.envelope_to == email +@mail_sender.store_emails_test_decorator +def test_create_mailbox_verified(): + email = random_email() + mailbox_utils.create_mailbox(user, email, verified=True) + mailbox = Mailbox.get_by(email=email) + assert mailbox is not None + assert mailbox.verified + activation = MailboxActivation.get_by(mailbox_id=mailbox.id) + assert activation is None + + assert 0 == len(mail_sender.get_stored_emails()) + + @mail_sender.store_emails_test_decorator def test_create_mailbox_with_digits(): email = random_email() - mailbox_utils.create_mailbox( - user, email, use_digit_codes=True, send_verification_link=False - ) + mailbox_utils.create_mailbox(user, email, use_digit_codes=True, send_link=False) mailbox = Mailbox.get_by(email=email) assert mailbox is not None assert not mailbox.verified @@ -93,9 +104,7 @@ def test_create_mailbox_with_digits(): @mail_sender.store_emails_test_decorator def test_send_verification_email(): email = random_email() - mailbox_utils.create_mailbox( - user, email, use_digit_codes=True, send_verification_link=False - ) + 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 @@ -121,7 +130,7 @@ def test_delete_default_mailbox(): def test_transfer_to_same_mailbox(): email = random_email() mailbox = mailbox_utils.create_mailbox( - user, email, use_digit_codes=True, send_verification_link=False + user, email, use_digit_codes=True, send_link=False ) with pytest.raises(mailbox_utils.MailboxError): mailbox_utils.delete_mailbox(user, mailbox.id, transfer_mailbox_id=mailbox.id) @@ -130,7 +139,7 @@ def test_transfer_to_same_mailbox(): def test_transfer_to_other_users_mailbox(): email = random_email() mailbox = mailbox_utils.create_mailbox( - user, email, use_digit_codes=True, send_verification_link=False + user, email, use_digit_codes=True, send_link=False ) other = create_new_user() other_mailbox = Mailbox.create(user_id=other.id, email=random_email(), commit=True) @@ -143,7 +152,7 @@ def test_transfer_to_other_users_mailbox(): def test_delete_with_no_transfer(): email = random_email() mailbox = mailbox_utils.create_mailbox( - user, email, use_digit_codes=True, send_verification_link=False + user, email, use_digit_codes=True, send_link=False ) mailbox_utils.delete_mailbox(user, mailbox.id, transfer_mailbox_id=None) job = Session.query(Job).order_by(Job.id.desc()).first() @@ -155,10 +164,10 @@ def test_delete_with_no_transfer(): def test_delete_with_transfer(): mailbox = mailbox_utils.create_mailbox( - user, random_email(), use_digit_codes=True, send_verification_link=False + user, random_email(), use_digit_codes=True, send_link=False ) transfer_mailbox = mailbox_utils.create_mailbox( - user, random_email(), use_digit_codes=True, send_verification_link=False + user, random_email(), use_digit_codes=True, send_link=False ) mailbox_utils.delete_mailbox( user, mailbox.id, transfer_mailbox_id=transfer_mailbox.id diff --git a/tests/user_settings/test_set_default_mailbox.py b/tests/user_settings/test_set_default_mailbox.py index fbaed22f..5853859e 100644 --- a/tests/user_settings/test_set_default_mailbox.py +++ b/tests/user_settings/test_set_default_mailbox.py @@ -29,7 +29,7 @@ def test_set_default_mailbox(): other, random_email(), use_digit_codes=True, - send_verification_link=False, + send_link=False, ) mailbox.verified = True Session.commit() @@ -43,7 +43,7 @@ def test_cannot_set_unverified(): user, random_email(), use_digit_codes=True, - send_verification_link=False, + send_link=False, ) with pytest.raises(user_settings.CannotSetMailbox): user_settings.set_default_mailbox(user, mailbox.id) @@ -55,7 +55,7 @@ def test_cannot_default_other_user_mailbox(): other, random_email(), use_digit_codes=True, - send_verification_link=False, + send_link=False, ) with pytest.raises(user_settings.CannotSetMailbox): user_settings.set_default_mailbox(user, mailbox.id)