From 288809eb84def338aff922821650c614d9a3af12 Mon Sep 17 00:00:00 2001 From: Antoine Nguyen Date: Tue, 17 Jun 2025 16:52:41 +0200 Subject: [PATCH] Added test --- modoboa/rspamd/__init__.py | 12 ----- modoboa/rspamd/api/v2/serializers.py | 2 +- modoboa/rspamd/app_settings.py | 46 ------------------ modoboa/rspamd/apps.py | 11 +---- modoboa/rspamd/handlers.py | 2 - .../management/commands/manage_rspamd_maps.py | 13 +++-- modoboa/rspamd/models.py | 6 --- modoboa/rspamd/tests.py | 48 +++++++++++++++++-- modoboa/rspamd/utils.py | 9 ---- modoboa/rspamd/views.py | 6 --- test_project/test_project/settings.py | 1 - 11 files changed, 53 insertions(+), 103 deletions(-) delete mode 100644 modoboa/rspamd/models.py delete mode 100644 modoboa/rspamd/views.py diff --git a/modoboa/rspamd/__init__.py b/modoboa/rspamd/__init__.py index e9bd3d7f1..44eb36e16 100644 --- a/modoboa/rspamd/__init__.py +++ b/modoboa/rspamd/__init__.py @@ -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" diff --git a/modoboa/rspamd/api/v2/serializers.py b/modoboa/rspamd/api/v2/serializers.py index 140780b4b..8e67aec50 100644 --- a/modoboa/rspamd/api/v2/serializers.py +++ b/modoboa/rspamd/api/v2/serializers.py @@ -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): diff --git a/modoboa/rspamd/app_settings.py b/modoboa/rspamd/app_settings.py index c06e6030e..59da1d894 100644 --- a/modoboa/rspamd/app_settings.py +++ b/modoboa/rspamd/app_settings.py @@ -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, - ) diff --git a/modoboa/rspamd/apps.py b/modoboa/rspamd/apps.py index eaf8bf274..ca561a0e3 100644 --- a/modoboa/rspamd/apps.py +++ b/modoboa/rspamd/apps.py @@ -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() diff --git a/modoboa/rspamd/handlers.py b/modoboa/rspamd/handlers.py index ee68de66b..b26d17a0e 100644 --- a/modoboa/rspamd/handlers.py +++ b/modoboa/rspamd/handlers.py @@ -1,5 +1,3 @@ -# -*- coding: utf-8 -*- - """Modoboa rspamd handlers.""" from django.core.management import call_command diff --git a/modoboa/rspamd/management/commands/manage_rspamd_maps.py b/modoboa/rspamd/management/commands/manage_rspamd_maps.py index c4cf4474a..af98ad8a6 100644 --- a/modoboa/rspamd/management/commands/manage_rspamd_maps.py +++ b/modoboa/rspamd/management/commands/manage_rspamd_maps.py @@ -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") diff --git a/modoboa/rspamd/models.py b/modoboa/rspamd/models.py deleted file mode 100644 index 1dfab7604..000000000 --- a/modoboa/rspamd/models.py +++ /dev/null @@ -1,6 +0,0 @@ -# -*- coding: utf-8 -*- -from __future__ import unicode_literals - -from django.db import models - -# Create your models here. diff --git a/modoboa/rspamd/tests.py b/modoboa/rspamd/tests.py index 5982e6bcd..2f9678e60 100644 --- a/modoboa/rspamd/tests.py +++ b/modoboa/rspamd/tests.py @@ -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)) diff --git a/modoboa/rspamd/utils.py b/modoboa/rspamd/utils.py index 14d014070..782959d02 100644 --- a/modoboa/rspamd/utils.py +++ b/modoboa/rspamd/utils.py @@ -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( diff --git a/modoboa/rspamd/views.py b/modoboa/rspamd/views.py deleted file mode 100644 index e784a0bd2..000000000 --- a/modoboa/rspamd/views.py +++ /dev/null @@ -1,6 +0,0 @@ -# -*- coding: utf-8 -*- -from __future__ import unicode_literals - -from django.shortcuts import render - -# Create your views here. diff --git a/test_project/test_project/settings.py b/test_project/test_project/settings.py index 75e1092c1..c815583ad 100644 --- a/test_project/test_project/settings.py +++ b/test_project/test_project/settings.py @@ -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"