From 5dc1cfea8632b1f464cc3ffb0c1957598239dd9a Mon Sep 17 00:00:00 2001 From: Antoine Nguyen Date: Tue, 13 Jan 2026 09:44:20 +0100 Subject: [PATCH] Improved coverage. --- modoboa/amavis/tests/test_jobs.py | 18 +++++++ modoboa/amavis/tests/test_viewsets.py | 5 +- modoboa/calendars/tests.py | 3 +- modoboa/core/management/commands/cleanlogs.py | 53 ------------------- modoboa/core/tests/test_core.py | 14 ----- modoboa/core/tests/test_jobs.py | 40 ++++++++++++++ modoboa/maillog/tests/test_views.py | 9 ++-- 7 files changed, 67 insertions(+), 75 deletions(-) create mode 100644 modoboa/amavis/tests/test_jobs.py delete mode 100644 modoboa/core/management/commands/cleanlogs.py create mode 100644 modoboa/core/tests/test_jobs.py diff --git a/modoboa/amavis/tests/test_jobs.py b/modoboa/amavis/tests/test_jobs.py new file mode 100644 index 000000000..45184a3e3 --- /dev/null +++ b/modoboa/amavis/tests/test_jobs.py @@ -0,0 +1,18 @@ +"""Tests cases for RQ jobs.""" + +from modoboa.lib.tests import ModoTestCase + +from modoboa.amavis import factories, jobs, models + + +class JobsTestCase(ModoTestCase): + + databases = "__all__" + + def test_qcleanup(self): + factories.create_spam("user@test.com", rs="D") + jobs.qcleanup() + self.assertEqual(models.Quarantine.objects.count(), 0) + self.assertEqual(models.Msgs.objects.count(), 0) + self.assertEqual(models.Maddr.objects.count(), 0) + self.assertEqual(models.Msgrcpt.objects.count(), 0) diff --git a/modoboa/amavis/tests/test_viewsets.py b/modoboa/amavis/tests/test_viewsets.py index 79c71cfaa..28cfd57f3 100644 --- a/modoboa/amavis/tests/test_viewsets.py +++ b/modoboa/amavis/tests/test_viewsets.py @@ -4,14 +4,13 @@ from unittest import mock from rq import SimpleWorker from django.core import mail -from django.core.management import call_command from django.test import override_settings from django.urls import reverse import django_rq from modoboa.admin import factories as admin_factories, models as admin_models -from modoboa.amavis import factories, models +from modoboa.amavis import factories, jobs, models from modoboa.amavis.utils import smart_str from modoboa.core import models as core_models from modoboa.lib.tests import ModoAPITestCase @@ -204,7 +203,7 @@ class QuarantineViewSetTestCase(TestDataMixin, ModoAPITestCase): self.assertEqual(self.msgrcpt.rs, "p") # Send notification to admins - call_command("amnotify") + jobs.amnotify() self.assertEqual(len(mail.outbox), 1) def _test_mark_selection(self, action, status): diff --git a/modoboa/calendars/tests.py b/modoboa/calendars/tests.py index 3f00ed214..bfd657782 100644 --- a/modoboa/calendars/tests.py +++ b/modoboa/calendars/tests.py @@ -18,6 +18,7 @@ from modoboa.lib.tests import ModoAPITestCase from modoboa.admin.factories import populate_database from . import factories +from . import jobs from . import models from . import mocks @@ -106,7 +107,7 @@ class AccessRuleTestCase(ModoAPITestCase): self.assertEqual(cfg.get(section, "permissions"), "Rr") # Call a second time - management.call_command("generate_rights", verbosity=False) + jobs.generate_rights() def test_rights_file_generation_with_admin(self): self.set_global_parameter( diff --git a/modoboa/core/management/commands/cleanlogs.py b/modoboa/core/management/commands/cleanlogs.py deleted file mode 100644 index 46f3d2ac5..000000000 --- a/modoboa/core/management/commands/cleanlogs.py +++ /dev/null @@ -1,53 +0,0 @@ -import datetime -import logging - -from django.core.management.base import BaseCommand -from django.utils import timezone - -from modoboa.core.models import Log -from modoboa.maillog.models import Maillog -from modoboa.parameters import tools as param_tools - - -class Command(BaseCommand): - """Command class.""" - - help = "Log table cleanup" # NOQA:A003 - - def add_arguments(self, parser): - """Add extra arguments to command line.""" - parser.add_argument( - "--debug", action="store_true", default=False, help="Activate debug output" - ) - parser.add_argument( - "--verbose", - action="store_true", - default=False, - help="Display informational messages", - ) - - def __vprint(self, msg): - if not self.verbose: - return - print(msg) - - def handle(self, *args, **options): - if options["debug"]: - log = logging.getLogger("django.db.backends") - log.setLevel(logging.DEBUG) - log.addHandler(logging.StreamHandler()) - self.verbose = options["verbose"] - - log_maximum_age = param_tools.get_global_parameter("log_maximum_age") - self.__vprint(f"Deleting logs older than {log_maximum_age} days...") - limit = timezone.now() - datetime.timedelta(log_maximum_age) - Log.objects.filter(date_created__lt=limit).delete() - message_history_maximum_age = param_tools.get_global_parameter( - "message_history_maximum_age" - ) - self.__vprint( - f"Deleting messages in history older than {message_history_maximum_age} days..." - ) - limit = timezone.now() - datetime.timedelta(message_history_maximum_age) - Maillog.objects.filter(date__lt=limit).delete() - self.__vprint("Done.") diff --git a/modoboa/core/tests/test_core.py b/modoboa/core/tests/test_core.py index 99a448ebb..94d0bd331 100644 --- a/modoboa/core/tests/test_core.py +++ b/modoboa/core/tests/test_core.py @@ -16,7 +16,6 @@ from django.urls import reverse from django.utils import timezone from modoboa.lib.tests import ModoTestCase, SimpleModoTestCase -from modoboa.maillog import factories as maillog_factories, models as maillog_models from .. import factories, mocks, models @@ -59,19 +58,6 @@ class ManagementCommandsTestCase(SimpleModoTestCase): management.call_command("load_initial_data", "--admin-username", "modoadmin") self.assertTrue(self.client.login(username="modoadmin", password="password")) - def test_clean_logs(self): - """Run cleanlogs command.""" - management.call_command("load_initial_data", "--no-frontend") - log1 = factories.LogFactory() - factories.LogFactory() - log1.date_created -= relativedelta(years=2) - log1.save(update_fields=["date_created"]) - maillog_factories.MaillogFactory(date=timezone.now() - relativedelta(days=190)) - maillog_factories.MaillogFactory() - management.call_command("cleanlogs") - self.assertEqual(models.Log.objects.count(), 1) - self.assertEqual(maillog_models.Maillog.objects.count(), 1) - def test_init_data_non_duplicate_client_creation(self): """Test that the load_initial_data command does not create duplicates client for the frontend deployment.""" diff --git a/modoboa/core/tests/test_jobs.py b/modoboa/core/tests/test_jobs.py new file mode 100644 index 000000000..0b52c8389 --- /dev/null +++ b/modoboa/core/tests/test_jobs.py @@ -0,0 +1,40 @@ +"""Tests cases for RQ jobs.""" + +from dateutil.relativedelta import relativedelta +import httmock + +from django.core import mail +from django.core import management +from django.utils import timezone + +from modoboa.core import factories, jobs, mocks, models +from modoboa.lib.tests import SimpleModoTestCase +from modoboa.maillog import factories as maillog_factories, models as maillog_models + + +class JobsTestCase(SimpleModoTestCase): + + def test_clean_logs(self): + """Check clean_logs job.""" + management.call_command("load_initial_data", "--no-frontend") + log1 = factories.LogFactory() + factories.LogFactory() + log1.date_created -= relativedelta(years=2) + log1.save(update_fields=["date_created"]) + maillog_factories.MaillogFactory(date=timezone.now() - relativedelta(days=190)) + maillog_factories.MaillogFactory() + jobs.clean_logs() + self.assertEqual(models.Log.objects.count(), 1) + self.assertEqual(maillog_models.Maillog.objects.count(), 1) + + def test_communicate_with_public_api(self): + """Check that job starts fine.""" + with httmock.HTTMock( + mocks.modo_api_instance_search, + mocks.modo_api_instance_create, + mocks.modo_api_instance_update, + mocks.modo_api_versions, + ): + jobs.communicate_with_public_api() + self.assertEqual(models.LocalConfig.objects.first().api_pk, 100) + self.assertEqual(len(mail.outbox), 0) diff --git a/modoboa/maillog/tests/test_views.py b/modoboa/maillog/tests/test_views.py index 376bb97bd..86bd6b9ca 100644 --- a/modoboa/maillog/tests/test_views.py +++ b/modoboa/maillog/tests/test_views.py @@ -11,6 +11,7 @@ from django.test import override_settings from modoboa.admin import factories as admin_factories from modoboa.lib.tests import SimpleModoTestCase +from modoboa.maillog import jobs class RunCommandsMixin: @@ -36,14 +37,14 @@ class RunCommandsMixin: with open(path, "w") as fp: fp.write(content) self.set_global_parameter("logfile", path) - call_command("logparser") + jobs.logparser() def run_update_statistics(self, rebuild=False): """Run update_statistics command.""" - args = [] if rebuild: - args.append("--rebuild") - call_command("update_statistics", *args) + call_command("update_statistics", ["--rebuild"]) + else: + jobs.update_statistics() @override_settings(RRDTOOL_TEST_MODE=True)