<s> from __future__ import with_statement <EOL> import copy <EOL> from django . conf import compat_patch_logging_config <EOL> from django . core import mail <EOL> from django . test import TestCase , RequestFactory <EOL> from django . test . utils import override_settings <EOL> from django . utils . log import CallbackFilter , RequireDebugFalse , getLogger <EOL> OLD_LOGGING = { <EOL> '<STR_LIT:version>' : <NUM_LIT:1> , <EOL> '<STR_LIT>' : False , <EOL> '<STR_LIT>' : { <EOL> '<STR_LIT>' : { <EOL> '<STR_LIT>' : '<STR_LIT>' , <EOL> '<STR_LIT:class>' : '<STR_LIT>' <EOL> } <EOL> } , <EOL> '<STR_LIT>' : { <EOL> '<STR_LIT>' : { <EOL> '<STR_LIT>' : [ '<STR_LIT>' ] , <EOL> '<STR_LIT>' : '<STR_LIT>' , <EOL> '<STR_LIT>' : True , <EOL> } , <EOL> } <EOL> } <EOL> class PatchLoggingConfigTest ( TestCase ) : <EOL> """<STR_LIT>""" <EOL> def test_filter_added ( self ) : <EOL> """<STR_LIT>""" <EOL> config = copy . deepcopy ( OLD_LOGGING ) <EOL> compat_patch_logging_config ( config ) <EOL> self . assertEqual ( <EOL> config [ "<STR_LIT>" ] [ "<STR_LIT>" ] [ "<STR_LIT>" ] , <EOL> [ '<STR_LIT>' ] ) <EOL> def test_filter_configuration ( self ) : <EOL> """<STR_LIT>""" <EOL> config = copy . deepcopy ( OLD_LOGGING ) <EOL> compat_patch_logging_config ( config ) <EOL> flt = config [ "<STR_LIT>" ] [ "<STR_LIT>" ] <EOL> self . assertEqual ( flt [ "<STR_LIT>" ] , "<STR_LIT>" ) <EOL> def test_require_debug_false_filter ( self ) : <EOL> """<STR_LIT>""" <EOL> filter_ = RequireDebugFalse ( ) <EOL> with self . settings ( DEBUG = True ) : <EOL> self . assertEqual ( filter_ . filter ( "<STR_LIT>" ) , False ) <EOL> with self . settings ( DEBUG = False ) : <EOL> self . assertEqual ( filter_ . filter ( "<STR_LIT>" ) , True ) <EOL> def test_no_patch_if_filters_key_exists ( self ) : <EOL> """<STR_LIT>""" <EOL> config = copy . deepcopy ( OLD_LOGGING ) <EOL> config [ "<STR_LIT>" ] [ "<STR_LIT>" ] [ "<STR_LIT>" ] = [ ] <EOL> new_config = copy . deepcopy ( config ) <EOL> compat_patch_logging_config ( new_config ) <EOL> self . assertEqual ( config , new_config ) <EOL> def test_no_patch_if_no_mail_admins_handler ( self ) : <EOL> """<STR_LIT>""" <EOL> config = copy . deepcopy ( OLD_LOGGING ) <EOL> config [ "<STR_LIT>" ] . pop ( "<STR_LIT>" ) <EOL> new_config = copy . deepcopy ( config ) <EOL> compat_patch_logging_config ( new_config ) <EOL> self . assertEqual ( config , new_config ) <EOL> class CallbackFilterTest ( TestCase ) : <EOL> def test_sense ( self ) : <EOL> f_false = CallbackFilter ( lambda r : False ) <EOL> f_true = CallbackFilter ( lambda r : True ) <EOL> self . assertEqual ( f_false . filter ( "<STR_LIT>" ) , False ) <EOL> self . assertEqual ( f_true . filter ( "<STR_LIT>" ) , True ) <EOL> def test_passes_on_record ( self ) : <EOL> collector = [ ] <EOL> def _callback ( record ) : <EOL> collector . append ( record ) <EOL> return True <EOL> f = CallbackFilter ( _callback ) <EOL> f . filter ( "<STR_LIT>" ) <EOL> self . assertEqual ( collector , [ "<STR_LIT>" ] ) <EOL> class AdminEmailHandlerTest ( TestCase ) : <EOL> def get_admin_email_handler ( self , logger ) : <EOL> admin_email_handler = [ <EOL> h for h in logger . handlers <EOL> if h . __class__ . __name__ == "<STR_LIT>" <EOL> ] [ <NUM_LIT:0> ] <EOL> return admin_email_handler <EOL> @ override_settings ( <EOL> ADMINS = ( ( '<STR_LIT>' , '<STR_LIT>' ) , ) , <EOL> EMAIL_SUBJECT_PREFIX = '<STR_LIT>' <EOL> ) <EOL> def test_accepts_args ( self ) : <EOL> """<STR_LIT>""" <EOL> message = "<STR_LIT>" <EOL> token1 = '<STR_LIT>' <EOL> token2 = '<STR_LIT>' <EOL> logger = getLogger ( '<STR_LIT>' ) <EOL> admin_email_handler = self . get_admin_email_handler ( logger ) <EOL> orig_filters = admin_email_handler . filters <EOL> try : <EOL> admin_email_handler . filters = [ ] <EOL> logger . error ( message , token1 , token2 ) <EOL> self . assertEqual ( len ( mail . outbox ) , <NUM_LIT:1> ) <EOL> self . assertEqual ( mail . outbox [ <NUM_LIT:0> ] . to , [ '<STR_LIT>' ] ) <EOL> self . assertEqual ( mail . outbox [ <NUM_LIT:0> ] . subject , <EOL> "<STR_LIT>" ) <EOL> finally : <EOL> admin_email_handler . filters = orig_filters <EOL> @ override_settings ( <EOL> ADMINS = ( ( '<STR_LIT>' , '<STR_LIT>' ) , ) , <EOL> EMAIL_SUBJECT_PREFIX = '<STR_LIT>' , <EOL> INTERNAL_IPS = ( '<STR_LIT:127.0.0.1>' , ) , <EOL> ) <EOL> def test_accepts_args_and_request ( self ) : <EOL> """<STR_LIT>""" <EOL> message = "<STR_LIT>" <EOL> token1 = '<STR_LIT>' <EOL> token2 = '<STR_LIT>' <EOL> logger = getLogger ( '<STR_LIT>' ) <EOL> admin_email_handler = self . get_admin_email_handler ( logger ) <EOL> orig_filters = admin_email_handler . filters <EOL> try : <EOL> admin_email_handler . filters = [ ] <EOL> rf = RequestFactory ( ) <EOL> request = rf . get ( '<STR_LIT:/>' ) <EOL> logger . error ( message , token1 , token2 , <EOL> extra = { <EOL> '<STR_LIT>' : <NUM_LIT> , <EOL> '<STR_LIT>' : request , <EOL> } <EOL> ) <EOL> self . assertEqual ( len ( mail . outbox ) , <NUM_LIT:1> ) <EOL> self . assertEqual ( mail . outbox [ <NUM_LIT:0> ] . to , [ '<STR_LIT>' ] ) <EOL> self . assertEqual ( mail . outbox [ <NUM_LIT:0> ] . subject , <EOL> "<STR_LIT>" ) <EOL> finally : <EOL> admin_email_handler . filters = orig_filters <EOL> @ override_settings ( <EOL> ADMINS = ( ( '<STR_LIT>' , '<STR_LIT>' ) , ) , <EOL> EMAIL_SUBJECT_PREFIX = '<STR_LIT>' , <EOL> DEBUG = False , <EOL> ) <EOL> def test_subject_accepts_newlines ( self ) : <EOL> """<STR_LIT>""" <EOL> message = u'<STR_LIT>' <EOL> expected_subject = u'<STR_LIT>' <EOL> self . assertEqual ( len ( mail . outbox ) , <NUM_LIT:0> ) <EOL> logger = getLogger ( '<STR_LIT>' ) <EOL> logger . error ( message ) <EOL> self . assertEqual ( len ( mail . outbox ) , <NUM_LIT:1> ) <EOL> self . assertFalse ( '<STR_LIT:\n>' in mail . outbox [ <NUM_LIT:0> ] . subject ) <EOL> self . assertFalse ( '<STR_LIT:\r>' in mail . outbox [ <NUM_LIT:0> ] . subject ) <EOL> self . assertEqual ( mail . outbox [ <NUM_LIT:0> ] . subject , expected_subject ) <EOL> @ override_settings ( <EOL> ADMINS = ( ( '<STR_LIT>' , '<STR_LIT>' ) , ) , <EOL> EMAIL_SUBJECT_PREFIX = '<STR_LIT>' , <EOL> DEBUG = False , <EOL> ) <EOL> def test_truncate_subject ( self ) : <EOL> """<STR_LIT>""" <EOL> message = '<STR_LIT:a>' * <NUM_LIT:1000> <EOL> expected_subject = '<STR_LIT>' + '<STR_LIT:a>' * <NUM_LIT> <EOL> self . assertEqual ( len ( mail . outbox ) , <NUM_LIT:0> ) <EOL> logger = getLogger ( '<STR_LIT>' ) <EOL> logger . error ( message ) <EOL> self . assertEqual ( len ( mail . outbox ) , <NUM_LIT:1> ) <EOL> self . assertEqual ( mail . outbox [ <NUM_LIT:0> ] . subject , expected_subject ) </s>
