<commit_before># -*- coding: utf-8 -*-
from __future__ import unicode_literals

from django.core.management import call_command, CommandError
from django.contrib.auth.models import User
from six import StringIO
from django_extensions.management.commands.set_fake_passwords import Command, DEFAULT_FAKE_PASSWORD

import pytest


@pytest.fixture(scope='module')  # noqa
def django_db_setup(django_db_setup, django_db_blocker):  # noqa
    """Load to database a set of users, create for export
    emails command testing"""
    with django_db_blocker.unblock():  # noqa
        call_command('loaddata', 'group.json')
        call_command('loaddata', 'user.json')


@pytest.mark.django_db()
def test_without_args(capsys, settings):
    settings.DEBUG = True

    old_passwords = User.objects.values_list('password', flat=True).order_by('id')
    assert len(set(old_passwords)) == 3

    generate_password = Command()
    generate_password.run_from_argv(['manage.py', 'set_fake_passwords'])
    out, err = capsys.readouterr()
    assert 'Reset 3 passwords' in out

    new_passwords = User.objects.values_list('password', flat=True).order_by('id')
    assert len(set(new_passwords)) == 1
    assert old_passwords != new_passwords

    for user in User.objects.all():
        assert user.check_password(DEFAULT_FAKE_PASSWORD)


@pytest.mark.django_db()
def test_with_password(capsys, settings):
    settings.DEBUG = True

    generate_password = Command()
    generate_password.run_from_argv(['manage.py', 'set_fake_passwords', '--password=<PASSWORD>'])
    out, err = capsys.readouterr()
    assert 'Reset 3 passwords' in out

    for user in User.objects.all():
        assert user.check_password("<PASSWORD>")


def test_without_debug(settings):
    settings.DEBUG = False

    out = StringIO()
    with pytest.raises(CommandError, message="Only available in debug mod"):
        call_command('set_fake_passwords', verbosity=3, stdout=out, stderr=out)<commit_msg>Additional tests for set_fake_passwords command<commit_after># -*- coding: utf-8 -*-
from __future__ import unicode_literals

from django.core.management import call_command, CommandError
from django.contrib.auth.models import User
from six import StringIO
from django_extensions.management.commands.set_fake_passwords import Command, DEFAULT_FAKE_PASSWORD
from unittest.mock import patch, Mock

import pytest


@pytest.fixture(scope='module')  # noqa
def django_db_setup(django_db_setup, django_db_blocker):  # noqa
    """Load to database a set of users, create for export
    emails command testing"""
    with django_db_blocker.unblock():  # noqa
        call_command('loaddata', 'group.json')
        call_command('loaddata', 'user.json')


@pytest.mark.django_db()
def test_without_args(capsys, settings):
    settings.DEBUG = True

    old_passwords = User.objects.values_list('password', flat=True).order_by('id')
    assert len(set(old_passwords)) == 3

    generate_password = Command()
    generate_password.run_from_argv(['manage.py', 'set_fake_passwords'])
    out, err = capsys.readouterr()
    assert 'Reset 3 passwords' in out

    new_passwords = User.objects.values_list('password', flat=True).order_by('id')
    assert len(set(new_passwords)) == 1
    assert old_passwords != new_passwords

    for user in User.objects.all():
        assert user.check_password(DEFAULT_FAKE_PASSWORD)


@pytest.mark.django_db()
def test_with_password(capsys, settings):
    settings.DEBUG = True

    generate_password = Command()
    generate_password.run_from_argv(['manage.py', 'set_fake_passwords', '--password=<PASSWORD>'])
    out, err = capsys.readouterr()
    assert 'Reset 3 passwords' in out

    for user in User.objects.all():
        assert user.check_password("<PASSWORD>")


@pytest.mark.django_db()
def test_with_prompt(settings):
    settings.DEBUG = True

    m_getpass = Mock()
    m_getpass.getpass.return_value = 'test'

    with patch.dict('sys.modules', getpass=m_getpass):
        call_command('set_fake_passwords', '--prompt')

    assert all([user.check_password("<PASSWORD>") for user in User.objects.all()])


@pytest.mark.django_db()
def test_with_prompt_with_empty_password(settings):
    settings.DEBUG = True

    m_getpass = Mock()
    m_getpass.getpass.return_value = None

    with pytest.raises(CommandError, match='You must enter a valid password'):
        with patch.dict('sys.modules', getpass=m_getpass):
            call_command('set_fake_passwords', '--prompt')


def test_without_debug(settings):
    settings.DEBUG = False

    out = StringIO()
    with pytest.raises(CommandError, message="Only available in debug mod"):
        call_command('set_fake_passwords', verbosity=3, stdout=out, stderr=out)
{'fraction_non_alphanumeric': 0.11488638776810363, 'fraction_numerical': 0.002548311743469951, 'mean_word_length': 4.515222482435597, 'pattern_counts': {'":': 0, '<': 8, '<?xml version=': 0, '>': 8, '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': '7054484', 'n_tokens_mistral': 1501, 'n_tokens_neox': 1463, 'n_words': 322}