♻️(tests) reorganization of test files
Create two new folders in mailbox_manager tests
This commit is contained in:
145
src/backend/mailbox_manager/tests/models/test_mailboxes.py
Normal file
145
src/backend/mailbox_manager/tests/models/test_mailboxes.py
Normal file
@@ -0,0 +1,145 @@
|
||||
"""
|
||||
Unit tests for the mailbox model
|
||||
"""
|
||||
|
||||
from django.core import exceptions
|
||||
from django.test.utils import override_settings
|
||||
|
||||
import pytest
|
||||
|
||||
from mailbox_manager import enums, factories, models
|
||||
|
||||
pytestmark = pytest.mark.django_db
|
||||
|
||||
# LOCAL PART FIELD
|
||||
|
||||
|
||||
def test_models_mailboxes__local_part_cannot_be_empty():
|
||||
"""The "local_part" field should not be empty."""
|
||||
with pytest.raises(exceptions.ValidationError, match="This field cannot be blank"):
|
||||
factories.MailboxFactory(local_part="")
|
||||
|
||||
|
||||
def test_models_mailboxes__local_part_cannot_be_null():
|
||||
"""The "local_part" field should not be null."""
|
||||
with pytest.raises(exceptions.ValidationError, match="This field cannot be null"):
|
||||
factories.MailboxFactory(local_part=None)
|
||||
|
||||
|
||||
def test_models_mailboxes__local_part_matches_expected_format():
|
||||
"""
|
||||
The local part should contain alpha-numeric caracters
|
||||
and a limited set of special caracters ("+", "-", ".", "_").
|
||||
"""
|
||||
# "-", ".", "_" are allowed
|
||||
factories.MailboxFactory(local_part="Marie-Jose.Perec_2024")
|
||||
|
||||
# other special characters should raise a validation error
|
||||
# "+" included, as this test is about mail creation
|
||||
for character in ["+", "@", "!", "$", "%", " "]:
|
||||
with pytest.raises(exceptions.ValidationError, match="Enter a valid value"):
|
||||
factories.MailboxFactory(local_part=f"marie{character}jo")
|
||||
|
||||
|
||||
def test_models_mailboxes__local_part_unique_per_domain():
|
||||
"""Local parts should be unique per domain."""
|
||||
|
||||
existing_mailbox = factories.MailboxFactory()
|
||||
|
||||
# same local part on another domain should not be a problem
|
||||
factories.MailboxFactory(local_part=existing_mailbox.local_part)
|
||||
|
||||
# same local part on the same domain should not be possible
|
||||
with pytest.raises(
|
||||
exceptions.ValidationError,
|
||||
match="Mailbox with this Local_part and Domain already exists.",
|
||||
):
|
||||
factories.MailboxFactory(
|
||||
local_part=existing_mailbox.local_part, domain=existing_mailbox.domain
|
||||
)
|
||||
|
||||
|
||||
# DOMAIN FIELD
|
||||
|
||||
|
||||
def test_models_mailboxes__domain_must_be_a_maildomain_instance():
|
||||
"""The "domain" field should be an instance of MailDomain."""
|
||||
expected_error = '"Mailbox.domain" must be a "MailDomain" instance.'
|
||||
with pytest.raises(ValueError, match=expected_error):
|
||||
factories.MailboxFactory(domain="")
|
||||
|
||||
with pytest.raises(ValueError, match=expected_error):
|
||||
factories.MailboxFactory(domain="domain-as-string.com")
|
||||
|
||||
|
||||
def test_models_mailboxes__domain_cannot_be_null():
|
||||
"""The "domain" field should not be null."""
|
||||
with pytest.raises(exceptions.ValidationError, match="This field cannot be null"):
|
||||
factories.MailboxFactory(domain=None)
|
||||
|
||||
|
||||
# SECONDARY_EMAIL FIELD
|
||||
|
||||
|
||||
def test_models_mailboxes__secondary_email_cannot_be_empty():
|
||||
"""The "secondary_email" field should not be empty."""
|
||||
with pytest.raises(exceptions.ValidationError, match="This field cannot be blank"):
|
||||
factories.MailboxFactory(secondary_email="")
|
||||
|
||||
|
||||
def test_models_mailboxes__secondary_email_cannot_be_null():
|
||||
"""The "secondary_email" field should not be null."""
|
||||
with pytest.raises(exceptions.ValidationError, match="This field cannot be null"):
|
||||
factories.MailboxFactory(secondary_email=None)
|
||||
|
||||
|
||||
@pytest.mark.parametrize(
|
||||
"domain_status",
|
||||
[
|
||||
enums.MailDomainStatusChoices.PENDING,
|
||||
enums.MailDomainStatusChoices.FAILED,
|
||||
],
|
||||
)
|
||||
def test_models_mailboxes__can_create_pending_mailboxes_on_non_enabled_domain(
|
||||
domain_status,
|
||||
):
|
||||
"""Mailbox creation is allowed for a domain pending and failed.
|
||||
A pending mailbox is created."""
|
||||
mailbox = factories.MailboxFactory(
|
||||
domain=factories.MailDomainFactory(status=domain_status)
|
||||
)
|
||||
assert mailbox.status == enums.MailboxStatusChoices.PENDING
|
||||
|
||||
|
||||
def test_models_mailboxes__cannot_create_mailboxes_on_disabled_domain():
|
||||
"""Mailbox creation is not allowed for a domain disabled.
|
||||
A disabled status for the mail domain raises an error."""
|
||||
with pytest.raises(
|
||||
exceptions.ValidationError,
|
||||
match="You can't create or update a mailbox for a disabled domain.",
|
||||
):
|
||||
factories.MailboxFactory(
|
||||
domain=factories.MailDomainFactory(
|
||||
status=enums.MailDomainStatusChoices.DISABLED
|
||||
)
|
||||
)
|
||||
assert not models.Mailbox.objects.exist()
|
||||
|
||||
|
||||
### REACTING TO DIMAIL-API
|
||||
### We mock dimail's responses to avoid testing dimail's container too
|
||||
|
||||
|
||||
@override_settings(MAIL_PROVISIONING_API_CREDENTIALS=None)
|
||||
def test_models_mailboxes__dimail_no_credentials():
|
||||
"""
|
||||
If MAIL_PROVISIONING_API_CREDENTIALS setting is not configured,
|
||||
trying to create a mailbox should raise an error.
|
||||
"""
|
||||
domain = factories.MailDomainEnabledFactory()
|
||||
|
||||
with pytest.raises(
|
||||
exceptions.ValidationError,
|
||||
match="Please configure MAIL_PROVISIONING_API_CREDENTIALS before creating any mailbox.",
|
||||
):
|
||||
factories.MailboxFactory(domain=domain)
|
||||
125
src/backend/mailbox_manager/tests/models/test_maildomain.py
Normal file
125
src/backend/mailbox_manager/tests/models/test_maildomain.py
Normal file
@@ -0,0 +1,125 @@
|
||||
"""
|
||||
Unit tests for the MailDomain model
|
||||
"""
|
||||
|
||||
from django.contrib.auth.models import AnonymousUser
|
||||
from django.core.exceptions import ValidationError
|
||||
from django.utils.text import slugify
|
||||
|
||||
import pytest
|
||||
|
||||
from core import factories as core_factories
|
||||
|
||||
from mailbox_manager import enums, factories
|
||||
|
||||
pytestmark = pytest.mark.django_db
|
||||
|
||||
# NAME FIELD
|
||||
|
||||
|
||||
def test_models_mail_domain__domain_name_should_not_be_empty():
|
||||
"""The domain name field should not be empty."""
|
||||
with pytest.raises(ValidationError, match="This field cannot be blank"):
|
||||
factories.MailDomainFactory(name="")
|
||||
|
||||
|
||||
def test_models_mail_domain__domain_name_should_not_be_null():
|
||||
"""The domain name field should not be null."""
|
||||
with pytest.raises(ValidationError, match="This field cannot be null."):
|
||||
factories.MailDomainFactory(name=None)
|
||||
|
||||
|
||||
def test_models_mail_domain__slug_inferred_from_name():
|
||||
"""Passed slug is ignored. Slug is automatically generated from name."""
|
||||
|
||||
name = "N3w_D0main-Name$.com"
|
||||
domain = factories.MailDomainFactory(name=name, slug="something else")
|
||||
assert domain.slug == slugify(name)
|
||||
|
||||
|
||||
# "STATUS" FIELD
|
||||
|
||||
|
||||
def test_models_mail_domain__status_should_not_be_empty():
|
||||
"""Status field should not be empty."""
|
||||
with pytest.raises(ValidationError, match="This field cannot be blank"):
|
||||
factories.MailDomainFactory(status="")
|
||||
|
||||
|
||||
def test_models_mail_domain__status_should_not_be_null():
|
||||
"""Status field should not be null."""
|
||||
with pytest.raises(ValidationError, match="This field cannot be null."):
|
||||
factories.MailDomainFactory(status=None)
|
||||
|
||||
|
||||
# get_abilities
|
||||
|
||||
|
||||
def test_models_maildomains_get_abilities_anonymous():
|
||||
"""Check abilities returned for an anonymous user."""
|
||||
maildomain = factories.MailDomainFactory()
|
||||
abilities = maildomain.get_abilities(AnonymousUser())
|
||||
assert abilities == {
|
||||
"delete": False,
|
||||
"get": False,
|
||||
"patch": False,
|
||||
"put": False,
|
||||
"post": False,
|
||||
"manage_accesses": False,
|
||||
}
|
||||
|
||||
|
||||
def test_models_maildomains_get_abilities_authenticated():
|
||||
"""Check abilities returned for an authenticated user."""
|
||||
maildomain = factories.MailDomainFactory()
|
||||
abilities = maildomain.get_abilities(core_factories.UserFactory())
|
||||
assert abilities == {
|
||||
"delete": False,
|
||||
"get": False,
|
||||
"patch": False,
|
||||
"put": False,
|
||||
"post": False,
|
||||
"manage_accesses": False,
|
||||
}
|
||||
|
||||
|
||||
def test_models_maildomains_get_abilities_owner():
|
||||
"""Check abilities returned for the owner of a maildomain."""
|
||||
access = factories.MailDomainAccessFactory(role=enums.MailDomainRoleChoices.OWNER)
|
||||
abilities = access.domain.get_abilities(access.user)
|
||||
assert abilities == {
|
||||
"delete": True,
|
||||
"get": True,
|
||||
"patch": True,
|
||||
"put": True,
|
||||
"post": True,
|
||||
"manage_accesses": True,
|
||||
}
|
||||
|
||||
|
||||
def test_models_maildomains_get_abilities_administrator():
|
||||
"""Check abilities returned for the administrator of a maildomain."""
|
||||
access = factories.MailDomainAccessFactory(role=enums.MailDomainRoleChoices.ADMIN)
|
||||
abilities = access.domain.get_abilities(access.user)
|
||||
assert abilities == {
|
||||
"delete": False,
|
||||
"get": True,
|
||||
"patch": True,
|
||||
"put": True,
|
||||
"post": True,
|
||||
"manage_accesses": True,
|
||||
}
|
||||
|
||||
|
||||
def test_models_maildomains_get_abilities_viewer():
|
||||
"""Check abilities returned for the member of a mail domain. It's a viewer role."""
|
||||
access = factories.MailDomainAccessFactory(role=enums.MailDomainRoleChoices.VIEWER)
|
||||
abilities = access.domain.get_abilities(access.user)
|
||||
assert abilities == {
|
||||
"delete": False,
|
||||
"get": True,
|
||||
"patch": False,
|
||||
"put": False,
|
||||
"post": False,
|
||||
"manage_accesses": False,
|
||||
}
|
||||
@@ -0,0 +1,60 @@
|
||||
"""
|
||||
Unit tests for the MailDomainAccess model
|
||||
"""
|
||||
|
||||
from django.core.exceptions import ValidationError
|
||||
|
||||
import pytest
|
||||
|
||||
from mailbox_manager import factories
|
||||
|
||||
pytestmark = pytest.mark.django_db
|
||||
|
||||
# USER FIELD
|
||||
|
||||
|
||||
def test_models_maildomainaccess__user_be_a_user_instance():
|
||||
"""The "user" field should be a user instance."""
|
||||
expected_error = '"MailDomainAccess.user" must be a "User" instance.'
|
||||
with pytest.raises(ValueError, match=expected_error):
|
||||
factories.MailDomainAccessFactory(user="")
|
||||
|
||||
|
||||
def test_models_maildomainaccess__user_should_not_be_null():
|
||||
"""The user field should not be null."""
|
||||
with pytest.raises(ValidationError, match="This field cannot be null."):
|
||||
factories.MailDomainAccessFactory(user=None)
|
||||
|
||||
|
||||
# DOMAIN FIELD
|
||||
|
||||
|
||||
def test_models_maildomainaccesses__domain_must_be_a_maildomain_instance():
|
||||
"""The "domain" field should be an instance of MailDomain."""
|
||||
expected_error = '"MailDomainAccess.domain" must be a "MailDomain" instance.'
|
||||
with pytest.raises(ValueError, match=expected_error):
|
||||
factories.MailDomainAccessFactory(domain="")
|
||||
|
||||
with pytest.raises(ValueError, match=expected_error):
|
||||
factories.MailDomainAccessFactory(domain="domain-as-string.com")
|
||||
|
||||
|
||||
def test_models_maildomainaccesses__domain_cannot_be_null():
|
||||
"""The "domain" field should not be null."""
|
||||
with pytest.raises(ValidationError, match="This field cannot be null"):
|
||||
factories.MailDomainAccessFactory(domain=None)
|
||||
|
||||
|
||||
# ROLE FIELD
|
||||
|
||||
|
||||
def test_models_maildomainaccesses__role_cannot_be_empty():
|
||||
"""The "role" field cannot be empty."""
|
||||
with pytest.raises(ValidationError, match="This field cannot be blank"):
|
||||
factories.MailDomainAccessFactory(role="")
|
||||
|
||||
|
||||
def test_models_maildomainaccesses__role_cannot_be_null():
|
||||
"""The "role" field cannot be null."""
|
||||
with pytest.raises(ValidationError, match="This field cannot be null"):
|
||||
factories.MailDomainAccessFactory(role=None)
|
||||
Reference in New Issue
Block a user