Skip to content

Commit 7f3375a

Browse files
committed
wip
1 parent 06f78ba commit 7f3375a

File tree

2 files changed

+63
-34
lines changed

2 files changed

+63
-34
lines changed

src/backend/mailbox_manager/tests/test_utils_dimail_client.py

Lines changed: 39 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,8 @@
33
"""
44

55
import re
6+
from logging import Logger
7+
from unittest import mock
68

79
import pytest
810
import responses
@@ -61,7 +63,8 @@ def test_dimail_synchronization__already_sync():
6163
assert set(models.Mailbox.objects.filter(domain=domain)) == set(pre_sync_mailboxes)
6264

6365

64-
def test_dimail_synchronization__synchronize_mailboxes():
66+
@mock.patch.object(Logger, "warning")
67+
def test_dimail_synchronization__synchronize_mailboxes(mock_warning):
6568
"""A mailbox existing solely on dimail should be synchronized
6669
upon calling sync function on its domain"""
6770
domain = factories.MailDomainEnabledFactory()
@@ -77,33 +80,50 @@ def test_dimail_synchronization__synchronize_mailboxes():
7780
status=status.HTTP_200_OK,
7881
content_type="application/json",
7982
)
83+
84+
mailbox = {
85+
"type": "mailbox",
86+
"status": "broken",
87+
"email": f"oxadmin@{domain.name}",
88+
"givenName": "Admin",
89+
"surName": "Context",
90+
"displayName": "Context Admin",
91+
}
92+
mailbox_with_wrong_domain = {
93+
"type": "mailbox",
94+
"status": "broken",
95+
"email": f"[email protected]",
96+
"givenName": "John",
97+
"surName": "Doe",
98+
"displayName": "John Doe",
99+
}
100+
mailbox_with_wrong_local_part = (
101+
{
102+
"type": "mailbox",
103+
"status": "broken",
104+
"email": f"nawaké@{domain.name}",
105+
"givenName": "Joe",
106+
"surName": "Doe",
107+
"displayName": "Joe Doe",
108+
},
109+
)
110+
80111
rsps.add(
81112
rsps.GET,
82113
re.compile(rf".*/domains/{domain.name}/mailboxes/"),
83114
body=str(
84-
[
85-
{
86-
"type": "mailbox",
87-
"status": "broken",
88-
"email": f"oxadmin@{domain.name}",
89-
"givenName": "Admin",
90-
"surName": "Context",
91-
"displayName": "Context Admin",
92-
},
93-
{
94-
"type": "mailbox",
95-
"status": "broken",
96-
"email": f"[email protected]",
97-
"givenName": "John",
98-
"surName": "Doe",
99-
"displayName": "John Doe",
100-
},
101-
]
115+
[mailbox, mailbox_with_wrong_domain, mailbox_with_wrong_local_part]
102116
),
103117
status=status.HTTP_200_OK,
104118
content_type="application/json",
105119
)
120+
106121
imported_mailboxes = dimail_client.synchronize_mailboxes_from_dimail(domain)
122+
assert mock_warning.call_count == 1
123+
assert mock_warning.call_args_list == (
124+
"Import of email %s failed",
125+
f"nawaké@{domain.name}",
126+
)
107127

108128
mailbox = models.Mailbox.objects.get()
109129
assert mailbox.local_part == "oxadmin"

src/backend/mailbox_manager/utils/dimail.py

Lines changed: 24 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
import ast
44
import smtplib
5+
from email.headerregistry import Address
56
from logging import getLogger
67

78
from django.conf import settings
@@ -200,20 +201,28 @@ def synchronize_mailboxes_from_dimail(self, domain):
200201
if not dimail_mailbox["email"] in [
201202
str(people_mailbox) for people_mailbox in people_mailboxes
202203
]:
203-
# sometimes dimail api returns email from another domain, so we decide to exclude
204-
# this kind of email
205-
dimail_local_part, dimail_domain = dimail_mailbox["email"].split("@")
206-
if dimail_domain == domain.name:
207-
# creates a mailbox on our end
208-
mailbox = models.Mailbox.objects.create(
209-
first_name=dimail_mailbox["givenName"],
210-
last_name=dimail_mailbox["surName"],
211-
local_part=dimail_local_part,
212-
domain=domain,
213-
secondary_email=dimail_mailbox[
214-
"email"
215-
], # secondary email is mandatory. Unfortunately, dimail doesn't store any.
216-
# We temporarily give current email as secondary email.
204+
try:
205+
# sometimes dimail api returns email from another domain, so we decide to exclude
206+
# this kind of email
207+
dimail_local_part, dimail_domain = dimail_mailbox["email"].split(
208+
"@"
217209
)
218-
imported_mailboxes.append(str(mailbox))
210+
address = Address(
211+
dimail_mailbox["displayName"], dimail_local_part, dimail_domain
212+
)
213+
if address.domain == domain.name:
214+
# creates a mailbox on our end
215+
mailbox = models.Mailbox.objects.create(
216+
first_name=dimail_mailbox["givenName"],
217+
last_name=dimail_mailbox["surName"],
218+
local_part=address.username,
219+
domain=domain,
220+
secondary_email=dimail_mailbox[
221+
"email"
222+
], # secondary email is mandatory. Unfortunately, dimail doesn't store any.
223+
# We temporarily give current email as secondary email.
224+
)
225+
imported_mailboxes.append(str(mailbox))
226+
except Exception as err:
227+
logger.warning("Import of email %s failed", dimail_mailbox["email"])
219228
return imported_mailboxes

0 commit comments

Comments
 (0)