Use different errors when handling mailboxes (#2178)

* Use different errors when handling mailboxes

* Update test
This commit is contained in:
Adrià Casajús 2024-08-02 09:19:27 +02:00 committed by GitHub
parent b823f4359a
commit 9b2e8c2e44
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
3 changed files with 19 additions and 9 deletions

View File

@ -22,6 +22,16 @@ class MailboxError(Exception):
self.msg = msg self.msg = msg
class OnlyPaidError(MailboxError):
def __init__(self):
self.msg = "Only available for paid plans"
class CannotVerifyError(MailboxError):
def __init__(self, msg: str):
self.msg = msg
MAX_ACTIVATION_TRIES = 3 MAX_ACTIVATION_TRIES = 3
@ -36,7 +46,7 @@ def create_mailbox(
LOG.i( LOG.i(
f"User {user} has tried to create mailbox with {email} but is not premium" f"User {user} has tried to create mailbox with {email} but is not premium"
) )
raise MailboxError("Only premium plan can add additional mailbox") raise OnlyPaidError()
if not is_valid_email(email): if not is_valid_email(email):
LOG.i( LOG.i(
f"User {user} has tried to create mailbox with {email} but is not valid email" f"User {user} has tried to create mailbox with {email} but is not valid email"
@ -162,20 +172,20 @@ def verify_mailbox_code(user: User, mailbox_id: int, code: str) -> Mailbox:
if activation.tries >= MAX_ACTIVATION_TRIES: if activation.tries >= MAX_ACTIVATION_TRIES:
LOG.i(f"User {user} failed to verify mailbox {mailbox_id} more than 3 times") LOG.i(f"User {user} failed to verify mailbox {mailbox_id} more than 3 times")
clear_activation_codes_for_mailbox(mailbox) clear_activation_codes_for_mailbox(mailbox)
raise MailboxError("Invalid activation code. Please request another code.") raise CannotVerifyError("Invalid activation code. Please request another code.")
if activation.created_at < arrow.now().shift(minutes=-15): if activation.created_at < arrow.now().shift(minutes=-15):
LOG.i( LOG.i(
f"User {user} failed to verify mailbox {mailbox_id} because code is too old" f"User {user} failed to verify mailbox {mailbox_id} because code is too old"
) )
clear_activation_codes_for_mailbox(mailbox) clear_activation_codes_for_mailbox(mailbox)
raise MailboxError("Invalid activation code. Please request another code.") raise CannotVerifyError("Invalid activation code. Please request another code.")
if code != activation.code: if code != activation.code:
LOG.i( LOG.i(
f"User {user} failed to verify mailbox {mailbox_id} because code does not match" f"User {user} failed to verify mailbox {mailbox_id} because code does not match"
) )
activation.tries = activation.tries + 1 activation.tries = activation.tries + 1
Session.commit() Session.commit()
raise MailboxError("Invalid activation code") raise CannotVerifyError("Invalid activation code")
LOG.i(f"User {user} has verified mailbox {mailbox_id}") LOG.i(f"User {user} has verified mailbox {mailbox_id}")
mailbox.verified = True mailbox.verified = True
clear_activation_codes_for_mailbox(mailbox) clear_activation_codes_for_mailbox(mailbox)

View File

@ -42,7 +42,7 @@ def test_create_mailbox_fail_for_free_user(flask_client):
) )
assert r.status_code == 400 assert r.status_code == 400
assert r.json == {"error": "Only premium plan can add additional mailbox"} assert r.json == {"error": "Only available for paid plans"}
def test_delete_mailbox(flask_client): def test_delete_mailbox(flask_client):

View File

@ -30,7 +30,7 @@ def test_free_user_cannot_add_mailbox():
user.lifetime = False user.lifetime = False
email = random_email() email = random_email()
try: try:
with pytest.raises(mailbox_utils.MailboxError): with pytest.raises(mailbox_utils.OnlyPaidError):
mailbox_utils.create_mailbox(user, email) mailbox_utils.create_mailbox(user, email)
finally: finally:
user.lifetime = True user.lifetime = True
@ -214,7 +214,7 @@ def test_verify_fail():
try: try:
mailbox_utils.verify_mailbox_code(user, mailbox.id, "9999999") mailbox_utils.verify_mailbox_code(user, mailbox.id, "9999999")
assert False, f"test {i}" assert False, f"test {i}"
except mailbox_utils.MailboxError: except mailbox_utils.CannotVerifyError:
activation = MailboxActivation.get_by(mailbox_id=mailbox.id) activation = MailboxActivation.get_by(mailbox_id=mailbox.id)
assert activation.tries == i + 1 assert activation.tries == i + 1
@ -225,7 +225,7 @@ def test_verify_too_may():
activation = MailboxActivation.get_by(mailbox_id=mailbox.id) activation = MailboxActivation.get_by(mailbox_id=mailbox.id)
activation.tries = mailbox_utils.MAX_ACTIVATION_TRIES activation.tries = mailbox_utils.MAX_ACTIVATION_TRIES
Session.commit() Session.commit()
with pytest.raises(mailbox_utils.MailboxError): with pytest.raises(mailbox_utils.CannotVerifyError):
mailbox_utils.verify_mailbox_code(user, mailbox.id, activation.code) mailbox_utils.verify_mailbox_code(user, mailbox.id, activation.code)
@ -235,7 +235,7 @@ def test_verify_too_old_code():
activation = MailboxActivation.get_by(mailbox_id=mailbox.id) activation = MailboxActivation.get_by(mailbox_id=mailbox.id)
activation.created_at = arrow.now().shift(minutes=-30) activation.created_at = arrow.now().shift(minutes=-30)
Session.commit() Session.commit()
with pytest.raises(mailbox_utils.MailboxError): with pytest.raises(mailbox_utils.CannotVerifyError):
mailbox_utils.verify_mailbox_code(user, mailbox.id, activation.code) mailbox_utils.verify_mailbox_code(user, mailbox.id, activation.code)