I can't create tests that involve authorization groups

Salutations,

I've been creating tests for testing web

I've been looking all over the internet to find the solution.

I've been building a fixture for a group that allows users to create blog posts. It's a series of test I've been building for the sake of authorization purposes. I've using both Pytest fixtures and factories with similar results.

Here's the Test Class:

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

I've also created Factories for both of them.

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()

I've created the "Contributor" Group using the development Admin page. I can't seem to understand why the groups are registering the groups and user classes. I've even tried to created the MemberFactory, but I ended with the same result. I would like to know how I can integrate authorization Groups into my tests. I can't seem to figure it out.

Thanks in advance.

Вернуться на верх