Allow to create pre-verified mailboxes

This commit is contained in:
Adrià Casajús 2024-08-01 17:18:51 +02:00
parent 5b784e8989
commit 2478def834
No known key found for this signature in database
GPG Key ID: F0033226A5AFC9B9
3 changed files with 38 additions and 23 deletions

View File

@ -28,8 +28,9 @@ MAX_ACTIVATION_TRIES = 3
def create_mailbox( def create_mailbox(
user: User, user: User,
email: str, email: str,
verified: bool = False,
use_digit_codes: bool = False, use_digit_codes: bool = False,
send_verification_link: bool = True, send_link: bool = True,
) -> Mailbox: ) -> Mailbox:
if not user.is_premium(): if not user.is_premium():
LOG.i( LOG.i(
@ -51,14 +52,19 @@ def create_mailbox(
f"User {user} has tried to create mailbox with {email} but email is invalid" f"User {user} has tried to create mailbox with {email} but email is invalid"
) )
raise MailboxError("Invalid email") raise MailboxError("Invalid email")
new_mailbox = Mailbox.create(email=email, user_id=user.id, commit=True) 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}") LOG.i(f"User {user} has created mailbox with {email}")
send_verification_email( send_verification_email(
user, user,
new_mailbox, new_mailbox,
use_digit_code=use_digit_codes, use_digit_code=use_digit_codes,
send_link=send_verification_link, send_link=send_link,
) )
return new_mailbox return new_mailbox

View File

@ -68,12 +68,23 @@ def test_create_mailbox():
assert mail_sent.envelope_to == email 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 @mail_sender.store_emails_test_decorator
def test_create_mailbox_with_digits(): def test_create_mailbox_with_digits():
email = random_email() email = random_email()
mailbox_utils.create_mailbox( mailbox_utils.create_mailbox(user, email, use_digit_codes=True, send_link=False)
user, email, use_digit_codes=True, send_verification_link=False
)
mailbox = Mailbox.get_by(email=email) mailbox = Mailbox.get_by(email=email)
assert mailbox is not None assert mailbox is not None
assert not mailbox.verified assert not mailbox.verified
@ -93,9 +104,7 @@ def test_create_mailbox_with_digits():
@mail_sender.store_emails_test_decorator @mail_sender.store_emails_test_decorator
def test_send_verification_email(): def test_send_verification_email():
email = random_email() email = random_email()
mailbox_utils.create_mailbox( mailbox_utils.create_mailbox(user, email, use_digit_codes=True, send_link=False)
user, email, use_digit_codes=True, send_verification_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 old_code = activation.code
@ -121,7 +130,7 @@ def test_delete_default_mailbox():
def test_transfer_to_same_mailbox(): def test_transfer_to_same_mailbox():
email = random_email() email = random_email()
mailbox = mailbox_utils.create_mailbox( 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): with pytest.raises(mailbox_utils.MailboxError):
mailbox_utils.delete_mailbox(user, mailbox.id, transfer_mailbox_id=mailbox.id) 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(): def test_transfer_to_other_users_mailbox():
email = random_email() email = random_email()
mailbox = mailbox_utils.create_mailbox( 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 = create_new_user()
other_mailbox = Mailbox.create(user_id=other.id, email=random_email(), commit=True) 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(): def test_delete_with_no_transfer():
email = random_email() email = random_email()
mailbox = mailbox_utils.create_mailbox( 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) mailbox_utils.delete_mailbox(user, mailbox.id, transfer_mailbox_id=None)
job = Session.query(Job).order_by(Job.id.desc()).first() 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(): def test_delete_with_transfer():
mailbox = mailbox_utils.create_mailbox( 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( 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( mailbox_utils.delete_mailbox(
user, mailbox.id, transfer_mailbox_id=transfer_mailbox.id user, mailbox.id, transfer_mailbox_id=transfer_mailbox.id

View File

@ -29,7 +29,7 @@ def test_set_default_mailbox():
other, other,
random_email(), random_email(),
use_digit_codes=True, use_digit_codes=True,
send_verification_link=False, send_link=False,
) )
mailbox.verified = True mailbox.verified = True
Session.commit() Session.commit()
@ -43,7 +43,7 @@ def test_cannot_set_unverified():
user, user,
random_email(), random_email(),
use_digit_codes=True, use_digit_codes=True,
send_verification_link=False, send_link=False,
) )
with pytest.raises(user_settings.CannotSetMailbox): with pytest.raises(user_settings.CannotSetMailbox):
user_settings.set_default_mailbox(user, mailbox.id) user_settings.set_default_mailbox(user, mailbox.id)
@ -55,7 +55,7 @@ def test_cannot_default_other_user_mailbox():
other, other,
random_email(), random_email(),
use_digit_codes=True, use_digit_codes=True,
send_verification_link=False, send_link=False,
) )
with pytest.raises(user_settings.CannotSetMailbox): with pytest.raises(user_settings.CannotSetMailbox):
user_settings.set_default_mailbox(user, mailbox.id) user_settings.set_default_mailbox(user, mailbox.id)