我无法创建涉及授权组的测试

问题描述 投票:0回答:1

致敬,

我一直在创建用于测试网络的测试

我一直在互联网上寻找解决方案。

我一直在为一个小组构建一个固定装置,允许用户创建博客文章。 这是我为了授权目的而构建的一系列测试。我使用 Pytest 装置和工厂得到了相似的结果。

这是测试类:

import pytest
import factory
from django.contrib.auth import get_user_model
from posts.models import Post
from members.models import Member
from factories import MemberFactory, PostFactory
from faker import Faker
from django.contrib.auth.models import Group

# Create your tests here.
User = get_user_model()
fake = Faker()





#Fixture
@pytest.fixture(scope="session")
def contributor_group(db):
    return Group.objects.create("Contributor")



@pytest.fixture(scope="session")
def authorized_user(db):
    authorized_user = MemberFactory()
    return authorized_user


 

# Post Tests
class TestPosts:
#Disallows user to create a post if they're not a contributor
    @pytest.mark.django_db
    def test_is_not_contributor(db):
        reg = MemberFactory()
        
        assert reg.has_post_permissions() is False
#Allows a user to create a post if they're a contributor.     
    @pytest.mark.django_db
    def test_can_post(db):
        contributor_group.
        print(authorized_user)
        
        print(contributor_group)
        print(authorized_user.has_post_permissions())
        assert authorized_user.has_post_permissions() is True

我还为他们两个创建了工厂。

import factory
from faker import Faker
from django.contrib.auth.models import PermissionsMixin
from django.contrib.auth import get_user_model
from posts.models import Post
from members.models import Member
fake = Faker()
User = get_user_model()

class MemberFactory(factory.django.DjangoModelFactory):
    class Meta:
        model = User
        

    username = factory.Faker('user_name')
    email = factory.Faker('email')
    password = 'password'
    is_active = True
    is_superuser = False
    
    @classmethod
    def has_post_permissions(self):
        self.has_post_permissions()

    


class PostFactory(factory.django.DjangoModelFactory):
    class Meta:
        model = Post

    title = fake.text(150)
    author = factory.SubFactory(MemberFactory)
    content = fake.text()

我使用开发管理页面创建了“贡献者”组。我似乎无法理解为什么这些组要注册组和用户类别。我什至尝试创建 MemberFactory,但最终得到相同的结果。我想知道如何将授权组集成到我的测试中。我似乎无法弄清楚。

提前致谢。

django pytest
1个回答
0
投票

您应该通过创建对象来测试权限,以完全测试您视图的功能,这里有一些您可以改进的地方:

测试权限.py

import pytest

from myapp.models import Post

from django.urls import reverse
from django.test import Client
from django.contrib.auth import get_user_model
from django.contrib.auth.models import Group, Permission
from django.contrib.contenttypes.models import ContentType

User = get_user_model()


@pytest.mark.django_db
class TestPermissions:

    def test_post_add_contributor_group_permission(db):
        client = Client()

        user = User.objects.create_user(
            username="testuser",
            email="[email protected]",
            password="pass123"
        )

        post_data = {
            "title": "Post Title",
            "content": "post content",
            "author": user.id
        }

        group = Group.objects.create(name="Contributor")
        client.login(username="testuser", password="pass123")

        # Send request without permission
        response = client.post(reverse("post-add"), post_data)
        assert response.status_code == 200
        assert Post.objects.all().count() == 0

        # Add permission to group, group to user and resend request
        content_type = ContentType.objects.get_for_model(Post)
        permission = Permission.objects.get(
            content_type=content_type,
            codename__icontains="add"
        )
        group.permissions.add(permission)
        user.groups.add(group)

        response = client.post(reverse("post-add"), post_data)

        assert response.status_code == 200
        assert Post.objects.all().count() == 1

views.py

def create_post(request):
    if request.method == "POST":
        title = request.POST.get("title")
        content = request.POST.get("content")
        user_id = request.POST.get("author")

        if request.user.has_perm("myapp.add_post"):
            Post.objects.create(
                title=title,
                content=content,
                author=User.objects.get(pk=user_id)
            )
        else:
            pass

    return render(request, "post_create.html")

© www.soinside.com 2019 - 2024. All rights reserved.