<commit_before>from django.core.exceptions import ValidationError
from django.test import TestCase, override_settings

from corehq.apps.domain.models import Domain
from corehq.apps.users.dbaccessors.all_commcare_users import delete_all_users
from corehq.apps.users.models import WebUser
from custom.nic_compliance.password_validation import UsedPasswordValidator
from custom.nic_compliance.const import RESTRICT_USED_PASSWORDS_NUM


class TestUsedPasswordsRestriction(TestCase):
    def setUp(self):
        delete_all_users()
        self.domain = Domain.get_or_create_with_name('qwerty', is_active=True)
        self.username = '<EMAIL>'
        self.password = '<PASSWORD>'
        self.user = WebUser.create(self.domain.name, self.username, self.password).get_django_user()

    def tearDown(self):
        self.user.delete()
        self.domain.delete()

    def test_used_password_reset(self):
        # fails for reuse of password
        with override_settings(AUTH_PASSWORD_VALIDATORS=[{
            'NAME': 'custom.nic_compliance.password_validation.UsedPasswordValidator',
        }]):
            with self.assertRaisesMessage(
                    ValidationError,
                    "Your password can not be same as last {num} passwords.".format(num=RESTRICT_USED_PASSWORDS_NUM)):
                UsedPasswordValidator().validate(self.password, self.user)
                # set as same password
                self.user._password = <PASSWORD>.password
                self.user.save()

            # successful for a password never used<commit_msg>remove redundant delete_all_users<commit_after>from django.core.exceptions import ValidationError
from django.test import TestCase, override_settings

from corehq.apps.domain.models import Domain
from corehq.apps.users.models import WebUser
from custom.nic_compliance.password_validation import UsedPasswordValidator
from custom.nic_compliance.const import RESTRICT_USED_PASSWORDS_NUM


class TestUsedPasswordsRestriction(TestCase):
    def setUp(self):
        self.domain = Domain.get_or_create_with_name('qwerty', is_active=True)
        self.username = '<EMAIL>.commcarehq.org'
        self.password = '<PASSWORD>'
        self.user = WebUser.create(self.domain.name, self.username, self.password).get_django_user()

    def tearDown(self):
        self.user.delete()
        self.domain.delete()

    def test_used_password_reset(self):
        # fails for reuse of password
        with override_settings(AUTH_PASSWORD_VALIDATORS=[{
            'NAME': 'custom.nic_compliance.password_validation.UsedPasswordValidator',
        }]):
            with self.assertRaisesMessage(
                    ValidationError,
                    "Your password can not be same as last {num} passwords.".format(num=RESTRICT_USED_PASSWORDS_NUM)):
                UsedPasswordValidator().validate(self.password, self.user)
                # set as same password
                self.user._password = <PASSWORD>
                self.user.save()

            # successful for a password never used
{'fraction_non_alphanumeric': 0.08363397835355854, 'fraction_numerical': 0.0, 'mean_word_length': 3.0829986613119145, 'pattern_counts': {'":': 0, '<': 9, '<?xml version=': 0, '>': 9, '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': '2977268', 'n_tokens_mistral': 857, 'n_tokens_neox': 802, 'n_words': 172}