🐛(dimail) fix unexpected status_code for proper debug

Remove duplicate and catch errors more gracefully. Fixes tests accordingly.
This commit is contained in:
Marie PUPO JEAMMET
2024-10-24 17:40:32 +02:00
committed by Marie
parent 21bf431940
commit 0b0b77cead
3 changed files with 12 additions and 13 deletions

View File

@@ -15,6 +15,7 @@ and this project adheres to
- ⬆️(dependencies) remove unneeded dependencies - ⬆️(dependencies) remove unneeded dependencies
- 🔥(teams) remove pagination of teams listing - 🔥(teams) remove pagination of teams listing
- 🔥(teams) remove search users by trigram - 🔥(teams) remove search users by trigram
- 🗃️(teams) remove `slug` field
### Added ### Added
@@ -30,10 +31,7 @@ and this project adheres to
- 🔧(sentry) restore default integrations - 🔧(sentry) restore default integrations
- 🔇(backend) remove Sentry duplicated warning/errors - 🔇(backend) remove Sentry duplicated warning/errors
- 👷(ci) add sharding e2e tests #467 - 👷(ci) add sharding e2e tests #467
- 🐛(dimail) fix unexpected status_code for proper debug #454
### Removed
- 🗃️(teams) remove `slug` field
## [1.4.1] - 2024-10-23 ## [1.4.1] - 2024-10-23

View File

@@ -13,6 +13,7 @@ from django.utils.translation import gettext_lazy as _
import pytest import pytest
import requests import requests
import responses import responses
from requests.exceptions import HTTPError
from rest_framework import status from rest_framework import status
from rest_framework.test import APIClient from rest_framework.test import APIClient
@@ -501,8 +502,7 @@ def test_api_mailboxes__user_unrelated_to_domain():
assert not models.Mailbox.objects.exists() assert not models.Mailbox.objects.exists()
@mock.patch.object(Logger, "error") def test_api_mailboxes__handling_dimail_unexpected_error(caplog):
def test_api_mailboxes__handling_dimail_unexpected_error(mock_error):
""" """
API should raise a clear error when dimail returns an unexpected response. API should raise a clear error when dimail returns an unexpected response.
""" """
@@ -527,12 +527,12 @@ def test_api_mailboxes__handling_dimail_unexpected_error(mock_error):
rsps.add( rsps.add(
rsps.POST, rsps.POST,
re.compile(rf".*/domains/{access.domain.name}/mailboxes/"), re.compile(rf".*/domains/{access.domain.name}/mailboxes/"),
body='{"details": "Internal server error"}', body='{"detail": "Internal server error"}',
status=status.HTTP_500_INTERNAL_SERVER_ERROR, status=status.HTTP_500_INTERNAL_SERVER_ERROR,
content_type="application/json", content_type="application/json",
) )
with pytest.raises(requests.exceptions.HTTPError): with pytest.raises(HTTPError):
response = client.post( response = client.post(
f"/api/v1.0/mail-domains/{access.domain.slug}/mailboxes/", f"/api/v1.0/mail-domains/{access.domain.slug}/mailboxes/",
mailbox_data, mailbox_data,
@@ -545,10 +545,9 @@ def test_api_mailboxes__handling_dimail_unexpected_error(mock_error):
assert not models.Mailbox.objects.exists() assert not models.Mailbox.objects.exists()
# Check error logger was called # Check error logger was called
assert mock_error.called assert len(caplog.records) == 2 # 1 + new empty info. To be investigated
assert mock_error.call_args_list[1][0] == ( assert caplog.records[0].levelname == "ERROR"
'Unexpected response from dimail: 500 {"details": "Internal server error"}', assert caplog.records[0].message == "[DIMAIL] 500: Internal server error"
)
@mock.patch.object(Logger, "error") @mock.patch.object(Logger, "error")

View File

@@ -1,6 +1,7 @@
"""A minimalist client to synchronize with mailbox provisioning API.""" """A minimalist client to synchronize with mailbox provisioning API."""
import ast import ast
import json
import smtplib import smtplib
from email.errors import HeaderParseError, NonASCIILocalPartDefect from email.errors import HeaderParseError, NonASCIILocalPartDefect
from email.headerregistry import Address from email.headerregistry import Address
@@ -13,6 +14,7 @@ from django.template.loader import render_to_string
from django.utils.translation import gettext_lazy as _ from django.utils.translation import gettext_lazy as _
import requests import requests
from requests.exceptions import HTTPError
from rest_framework import status from rest_framework import status
from urllib3.util import Retry from urllib3.util import Retry
@@ -157,7 +159,7 @@ class DimailAPIClient:
def pass_dimail_unexpected_response(self, response): def pass_dimail_unexpected_response(self, response):
"""Raise error when encountering an unexpected error in dimail.""" """Raise error when encountering an unexpected error in dimail."""
error_content = response.content.decode("utf-8") error_content = json.loads(response.content.decode(response.encoding).replace("'", '"'))
logger.error( logger.error(
"[DIMAIL] unexpected error : %s %s", response.status_code, error_content "[DIMAIL] unexpected error : %s %s", response.status_code, error_content