policies/reputation: fix test

Signed-off-by: Jens Langhammer <jens.langhammer@beryju.org>
This commit is contained in:
Jens Langhammer 2022-01-05 21:44:15 +01:00
parent 3b05c9cb1a
commit 5a2c367e89
3 changed files with 5 additions and 8 deletions

View File

@ -36,14 +36,15 @@ class ReputationPolicy(Policy):
def passes(self, request: PolicyRequest) -> PolicyResult: def passes(self, request: PolicyRequest) -> PolicyResult:
remote_ip = get_client_ip(request.http_request) remote_ip = get_client_ip(request.http_request)
passing = False
query = Q() query = Q()
if self.check_ip: if self.check_ip:
query |= Q(ip=remote_ip) query |= Q(ip=remote_ip)
if self.check_username: if self.check_username:
query |= Q(identifier=request.user.username) query |= Q(identifier=request.user.username)
score = Reputation.objects.filter(query).annotate(total_score=Sum("score")).total_score score = (
passing += passing or score <= self.threshold Reputation.objects.filter(query).aggregate(total_score=Sum("score"))["total_score"] or 0
)
passing = score <= self.threshold
LOGGER.debug( LOGGER.debug(
"Score for user", "Score for user",
username=request.user.username, username=request.user.username,

View File

@ -4,7 +4,6 @@ from django.core.cache import cache
from django.test import RequestFactory, TestCase from django.test import RequestFactory, TestCase
from authentik.core.models import User from authentik.core.models import User
from authentik.lib.utils.http import DEFAULT_IP
from authentik.policies.reputation.models import CACHE_KEY_PREFIX, Reputation, ReputationPolicy from authentik.policies.reputation.models import CACHE_KEY_PREFIX, Reputation, ReputationPolicy
from authentik.policies.reputation.tasks import save_reputation from authentik.policies.reputation.tasks import save_reputation
from authentik.policies.types import PolicyRequest from authentik.policies.types import PolicyRequest

View File

@ -48,10 +48,7 @@ class AuthenticatorSMSStageTests(APITestCase):
"title": "", "title": "",
}, },
"pending_user": "foo", "pending_user": "foo",
"pending_user_avatar": ( "pending_user_avatar": "/static/dist/assets/images/user_default.png",
"https://secure.gravatar.com/avatar/d41d8cd98f00"
"b204e9800998ecf8427e?s=158&amp;r=g"
),
"phone_number_required": True, "phone_number_required": True,
"type": ChallengeTypes.NATIVE.value, "type": ChallengeTypes.NATIVE.value,
}, },