(tests) fix tests after adding slugs to domains

- slug readonly on admin
- fix test to expect slug in payload, when retrieving a domain
This commit is contained in:
Marie PUPO JEAMMET
2024-06-19 16:48:07 +02:00
committed by Sebastien Nobour
parent 93d4abee58
commit 19c36eafde
5 changed files with 24 additions and 4 deletions

View File

@@ -14,9 +14,10 @@ class MailDomainAdmin(admin.ModelAdmin):
"name",
"created_at",
"updated_at",
"slug",
)
search_fields = ("name",)
readonly_fields = ["created_at"]
readonly_fields = ["created_at", "slug"]
@admin.register(models.MailDomainAccess)

View File

@@ -26,6 +26,12 @@ class MailDomainSerializer(serializers.ModelSerializer):
"created_at",
"updated_at",
]
read_only_fields = [
"id",
"slug",
"created_at",
"updated_at",
]
class MailDomainAccessSerializer(serializers.ModelSerializer):

View File

@@ -28,10 +28,14 @@ class MailDomain(BaseModel):
return self.name
def save(self, *args, **kwargs):
if not self.slug:
self.slug = slugify(self.name)
"""Override save function to compute the slug."""
self.slug = self.get_slug()
return super().save(*args, **kwargs)
def get_slug(self):
"""Compute slug value from name."""
return slugify(self.name)
def get_abilities(self, user):
"""
Compute and return abilities for a given user on the domain.

View File

@@ -66,6 +66,7 @@ def test_api_mail_domains__retrieve_authenticated_related():
assert response.json() == {
"id": str(domain.id),
"name": domain.name,
"slug": domain.slug,
"created_at": domain.created_at.isoformat().replace("+00:00", "Z"),
"updated_at": domain.updated_at.isoformat().replace("+00:00", "Z"),
}

View File

@@ -3,6 +3,7 @@ Unit tests for the MailDomain model
"""
from django.core.exceptions import ValidationError
from django.utils.text import slugify
import pytest
@@ -10,7 +11,6 @@ from mailbox_manager import factories
pytestmark = pytest.mark.django_db
# NAME FIELD
@@ -24,3 +24,11 @@ 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)