Skip to content

Commit

Permalink
chore: allow to specify partner_id to custom_domain
Browse files Browse the repository at this point in the history
  • Loading branch information
cquintana92 committed Sep 16, 2024
1 parent 474de77 commit f940df0
Show file tree
Hide file tree
Showing 2 changed files with 39 additions and 1 deletion.
8 changes: 7 additions & 1 deletion app/custom_domain_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,9 @@ def can_domain_be_used(user: User, domain: str) -> Optional[str]:
return None


def create_custom_domain(user: User, domain: str) -> CreateCustomDomainResult:
def create_custom_domain(
user: User, domain: str, partner_id: Optional[int] = None
) -> CreateCustomDomainResult:
if not user.is_premium():
return CreateCustomDomainResult(
message="Only premium plan can add custom domain",
Expand All @@ -91,6 +93,10 @@ def create_custom_domain(user: User, domain: str) -> CreateCustomDomainResult:
)
new_custom_domain.ownership_verified = True

# Add the partner_id in case it's passed
if partner_id is not None:
new_custom_domain.partner_id = partner_id

Session.commit()

return CreateCustomDomainResult(
Expand Down
32 changes: 32 additions & 0 deletions tests/test_custom_domain_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
)
from app.db import Session
from app.models import User, CustomDomain, Mailbox
from tests.utils import get_proton_partner
from tests.utils import create_new_user, random_string, random_domain

user: Optional[User] = None
Expand Down Expand Up @@ -108,3 +109,34 @@ def test_can_create_custom_domain():

assert res.instance.domain == domain
assert res.instance.user_id == user.id


def test_can_create_custom_domain_validates_if_parent_is_validated():
root_domain = random_domain()
subdomain = f"{random_string(10)}.{root_domain}"

# Create custom domain with the root domain
CustomDomain.create(
user_id=user.id,
domain=root_domain,
verified=True,
ownership_verified=True,
commit=True,
)

# Create custom domain with subdomain. Should automatically be verified
res = create_custom_domain(user=user, domain=subdomain)
assert res.success is True
assert res.instance.domain == subdomain
assert res.instance.user_id == user.id
assert res.instance.ownership_verified is True


def test_creates_custom_domain_with_partner_id():
domain = random_domain()
proton_partner = get_proton_partner()
res = create_custom_domain(user=user, domain=domain, partner_id=proton_partner.id)
assert res.success is True
assert res.instance.domain == domain
assert res.instance.user_id == user.id
assert res.instance.partner_id == proton_partner.id

0 comments on commit f940df0

Please sign in to comment.