<NME> factories.py
<BEF> """Factories for core application."""

from . import models


class GroupFactory(factory.DjangoModelFactory):
    FACTORY_FOR = Group

    name = 'DefaultGroup'


class UserFactory(factory.DjangoModelFactory):
    FACTORY_FOR = models.User

    email = factory.LazyAttribute(lambda a: a.username)
    password = '{PLAIN}toto'

    # mailbox = factory.RelatedFactory(
    #     MailboxFactory, 'user',
    #     address=factory.LazyAttribute(
    #         lambda m: m.user.email.replace('@%s' % m.domain.name, '')
    #     )
    # )

    @factory.post_generation
    def groups(self, create, extracted, **kwargs):
        if not create:
            # Simple build, do nothing.
            return

        if extracted:
            # A list of groups were passed in, use them
            for group in extracted:
                self.groups.add(Group.objects.get(name=group))
    """A factory to create User instances."""

    class Meta(object):
        model = models.User
        django_get_or_create = ("username", )

    email = factory.LazyAttribute(lambda a: a.username)
    password = "{PLAIN}toto"

    @factory.post_generation
    def groups(self, create, extracted, **kwargs):
        if not create:
            return

        if extracted:
            for group in extracted:
                self.groups.add(Group.objects.get(name=group))


class LogFactory(factory.django.DjangoModelFactory):
    """Factory for Log."""

    class Meta(object):
        model = models.Log

    logger = "modoboa.admin"
    message = "A message"
    level = "info"

<MSG> Fixing unit tests.

<DFF> @@ -3,32 +3,33 @@ from django.contrib.auth.models import Group
 from . import models
 
 
+class PermissionFactory(factory.DjangoModelFactory):
+    ABSTRACT_FACTORY = True
+
+    @factory.post_generation
+    def set_permission(self, create, extracted, **kwargs):
+        if not create:
+            return
+        self.post_create(models.User.objects.get(pk=1))
+
+
 class GroupFactory(factory.DjangoModelFactory):
     FACTORY_FOR = Group
 
     name = 'DefaultGroup'
 
 
-class UserFactory(factory.DjangoModelFactory):
+class UserFactory(PermissionFactory):
     FACTORY_FOR = models.User
 
     email = factory.LazyAttribute(lambda a: a.username)
     password = '{PLAIN}toto'
 
-    # mailbox = factory.RelatedFactory(
-    #     MailboxFactory, 'user',
-    #     address=factory.LazyAttribute(
-    #         lambda m: m.user.email.replace('@%s' % m.domain.name, '')
-    #     )
-    # )
-
     @factory.post_generation
     def groups(self, create, extracted, **kwargs):
         if not create:
-            # Simple build, do nothing.
             return
 
         if extracted:
-            # A list of groups were passed in, use them
             for group in extracted:
                 self.groups.add(Group.objects.get(name=group))

Fixing unit tests.