gpt4 book ai didi

django - pytest-django:设置用户权限

转载 作者:行者123 更新时间:2023-12-01 09:16:44 27 4
gpt4 key购买 nike

我使用 pytest 3.0.6 和 pytest-django 3.1.2 为 Django 开发一个库。我的这个非常简单的测试失败了,我不明白发生了什么:

# test_mytest.py
import pytest
from django.contrib.auth.models import Permission
from django.contrib.contenttypes.models import ContentType


@pytest.mark.django_db
def test_user_has_perm(django_user_model):
# Create a new user
john_doe = django_user_model.objects.create_user('johndoe', email='jd@example.com', password='123456')

# Get or create the permission to set on user
user_ct = ContentType.objects.get(app_label='auth', model='user')
p, _ = Permission.objects.get_or_create(content_type=user_ct, codename='delete_user', name="Can delete user")

# User don't have the permission
assert john_doe.has_perm(p) is False

# Set permission to user
john_doe.user_permissions.add(p)
assert john_doe.has_perm(p) is True # ---> FAIL

以防万一,测试的结果是:

$ pytest
============================= test session starts =============================
platform win32 -- Python 3.5.3, pytest-3.0.6, py-1.4.32, pluggy-0.4.0
Django settings: testsite.settings (from ini file)
rootdir: D:\Dev\foss\django-modern-rpc, inifile: tox.ini
plugins: pythonpath-0.7.1, django-3.1.2, cov-2.4.0
collected 1 items

modernrpc\tests\test_test_test.py F

================================== FAILURES ===================================
_____________________________ test_user_has_perm ______________________________

django_user_model = <class 'django.contrib.auth.models.User'>

@pytest.mark.django_db
def test_user_has_perm(django_user_model):
# Create a new user
john_doe = django_user_model.objects.create_user('johndoe', email='jd@example.com', password='123456')

# Get or create the permission to set on user
user_ct = ContentType.objects.get(app_label='auth', model='user')
p, _ = Permission.objects.get_or_create(content_type=user_ct, codename='delete_user', name="Can delete user")

# User don't have the permission
assert john_doe.has_perm(p) is False

# Set permission to user
john_doe.user_permissions.add(p)
> assert john_doe.has_perm(p) is True # ---> FAIL
E assert False is True
E + where False = <bound method PermissionsMixin.has_perm of <User: johndoe>>(<Permission: auth | user | Can delete user>)
E + where <bound method PermissionsMixin.has_perm of <User: johndoe>> = <User: johndoe>.has_perm

modernrpc\tests\test_test_test.py:20: AssertionError
========================== 1 failed in 0.32 seconds ===========================

配置 block ,来自 tox.ini:

[pytest]
DJANGO_SETTINGS_MODULE = testsite.settings
norecursedirs = .git __pycache__ build dist venv* .tox .vscode .cache *.egg-info
python_paths = modernrpc/tests
testpaths = modernrpc/tests
python_files = test_*.py dummy_*.py

以及数据库配置,来自测试设置:

BASE_DIR = os.path.dirname(os.path.dirname(__file__))
DATABASES = {
'default': {
'ENGINE': 'django.db.backends.sqlite3',
'NAME': os.path.join(BASE_DIR, 'modern_rpc.sqlite3'),
},
}

我做错了什么?

最佳答案

你需要使用字符串'app_label.codename':

Returns True if the user has the specified permission, where perm is in the format "<app label>.<permission codename>".

此外,如果您在上次调用 has_perm 后更改了权限,则必须清除 user._perm_cacheuser._user_perm_cache 或检索数据库中此用户的新实例,以确保没有缓存:

 del john_doe._perm_cache
del john_doe._user_perm_cache
# OR
john_doe = django_user_model.objects.get(username='johndoe')

这是因为 has_perm 会调用 auth 后端,而后端又会先查询这些缓存。

关于django - pytest-django:设置用户权限,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42220272/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com