fix failing unittests

This commit is contained in:
Jens Langhammer 2019-03-03 20:34:00 +01:00
parent fb82d56307
commit a4707ddc54
5 changed files with 9 additions and 13 deletions

View file

@ -1,6 +1,7 @@
"""passbook core policy engine""" """passbook core policy engine"""
from logging import getLogger from logging import getLogger
from ipware import get_client_ip
from celery import group from celery import group
from django.http import HttpRequest from django.http import HttpRequest
@ -9,15 +10,6 @@ from passbook.core.models import Policy, User
LOGGER = getLogger(__name__) LOGGER = getLogger(__name__)
def get_remote_ip(request: HttpRequest) -> str:
"""Return the remote's IP"""
if not request:
return '0.0.0.0' # nosec
if request.META.get('HTTP_X_FORWARDED_FOR'):
return request.META.get('HTTP_X_FORWARDED_FOR')
return request.META.get('REMOTE_ADDR')
@CELERY_APP.task() @CELERY_APP.task()
def _policy_engine_task(user_pk, policy_pk, **kwargs): def _policy_engine_task(user_pk, policy_pk, **kwargs):
"""Task wrapper to run policy checking""" """Task wrapper to run policy checking"""
@ -66,8 +58,11 @@ class PolicyEngine:
signatures = [] signatures = []
kwargs = { kwargs = {
'__password__': getattr(self._user, '__password__', None), '__password__': getattr(self._user, '__password__', None),
'remote_ip': get_remote_ip(self._request)
} }
if self._request:
kwargs['remote_ip'], _ = get_client_ip(self._request)
if not kwargs['remote_ip']:
kwargs['remote_ip'] = '255.255.255.255',
for policy in self.policies: for policy in self.policies:
signatures.append(_policy_engine_task.s(self._user.pk, policy.pk.hex, **kwargs)) signatures.append(_policy_engine_task.s(self._user.pk, policy.pk.hex, **kwargs))
self._group = group(signatures)() self._group = group(signatures)()

View file

@ -1,5 +1,6 @@
django>=2.0 django>=2.0
django-model-utils django-model-utils
django-ipware
djangorestframework djangorestframework
PyYAML PyYAML
raven raven

View file

@ -1 +0,0 @@
django-ipware

View file

@ -13,7 +13,9 @@ LOGGER = getLogger(__name__)
def update_score(request, username, amount): def update_score(request, username, amount):
"""Update score for IP and User""" """Update score for IP and User"""
remote_ip = get_client_ip(request) remote_ip, _ = get_client_ip(request)
if not remote_ip:
remote_ip = '255.255.255.255'
ip_score, _ = IPScore.objects.update_or_create(ip=remote_ip) ip_score, _ = IPScore.objects.update_or_create(ip=remote_ip)
ip_score.score += amount ip_score.score += amount
ip_score.save() ip_score.save()

View file

@ -4,7 +4,6 @@
-r passbook/saml_idp/requirements.txt -r passbook/saml_idp/requirements.txt
-r passbook/otp/requirements.txt -r passbook/otp/requirements.txt
-r passbook/oauth_provider/requirements.txt -r passbook/oauth_provider/requirements.txt
-r passbook/suspicious_policy/requirements.txt
-r passbook/captcha_factor/requirements.txt -r passbook/captcha_factor/requirements.txt
-r passbook/admin/requirements.txt -r passbook/admin/requirements.txt
-r passbook/api/requirements.txt -r passbook/api/requirements.txt