Skip to content

Commit

Permalink
✅(tests) test dimail setup command
Browse files Browse the repository at this point in the history
Test (and slightly improve) dimail setup command,
which populate database on local dimail container
  • Loading branch information
mjeammet authored and Morendil committed Nov 5, 2024
1 parent 988a091 commit 9892390
Show file tree
Hide file tree
Showing 3 changed files with 88 additions and 12 deletions.
9 changes: 4 additions & 5 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down
22 changes: 15 additions & 7 deletions src/backend/mailbox_manager/management/commands/setup_dimail_db.py
Original file line number Diff line number Diff line change
Expand Up @@ -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()


Expand Down Expand Up @@ -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")

Expand Down
69 changes: 69 additions & 0 deletions src/backend/mailbox_manager/tests/test_command_dimail_setup_db.py
Original file line number Diff line number Diff line change
@@ -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

0 comments on commit 9892390

Please sign in to comment.