<s> """<STR_LIT>""" <EOL> from django . conf import settings <EOL> from django . core import mail <EOL> from django . core . urlresolvers import reverse <EOL> from django . test . utils import override_settings <EOL> import mock <EOL> from tests import case <EOL> from tests . utils import patch_session <EOL> @ mock . patch . object ( settings , "<STR_LIT>" , False ) <EOL> class LoginTest ( case . view . ViewTestCase ) : <EOL> """<STR_LIT>""" <EOL> @ property <EOL> def url ( self ) : <EOL> """<STR_LIT>""" <EOL> return reverse ( "<STR_LIT>" ) <EOL> def test_login ( self ) : <EOL> """<STR_LIT>""" <EOL> self . F . UserFactory . create ( username = "<STR_LIT:test>" , password = "<STR_LIT>" ) <EOL> form = self . get ( ) . forms [ "<STR_LIT>" ] <EOL> form [ "<STR_LIT:username>" ] = "<STR_LIT:test>" <EOL> form [ "<STR_LIT:password>" ] = "<STR_LIT>" <EOL> res = form . submit ( status = <NUM_LIT> ) <EOL> self . assertRedirects ( res , reverse ( "<STR_LIT>" ) ) <EOL> def test_login_next_redirect_prevent_external ( self ) : <EOL> """<STR_LIT>""" <EOL> url = reverse ( "<STR_LIT>" ) + "<STR_LIT>" <EOL> self . F . UserFactory . create ( username = "<STR_LIT:test>" , password = "<STR_LIT>" ) <EOL> form = self . app . get ( url ) . forms [ "<STR_LIT>" ] <EOL> form [ "<STR_LIT:username>" ] = "<STR_LIT:test>" <EOL> form [ "<STR_LIT:password>" ] = "<STR_LIT>" <EOL> res = form . submit ( status = <NUM_LIT> ) <EOL> self . assertRedirects ( res , reverse ( "<STR_LIT>" ) ) <EOL> def test_login_next_redirect_only_to_internal ( self ) : <EOL> """<STR_LIT>""" <EOL> url = reverse ( "<STR_LIT>" ) + "<STR_LIT>" <EOL> self . F . UserFactory . create ( username = "<STR_LIT:test>" , password = "<STR_LIT>" ) <EOL> form = self . app . get ( url ) . forms [ "<STR_LIT>" ] <EOL> form [ "<STR_LIT:username>" ] = "<STR_LIT:test>" <EOL> form [ "<STR_LIT:password>" ] = "<STR_LIT>" <EOL> res = form . submit ( status = <NUM_LIT> ) <EOL> self . assertRedirects ( res , reverse ( "<STR_LIT>" ) ) <EOL> def test_email_login ( self ) : <EOL> """<STR_LIT>""" <EOL> self . F . UserFactory . create ( <EOL> username = "<STR_LIT:test>" , email = "<STR_LIT>" , password = "<STR_LIT>" ) <EOL> form = self . get ( ) . forms [ "<STR_LIT>" ] <EOL> form [ "<STR_LIT:username>" ] = "<STR_LIT>" <EOL> form [ "<STR_LIT:password>" ] = "<STR_LIT>" <EOL> res = form . submit ( status = <NUM_LIT> ) <EOL> self . assertRedirects ( res , reverse ( "<STR_LIT>" ) ) <EOL> def test_login_failed ( self ) : <EOL> """<STR_LIT>""" <EOL> self . F . UserFactory . create ( username = "<STR_LIT:test>" , password = "<STR_LIT>" ) <EOL> form = self . get ( ) . forms [ "<STR_LIT>" ] <EOL> form [ "<STR_LIT:username>" ] = "<STR_LIT:test>" <EOL> form [ "<STR_LIT:password>" ] = "<STR_LIT>" <EOL> res = form . submit ( status = <NUM_LIT:200> ) <EOL> res . mustcontain ( "<STR_LIT>" ) <EOL> def test_display_captcha ( self ) : <EOL> """<STR_LIT>""" <EOL> res = self . get ( ) <EOL> for i in range ( <NUM_LIT:6> ) : <EOL> res = res . forms [ "<STR_LIT>" ] . submit ( ) <EOL> form = res . forms [ "<STR_LIT>" ] <EOL> self . assertIn ( "<STR_LIT>" , form . fields ) <EOL> def test_bad_captcha ( self ) : <EOL> """<STR_LIT>""" <EOL> self . F . UserFactory . create ( username = "<STR_LIT:test>" , password = "<STR_LIT>" ) <EOL> session_data = { } <EOL> with patch_session ( session_data ) : <EOL> res = self . get ( ) <EOL> for i in range ( <NUM_LIT:6> ) : <EOL> res = res . forms [ "<STR_LIT>" ] . submit ( ) <EOL> form = res . forms [ "<STR_LIT>" ] <EOL> answer = session_data [ "<STR_LIT>" ] <EOL> form [ "<STR_LIT>" ] = answer + <NUM_LIT:1> <EOL> form [ "<STR_LIT:username>" ] = "<STR_LIT:test>" <EOL> form [ "<STR_LIT:password>" ] = "<STR_LIT>" <EOL> res = form . submit ( status = <NUM_LIT:200> ) <EOL> res . mustcontain ( "<STR_LIT>" ) <EOL> def test_good_captcha ( self ) : <EOL> """<STR_LIT>""" <EOL> self . F . UserFactory . create ( username = "<STR_LIT:test>" , password = "<STR_LIT>" ) <EOL> session_data = { } <EOL> with patch_session ( session_data ) : <EOL> res = self . get ( ) <EOL> for i in range ( <NUM_LIT:6> ) : <EOL> res = res . forms [ "<STR_LIT>" ] . submit ( ) <EOL> form = res . forms [ "<STR_LIT>" ] <EOL> answer = session_data [ "<STR_LIT>" ] <EOL> form [ "<STR_LIT>" ] = answer <EOL> form [ "<STR_LIT:username>" ] = "<STR_LIT:test>" <EOL> form [ "<STR_LIT:password>" ] = "<STR_LIT>" <EOL> res = form . submit ( status = <NUM_LIT> ) <EOL> self . assertRedirects ( res , reverse ( "<STR_LIT>" ) ) <EOL> @ mock . patch . object ( settings , "<STR_LIT>" , True ) <EOL> class BrowserIDTest ( case . view . ViewTestCase ) : <EOL> """<STR_LIT>""" <EOL> @ property <EOL> def url ( self ) : <EOL> """<STR_LIT>""" <EOL> return reverse ( "<STR_LIT>" ) <EOL> def new_browserid ( self , email = "<STR_LIT>" ) : <EOL> """<STR_LIT>""" <EOL> with mock . patch ( "<STR_LIT>" ) as verify : <EOL> verify . return_value = { "<STR_LIT:email>" : email } <EOL> form = self . get ( ) . forms [ "<STR_LIT>" ] <EOL> form [ "<STR_LIT>" ] . force_value ( "<STR_LIT:foo>" ) <EOL> res = form . submit ( status = <NUM_LIT> ) <EOL> return res <EOL> def test_fail_redirect ( self ) : <EOL> """<STR_LIT>""" <EOL> url = reverse ( "<STR_LIT>" ) + "<STR_LIT>" <EOL> form = self . app . get ( url ) . forms [ "<STR_LIT>" ] <EOL> res = form . submit ( status = <NUM_LIT> ) <EOL> self . assertRedirects ( res , url ) <EOL> def test_fail_message ( self ) : <EOL> """<STR_LIT>""" <EOL> form = self . get ( ) . forms [ "<STR_LIT>" ] <EOL> res = form . submit ( status = <NUM_LIT> ) . follow ( ) <EOL> self . assertContains ( res , "<STR_LIT>" ) <EOL> def test_new_user ( self ) : <EOL> """<STR_LIT>""" <EOL> self . new_browserid ( ) <EOL> user = self . model . User . objects . get ( ) <EOL> self . assertTrue ( user . username . startswith ( "<STR_LIT>" ) ) <EOL> def test_new_user_role ( self ) : <EOL> """<STR_LIT>""" <EOL> from preferences import preferences <EOL> cp = preferences . CorePreferences <EOL> role = self . F . RoleFactory . create ( ) <EOL> cp . default_new_user_role = role <EOL> cp . save ( ) <EOL> self . new_browserid ( ) <EOL> user = self . model . User . objects . get ( ) <EOL> self . assertTrue ( user . roles . get ( ) , role ) <EOL> def test_set_username_initial ( self ) : <EOL> """<STR_LIT>""" <EOL> form = self . new_browserid ( ) . follow ( ) . follow ( ) . forms [ "<STR_LIT>" ] <EOL> self . assertEqual ( form . fields [ "<STR_LIT:username>" ] [ <NUM_LIT:0> ] . value , "<STR_LIT>" ) <EOL> def test_set_username ( self ) : <EOL> """<STR_LIT>""" <EOL> form = self . new_browserid ( ) . follow ( ) . follow ( ) . forms [ "<STR_LIT>" ] <EOL> form [ "<STR_LIT:username>" ] = "<STR_LIT>" <EOL> res = form . submit ( status = <NUM_LIT> ) <EOL> self . assertRedirects ( res , "<STR_LIT:/>" ) <EOL> user = self . model . User . objects . get ( ) <EOL> self . assertEqual ( user . username , "<STR_LIT>" ) <EOL> def test_set_username_next_internal ( self ) : <EOL> """<STR_LIT>""" <EOL> self . new_browserid ( ) <EOL> url = reverse ( "<STR_LIT>" ) + "<STR_LIT>" <EOL> form = self . app . get ( url ) . forms [ "<STR_LIT>" ] <EOL> form [ "<STR_LIT:username>" ] = "<STR_LIT>" <EOL> res = form . submit ( status = <NUM_LIT> ) <EOL> self . assertRedirects ( res , "<STR_LIT>" ) <EOL> def test_set_username_next_prevent_external ( self ) : <EOL> """<STR_LIT>""" <EOL> self . new_browserid ( ) <EOL> url = reverse ( "<STR_LIT>" ) + "<STR_LIT>" <EOL> form = self . app . get ( url ) . forms [ "<STR_LIT>" ] <EOL> form [ "<STR_LIT:username>" ] = "<STR_LIT>" <EOL> res = form . submit ( status = <NUM_LIT> ) <EOL> self . assertRedirects ( res , "<STR_LIT:/>" ) <EOL> def test_set_username_error ( self ) : <EOL> """<STR_LIT>""" <EOL> self . F . UserFactory . create ( username = "<STR_LIT>" ) <EOL> form = self . new_browserid ( ) . follow ( ) . follow ( ) . forms [ "<STR_LIT>" ] <EOL> form [ "<STR_LIT:username>" ] = "<STR_LIT>" <EOL> res = form . submit ( status = <NUM_LIT:200> ) <EOL> res . mustcontain ( "<STR_LIT>" ) <EOL> def test_auto_username_logout ( self ) : <EOL> """<STR_LIT>""" <EOL> logoutform = self . new_browserid ( ) . follow ( ) . follow ( ) . forms [ "<STR_LIT>" ] <EOL> res = logoutform . submit ( ) . follow ( ) <EOL> self . assertRedirects ( res , reverse ( "<STR_LIT>" ) + "<STR_LIT>" ) <EOL> @ mock . patch . object ( settings , "<STR_LIT>" , False ) <EOL> class LogoutTest ( case . view . ViewTestCase ) : <EOL> """<STR_LIT>""" <EOL> @ property <EOL> def url ( self ) : <EOL> """<STR_LIT>""" <EOL> return reverse ( "<STR_LIT>" ) <EOL> def test_get_405 ( self ) : <EOL> """<STR_LIT>""" <EOL> self . get ( status = <NUM_LIT> ) <EOL> def test_logout_redirect ( self ) : <EOL> """<STR_LIT>""" <EOL> user = self . F . UserFactory . create ( ) <EOL> form = self . app . get ( "<STR_LIT>" , user = user ) . forms [ "<STR_LIT>" ] <EOL> res = form . submit ( ) <EOL> self . assertRedirects ( res , "<STR_LIT>" ) <EOL> class PasswordStrengthTests ( object ) : <EOL> """<STR_LIT>""" <EOL> form_id = None <EOL> extra_form_data = { } <EOL> password_fields = [ "<STR_LIT>" , "<STR_LIT>" ] <EOL> def get_form ( self ) : <EOL> """<STR_LIT>""" <EOL> return self . get ( ) . forms [ self . form_id ] <EOL> def submit_form ( self , password , status ) : <EOL> """<STR_LIT>""" <EOL> form = self . get_form ( ) <EOL> for k , v in self . extra_form_data . items ( ) : <EOL> form [ k ] = v <EOL> for fn in self . password_fields : <EOL> form [ fn ] = password <EOL> return form . submit ( status = status ) <EOL> @ override_settings ( MINIMUM_PASSWORD_CHARS = <NUM_LIT:10> ) <EOL> def test_minimum_password_length ( self ) : <EOL> """<STR_LIT>""" <EOL> res = self . submit_form ( "<STR_LIT>" , status = <NUM_LIT:200> ) <EOL> res . mustcontain ( "<STR_LIT>" ) <EOL> @ override_settings ( PASSWORD_REQUIRE_ALPHA_NUMERIC = True ) <EOL> def test_password_require_numeric ( self ) : <EOL> """<STR_LIT>""" <EOL> res = self . submit_form ( "<STR_LIT>" , status = <NUM_LIT:200> ) <EOL> res . mustcontain ( "<STR_LIT>" ) <EOL> @ override_settings ( PASSWORD_REQUIRE_ALPHA_NUMERIC = True ) <EOL> def test_password_require_alpha ( self ) : <EOL> """<STR_LIT>""" <EOL> res = self . submit_form ( "<STR_LIT>" , status = <NUM_LIT:200> ) <EOL> res . mustcontain ( "<STR_LIT>" ) <EOL> @ override_settings ( FORBIDDEN_PASSWORDS = [ "<STR_LIT>" ] ) <EOL> def test_forbidden_passwords ( self ) : <EOL> """<STR_LIT>""" <EOL> res = self . submit_form ( "<STR_LIT>" , status = <NUM_LIT:200> ) <EOL> res . mustcontain ( "<STR_LIT>" ) <EOL> class PasswordChangeTest ( PasswordStrengthTests , <EOL> case . view . AuthenticatedViewTestCase <EOL> ) : <EOL> """<STR_LIT>""" <EOL> form_id = "<STR_LIT>" <EOL> extra_form_data = { "<STR_LIT>" : "<STR_LIT>" } <EOL> def setUp ( self ) : <EOL> """<STR_LIT>""" <EOL> super ( PasswordChangeTest , self ) . setUp ( ) <EOL> self . user . set_password ( "<STR_LIT>" ) <EOL> self . user . save ( ) <EOL> @ property <EOL> def url ( self ) : <EOL> """<STR_LIT>""" <EOL> return reverse ( "<STR_LIT>" ) <EOL> def test_change_password ( self ) : <EOL> """<STR_LIT>""" <EOL> res = self . submit_form ( "<STR_LIT>" , status = <NUM_LIT> ) . follow ( ) . follow ( ) <EOL> res . mustcontain ( "<STR_LIT>" ) <EOL> class PasswordResetTest ( case . view . ViewTestCase ) : <EOL> """<STR_LIT>""" <EOL> def setUp ( self ) : <EOL> """<STR_LIT>""" <EOL> super ( PasswordResetTest , self ) . setUp ( ) <EOL> @ property <EOL> def url ( self ) : <EOL> """<STR_LIT>""" <EOL> return reverse ( "<STR_LIT>" ) <EOL> def test_reset_password ( self ) : <EOL> """<STR_LIT>""" <EOL> self . F . UserFactory . create ( <EOL> email = "<STR_LIT>" , <EOL> password = "<STR_LIT>" <EOL> ) <EOL> r = self . get ( ) <EOL> form = r . forms [ "<STR_LIT>" ] <EOL> form [ "<STR_LIT:email>" ] = "<STR_LIT>" <EOL> res = form . submit ( status = <NUM_LIT> ) . follow ( ) . follow ( ) <EOL> res . mustcontain ( "<STR_LIT>" ) <EOL> self . assertEqual ( len ( mail . outbox ) , <NUM_LIT:1> ) <EOL> self . assertEqual ( mail . outbox [ <NUM_LIT:0> ] . to , [ "<STR_LIT>" ] ) <EOL> def test_bad_email ( self ) : <EOL> """<STR_LIT>""" <EOL> form = self . get ( ) . forms [ "<STR_LIT>" ] <EOL> form [ "<STR_LIT:email>" ] = "<STR_LIT>" <EOL> res = form . submit ( status = <NUM_LIT> ) . follow ( ) . follow ( ) <EOL> res . mustcontain ( "<STR_LIT>" ) <EOL> self . assertEqual ( len ( mail . outbox ) , <NUM_LIT:0> ) <EOL> class PasswordResetConfirmTest ( PasswordStrengthTests , case . view . ViewTestCase ) : <EOL> """<STR_LIT>""" <EOL> form_id = "<STR_LIT>" <EOL> def setUp ( self ) : <EOL> """<STR_LIT>""" <EOL> super ( PasswordResetConfirmTest , self ) . setUp ( ) <EOL> self . user = self . F . UserFactory . create ( <EOL> email = "<STR_LIT>" , <EOL> password = "<STR_LIT>" ) <EOL> assert self . user . has_usable_password ( ) <EOL> @ property <EOL> def url ( self ) : <EOL> """<STR_LIT>""" <EOL> form = self . app . get ( <EOL> reverse ( "<STR_LIT>" ) ) . forms [ "<STR_LIT>" ] <EOL> form [ "<STR_LIT:email>" ] = "<STR_LIT>" <EOL> form . submit ( status = <NUM_LIT> ) <EOL> for line in mail . outbox [ <NUM_LIT:0> ] . body . splitlines ( ) : <EOL> if "<STR_LIT>" in line : <EOL> return line . strip ( ) <EOL> self . fail ( "<STR_LIT>" ) <EOL> def test_reset_password_confirm ( self ) : <EOL> """<STR_LIT>""" <EOL> res = self . submit_form ( "<STR_LIT>" , status = <NUM_LIT> ) . follow ( ) . follow ( ) <EOL> res . mustcontain ( "<STR_LIT>" ) <EOL> class RegisterTest ( PasswordStrengthTests , case . view . ViewTestCase ) : <EOL> """<STR_LIT>""" <EOL> form_id = "<STR_LIT>" <EOL> extra_form_data = { "<STR_LIT:username>" : "<STR_LIT>" , "<STR_LIT:email>" : "<STR_LIT>" } <EOL> password_fields = [ "<STR_LIT>" , "<STR_LIT>" ] <EOL> @ property <EOL> def url ( self ) : <EOL> """<STR_LIT>""" <EOL> return reverse ( "<STR_LIT>" ) <EOL> def test_register ( self ) : <EOL> """<STR_LIT>""" <EOL> res = self . submit_form ( "<STR_LIT>" , status = <NUM_LIT> ) . follow ( ) . follow ( ) <EOL> res . mustcontain ( "<STR_LIT>" ) <EOL> def test_new_user_role ( self ) : <EOL> """<STR_LIT>""" <EOL> from preferences import preferences <EOL> cp = preferences . CorePreferences <EOL> role = self . F . RoleFactory . create ( ) <EOL> cp . default_new_user_role = role <EOL> cp . save ( ) <EOL> self . submit_form ( "<STR_LIT>" , status = <NUM_LIT> ) <EOL> user = self . model . User . objects . get ( ) <EOL> self . assertTrue ( user . roles . get ( ) , role ) <EOL> class ActivateTest ( case . view . ViewTestCase ) : <EOL> @ property <EOL> def url ( self ) : <EOL> """<STR_LIT>""" <EOL> form = self . app . get ( <EOL> reverse ( "<STR_LIT>" ) ) . forms [ "<STR_LIT>" ] <EOL> form [ "<STR_LIT:username>" ] = "<STR_LIT>" <EOL> form [ "<STR_LIT:email>" ] = "<STR_LIT>" <EOL> form [ "<STR_LIT>" ] = "<STR_LIT>" <EOL> form [ "<STR_LIT>" ] = "<STR_LIT>" <EOL> form . submit ( status = <NUM_LIT> ) <EOL> for line in mail . outbox [ <NUM_LIT:0> ] . body . splitlines ( ) : <EOL> if "<STR_LIT>" in line : <EOL> return line . strip ( ) <EOL> self . fail ( "<STR_LIT>" ) <EOL> def test_activate ( self ) : <EOL> """<STR_LIT>""" <EOL> res = self . get ( status = <NUM_LIT> ) . follow ( ) . follow ( ) <EOL> res . mustcontain ( "<STR_LIT>" ) <EOL> def test_failed_activate ( self ) : <EOL> """<STR_LIT>""" <EOL> res = self . app . get ( <EOL> reverse ( "<STR_LIT>" , kwargs = { "<STR_LIT>" : "<STR_LIT:foo>" } ) <EOL> ) <EOL> res . mustcontain ( "<STR_LIT>" ) <EOL> class CreateApiKeyTest ( case . view . AuthenticatedViewTestCase ) : <EOL> """<STR_LIT>""" <EOL> def setUp ( self ) : <EOL> """<STR_LIT>""" <EOL> super ( CreateApiKeyTest , self ) . setUp ( ) <EOL> self . owner = self . F . UserFactory . create ( ) <EOL> @ property <EOL> def url ( self ) : <EOL> """<STR_LIT>""" <EOL> return reverse ( "<STR_LIT>" , kwargs = { "<STR_LIT>" : self . owner . id } ) <EOL> def test_post ( self ) : <EOL> """<STR_LIT>""" <EOL> self . add_perm ( "<STR_LIT>" ) <EOL> res = self . get ( ) . forms [ "<STR_LIT>" ] . submit ( ) <EOL> self . assertRedirects ( res , self . url ) <EOL> ak = self . model . ApiKey . objects . get ( ) <EOL> self . assertEqual ( ak . created_by , self . user ) <EOL> self . assertEqual ( ak . owner , self . owner ) </s>
