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

View File

@@ -13,6 +13,7 @@ from django.utils.translation import gettext_lazy as _
import pytest
import requests
import responses
from requests.exceptions import HTTPError
from rest_framework import status
from rest_framework.test import APIClient
@@ -501,8 +502,7 @@ def test_api_mailboxes__user_unrelated_to_domain():
assert not models.Mailbox.objects.exists()
@mock.patch.object(Logger, "error")
def test_api_mailboxes__handling_dimail_unexpected_error(mock_error):
def test_api_mailboxes__handling_dimail_unexpected_error(caplog):
"""
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.POST,
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,
content_type="application/json",
)
with pytest.raises(requests.exceptions.HTTPError):
with pytest.raises(HTTPError):
response = client.post(
f"/api/v1.0/mail-domains/{access.domain.slug}/mailboxes/",
mailbox_data,
@@ -545,10 +545,9 @@ def test_api_mailboxes__handling_dimail_unexpected_error(mock_error):
assert not models.Mailbox.objects.exists()
# Check error logger was called
assert mock_error.called
assert mock_error.call_args_list[1][0] == (
'Unexpected response from dimail: 500 {"details": "Internal server error"}',
)
assert len(caplog.records) == 2 # 1 + new empty info. To be investigated
assert caplog.records[0].levelname == "ERROR"
assert caplog.records[0].message == "[DIMAIL] 500: Internal server error"
@mock.patch.object(Logger, "error")

View File

@@ -1,6 +1,7 @@
"""A minimalist client to synchronize with mailbox provisioning API."""
import ast
import json
import smtplib
from email.errors import HeaderParseError, NonASCIILocalPartDefect
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 _
import requests
from requests.exceptions import HTTPError
from rest_framework import status
from urllib3.util import Retry
@@ -157,7 +159,7 @@ class DimailAPIClient:
def pass_dimail_unexpected_response(self, response):
"""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(
"[DIMAIL] unexpected error : %s %s", response.status_code, error_content