Improved coverage

This commit is contained in:
Antoine Nguyen
2025-09-05 18:45:15 +02:00
parent 5c0122cca7
commit 417715cde7
3 changed files with 31 additions and 63 deletions
-33
View File
@@ -1,33 +0,0 @@
"""Modoboa amavis constants."""
import collections
from django.utils.translation import gettext_lazy as _
MESSAGE_TYPES = collections.OrderedDict(
(
("C", _("Clean")),
("S", _("Spam")),
("Y", _("Spammy")),
("V", _("Virus")),
("H", _("Bad Header")),
("M", _("Bad MIME")),
("B", _("Banned")),
("O", _("Over sized")),
("T", _("MTA error")),
("U", _("Unchecked")),
)
)
MESSAGE_TYPE_COLORS = {
"C": "success",
"S": "danger",
"Y": "warning",
"V": "danger",
"H": "warning",
"M": "warning",
"B": "warning",
"O": "warning",
"T": "warning",
"U": "default",
}
-30
View File
@@ -3,13 +3,10 @@ import re
import socket
import struct
from email.utils import parseaddr
from functools import wraps
import idna
from django.conf import settings
from django.contrib.auth.views import redirect_to_login
from django.urls import reverse
from django.utils.translation import gettext as _
from rest_framework import authentication, exceptions
@@ -23,33 +20,6 @@ from .models import Msgrcpt, Policy, Users
from .utils import smart_bytes, smart_str
def selfservice(ssfunc=None):
"""Decorator used to expose views to the 'self-service' feature
The 'self-service' feature allows users to act on quarantined
messages without beeing authenticated.
This decorator only acts as a 'router'.
:param ssfunc: the function to call if the 'self-service'
pre-requisites are satisfied
"""
def decorator(f):
@wraps(f)
def wrapped_f(request, *args, **kwargs):
secret_id = request.GET.get("secret_id")
if not secret_id and request.user.is_authenticated:
return f(request, *args, **kwargs)
if not param_tools.get_global_parameter("self_service"):
return redirect_to_login(reverse("modoboa_amavis:index"))
return ssfunc(request, *args, **kwargs)
return wrapped_f
return decorator
class SelfServiceAuthentication(authentication.BaseAuthentication):
def authenticate(self, request):
+31
View File
@@ -17,6 +17,21 @@ from modoboa.core import models as core_models
from modoboa.lib.tests import ModoAPITestCase
AMAVIS_SETTINGS = {
"localpart_is_case_sensitive": False,
"recipient_delimiter": "",
"max_attachment_size": 14,
"released_msgs_cleanup": False,
"am_pdp_mode": "unix",
"am_pdp_host": "localhost",
"am_pdp_port": 9998,
"user_can_release": False,
"self_service": False,
"manual_learning": True,
"sa_is_local": True,
}
class TestDataMixin:
"""A mixin to provide test data."""
@@ -71,6 +86,13 @@ class QuarantineViewSetTestCase(TestDataMixin, ModoAPITestCase):
resp = self.client.get(f"{url}?rcpt={rcpt}")
self.assertEqual(resp.status_code, 200)
user = core_models.User.objects.get(username="user@test.com")
self.client.force_authenticate(user)
resp = self.client.get(f"{url}?rcpt={rcpt}")
self.assertEqual(resp.status_code, 200)
self.msgrcpt.refresh_from_db()
self.assertEqual(self.msgrcpt.rs, "V")
def test_headers(self):
mail_id = smart_str(self.msgrcpt.mail.mail_id)
url = reverse("v2:amavis-quarantine-headers", args=[mail_id])
@@ -286,3 +308,12 @@ class PolicyViewSetTestCase(ModoAPITestCase):
p = models.Policy.objects.get(users__email="@test.com")
self.assertEqual(p.bypass_virus_checks, data["bypass_virus_checks"])
self.assertEqual(p.spam_tag2_level, data["spam_tag2_level"])
class ParametersAPITestCase(ModoAPITestCase):
def test_update(self):
settings = AMAVIS_SETTINGS.copy()
url = reverse("v2:parameter-global-detail", args=["core"])
resp = self.client.put(url, settings, format="json")
self.assertEqual(resp.status_code, 200)