Skip to content

Commit

Permalink
✨(dimail) send pending mailboxes upon domain activation
Browse files Browse the repository at this point in the history
send creation requests to dimail for all pending mailboxes
when domain goes from "pending" to "enabled".
  • Loading branch information
mjeammet committed Jan 10, 2025
1 parent 9bcb8a4 commit 3ea0d6c
Show file tree
Hide file tree
Showing 4 changed files with 58 additions and 1 deletion.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
2 changes: 1 addition & 1 deletion src/backend/mailbox_manager/api/client/serializers.py
Original file line number Diff line number Diff line change
Expand Up @@ -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("'", '"')
)
Expand Down
26 changes: 26 additions & 0 deletions src/backend/mailbox_manager/tests/test_utils_dimail_client.py
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand All @@ -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.
Expand Down Expand Up @@ -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
30 changes: 30 additions & 0 deletions src/backend/mailbox_manager/utils/dimail.py
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down Expand Up @@ -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
)

0 comments on commit 3ea0d6c

Please sign in to comment.