2017-06-23 21:00:42 +00:00
|
|
|
import factory
|
|
|
|
from django.contrib.auth.models import Permission
|
|
|
|
|
2018-06-10 08:55:16 +00:00
|
|
|
from funkwhale_api.factories import ManyToManyFromList, registry
|
|
|
|
|
2017-06-23 21:00:42 +00:00
|
|
|
|
2018-05-18 19:19:20 +00:00
|
|
|
@registry.register
|
|
|
|
class GroupFactory(factory.django.DjangoModelFactory):
|
2018-06-09 13:36:16 +00:00
|
|
|
name = factory.Sequence(lambda n: "group-{0}".format(n))
|
2018-05-18 19:19:20 +00:00
|
|
|
|
|
|
|
class Meta:
|
2018-06-09 13:36:16 +00:00
|
|
|
model = "auth.Group"
|
2018-05-18 19:19:20 +00:00
|
|
|
|
|
|
|
@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]
|
2018-05-18 19:19:20 +00:00
|
|
|
)
|
|
|
|
for p in extracted
|
|
|
|
]
|
|
|
|
# A list of permissions were passed in, use them
|
|
|
|
self.permissions.add(*perms)
|
|
|
|
|
|
|
|
|
2017-12-24 18:15:21 +00:00
|
|
|
@registry.register
|
2017-06-23 21:00:42 +00:00
|
|
|
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")
|
2018-05-08 14:31:19 +00:00
|
|
|
subsonic_api_token = None
|
2018-06-09 13:36:16 +00:00
|
|
|
groups = ManyToManyFromList("groups")
|
2017-06-23 21:00:42 +00:00
|
|
|
|
|
|
|
class Meta:
|
2018-06-09 13:36:16 +00:00
|
|
|
model = "users.User"
|
|
|
|
django_get_or_create = ("username",)
|
2017-06-23 21:00:42 +00:00
|
|
|
|
|
|
|
@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]
|
2017-06-23 21:00:42 +00:00
|
|
|
)
|
|
|
|
for p in extracted
|
|
|
|
]
|
|
|
|
# A list of permissions were passed in, use them
|
|
|
|
self.user_permissions.add(*perms)
|
2017-12-24 18:15:21 +00:00
|
|
|
|
|
|
|
|
2018-06-09 13:36:16 +00:00
|
|
|
@registry.register(name="users.SuperUser")
|
2017-12-24 18:15:21 +00:00
|
|
|
class SuperUserFactory(UserFactory):
|
|
|
|
is_staff = True
|
|
|
|
is_superuser = True
|