From fc7a8329697bd652cddb225423a02583fa2be553 Mon Sep 17 00:00:00 2001 From: Son NK Date: Sat, 25 Jan 2020 22:40:30 +0700 Subject: [PATCH] add can_be_used_as_personal_email() --- app/email_utils.py | 20 ++++++++++++++++++++ tests/test_email_utils.py | 19 +++++++++++++++++++ 2 files changed, 39 insertions(+) diff --git a/app/email_utils.py b/app/email_utils.py index 0ae8a5ff..567c4256 100644 --- a/app/email_utils.py +++ b/app/email_utils.py @@ -288,3 +288,23 @@ def email_belongs_to_alias_domains(email: str) -> bool: return True return False + + +def can_be_used_as_personal_email(email: str) -> bool: + """return True if an email can be used as a personal email. Currently the only condition is email domain is not + - one of ALIAS_DOMAINS + - one of custom domains + """ + domain = get_email_domain_part(email) + if not domain: + return False + + if domain in ALIAS_DOMAINS: + return False + + from app.models import CustomDomain + + if CustomDomain.get_by(domain=domain, verified=True): + return False + + return True diff --git a/tests/test_email_utils.py b/tests/test_email_utils.py index 0b5aad55..561c2a71 100644 --- a/tests/test_email_utils.py +++ b/tests/test_email_utils.py @@ -4,7 +4,10 @@ from app.email_utils import ( get_email_local_part, get_email_domain_part, email_belongs_to_alias_domains, + can_be_used_as_personal_email, ) +from app.extensions import db +from app.models import User, CustomDomain def test_get_email_name(): @@ -36,3 +39,19 @@ def test_email_belongs_to_alias_domains(): assert email_belongs_to_alias_domains("hey@d1.test") assert not email_belongs_to_alias_domains("hey@d3.test") + + +def test_can_be_used_as_personal_email(flask_client): + # default alias domain + assert not can_be_used_as_personal_email("ab@sl.local") + assert not can_be_used_as_personal_email("hey@d1.test") + + assert can_be_used_as_personal_email("hey@ab.cd") + # custom domain + user = User.create( + email="a@b.c", password="password", name="Test User", activated=True + ) + db.session.commit() + CustomDomain.create(user_id=user.id, domain="ab.cd", verified=True) + db.session.commit() + assert not can_be_used_as_personal_email("hey@ab.cd")