♻️(tests) refacto dimail tests with fixtures
test_api_mailboxes_create exceeded 1000 lines. By using fixtures, we can at least factorize dimail response when token is ok or mailbox_data sent to our API when creating a mailbox.
This commit is contained in:
committed by
Marie
parent
b24cb23a83
commit
4262f469d6
@@ -3,6 +3,7 @@
|
||||
from logging import getLogger
|
||||
|
||||
from django.contrib.auth.hashers import make_password
|
||||
from django.core import exceptions as django_exceptions
|
||||
|
||||
from requests.exceptions import HTTPError
|
||||
from rest_framework import exceptions, serializers
|
||||
@@ -44,10 +45,17 @@ class MailboxSerializer(serializers.ModelSerializer):
|
||||
"password": make_password(None), # generate an unusable password
|
||||
}
|
||||
)
|
||||
if mailbox.domain.status == enums.MailDomainStatusChoices.ENABLED:
|
||||
client = DimailAPIClient()
|
||||
|
||||
if validated_data["domain"].status == enums.MailDomainStatusChoices.ENABLED:
|
||||
# send new mailbox request to dimail
|
||||
response = client.create_mailbox(mailbox, self.context["request"].user.sub)
|
||||
client = DimailAPIClient()
|
||||
try:
|
||||
response = client.create_mailbox(
|
||||
mailbox, self.context["request"].user.sub
|
||||
)
|
||||
except django_exceptions.ValidationError as exc:
|
||||
mailbox.delete()
|
||||
raise exc
|
||||
|
||||
mailbox.status = enums.MailDomainStatusChoices.ENABLED
|
||||
mailbox_data = response.json()
|
||||
@@ -68,7 +76,6 @@ class MailboxSerializer(serializers.ModelSerializer):
|
||||
mailbox,
|
||||
)
|
||||
|
||||
# actually save mailbox on our database
|
||||
return mailbox
|
||||
|
||||
|
||||
|
||||
@@ -311,6 +311,9 @@ class Mailbox(AbstractBaseUser, BaseModel):
|
||||
fields=["first_name", "last_name", "domain"],
|
||||
name="unique_ox_display_name",
|
||||
),
|
||||
# Display name in OpenXChange must be unique
|
||||
# To avoid sending failing requests to dimail,
|
||||
# we impose uniqueness here too
|
||||
]
|
||||
ordering = ["-created_at"]
|
||||
|
||||
|
||||
File diff suppressed because it is too large
Load Diff
37
src/backend/mailbox_manager/tests/conftest.py
Normal file
37
src/backend/mailbox_manager/tests/conftest.py
Normal file
@@ -0,0 +1,37 @@
|
||||
"""
|
||||
Fixtures for mailbox manager tests
|
||||
"""
|
||||
|
||||
import re
|
||||
|
||||
import pytest
|
||||
import responses
|
||||
from rest_framework import status
|
||||
|
||||
from mailbox_manager import factories
|
||||
from mailbox_manager.tests.fixtures.dimail import TOKEN_OK
|
||||
|
||||
|
||||
## DIMAIL RESPONSES
|
||||
@pytest.fixture(name="dimail_token_ok")
|
||||
def fixture_dimail_token_ok():
|
||||
"""Mock dimail response when /token/ endpoit is given valid credentials."""
|
||||
responses.add(
|
||||
responses.GET,
|
||||
re.compile(r".*/token/"),
|
||||
body=TOKEN_OK,
|
||||
status=status.HTTP_200_OK,
|
||||
content_type="application/json",
|
||||
)
|
||||
|
||||
|
||||
@pytest.fixture(name="mailbox_data")
|
||||
def fixture_mailbox_data():
|
||||
"""Provides valid mailbox data for tests."""
|
||||
example = factories.MailboxFactory.build()
|
||||
return {
|
||||
"first_name": example.first_name,
|
||||
"last_name": example.last_name,
|
||||
"local_part": example.local_part,
|
||||
"secondary_email": example.secondary_email,
|
||||
}
|
||||
@@ -3,9 +3,8 @@
|
||||
|
||||
import json
|
||||
|
||||
|
||||
## USERS
|
||||
|
||||
|
||||
def response_user_created(user_sub):
|
||||
"""mimic dimail response upon successful user creation."""
|
||||
return json.dumps(
|
||||
@@ -19,7 +18,6 @@ def response_user_created(user_sub):
|
||||
|
||||
|
||||
## DOMAINS
|
||||
|
||||
CHECK_DOMAIN_BROKEN = {
|
||||
"name": "example.fr",
|
||||
"state": "broken",
|
||||
@@ -290,12 +288,10 @@ DOMAIN_SPEC = [
|
||||
|
||||
|
||||
## TOKEN
|
||||
|
||||
TOKEN_OK = json.dumps({"access_token": "token", "token_type": "bearer"})
|
||||
|
||||
|
||||
## ALLOWS
|
||||
|
||||
|
||||
def response_allows_created(user_name, domain_name):
|
||||
"""mimic dimail response upon successful allows creation.
|
||||
Dimail expects a name but our names are ProConnect's uuids."""
|
||||
@@ -303,8 +299,6 @@ def response_allows_created(user_name, domain_name):
|
||||
|
||||
|
||||
## MAILBOXES
|
||||
|
||||
|
||||
def response_mailbox_created(email_address):
|
||||
"""mimic dimail response upon successful mailbox creation."""
|
||||
return json.dumps({"email": email_address, "password": "password"})
|
||||
|
||||
Reference in New Issue
Block a user