funkwhale/api/funkwhale_api/users/factories.py

64 wiersze
1.9 KiB
Python
Czysty Zwykły widok Historia

import factory
from django.contrib.auth.models import Permission
2018-06-10 08:55:16 +00:00
from funkwhale_api.factories import ManyToManyFromList, registry
@registry.register
class GroupFactory(factory.django.DjangoModelFactory):
2018-06-09 13:36:16 +00:00
name = factory.Sequence(lambda n: "group-{0}".format(n))
class Meta:
2018-06-09 13:36:16 +00:00
model = "auth.Group"
@factory.post_generation
def perms(self, create, extracted, **kwargs):
if not create:
# Simple build, do nothing.
return
if extracted:
perms = [
Permission.objects.get(
2018-06-09 13:36:16 +00:00
content_type__app_label=p.split(".")[0], codename=p.split(".")[1]
)
for p in extracted
]
# A list of permissions were passed in, use them
self.permissions.add(*perms)
@registry.register
class UserFactory(factory.django.DjangoModelFactory):
2018-06-09 13:36:16 +00:00
username = factory.Sequence(lambda n: "user-{0}".format(n))
email = factory.Sequence(lambda n: "user-{0}@example.com".format(n))
password = factory.PostGenerationMethodCall("set_password", "test")
subsonic_api_token = None
2018-06-09 13:36:16 +00:00
groups = ManyToManyFromList("groups")
class Meta:
2018-06-09 13:36:16 +00:00
model = "users.User"
django_get_or_create = ("username",)
@factory.post_generation
def perms(self, create, extracted, **kwargs):
if not create:
# Simple build, do nothing.
return
if extracted:
perms = [
Permission.objects.get(
2018-06-09 13:36:16 +00:00
content_type__app_label=p.split(".")[0], codename=p.split(".")[1]
)
for p in extracted
]
# A list of permissions were passed in, use them
self.user_permissions.add(*perms)
2018-06-09 13:36:16 +00:00
@registry.register(name="users.SuperUser")
class SuperUserFactory(UserFactory):
is_staff = True
is_superuser = True