Skip to content

Commit

Permalink
✨(api) restrict mailbox sync to enabled domains
Browse files Browse the repository at this point in the history
Pending, failed and deactivated domains should not be sync'ed.
  • Loading branch information
mjeammet committed Jan 31, 2025
1 parent 20497a1 commit f0edee4
Show file tree
Hide file tree
Showing 3 changed files with 36 additions and 0 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ and this project adheres to
### Fixed

- 🚑️(plugins) fix name from SIRET specific case #674
- 🐛(api) restrict mailbox sync to enabled domains


## [1.10.1] - 2025-01-27
Expand Down
13 changes: 13 additions & 0 deletions src/backend/mailbox_manager/admin.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,9 +13,15 @@
@admin.action(description=_("Synchronise from dimail"))
def sync_mailboxes_from_dimail(modeladmin, request, queryset): # pylint: disable=unused-argument
"""Admin action to synchronize existing mailboxes from dimail to our database."""
excluded_domains = []

client = DimailAPIClient()

for domain in queryset:
if domain.status != enums.MailDomainStatusChoices.ENABLED:
excluded_domains.append(domain.name)
continue

try:
imported_mailboxes = client.import_mailboxes(domain)
except exceptions.HTTPError as err:
Expand All @@ -31,6 +37,13 @@ def sync_mailboxes_from_dimail(modeladmin, request, queryset): # pylint: disabl
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"))
Expand Down
22 changes: 22 additions & 0 deletions src/backend/mailbox_manager/tests/test_admin_actions.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,28 @@
)


@pytest.mark.django_db
def test_admin_action__should_not_sync_if_domain_enabled(client):
"""Test admin action to check health of some domains"""
admin = core_factories.UserFactory(is_staff=True, is_superuser=True)
client.force_login(admin)
domain = factories.MailDomainFactory()
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
def test_admin_action__should_switch_to_failed_when_domain_broken(client):
"""Test admin action to check health of some domains"""
Expand Down

0 comments on commit f0edee4

Please sign in to comment.