🧑‍💻(setup_dimail_db) create missing access

Add missing John Doe access role on domain test.domain.com
This commit is contained in:
Sabrina Demagny
2024-12-26 18:18:20 +01:00
parent ea3a45ea87
commit 6c8329405d
2 changed files with 39 additions and 24 deletions

View File

@@ -7,7 +7,7 @@ from django.core.management.base import BaseCommand, CommandError
import requests import requests
from rest_framework import status from rest_framework import status
from mailbox_manager.enums import MailDomainStatusChoices from mailbox_manager import enums
from mailbox_manager.models import MailDomain, MailDomainAccess from mailbox_manager.models import MailDomain, MailDomainAccess
User = get_user_model() User = get_user_model()
@@ -65,10 +65,9 @@ class Command(BaseCommand):
# we create a domain and add John Doe to it # we create a domain and add John Doe to it
domain_name = "test.domain.com" domain_name = "test.domain.com"
if not MailDomain.objects.filter(name=domain_name).exists(): domain = MailDomain.objects.get_or_create(
MailDomain.objects.create( name=domain_name, defaults={"status": enums.MailDomainStatusChoices.ENABLED}
name=domain_name, status=MailDomainStatusChoices.ENABLED )[0]
)
self.create_domain(domain_name) self.create_domain(domain_name)
# we create a dimail user for keycloak+regie user John Doe # we create a dimail user for keycloak+regie user John Doe
@@ -79,8 +78,14 @@ class Command(BaseCommand):
except User.DoesNotExist: except User.DoesNotExist:
self.stdout.write("people@people.world user not found", ending="\n") self.stdout.write("people@people.world user not found", ending="\n")
else: else:
# create accesses for john doe
self.create_user(name=people_base_user.sub) self.create_user(name=people_base_user.sub)
self.create_allows(people_base_user.sub, domain_name) 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"]: if options["populate_from_people"]:
self._populate_dimail_from_people() self._populate_dimail_from_people()
@@ -189,11 +194,11 @@ class Command(BaseCommand):
# create missing domains # create missing domains
for domain in domain_to_create: for domain in domain_to_create:
# enforce domain status # enforce domain status
if domain.status != MailDomainStatusChoices.ENABLED: if domain.status != enums.MailDomainStatusChoices.ENABLED:
self.stdout.write( self.stdout.write(
f" - {domain.name} status {domain.status} -> ENABLED" f" - {domain.name} status {domain.status} -> ENABLED"
) )
domain.status = MailDomainStatusChoices.ENABLED domain.status = enums.MailDomainStatusChoices.ENABLED
domain.save() domain.save()
self.create_domain(domain.name) self.create_domain(domain.name)

View File

@@ -101,21 +101,31 @@ def test_commands_setup_dimail_db(settings):
call_command("setup_dimail_db", "--populate-from-people") call_command("setup_dimail_db", "--populate-from-people")
# check dimail API received the expected requests # 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 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": []}'
)
assert responses.calls[7].request.url == f"{DIMAIL_URL}/allows/"
assert ( assert (
responses.calls[7].request.body len(responses.calls) == 5 + 3 + 3
== b'{"domain": "some.domain.com", "user": "sub.toto.123"}' ) # calls for some.domain.com and test.domain.com
)
dimail_calls = []
for call in responses.calls[5:]:
dimail_calls.append((call.request.url, call.request.body))
assert (
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