✨(domains) allow to run all fetch domain data from dimail
Fetch domain status and expected config from dimail.
This commit is contained in:
@@ -35,6 +35,9 @@ class MailDomainViewSet(
|
||||
- name: str
|
||||
- support_email: str
|
||||
Return newly created domain
|
||||
|
||||
POST /api/<version>/mail-domains/<domain-slug>/fetch/
|
||||
Fetch domain status and expected config from dimail.
|
||||
"""
|
||||
|
||||
permission_classes = [permissions.AccessPermission]
|
||||
@@ -61,6 +64,17 @@ class MailDomainViewSet(
|
||||
}
|
||||
)
|
||||
|
||||
@action(detail=True, methods=["post"], url_path="fetch")
|
||||
def fetch_from_dimail(self, request, *args, **kwargs):
|
||||
"""Fetch domain status and expected config from dimail."""
|
||||
domain = self.get_object()
|
||||
client = DimailAPIClient()
|
||||
client.fetch_domain_status(domain)
|
||||
client.fetch_domain_expected_config(domain)
|
||||
return Response(
|
||||
serializers.MailDomainSerializer(domain, context={"request": request}).data
|
||||
)
|
||||
|
||||
|
||||
# pylint: disable=too-many-ancestors
|
||||
class MailDomainAccessViewSet(
|
||||
|
||||
@@ -0,0 +1,143 @@
|
||||
"""
|
||||
Tests for MailDomains API endpoint in People's mailbox manager app. Focus on "fetch" action.
|
||||
"""
|
||||
|
||||
import json
|
||||
import re
|
||||
|
||||
import pytest
|
||||
import responses
|
||||
from rest_framework import status
|
||||
from rest_framework.test import APIClient
|
||||
|
||||
from core import factories as core_factories
|
||||
|
||||
from mailbox_manager import enums, factories
|
||||
from mailbox_manager.tests.fixtures import dimail as dimail_fixtures
|
||||
|
||||
pytestmark = pytest.mark.django_db
|
||||
|
||||
|
||||
@responses.activate
|
||||
def test_api_mail_domains__fetch_from_dimail__anonymous():
|
||||
"""
|
||||
Anonymous users should not be allowed to fetch a domain from dimail.
|
||||
"""
|
||||
client = APIClient()
|
||||
|
||||
domain = factories.MailDomainFactory()
|
||||
|
||||
response = client.post(
|
||||
f"/api/v1.0/mail-domains/{domain.slug}/fetch/",
|
||||
)
|
||||
assert response.status_code == status.HTTP_401_UNAUTHORIZED
|
||||
|
||||
|
||||
@responses.activate
|
||||
def test_api_mail_domains__fetch_from_dimail__unrelated():
|
||||
"""
|
||||
Authenticated users shouldn't be allowed to fetch
|
||||
a domain from dimail if they are not an owner or admin.
|
||||
"""
|
||||
user = core_factories.UserFactory()
|
||||
|
||||
client = APIClient()
|
||||
client.force_login(user)
|
||||
|
||||
domain = factories.MailDomainFactory(
|
||||
status=enums.MailDomainStatusChoices.PENDING,
|
||||
)
|
||||
|
||||
response = client.post(
|
||||
f"/api/v1.0/mail-domains/{domain.slug}/fetch/",
|
||||
)
|
||||
|
||||
assert response.status_code == status.HTTP_404_NOT_FOUND
|
||||
|
||||
|
||||
def test_api_mail_domains__fetch_from_dimail__viewer():
|
||||
"""
|
||||
Authenticated users shouldn't be allowed to fetch a domain from dimail
|
||||
if they are a viewer.
|
||||
"""
|
||||
user = core_factories.UserFactory()
|
||||
|
||||
client = APIClient()
|
||||
client.force_login(user)
|
||||
access = factories.MailDomainAccessFactory(
|
||||
user=user,
|
||||
role=enums.MailDomainRoleChoices.VIEWER,
|
||||
)
|
||||
response = client.post(
|
||||
f"/api/v1.0/mail-domains/{access.domain.slug}/fetch/",
|
||||
)
|
||||
assert response.status_code == status.HTTP_403_FORBIDDEN
|
||||
|
||||
|
||||
@pytest.mark.parametrize(
|
||||
"role",
|
||||
[
|
||||
enums.MailDomainRoleChoices.ADMIN,
|
||||
enums.MailDomainRoleChoices.OWNER,
|
||||
],
|
||||
)
|
||||
@responses.activate
|
||||
def test_api_mail_domains__fetch_from_dimail(role):
|
||||
"""
|
||||
Authenticated users should be allowed to fetch a domain
|
||||
from dimail if they are an owner or admin.
|
||||
"""
|
||||
|
||||
user = core_factories.UserFactory()
|
||||
|
||||
client = APIClient()
|
||||
client.force_login(user)
|
||||
|
||||
domain = factories.MailDomainFactory(
|
||||
status=enums.MailDomainStatusChoices.PENDING,
|
||||
)
|
||||
factories.MailDomainAccessFactory(
|
||||
domain=domain,
|
||||
user=user,
|
||||
role=role,
|
||||
)
|
||||
|
||||
assert domain.expected_config is None
|
||||
assert domain.last_check_details is None
|
||||
|
||||
responses.add(
|
||||
responses.GET,
|
||||
re.compile(rf".*/domains/{domain.name}/check/"),
|
||||
json=dimail_fixtures.CHECK_DOMAIN_OK,
|
||||
status=200,
|
||||
)
|
||||
responses.add(
|
||||
responses.GET,
|
||||
re.compile(rf".*/domains/{domain.name}/spec/"),
|
||||
json=dimail_fixtures.DOMAIN_SPEC,
|
||||
status=200,
|
||||
)
|
||||
|
||||
response = client.post(
|
||||
f"/api/v1.0/mail-domains/{domain.slug}/fetch/",
|
||||
)
|
||||
|
||||
domain.refresh_from_db()
|
||||
assert response.status_code == status.HTTP_200_OK
|
||||
assert response.json() == {
|
||||
"id": str(domain.id),
|
||||
"name": domain.name,
|
||||
"slug": domain.slug,
|
||||
"status": str(enums.MailDomainStatusChoices.ENABLED),
|
||||
"created_at": domain.created_at.isoformat().replace("+00:00", "Z"),
|
||||
"updated_at": domain.updated_at.isoformat().replace("+00:00", "Z"),
|
||||
"abilities": domain.get_abilities(user),
|
||||
"count_mailboxes": 0,
|
||||
"support_email": domain.support_email,
|
||||
"last_check_details": dimail_fixtures.CHECK_DOMAIN_OK,
|
||||
"action_required_details": {},
|
||||
"expected_config": dimail_fixtures.DOMAIN_SPEC,
|
||||
}
|
||||
assert domain.expected_config == dimail_fixtures.DOMAIN_SPEC
|
||||
assert domain.last_check_details == dimail_fixtures.CHECK_DOMAIN_OK
|
||||
assert domain.status == enums.MailDomainStatusChoices.ENABLED
|
||||
@@ -2,9 +2,6 @@
|
||||
Tests for MailDomains API endpoint in People's mailbox manager app. Focus on "retrieve" action.
|
||||
"""
|
||||
|
||||
import json
|
||||
import re
|
||||
|
||||
import pytest
|
||||
import responses
|
||||
from rest_framework import status
|
||||
|
||||
@@ -431,6 +431,7 @@ class DimailAPIClient:
|
||||
self.API_URL,
|
||||
exc_info=error,
|
||||
)
|
||||
raise error
|
||||
if response.status_code == status.HTTP_200_OK:
|
||||
return response.json()
|
||||
return self.raise_exception_for_unexpected_response(response)
|
||||
|
||||
Reference in New Issue
Block a user