✨(api) restrict mailbox sync to enabled domains
Pending, failed and deactivated domains should not be sync'ed.
This commit is contained in:
committed by
Marie
parent
2c15609c1e
commit
7f0e231474
@@ -15,6 +15,7 @@ and this project adheres to
|
|||||||
### Fixed
|
### Fixed
|
||||||
|
|
||||||
- 🚑️(plugins) fix name from SIRET specific case #674
|
- 🚑️(plugins) fix name from SIRET specific case #674
|
||||||
|
- 🐛(api) restrict mailbox sync to enabled domains
|
||||||
|
|
||||||
|
|
||||||
## [1.10.1] - 2025-01-27
|
## [1.10.1] - 2025-01-27
|
||||||
|
|||||||
@@ -12,10 +12,17 @@ from mailbox_manager.utils.dimail import DimailAPIClient
|
|||||||
|
|
||||||
@admin.action(description=_("Synchronise from dimail"))
|
@admin.action(description=_("Synchronise from dimail"))
|
||||||
def sync_mailboxes_from_dimail(modeladmin, request, queryset): # pylint: disable=unused-argument
|
def sync_mailboxes_from_dimail(modeladmin, request, queryset): # pylint: disable=unused-argument
|
||||||
"""Admin action to synchronize existing mailboxes from dimail to our database."""
|
"""Admin action to synchronize existing mailboxes from dimail to our database.
|
||||||
|
Only works on enabled domains."""
|
||||||
|
excluded_domains = []
|
||||||
|
|
||||||
client = DimailAPIClient()
|
client = DimailAPIClient()
|
||||||
|
|
||||||
for domain in queryset:
|
for domain in queryset:
|
||||||
|
if domain.status != enums.MailDomainStatusChoices.ENABLED:
|
||||||
|
excluded_domains.append(domain.name)
|
||||||
|
continue
|
||||||
|
|
||||||
try:
|
try:
|
||||||
imported_mailboxes = client.import_mailboxes(domain)
|
imported_mailboxes = client.import_mailboxes(domain)
|
||||||
except exceptions.HTTPError as err:
|
except exceptions.HTTPError as err:
|
||||||
@@ -31,6 +38,13 @@ def sync_mailboxes_from_dimail(modeladmin, request, queryset): # pylint: disabl
|
|||||||
f"Imported mailboxes: {', '.join(imported_mailboxes)}"
|
f"Imported mailboxes: {', '.join(imported_mailboxes)}"
|
||||||
),
|
),
|
||||||
)
|
)
|
||||||
|
if excluded_domains:
|
||||||
|
messages.warning(
|
||||||
|
request,
|
||||||
|
_(
|
||||||
|
f"Sync require enabled domains. Excluded domains: {', '.join(excluded_domains)}"
|
||||||
|
),
|
||||||
|
)
|
||||||
|
|
||||||
|
|
||||||
@admin.action(description=_("Check and update status from dimail"))
|
@admin.action(description=_("Check and update status from dimail"))
|
||||||
|
|||||||
@@ -23,6 +23,38 @@ from .fixtures.dimail import (
|
|||||||
)
|
)
|
||||||
|
|
||||||
|
|
||||||
|
@pytest.mark.parametrize(
|
||||||
|
"domain_status",
|
||||||
|
[
|
||||||
|
enums.MailDomainStatusChoices.PENDING,
|
||||||
|
enums.MailDomainStatusChoices.FAILED,
|
||||||
|
enums.MailDomainStatusChoices.DISABLED,
|
||||||
|
],
|
||||||
|
)
|
||||||
|
@pytest.mark.django_db
|
||||||
|
def test_sync_mailboxes__should_not_sync_if_domain_is_not_enabled(
|
||||||
|
domain_status, client
|
||||||
|
):
|
||||||
|
"""Mailboxes should not be sync'ed on non-enabled domains."""
|
||||||
|
admin = core_factories.UserFactory(is_staff=True, is_superuser=True)
|
||||||
|
client.force_login(admin)
|
||||||
|
domain = factories.MailDomainFactory(status=domain_status)
|
||||||
|
data = {
|
||||||
|
"action": "sync_mailboxes_from_dimail",
|
||||||
|
"_selected_action": [domain.id],
|
||||||
|
}
|
||||||
|
url = reverse("admin:mailbox_manager_maildomain_changelist")
|
||||||
|
|
||||||
|
with responses.RequestsMock():
|
||||||
|
# No call expected
|
||||||
|
response = client.post(url, data, follow=True)
|
||||||
|
assert response.status_code == status.HTTP_200_OK
|
||||||
|
assert (
|
||||||
|
f"Sync require enabled domains. Excluded domains: {domain}"
|
||||||
|
in response.content.decode("utf-8")
|
||||||
|
)
|
||||||
|
|
||||||
|
|
||||||
@pytest.mark.django_db
|
@pytest.mark.django_db
|
||||||
def test_fetch_domain_status__should_switch_to_failed_when_domain_broken(client):
|
def test_fetch_domain_status__should_switch_to_failed_when_domain_broken(client):
|
||||||
"""Test admin action to check health of some domains"""
|
"""Test admin action to check health of some domains"""
|
||||||
|
|||||||
Reference in New Issue
Block a user