aboutsummaryrefslogtreecommitdiffstats
path: root/pyblackbird_cc/users/tests/factories.py
diff options
context:
space:
mode:
authorMatthew Lemon <y@yulqen.org>2024-05-13 17:26:25 +0100
committerMatthew Lemon <y@yulqen.org>2024-05-13 17:26:25 +0100
commitefbbd480ddc62e695123d31c31d233b0df5155bd (patch)
treebc2fb465edd5050d83c97f280b1aac8e023fe3e5 /pyblackbird_cc/users/tests/factories.py
After first pre-commit processing
Diffstat (limited to 'pyblackbird_cc/users/tests/factories.py')
-rw-r--r--pyblackbird_cc/users/tests/factories.py40
1 files changed, 40 insertions, 0 deletions
diff --git a/pyblackbird_cc/users/tests/factories.py b/pyblackbird_cc/users/tests/factories.py
new file mode 100644
index 0000000..5c83741
--- /dev/null
+++ b/pyblackbird_cc/users/tests/factories.py
@@ -0,0 +1,40 @@
+from collections.abc import Sequence
+from typing import Any
+
+from factory import Faker
+from factory import post_generation
+from factory.django import DjangoModelFactory
+
+from pyblackbird_cc.users.models import User
+
+
+class UserFactory(DjangoModelFactory):
+ email = Faker("email")
+ name = Faker("name")
+
+ @post_generation
+ def password(self, create: bool, extracted: Sequence[Any], **kwargs): # noqa: FBT001
+ password = (
+ extracted
+ if extracted
+ else Faker(
+ "password",
+ length=42,
+ special_chars=True,
+ digits=True,
+ upper_case=True,
+ lower_case=True,
+ ).evaluate(None, None, extra={"locale": None})
+ )
+ self.set_password(password)
+
+ @classmethod
+ def _after_postgeneration(cls, instance, create, results=None):
+ """Save again the instance if creating and at least one hook ran."""
+ if create and results and not cls._meta.skip_postgeneration_save:
+ # Some post-generation hooks ran, and may have modified us.
+ instance.save()
+
+ class Meta:
+ model = User
+ django_get_or_create = ["email"]