Skip to content

Commit

Permalink
πŸ§‘β€πŸ’»(setup_dimail_db) create missing access
Browse files Browse the repository at this point in the history
Add missing John Doe access role on domain test.domain.com
  • Loading branch information
sdemagny committed Jan 17, 2025
1 parent ea3a45e commit 6c83294
Show file tree
Hide file tree
Showing 2 changed files with 37 additions and 22 deletions.
19 changes: 12 additions & 7 deletions src/backend/mailbox_manager/management/commands/setup_dimail_db.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
import requests
from rest_framework import status

from mailbox_manager.enums import MailDomainStatusChoices
from mailbox_manager import enums
from mailbox_manager.models import MailDomain, MailDomainAccess

User = get_user_model()
Expand Down Expand Up @@ -65,10 +65,9 @@ def handle(self, *args, **options):

# we create a domain and add John Doe to it
domain_name = "test.domain.com"
if not MailDomain.objects.filter(name=domain_name).exists():
MailDomain.objects.create(
name=domain_name, status=MailDomainStatusChoices.ENABLED
)
domain = MailDomain.objects.get_or_create(
name=domain_name, defaults={"status": enums.MailDomainStatusChoices.ENABLED}
)[0]
self.create_domain(domain_name)

# we create a dimail user for keycloak+regie user John Doe
Expand All @@ -79,8 +78,14 @@ def handle(self, *args, **options):
except User.DoesNotExist:
self.stdout.write("[email protected] user not found", ending="\n")
else:
# create accesses for john doe
self.create_user(name=people_base_user.sub)
self.create_allows(people_base_user.sub, domain_name)
MailDomainAccess.objects.get_or_create(
user=people_base_user,
domain=domain,
defaults={"role": enums.MailDomainRoleChoices.OWNER},
)

if options["populate_from_people"]:
self._populate_dimail_from_people()
Expand Down Expand Up @@ -189,11 +194,11 @@ def _populate_dimail_from_people(self):
# create missing domains
for domain in domain_to_create:
# enforce domain status
if domain.status != MailDomainStatusChoices.ENABLED:
if domain.status != enums.MailDomainStatusChoices.ENABLED:
self.stdout.write(
f" - {domain.name} status {domain.status} -> ENABLED"
)
domain.status = MailDomainStatusChoices.ENABLED
domain.status = enums.MailDomainStatusChoices.ENABLED
domain.save()
self.create_domain(domain.name)

Expand Down
40 changes: 25 additions & 15 deletions src/backend/mailbox_manager/tests/commands/test_dimail_setup_db.py
Original file line number Diff line number Diff line change
Expand Up @@ -101,21 +101,31 @@ def test_commands_setup_dimail_db(settings):
call_command("setup_dimail_db", "--populate-from-people")

# check dimail API received the expected requests
assert len(responses.calls) == 5 + 3
assert responses.calls[5].request.url == f"{DIMAIL_URL}/domains/"
assert responses.calls[5].request.body == (
b'{"name": "some.domain.com", "context_name": "context", '
b'"features": ["webmail", "mailbox", "alias"]}'
)
assert (
len(responses.calls) == 5 + 3 + 3
) # calls for some.domain.com and test.domain.com

assert responses.calls[6].request.url == f"{DIMAIL_URL}/users/"
assert responses.calls[6].request.body == (
b'{"name": "sub.toto.123", "password": "no", "is_admin": false, '
b'"perms": []}'
)
dimail_calls = []
for call in responses.calls[5:]:
dimail_calls.append((call.request.url, call.request.body))

assert responses.calls[7].request.url == f"{DIMAIL_URL}/allows/"
assert (
responses.calls[7].request.body
== b'{"domain": "some.domain.com", "user": "sub.toto.123"}'
)
f"{DIMAIL_URL}/domains/",
(
b'{"name": "some.domain.com", "context_name": "context", '
b'"features": ["webmail", "mailbox", "alias"]}'
),
) in dimail_calls

assert (
f"{DIMAIL_URL}/users/",
(
b'{"name": "sub.toto.123", "password": "no", "is_admin": false, '
b'"perms": []}'
),
) in dimail_calls

assert (
f"{DIMAIL_URL}/allows/",
b'{"domain": "some.domain.com", "user": "sub.toto.123"}',
) in dimail_calls

0 comments on commit 6c83294

Please sign in to comment.