✅(tests) test dimail setup command
Test (and slightly improve) dimail setup command, which populate database on local dimail container
This commit is contained in:
committed by
Laurent Bossavit
parent
988a091e53
commit
989239082e
9
Makefile
9
Makefile
@@ -131,11 +131,6 @@ demo: ## flush db then create a demo for load testing purpose
|
|||||||
@$(MANAGE) create_demo
|
@$(MANAGE) create_demo
|
||||||
.PHONY: 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...
|
# Nota bene: Black should come after isort just in case they don't agree...
|
||||||
lint: ## lint back-end python sources
|
lint: ## lint back-end python sources
|
||||||
@@ -283,6 +278,10 @@ i18n-generate-and-upload: \
|
|||||||
# -- INTEROPERABILTY
|
# -- INTEROPERABILTY
|
||||||
# -- Dimail configuration
|
# -- Dimail configuration
|
||||||
|
|
||||||
|
recreate-dimail-container:
|
||||||
|
@$(COMPOSE) up --force-recreate -d dimail
|
||||||
|
.PHONY: recreate-dimail-container
|
||||||
|
|
||||||
dimail-setup-db:
|
dimail-setup-db:
|
||||||
@echo "$(BOLD)Populating database of local dimail API container$(RESET)"
|
@echo "$(BOLD)Populating database of local dimail API container$(RESET)"
|
||||||
@$(MANAGE) setup_dimail_db
|
@$(MANAGE) setup_dimail_db
|
||||||
|
|||||||
@@ -7,6 +7,9 @@ from django.core.management.base import BaseCommand, CommandError
|
|||||||
import requests
|
import requests
|
||||||
from rest_framework import status
|
from rest_framework import status
|
||||||
|
|
||||||
|
from mailbox_manager.enums import MailDomainStatusChoices
|
||||||
|
from mailbox_manager.models import MailDomain
|
||||||
|
|
||||||
User = get_user_model()
|
User = get_user_model()
|
||||||
|
|
||||||
|
|
||||||
@@ -48,16 +51,21 @@ class Command(BaseCommand):
|
|||||||
perms=["new_domain", "create_users", "manage_users"],
|
perms=["new_domain", "create_users", "manage_users"],
|
||||||
)
|
)
|
||||||
|
|
||||||
|
# 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)
|
||||||
|
|
||||||
# we create a dimail user for keycloak+regie user John Doe
|
# 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 way, la Régie will be able to make request in the name of
|
||||||
# this user
|
# this user
|
||||||
people_base_user = User.objects.get(name="John Doe")
|
people_base_user = User.objects.filter(name="John Doe")
|
||||||
self.create_user(name=people_base_user.sub, password="whatever") # noqa S106
|
if people_base_user.exists():
|
||||||
|
self.create_user(name=people_base_user.sub, password="whatever") # noqa S106
|
||||||
# we create a domain and add John Doe to it
|
self.create_allows(people_base_user.sub, domain_name)
|
||||||
domain_name = "test.domain.com"
|
|
||||||
self.create_domain(domain_name)
|
|
||||||
self.create_allows(people_base_user.sub, domain_name)
|
|
||||||
|
|
||||||
self.stdout.write("DONE", ending="\n")
|
self.stdout.write("DONE", ending="\n")
|
||||||
|
|
||||||
|
|||||||
@@ -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
|
||||||
Reference in New Issue
Block a user