Skip to content

Commit 9892390

Browse files
mjeammetMorendil
authored andcommitted
✅(tests) test dimail setup command
Test (and slightly improve) dimail setup command, which populate database on local dimail container
1 parent 988a091 commit 9892390

File tree

3 files changed

+88
-12
lines changed

3 files changed

+88
-12
lines changed

Makefile

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -131,11 +131,6 @@ demo: ## flush db then create a demo for load testing purpose
131131
@$(MANAGE) create_demo
132132
.PHONY: demo
133133

134-
reset-dimail-container:
135-
@$(COMPOSE) up --force-recreate -d dimail
136-
@$(MAKE) setup-dimail-db
137-
.PHONY: reset-dimail-container
138-
139134

140135
# Nota bene: Black should come after isort just in case they don't agree...
141136
lint: ## lint back-end python sources
@@ -283,6 +278,10 @@ i18n-generate-and-upload: \
283278
# -- INTEROPERABILTY
284279
# -- Dimail configuration
285280

281+
recreate-dimail-container:
282+
@$(COMPOSE) up --force-recreate -d dimail
283+
.PHONY: recreate-dimail-container
284+
286285
dimail-setup-db:
287286
@echo "$(BOLD)Populating database of local dimail API container$(RESET)"
288287
@$(MANAGE) setup_dimail_db

src/backend/mailbox_manager/management/commands/setup_dimail_db.py

Lines changed: 15 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,9 @@
77
import requests
88
from rest_framework import status
99

10+
from mailbox_manager.enums import MailDomainStatusChoices
11+
from mailbox_manager.models import MailDomain
12+
1013
User = get_user_model()
1114

1215

@@ -48,16 +51,21 @@ def handle(self, *args, **options):
4851
perms=["new_domain", "create_users", "manage_users"],
4952
)
5053

51-
# we create a dimail user for keycloak+regie user John Doe
52-
# This way, la Régie will be able to make request in the name of
53-
# this user
54-
people_base_user = User.objects.get(name="John Doe")
55-
self.create_user(name=people_base_user.sub, password="whatever") # noqa S106
56-
5754
# we create a domain and add John Doe to it
5855
domain_name = "test.domain.com"
56+
if not MailDomain.objects.filter(name=domain_name).exists():
57+
MailDomain.objects.create(
58+
name=domain_name, status=MailDomainStatusChoices.ENABLED
59+
)
5960
self.create_domain(domain_name)
60-
self.create_allows(people_base_user.sub, domain_name)
61+
62+
# we create a dimail user for keycloak+regie user John Doe
63+
# This way, la Régie will be able to make request in the name of
64+
# this user
65+
people_base_user = User.objects.filter(name="John Doe")
66+
if people_base_user.exists():
67+
self.create_user(name=people_base_user.sub, password="whatever") # noqa S106
68+
self.create_allows(people_base_user.sub, domain_name)
6169

6270
self.stdout.write("DONE", ending="\n")
6371

Lines changed: 69 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,69 @@
1+
"""Test the `setup_dimail_db` management command"""
2+
3+
from django.conf import settings
4+
from django.contrib.auth import get_user_model
5+
from django.core.management import call_command
6+
7+
import pytest
8+
import requests
9+
10+
from mailbox_manager.management.commands.setup_dimail_db import DIMAIL_URL, admin
11+
12+
pytestmark = pytest.mark.django_db
13+
14+
admin_auth = (admin["username"], admin["password"])
15+
User = get_user_model()
16+
17+
18+
@pytest.mark.skipif(
19+
settings.DEBUG is not True,
20+
reason="Run only in local (dimail container not running in other envs)",
21+
)
22+
def test_commands_setup_dimail_db():
23+
"""The create_demo management command should create objects as expected."""
24+
call_command("setup_dimail_db")
25+
26+
# check created users
27+
response = requests.get(url=f"{DIMAIL_URL}/users/", auth=admin_auth, timeout=10)
28+
users = response.json()
29+
30+
# if John Doe exists, we created a dimail user for them
31+
local_user = User.objects.filter(name="John Doe").exists()
32+
33+
assert len(users) == 3 if local_user else 2
34+
# remove uuid because we cannot devine them
35+
[user.pop("uuid") for user in users] # pylint: disable=W0106
36+
37+
if local_user:
38+
assert users.pop() == {
39+
"is_admin": False,
40+
"name": User.objects.get(name="John Doe").uuid,
41+
"perms": [],
42+
}
43+
assert users == [
44+
{
45+
"is_admin": True,
46+
"name": "admin",
47+
"perms": [],
48+
},
49+
{
50+
"is_admin": False,
51+
"name": "la_regie",
52+
"perms": [
53+
"new_domain",
54+
"create_users",
55+
"manage_users",
56+
],
57+
},
58+
]
59+
60+
# check created domains
61+
response = requests.get(url=f"{DIMAIL_URL}/domains/", auth=admin_auth, timeout=10)
62+
domains = response.json()
63+
assert len(domains) == 1
64+
assert domains[0]["name"] == "test.domain.com"
65+
66+
# check created allows
67+
response = requests.get(url=f"{DIMAIL_URL}/allows/", auth=admin_auth, timeout=10)
68+
allows = response.json()
69+
assert len(allows) == 2 if local_user else 1

0 commit comments

Comments
 (0)