2019-03-25 16:02:51 +00:00
|
|
|
import pytz
|
2017-06-23 21:00:42 +00:00
|
|
|
import factory
|
|
|
|
from django.contrib.auth.models import Permission
|
2018-06-19 19:47:43 +00:00
|
|
|
from django.utils import timezone
|
2019-01-04 13:36:08 +00:00
|
|
|
from funkwhale_api.factories import ManyToManyFromList, registry, NoUpdateOnCreate
|
2018-06-10 08:55:16 +00:00
|
|
|
|
2018-07-22 10:20:16 +00:00
|
|
|
from . import models
|
|
|
|
|
2017-06-23 21:00:42 +00:00
|
|
|
|
2018-05-18 19:19:20 +00:00
|
|
|
@registry.register
|
2019-01-04 13:36:08 +00:00
|
|
|
class GroupFactory(NoUpdateOnCreate, 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)
|
|
|
|
|
|
|
|
|
2020-04-01 12:34:56 +00:00
|
|
|
@registry.register
|
|
|
|
class EmailAddressFactory(NoUpdateOnCreate, factory.django.DjangoModelFactory):
|
|
|
|
verified = False
|
|
|
|
primary = True
|
|
|
|
|
|
|
|
class Meta:
|
|
|
|
model = "account.EmailAddress"
|
|
|
|
|
|
|
|
|
|
|
|
@registry.register
|
|
|
|
class EmailConfirmationFactory(NoUpdateOnCreate, factory.django.DjangoModelFactory):
|
|
|
|
email_address = factory.SubFactory(EmailAddressFactory)
|
|
|
|
|
|
|
|
class Meta:
|
|
|
|
model = "account.EmailConfirmation"
|
|
|
|
|
|
|
|
|
2018-06-19 19:47:43 +00:00
|
|
|
@registry.register
|
2019-01-04 13:36:08 +00:00
|
|
|
class InvitationFactory(NoUpdateOnCreate, factory.django.DjangoModelFactory):
|
2018-06-19 19:47:43 +00:00
|
|
|
owner = factory.LazyFunction(lambda: UserFactory())
|
|
|
|
|
|
|
|
class Meta:
|
|
|
|
model = "users.Invitation"
|
|
|
|
|
|
|
|
class Params:
|
|
|
|
expired = factory.Trait(expiration_date=factory.LazyFunction(timezone.now))
|
|
|
|
|
|
|
|
|
2019-07-08 13:26:14 +00:00
|
|
|
class PasswordSetter(factory.PostGenerationMethodCall):
|
|
|
|
def call(self, instance, step, context):
|
|
|
|
if context.value_provided and context.value is None:
|
|
|
|
# disable setting the password, it's set by hand outside of the factory
|
|
|
|
return
|
|
|
|
|
|
|
|
return super().call(instance, step, context)
|
|
|
|
|
|
|
|
|
2017-12-24 18:15:21 +00:00
|
|
|
@registry.register
|
2017-06-23 21:00:42 +00:00
|
|
|
class UserFactory(factory.django.DjangoModelFactory):
|
2019-07-08 13:26:14 +00:00
|
|
|
username = factory.Faker("user_name")
|
|
|
|
email = factory.Faker("email")
|
|
|
|
password = password = PasswordSetter("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")
|
2018-07-22 10:20:16 +00:00
|
|
|
avatar = factory.django.ImageField()
|
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
|
|
|
|
2018-06-19 19:47:43 +00:00
|
|
|
class Params:
|
|
|
|
invited = factory.Trait(invitation=factory.SubFactory(InvitationFactory))
|
|
|
|
|
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-07-22 10:20:16 +00:00
|
|
|
@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
|
|
|
|
|
2020-04-01 12:34:56 +00:00
|
|
|
@factory.post_generation
|
|
|
|
def verified_email(self, create, extracted, **kwargs):
|
|
|
|
if not create or extracted is None:
|
|
|
|
return
|
|
|
|
return EmailConfirmationFactory(
|
|
|
|
email_address__verified=extracted,
|
|
|
|
email_address__email=self.email,
|
|
|
|
email_address__user=self,
|
|
|
|
)
|
|
|
|
|
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
|
2019-03-25 16:02:51 +00:00
|
|
|
|
|
|
|
|
|
|
|
@registry.register
|
|
|
|
class ApplicationFactory(factory.django.DjangoModelFactory):
|
|
|
|
name = factory.Faker("name")
|
|
|
|
redirect_uris = factory.Faker("url")
|
|
|
|
client_type = models.Application.CLIENT_CONFIDENTIAL
|
|
|
|
authorization_grant_type = models.Application.GRANT_AUTHORIZATION_CODE
|
|
|
|
scope = "read"
|
|
|
|
|
|
|
|
class Meta:
|
|
|
|
model = "users.Application"
|
|
|
|
|
|
|
|
|
|
|
|
@registry.register
|
|
|
|
class GrantFactory(factory.django.DjangoModelFactory):
|
|
|
|
application = factory.SubFactory(ApplicationFactory)
|
|
|
|
scope = factory.SelfAttribute(".application.scope")
|
|
|
|
redirect_uri = factory.SelfAttribute(".application.redirect_uris")
|
|
|
|
user = factory.SubFactory(UserFactory)
|
|
|
|
expires = factory.Faker("future_datetime", end_date="+15m")
|
|
|
|
code = factory.Faker("uuid4")
|
|
|
|
|
|
|
|
class Meta:
|
|
|
|
model = "users.Grant"
|
|
|
|
|
|
|
|
|
|
|
|
@registry.register
|
|
|
|
class AccessTokenFactory(factory.django.DjangoModelFactory):
|
|
|
|
application = factory.SubFactory(ApplicationFactory)
|
|
|
|
user = factory.SubFactory(UserFactory)
|
|
|
|
expires = factory.Faker("future_datetime", tzinfo=pytz.UTC)
|
|
|
|
token = factory.Faker("uuid4")
|
2020-04-01 12:34:56 +00:00
|
|
|
scope = "read"
|
2019-03-25 16:02:51 +00:00
|
|
|
|
|
|
|
class Meta:
|
|
|
|
model = "users.AccessToken"
|
|
|
|
|
|
|
|
|
|
|
|
@registry.register
|
|
|
|
class RefreshTokenFactory(factory.django.DjangoModelFactory):
|
|
|
|
application = factory.SubFactory(ApplicationFactory)
|
|
|
|
user = factory.SubFactory(UserFactory)
|
|
|
|
token = factory.Faker("uuid4")
|
|
|
|
|
|
|
|
class Meta:
|
|
|
|
model = "users.RefreshToken"
|