<s> import re <EOL> from django . forms import ValidationError <EOL> from nose . tools import eq_ <EOL> from pyquery import PyQuery as pq <EOL> from kitsune . users . forms import ( <EOL> AuthenticationForm , ProfileForm , RegisterForm , SetPasswordForm , <EOL> ForgotUsernameForm , username_allowed ) <EOL> from kitsune . sumo . tests import TestCase <EOL> from kitsune . users . tests import TestCaseBase , UserFactory <EOL> from kitsune . users . validators import TwitterValidator <EOL> class AuthenticationFormTests ( TestCaseBase ) : <EOL> """<STR_LIT>""" <EOL> def setUp ( self ) : <EOL> self . active_user = UserFactory ( username = '<STR_LIT>' , is_active = True ) <EOL> self . inactive_user = UserFactory ( username = '<STR_LIT>' , is_active = False ) <EOL> def test_only_active ( self ) : <EOL> form = AuthenticationForm ( data = { '<STR_LIT:username>' : self . active_user . username , <EOL> '<STR_LIT:password>' : '<STR_LIT>' } ) <EOL> assert form . is_valid ( ) <EOL> form = AuthenticationForm ( data = { <EOL> '<STR_LIT:username>' : self . inactive_user . username , <EOL> '<STR_LIT:password>' : '<STR_LIT>' } ) <EOL> assert not form . is_valid ( ) <EOL> def test_allow_inactive ( self ) : <EOL> form = AuthenticationForm ( only_active = False , <EOL> data = { '<STR_LIT:username>' : self . active_user . username , <EOL> '<STR_LIT:password>' : '<STR_LIT>' } ) <EOL> assert form . is_valid ( ) <EOL> form = AuthenticationForm ( only_active = False , data = { <EOL> '<STR_LIT:username>' : self . inactive_user . username , <EOL> '<STR_LIT:password>' : '<STR_LIT>' } ) <EOL> assert form . is_valid ( ) <EOL> def test_at_in_username ( self ) : <EOL> u = UserFactory ( username = '<STR_LIT>' ) <EOL> form = AuthenticationForm ( data = { '<STR_LIT:username>' : u . username , <EOL> '<STR_LIT:password>' : '<STR_LIT>' } ) <EOL> assert form . is_valid ( ) <EOL> FACEBOOK_URLS = ( <EOL> ( '<STR_LIT>' , True ) , <EOL> ( '<STR_LIT>' , True ) , <EOL> ( '<STR_LIT>' , False ) , <EOL> ( '<STR_LIT>' , False ) , <EOL> ( '<STR_LIT>' , False ) , <EOL> ) <EOL> class ProfileFormTestCase ( TestCaseBase ) : <EOL> form = ProfileForm ( ) <EOL> def setUp ( self ) : <EOL> self . form . cleaned_data = { } <EOL> def test_facebook_pattern_attr ( self ) : <EOL> """<STR_LIT>""" <EOL> fragment = pq ( self . form . as_ul ( ) ) <EOL> facebook = fragment ( '<STR_LIT>' ) [ <NUM_LIT:0> ] <EOL> assert '<STR_LIT>' in facebook . attrib <EOL> pattern = re . compile ( facebook . attrib [ '<STR_LIT>' ] ) <EOL> for url , match in FACEBOOK_URLS : <EOL> eq_ ( bool ( pattern . match ( url ) ) , match ) <EOL> def test_clean_facebook ( self ) : <EOL> clean = self . form . clean_facebook <EOL> for url , match in FACEBOOK_URLS : <EOL> self . form . cleaned_data [ '<STR_LIT>' ] = url <EOL> if match : <EOL> clean ( ) <EOL> else : <EOL> self . assertRaises ( ValidationError , clean ) <EOL> class TwitterValidatorTestCase ( TestCase ) : <EOL> def setUp ( self ) : <EOL> def test_valid ( self ) : <EOL> TwitterValidator ( '<STR_LIT>' ) <EOL> def test_has_number ( self ) : <EOL> TwitterValidator ( '<STR_LIT>' ) <EOL> def test_has_letter_number_underscore ( self ) : <EOL> TwitterValidator ( '<STR_LIT>' ) <EOL> def test_has_slash ( self ) : <EOL> self . assertRaises ( ValidationError , lambda : TwitterValidator ( '<STR_LIT>' ) ) <EOL> def test_has_at_sign ( self ) : <EOL> self . assertRaises ( ValidationError , lambda : TwitterValidator ( '<STR_LIT>' ) ) <EOL> class RegisterFormTests ( TestCaseBase ) : <EOL> """<STR_LIT>""" <EOL> def test_common_password ( self ) : <EOL> form = RegisterForm ( { '<STR_LIT:username>' : '<STR_LIT>' , <EOL> '<STR_LIT:password>' : '<STR_LIT:password>' , <EOL> '<STR_LIT>' : '<STR_LIT:password>' , <EOL> '<STR_LIT:email>' : '<STR_LIT>' } ) <EOL> assert not form . is_valid ( ) <EOL> def test_strong_password ( self ) : <EOL> form = RegisterForm ( { '<STR_LIT:username>' : '<STR_LIT>' , <EOL> '<STR_LIT:password>' : '<STR_LIT>' , <EOL> '<STR_LIT>' : '<STR_LIT>' , <EOL> '<STR_LIT:email>' : '<STR_LIT>' } ) <EOL> assert form . is_valid ( ) <EOL> def test_bad_username ( self ) : <EOL> form = RegisterForm ( { '<STR_LIT:username>' : '<STR_LIT>' , <EOL> '<STR_LIT:password>' : '<STR_LIT>' , <EOL> '<STR_LIT>' : '<STR_LIT>' , <EOL> '<STR_LIT:email>' : '<STR_LIT>' } ) <EOL> assert not form . is_valid ( ) <EOL> form = RegisterForm ( { '<STR_LIT:username>' : '<STR_LIT>' , <EOL> '<STR_LIT:password>' : '<STR_LIT>' , <EOL> '<STR_LIT>' : '<STR_LIT>' , <EOL> '<STR_LIT:email>' : '<STR_LIT>' } ) <EOL> assert not form . is_valid ( ) <EOL> form = RegisterForm ( { '<STR_LIT:username>' : '<STR_LIT>' , <EOL> '<STR_LIT:password>' : '<STR_LIT>' , <EOL> '<STR_LIT>' : '<STR_LIT>' , <EOL> '<STR_LIT:email>' : '<STR_LIT>' } ) <EOL> assert not form . is_valid ( ) <EOL> form = RegisterForm ( { '<STR_LIT:username>' : '<STR_LIT>' , <EOL> '<STR_LIT:password>' : '<STR_LIT>' , <EOL> '<STR_LIT>' : '<STR_LIT>' , <EOL> '<STR_LIT:email>' : '<STR_LIT>' } ) <EOL> assert not form . is_valid ( ) <EOL> form = RegisterForm ( { '<STR_LIT:username>' : '<STR_LIT>' , <EOL> '<STR_LIT:password>' : '<STR_LIT>' , <EOL> '<STR_LIT>' : '<STR_LIT>' , <EOL> '<STR_LIT:email>' : '<STR_LIT>' } ) <EOL> assert form . is_valid ( ) <EOL> class SetPasswordFormTests ( TestCaseBase ) : <EOL> """<STR_LIT>""" <EOL> def test_common_password ( self ) : <EOL> form = SetPasswordForm ( None , data = { '<STR_LIT>' : '<STR_LIT:password>' , <EOL> '<STR_LIT>' : '<STR_LIT:password>' } ) <EOL> assert not form . is_valid ( ) <EOL> class PasswordChangeFormFormTests ( TestCaseBase ) : <EOL> """<STR_LIT>""" <EOL> def test_common_password ( self ) : <EOL> u = UserFactory ( ) <EOL> form = SetPasswordForm ( u , data = { '<STR_LIT>' : '<STR_LIT:password>' , <EOL> '<STR_LIT>' : '<STR_LIT:password>' , <EOL> '<STR_LIT>' : '<STR_LIT>' } ) <EOL> assert not form . is_valid ( ) <EOL> class ForgotUsernameFormTests ( TestCaseBase ) : <EOL> """<STR_LIT>""" <EOL> def test_email_doesnt_exist ( self ) : <EOL> """<STR_LIT>""" <EOL> form = ForgotUsernameForm ( { '<STR_LIT:email>' : '<STR_LIT>' } ) <EOL> assert not form . is_valid ( ) <EOL> def test_valid_email ( self ) : <EOL> """<STR_LIT>""" <EOL> u = UserFactory ( email = '<STR_LIT>' , is_active = True ) <EOL> form = ForgotUsernameForm ( { '<STR_LIT:email>' : u . email } ) <EOL> assert form . is_valid ( ) <EOL> class Testusername_allowed ( TestCase ) : <EOL> def test_good_names ( self ) : <EOL> data = [ <EOL> ( '<STR_LIT>' , True ) , <EOL> ( '<STR_LIT>' , True ) , <EOL> ( '<STR_LIT>' , False ) , <EOL> ] <EOL> for name , expected in data : <EOL> eq_ ( username_allowed ( name ) , expected ) </s>
