diff --git a/CHANGELOG.md b/CHANGELOG.md index c0544d166..3ebea00dd 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -10,6 +10,7 @@ and this project adheres to ### Added +- ✨(dimail) send pending mailboxes upon domain activation - 🧑‍💻(oidc) add ability to pull registration ID (e.g. SIRET) from OIDC #577 ### Fixed diff --git a/src/backend/mailbox_manager/api/client/serializers.py b/src/backend/mailbox_manager/api/client/serializers.py index 9de76208b..98de70de6 100644 --- a/src/backend/mailbox_manager/api/client/serializers.py +++ b/src/backend/mailbox_manager/api/client/serializers.py @@ -43,7 +43,7 @@ def create(self, validated_data): validated_data, self.context["request"].user.sub ) - # fix format to have actual json, and remove uuid + # fix format to have actual json mailbox_data = json.loads( response.content.decode("utf-8").replace("'", '"') ) 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 970cdaa0e..b9392ce0a 100644 --- a/src/backend/mailbox_manager/tests/test_utils_dimail_client.py +++ b/src/backend/mailbox_manager/tests/test_utils_dimail_client.py @@ -12,6 +12,8 @@ import responses from rest_framework import status +from core import factories as core_factories + from mailbox_manager import enums, factories, models from mailbox_manager.utils.dimail import DimailAPIClient @@ -20,6 +22,12 @@ pytestmark = pytest.mark.django_db +@pytest.fixture +def authenticated_user(): + """Create an user""" + return core_factories.UserFactory() + + def test_dimail_synchronization__already_sync(): """ No mailbox should be created when everything is already synced. @@ -176,3 +184,21 @@ def test_dimail__fetch_domain_status_from_dimail(): response = dimail_client.fetch_domain_status(domain) assert response.status_code == status.HTTP_200_OK assert domain.status == enums.MailDomainStatusChoices.FAILED + + +def test_dimail___pending_mailboxes_enabled_upon_domain_activation(caplog): + """Status of pending mailboxes should switch to "enabled" when their related domain + goes from "pending" to "enabled".""" + + domain = factories.MailDomainFactory() + factories.MailboxFactory.create_batch( + 3, domain=domain, status=enums.MailboxStatusChoices.PENDING + ) + + dimail_client = DimailAPIClient() + dimail_client.enable_pending_mailboxes(domain=domain) + + mailboxes = models.Mailbox.objects.filter(domain=domain) + assert len(mailboxes) == 3 + for mailbox in mailboxes: + assert mailbox.status == enums.MailboxStatusChoices.ENABLED diff --git a/src/backend/mailbox_manager/utils/dimail.py b/src/backend/mailbox_manager/utils/dimail.py index c4ec5f4fb..507ab557b 100644 --- a/src/backend/mailbox_manager/utils/dimail.py +++ b/src/backend/mailbox_manager/utils/dimail.py @@ -138,6 +138,9 @@ def create_mailbox(self, mailbox, user_sub=None): ) raise error + import pdb + + pdb.set_trace() if response.status_code == status.HTTP_201_CREATED: logger.info( "Mailbox successfully created on domain %s by user %s", @@ -420,3 +423,30 @@ def fetch_domain_status(self, domain): domain.save() return response return self.raise_exception_for_unexpected_response(response) + + def enable_pending_mailboxes(self, domain): + """Send request 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) + + # fix format to have actual json + mailbox_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 + ) +