<s> import os <EOL> import re <EOL> from django . conf import settings <EOL> from django . contrib . auth import SESSION_KEY <EOL> from django . contrib . auth . forms import AuthenticationForm <EOL> from django . contrib . sites . models import Site , RequestSite <EOL> from django . contrib . auth . models import User <EOL> from django . test import TestCase <EOL> from django . core import mail <EOL> from django . core . urlresolvers import reverse <EOL> class AuthViewsTestCase ( TestCase ) : <EOL> """<STR_LIT>""" <EOL> fixtures = [ '<STR_LIT>' ] <EOL> urls = '<STR_LIT>' <EOL> def setUp ( self ) : <EOL> self . old_LANGUAGES = settings . LANGUAGES <EOL> self . old_LANGUAGE_CODE = settings . LANGUAGE_CODE <EOL> settings . LANGUAGES = ( ( '<STR_LIT>' , '<STR_LIT>' ) , ) <EOL> settings . LANGUAGE_CODE = '<STR_LIT>' <EOL> self . old_TEMPLATE_DIRS = settings . TEMPLATE_DIRS <EOL> settings . TEMPLATE_DIRS = ( <EOL> os . path . join ( <EOL> os . path . dirname ( __file__ ) , <EOL> '<STR_LIT>' <EOL> ) <EOL> , ) <EOL> def tearDown ( self ) : <EOL> settings . LANGUAGES = self . old_LANGUAGES <EOL> settings . LANGUAGE_CODE = self . old_LANGUAGE_CODE <EOL> settings . TEMPLATE_DIRS = self . old_TEMPLATE_DIRS <EOL> class PasswordResetTest ( AuthViewsTestCase ) : <EOL> def setUp ( self ) : <EOL> self . old_LANGUAGES = settings . LANGUAGES <EOL> self . old_LANGUAGE_CODE = settings . LANGUAGE_CODE <EOL> settings . LANGUAGES = ( ( '<STR_LIT>' , '<STR_LIT>' ) , ) <EOL> settings . LANGUAGE_CODE = '<STR_LIT>' <EOL> def tearDown ( self ) : <EOL> settings . LANGUAGES = self . old_LANGUAGES <EOL> settings . LANGUAGE_CODE = self . old_LANGUAGE_CODE <EOL> def test_email_not_found ( self ) : <EOL> "<STR_LIT>" <EOL> response = self . client . get ( '<STR_LIT>' ) <EOL> self . assertEquals ( response . status_code , <NUM_LIT:200> ) <EOL> response = self . client . post ( '<STR_LIT>' , { '<STR_LIT:email>' : '<STR_LIT>' } ) <EOL> self . assertContains ( response , "<STR_LIT>" ) <EOL> self . assertEquals ( len ( mail . outbox ) , <NUM_LIT:0> ) <EOL> def test_email_found ( self ) : <EOL> "<STR_LIT>" <EOL> response = self . client . post ( '<STR_LIT>' , { '<STR_LIT:email>' : '<STR_LIT>' } ) <EOL> self . assertEquals ( response . status_code , <NUM_LIT> ) <EOL> self . assertEquals ( len ( mail . outbox ) , <NUM_LIT:1> ) <EOL> self . assert_ ( "<STR_LIT>" in mail . outbox [ <NUM_LIT:0> ] . body ) <EOL> def _test_confirm_start ( self ) : <EOL> response = self . client . post ( '<STR_LIT>' , { '<STR_LIT:email>' : '<STR_LIT>' } ) <EOL> self . assertEquals ( response . status_code , <NUM_LIT> ) <EOL> self . assertEquals ( len ( mail . outbox ) , <NUM_LIT:1> ) <EOL> return self . _read_signup_email ( mail . outbox [ <NUM_LIT:0> ] ) <EOL> def _read_signup_email ( self , email ) : <EOL> urlmatch = re . search ( r"<STR_LIT>" , email . body ) <EOL> self . assert_ ( urlmatch is not None , "<STR_LIT>" ) <EOL> return urlmatch . group ( ) , urlmatch . groups ( ) [ <NUM_LIT:0> ] <EOL> def test_confirm_valid ( self ) : <EOL> url , path = self . _test_confirm_start ( ) <EOL> response = self . client . get ( path ) <EOL> self . assertEquals ( response . status_code , <NUM_LIT:200> ) <EOL> self . assert_ ( "<STR_LIT>" in response . content ) <EOL> def test_confirm_invalid ( self ) : <EOL> url , path = self . _test_confirm_start ( ) <EOL> path = path [ : - <NUM_LIT:5> ] + ( "<STR_LIT:0>" * <NUM_LIT:4> ) + path [ - <NUM_LIT:1> ] <EOL> response = self . client . get ( path ) <EOL> self . assertEquals ( response . status_code , <NUM_LIT:200> ) <EOL> self . assert_ ( "<STR_LIT>" in response . content ) <EOL> def test_confirm_invalid_post ( self ) : <EOL> url , path = self . _test_confirm_start ( ) <EOL> path = path [ : - <NUM_LIT:5> ] + ( "<STR_LIT:0>" * <NUM_LIT:4> ) + path [ - <NUM_LIT:1> ] <EOL> response = self . client . post ( path , { '<STR_LIT>' : '<STR_LIT>' , <EOL> '<STR_LIT>' : '<STR_LIT>' } ) <EOL> u = User . objects . get ( email = '<STR_LIT>' ) <EOL> self . assert_ ( not u . check_password ( "<STR_LIT>" ) ) <EOL> def test_confirm_complete ( self ) : <EOL> url , path = self . _test_confirm_start ( ) <EOL> response = self . client . post ( path , { '<STR_LIT>' : '<STR_LIT>' , <EOL> '<STR_LIT>' : '<STR_LIT>' } ) <EOL> self . assertEquals ( response . status_code , <NUM_LIT> ) <EOL> u = User . objects . get ( email = '<STR_LIT>' ) <EOL> self . assert_ ( u . check_password ( "<STR_LIT>" ) ) <EOL> response = self . client . get ( path ) <EOL> self . assertEquals ( response . status_code , <NUM_LIT:200> ) <EOL> self . assert_ ( "<STR_LIT>" in response . content ) <EOL> def test_confirm_different_passwords ( self ) : <EOL> url , path = self . _test_confirm_start ( ) <EOL> response = self . client . post ( path , { '<STR_LIT>' : '<STR_LIT>' , <EOL> '<STR_LIT>' : '<STR_LIT>' } ) <EOL> self . assertEquals ( response . status_code , <NUM_LIT:200> ) <EOL> self . assert_ ( "<STR_LIT>" in response . content ) <EOL> class ChangePasswordTest ( AuthViewsTestCase ) : <EOL> def login ( self , password = '<STR_LIT:password>' ) : <EOL> response = self . client . post ( '<STR_LIT>' , { <EOL> '<STR_LIT:username>' : '<STR_LIT>' , <EOL> '<STR_LIT:password>' : password <EOL> } <EOL> ) <EOL> self . assertEquals ( response . status_code , <NUM_LIT> ) <EOL> self . assert_ ( response [ '<STR_LIT>' ] . endswith ( settings . LOGIN_REDIRECT_URL ) ) <EOL> def fail_login ( self , password = '<STR_LIT:password>' ) : <EOL> response = self . client . post ( '<STR_LIT>' , { <EOL> '<STR_LIT:username>' : '<STR_LIT>' , <EOL> '<STR_LIT:password>' : password <EOL> } <EOL> ) <EOL> self . assertEquals ( response . status_code , <NUM_LIT:200> ) <EOL> self . assert_ ( "<STR_LIT>" in response . content ) <EOL> def logout ( self ) : <EOL> response = self . client . get ( '<STR_LIT>' ) <EOL> def test_password_change_fails_with_invalid_old_password ( self ) : <EOL> self . login ( ) <EOL> response = self . client . post ( '<STR_LIT>' , { <EOL> '<STR_LIT>' : '<STR_LIT>' , <EOL> '<STR_LIT>' : '<STR_LIT>' , <EOL> '<STR_LIT>' : '<STR_LIT>' , <EOL> } <EOL> ) <EOL> self . assertEquals ( response . status_code , <NUM_LIT:200> ) <EOL> self . assert_ ( "<STR_LIT>" in response . content ) <EOL> def test_password_change_fails_with_mismatched_passwords ( self ) : <EOL> self . login ( ) <EOL> response = self . client . post ( '<STR_LIT>' , { <EOL> '<STR_LIT>' : '<STR_LIT:password>' , <EOL> '<STR_LIT>' : '<STR_LIT>' , <EOL> '<STR_LIT>' : '<STR_LIT>' , <EOL> } <EOL> ) <EOL> self . assertEquals ( response . status_code , <NUM_LIT:200> ) <EOL> self . assert_ ( "<STR_LIT>" in response . content ) <EOL> def test_password_change_succeeds ( self ) : <EOL> self . login ( ) <EOL> response = self . client . post ( '<STR_LIT>' , { <EOL> '<STR_LIT>' : '<STR_LIT:password>' , <EOL> '<STR_LIT>' : '<STR_LIT>' , <EOL> '<STR_LIT>' : '<STR_LIT>' , <EOL> } <EOL> ) <EOL> self . assertEquals ( response . status_code , <NUM_LIT> ) <EOL> self . assert_ ( response [ '<STR_LIT>' ] . endswith ( '<STR_LIT>' ) ) <EOL> self . fail_login ( ) <EOL> self . login ( password = '<STR_LIT>' ) <EOL> class LoginTest ( AuthViewsTestCase ) : <EOL> def test_current_site_in_context_after_login ( self ) : <EOL> response = self . client . get ( reverse ( '<STR_LIT>' ) ) <EOL> self . assertEquals ( response . status_code , <NUM_LIT:200> ) <EOL> site = Site . objects . get_current ( ) <EOL> self . assertEquals ( response . context [ '<STR_LIT>' ] , site ) <EOL> self . assertEquals ( response . context [ '<STR_LIT>' ] , site . name ) <EOL> self . assert_ ( isinstance ( response . context [ '<STR_LIT>' ] , AuthenticationForm ) , <EOL> '<STR_LIT>' ) <EOL> class LogoutTest ( AuthViewsTestCase ) : <EOL> urls = '<STR_LIT>' <EOL> def login ( self , password = '<STR_LIT:password>' ) : <EOL> response = self . client . post ( '<STR_LIT>' , { <EOL> '<STR_LIT:username>' : '<STR_LIT>' , <EOL> '<STR_LIT:password>' : password <EOL> } <EOL> ) <EOL> self . assertEquals ( response . status_code , <NUM_LIT> ) <EOL> self . assert_ ( response [ '<STR_LIT>' ] . endswith ( settings . LOGIN_REDIRECT_URL ) ) <EOL> self . assert_ ( SESSION_KEY in self . client . session ) <EOL> def confirm_logged_out ( self ) : <EOL> self . assert_ ( SESSION_KEY not in self . client . session ) <EOL> def test_logout_default ( self ) : <EOL> "<STR_LIT>" <EOL> self . login ( ) <EOL> response = self . client . get ( '<STR_LIT>' ) <EOL> self . assertEquals ( <NUM_LIT:200> , response . status_code ) <EOL> self . assert_ ( '<STR_LIT>' in response . content ) <EOL> self . confirm_logged_out ( ) <EOL> def test_logout_with_next_page_specified ( self ) : <EOL> "<STR_LIT>" <EOL> self . login ( ) <EOL> response = self . client . get ( '<STR_LIT>' ) <EOL> self . assertEqual ( response . status_code , <NUM_LIT> ) <EOL> self . assert_ ( response [ '<STR_LIT>' ] . endswith ( '<STR_LIT>' ) ) <EOL> self . confirm_logged_out ( ) <EOL> def test_logout_with_redirect_argument ( self ) : <EOL> "<STR_LIT>" <EOL> self . login ( ) <EOL> response = self . client . get ( '<STR_LIT>' ) <EOL> self . assertEqual ( response . status_code , <NUM_LIT> ) <EOL> self . assert_ ( response [ '<STR_LIT>' ] . endswith ( '<STR_LIT>' ) ) <EOL> self . confirm_logged_out ( ) <EOL> def test_logout_with_custom_redirect_argument ( self ) : <EOL> "<STR_LIT>" <EOL> self . login ( ) <EOL> response = self . client . get ( '<STR_LIT>' ) <EOL> self . assertEqual ( response . status_code , <NUM_LIT> ) <EOL> self . assert_ ( response [ '<STR_LIT>' ] . endswith ( '<STR_LIT>' ) ) <EOL> self . confirm_logged_out ( ) </s>
