<s> from __future__ import unicode_literals <EOL> import re <EOL> from django . contrib import messages <EOL> from django . contrib . auth . models import User <EOL> from django . core . exceptions import ValidationError <EOL> from django . test . client import RequestFactory <EOL> from djblets . registries . errors import ItemLookupError , RegistrationError <EOL> from djblets . testing . decorators import add_fixtures <EOL> from kgb import SpyAgency <EOL> from reviewboard . accounts . backends import ( AuthBackend , <EOL> get_enabled_auth_backends , <EOL> INVALID_USERNAME_CHAR_REGEX , <EOL> StandardAuthBackend ) <EOL> from reviewboard . accounts . forms . pages import ( AccountPageForm , <EOL> ChangePasswordForm , <EOL> ProfileForm ) <EOL> from reviewboard . accounts . models import ( LocalSiteProfile , <EOL> Profile , <EOL> ReviewRequestVisit , <EOL> Trophy ) <EOL> from reviewboard . accounts . pages import ( AccountPage , get_page_classes , <EOL> register_account_page_class , <EOL> unregister_account_page_class ) <EOL> from reviewboard . testing import TestCase <EOL> class AuthBackendTests ( TestCase ) : <EOL> """<STR_LIT>""" <EOL> def _get_standard_auth_backend ( self ) : <EOL> backend = None <EOL> for backend in get_enabled_auth_backends ( ) : <EOL> if type ( backend ) is StandardAuthBackend : <EOL> break <EOL> self . assertIs ( type ( backend ) , StandardAuthBackend ) <EOL> return backend <EOL> @ add_fixtures ( [ '<STR_LIT>' ] ) <EOL> def test_get_or_create_user_exists ( self ) : <EOL> """<STR_LIT>""" <EOL> original_count = User . objects . count ( ) <EOL> user = User . objects . get ( username = '<STR_LIT>' ) <EOL> backend = self . _get_standard_auth_backend ( ) <EOL> result = backend . get_or_create_user ( '<STR_LIT>' , None ) <EOL> self . assertEqual ( original_count , User . objects . count ( ) ) <EOL> self . assertEqual ( user , result ) <EOL> def test_get_or_create_user_new ( self ) : <EOL> """<STR_LIT>""" <EOL> backend = self . _get_standard_auth_backend ( ) <EOL> self . assertIsInstance ( backend , StandardAuthBackend ) <EOL> user = backend . get_or_create_user ( '<STR_LIT>' , None ) <EOL> self . assertIsNone ( user ) <EOL> @ add_fixtures ( [ '<STR_LIT>' ] ) <EOL> def test_get_user_exists ( self ) : <EOL> """<STR_LIT>""" <EOL> user = User . objects . get ( username = '<STR_LIT>' ) <EOL> backend = self . _get_standard_auth_backend ( ) <EOL> result = backend . get_user ( user . pk ) <EOL> self . assertEqual ( user , result ) <EOL> def test_get_user_not_exists ( self ) : <EOL> """<STR_LIT>""" <EOL> backend = self . _get_standard_auth_backend ( ) <EOL> result = backend . get_user ( <NUM_LIT:1> ) <EOL> self . assertIsNone ( result ) <EOL> class ReviewRequestVisitTests ( TestCase ) : <EOL> """<STR_LIT>""" <EOL> fixtures = [ '<STR_LIT>' ] <EOL> def test_default_visibility ( self ) : <EOL> """<STR_LIT>""" <EOL> review_request = self . create_review_request ( publish = True ) <EOL> self . client . login ( username = '<STR_LIT>' , password = '<STR_LIT>' ) <EOL> self . client . get ( review_request . get_absolute_url ( ) ) <EOL> visit = ReviewRequestVisit . objects . get ( <EOL> user__username = '<STR_LIT>' , review_request = review_request . id ) <EOL> self . assertEqual ( visit . visibility , ReviewRequestVisit . VISIBLE ) <EOL> class ProfileTests ( TestCase ) : <EOL> """<STR_LIT>""" <EOL> fixtures = [ '<STR_LIT>' ] <EOL> def test_is_profile_visible_with_public ( self ) : <EOL> """<STR_LIT>""" <EOL> user1 = User . objects . get ( username = '<STR_LIT>' ) <EOL> user2 = User . objects . get ( username = '<STR_LIT>' ) <EOL> self . assertTrue ( user1 . is_profile_visible ( user2 ) ) <EOL> def test_is_profile_visible_with_private ( self ) : <EOL> """<STR_LIT>""" <EOL> user1 = User . objects . get ( username = '<STR_LIT>' ) <EOL> user2 = User . objects . get ( username = '<STR_LIT>' ) <EOL> profile = user1 . get_profile ( ) <EOL> profile . is_private = True <EOL> profile . save ( ) <EOL> self . assertFalse ( user1 . is_profile_visible ( user2 ) ) <EOL> self . assertTrue ( user1 . is_profile_visible ( user1 ) ) <EOL> user2 . is_staff = True <EOL> self . assertTrue ( user1 . is_profile_visible ( user2 ) ) <EOL> @ add_fixtures ( [ '<STR_LIT>' , '<STR_LIT>' ] ) <EOL> def test_is_star_unstar_updating_count_correctly ( self ) : <EOL> """<STR_LIT>""" <EOL> user1 = User . objects . get ( username = '<STR_LIT>' ) <EOL> profile1 = user1 . get_profile ( ) <EOL> review_request = self . create_review_request ( publish = True ) <EOL> site_profile = profile1 . site_profiles . get ( local_site = None ) <EOL> profile1 . star_review_request ( review_request ) <EOL> site_profile = LocalSiteProfile . objects . get ( pk = site_profile . pk ) <EOL> self . assertTrue ( review_request in <EOL> profile1 . starred_review_requests . all ( ) ) <EOL> self . assertEqual ( site_profile . starred_public_request_count , <NUM_LIT:1> ) <EOL> profile1 . unstar_review_request ( review_request ) <EOL> site_profile = LocalSiteProfile . objects . get ( pk = site_profile . pk ) <EOL> self . assertFalse ( review_request in <EOL> profile1 . starred_review_requests . all ( ) ) <EOL> self . assertEqual ( site_profile . starred_public_request_count , <NUM_LIT:0> ) <EOL> class AccountPageTests ( TestCase ) : <EOL> """<STR_LIT>""" <EOL> builtin_pages = set ( [ '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' , <EOL> '<STR_LIT>' ] ) <EOL> def tearDown ( self ) : <EOL> """<STR_LIT>""" <EOL> super ( AccountPageTests , self ) . tearDown ( ) <EOL> AccountPage . registry . reset ( ) <EOL> def test_default_pages ( self ) : <EOL> """<STR_LIT>""" <EOL> page_classes = list ( get_page_classes ( ) ) <EOL> self . assertEqual ( len ( page_classes ) , len ( self . builtin_pages ) ) <EOL> page_class_ids = [ page_cls . page_id for page_cls in page_classes ] <EOL> self . assertEqual ( set ( page_class_ids ) , self . builtin_pages ) <EOL> def test_register_account_page_class ( self ) : <EOL> """<STR_LIT>""" <EOL> class MyPage ( AccountPage ) : <EOL> page_id = '<STR_LIT>' <EOL> page_title = '<STR_LIT>' <EOL> register_account_page_class ( MyPage ) <EOL> page_classes = list ( get_page_classes ( ) ) <EOL> self . assertEqual ( len ( page_classes ) , len ( self . builtin_pages ) + <NUM_LIT:1> ) <EOL> self . assertEqual ( page_classes [ - <NUM_LIT:1> ] , MyPage ) <EOL> def test_register_account_page_class_with_duplicate ( self ) : <EOL> """<STR_LIT>""" <EOL> class MyPage ( AccountPage ) : <EOL> page_id = '<STR_LIT>' <EOL> page_title = '<STR_LIT>' <EOL> register_account_page_class ( MyPage ) <EOL> with self . assertRaises ( RegistrationError ) : <EOL> register_account_page_class ( MyPage ) <EOL> def test_unregister_account_page_class ( self ) : <EOL> """<STR_LIT>""" <EOL> class MyPage ( AccountPage ) : <EOL> page_id = '<STR_LIT>' <EOL> page_title = '<STR_LIT>' <EOL> register_account_page_class ( MyPage ) <EOL> unregister_account_page_class ( MyPage ) <EOL> page_classes = list ( get_page_classes ( ) ) <EOL> self . assertEqual ( len ( page_classes ) , len ( self . builtin_pages ) ) <EOL> def test_unregister_unknown_account_page_class ( self ) : <EOL> """<STR_LIT>""" <EOL> class MyPage ( AccountPage ) : <EOL> page_id = '<STR_LIT>' <EOL> page_title = '<STR_LIT>' <EOL> with self . assertRaises ( ItemLookupError ) : <EOL> unregister_account_page_class ( MyPage ) <EOL> def test_add_form_to_page ( self ) : <EOL> """<STR_LIT>""" <EOL> class MyPage ( AccountPage ) : <EOL> page_id = '<STR_LIT>' <EOL> page_title = '<STR_LIT>' <EOL> class MyForm ( AccountPageForm ) : <EOL> form_id = '<STR_LIT>' <EOL> register_account_page_class ( MyPage ) <EOL> MyPage . add_form ( MyForm ) <EOL> self . assertEqual ( MyPage . form_classes , [ MyForm ] ) <EOL> def test_add_duplicate_form_to_page ( self ) : <EOL> """<STR_LIT>""" <EOL> class MyForm ( AccountPageForm ) : <EOL> form_id = '<STR_LIT>' <EOL> class MyPage ( AccountPage ) : <EOL> page_id = '<STR_LIT>' <EOL> page_title = '<STR_LIT>' <EOL> form_classes = [ MyForm ] <EOL> register_account_page_class ( MyPage ) <EOL> with self . assertRaises ( RegistrationError ) : <EOL> MyPage . add_form ( MyForm ) <EOL> self . assertEqual ( MyPage . form_classes , [ MyForm ] ) <EOL> def test_remove_form_from_page ( self ) : <EOL> """<STR_LIT>""" <EOL> class MyForm ( AccountPageForm ) : <EOL> form_id = '<STR_LIT>' <EOL> class MyPage ( AccountPage ) : <EOL> page_id = '<STR_LIT>' <EOL> page_title = '<STR_LIT>' <EOL> form_classes = [ MyForm ] <EOL> register_account_page_class ( MyPage ) <EOL> MyPage . remove_form ( MyForm ) <EOL> self . assertEqual ( MyPage . form_classes , [ ] ) <EOL> def test_remove_unknown_form_from_page ( self ) : <EOL> """<STR_LIT>""" <EOL> class MyForm ( AccountPageForm ) : <EOL> form_id = '<STR_LIT>' <EOL> class MyPage ( AccountPage ) : <EOL> page_id = '<STR_LIT>' <EOL> page_title = '<STR_LIT>' <EOL> register_account_page_class ( MyPage ) <EOL> with self . assertRaises ( ItemLookupError ) : <EOL> MyPage . remove_form ( MyForm ) <EOL> def test_default_form_classes_for_page ( self ) : <EOL> """<STR_LIT>""" <EOL> class MyForm ( AccountPageForm ) : <EOL> form_id = '<STR_LIT>' <EOL> class MyPage ( AccountPage ) : <EOL> page_id = '<STR_LIT>' <EOL> page_title = '<STR_LIT>' <EOL> form_classes = [ MyForm ] <EOL> register_account_page_class ( MyPage ) <EOL> self . assertEqual ( MyPage . form_classes , [ MyForm ] ) <EOL> unregister_account_page_class ( MyPage ) <EOL> self . assertEqual ( MyPage . form_classes , [ ] ) <EOL> register_account_page_class ( MyPage ) <EOL> self . assertEqual ( MyPage . form_classes , [ MyForm ] ) <EOL> def test_empty_default_form_classes_for_page ( self ) : <EOL> """<STR_LIT>""" <EOL> class MyPage ( AccountPage ) : <EOL> page_id = '<STR_LIT>' <EOL> page_title = '<STR_LIT>' <EOL> class MyForm ( AccountPageForm ) : <EOL> form_id = '<STR_LIT>' <EOL> register_account_page_class ( MyPage ) <EOL> self . assertEqual ( MyPage . form_classes , [ ] ) <EOL> MyPage . add_form ( MyForm ) <EOL> self . assertEqual ( MyPage . form_classes , [ MyForm ] ) <EOL> unregister_account_page_class ( MyPage ) <EOL> self . assertEqual ( MyPage . form_classes , [ ] ) <EOL> register_account_page_class ( MyPage ) <EOL> self . assertEqual ( MyPage . form_classes , [ ] ) <EOL> class UsernameTests ( TestCase ) : <EOL> """<STR_LIT>""" <EOL> cases = [ <EOL> ( '<STR_LIT>' , '<STR_LIT>' ) , <EOL> ( '<STR_LIT>' , '<STR_LIT>' ) , <EOL> ( '<STR_LIT>' , '<STR_LIT>' ) , <EOL> ( '<STR_LIT>' , '<STR_LIT>' ) , <EOL> ( '<STR_LIT>' , '<STR_LIT>' ) , <EOL> ( '<STR_LIT>' , '<STR_LIT>' ) , <EOL> ( '<STR_LIT>' , '<STR_LIT>' ) , <EOL> ( '<STR_LIT>' , '<STR_LIT>' ) , <EOL> ( '<STR_LIT>' , '<STR_LIT>' ) , <EOL> ] <EOL> def test ( self ) : <EOL> """<STR_LIT>""" <EOL> for orig , new in self . cases : <EOL> self . assertEqual ( <EOL> re . sub ( INVALID_USERNAME_CHAR_REGEX , '<STR_LIT>' , orig ) . lower ( ) , <EOL> new ) <EOL> class TrophyTests ( TestCase ) : <EOL> """<STR_LIT>""" <EOL> fixtures = [ '<STR_LIT>' ] <EOL> def test_is_fish_trophy_awarded_for_new_review_request ( self ) : <EOL> """<STR_LIT>""" <EOL> user1 = User . objects . get ( username = '<STR_LIT>' ) <EOL> category = '<STR_LIT>' <EOL> review_request = self . create_review_request ( publish = True , id = <NUM_LIT> , <EOL> submitter = user1 ) <EOL> trophies = Trophy . objects . get_trophies ( review_request ) <EOL> self . assertEqual ( trophies [ <NUM_LIT:0> ] . category , category ) <EOL> self . assertTrue ( <EOL> trophies [ <NUM_LIT:0> ] . review_request . extra_data [ '<STR_LIT>' ] ) <EOL> def test_is_fish_trophy_awarded_for_older_review_request ( self ) : <EOL> """<STR_LIT>""" <EOL> user1 = User . objects . get ( username = '<STR_LIT>' ) <EOL> category = '<STR_LIT>' <EOL> review_request = self . create_review_request ( publish = True , id = <NUM_LIT> , <EOL> submitter = user1 ) <EOL> del review_request . extra_data [ '<STR_LIT>' ] <EOL> trophies = Trophy . objects . get_trophies ( review_request ) <EOL> self . assertEqual ( trophies [ <NUM_LIT:0> ] . category , category ) <EOL> self . assertTrue ( <EOL> trophies [ <NUM_LIT:0> ] . review_request . extra_data [ '<STR_LIT>' ] ) <EOL> def test_is_milestone_trophy_awarded_for_new_review_request ( self ) : <EOL> """<STR_LIT>""" <EOL> user1 = User . objects . get ( username = '<STR_LIT>' ) <EOL> category = '<STR_LIT>' <EOL> review_request = self . create_review_request ( publish = True , id = <NUM_LIT:1000> , <EOL> submitter = user1 ) <EOL> trophies = Trophy . objects . compute_trophies ( review_request ) <EOL> self . assertEqual ( trophies [ <NUM_LIT:0> ] . category , category ) <EOL> self . assertTrue ( <EOL> trophies [ <NUM_LIT:0> ] . review_request . extra_data [ '<STR_LIT>' ] ) <EOL> def test_is_milestone_trophy_awarded_for_older_review_request ( self ) : <EOL> """<STR_LIT>""" <EOL> user1 = User . objects . get ( username = '<STR_LIT>' ) <EOL> category = '<STR_LIT>' <EOL> review_request = self . create_review_request ( publish = True , id = <NUM_LIT> , <EOL> submitter = user1 ) <EOL> del review_request . extra_data [ '<STR_LIT>' ] <EOL> trophies = Trophy . objects . compute_trophies ( review_request ) <EOL> self . assertEqual ( trophies [ <NUM_LIT:0> ] . category , category ) <EOL> self . assertTrue ( <EOL> trophies [ <NUM_LIT:0> ] . review_request . extra_data [ '<STR_LIT>' ] ) <EOL> def test_is_no_trophy_awarded ( self ) : <EOL> """<STR_LIT>""" <EOL> user1 = User . objects . get ( username = '<STR_LIT>' ) <EOL> review_request = self . create_review_request ( publish = True , id = <NUM_LIT> , <EOL> submitter = user1 ) <EOL> trophies = Trophy . objects . compute_trophies ( review_request ) <EOL> self . assertFalse ( trophies ) <EOL> class SandboxAuthBackend ( AuthBackend ) : <EOL> """<STR_LIT>""" <EOL> backend_id = '<STR_LIT>' <EOL> name = '<STR_LIT:test>' <EOL> supports_change_name = True <EOL> supports_change_email = True <EOL> supports_change_password = True <EOL> def authenticate ( self , username , password ) : <EOL> """<STR_LIT>""" <EOL> raise Exception <EOL> def update_password ( self , user , password ) : <EOL> """<STR_LIT>""" <EOL> raise Exception <EOL> def update_name ( self , user ) : <EOL> """<STR_LIT>""" <EOL> raise Exception <EOL> def update_email ( self , user ) : <EOL> """<STR_LIT>""" <EOL> raise Exception <EOL> class SandboxTests ( SpyAgency , TestCase ) : <EOL> """<STR_LIT>""" <EOL> def setUp ( self ) : <EOL> """<STR_LIT>""" <EOL> super ( SandboxTests , self ) . setUp ( ) <EOL> self . factory = RequestFactory ( ) <EOL> self . request = self . factory . get ( '<STR_LIT:test>' ) <EOL> self . user = User . objects . create_user ( username = '<STR_LIT>' , email = '<STR_LIT>' , <EOL> password = '<STR_LIT:password>' ) <EOL> self . profile = Profile . objects . get_or_create ( user = self . user ) <EOL> self . spy_on ( get_enabled_auth_backends , <EOL> call_fake = lambda : [ SandboxAuthBackend ( ) ] ) <EOL> self . spy_on ( messages . add_message , <EOL> call_fake = lambda x , y , z : None ) <EOL> def tearDown ( self ) : <EOL> """<STR_LIT>""" <EOL> super ( SandboxTests , self ) . tearDown ( ) <EOL> def test_authenticate_auth_backend ( self ) : <EOL> """<STR_LIT>""" <EOL> form = ChangePasswordForm ( page = None , request = self . request , <EOL> user = self . user ) <EOL> form . cleaned_data = { <EOL> '<STR_LIT>' : self . user . password , <EOL> } <EOL> self . spy_on ( SandboxAuthBackend . authenticate ) <EOL> self . assertRaisesMessage ( <EOL> ValidationError , <EOL> '<STR_LIT>' <EOL> '<STR_LIT>' , <EOL> lambda : form . clean_old_password ( ) ) <EOL> self . assertTrue ( SandboxAuthBackend . authenticate . called ) <EOL> def test_update_password_auth_backend ( self ) : <EOL> """<STR_LIT>""" <EOL> form = ChangePasswordForm ( page = None , request = self . request , <EOL> user = self . user ) <EOL> form . cleaned_data = { <EOL> '<STR_LIT>' : self . user . password , <EOL> '<STR_LIT>' : '<STR_LIT>' , <EOL> '<STR_LIT>' : '<STR_LIT>' , <EOL> } <EOL> self . spy_on ( SandboxAuthBackend . update_password ) <EOL> form . save ( ) <EOL> self . assertTrue ( SandboxAuthBackend . update_password . called ) <EOL> def test_update_name_auth_backend ( self ) : <EOL> """<STR_LIT>""" <EOL> form = ProfileForm ( page = None , request = self . request , user = self . user ) <EOL> form . cleaned_data = { <EOL> '<STR_LIT>' : '<STR_LIT>' , <EOL> '<STR_LIT>' : '<STR_LIT>' , <EOL> '<STR_LIT:email>' : '<STR_LIT>' , <EOL> '<STR_LIT>' : '<STR_LIT>' , <EOL> } <EOL> self . user . email = '<STR_LIT>' <EOL> self . spy_on ( SandboxAuthBackend . update_name ) <EOL> form . save ( ) <EOL> self . assertTrue ( SandboxAuthBackend . update_name . called ) <EOL> def test_update_email_auth_backend ( self ) : <EOL> """<STR_LIT>""" <EOL> form = ProfileForm ( page = None , request = self . request , user = self . user ) <EOL> form . cleaned_data = { <EOL> '<STR_LIT>' : '<STR_LIT>' , <EOL> '<STR_LIT>' : '<STR_LIT>' , <EOL> '<STR_LIT:email>' : '<STR_LIT>' , <EOL> '<STR_LIT>' : '<STR_LIT>' , <EOL> } <EOL> self . user . first_name = '<STR_LIT>' <EOL> self . user . last_name = '<STR_LIT>' <EOL> self . spy_on ( SandboxAuthBackend . update_email ) <EOL> form . save ( ) <EOL> self . assertTrue ( SandboxAuthBackend . update_email . called ) </s>
