<s> from django import http <EOL> from django . conf import settings , global_settings <EOL> from django . contrib . messages import constants , utils , get_level , set_level <EOL> from django . contrib . messages . api import MessageFailure <EOL> from django . contrib . messages . storage import default_storage , base <EOL> from django . contrib . messages . storage . base import Message <EOL> from django . core . urlresolvers import reverse <EOL> from django . test import TestCase <EOL> from django . test . utils import override_settings <EOL> from django . utils . translation import ugettext_lazy <EOL> from django . utils . unittest import skipIf <EOL> def skipUnlessAuthIsInstalled ( func ) : <EOL> return skipIf ( <EOL> '<STR_LIT>' not in settings . INSTALLED_APPS , <EOL> "<STR_LIT>" ) ( func ) <EOL> def add_level_messages ( storage ) : <EOL> """<STR_LIT>""" <EOL> storage . add ( constants . INFO , '<STR_LIT>' ) <EOL> storage . add ( <NUM_LIT> , '<STR_LIT>' ) <EOL> storage . add ( constants . DEBUG , '<STR_LIT>' , extra_tags = '<STR_LIT>' ) <EOL> storage . add ( constants . WARNING , '<STR_LIT>' ) <EOL> storage . add ( constants . ERROR , '<STR_LIT>' ) <EOL> storage . add ( constants . SUCCESS , '<STR_LIT>' ) <EOL> class override_settings_tags ( override_settings ) : <EOL> def enable ( self ) : <EOL> super ( override_settings_tags , self ) . enable ( ) <EOL> self . old_level_tags = base . LEVEL_TAGS <EOL> base . LEVEL_TAGS = utils . get_level_tags ( ) <EOL> def disable ( self ) : <EOL> super ( override_settings_tags , self ) . disable ( ) <EOL> base . LEVEL_TAGS = self . old_level_tags <EOL> class BaseTest ( TestCase ) : <EOL> storage_class = default_storage <EOL> urls = '<STR_LIT>' <EOL> levels = { <EOL> '<STR_LIT>' : constants . DEBUG , <EOL> '<STR_LIT:info>' : constants . INFO , <EOL> '<STR_LIT:success>' : constants . SUCCESS , <EOL> '<STR_LIT>' : constants . WARNING , <EOL> '<STR_LIT:error>' : constants . ERROR , <EOL> } <EOL> def setUp ( self ) : <EOL> self . settings_override = override_settings_tags ( <EOL> TEMPLATE_DIRS = ( ) , <EOL> TEMPLATE_CONTEXT_PROCESSORS = global_settings . TEMPLATE_CONTEXT_PROCESSORS , <EOL> MESSAGE_TAGS = '<STR_LIT>' , <EOL> MESSAGE_STORAGE = '<STR_LIT>' % ( self . storage_class . __module__ , <EOL> self . storage_class . __name__ ) , <EOL> SESSION_SERIALIZER = '<STR_LIT>' , <EOL> ) <EOL> self . settings_override . enable ( ) <EOL> def tearDown ( self ) : <EOL> self . settings_override . disable ( ) <EOL> def get_request ( self ) : <EOL> return http . HttpRequest ( ) <EOL> def get_response ( self ) : <EOL> return http . HttpResponse ( ) <EOL> def get_storage ( self , data = None ) : <EOL> """<STR_LIT>""" <EOL> storage = self . storage_class ( self . get_request ( ) ) <EOL> storage . _loaded_data = data or [ ] <EOL> return storage <EOL> def test_add ( self ) : <EOL> storage = self . get_storage ( ) <EOL> self . assertFalse ( storage . added_new ) <EOL> storage . add ( constants . INFO , '<STR_LIT>' ) <EOL> self . assertTrue ( storage . added_new ) <EOL> storage . add ( constants . INFO , '<STR_LIT>' , extra_tags = '<STR_LIT>' ) <EOL> self . assertEqual ( len ( storage ) , <NUM_LIT:2> ) <EOL> def test_add_lazy_translation ( self ) : <EOL> storage = self . get_storage ( ) <EOL> response = self . get_response ( ) <EOL> storage . add ( constants . INFO , ugettext_lazy ( '<STR_LIT>' ) ) <EOL> storage . update ( response ) <EOL> storing = self . stored_messages_count ( storage , response ) <EOL> self . assertEqual ( storing , <NUM_LIT:1> ) <EOL> def test_no_update ( self ) : <EOL> storage = self . get_storage ( ) <EOL> response = self . get_response ( ) <EOL> storage . update ( response ) <EOL> storing = self . stored_messages_count ( storage , response ) <EOL> self . assertEqual ( storing , <NUM_LIT:0> ) <EOL> def test_add_update ( self ) : <EOL> storage = self . get_storage ( ) <EOL> response = self . get_response ( ) <EOL> storage . add ( constants . INFO , '<STR_LIT>' ) <EOL> storage . add ( constants . INFO , '<STR_LIT>' , extra_tags = '<STR_LIT>' ) <EOL> storage . update ( response ) <EOL> storing = self . stored_messages_count ( storage , response ) <EOL> self . assertEqual ( storing , <NUM_LIT:2> ) <EOL> def test_existing_add_read_update ( self ) : <EOL> storage = self . get_existing_storage ( ) <EOL> response = self . get_response ( ) <EOL> storage . add ( constants . INFO , '<STR_LIT>' ) <EOL> list ( storage ) <EOL> storage . update ( response ) <EOL> storing = self . stored_messages_count ( storage , response ) <EOL> self . assertEqual ( storing , <NUM_LIT:0> ) <EOL> def test_existing_read_add_update ( self ) : <EOL> storage = self . get_existing_storage ( ) <EOL> response = self . get_response ( ) <EOL> list ( storage ) <EOL> storage . add ( constants . INFO , '<STR_LIT>' ) <EOL> storage . update ( response ) <EOL> storing = self . stored_messages_count ( storage , response ) <EOL> self . assertEqual ( storing , <NUM_LIT:1> ) <EOL> @ override_settings ( MESSAGE_LEVEL = constants . DEBUG ) <EOL> def test_full_request_response_cycle ( self ) : <EOL> """<STR_LIT>""" <EOL> data = { <EOL> '<STR_LIT>' : [ '<STR_LIT>' % x for x in range ( <NUM_LIT:5> ) ] , <EOL> } <EOL> show_url = reverse ( '<STR_LIT>' ) <EOL> for level in ( '<STR_LIT>' , '<STR_LIT:info>' , '<STR_LIT:success>' , '<STR_LIT>' , '<STR_LIT:error>' ) : <EOL> add_url = reverse ( '<STR_LIT>' , <EOL> args = ( level , ) ) <EOL> response = self . client . post ( add_url , data , follow = True ) <EOL> self . assertRedirects ( response , show_url ) <EOL> self . assertTrue ( '<STR_LIT>' in response . context ) <EOL> messages = [ Message ( self . levels [ level ] , msg ) for msg in <EOL> data [ '<STR_LIT>' ] ] <EOL> self . assertEqual ( list ( response . context [ '<STR_LIT>' ] ) , messages ) <EOL> for msg in data [ '<STR_LIT>' ] : <EOL> self . assertContains ( response , msg ) <EOL> @ override_settings ( MESSAGE_LEVEL = constants . DEBUG ) <EOL> def test_with_template_response ( self ) : <EOL> data = { <EOL> '<STR_LIT>' : [ '<STR_LIT>' % x for x in range ( <NUM_LIT:5> ) ] , <EOL> } <EOL> show_url = reverse ( '<STR_LIT>' ) <EOL> for level in self . levels . keys ( ) : <EOL> add_url = reverse ( '<STR_LIT>' , <EOL> args = ( level , ) ) <EOL> response = self . client . post ( add_url , data , follow = True ) <EOL> self . assertRedirects ( response , show_url ) <EOL> self . assertTrue ( '<STR_LIT>' in response . context ) <EOL> for msg in data [ '<STR_LIT>' ] : <EOL> self . assertContains ( response , msg ) <EOL> response = self . client . get ( show_url ) <EOL> for msg in data [ '<STR_LIT>' ] : <EOL> self . assertNotContains ( response , msg ) <EOL> @ override_settings ( MESSAGE_LEVEL = constants . DEBUG ) <EOL> def test_multiple_posts ( self ) : <EOL> """<STR_LIT>""" <EOL> data = { <EOL> '<STR_LIT>' : [ '<STR_LIT>' % x for x in range ( <NUM_LIT:5> ) ] , <EOL> } <EOL> show_url = reverse ( '<STR_LIT>' ) <EOL> messages = [ ] <EOL> for level in ( '<STR_LIT>' , '<STR_LIT:info>' , '<STR_LIT:success>' , '<STR_LIT>' , '<STR_LIT:error>' ) : <EOL> messages . extend ( [ Message ( self . levels [ level ] , msg ) for msg in <EOL> data [ '<STR_LIT>' ] ] ) <EOL> add_url = reverse ( '<STR_LIT>' , <EOL> args = ( level , ) ) <EOL> self . client . post ( add_url , data ) <EOL> response = self . client . get ( show_url ) <EOL> self . assertTrue ( '<STR_LIT>' in response . context ) <EOL> self . assertEqual ( list ( response . context [ '<STR_LIT>' ] ) , messages ) <EOL> for msg in data [ '<STR_LIT>' ] : <EOL> self . assertContains ( response , msg ) <EOL> @ override_settings ( <EOL> INSTALLED_APPS = filter ( <EOL> lambda app : app != '<STR_LIT>' , settings . INSTALLED_APPS ) , <EOL> MIDDLEWARE_CLASSES = filter ( <EOL> lambda m : '<STR_LIT>' not in m , settings . MIDDLEWARE_CLASSES ) , <EOL> TEMPLATE_CONTEXT_PROCESSORS = filter ( <EOL> lambda p : '<STR_LIT>' not in p , <EOL> settings . TEMPLATE_CONTEXT_PROCESSORS ) , <EOL> MESSAGE_LEVEL = constants . DEBUG <EOL> ) <EOL> def test_middleware_disabled ( self ) : <EOL> """<STR_LIT>""" <EOL> data = { <EOL> '<STR_LIT>' : [ '<STR_LIT>' % x for x in range ( <NUM_LIT:5> ) ] , <EOL> } <EOL> show_url = reverse ( '<STR_LIT>' ) <EOL> for level in ( '<STR_LIT>' , '<STR_LIT:info>' , '<STR_LIT:success>' , '<STR_LIT>' , '<STR_LIT:error>' ) : <EOL> add_url = reverse ( '<STR_LIT>' , <EOL> args = ( level , ) ) <EOL> self . assertRaises ( MessageFailure , self . client . post , add_url , <EOL> data , follow = True ) <EOL> @ override_settings ( <EOL> INSTALLED_APPS = filter ( <EOL> lambda app : app != '<STR_LIT>' , settings . INSTALLED_APPS ) , <EOL> MIDDLEWARE_CLASSES = filter ( <EOL> lambda m : '<STR_LIT>' not in m , settings . MIDDLEWARE_CLASSES ) , <EOL> TEMPLATE_CONTEXT_PROCESSORS = filter ( <EOL> lambda p : '<STR_LIT>' not in p , <EOL> settings . TEMPLATE_CONTEXT_PROCESSORS ) , <EOL> MESSAGE_LEVEL = constants . DEBUG <EOL> ) <EOL> def test_middleware_disabled_fail_silently ( self ) : <EOL> """<STR_LIT>""" <EOL> data = { <EOL> '<STR_LIT>' : [ '<STR_LIT>' % x for x in range ( <NUM_LIT:5> ) ] , <EOL> '<STR_LIT>' : True , <EOL> } <EOL> show_url = reverse ( '<STR_LIT>' ) <EOL> for level in ( '<STR_LIT>' , '<STR_LIT:info>' , '<STR_LIT:success>' , '<STR_LIT>' , '<STR_LIT:error>' ) : <EOL> add_url = reverse ( '<STR_LIT>' , <EOL> args = ( level , ) ) <EOL> response = self . client . post ( add_url , data , follow = True ) <EOL> self . assertRedirects ( response , show_url ) <EOL> self . assertFalse ( '<STR_LIT>' in response . context ) <EOL> def stored_messages_count ( self , storage , response ) : <EOL> """<STR_LIT>""" <EOL> raise NotImplementedError ( '<STR_LIT>' ) <EOL> def test_get ( self ) : <EOL> raise NotImplementedError ( '<STR_LIT>' ) <EOL> def get_existing_storage ( self ) : <EOL> return self . get_storage ( [ Message ( constants . INFO , '<STR_LIT>' ) , <EOL> Message ( constants . INFO , '<STR_LIT>' , <EOL> extra_tags = '<STR_LIT>' ) ] ) <EOL> def test_existing_read ( self ) : <EOL> """<STR_LIT>""" <EOL> storage = self . get_existing_storage ( ) <EOL> self . assertFalse ( storage . used ) <EOL> data = list ( storage ) <EOL> self . assertTrue ( storage . used ) <EOL> self . assertEqual ( data , list ( storage ) ) <EOL> def test_existing_add ( self ) : <EOL> storage = self . get_existing_storage ( ) <EOL> self . assertFalse ( storage . added_new ) <EOL> storage . add ( constants . INFO , '<STR_LIT>' ) <EOL> self . assertTrue ( storage . added_new ) <EOL> def test_default_level ( self ) : <EOL> request = self . get_request ( ) <EOL> self . assertEqual ( get_level ( request ) , constants . INFO ) <EOL> storage = self . get_storage ( ) <EOL> request . _messages = storage <EOL> self . assertEqual ( get_level ( request ) , constants . INFO ) <EOL> add_level_messages ( storage ) <EOL> self . assertEqual ( len ( storage ) , <NUM_LIT:5> ) <EOL> def test_low_level ( self ) : <EOL> request = self . get_request ( ) <EOL> storage = self . storage_class ( request ) <EOL> request . _messages = storage <EOL> self . assertTrue ( set_level ( request , <NUM_LIT:5> ) ) <EOL> self . assertEqual ( get_level ( request ) , <NUM_LIT:5> ) <EOL> add_level_messages ( storage ) <EOL> self . assertEqual ( len ( storage ) , <NUM_LIT:6> ) <EOL> def test_high_level ( self ) : <EOL> request = self . get_request ( ) <EOL> storage = self . storage_class ( request ) <EOL> request . _messages = storage <EOL> self . assertTrue ( set_level ( request , <NUM_LIT:30> ) ) <EOL> self . assertEqual ( get_level ( request ) , <NUM_LIT:30> ) <EOL> add_level_messages ( storage ) <EOL> self . assertEqual ( len ( storage ) , <NUM_LIT:2> ) <EOL> @ override_settings ( MESSAGE_LEVEL = <NUM_LIT> ) <EOL> def test_settings_level ( self ) : <EOL> request = self . get_request ( ) <EOL> storage = self . storage_class ( request ) <EOL> self . assertEqual ( get_level ( request ) , <NUM_LIT> ) <EOL> add_level_messages ( storage ) <EOL> self . assertEqual ( len ( storage ) , <NUM_LIT:3> ) <EOL> def test_tags ( self ) : <EOL> storage = self . get_storage ( ) <EOL> storage . level = <NUM_LIT:0> <EOL> add_level_messages ( storage ) <EOL> tags = [ msg . tags for msg in storage ] <EOL> self . assertEqual ( tags , <EOL> [ '<STR_LIT:info>' , '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT:error>' , <EOL> '<STR_LIT:success>' ] ) <EOL> @ override_settings_tags ( MESSAGE_TAGS = { <EOL> constants . INFO : '<STR_LIT:info>' , <EOL> constants . DEBUG : '<STR_LIT>' , <EOL> constants . WARNING : '<STR_LIT>' , <EOL> constants . ERROR : '<STR_LIT>' , <EOL> <NUM_LIT> : '<STR_LIT>' , <EOL> } <EOL> ) <EOL> def test_custom_tags ( self ) : <EOL> storage = self . get_storage ( ) <EOL> storage . level = <NUM_LIT:0> <EOL> add_level_messages ( storage ) <EOL> tags = [ msg . tags for msg in storage ] <EOL> self . assertEqual ( tags , <EOL> [ '<STR_LIT:info>' , '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT:success>' ] ) </s>
