I am fairly new to Django. I am trying to set the groups
field for User
using factory_boy
. The default User
class has a field _groups
. I tried setting that, but that is not helping.
class GroupFactory(factory.django.DjangoModelFactory):
class Meta:
model = Group
name = Sequence(lambda n: "group_{0}".format(n))
class UserFactory(factory.django.DjangoModelFactory):
class Meta:
model = User
username = factory.Sequence(lambda n: "user_{0}".format(n))
password = "test"
first_name = u'ßhamra'
last_name = u'ßhamra'
_groups = factory.SubFactory(GroupFactory)
@classmethod
def _create(cls, model_class, *args, **kwargs):
"""
Override the default ``_create`` with our custom call.
Due to internal behavior or create user create method that forces is_staff kwarg
"""
g = GroupFactory("abc")
manager = cls._get_manager(model_class)
is_staff = kwargs.pop('is_staff', None)
user = manager.create_user(*args, **kwargs)
if is_staff:
user.is_staff = is_staff
user.save()
return user
When using Factory Boy you create classes called "factories", which each represent a Django model. For example, for a user, you would create a factory class as follows: # factories.pyimportfactoryfromfactory.djangoimportDjangoModelFactoryfrom.modelsimportUser# Defining a factoryclassUserFactory(DjangoModelFactory):classMeta:model=Username=factory.
It's a library that's built for automated testing, but it also works well for this use-case. Factory Boy can easily be configured to generate random but realistic data like names, emails and paragraphs by internally using the Fakerlibrary. When using Factory Boy you create classes called "factories", which each represent a Django model.
The purpose of factory_boy is to provide a default way of getting a new instance, while still being able to override some fields on a per-call basis.
The code below utilizes the standard django groups. You have to use a factory boy post generation to add groups to the user.
Factories
import factory
import django.contrib.auth.models as auth_models
from django.contrib.auth.hashers import make_password
user_password = 'password'
class SubscribedGroupFactory(factory.django.DjangoModelFactory):
class Meta:
model = auth_models.Group
name = 'subscribed'
class UserFactory(factory.django.DjangoModelFactory):
class Meta:
model = auth_models.User
first_name = "Sophia"
last_name = "Ball"
username = "[email protected]"
password = make_password(user_password)
email = "[email protected]"
is_active = True
@factory.post_generation
def groups(self, create, extracted, **kwargs):
if not create:
return
if extracted:
for group in extracted:
self.groups.add(group)
Test
from app.factories import UserFactory, SubscribedGroupFactory, user_password
class TestCenterTest(TestCase):
def test_test_center_redirect(self):
user = UserFactory.create(groups=(SubscribedGroupFactory.create(),))
self.client.login(username=user.email, password=user_password)
response = self.client.get('/test-center/')
self.assertEqual(302, response.status_code)
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With