From 4e78755b0ab4749613de0606199f06b3a7367ab3 Mon Sep 17 00:00:00 2001 From: Marie PUPO JEAMMET Date: Wed, 15 Jan 2025 17:40:30 +0100 Subject: [PATCH] =?UTF-8?q?fixup!=20=E2=9C=A8(dimail)=20send=20pending=20m?= =?UTF-8?q?ailboxes=20upon=20domain=20activation?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../mailbox_manager/api/client/serializers.py | 12 +++----- .../tests/test_admin_actions.py | 9 +++--- .../tests/test_utils_dimail_client.py | 6 ++-- src/backend/mailbox_manager/utils/dimail.py | 30 ++++++++----------- 4 files changed, 24 insertions(+), 33 deletions(-) diff --git a/src/backend/mailbox_manager/api/client/serializers.py b/src/backend/mailbox_manager/api/client/serializers.py index 98de70de6..4d298db9b 100644 --- a/src/backend/mailbox_manager/api/client/serializers.py +++ b/src/backend/mailbox_manager/api/client/serializers.py @@ -36,23 +36,19 @@ def create(self, validated_data): Override create function to fire a request on mailbox creation. """ mailbox = super().create(validated_data) - if validated_data["domain"].status == enums.MailDomainStatusChoices.ENABLED: + if mailbox.domain.status == enums.MailDomainStatusChoices.ENABLED: client = DimailAPIClient() # send new mailbox request to dimail - response = client.create_mailbox( - validated_data, self.context["request"].user.sub - ) + response = client.create_mailbox(mailbox, self.context["request"].user.sub) # fix format to have actual json - mailbox_data = json.loads( - response.content.decode("utf-8").replace("'", '"') - ) + dimail_data = json.loads(response.content.decode("utf-8").replace("'", '"')) mailbox.status = enums.MailDomainStatusChoices.ENABLED mailbox.save() # send confirmation email client.notify_mailbox_creation( - recipient=validated_data["secondary_email"], mailbox_data=mailbox_data + recipient=mailbox.secondary_email, mailbox_data=dimail_data ) # actually save mailbox on our database diff --git a/src/backend/mailbox_manager/tests/test_admin_actions.py b/src/backend/mailbox_manager/tests/test_admin_actions.py index affe2a6d0..65e454a16 100644 --- a/src/backend/mailbox_manager/tests/test_admin_actions.py +++ b/src/backend/mailbox_manager/tests/test_admin_actions.py @@ -95,7 +95,7 @@ def test_admin_action__ready_domain_should_status_from_dimail(client): url = reverse("admin:mailbox_manager_maildomain_changelist") check_response_ok = CHECK_DOMAIN_BROKEN.copy() - check_response_ok["domain"] = domain.name + check_response_ok["name"] = domain.name check_response_ok["state"] = "ok" with responses.RequestsMock() as rsps: @@ -103,14 +103,14 @@ def test_admin_action__ready_domain_should_status_from_dimail(client): rsps.GET, re.compile(rf".*/domains/{domain.name}/check/"), body=json.dumps(check_response_ok), - status=200, + status=status.HTTP_200_OK, content_type="application/json", ) rsps.add( rsps.GET, re.compile(r".*/token/"), body='{"access_token": "domain_owner_token"}', - status=200, + status=status.HTTP_200_OK, content_type="application/json", ) rsps.add( @@ -128,7 +128,8 @@ def test_admin_action__ready_domain_should_status_from_dimail(client): ) response = client.post(url, data, follow=True) - assert response.status_code == 200 + assert response.status_code == status.HTTP_200_OK + assert "Check domains done with success" in response.content.decode("utf-8") domain.refresh_from_db() assert domain.status == enums.MailDomainStatusChoices.ENABLED for mailbox in models.Mailbox.objects.filter(domain=domain): diff --git a/src/backend/mailbox_manager/tests/test_utils_dimail_client.py b/src/backend/mailbox_manager/tests/test_utils_dimail_client.py index d2b9efcf5..5bb37c8a5 100644 --- a/src/backend/mailbox_manager/tests/test_utils_dimail_client.py +++ b/src/backend/mailbox_manager/tests/test_utils_dimail_client.py @@ -212,7 +212,7 @@ def test_dimail___pending_mailboxes_enabled_upon_domain_activation(caplog): re.compile(rf".*/domains/{domain.name}/mailboxes/"), body=str( { - "email": f"new_mailbox@{domain.name}", + "email": f"mock@{domain.name}", "password": "newpass", "uuid": "uuid", } @@ -238,7 +238,7 @@ def test_dimail___pending_mailboxes_enabled_upon_domain_activation(caplog): ) assert ( caplog.records[2].message - == f"Information for mailbox new_mailbox@{domain.name} sent to {pending_mailbox1.secondary_email}." + == f"Information for mailbox mock@{domain.name} sent to {pending_mailbox1.secondary_email}." ) assert ( caplog.records[4].message @@ -246,5 +246,5 @@ def test_dimail___pending_mailboxes_enabled_upon_domain_activation(caplog): ) assert ( caplog.records[5].message - == f"Information for mailbox new_mailbox@{domain.name} sent to {pending_mailbox2.secondary_email}." + == f"Information for mailbox mock@{domain.name} sent to {pending_mailbox2.secondary_email}." ) diff --git a/src/backend/mailbox_manager/utils/dimail.py b/src/backend/mailbox_manager/utils/dimail.py index 56c50d071..625631ab3 100644 --- a/src/backend/mailbox_manager/utils/dimail.py +++ b/src/backend/mailbox_manager/utils/dimail.py @@ -116,15 +116,15 @@ def create_mailbox(self, mailbox, user_sub=None): """Send a CREATE mailbox request to mail provisioning API.""" payload = { - "givenName": mailbox["first_name"], - "surName": mailbox["last_name"], - "displayName": f"{mailbox['first_name']} {mailbox['last_name']}", + "givenName": mailbox.first_name, + "surName": mailbox.last_name, + "displayName": f"{mailbox.first_name} {mailbox.last_name}", } headers = self.get_headers(user_sub) try: response = session.post( - f"{self.API_URL}/domains/{mailbox['domain']}/mailboxes/{mailbox['local_part']}", + f"{self.API_URL}/domains/{mailbox.domain.name}/mailboxes/{mailbox.local_part}", json=payload, headers=headers, verify=True, @@ -141,7 +141,7 @@ def create_mailbox(self, mailbox, user_sub=None): if response.status_code == status.HTTP_201_CREATED: logger.info( "Mailbox successfully created on domain %s by user %s", - str(mailbox["domain"]), + str(mailbox.domain), user_sub, ) return response @@ -149,7 +149,7 @@ def create_mailbox(self, mailbox, user_sub=None): if response.status_code == status.HTTP_403_FORBIDDEN: logger.error( "[DIMAIL] 403 Forbidden: you cannot access domain %s", - str(mailbox["domain"]), + str(mailbox.domain), ) raise exceptions.PermissionDenied( "Permission denied. Please check your MAIL_PROVISIONING_API_CREDENTIALS." @@ -410,6 +410,7 @@ def fetch_domain_status(self, domain): domain.status != enums.MailDomainStatusChoices.ENABLED and dimail_status == "ok" ): + self.enable_pending_mailboxes(domain) domain.status = enums.MailDomainStatusChoices.ENABLED domain.save() elif ( @@ -422,28 +423,21 @@ def fetch_domain_status(self, domain): return self.raise_exception_for_unexpected_response(response) def enable_pending_mailboxes(self, domain): - """Send request for all pending mailboxes of a domain.""" + """Send requests for all pending mailboxes of a domain.""" pending_mailboxes = models.Mailbox.objects.filter( domain=domain, status=enums.MailboxStatusChoices.PENDING ) for mailbox in pending_mailboxes: - from mailbox_manager.api.client import serializers - - serialized_mailbox = serializers.MailboxSerializer(mailbox).data - serialized_mailbox["domain"] = domain - - response = self.create_mailbox(serialized_mailbox) + response = self.create_mailbox(mailbox) # fix format to have actual json - mailbox_data = json.loads( - response.content.decode("utf-8").replace("'", '"') - ) + dimail_data = json.loads(response.content.decode("utf-8").replace("'", '"')) mailbox.status = enums.MailDomainStatusChoices.ENABLED mailbox.save() # send confirmation email self.notify_mailbox_creation( - recipient=serialized_mailbox["secondary_email"], - mailbox_data=mailbox_data, + recipient=mailbox.secondary_email, + mailbox_data=dimail_data, )