diff --git a/src/backend/demo/defaults.py b/src/backend/demo/defaults.py index 498156914..4ffd91e1f 100644 --- a/src/backend/demo/defaults.py +++ b/src/backend/demo/defaults.py @@ -4,4 +4,5 @@ "users": 1000, "teams": 100, "max_users_per_team": 100, + "domains": 20, } diff --git a/src/backend/demo/management/commands/create_demo.py b/src/backend/demo/management/commands/create_demo.py index ccce0ecef..009e3631e 100755 --- a/src/backend/demo/management/commands/create_demo.py +++ b/src/backend/demo/management/commands/create_demo.py @@ -10,12 +10,15 @@ from django import db from django.conf import settings from django.core.management.base import BaseCommand, CommandError +from django.utils.text import slugify from faker import Faker from core import models from demo import defaults +from mailbox_manager import models as mailbox_models +from mailbox_manager.enums import MailDomainStatusChoices fake = Faker() @@ -152,6 +155,35 @@ def create_demo(stdout): ) queue.flush() + with Timeit(stdout, "Creating domains"): + for _i in range(defaults.NB_OBJECTS["domains"]): + name = fake.domain_name() + slug = slugify(name) + + queue.push( + mailbox_models.MailDomain( + name=name, + # slug should be automatic but bulk_create doesn't use save + slug=slug, + status=random.choice(MailDomainStatusChoices.choices)[0], + ) + ) + queue.flush() + + with Timeit(stdout, "Creating accesses to domains"): + domains_ids = list( + mailbox_models.MailDomain.objects.values_list("id", flat=True) + ) + for domain_id in domains_ids: + queue.push( + mailbox_models.MailDomainAccess( + domain_id=domain_id, + user_id=random.choice(users_ids), + role=models.RoleChoices.OWNER, + ) + ) + queue.flush() + class Command(BaseCommand): """A management command to create a demo database.""" diff --git a/src/backend/demo/tests/test_commands_create_demo.py b/src/backend/demo/tests/test_commands_create_demo.py index 0ae0d44f5..f48627531 100644 --- a/src/backend/demo/tests/test_commands_create_demo.py +++ b/src/backend/demo/tests/test_commands_create_demo.py @@ -10,12 +10,13 @@ from core import models from demo import defaults +from mailbox_manager import models as mailbox_models TEST_NB_OBJECTS = { "users": 5, "teams": 3, - "max_identities_per_user": 3, "max_users_per_team": 5, + "domains": 2, } pytestmark = pytest.mark.django_db @@ -27,9 +28,11 @@ def test_commands_create_demo(): """The create_demo management command should create objects as expected.""" call_command("create_demo") - assert models.User.objects.count() == 5 - assert models.Team.objects.count() == 3 - assert models.TeamAccess.objects.count() >= 3 + assert models.User.objects.count() == TEST_NB_OBJECTS["users"] + assert models.Team.objects.count() == TEST_NB_OBJECTS["teams"] + assert models.TeamAccess.objects.count() >= TEST_NB_OBJECTS["teams"] + assert mailbox_models.MailDomain.objects.count() == TEST_NB_OBJECTS["domains"] + assert mailbox_models.MailDomainAccess.objects.count() == TEST_NB_OBJECTS["domains"] def test_commands_createsuperuser():