diff --git a/Makefile b/Makefile index 12236fecf..903a8dff4 100644 --- a/Makefile +++ b/Makefile @@ -131,11 +131,6 @@ demo: ## flush db then create a demo for load testing purpose @$(MANAGE) create_demo .PHONY: demo -reset-dimail-container: - @$(COMPOSE) up --force-recreate -d dimail - @$(MAKE) setup-dimail-db -.PHONY: reset-dimail-container - # Nota bene: Black should come after isort just in case they don't agree... lint: ## lint back-end python sources @@ -283,6 +278,10 @@ i18n-generate-and-upload: \ # -- INTEROPERABILTY # -- Dimail configuration +recreate-dimail-container: + @$(COMPOSE) up --force-recreate -d dimail +.PHONY: recreate-dimail-container + dimail-setup-db: @echo "$(BOLD)Populating database of local dimail API container$(RESET)" @$(MANAGE) setup_dimail_db diff --git a/src/backend/mailbox_manager/management/commands/setup_dimail_db.py b/src/backend/mailbox_manager/management/commands/setup_dimail_db.py index d928b7ef8..be0e19e9e 100644 --- a/src/backend/mailbox_manager/management/commands/setup_dimail_db.py +++ b/src/backend/mailbox_manager/management/commands/setup_dimail_db.py @@ -7,6 +7,9 @@ import requests from rest_framework import status +from mailbox_manager.enums import MailDomainStatusChoices +from mailbox_manager.models import MailDomain + User = get_user_model() @@ -48,16 +51,21 @@ def handle(self, *args, **options): perms=["new_domain", "create_users", "manage_users"], ) - # we create a dimail user for keycloak+regie user John Doe - # This way, la Régie will be able to make request in the name of - # this user - people_base_user = User.objects.get(name="John Doe") - self.create_user(name=people_base_user.sub, password="whatever") # noqa S106 - # we create a domain and add John Doe to it domain_name = "test.domain.com" + if not MailDomain.objects.filter(name=domain_name).exists(): + MailDomain.objects.create( + name=domain_name, status=MailDomainStatusChoices.ENABLED + ) self.create_domain(domain_name) - self.create_allows(people_base_user.sub, domain_name) + + # we create a dimail user for keycloak+regie user John Doe + # This way, la Régie will be able to make request in the name of + # this user + people_base_user = User.objects.filter(name="John Doe") + if people_base_user.exists(): + self.create_user(name=people_base_user.sub, password="whatever") # noqa S106 + self.create_allows(people_base_user.sub, domain_name) self.stdout.write("DONE", ending="\n") diff --git a/src/backend/mailbox_manager/tests/test_command_dimail_setup_db.py b/src/backend/mailbox_manager/tests/test_command_dimail_setup_db.py new file mode 100644 index 000000000..b7e8cf5b2 --- /dev/null +++ b/src/backend/mailbox_manager/tests/test_command_dimail_setup_db.py @@ -0,0 +1,69 @@ +"""Test the `setup_dimail_db` management command""" + +from django.conf import settings +from django.contrib.auth import get_user_model +from django.core.management import call_command + +import pytest +import requests + +from mailbox_manager.management.commands.setup_dimail_db import DIMAIL_URL, admin + +pytestmark = pytest.mark.django_db + +admin_auth = (admin["username"], admin["password"]) +User = get_user_model() + + +@pytest.mark.skipif( + settings.DEBUG is not True, + reason="Run only in local (dimail container not running in other envs)", +) +def test_commands_setup_dimail_db(): + """The create_demo management command should create objects as expected.""" + call_command("setup_dimail_db") + + # check created users + response = requests.get(url=f"{DIMAIL_URL}/users/", auth=admin_auth, timeout=10) + users = response.json() + + # if John Doe exists, we created a dimail user for them + local_user = User.objects.filter(name="John Doe").exists() + + assert len(users) == 3 if local_user else 2 + # remove uuid because we cannot devine them + [user.pop("uuid") for user in users] # pylint: disable=W0106 + + if local_user: + assert users.pop() == { + "is_admin": False, + "name": User.objects.get(name="John Doe").uuid, + "perms": [], + } + assert users == [ + { + "is_admin": True, + "name": "admin", + "perms": [], + }, + { + "is_admin": False, + "name": "la_regie", + "perms": [ + "new_domain", + "create_users", + "manage_users", + ], + }, + ] + + # check created domains + response = requests.get(url=f"{DIMAIL_URL}/domains/", auth=admin_auth, timeout=10) + domains = response.json() + assert len(domains) == 1 + assert domains[0]["name"] == "test.domain.com" + + # check created allows + response = requests.get(url=f"{DIMAIL_URL}/allows/", auth=admin_auth, timeout=10) + allows = response.json() + assert len(allows) == 2 if local_user else 1