<commit_before>from django.contrib.auth.models import User, Group

from candidates.models import (
    TRUSTED_TO_MERGE_GROUP_NAME,
    TRUSTED_TO_LOCK_GROUP_NAME,
    TRUSTED_TO_RENAME_GROUP_NAME,
    RESULT_RECORDERS_GROUP_NAME,
)
from official_documents.models import DOCUMENT_UPLOADERS_GROUP_NAME

class TestUserMixin(object):

    @classmethod
    def setUpClass(cls):
        cls.user = User.objects.create_user(
            'john',
            '<EMAIL>',
            '<PASSWORD>',
        )
        cls.user_who_can_merge = User.objects.create_user(
            'alice',
            '<EMAIL>',
            '<PASSWORD>',
        )
        cls.user_who_can_lock = User.objects.create_user(
            'charles',
            '<EMAIL>',
            '<PASSWORD>',
        )
        cls.user_who_can_upload_documents = User.objects.create_user(
            'delilah',
            '<EMAIL>',
            '<PASSWORD>',
        )
        cls.user_who_can_rename = User.objects.create_user(
            'ermintrude',
            '<EMAIL>',
            '<PASSWORD>',
        )
        cls.user_who_can_record_results = User.objects.create_user(
            'frankie',
            '<EMAIL>',
            '<PASSWORD>',
        )
        merger_group = Group.objects.get(name=TRUSTED_TO_MERGE_GROUP_NAME)
        merger_group.user_set.add(cls.user_who_can_merge)
        locker_group = Group.objects.get(name=TRUSTED_TO_LOCK_GROUP_NAME)
        locker_group.user_set.add(cls.user_who_can_lock)
        uploader_group = Group.objects.get(name=DOCUMENT_UPLOADERS_GROUP_NAME)
        uploader_group.user_set.add(cls.user_who_can_upload_documents)
        renamer_group = Group.objects.get(name=TRUSTED_TO_RENAME_GROUP_NAME)
        renamer_group.user_set.add(cls.user_who_can_rename)
        recorder_group = Group.objects.get(name=RESULT_RECORDERS_GROUP_NAME)
        recorder_group.user_set.add(cls.user_who_can_record_results)
        for u in (
                cls.user,
                cls.user_who_can_merge,
                cls.user_who_can_lock,
                cls.user_who_can_upload_documents,
                cls.user_who_can_rename
        ):
            terms = u.terms_agreement
            terms.assigned_to_dc = True
            terms.save()
        cls.user_refused = User.objects.create_user(
            'johnrefused',
            '<EMAIL>',
            '<PASSWORD>',
        )

    @classmethod
    def tearDownClass(cls):
        cls.user_refused.delete()
        cls.user_who_can_rename.delete()
        cls.user_who_can_upload_documents.delete()
        cls.user_who_can_lock.delete()
        cls.user_who_can_merge.delete()
        cls.user.delete()<commit_msg>Refactor test user creation<commit_after>from django.contrib.auth.models import User, Group

from candidates.models import (
    TRUSTED_TO_MERGE_GROUP_NAME,
    TRUSTED_TO_LOCK_GROUP_NAME,
    TRUSTED_TO_RENAME_GROUP_NAME,
    RESULT_RECORDERS_GROUP_NAME,
)
from official_documents.models import DOCUMENT_UPLOADERS_GROUP_NAME

class TestUserMixin(object):

    @classmethod
    def setUpClass(cls):
        cls.users_to_delete = []
        for username, attr, group_names in (
                ('john', 'user', []),
                ('alice', 'user_who_can_merge', [TRUSTED_TO_MERGE_GROUP_NAME]),
                ('charles', 'user_who_can_lock', [TRUSTED_TO_LOCK_GROUP_NAME]),
                ('delilah', 'user_who_can_upload_documents', [DOCUMENT_UPLOADERS_GROUP_NAME]),
                ('ermintrude', 'user_who_can_rename', [TRUSTED_TO_RENAME_GROUP_NAME]),
                ('frankie', 'user_who_can_record_results', [RESULT_RECORDERS_GROUP_NAME]),
        ):
            u = User.objects.create_user(
                username,
                username + '@example.com',
                'notagoodpassword',
            )
            terms = u.terms_agreement
            terms.assigned_to_dc = True
            terms.save()
            for group_name in group_names:
                group = Group.objects.get(name=group_name)
                group.user_set.add(u)
            setattr(cls, attr, u)
            cls.users_to_delete.append(u)
        # Also add a user who hasn't accepted the terms, and isn't in
        # any groups:
        cls.user_refused = User.objects.create_user(
            'johnrefused',
            '<EMAIL>',
            'notagoodpasswordeither',
        )
        cls.users_to_delete.append(cls.user_refused)

    @classmethod
    def tearDownClass(cls):
        for u in cls.users_to_delete:
            u.delete()
{'fraction_non_alphanumeric': 0.09559637087851294, 'fraction_numerical': 0.0, 'mean_word_length': 2.1920903954802258, 'pattern_counts': {'":': 0, '<': 18, '<?xml version=': 0, '>': 18, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '2452693', 'n_tokens_mistral': 1457, 'n_tokens_neox': 1451, 'n_words': 189}