【发布时间】:2021-03-24 11:41:49
【问题描述】:
我正在测试我在 Django 中编写的 REST API,但此验证器无法按预期工作。我读了the docs on this,但我需要的不仅仅是描述;我需要一个工作示例。
我在 settings.py 中将其定义为默认值。
# my_app/settings.py
AUTH_PASSWORD_VALIDATORS = [
{
'NAME':
'django.contrib.auth.password_validation.UserAttributeSimilarityValidator',
},
...
]
但是,当我运行测试时,我得到了意想不到的意外成功。
# api/authentication/tests.py
body = {
'username': 'frank',
'email': 'frank@example.com',
'password1': 'frank@example.com',
'password2': 'frank@example.com',
}
response = self.client.post(url, body, format='json'))
self.assertEqual(response.status_code, status.HTTP_400_BAD_REQUEST)
> ./manage.py test
Creating test database for alias 'default'...
System check identified no issues (0 silenced).
F
======================================================================
FAIL: test_register (api.authentication.tests.AuthTests)
Ensure we can register a user and test for validation errors.
----------------------------------------------------------------------
Traceback (most recent call last):
File "/home/matt/Repositories/my_app/back-end/api/authentication/tests.py", line 108, in case_password_has_email
self.assertEqual(response.status_code, status.HTTP_400_BAD_REQUEST)
AssertionError: 201 != 400
----------------------------------------------------------------------
Ran 1 test in 0.275s
FAILED (failures=1)
Destroying test database for alias 'default'...
我错过了这个验证器的重点吗?我只是用错了吗?我的预期行为是发送带有错误消息的 400 响应,就像其他验证器允许的那样。我该如何做到这一点?
【问题讨论】:
-
您的注册视图到底是什么样的?
-
@WillemVanOnsem 我从
django-rest-auth导入:re_path(r'^register/', include('rest_auth.registration.urls')),。来源在这里:github.com/Tivix/django-rest-auth/blob/master/rest_auth/…
标签: python django validation django-rest-auth