Skip to content

Commit

Permalink
fixup! ✨(dimail) send pending mailboxes upon domain activation
Browse files Browse the repository at this point in the history
  • Loading branch information
mjeammet committed Jan 15, 2025
1 parent 7adf0cd commit 4e78755
Show file tree
Hide file tree
Showing 4 changed files with 24 additions and 33 deletions.
12 changes: 4 additions & 8 deletions src/backend/mailbox_manager/api/client/serializers.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
9 changes: 5 additions & 4 deletions src/backend/mailbox_manager/tests/test_admin_actions.py
Original file line number Diff line number Diff line change
Expand Up @@ -95,22 +95,22 @@ 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:
rsps.add(
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(
Expand All @@ -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):
Expand Down
6 changes: 3 additions & 3 deletions src/backend/mailbox_manager/tests/test_utils_dimail_client.py
Original file line number Diff line number Diff line change
Expand Up @@ -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",
}
Expand All @@ -238,13 +238,13 @@ 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
== f"Mailbox successfully created on domain {domain.name} by user None"
)
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}."
)
30 changes: 12 additions & 18 deletions src/backend/mailbox_manager/utils/dimail.py
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand All @@ -141,15 +141,15 @@ 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

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."
Expand Down Expand Up @@ -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 (
Expand All @@ -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,
)

0 comments on commit 4e78755

Please sign in to comment.