<s> import re <EOL> from datetime import datetime , timedelta <EOL> from django . contrib . auth import get_user_model <EOL> from django . core . urlresolvers import reverse <EOL> from django . core import mail <EOL> from django . contrib . auth . forms import PasswordChangeForm <EOL> from django . test import TestCase <EOL> from userena import forms <EOL> from userena import settings as userena_settings <EOL> from userena . utils import get_user_profile <EOL> User = get_user_model ( ) <EOL> class UserenaViewsTests ( TestCase ) : <EOL> """<STR_LIT>""" <EOL> fixtures = [ '<STR_LIT>' , '<STR_LIT>' ] <EOL> def test_valid_activation ( self ) : <EOL> """<STR_LIT>""" <EOL> self . client . post ( reverse ( '<STR_LIT>' ) , <EOL> data = { '<STR_LIT:username>' : '<STR_LIT>' , <EOL> '<STR_LIT:email>' : '<STR_LIT>' , <EOL> '<STR_LIT>' : '<STR_LIT>' , <EOL> '<STR_LIT>' : '<STR_LIT>' , <EOL> '<STR_LIT>' : '<STR_LIT>' } ) <EOL> user = User . objects . get ( email = '<STR_LIT>' ) <EOL> response = self . client . get ( reverse ( '<STR_LIT>' , <EOL> kwargs = { '<STR_LIT>' : user . userena_signup . activation_key } ) ) <EOL> self . assertRedirects ( response , <EOL> reverse ( '<STR_LIT>' , kwargs = { '<STR_LIT:username>' : user . username } ) ) <EOL> user = User . objects . get ( email = '<STR_LIT>' ) <EOL> self . assertTrue ( user . is_active ) <EOL> def test_activation_expired_retry ( self ) : <EOL> """<STR_LIT>""" <EOL> userena_settings . USERENA_ACTIVATION_RETRY = True <EOL> self . client . post ( reverse ( '<STR_LIT>' ) , <EOL> data = { '<STR_LIT:username>' : '<STR_LIT>' , <EOL> '<STR_LIT:email>' : '<STR_LIT>' , <EOL> '<STR_LIT>' : '<STR_LIT>' , <EOL> '<STR_LIT>' : '<STR_LIT>' , <EOL> '<STR_LIT>' : '<STR_LIT>' } ) <EOL> user = User . objects . get ( email = '<STR_LIT>' ) <EOL> user . date_joined = datetime . today ( ) - timedelta ( days = <NUM_LIT:30> ) <EOL> user . save ( ) <EOL> response = self . client . get ( reverse ( '<STR_LIT>' , <EOL> kwargs = { '<STR_LIT>' : user . userena_signup . activation_key } ) ) <EOL> self . assertContains ( response , "<STR_LIT>" ) <EOL> user = User . objects . get ( email = '<STR_LIT>' ) <EOL> self . assertTrue ( not user . is_active ) <EOL> userena_settings . USERENA_ACTIVATION_RETRY = False <EOL> def test_retry_activation_ask ( self ) : <EOL> """<STR_LIT>""" <EOL> userena_settings . USERENA_ACTIVATION_RETRY = True <EOL> self . client . post ( reverse ( '<STR_LIT>' ) , <EOL> data = { '<STR_LIT:username>' : '<STR_LIT>' , <EOL> '<STR_LIT:email>' : '<STR_LIT>' , <EOL> '<STR_LIT>' : '<STR_LIT>' , <EOL> '<STR_LIT>' : '<STR_LIT>' , <EOL> '<STR_LIT>' : '<STR_LIT>' } ) <EOL> user = User . objects . get ( email = '<STR_LIT>' ) <EOL> user . date_joined = datetime . today ( ) - timedelta ( days = <NUM_LIT:30> ) <EOL> user . save ( ) <EOL> old_key = user . userena_signup . activation_key <EOL> response = self . client . get ( reverse ( '<STR_LIT>' , <EOL> kwargs = { '<STR_LIT>' : old_key } ) ) <EOL> user = User . objects . get ( email = '<STR_LIT>' ) <EOL> self . assertContains ( response , "<STR_LIT>" ) <EOL> self . assertNotEqual ( old_key , user . userena_signup . activation_key ) <EOL> user = User . objects . get ( email = '<STR_LIT>' ) <EOL> self . assertTrue ( not user . is_active ) <EOL> self . assertEqual ( len ( mail . outbox ) , <NUM_LIT:2> ) <EOL> self . assertEqual ( mail . outbox [ <NUM_LIT:1> ] . to , [ '<STR_LIT>' ] ) <EOL> self . assertTrue ( mail . outbox [ <NUM_LIT:1> ] . body . find ( "<STR_LIT>" ) > - <NUM_LIT:1> ) <EOL> response = self . client . get ( reverse ( '<STR_LIT>' , <EOL> kwargs = { '<STR_LIT>' : user . userena_signup . activation_key } ) ) <EOL> self . assertRedirects ( response , <EOL> reverse ( '<STR_LIT>' , kwargs = { '<STR_LIT:username>' : user . username } ) ) <EOL> user = User . objects . get ( email = '<STR_LIT>' ) <EOL> self . assertTrue ( user . is_active ) <EOL> userena_settings . USERENA_ACTIVATION_RETRY = False <EOL> def test_invalid_activation ( self ) : <EOL> """<STR_LIT>""" <EOL> response = self . client . get ( reverse ( '<STR_LIT>' , <EOL> kwargs = { '<STR_LIT>' : '<STR_LIT>' } ) ) <EOL> self . assertEqual ( response . status_code , <NUM_LIT:200> ) <EOL> self . assertTemplateUsed ( response , <EOL> '<STR_LIT>' ) <EOL> def test_valid_confirmation ( self ) : <EOL> """<STR_LIT>""" <EOL> user = User . objects . get ( pk = <NUM_LIT:1> ) <EOL> user . userena_signup . change_email ( '<STR_LIT>' ) <EOL> response = self . client . get ( reverse ( '<STR_LIT>' , <EOL> kwargs = { '<STR_LIT>' : user . userena_signup . email_confirmation_key } ) ) <EOL> self . assertRedirects ( response , <EOL> reverse ( '<STR_LIT>' , kwargs = { '<STR_LIT:username>' : user . username } ) ) <EOL> def test_invalid_confirmation ( self ) : <EOL> """<STR_LIT>""" <EOL> response = self . client . get ( reverse ( '<STR_LIT>' , <EOL> kwargs = { '<STR_LIT>' : '<STR_LIT>' } ) ) <EOL> self . assertTemplateUsed ( response , <EOL> '<STR_LIT>' ) <EOL> def test_disabled_view ( self ) : <EOL> """<STR_LIT>""" <EOL> response = self . client . get ( reverse ( '<STR_LIT>' , <EOL> kwargs = { '<STR_LIT:username>' : '<STR_LIT>' } ) ) <EOL> self . assertEqual ( response . status_code , <NUM_LIT> ) <EOL> u = User . objects . filter ( username = '<STR_LIT>' ) . update ( is_active = False ) <EOL> response = self . client . get ( reverse ( '<STR_LIT>' , <EOL> kwargs = { '<STR_LIT:username>' : '<STR_LIT>' } ) ) <EOL> self . assertEqual ( response . status_code , <NUM_LIT:200> ) <EOL> self . assertTemplateUsed ( response , <EOL> '<STR_LIT>' ) <EOL> def test_signup_view ( self ) : <EOL> """<STR_LIT>""" <EOL> response = self . client . get ( reverse ( '<STR_LIT>' ) ) <EOL> self . assertEqual ( response . status_code , <NUM_LIT:200> ) <EOL> self . assertTemplateUsed ( response , <EOL> '<STR_LIT>' ) <EOL> self . assertTrue ( isinstance ( response . context [ '<STR_LIT>' ] , <EOL> forms . SignupForm ) ) <EOL> userena_settings . USERENA_WITHOUT_USERNAMES = True <EOL> response = self . client . get ( reverse ( '<STR_LIT>' ) ) <EOL> self . assertTrue ( isinstance ( response . context [ '<STR_LIT>' ] , <EOL> forms . SignupFormOnlyEmail ) ) <EOL> userena_settings . USERENA_WITHOUT_USERNAMES = False <EOL> userena_settings . USERENA_DISABLE_SIGNUP = True <EOL> response = self . client . get ( reverse ( '<STR_LIT>' ) ) <EOL> self . assertEqual ( response . status_code , <NUM_LIT> ) <EOL> userena_settings . USERENA_DISABLE_SIGNUP = False <EOL> def test_signup_view_signout ( self ) : <EOL> """<STR_LIT>""" <EOL> self . assertTrue ( self . client . login ( username = '<STR_LIT>' , password = '<STR_LIT>' ) ) <EOL> response = self . client . post ( reverse ( '<STR_LIT>' ) , <EOL> data = { '<STR_LIT:username>' : '<STR_LIT>' , <EOL> '<STR_LIT:email>' : '<STR_LIT>' , <EOL> '<STR_LIT>' : '<STR_LIT>' , <EOL> '<STR_LIT>' : '<STR_LIT>' , <EOL> '<STR_LIT>' : '<STR_LIT>' } ) <EOL> self . assertFalse ( len ( self . client . session . keys ( ) ) > <NUM_LIT:0> ) <EOL> def test_signup_view_success ( self ) : <EOL> """<STR_LIT>""" <EOL> response = self . client . post ( reverse ( '<STR_LIT>' ) , <EOL> data = { '<STR_LIT:username>' : '<STR_LIT>' , <EOL> '<STR_LIT:email>' : '<STR_LIT>' , <EOL> '<STR_LIT>' : '<STR_LIT>' , <EOL> '<STR_LIT>' : '<STR_LIT>' , <EOL> '<STR_LIT>' : '<STR_LIT>' } ) <EOL> self . assertRedirects ( response , <EOL> reverse ( '<STR_LIT>' , kwargs = { '<STR_LIT:username>' : '<STR_LIT>' } ) ) <EOL> self . assertEqual ( User . objects . filter ( email__iexact = '<STR_LIT>' ) . count ( ) , <NUM_LIT:1> ) <EOL> def test_signup_view_with_signin ( self ) : <EOL> """<STR_LIT>""" <EOL> userena_settings . USERENA_SIGNIN_AFTER_SIGNUP = True <EOL> userena_settings . USERENA_ACTIVATION_REQUIRED = True <EOL> response = self . client . post ( reverse ( '<STR_LIT>' ) , <EOL> data = { '<STR_LIT:username>' : '<STR_LIT>' , <EOL> '<STR_LIT:email>' : '<STR_LIT>' , <EOL> '<STR_LIT>' : '<STR_LIT>' , <EOL> '<STR_LIT>' : '<STR_LIT>' , <EOL> '<STR_LIT>' : '<STR_LIT>' } ) <EOL> userena_settings . USERENA_SIGNIN_AFTER_SIGNUP = False <EOL> userena_settings . USERENA_ACTIVATION_REQUIRED = True <EOL> response_check = self . client . get ( reverse ( '<STR_LIT>' , <EOL> kwargs = { '<STR_LIT:username>' : '<STR_LIT>' } ) ) <EOL> self . assertEqual ( response_check . status_code , <NUM_LIT> ) <EOL> userena_settings . USERENA_SIGNIN_AFTER_SIGNUP = True <EOL> userena_settings . USERENA_ACTIVATION_REQUIRED = False <EOL> response = self . client . post ( reverse ( '<STR_LIT>' ) , <EOL> data = { '<STR_LIT:username>' : '<STR_LIT>' , <EOL> '<STR_LIT:email>' : '<STR_LIT>' , <EOL> '<STR_LIT>' : '<STR_LIT>' , <EOL> '<STR_LIT>' : '<STR_LIT>' , <EOL> '<STR_LIT>' : '<STR_LIT>' } ) <EOL> userena_settings . USERENA_SIGNIN_AFTER_SIGNUP = False <EOL> userena_settings . USERENA_ACTIVATION_REQUIRED = True <EOL> response_check = self . client . get ( reverse ( '<STR_LIT>' , <EOL> kwargs = { '<STR_LIT:username>' : '<STR_LIT>' } ) ) <EOL> self . assertEqual ( response_check . status_code , <NUM_LIT:200> ) <EOL> def test_signin_view ( self ) : <EOL> """<STR_LIT>""" <EOL> response = self . client . get ( reverse ( '<STR_LIT>' ) ) <EOL> self . assertEqual ( response . status_code , <NUM_LIT:200> ) <EOL> self . assertTemplateUsed ( response , <EOL> '<STR_LIT>' ) <EOL> def test_signin_view_remember_me_on ( self ) : <EOL> """<STR_LIT>""" <EOL> response = self . client . post ( reverse ( '<STR_LIT>' ) , <EOL> data = { '<STR_LIT>' : '<STR_LIT>' , <EOL> '<STR_LIT:password>' : '<STR_LIT>' , <EOL> '<STR_LIT>' : True } ) <EOL> self . assertEqual ( self . client . session . get_expiry_age ( ) , <EOL> userena_settings . USERENA_REMEMBER_ME_DAYS [ <NUM_LIT:1> ] * <NUM_LIT> * <NUM_LIT> ) <EOL> def test_signin_view_remember_off ( self ) : <EOL> """<STR_LIT>""" <EOL> response = self . client . post ( reverse ( '<STR_LIT>' ) , <EOL> data = { '<STR_LIT>' : '<STR_LIT>' , <EOL> '<STR_LIT:password>' : '<STR_LIT>' } ) <EOL> self . assertTrue ( self . client . session . get_expire_at_browser_close ( ) ) <EOL> def test_signin_view_inactive ( self ) : <EOL> """<STR_LIT>""" <EOL> user = User . objects . get ( email = '<STR_LIT>' ) <EOL> user . is_active = False <EOL> user . save ( ) <EOL> response = self . client . post ( reverse ( '<STR_LIT>' ) , <EOL> data = { '<STR_LIT>' : '<STR_LIT>' , <EOL> '<STR_LIT:password>' : '<STR_LIT>' } ) <EOL> self . assertRedirects ( response , <EOL> reverse ( '<STR_LIT>' , <EOL> kwargs = { '<STR_LIT:username>' : user . username } ) ) <EOL> def test_signin_view_success ( self ) : <EOL> """<STR_LIT>""" <EOL> response = self . client . post ( reverse ( '<STR_LIT>' ) , <EOL> data = { '<STR_LIT>' : '<STR_LIT>' , <EOL> '<STR_LIT:password>' : '<STR_LIT>' } ) <EOL> self . assertRedirects ( response , reverse ( '<STR_LIT>' , <EOL> kwargs = { '<STR_LIT:username>' : '<STR_LIT>' } ) ) <EOL> response = self . client . post ( reverse ( '<STR_LIT>' ) , <EOL> data = { '<STR_LIT>' : '<STR_LIT>' , <EOL> '<STR_LIT:password>' : '<STR_LIT>' , <EOL> '<STR_LIT>' : '<STR_LIT>' } ) <EOL> self . assertRedirects ( response , '<STR_LIT>' ) <EOL> def test_signin_view_with_invalid_next ( self ) : <EOL> """<STR_LIT>""" <EOL> response = self . client . post ( reverse ( '<STR_LIT>' ) , <EOL> data = { '<STR_LIT>' : '<STR_LIT>' , <EOL> '<STR_LIT:password>' : '<STR_LIT>' , <EOL> '<STR_LIT>' : '<STR_LIT>' } , <EOL> follow = True ) <EOL> self . assertEqual ( response . status_code , <NUM_LIT> ) <EOL> def test_signout_view ( self ) : <EOL> """<STR_LIT>""" <EOL> response = self . client . get ( reverse ( '<STR_LIT>' ) ) <EOL> self . assertEqual ( response . status_code , <NUM_LIT:200> ) <EOL> self . assertTemplateUsed ( response , <EOL> '<STR_LIT>' ) <EOL> def test_change_email_view ( self ) : <EOL> """<STR_LIT>""" <EOL> response = self . client . get ( reverse ( '<STR_LIT>' , <EOL> kwargs = { '<STR_LIT:username>' : '<STR_LIT>' } ) ) <EOL> self . assertEqual ( response . status_code , <NUM_LIT> ) <EOL> client = self . client . login ( username = '<STR_LIT>' , password = '<STR_LIT>' ) <EOL> response = self . client . get ( reverse ( '<STR_LIT>' , <EOL> kwargs = { '<STR_LIT:username>' : '<STR_LIT>' } ) ) <EOL> self . assertEqual ( response . status_code , <NUM_LIT:200> ) <EOL> self . assertTrue ( isinstance ( response . context [ '<STR_LIT>' ] , <EOL> forms . ChangeEmailForm ) ) <EOL> self . assertTemplateUsed ( response , <EOL> '<STR_LIT>' ) <EOL> def test_change_valid_email_view ( self ) : <EOL> """<STR_LIT>""" <EOL> self . client . login ( username = '<STR_LIT>' , password = '<STR_LIT>' ) <EOL> response = self . client . post ( reverse ( '<STR_LIT>' , <EOL> kwargs = { '<STR_LIT:username>' : '<STR_LIT>' } ) , <EOL> data = { '<STR_LIT:email>' : '<STR_LIT>' } ) <EOL> self . assertRedirects ( response , <EOL> reverse ( '<STR_LIT>' , <EOL> kwargs = { '<STR_LIT:username>' : '<STR_LIT>' } ) ) <EOL> def test_change_password_view ( self ) : <EOL> """<STR_LIT>""" <EOL> self . client . login ( username = '<STR_LIT>' , password = '<STR_LIT>' ) <EOL> response = self . client . get ( reverse ( '<STR_LIT>' , <EOL> kwargs = { '<STR_LIT:username>' : '<STR_LIT>' } ) ) <EOL> self . assertEqual ( response . status_code , <NUM_LIT:200> ) <EOL> self . assertTemplateUsed ( response , '<STR_LIT>' ) <EOL> self . assertTrue ( response . context [ '<STR_LIT>' ] , <EOL> PasswordChangeForm ) <EOL> def test_change_password_view_success ( self ) : <EOL> """<STR_LIT>""" <EOL> self . client . login ( username = '<STR_LIT>' , password = '<STR_LIT>' ) <EOL> new_password = '<STR_LIT>' <EOL> response = self . client . post ( reverse ( '<STR_LIT>' , <EOL> kwargs = { '<STR_LIT:username>' : '<STR_LIT>' } ) , <EOL> data = { '<STR_LIT>' : new_password , <EOL> '<STR_LIT>' : '<STR_LIT>' , <EOL> '<STR_LIT>' : '<STR_LIT>' } ) <EOL> self . assertRedirects ( response , reverse ( '<STR_LIT>' , <EOL> kwargs = { '<STR_LIT:username>' : '<STR_LIT>' } ) ) <EOL> john = User . objects . get ( username = '<STR_LIT>' ) <EOL> self . assertTrue ( john . check_password ( new_password ) ) <EOL> def test_profile_detail_view ( self ) : <EOL> """<STR_LIT>""" <EOL> response = self . client . get ( reverse ( '<STR_LIT>' , <EOL> kwargs = { '<STR_LIT:username>' : '<STR_LIT>' } ) ) <EOL> self . assertEqual ( response . status_code , <NUM_LIT:200> ) <EOL> self . assertTemplateUsed ( response , '<STR_LIT>' ) <EOL> def test_profile_edit_view ( self ) : <EOL> """<STR_LIT>""" <EOL> self . client . login ( username = '<STR_LIT>' , password = '<STR_LIT>' ) <EOL> response = self . client . get ( reverse ( '<STR_LIT>' , <EOL> kwargs = { '<STR_LIT:username>' : '<STR_LIT>' } ) ) <EOL> self . assertEqual ( response . status_code , <NUM_LIT:200> ) <EOL> self . assertTemplateUsed ( response , '<STR_LIT>' ) <EOL> self . assertTrue ( isinstance ( response . context [ '<STR_LIT>' ] , <EOL> forms . EditProfileForm ) ) <EOL> def test_profile_edit_view_success ( self ) : <EOL> """<STR_LIT>""" <EOL> self . client . login ( username = '<STR_LIT>' , password = '<STR_LIT>' ) <EOL> new_about_me = '<STR_LIT>' <EOL> response = self . client . post ( reverse ( '<STR_LIT>' , <EOL> kwargs = { '<STR_LIT:username>' : '<STR_LIT>' } ) , <EOL> data = { '<STR_LIT>' : new_about_me , <EOL> '<STR_LIT>' : '<STR_LIT>' , <EOL> '<STR_LIT>' : '<STR_LIT>' } ) <EOL> self . assertRedirects ( response , reverse ( '<STR_LIT>' , <EOL> kwargs = { '<STR_LIT:username>' : '<STR_LIT>' } ) ) <EOL> profile = get_user_profile ( user = User . objects . get ( username = '<STR_LIT>' ) ) <EOL> self . assertEqual ( profile . about_me , new_about_me ) <EOL> def test_profile_list_view ( self ) : <EOL> """<STR_LIT>""" <EOL> userena_settings . USERENA_DISABLE_PROFILE_LIST = False <EOL> response = self . client . get ( reverse ( '<STR_LIT>' ) ) <EOL> self . assertEqual ( response . status_code , <NUM_LIT:200> ) <EOL> self . assertTemplateUsed ( response , '<STR_LIT>' ) <EOL> userena_settings . USERENA_DISABLE_PROFILE_LIST = True <EOL> response = self . client . get ( reverse ( '<STR_LIT>' ) ) <EOL> self . assertEqual ( response . status_code , <NUM_LIT> ) <EOL> def test_password_reset_view_success ( self ) : <EOL> """<STR_LIT>""" <EOL> response = self . client . post ( reverse ( '<STR_LIT>' ) , <EOL> data = { '<STR_LIT:email>' : '<STR_LIT>' , } ) <EOL> self . assertEqual ( response . status_code , <NUM_LIT> ) <EOL> self . assertIn ( reverse ( '<STR_LIT>' ) , response . get ( '<STR_LIT>' ) ) <EOL> self . assertTrue ( mail . outbox ) <EOL> def test_password_reset_view_failure ( self ) : <EOL> """<STR_LIT>""" <EOL> response = self . client . post ( reverse ( '<STR_LIT>' ) , <EOL> data = { '<STR_LIT:email>' : '<STR_LIT>' , } ) <EOL> self . assertIn ( response . status_code , [ <NUM_LIT:200> , <NUM_LIT> ] ) <EOL> self . assertFalse ( mail . outbox ) <EOL> def test_password_reset_confirm ( self ) : <EOL> self . client . post ( reverse ( '<STR_LIT>' ) , <EOL> data = { '<STR_LIT:email>' : '<STR_LIT>' , } ) <EOL> confirm_mail = mail . outbox [ <NUM_LIT:0> ] <EOL> confirm_url = re . search ( r'<STR_LIT>' , confirm_mail . body ) . group ( ) <EOL> response = self . client . get ( confirm_url ) <EOL> self . assertEqual ( response . status_code , <NUM_LIT:200> ) <EOL> response = self . client . post ( confirm_url , <EOL> data = { '<STR_LIT>' : '<STR_LIT>' , <EOL> '<STR_LIT>' : '<STR_LIT>' , } ) <EOL> self . assertEqual ( response . status_code , <NUM_LIT> ) <EOL> self . assertIn ( reverse ( '<STR_LIT>' ) , response . get ( '<STR_LIT>' ) ) </s>
