✨(mail) add first_name and last_name for Mailbox
Mail provisioning API needs a full name to create a new mailbox.
This commit is contained in:
@@ -38,4 +38,4 @@ class MailDomainAccessAdmin(admin.ModelAdmin):
|
|||||||
class MailboxAdmin(admin.ModelAdmin):
|
class MailboxAdmin(admin.ModelAdmin):
|
||||||
"""Admin for mailbox model."""
|
"""Admin for mailbox model."""
|
||||||
|
|
||||||
list_display = ("__str__", "domain")
|
list_display = ("__str__", "first_name", "last_name")
|
||||||
|
|||||||
@@ -10,7 +10,7 @@ class MailboxSerializer(serializers.ModelSerializer):
|
|||||||
|
|
||||||
class Meta:
|
class Meta:
|
||||||
model = models.Mailbox
|
model = models.Mailbox
|
||||||
fields = ["id", "local_part", "secondary_email"]
|
fields = ["id", "first_name", "last_name", "local_part", "secondary_email"]
|
||||||
|
|
||||||
|
|
||||||
class MailDomainSerializer(serializers.ModelSerializer):
|
class MailDomainSerializer(serializers.ModelSerializer):
|
||||||
|
|||||||
@@ -62,16 +62,16 @@ class MailDomainAccessFactory(factory.django.DjangoModelFactory):
|
|||||||
|
|
||||||
|
|
||||||
class MailboxFactory(factory.django.DjangoModelFactory):
|
class MailboxFactory(factory.django.DjangoModelFactory):
|
||||||
"""A factory to create mailboxes for mail domain members."""
|
"""A factory to create mailboxes for a mail domain."""
|
||||||
|
|
||||||
class Meta:
|
class Meta:
|
||||||
model = models.Mailbox
|
model = models.Mailbox
|
||||||
|
|
||||||
class Params:
|
first_name = factory.Faker("first_name", locale="fr_FR")
|
||||||
"""Parameters for fields."""
|
last_name = factory.Faker("last_name", locale="de_DE")
|
||||||
|
|
||||||
full_name = factory.Faker("name")
|
local_part = factory.LazyAttribute(
|
||||||
|
lambda a: f"{slugify(a.first_name)}.{slugify(a.last_name)}"
|
||||||
local_part = factory.LazyAttribute(lambda a: a.full_name.lower().replace(" ", "."))
|
)
|
||||||
domain = factory.SubFactory(MailDomainEnabledFactory)
|
domain = factory.SubFactory(MailDomainEnabledFactory)
|
||||||
secondary_email = factory.Faker("email")
|
secondary_email = factory.Faker("email")
|
||||||
|
|||||||
@@ -0,0 +1,23 @@
|
|||||||
|
# Generated by Django 5.0.6 on 2024-08-07 09:43
|
||||||
|
|
||||||
|
from django.db import migrations, models
|
||||||
|
|
||||||
|
|
||||||
|
class Migration(migrations.Migration):
|
||||||
|
|
||||||
|
dependencies = [
|
||||||
|
('mailbox_manager', '0007_alter_maildomainaccess_role'),
|
||||||
|
]
|
||||||
|
|
||||||
|
operations = [
|
||||||
|
migrations.AddField(
|
||||||
|
model_name='mailbox',
|
||||||
|
name='first_name',
|
||||||
|
field=models.CharField(blank=True, max_length=200),
|
||||||
|
),
|
||||||
|
migrations.AddField(
|
||||||
|
model_name='mailbox',
|
||||||
|
name='last_name',
|
||||||
|
field=models.CharField(blank=True, max_length=200),
|
||||||
|
),
|
||||||
|
]
|
||||||
@@ -0,0 +1,22 @@
|
|||||||
|
# Generated by Django 5.0.6 on 2024-08-07 09:43
|
||||||
|
|
||||||
|
from django.db import migrations, models
|
||||||
|
from django.db.models import F
|
||||||
|
|
||||||
|
|
||||||
|
def populate_first_name_last_name(apps, schema_editor):
|
||||||
|
Mailbox = apps.get_model('mailbox_manager', 'Mailbox')
|
||||||
|
Mailbox.objects.filter(first_name='').update(first_name=F("local_part"))
|
||||||
|
Mailbox.objects.filter(last_name='').update(last_name=F("local_part"))
|
||||||
|
|
||||||
|
|
||||||
|
class Migration(migrations.Migration):
|
||||||
|
|
||||||
|
dependencies = [
|
||||||
|
('mailbox_manager', '0008_mailbox_first_name_mailbox_last_name'),
|
||||||
|
]
|
||||||
|
|
||||||
|
operations = [
|
||||||
|
migrations.RunPython(populate_first_name_last_name, reverse_code=migrations.RunPython.noop),
|
||||||
|
|
||||||
|
]
|
||||||
@@ -0,0 +1,23 @@
|
|||||||
|
# Generated by Django 5.0.6 on 2024-08-07 10:28
|
||||||
|
|
||||||
|
from django.db import migrations, models
|
||||||
|
|
||||||
|
|
||||||
|
class Migration(migrations.Migration):
|
||||||
|
|
||||||
|
dependencies = [
|
||||||
|
('mailbox_manager', '0009_fill_mailbox_first_name_mailbox_last_name'),
|
||||||
|
]
|
||||||
|
|
||||||
|
operations = [
|
||||||
|
migrations.AlterField(
|
||||||
|
model_name='mailbox',
|
||||||
|
name='first_name',
|
||||||
|
field=models.CharField(max_length=200),
|
||||||
|
),
|
||||||
|
migrations.AlterField(
|
||||||
|
model_name='mailbox',
|
||||||
|
name='last_name',
|
||||||
|
field=models.CharField(max_length=200),
|
||||||
|
),
|
||||||
|
]
|
||||||
@@ -108,6 +108,8 @@ class MailDomainAccess(BaseModel):
|
|||||||
class Mailbox(BaseModel):
|
class Mailbox(BaseModel):
|
||||||
"""Mailboxes for users from mail domain."""
|
"""Mailboxes for users from mail domain."""
|
||||||
|
|
||||||
|
first_name = models.CharField(max_length=200, blank=False)
|
||||||
|
last_name = models.CharField(max_length=200, blank=False)
|
||||||
local_part = models.CharField(
|
local_part = models.CharField(
|
||||||
_("local_part"),
|
_("local_part"),
|
||||||
max_length=150,
|
max_length=150,
|
||||||
|
|||||||
@@ -104,6 +104,47 @@ def test_api_mailboxes__create_roles_success(role):
|
|||||||
assert mailbox.secondary_email == mailbox_values["secondary_email"]
|
assert mailbox.secondary_email == mailbox_values["secondary_email"]
|
||||||
assert response.json() == {
|
assert response.json() == {
|
||||||
"id": str(mailbox.id),
|
"id": str(mailbox.id),
|
||||||
|
"first_name": str(mailbox.first_name),
|
||||||
|
"last_name": str(mailbox.last_name),
|
||||||
|
"local_part": str(mailbox.local_part),
|
||||||
|
"secondary_email": str(mailbox.secondary_email),
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
@pytest.mark.parametrize(
|
||||||
|
"role",
|
||||||
|
[
|
||||||
|
enums.MailDomainRoleChoices.OWNER,
|
||||||
|
enums.MailDomainRoleChoices.ADMIN,
|
||||||
|
],
|
||||||
|
)
|
||||||
|
def test_api_mailboxes__create_with_accent_success(role):
|
||||||
|
"""Users with proper abilities should be able to create mailbox on the mail domain with a
|
||||||
|
first_name accentuated."""
|
||||||
|
mail_domain = factories.MailDomainEnabledFactory()
|
||||||
|
access = factories.MailDomainAccessFactory(role=role, domain=mail_domain)
|
||||||
|
|
||||||
|
client = APIClient()
|
||||||
|
client.force_login(access.user)
|
||||||
|
|
||||||
|
mailbox_values = serializers.MailboxSerializer(
|
||||||
|
factories.MailboxFactory.build(first_name="Aimé")
|
||||||
|
).data
|
||||||
|
response = client.post(
|
||||||
|
f"/api/v1.0/mail-domains/{mail_domain.slug}/mailboxes/",
|
||||||
|
mailbox_values,
|
||||||
|
format="json",
|
||||||
|
)
|
||||||
|
|
||||||
|
assert response.status_code == status.HTTP_201_CREATED
|
||||||
|
mailbox = models.Mailbox.objects.get()
|
||||||
|
|
||||||
|
assert mailbox.local_part == mailbox_values["local_part"]
|
||||||
|
assert mailbox.secondary_email == mailbox_values["secondary_email"]
|
||||||
|
assert response.json() == {
|
||||||
|
"id": str(mailbox.id),
|
||||||
|
"first_name": str(mailbox.first_name),
|
||||||
|
"last_name": str(mailbox.last_name),
|
||||||
"local_part": str(mailbox.local_part),
|
"local_part": str(mailbox.local_part),
|
||||||
"secondary_email": str(mailbox.secondary_email),
|
"secondary_email": str(mailbox.secondary_email),
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -65,11 +65,15 @@ def test_api_mailboxes__list_roles(role):
|
|||||||
assert response.json()["results"] == [
|
assert response.json()["results"] == [
|
||||||
{
|
{
|
||||||
"id": str(mailbox2.id),
|
"id": str(mailbox2.id),
|
||||||
|
"first_name": str(mailbox2.first_name),
|
||||||
|
"last_name": str(mailbox2.last_name),
|
||||||
"local_part": str(mailbox2.local_part),
|
"local_part": str(mailbox2.local_part),
|
||||||
"secondary_email": str(mailbox2.secondary_email),
|
"secondary_email": str(mailbox2.secondary_email),
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
"id": str(mailbox1.id),
|
"id": str(mailbox1.id),
|
||||||
|
"first_name": str(mailbox1.first_name),
|
||||||
|
"last_name": str(mailbox1.last_name),
|
||||||
"local_part": str(mailbox1.local_part),
|
"local_part": str(mailbox1.local_part),
|
||||||
"secondary_email": str(mailbox1.secondary_email),
|
"secondary_email": str(mailbox1.secondary_email),
|
||||||
},
|
},
|
||||||
|
|||||||
Reference in New Issue
Block a user