<s> from __future__ import unicode_literals <EOL> import os <EOL> from django . contrib . auth . models import User <EOL> from django . contrib . auth . password_validation import ( <EOL> CommonPasswordValidator , MinimumLengthValidator , NumericPasswordValidator , <EOL> UserAttributeSimilarityValidator , get_default_password_validators , <EOL> get_password_validators , password_changed , <EOL> password_validators_help_text_html , password_validators_help_texts , <EOL> validate_password , <EOL> ) <EOL> from django . core . exceptions import ValidationError <EOL> from django . test import TestCase , override_settings <EOL> from django . utils . _os import upath <EOL> @ override_settings ( AUTH_PASSWORD_VALIDATORS = [ <EOL> { '<STR_LIT>' : '<STR_LIT>' } , <EOL> { '<STR_LIT>' : '<STR_LIT>' , '<STR_LIT>' : { <EOL> '<STR_LIT>' : <NUM_LIT:12> , <EOL> } } , <EOL> ] ) <EOL> class PasswordValidationTest ( TestCase ) : <EOL> def test_get_default_password_validators ( self ) : <EOL> validators = get_default_password_validators ( ) <EOL> self . assertEqual ( len ( validators ) , <NUM_LIT:2> ) <EOL> self . assertEqual ( validators [ <NUM_LIT:0> ] . __class__ . __name__ , '<STR_LIT>' ) <EOL> self . assertEqual ( validators [ <NUM_LIT:1> ] . __class__ . __name__ , '<STR_LIT>' ) <EOL> self . assertEqual ( validators [ <NUM_LIT:1> ] . min_length , <NUM_LIT:12> ) <EOL> def test_get_password_validators_custom ( self ) : <EOL> validator_config = [ { '<STR_LIT>' : '<STR_LIT>' } ] <EOL> validators = get_password_validators ( validator_config ) <EOL> self . assertEqual ( len ( validators ) , <NUM_LIT:1> ) <EOL> self . assertEqual ( validators [ <NUM_LIT:0> ] . __class__ . __name__ , '<STR_LIT>' ) <EOL> self . assertEqual ( get_password_validators ( [ ] ) , [ ] ) <EOL> def test_validate_password ( self ) : <EOL> self . assertIsNone ( validate_password ( '<STR_LIT>' ) ) <EOL> msg_too_short = '<STR_LIT>' <EOL> with self . assertRaises ( ValidationError ) as cm : <EOL> validate_password ( '<STR_LIT>' ) <EOL> self . assertEqual ( cm . exception . messages , [ msg_too_short ] ) <EOL> self . assertEqual ( cm . exception . error_list [ <NUM_LIT:0> ] . code , '<STR_LIT>' ) <EOL> with self . assertRaises ( ValidationError ) as cm : <EOL> validate_password ( '<STR_LIT:password>' ) <EOL> self . assertEqual ( cm . exception . messages , [ '<STR_LIT>' , msg_too_short ] ) <EOL> self . assertEqual ( cm . exception . error_list [ <NUM_LIT:0> ] . code , '<STR_LIT>' ) <EOL> self . assertIsNone ( validate_password ( '<STR_LIT:password>' , password_validators = [ ] ) ) <EOL> def test_password_changed ( self ) : <EOL> self . assertIsNone ( password_changed ( '<STR_LIT:password>' ) ) <EOL> def test_password_validators_help_texts ( self ) : <EOL> help_texts = password_validators_help_texts ( ) <EOL> self . assertEqual ( len ( help_texts ) , <NUM_LIT:2> ) <EOL> self . assertIn ( '<STR_LIT>' , help_texts [ <NUM_LIT:1> ] ) <EOL> self . assertEqual ( password_validators_help_texts ( password_validators = [ ] ) , [ ] ) <EOL> def test_password_validators_help_text_html ( self ) : <EOL> help_text = password_validators_help_text_html ( ) <EOL> self . assertEqual ( help_text . count ( '<STR_LIT>' ) , <NUM_LIT:2> ) <EOL> self . assertIn ( '<STR_LIT>' , help_text ) <EOL> @ override_settings ( AUTH_PASSWORD_VALIDATORS = [ ] ) <EOL> def test_empty_password_validator_help_text_html ( self ) : <EOL> self . assertEqual ( password_validators_help_text_html ( ) , '<STR_LIT>' ) <EOL> class MinimumLengthValidatorTest ( TestCase ) : <EOL> def test_validate ( self ) : <EOL> expected_error = "<STR_LIT>" <EOL> self . assertIsNone ( MinimumLengthValidator ( ) . validate ( '<STR_LIT>' ) ) <EOL> self . assertIsNone ( MinimumLengthValidator ( min_length = <NUM_LIT:3> ) . validate ( '<STR_LIT>' ) ) <EOL> with self . assertRaises ( ValidationError ) as cm : <EOL> MinimumLengthValidator ( ) . validate ( '<STR_LIT>' ) <EOL> self . assertEqual ( cm . exception . messages , [ expected_error % <NUM_LIT:8> ] ) <EOL> self . assertEqual ( cm . exception . error_list [ <NUM_LIT:0> ] . code , '<STR_LIT>' ) <EOL> with self . assertRaises ( ValidationError ) as cm : <EOL> MinimumLengthValidator ( min_length = <NUM_LIT:3> ) . validate ( '<STR_LIT>' ) <EOL> self . assertEqual ( cm . exception . messages , [ expected_error % <NUM_LIT:3> ] ) <EOL> def test_help_text ( self ) : <EOL> self . assertEqual ( <EOL> MinimumLengthValidator ( ) . get_help_text ( ) , <EOL> "<STR_LIT>" <EOL> ) <EOL> class UserAttributeSimilarityValidatorTest ( TestCase ) : <EOL> def test_validate ( self ) : <EOL> user = User . objects . create_user ( <EOL> username = '<STR_LIT>' , password = '<STR_LIT:password>' , email = '<STR_LIT>' , <EOL> first_name = '<STR_LIT>' , last_name = '<STR_LIT>' , <EOL> ) <EOL> expected_error = "<STR_LIT>" <EOL> self . assertIsNone ( UserAttributeSimilarityValidator ( ) . validate ( '<STR_LIT>' ) ) <EOL> with self . assertRaises ( ValidationError ) as cm : <EOL> UserAttributeSimilarityValidator ( ) . validate ( '<STR_LIT>' , user = user ) , <EOL> self . assertEqual ( cm . exception . messages , [ expected_error % "<STR_LIT:username>" ] ) <EOL> self . assertEqual ( cm . exception . error_list [ <NUM_LIT:0> ] . code , '<STR_LIT>' ) <EOL> with self . assertRaises ( ValidationError ) as cm : <EOL> UserAttributeSimilarityValidator ( ) . validate ( '<STR_LIT>' , user = user ) , <EOL> self . assertEqual ( cm . exception . messages , [ expected_error % "<STR_LIT>" ] ) <EOL> with self . assertRaises ( ValidationError ) as cm : <EOL> UserAttributeSimilarityValidator ( <EOL> user_attributes = [ '<STR_LIT>' ] , <EOL> max_similarity = <NUM_LIT> , <EOL> ) . validate ( '<STR_LIT>' , user = user ) <EOL> self . assertEqual ( cm . exception . messages , [ expected_error % "<STR_LIT>" ] ) <EOL> self . assertIsNone ( <EOL> UserAttributeSimilarityValidator ( user_attributes = [ '<STR_LIT>' ] ) . validate ( '<STR_LIT>' , user = user ) <EOL> ) <EOL> def test_help_text ( self ) : <EOL> self . assertEqual ( <EOL> UserAttributeSimilarityValidator ( ) . get_help_text ( ) , <EOL> "<STR_LIT>" <EOL> ) <EOL> class CommonPasswordValidatorTest ( TestCase ) : <EOL> def test_validate ( self ) : <EOL> expected_error = "<STR_LIT>" <EOL> self . assertIsNone ( CommonPasswordValidator ( ) . validate ( '<STR_LIT>' ) ) <EOL> with self . assertRaises ( ValidationError ) as cm : <EOL> CommonPasswordValidator ( ) . validate ( '<STR_LIT>' ) <EOL> self . assertEqual ( cm . exception . messages , [ expected_error ] ) <EOL> def test_validate_custom_list ( self ) : <EOL> path = os . path . join ( os . path . dirname ( os . path . realpath ( upath ( __file__ ) ) ) , '<STR_LIT>' ) <EOL> validator = CommonPasswordValidator ( password_list_path = path ) <EOL> expected_error = "<STR_LIT>" <EOL> self . assertIsNone ( validator . validate ( '<STR_LIT>' ) ) <EOL> with self . assertRaises ( ValidationError ) as cm : <EOL> validator . validate ( '<STR_LIT>' ) <EOL> self . assertEqual ( cm . exception . messages , [ expected_error ] ) <EOL> self . assertEqual ( cm . exception . error_list [ <NUM_LIT:0> ] . code , '<STR_LIT>' ) <EOL> def test_help_text ( self ) : <EOL> self . assertEqual ( <EOL> CommonPasswordValidator ( ) . get_help_text ( ) , <EOL> "<STR_LIT>" <EOL> ) <EOL> class NumericPasswordValidatorTest ( TestCase ) : <EOL> def test_validate ( self ) : <EOL> expected_error = "<STR_LIT>" <EOL> self . assertIsNone ( NumericPasswordValidator ( ) . validate ( '<STR_LIT>' ) ) <EOL> with self . assertRaises ( ValidationError ) as cm : <EOL> NumericPasswordValidator ( ) . validate ( '<STR_LIT>' ) <EOL> self . assertEqual ( cm . exception . messages , [ expected_error ] ) <EOL> self . assertEqual ( cm . exception . error_list [ <NUM_LIT:0> ] . code , '<STR_LIT>' ) <EOL> def test_help_text ( self ) : <EOL> self . assertEqual ( <EOL> NumericPasswordValidator ( ) . get_help_text ( ) , <EOL> "<STR_LIT>" <EOL> ) </s>
