-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
✨(backend) add admin action to check domain health
Allow to select some domains to check status thanks to a dimail call.
- Loading branch information
Showing
4 changed files
with
129 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,78 @@ | ||
""" | ||
Unit tests for admin actions | ||
""" | ||
|
||
import json | ||
import re | ||
|
||
from django.urls import reverse | ||
|
||
import pytest | ||
import responses | ||
|
||
from core import factories as core_factories | ||
|
||
from mailbox_manager import enums, factories | ||
|
||
from .fixtures.dimail import CHECK_DOMAIN_BROKEN | ||
|
||
|
||
@pytest.mark.django_db | ||
def test_admin_action__check_domain_health(client): | ||
"""Test admin action to check health of some domains""" | ||
admin = core_factories.UserFactory(is_staff=True, is_superuser=True) | ||
client.force_login(admin) | ||
domain1 = factories.MailDomainEnabledFactory() | ||
domain2 = factories.MailDomainEnabledFactory() | ||
data = { | ||
"action": "check_domain_health", | ||
"_selected_action": [ | ||
domain1.id, | ||
domain2.id, | ||
], | ||
} | ||
url = reverse("admin:mailbox_manager_maildomain_changelist") | ||
|
||
with responses.RequestsMock() as rsps: | ||
body_content_domain1 = CHECK_DOMAIN_BROKEN.copy() | ||
body_content_domain1["name"] = domain1.name | ||
body_content_domain2 = CHECK_DOMAIN_BROKEN.copy() | ||
body_content_domain2["name"] = domain2.name | ||
rsps.add( | ||
rsps.GET, | ||
re.compile(rf".*/domains/{domain1.name}/check/"), | ||
body=json.dumps(body_content_domain1), | ||
status=200, | ||
content_type="application/json", | ||
) | ||
rsps.add( | ||
rsps.GET, | ||
re.compile(rf".*/domains/{domain2.name}/check/"), | ||
body=json.dumps(body_content_domain2), | ||
status=200, | ||
content_type="application/json", | ||
) | ||
response = client.post(url, data, follow=True) | ||
assert response.status_code == 200 | ||
domain1.refresh_from_db() | ||
domain2.refresh_from_db() | ||
assert domain1.status == enums.MailDomainStatusChoices.FAILED | ||
assert domain2.status == enums.MailDomainStatusChoices.FAILED | ||
assert "Check domain done with success" in response.content.decode("utf-8") | ||
|
||
# check with a valid domain info from dimail | ||
body_content_domain1["state"] = "ok" | ||
rsps.add( | ||
rsps.GET, | ||
re.compile(rf".*/domains/{domain1.name}/check/"), | ||
body=json.dumps(body_content_domain1), | ||
status=200, | ||
content_type="application/json", | ||
) | ||
response = client.post(url, data, follow=True) | ||
assert response.status_code == 200 | ||
domain1.refresh_from_db() | ||
domain2.refresh_from_db() | ||
assert domain1.status == enums.MailDomainStatusChoices.ENABLED | ||
assert domain2.status == enums.MailDomainStatusChoices.FAILED | ||
assert "Check domain done with success" in response.content.decode("utf-8") |