Skip to content
Snippets Groups Projects
factories.py 2.58 KiB
Newer Older
  • Learn to ignore specific revisions
  • import factory
    from django.contrib.auth.models import Permission
    
    from django.utils import timezone
    
    Eliot Berriot's avatar
    Eliot Berriot committed
    from funkwhale_api.factories import ManyToManyFromList, registry
    
    
    from . import models
    
    
    @registry.register
    class GroupFactory(factory.django.DjangoModelFactory):
    
    Eliot Berriot's avatar
    Eliot Berriot committed
        name = factory.Sequence(lambda n: "group-{0}".format(n))
    
    Eliot Berriot's avatar
    Eliot Berriot committed
            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(
    
    Eliot Berriot's avatar
    Eliot Berriot committed
                        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 InvitationFactory(factory.django.DjangoModelFactory):
        owner = factory.LazyFunction(lambda: UserFactory())
    
        class Meta:
            model = "users.Invitation"
    
        class Params:
            expired = factory.Trait(expiration_date=factory.LazyFunction(timezone.now))
    
    
    
    @registry.register
    
    class UserFactory(factory.django.DjangoModelFactory):
    
    Eliot Berriot's avatar
    Eliot Berriot committed
        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
    
    Eliot Berriot's avatar
    Eliot Berriot committed
        groups = ManyToManyFromList("groups")
    
        avatar = factory.django.ImageField()
    
    Eliot Berriot's avatar
    Eliot Berriot committed
            model = "users.User"
            django_get_or_create = ("username",)
    
        class Params:
            invited = factory.Trait(invitation=factory.SubFactory(InvitationFactory))
    
    
        @factory.post_generation
        def perms(self, create, extracted, **kwargs):
            if not create:
                # Simple build, do nothing.
                return
    
            if extracted:
                perms = [
                    Permission.objects.get(
    
    Eliot Berriot's avatar
    Eliot Berriot committed
                        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)
    
        @factory.post_generation
        def with_actor(self, create, extracted, **kwargs):
            if not create or not extracted:
                return
            self.actor = models.create_actor(self)
            self.save(update_fields=["actor"])
            return self.actor
    
    
    Eliot Berriot's avatar
    Eliot Berriot committed
    @registry.register(name="users.SuperUser")
    
    class SuperUserFactory(UserFactory):
        is_staff = True
        is_superuser = True