Added test

This commit is contained in:
Antoine Nguyen
2025-06-17 16:52:41 +02:00
parent 8e28176c04
commit 288809eb84
11 changed files with 53 additions and 103 deletions
-12
View File
@@ -1,15 +1,3 @@
# -*- coding: utf-8 -*-
"""The rspamd frontend of Modoboa."""
from __future__ import unicode_literals
from pkg_resources import DistributionNotFound, get_distribution
try:
__version__ = get_distribution(__name__).version
except DistributionNotFound:
# package is not installed
pass
default_app_config = "modoboa.rspamd.apps.ModoboaRspamdConfig"
+1 -1
View File
@@ -7,7 +7,7 @@ from django.utils.translation import gettext_lazy as _
from rest_framework import serializers
def validate_file_path(value):
def validate_file_path(value: str) -> None:
if value:
if os.path.isfile(value):
if not os.access(value, os.W_OK):
-46
View File
@@ -1,14 +1,9 @@
# -*- coding: utf-8 -*-
"""Modoboa rspamd forms."""
import collections
from django import forms
from django.utils.translation import gettext_lazy as _
from modoboa.lib import form_utils
from modoboa.parameters import forms as param_forms
RSPAMD_PARAMETERS_STRUCT = collections.OrderedDict(
[
@@ -68,44 +63,3 @@ RSPAMD_PARAMETERS_STRUCT = collections.OrderedDict(
),
]
)
class ParametersForm(param_forms.AdminParametersForm):
"""Extension settings."""
app = "modoboa.rspamd"
dkim_settings_sep = form_utils.SeparatorField(label=_("DKIM signing settings"))
key_map_path = forms.CharField(
label=_("Key map path"),
initial="",
help_text=_(
"Absolute path of the file which contains paths to DKIM "
"private keys. "
"Must be readable by _rspamd group or user."
),
required=False,
)
selector_map_path = forms.CharField(
label=_("Selector map path"),
initial="",
help_text=_(
"Absolute path of the file which contains names of "
"DKIM selectors. "
"Must be readable by _rspamd group or user."
),
required=False,
)
miscellaneous_sep = form_utils.SeparatorField(label=_("Miscellaneous"))
rspamd_dashboard_location = forms.CharField(
label=_("Rspamd dashboard location"),
initial="/rspamd",
help_text=_(
"Location of the rspamd dashboard. "
"Can either be a relative URL (eg. '/rspamd') "
"or a full URL (eg. 'https://rspamd.domain.tld/')."
"Only use for guidance on the Super-Admin dashboard."
),
required=False,
)
+2 -9
View File
@@ -1,9 +1,6 @@
# -*- coding: utf-8 -*-
from django.apps import AppConfig
from django.utils.translation import gettext_lazy
from .utils import check_rspamd_installed
def load_rspamd_settings():
"""Load rspamd settings.
@@ -15,9 +12,6 @@ def load_rspamd_settings():
from . import app_settings
from .api.v2 import serializers
param_tools.registry.add(
"global", app_settings.ParametersForm, gettext_lazy("Rspamd")
)
param_tools.registry.add2(
"global",
"rspamd",
@@ -32,7 +26,6 @@ class ModoboaRspamdConfig(AppConfig):
verbose_name = "Modoboa connector for Rspamd."
def ready(self):
if check_rspamd_installed():
from . import handlers # NOQA:F401
from . import handlers # NOQA:F401
load_rspamd_settings()
load_rspamd_settings()
-2
View File
@@ -1,5 +1,3 @@
# -*- coding: utf-8 -*-
"""Modoboa rspamd handlers."""
from django.core.management import call_command
@@ -10,8 +10,8 @@ class Command(BaseCommand):
"""Command class."""
def load_files(self):
config = dict(param_tools.get_global_parameters("modoboa.rspamd"))
if not config["key_map_path"] or not config["selector_map_path"]:
self.config = dict(param_tools.get_global_parameters("rspamd"))
if not self.config["key_map_path"] or not self.config["selector_map_path"]:
raise CommandError(
"path map path and/or selector map path "
"not set in modoboa rspamd settings."
@@ -19,7 +19,7 @@ class Command(BaseCommand):
self.dkim_path_map = {}
try:
with open(config["key_map_path"], "r") as f:
with open(self.config["key_map_path"]) as f:
for line in f:
domain_name, path = line.split()
self.dkim_path_map[domain_name] = path.replace("\n", "")
@@ -27,7 +27,7 @@ class Command(BaseCommand):
pass
self.selector_map = {}
try:
with open(config["selector_map_path"], "r") as f:
with open(self.config["selector_map_path"]) as f:
for line in f:
domain_name, selector = line.split()
self.selector_map[domain_name] = selector
@@ -89,13 +89,12 @@ class Command(BaseCommand):
qset = models.Domain.objects.all()
for domain in qset:
self.manage_domain(domain)
config = dict(param_tools.get_global_parameters("modoboa.rspamd"))
if self.modified_selector_file:
with open(config["selector_map_path"], "w") as f:
with open(self.config["selector_map_path"], "w") as f:
for domain_name, selector in self.selector_map.items():
f.write(f"{domain_name} {selector}\n")
if self.modified_key_path_file:
with open(config["key_map_path"], "w") as f:
with open(self.config["key_map_path"], "w") as f:
for domain_name, key_path in self.dkim_path_map.items():
f.write(f"{domain_name} {key_path}\n")
-6
View File
@@ -1,6 +0,0 @@
# -*- coding: utf-8 -*-
from __future__ import unicode_literals
from django.db import models
# Create your models here.
+44 -4
View File
@@ -1,6 +1,46 @@
# -*- coding: utf-8 -*-
from __future__ import unicode_literals
import os
import shutil
from django.test import TestCase
from django.core import management
# Create your tests here.
from modoboa.admin import factories as admin_factories, models as admin_models
from modoboa.lib.tests import ModoAPITestCase
class ManagementCommandTestCase(ModoAPITestCase):
@classmethod
def setUpTestData(cls):
super().setUpTestData()
admin_factories.populate_database()
def tearDown(self):
"""Reset test env."""
shutil.rmtree(self.workdir)
def setUp(self):
super().setUp()
self.key_map_path = f"{self.workdir}/keys.map"
self.set_global_parameter("key_map_path", self.key_map_path)
self.selector_map_path = f"{self.workdir}/selectors.map"
self.set_global_parameter("selector_map_path", self.selector_map_path)
def test_command(self):
management.call_command("manage_rspamd_maps")
self.assertFalse(os.path.exists(self.key_map_path))
self.assertFalse(os.path.exists(self.selector_map_path))
domain = admin_models.Domain.objects.get(name="test.com")
domain.enable_dkim = True
domain.save()
management.call_command("manage_rspamd_maps")
self.assertTrue(os.path.exists(self.key_map_path))
self.assertTrue(os.path.exists(self.selector_map_path))
def test_command_with_arg(self):
domain = admin_models.Domain.objects.get(name="test.com")
domain.enable_dkim = True
domain.save()
management.call_command("manage_rspamd_maps", "--domain", "test.com")
self.assertTrue(os.path.exists(self.key_map_path))
self.assertTrue(os.path.exists(self.selector_map_path))
-9
View File
@@ -1,21 +1,12 @@
"""Utilies for modoboa.rspamd plugin."""
import shutil
from django.core.exceptions import AppRegistryNotReady
def check_rspamd_installed():
return shutil.which("rspamd") is not None
def get_rspamd_options():
try:
from modoboa.core import models
is_rspamd_installed = check_rspamd_installed()
if not is_rspamd_installed:
return None
rspamd_options = {}
localconfig = models.LocalConfig.objects.first()
rspamd_location = localconfig.parameters.get_value(
-6
View File
@@ -1,6 +0,0 @@
# -*- coding: utf-8 -*-
from __future__ import unicode_literals
from django.shortcuts import render
# Create your views here.
-1
View File
@@ -260,7 +260,6 @@ SPECTACULAR_SETTINGS = {
# DOVECOT_LOOKUP_PATH = ('/path/to/dovecot', )
DOVECOT_USER = "root"
MODOBOA_API_URL = "https://api.modoboa.org/1/"
PID_FILE_STORAGE_PATH = "/tmp"