Skip to content
This repository
tree: 8836eccd29
Fetching contributors…

Cannot retrieve contributors at this time

file 78 lines (65 sloc) 3.112 kb
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78
# -*- coding: utf-8 -*-
import mockups
from datetime import datetime
from django.contrib.auth.models import User, UNUSABLE_PASSWORD
from mockups import Mockup, Factory
from mockups import generators


class UserFactory(Factory):
    username = generators.UUIDGenerator(max_length=30)
    first_name = generators.LoremWordGenerator(1)
    last_name = generators.LoremWordGenerator(1)
    password = generators.StaticGenerator(UNUSABLE_PASSWORD)
    is_active = generators.StaticGenerator(True)
    # don't generate admin users
    is_staff = generators.StaticGenerator(False)
    is_superuser = generators.StaticGenerator(False)
    date_joined = generators.DateTimeGenerator(max_date=datetime.now())
    last_login = generators.DateTimeGenerator(max_date=datetime.now())


class UserMockup(Mockup):
    '''
:class:`UserMockup` is automatically used by default to create new
``User`` instances. It uses the following values to assure that you can
use the generated instances without any modification:

* ``username`` only contains chars that are allowed by django's auth forms.
* ``email`` is unique.
* ``first_name`` and ``last_name`` are single, random words of the lorem
ipsum text.
* ``is_staff`` and ``is_superuser`` are always ``False``.
* ``is_active`` is always ``True``.
* ``date_joined`` and ``last_login`` are always in the past and it is
assured that ``date_joined`` will be lower than ``last_login``.
'''

    # don't follow permissions and groups
    follow_m2m = False
    factory = UserFactory

    def __init__(self, *args, **kwargs):
        '''
By default the password is set to an unusable value, this makes it
impossible to login with the generated users. If you want to use for
example ``mockups.create_one('auth.User')`` in your unittests to have
a user instance which you can use to login with the testing client you
can provide a ``username`` and a ``password`` argument. Then you can do
something like::

mockups.create_one('auth.User', username='foo', password='bar`)
self.client.login(username='foo', password='bar')
'''
        self.username = kwargs.pop('username', None)
        self.password = kwargs.pop('password', None)
        super(UserMockup, self).__init__(*args, **kwargs)
        if self.username:
            self.update_fieldname_generator(
                username = generators.StaticGenerator(self.username)
                )

    def unique_email(self, model, instance):
        if User.objects.filter(email=instance.email):
            raise mockups.InvalidConstraint(('email',))

    def prepare_class(self):
        self.add_constraint(self.unique_email)

    def post_process_instance(self, instance):
        # make sure user's last login was not before he joined
        if instance.last_login < instance.date_joined:
            instance.last_login = instance.date_joined
        if self.password:
            instance.set_password(self.password)
        return instance


mockups.register(User, UserMockup, fail_silently=True)
Something went wrong with that request. Please try again.