From 6c8329405da8225aa252d99656b6fed6687765ee Mon Sep 17 00:00:00 2001 From: Sabrina Demagny Date: Thu, 26 Dec 2024 18:18:20 +0100 Subject: [PATCH] =?UTF-8?q?=F0=9F=A7=91=E2=80=8D=F0=9F=92=BB(setup=5Fdimai?= =?UTF-8?q?l=5Fdb)=20create=20missing=20access?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Add missing John Doe access role on domain test.domain.com --- .../management/commands/setup_dimail_db.py | 19 +++++---- .../tests/commands/test_dimail_setup_db.py | 40 ++++++++++++------- 2 files changed, 37 insertions(+), 22 deletions(-) diff --git a/src/backend/mailbox_manager/management/commands/setup_dimail_db.py b/src/backend/mailbox_manager/management/commands/setup_dimail_db.py index d4e799c93..34653ef61 100644 --- a/src/backend/mailbox_manager/management/commands/setup_dimail_db.py +++ b/src/backend/mailbox_manager/management/commands/setup_dimail_db.py @@ -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() @@ -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 @@ -79,8 +78,14 @@ def handle(self, *args, **options): except User.DoesNotExist: self.stdout.write("people@people.world 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() @@ -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) diff --git a/src/backend/mailbox_manager/tests/commands/test_dimail_setup_db.py b/src/backend/mailbox_manager/tests/commands/test_dimail_setup_db.py index b65a49e0f..dfc818701 100644 --- a/src/backend/mailbox_manager/tests/commands/test_dimail_setup_db.py +++ b/src/backend/mailbox_manager/tests/commands/test_dimail_setup_db.py @@ -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