【问题标题】:How is the UserAttributeSimilarityValidator supposed to be used in Django REST Auth?UserAttributeSimilarityValidator 应该如何在 Django REST Auth 中使用?
【发布时间】: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 响应,就像其他验证器允许的那样。我该如何做到这一点?

【问题讨论】:

标签: python django validation django-rest-auth


【解决方案1】:

编辑 1

我引用的 repo 不再维护,因为项目已移至此处:https://github.com/iMerica/dj-rest-auth。文档在这里:https://github.com/iMerica/dj-rest-auth。基本上,只需使用 pip 包dj-rest-auth 而不是django-rest-auth

编辑 2

看起来旧 repo 中的 PR 尚未添加到新 repo 中,因此必须更改。

原答案

这里有一个针对此错误的 PR:https://github.com/Tivix/django-rest-auth/pull/482

使用 pip 安装:

pip install git+https://github.com/dgilge/django-rest-auth@patch-2

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2016-07-01
    • 2019-05-31
    • 1970-01-01
    • 2020-10-03
    • 2018-04-27
    • 2016-07-14
    • 2017-01-31
    相关资源
    最近更新 更多