[英]How do you test django-axes with a Django unit test?
I'm using Django Axes with my Django project to ensure that if someone is trying to guess a password, their IP gets blocked for a while.我将 Django 轴与我的 Django 项目一起使用,以确保如果有人试图猜测密码,他们的 IP 会被阻止一段时间。
It's setup, and works great.设置好了,效果很好。
I want to write a Django test that makes sure that after X number of guesses, the user is really locked out.我想编写一个 Django 测试,以确保在 X 次猜测之后,用户真的被锁定了。
The problem I'm having is that if you try to use the django test client to do a login as you normally would:我遇到的问题是,如果您尝试像往常一样使用 django 测试客户端进行登录:
self.client.login(username="invalid_user", password="invalid_password")
Axes crashes, because it isn't getting a request arg:轴崩溃,因为它没有收到请求 arg:
Traceback (most recent call last):
File "/opt/my_project/website/login_app/tests/test_views.py", line 71, in test_brute_force_attempts
self.client.login(username="invalid_user", password="invalid_password")
File "/opt/my_project/venv3/lib/python3.6/site-packages/django/test/client.py", line 602, in login
user = authenticate(**credentials)
File "/opt/my_project/venv3/lib/python3.6/site-packages/django/contrib/auth/__init__.py", line 73, in authenticate
user = backend.authenticate(request, **credentials)
File "/opt/my_project/venv3/lib/python3.6/site-packages/axes/helpers.py", line 411, in inner
return func(*args, **kwargs)
File "/opt/my_project/venv3/lib/python3.6/site-packages/axes/backends.py", line 39, in authenticate
"AxesBackend requires a request as an argument to authenticate"
axes.exceptions.AxesBackendRequestParameterRequired: AxesBackend requires a request as an argument to authenticate
This is a known issue, and is discussed here: https://github.com/jazzband/django-axes/issues/433这是一个已知问题,在这里讨论: https://github.com/jazzband/django-axes/issues/433
As far as I can tell, you just need to pass self.client.login
a request=
arg, which it will pass to django.contrib.auth.authenticate
, and it'll be happy.据我所知,您只需向
self.client.login
传递一个request=
arg,它将传递给django.contrib.auth.authenticate
,它会很高兴。 I can't figure out where to get this request
within a test method.我不知道在测试方法中从哪里获得这个
request
。 I tried this, but it didn't work either:我试过这个,但它也没有工作:
class TestBruteForceAttempts(TestCase):
def test_brute_force_attempts(self):
login_attempts_to_make = django_settings.AXES_FAILURE_LIMIT + 1
for i in range(login_attempts_to_make):
self.client.login(request=self.client.request(), username="invalid_user", password="invalid_password")
...
Is there any way to test Django Axes from a Django unit test?有没有办法从 Django 单元测试中测试 Django 轴? I'm clearly doing something wrong.
我显然做错了什么。
As stated in the Django Axes documentation , rather than using client.login
you can directly call the Django authenticate
function passing a mock request.如Django Axes 文档中所述,您可以直接调用 Django
authenticate
function传递模拟请求,而不是使用client.login
。
Something like this should do the trick:这样的事情应该可以解决问题:
from django.contrib.auth import authenticate
from django.http import HttpRequest
class TestBruteForceAttempts(TestCase):
def test_brute_force_attempts(self):
login_attempts_to_make = django_settings.AXES_FAILURE_LIMIT + 1
for i in range(login_attempts_to_make):
request = HttpRequest()
authenticate(request, username="invalid_user", password="invalid_password")
# Assert IP is blocked
You probably need to specify some information in the request, for example a fixed IP.您可能需要在请求中指定一些信息,例如固定的 IP。
Alternatively, here you can see how the test you need is implemented in Django Axes, you could use the same approach.或者, 在这里您可以看到您需要的测试是如何在 Django 轴中实现的,您可以使用相同的方法。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.