<s> import json <EOL> import sys <EOL> import warnings <EOL> from django . conf import settings <EOL> from django . contrib . auth import get_user_model <EOL> from django . contrib . auth . models import AnonymousUser , Permission <EOL> from django . contrib . sites . models import Site <EOL> from django . core . cache import cache <EOL> from django . core . exceptions import ObjectDoesNotExist <EOL> from django . core . urlresolvers import reverse <EOL> from django . template import engines <EOL> from django . template . context import Context <EOL> from django . test import testcases <EOL> from django . test . client import RequestFactory <EOL> from django . utils . timezone import now <EOL> from django . utils . translation import activate <EOL> from django . utils . six . moves . urllib . parse import unquote , urljoin <EOL> from menus . menu_pool import menu_pool <EOL> from cms . models import Page <EOL> from cms . test_utils . util . context_managers import UserLoginContext <EOL> from cms . utils . compat import DJANGO_1_8 <EOL> from cms . utils . permissions import set_current_user <EOL> URL_CMS_PAGE = "<STR_LIT>" <EOL> URL_CMS_PAGE_ADD = urljoin ( URL_CMS_PAGE , "<STR_LIT>" ) <EOL> URL_CMS_PAGE_CHANGE_BASE = urljoin ( URL_CMS_PAGE , "<STR_LIT>" ) <EOL> if DJANGO_1_8 : <EOL> URL_CMS_PAGE_CHANGE = URL_CMS_PAGE_CHANGE_BASE <EOL> else : <EOL> URL_CMS_PAGE_CHANGE = urljoin ( URL_CMS_PAGE_CHANGE_BASE , "<STR_LIT>" ) <EOL> URL_CMS_PAGE_ADVANCED_CHANGE = urljoin ( URL_CMS_PAGE , "<STR_LIT>" ) <EOL> URL_CMS_PAGE_PERMISSION_CHANGE = urljoin ( URL_CMS_PAGE , "<STR_LIT>" ) <EOL> URL_CMS_PAGE_PERMISSIONS = urljoin ( URL_CMS_PAGE , "<STR_LIT>" ) <EOL> URL_CMS_PAGE_PUBLISHED = urljoin ( URL_CMS_PAGE , "<STR_LIT>" ) <EOL> URL_CMS_PAGE_MOVE = urljoin ( URL_CMS_PAGE , "<STR_LIT>" ) <EOL> URL_CMS_PAGE_CHANGE_LANGUAGE = URL_CMS_PAGE_CHANGE + "<STR_LIT>" <EOL> URL_CMS_PAGE_CHANGE_TEMPLATE = urljoin ( URL_CMS_PAGE_CHANGE , "<STR_LIT>" ) <EOL> URL_CMS_PAGE_PUBLISH = urljoin ( URL_CMS_PAGE_CHANGE_BASE , "<STR_LIT>" ) <EOL> URL_CMS_PAGE_DELETE = urljoin ( URL_CMS_PAGE_CHANGE_BASE , "<STR_LIT>" ) <EOL> URL_CMS_PLUGIN_ADD = urljoin ( URL_CMS_PAGE , "<STR_LIT>" ) <EOL> URL_CMS_PLUGIN_EDIT = urljoin ( URL_CMS_PAGE , "<STR_LIT>" ) <EOL> URL_CMS_PLUGIN_MOVE = urljoin ( URL_CMS_PAGE , "<STR_LIT>" ) <EOL> URL_CMS_PLUGIN_PAGE_MOVE = urljoin ( URL_CMS_PAGE_CHANGE_BASE , "<STR_LIT>" ) <EOL> URL_CMS_PLUGIN_PAGE_ADD = urljoin ( URL_CMS_PAGE_CHANGE_BASE , "<STR_LIT>" ) <EOL> URL_CMS_PLUGIN_REMOVE = urljoin ( URL_CMS_PAGE , "<STR_LIT>" ) <EOL> URL_CMS_PLUGIN_DELETE = urljoin ( URL_CMS_PAGE , "<STR_LIT>" ) <EOL> URL_CMS_PLUGINS_COPY = urljoin ( URL_CMS_PAGE , "<STR_LIT>" ) <EOL> URL_CMS_TRANSLATION_DELETE = urljoin ( URL_CMS_PAGE_CHANGE_BASE , "<STR_LIT>" ) <EOL> URL_CMS_USERSETTINGS = "<STR_LIT>" <EOL> URL_CMS_PAGE_HISTORY = urljoin ( URL_CMS_PAGE_CHANGE_BASE , "<STR_LIT>" ) <EOL> URL_CMS_PLUGIN_HISTORY_EDIT = urljoin ( URL_CMS_PAGE_HISTORY , "<STR_LIT>" ) <EOL> class _Warning ( object ) : <EOL> def __init__ ( self , message , category , filename , lineno ) : <EOL> self . message = message <EOL> self . category = category <EOL> self . filename = filename <EOL> self . lineno = lineno <EOL> def _collectWarnings ( observeWarning , f , * args , ** kwargs ) : <EOL> def showWarning ( message , category , filename , lineno , file = None , line = None ) : <EOL> assert isinstance ( message , Warning ) <EOL> observeWarning ( _Warning ( <EOL> message . args [ <NUM_LIT:0> ] , category , filename , lineno ) ) <EOL> for v in sys . modules . values ( ) : <EOL> if v is not None : <EOL> try : <EOL> v . __warningregistry__ = None <EOL> except : <EOL> pass <EOL> origFilters = warnings . filters [ : ] <EOL> origShow = warnings . showwarning <EOL> warnings . simplefilter ( '<STR_LIT>' ) <EOL> try : <EOL> warnings . showwarning = showWarning <EOL> result = f ( * args , ** kwargs ) <EOL> finally : <EOL> warnings . filters [ : ] = origFilters <EOL> warnings . showwarning = origShow <EOL> return result <EOL> class BaseCMSTestCase ( object ) : <EOL> counter = <NUM_LIT:1> <EOL> def _fixture_setup ( self ) : <EOL> super ( BaseCMSTestCase , self ) . _fixture_setup ( ) <EOL> self . create_fixtures ( ) <EOL> activate ( "<STR_LIT>" ) <EOL> def create_fixtures ( self ) : <EOL> pass <EOL> def _post_teardown ( self ) : <EOL> menu_pool . clear ( ) <EOL> cache . clear ( ) <EOL> super ( BaseCMSTestCase , self ) . _post_teardown ( ) <EOL> set_current_user ( None ) <EOL> def login_user_context ( self , user ) : <EOL> return UserLoginContext ( self , user ) <EOL> def _create_user ( self , username , is_staff = False , is_superuser = False , <EOL> is_active = True , add_default_permissions = False , permissions = None ) : <EOL> """<STR_LIT>""" <EOL> User = get_user_model ( ) <EOL> fields = dict ( email = username + '<STR_LIT>' , last_login = now ( ) , <EOL> is_staff = is_staff , is_active = is_active , is_superuser = is_superuser <EOL> ) <EOL> if ( get_user_model ( ) . USERNAME_FIELD != '<STR_LIT:email>' ) : <EOL> fields [ get_user_model ( ) . USERNAME_FIELD ] = username <EOL> user = User ( ** fields ) <EOL> user . set_password ( getattr ( user , get_user_model ( ) . USERNAME_FIELD ) ) <EOL> user . save ( ) <EOL> if is_staff and not is_superuser and add_default_permissions : <EOL> user . user_permissions . add ( Permission . objects . get ( codename = '<STR_LIT>' ) ) <EOL> user . user_permissions . add ( Permission . objects . get ( codename = '<STR_LIT>' ) ) <EOL> user . user_permissions . add ( Permission . objects . get ( codename = '<STR_LIT>' ) ) <EOL> user . user_permissions . add ( Permission . objects . get ( codename = '<STR_LIT>' ) ) <EOL> user . user_permissions . add ( Permission . objects . get ( codename = '<STR_LIT>' ) ) <EOL> user . user_permissions . add ( Permission . objects . get ( codename = '<STR_LIT>' ) ) <EOL> user . user_permissions . add ( Permission . objects . get ( codename = '<STR_LIT>' ) ) <EOL> if is_staff and not is_superuser and permissions : <EOL> for permission in permissions : <EOL> user . user_permissions . add ( Permission . objects . get ( codename = permission ) ) <EOL> return user <EOL> def get_superuser ( self ) : <EOL> try : <EOL> query = dict ( ) <EOL> if get_user_model ( ) . USERNAME_FIELD != "<STR_LIT:email>" : <EOL> query [ get_user_model ( ) . USERNAME_FIELD ] = "<STR_LIT>" <EOL> else : <EOL> query [ get_user_model ( ) . USERNAME_FIELD ] = "<STR_LIT>" <EOL> admin = get_user_model ( ) . objects . get ( ** query ) <EOL> except get_user_model ( ) . DoesNotExist : <EOL> admin = self . _create_user ( "<STR_LIT>" , is_staff = True , is_superuser = True ) <EOL> return admin <EOL> def get_staff_user_with_no_permissions ( self ) : <EOL> """<STR_LIT>""" <EOL> staff = self . _create_user ( "<STR_LIT>" , is_staff = True , is_superuser = False ) <EOL> return staff <EOL> def get_staff_user_with_std_permissions ( self ) : <EOL> """<STR_LIT>""" <EOL> staff = self . _create_user ( "<STR_LIT>" , is_staff = True , is_superuser = False , <EOL> add_default_permissions = True ) <EOL> return staff <EOL> def get_standard_user ( self ) : <EOL> """<STR_LIT>""" <EOL> standard = self . _create_user ( "<STR_LIT>" , is_staff = False , is_superuser = False ) <EOL> return standard <EOL> def get_new_page_data ( self , parent_id = '<STR_LIT>' ) : <EOL> page_data = { <EOL> '<STR_LIT:title>' : '<STR_LIT>' % self . counter , <EOL> '<STR_LIT>' : '<STR_LIT>' % self . counter , <EOL> '<STR_LIT>' : settings . LANGUAGES [ <NUM_LIT:0> ] [ <NUM_LIT:0> ] , <EOL> '<STR_LIT>' : '<STR_LIT>' , <EOL> '<STR_LIT>' : parent_id , <EOL> '<STR_LIT>' : <NUM_LIT:1> , <EOL> '<STR_LIT>' : <NUM_LIT:0> , <EOL> '<STR_LIT>' : <NUM_LIT:0> , <EOL> '<STR_LIT>' : <NUM_LIT:0> , <EOL> '<STR_LIT>' : <NUM_LIT:0> , <EOL> '<STR_LIT>' : <NUM_LIT:0> , <EOL> '<STR_LIT>' : <NUM_LIT:0> <EOL> } <EOL> self . counter += <NUM_LIT:1> <EOL> return page_data <EOL> def get_new_page_data_dbfields ( self , parent = None , site = None , <EOL> language = None , <EOL> template = '<STR_LIT>' , ) : <EOL> page_data = { <EOL> '<STR_LIT:title>' : '<STR_LIT>' % self . counter , <EOL> '<STR_LIT>' : '<STR_LIT>' % self . counter , <EOL> '<STR_LIT>' : settings . LANGUAGES [ <NUM_LIT:0> ] [ <NUM_LIT:0> ] if not language else language , <EOL> '<STR_LIT>' : template , <EOL> '<STR_LIT>' : parent if parent else None , <EOL> '<STR_LIT>' : site if site else Site . objects . get_current ( ) , <EOL> } <EOL> self . counter = self . counter + <NUM_LIT:1> <EOL> return page_data <EOL> def get_pagedata_from_dbfields ( self , page_data ) : <EOL> """<STR_LIT>""" <EOL> page_data [ '<STR_LIT>' ] = page_data [ '<STR_LIT>' ] . id <EOL> page_data [ '<STR_LIT>' ] = page_data [ '<STR_LIT>' ] . id if page_data [ '<STR_LIT>' ] else '<STR_LIT>' <EOL> page_data [ '<STR_LIT>' ] = <NUM_LIT:0> <EOL> page_data [ '<STR_LIT>' ] = <NUM_LIT:0> <EOL> page_data [ '<STR_LIT>' ] = <NUM_LIT:0> <EOL> page_data [ '<STR_LIT>' ] = <NUM_LIT:0> <EOL> page_data [ '<STR_LIT>' ] = <NUM_LIT:0> <EOL> page_data [ '<STR_LIT>' ] = <NUM_LIT:0> <EOL> return page_data <EOL> def print_page_structure ( self , qs ) : <EOL> """<STR_LIT>""" <EOL> for page in qs . order_by ( '<STR_LIT:path>' ) : <EOL> ident = "<STR_LIT:U+0020>" * page . level <EOL> print ( u"<STR_LIT>" % ( ident , page , <EOL> page . pk , page . path , page . depth , page . numchild ) ) <EOL> def print_node_structure ( self , nodes , * extra ) : <EOL> def _rec ( nodes , level = <NUM_LIT:0> ) : <EOL> ident = level * '<STR_LIT:U+0020>' <EOL> for node in nodes : <EOL> raw_attrs = [ ( bit , getattr ( node , bit , node . attr . get ( bit , "<STR_LIT>" ) ) ) for bit in extra ] <EOL> attrs = '<STR_LIT:U+002CU+0020>' . join ( [ '<STR_LIT>' % data for data in raw_attrs ] ) <EOL> print ( u"<STR_LIT>" % ( ident , node . title , attrs ) ) <EOL> _rec ( node . children , level + <NUM_LIT:1> ) <EOL> _rec ( nodes ) <EOL> def assertObjectExist ( self , qs , ** filter ) : <EOL> try : <EOL> return qs . get ( ** filter ) <EOL> except ObjectDoesNotExist : <EOL> pass <EOL> raise self . failureException ( "<STR_LIT>" % filter ) <EOL> def assertObjectDoesNotExist ( self , qs , ** filter ) : <EOL> try : <EOL> qs . get ( ** filter ) <EOL> except ObjectDoesNotExist : <EOL> return <EOL> raise self . failureException ( "<STR_LIT>" % filter ) <EOL> def copy_page ( self , page , target_page , position = <NUM_LIT:0> ) : <EOL> from cms . utils . page import get_available_slug <EOL> data = { <EOL> '<STR_LIT>' : position , <EOL> '<STR_LIT:target>' : target_page . pk , <EOL> '<STR_LIT>' : <NUM_LIT:1> , <EOL> '<STR_LIT>' : '<STR_LIT>' , <EOL> '<STR_LIT>' : '<STR_LIT>' , <EOL> } <EOL> response = self . client . post ( URL_CMS_PAGE + "<STR_LIT>" % page . pk , data ) <EOL> self . assertEqual ( response . status_code , <NUM_LIT:200> ) <EOL> title = page . title_set . all ( ) [ <NUM_LIT:0> ] <EOL> copied_slug = get_available_slug ( title ) <EOL> parent = target_page <EOL> copied_page = self . assertObjectExist ( Page . objects , title_set__slug = copied_slug , parent = parent ) <EOL> expected = { "<STR_LIT:id>" : copied_page . pk } <EOL> self . assertEqual ( json . loads ( response . content . decode ( '<STR_LIT:utf8>' ) ) , expected ) <EOL> return copied_page <EOL> def move_page ( self , page , target_page , position = "<STR_LIT>" ) : <EOL> page . move_page ( target_page , position ) <EOL> return self . reload_page ( page ) <EOL> def reload_page ( self , page ) : <EOL> """<STR_LIT>""" <EOL> return self . reload ( page ) <EOL> def reload ( self , obj ) : <EOL> return obj . __class__ . objects . get ( pk = obj . pk ) <EOL> def get_pages_root ( self ) : <EOL> return unquote ( reverse ( "<STR_LIT>" ) ) <EOL> def get_context ( self , path = None , page = None ) : <EOL> if not path : <EOL> path = self . get_pages_root ( ) <EOL> context = { } <EOL> request = self . get_request ( path , page = page ) <EOL> context [ '<STR_LIT>' ] = request <EOL> return Context ( context ) <EOL> def get_request ( self , path = None , language = None , post_data = None , enforce_csrf_checks = False , page = None ) : <EOL> factory = RequestFactory ( ) <EOL> if not path : <EOL> path = self . get_pages_root ( ) <EOL> if not language : <EOL> if settings . USE_I18N : <EOL> language = settings . LANGUAGES [ <NUM_LIT:0> ] [ <NUM_LIT:0> ] <EOL> else : <EOL> language = settings . LANGUAGE_CODE <EOL> if post_data : <EOL> request = factory . post ( path , post_data ) <EOL> else : <EOL> request = factory . get ( path ) <EOL> request . session = self . client . session <EOL> request . user = getattr ( self , '<STR_LIT:user>' , AnonymousUser ( ) ) <EOL> request . LANGUAGE_CODE = language <EOL> request . _dont_enforce_csrf_checks = not enforce_csrf_checks <EOL> if page : <EOL> request . current_page = page <EOL> else : <EOL> request . current_page = None <EOL> class MockStorage ( object ) : <EOL> def __len__ ( self ) : <EOL> return <NUM_LIT:0> <EOL> def __iter__ ( self ) : <EOL> return iter ( [ ] ) <EOL> def add ( self , level , message , extra_tags = '<STR_LIT>' ) : <EOL> pass <EOL> def update ( self , response ) : <EOL> pass <EOL> request . _messages = MockStorage ( ) <EOL> return request <EOL> def check_published_page_attributes ( self , page ) : <EOL> public_page = page . publisher_public <EOL> if page . parent : <EOL> self . assertEqual ( page . parent_id , public_page . parent . publisher_draft . id ) <EOL> self . assertEqual ( page . depth , public_page . depth ) <EOL> draft_siblings = list ( Page . objects . filter ( parent_id = page . parent_id , publisher_is_draft = True ) . order_by ( '<STR_LIT:path>' ) ) <EOL> public_siblings = list ( Page . objects . filter ( parent_id = public_page . parent_id , publisher_is_draft = False ) . order_by ( '<STR_LIT:path>' ) ) <EOL> skip = <NUM_LIT:0> <EOL> for i , sibling in enumerate ( draft_siblings ) : <EOL> if not sibling . publisher_public_id : <EOL> skip += <NUM_LIT:1> <EOL> continue <EOL> self . assertEqual ( sibling . id , <EOL> public_siblings [ i - skip ] . publisher_draft . id ) <EOL> def failUnlessWarns ( self , category , message , f , * args , ** kwargs ) : <EOL> warningsShown = [ ] <EOL> result = _collectWarnings ( warningsShown . append , f , * args , ** kwargs ) <EOL> if not warningsShown : <EOL> self . fail ( "<STR_LIT>" ) <EOL> first = warningsShown [ <NUM_LIT:0> ] <EOL> for other in warningsShown [ <NUM_LIT:1> : ] : <EOL> if ( ( other . message , other . category ) <EOL> != ( first . message , first . category ) ) : <EOL> self . fail ( "<STR_LIT>" ) <EOL> self . assertEqual ( first . message , message ) <EOL> self . assertTrue ( first . category is category ) <EOL> return result <EOL> assertWarns = failUnlessWarns <EOL> def render_template_obj ( self , template , context , request ) : <EOL> template_obj = engines [ '<STR_LIT>' ] . from_string ( template ) <EOL> return template_obj . render ( context , request ) <EOL> def apphook_clear ( self ) : <EOL> from cms . apphook_pool import apphook_pool <EOL> for name , label in list ( apphook_pool . get_apphooks ( ) ) : <EOL> if apphook_pool . apps [ name ] . __class__ . __module__ in sys . modules : <EOL> del sys . modules [ apphook_pool . apps [ name ] . __class__ . __module__ ] <EOL> apphook_pool . clear ( ) <EOL> class CMSTestCase ( BaseCMSTestCase , testcases . TestCase ) : <EOL> pass <EOL> class TransactionCMSTestCase ( BaseCMSTestCase , testcases . TransactionTestCase ) : <EOL> pass </s>
