<s> from __future__ import unicode_literals <EOL> from django . apps import apps <EOL> from django . contrib . auth import get_user_model <EOL> from django . contrib . auth . models import Permission <EOL> from django . core . exceptions import ImproperlyConfigured <EOL> from django . core . exceptions import ValidationError <EOL> from django . core . urlresolvers import reverse <EOL> from django . test import LiveServerTestCase as DjangoLiveServerTestCase <EOL> from django . test import TransactionTestCase as DjangoTransactionTestCase <EOL> from django . test import TestCase as DjangoTestCase <EOL> from django . utils . translation import activate <EOL> from djangobmf . conf import settings <EOL> from djangobmf . demo import FIXTURES <EOL> from djangobmf . sites import site <EOL> from collections import OrderedDict <EOL> import json <EOL> class BaseTestCase ( object ) : <EOL> def setUp ( self ) : <EOL> activate ( '<STR_LIT>' ) <EOL> site . activate ( test = True ) <EOL> super ( BaseTestCase , self ) . setUp ( ) <EOL> def create_user ( self , username , is_staff = False , is_superuser = False , <EOL> is_active = True , permissions = None , create_employee = True ) : <EOL> """<STR_LIT>""" <EOL> user = get_user_model ( ) <EOL> username_field = user . USERNAME_FIELD <EOL> fields = { <EOL> '<STR_LIT:email>' : username + '<STR_LIT>' , <EOL> '<STR_LIT>' : is_staff , <EOL> '<STR_LIT>' : is_active , <EOL> '<STR_LIT>' : is_superuser , <EOL> } <EOL> fields [ username_field ] = username <EOL> user_obj = user ( ** fields ) <EOL> user_obj . set_password ( getattr ( user_obj , username_field ) ) <EOL> user_obj . save ( ) <EOL> try : <EOL> apps . get_model ( settings . CONTRIB_EMPLOYEE ) . objects . create ( user = user_obj ) <EOL> except LookupError : <EOL> pass <EOL> if not is_superuser and permissions : <EOL> for permission in permissions : <EOL> user_obj . user_permissions . add ( Permission . objects . get ( codename = permission ) ) <EOL> return user_obj <EOL> def client_login ( self , username ) : <EOL> self . client . login ( username = username , password = username ) <EOL> class DemoDataMixin ( object ) : <EOL> """<STR_LIT>""" <EOL> fixtures = FIXTURES <EOL> class SuperuserMixin ( object ) : <EOL> """<STR_LIT>""" <EOL> def setUp ( self ) : <EOL> super ( SuperuserMixin , self ) . setUp ( ) <EOL> self . user = self . create_user ( "<STR_LIT>" , is_superuser = True ) <EOL> self . client_login ( "<STR_LIT>" ) <EOL> class ModuleTestFactory ( SuperuserMixin , BaseTestCase ) : <EOL> """<STR_LIT>""" <EOL> app = None <EOL> def setUp ( self ) : <EOL> super ( BaseTestCase , self ) . setUp ( ) <EOL> self . user = self . create_user ( "<STR_LIT>" , is_superuser = True ) <EOL> self . client_login ( "<STR_LIT>" ) <EOL> self . appconf = [ app for app in apps . get_app_configs ( ) if isinstance ( app , self . app ) ] [ <NUM_LIT:0> ] <EOL> self . models = [ m for m in self . appconf . get_models ( ) if m in site . models . values ( ) ] <EOL> def test_module_create ( self ) : <EOL> for model in self . models : <EOL> ns = model . _bmfmeta . namespace_api <EOL> for key , slug , view in site . modules [ model ] . list_creates ( ) : <EOL> url = reverse ( '<STR_LIT>' % ns , kwargs = { <EOL> '<STR_LIT:key>' : key , <EOL> } ) <EOL> response = self . client . get ( url , HTTP_X_REQUESTED_WITH = '<STR_LIT>' ) <EOL> self . assertEqual ( response . status_code , <NUM_LIT:200> ) <EOL> def test_module_report ( self ) : <EOL> for model in self . models : <EOL> ns = model . _bmfmeta . namespace_api <EOL> for obj in model . objects . all ( ) : <EOL> for key , slug , view in site . modules [ model ] . list_reports ( ) : <EOL> url = reverse ( '<STR_LIT>' % ns , kwargs = { <EOL> '<STR_LIT>' : obj . pk , <EOL> '<STR_LIT:key>' : key , <EOL> } ) <EOL> response = self . client . get ( url ) <EOL> self . assertEqual ( response . status_code , <NUM_LIT:200> ) <EOL> def test_module_update ( self ) : <EOL> for model in self . models : <EOL> ns = model . _bmfmeta . namespace_api <EOL> for obj in model . objects . all ( ) : <EOL> url = reverse ( '<STR_LIT>' % ns , kwargs = { <EOL> '<STR_LIT>' : obj . pk , <EOL> } ) <EOL> response = self . client . get ( url , HTTP_X_REQUESTED_WITH = '<STR_LIT>' ) <EOL> self . assertTrue ( response . status_code in [ <NUM_LIT:200> , <NUM_LIT> ] ) <EOL> def test_module_delete ( self ) : <EOL> for model in self . models : <EOL> ns = model . _bmfmeta . namespace_api <EOL> for obj in model . objects . all ( ) : <EOL> url = reverse ( '<STR_LIT>' % ns , kwargs = { <EOL> '<STR_LIT>' : obj . pk , <EOL> } ) <EOL> response = self . client . get ( url , HTTP_X_REQUESTED_WITH = '<STR_LIT>' ) <EOL> self . assertTrue ( response . status_code in [ <NUM_LIT:200> , <NUM_LIT> ] ) <EOL> def test_module_api_data ( self ) : <EOL> for model in self . models : <EOL> url = reverse ( '<STR_LIT>' % settings . APP_LABEL , kwargs = { <EOL> '<STR_LIT>' : model . _meta . app_label , <EOL> '<STR_LIT>' : model . _meta . model_name , <EOL> } ) <EOL> response = self . client . get ( url , HTTP_X_REQUESTED_WITH = '<STR_LIT>' ) <EOL> self . assertEqual ( response . status_code , <NUM_LIT:200> ) <EOL> def test_module_api_detail ( self ) : <EOL> for model in self . models : <EOL> for obj in model . objects . all ( ) : <EOL> url = reverse ( '<STR_LIT>' % settings . APP_LABEL , kwargs = { <EOL> '<STR_LIT>' : obj . _meta . app_label , <EOL> '<STR_LIT>' : obj . _meta . model_name , <EOL> '<STR_LIT>' : obj . pk , <EOL> } ) <EOL> response = self . client . get ( url , HTTP_X_REQUESTED_WITH = '<STR_LIT>' ) <EOL> self . assertTrue ( response . status_code in [ <NUM_LIT:200> ] ) <EOL> def test_module_api_related ( self ) : <EOL> self . appconf = [ app for app in apps . get_app_configs ( ) if isinstance ( app , self . app ) ] [ <NUM_LIT:0> ] <EOL> for relation in self . appconf . bmf_config . bmf_relations : <EOL> if relation . _model not in self . models : <EOL> continue <EOL> for obj in relation . _model . objects . all ( ) : <EOL> url = reverse ( '<STR_LIT>' % settings . APP_LABEL , kwargs = { <EOL> '<STR_LIT>' : obj . _meta . app_label , <EOL> '<STR_LIT>' : obj . _meta . model_name , <EOL> '<STR_LIT>' : obj . pk , <EOL> '<STR_LIT>' : relation . slug , <EOL> } ) <EOL> response = self . client . get ( url , HTTP_X_REQUESTED_WITH = '<STR_LIT>' ) <EOL> self . assertTrue ( response . status_code in [ <NUM_LIT:200> ] ) <EOL> def get_views ( self ) : <EOL> for model in self . models : <EOL> for dashboard in site . dashboards : <EOL> for category in dashboard : <EOL> for view in category : <EOL> if view . model == model : <EOL> yield ( model , view , dashboard . key , category . key , view . key ) <EOL> def test_module_api_view ( self ) : <EOL> for v in self . get_views ( ) : <EOL> url = reverse ( '<STR_LIT>' % settings . APP_LABEL , kwargs = { <EOL> '<STR_LIT>' : v [ <NUM_LIT:2> ] , <EOL> '<STR_LIT>' : v [ <NUM_LIT:3> ] , <EOL> '<STR_LIT>' : v [ <NUM_LIT:4> ] , <EOL> } ) <EOL> response = self . client . get ( url , HTTP_X_REQUESTED_WITH = '<STR_LIT>' ) <EOL> self . assertEqual ( response . status_code , <NUM_LIT:200> ) <EOL> def test_module_dashboard_views ( self ) : <EOL> for v in self . get_views ( ) : <EOL> url = reverse ( '<STR_LIT>' % settings . APP_LABEL , kwargs = { <EOL> '<STR_LIT>' : v [ <NUM_LIT:2> ] , <EOL> '<STR_LIT>' : v [ <NUM_LIT:3> ] , <EOL> '<STR_LIT>' : v [ <NUM_LIT:4> ] , <EOL> } ) <EOL> response = self . client . get ( url ) <EOL> self . assertEqual ( response . status_code , <NUM_LIT:200> ) <EOL> class WorkflowTestFactory ( SuperuserMixin , BaseTestCase ) : <EOL> """<STR_LIT:U+0020>""" <EOL> workflow = None <EOL> def setUp ( self ) : <EOL> super ( BaseTestCase , self ) . setUp ( ) <EOL> self . user = self . create_user ( "<STR_LIT>" , is_superuser = True ) <EOL> self . client_login ( "<STR_LIT>" ) <EOL> self . prepare_workflow_test ( ) <EOL> def prepare_workflow_test ( self ) : <EOL> workflow = self . workflow ( ) <EOL> states = list ( workflow . _states . keys ( ) ) <EOL> states . remove ( workflow . _initial_state_key ) <EOL> heap = [ <EOL> ( workflow . _initial_state_key + '<STR_LIT>' + key + '<STR_LIT>' + transition . target ) <EOL> for key , transition in workflow . _from_here ( ) <EOL> ] <EOL> heap . reverse ( ) <EOL> self . objects = { workflow . _initial_state_key : None } <EOL> self . transitions = OrderedDict ( ) <EOL> while len ( heap ) > <NUM_LIT:0> : <EOL> current = heap . pop ( ) <EOL> obj , transition , target = current . split ( '<STR_LIT>' ) <EOL> self . transitions [ current ] = { <EOL> '<STR_LIT:object>' : None , <EOL> '<STR_LIT>' : obj , <EOL> '<STR_LIT:state>' : obj . rsplit ( '<STR_LIT::>' , <NUM_LIT:1> ) [ - <NUM_LIT:1> ] , <EOL> '<STR_LIT:user>' : None , <EOL> '<STR_LIT>' : transition , <EOL> } <EOL> if target in states : <EOL> workflow . _set_state ( target ) <EOL> self . objects [ '<STR_LIT>' % ( obj , target ) ] = None <EOL> for key , transition in workflow . _from_here ( ) : <EOL> heap . insert ( <NUM_LIT:0> , '<STR_LIT>' % ( <EOL> obj , <EOL> target , <EOL> key , <EOL> transition . target , <EOL> ) ) <EOL> states . remove ( target ) <EOL> def get_object ( self , key ) : <EOL> """<STR_LIT>""" <EOL> old = self . objects . get ( key , None ) <EOL> if not old : <EOL> raise ImproperlyConfigured ( <EOL> '<STR_LIT>' % key <EOL> ) <EOL> if not old . pk : <EOL> old . save ( ) <EOL> old . pk = None <EOL> old . save ( ) <EOL> new = old . _default_manager . get ( pk = old . pk ) <EOL> return new <EOL> def auto_workflow_test ( self ) : <EOL> for key , trans in self . transitions . items ( ) : <EOL> obj = trans [ '<STR_LIT:object>' ] or self . get_object ( trans [ '<STR_LIT>' ] ) <EOL> if obj . _bmfmeta . workflow . key != trans [ '<STR_LIT:state>' ] : <EOL> raise ImproperlyConfigured ( <EOL> '<STR_LIT>' % ( <EOL> obj , <EOL> key , <EOL> ) <EOL> ) <EOL> user = trans [ '<STR_LIT:user>' ] or self . user <EOL> try : <EOL> obj . _bmfmeta . workflow . transition ( trans [ '<STR_LIT>' ] , user , silent = True ) <EOL> except ValidationError as e : <EOL> raise ImproperlyConfigured ( <EOL> '<STR_LIT>' % ( <EOL> obj , <EOL> key , <EOL> e , <EOL> ) <EOL> ) <EOL> if trans [ '<STR_LIT>' ] : <EOL> new_key = '<STR_LIT>' % ( <EOL> trans [ '<STR_LIT>' ] , <EOL> obj . _bmfmeta . workflow . key <EOL> ) <EOL> if not self . objects . get ( new_key , None ) : <EOL> self . objects [ new_key ] = obj <EOL> class ModuleMixin ( SuperuserMixin ) : <EOL> model = None <EOL> def get_latest_object ( self ) : <EOL> return self . model . objects . order_by ( '<STR_LIT>' ) . last ( ) <EOL> def autotest_get ( <EOL> self , namespace = None , status_code = <NUM_LIT:200> , data = None , parameter = None , <EOL> urlconf = None , args = None , kwargs = None , current_app = None , url = None , api = True ) : <EOL> """<STR_LIT>""" <EOL> if api : <EOL> ns = self . model . _bmfmeta . namespace_api <EOL> else : <EOL> ns = self . model . _bmfmeta . namespace_detail <EOL> if not url : <EOL> url = reverse ( ns + '<STR_LIT::>' + namespace , urlconf , args , kwargs , current_app ) <EOL> if parameter : <EOL> url += '<STR_LIT:?>' + parameter <EOL> r = self . client . get ( url , data ) <EOL> self . assertEqual ( r . status_code , status_code ) <EOL> return r <EOL> def autotest_post ( <EOL> self , namespace = None , status_code = <NUM_LIT:200> , data = None , parameter = None , <EOL> urlconf = None , args = None , kwargs = None , current_app = None , url = None , api = True ) : <EOL> """<STR_LIT>""" <EOL> if api : <EOL> ns = self . model . _bmfmeta . namespace_api <EOL> else : <EOL> ns = self . model . _bmfmeta . namespace_detail <EOL> if not url : <EOL> url = reverse ( ns + '<STR_LIT::>' + namespace , urlconf , args , kwargs , current_app ) <EOL> if parameter : <EOL> url += '<STR_LIT:?>' + parameter <EOL> r = self . client . post ( url , data ) <EOL> self . assertEqual ( r . status_code , status_code ) <EOL> return r <EOL> def autotest_ajax_get ( <EOL> self , namespace = None , status_code = <NUM_LIT:200> , data = None , parameter = None , <EOL> urlconf = None , args = None , kwargs = None , current_app = None , url = None , api = True ) : <EOL> """<STR_LIT>""" <EOL> if api : <EOL> ns = self . model . _bmfmeta . namespace_api <EOL> else : <EOL> ns = self . model . _bmfmeta . namespace_detail <EOL> if not url : <EOL> url = reverse ( ns + '<STR_LIT::>' + namespace , urlconf , args , kwargs , current_app ) <EOL> if parameter : <EOL> url += '<STR_LIT:?>' + parameter <EOL> r = self . client . get ( url , data , HTTP_X_REQUESTED_WITH = '<STR_LIT>' ) <EOL> self . assertEqual ( r . status_code , status_code ) <EOL> if status_code == <NUM_LIT:200> : <EOL> return json . loads ( r . content . decode ( ) ) <EOL> return r <EOL> def autotest_ajax_post ( <EOL> self , namespace = None , status_code = <NUM_LIT:200> , data = None , parameter = None , <EOL> urlconf = None , args = None , kwargs = None , current_app = None , url = None , api = True ) : <EOL> """<STR_LIT>""" <EOL> if api : <EOL> ns = self . model . _bmfmeta . namespace_api <EOL> else : <EOL> ns = self . model . _bmfmeta . namespace_detail <EOL> if not url : <EOL> url = reverse ( ns + '<STR_LIT::>' + namespace , urlconf , args , kwargs , current_app ) <EOL> if parameter : <EOL> url += '<STR_LIT:?>' + parameter <EOL> r = self . client . post ( url , data , HTTP_X_REQUESTED_WITH = '<STR_LIT>' ) <EOL> self . assertEqual ( r . status_code , status_code ) <EOL> if status_code == <NUM_LIT:200> : <EOL> return json . loads ( r . content . decode ( ) ) <EOL> return r <EOL> class TestCase ( BaseTestCase , DjangoTestCase ) : <EOL> pass <EOL> class TransactionTestCase ( BaseTestCase , DjangoTransactionTestCase ) : <EOL> pass <EOL> class LiveServerTestCase ( BaseTestCase , DjangoLiveServerTestCase ) : <EOL> pass </s>
