<s> from functools import wraps <EOL> import os <EOL> from django . conf import settings <EOL> from django . contrib . auth . middleware import AuthenticationMiddleware <EOL> from django . contrib . messages . storage import default_storage <EOL> from django . core . handlers import wsgi <EOL> from django import http <EOL> from django . test . client import RequestFactory <EOL> from django . utils . importlib import import_module <EOL> from django . utils import unittest <EOL> from ceilometerclient . v2 import client as ceilometer_client <EOL> from cinderclient import client as cinder_client <EOL> import glanceclient <EOL> from heatclient import client as heat_client <EOL> from keystoneclient . v2_0 import client as keystone_client <EOL> from neutronclient . v2_0 import client as neutron_client <EOL> from novaclient . v1_1 import client as nova_client <EOL> from swiftclient import client as swift_client <EOL> try : <EOL> from troveclient import client as trove_client <EOL> with_trove = True <EOL> except ImportError : <EOL> with_trove = False <EOL> import httplib2 <EOL> import mox <EOL> from openstack_auth import user <EOL> from openstack_auth import utils <EOL> from horizon import middleware <EOL> from horizon . test import helpers as horizon_helpers <EOL> from openstack_dashboard import api <EOL> from openstack_dashboard import context_processors <EOL> from openstack_dashboard . test . test_data import utils as test_utils <EOL> wsgi . WSGIRequest . __repr__ = lambda self : "<STR_LIT>" <EOL> def create_stubs ( stubs_to_create = { } ) : <EOL> if not isinstance ( stubs_to_create , dict ) : <EOL> raise TypeError ( "<STR_LIT>" <EOL> "<STR_LIT>" % type ( stubs_to_create ) . __name__ ) <EOL> def inner_stub_out ( fn ) : <EOL> @ wraps ( fn ) <EOL> def instance_stub_out ( self , * args , ** kwargs ) : <EOL> for key in stubs_to_create : <EOL> if not ( isinstance ( stubs_to_create [ key ] , tuple ) or <EOL> isinstance ( stubs_to_create [ key ] , list ) ) : <EOL> raise TypeError ( "<STR_LIT>" <EOL> "<STR_LIT>" <EOL> "<STR_LIT>" <EOL> % type ( stubs_to_create [ key ] ) . __name__ ) <EOL> for value in stubs_to_create [ key ] : <EOL> self . mox . StubOutWithMock ( key , value ) <EOL> return fn ( self , * args , ** kwargs ) <EOL> return instance_stub_out <EOL> return inner_stub_out <EOL> class RequestFactoryWithMessages ( RequestFactory ) : <EOL> def get ( self , * args , ** kwargs ) : <EOL> req = super ( RequestFactoryWithMessages , self ) . get ( * args , ** kwargs ) <EOL> req . user = utils . get_user ( req ) <EOL> req . session = [ ] <EOL> req . _messages = default_storage ( req ) <EOL> return req <EOL> def post ( self , * args , ** kwargs ) : <EOL> req = super ( RequestFactoryWithMessages , self ) . post ( * args , ** kwargs ) <EOL> req . user = utils . get_user ( req ) <EOL> req . session = [ ] <EOL> req . _messages = default_storage ( req ) <EOL> return req <EOL> @ unittest . skipIf ( os . environ . get ( '<STR_LIT>' , False ) , <EOL> "<STR_LIT>" ) <EOL> class TestCase ( horizon_helpers . TestCase ) : <EOL> """<STR_LIT>""" <EOL> def setUp ( self ) : <EOL> test_utils . load_test_data ( self ) <EOL> self . mox = mox . Mox ( ) <EOL> self . factory = RequestFactoryWithMessages ( ) <EOL> self . context = { '<STR_LIT>' : self . tenants . list ( ) } <EOL> def fake_conn_request ( * args , ** kwargs ) : <EOL> raise Exception ( "<STR_LIT>" <EOL> "<STR_LIT>" <EOL> % ( args , kwargs ) ) <EOL> self . _real_conn_request = httplib2 . Http . _conn_request <EOL> httplib2 . Http . _conn_request = fake_conn_request <EOL> self . _real_context_processor = context_processors . openstack <EOL> context_processors . openstack = lambda request : self . context <EOL> self . _real_get_user = utils . get_user <EOL> tenants = self . context [ '<STR_LIT>' ] <EOL> self . setActiveUser ( id = self . user . id , <EOL> token = self . token , <EOL> username = self . user . name , <EOL> tenant_id = self . tenant . id , <EOL> service_catalog = self . service_catalog , <EOL> authorized_tenants = tenants ) <EOL> self . request = http . HttpRequest ( ) <EOL> self . request . session = self . client . _session ( ) <EOL> self . request . session [ '<STR_LIT>' ] = self . token . id <EOL> middleware . HorizonMiddleware ( ) . process_request ( self . request ) <EOL> AuthenticationMiddleware ( ) . process_request ( self . request ) <EOL> os . environ [ "<STR_LIT>" ] = "<STR_LIT:True>" <EOL> def tearDown ( self ) : <EOL> self . mox . UnsetStubs ( ) <EOL> httplib2 . Http . _conn_request = self . _real_conn_request <EOL> context_processors . openstack = self . _real_context_processor <EOL> utils . get_user = self . _real_get_user <EOL> self . mox . VerifyAll ( ) <EOL> del os . environ [ "<STR_LIT>" ] <EOL> def setActiveUser ( self , id = None , token = None , username = None , tenant_id = None , <EOL> service_catalog = None , tenant_name = None , roles = None , <EOL> authorized_tenants = None , enabled = True ) : <EOL> def get_user ( request ) : <EOL> return user . User ( id = id , <EOL> token = token , <EOL> user = username , <EOL> tenant_id = tenant_id , <EOL> service_catalog = service_catalog , <EOL> roles = roles , <EOL> enabled = enabled , <EOL> authorized_tenants = authorized_tenants , <EOL> endpoint = settings . OPENSTACK_KEYSTONE_URL ) <EOL> utils . get_user = get_user <EOL> def assertRedirectsNoFollow ( self , response , expected_url ) : <EOL> """<STR_LIT>""" <EOL> assert ( response . status_code / <NUM_LIT:100> == <NUM_LIT:3> ) , "<STR_LIT>" <EOL> self . assertEqual ( response . _headers . get ( '<STR_LIT:location>' , None ) , <EOL> ( '<STR_LIT>' , settings . TESTSERVER + expected_url ) ) <EOL> self . assertEqual ( response . status_code , <NUM_LIT> ) <EOL> def assertNoFormErrors ( self , response , context_name = "<STR_LIT>" ) : <EOL> """<STR_LIT>""" <EOL> context = getattr ( response , "<STR_LIT>" , { } ) <EOL> if not context or context_name not in context : <EOL> return True <EOL> errors = response . context [ context_name ] . _errors <EOL> assert len ( errors ) == <NUM_LIT:0> , "<STR_LIT>" % errors <EOL> def assertFormErrors ( self , response , count = <NUM_LIT:0> , message = None , <EOL> context_name = "<STR_LIT>" ) : <EOL> """<STR_LIT>""" <EOL> context = getattr ( response , "<STR_LIT>" , { } ) <EOL> assert ( context and context_name in context ) , "<STR_LIT>" <EOL> errors = response . context [ context_name ] . _errors <EOL> if count : <EOL> assert len ( errors ) == count , "<STR_LIT>" % ( len ( errors ) , count ) <EOL> if message and message not in unicode ( errors ) : <EOL> self . fail ( "<STR_LIT>" <EOL> % [ "<STR_LIT>" % ( key , [ e for e in field_errors ] ) for <EOL> ( key , field_errors ) in errors . items ( ) ] ) <EOL> else : <EOL> assert len ( errors ) > <NUM_LIT:0> , "<STR_LIT>" <EOL> class BaseAdminViewTests ( TestCase ) : <EOL> """<STR_LIT>""" <EOL> def setActiveUser ( self , * args , ** kwargs ) : <EOL> if "<STR_LIT>" not in kwargs : <EOL> kwargs [ '<STR_LIT>' ] = [ self . roles . admin . _info ] <EOL> super ( BaseAdminViewTests , self ) . setActiveUser ( * args , ** kwargs ) <EOL> def setSessionValues ( self , ** kwargs ) : <EOL> settings . SESSION_ENGINE = '<STR_LIT>' <EOL> engine = import_module ( settings . SESSION_ENGINE ) <EOL> store = engine . SessionStore ( ) <EOL> for key in kwargs : <EOL> store [ key ] = kwargs [ key ] <EOL> self . request . session [ key ] = kwargs [ key ] <EOL> store . save ( ) <EOL> self . session = store <EOL> self . client . cookies [ settings . SESSION_COOKIE_NAME ] = store . session_key <EOL> class APITestCase ( TestCase ) : <EOL> """<STR_LIT>""" <EOL> def setUp ( self ) : <EOL> super ( APITestCase , self ) . setUp ( ) <EOL> utils . patch_middleware_get_user ( ) <EOL> def fake_keystoneclient ( request , admin = False ) : <EOL> """<STR_LIT>""" <EOL> return self . stub_keystoneclient ( ) <EOL> self . _original_glanceclient = api . glance . glanceclient <EOL> self . _original_keystoneclient = api . keystone . keystoneclient <EOL> self . _original_novaclient = api . nova . novaclient <EOL> self . _original_neutronclient = api . neutron . neutronclient <EOL> self . _original_cinderclient = api . cinder . cinderclient <EOL> self . _original_heatclient = api . heat . heatclient <EOL> self . _original_ceilometerclient = api . ceilometer . ceilometerclient <EOL> if with_trove : <EOL> self . _original_troveclient = api . trove . troveclient <EOL> api . glance . glanceclient = lambda request : self . stub_glanceclient ( ) <EOL> api . keystone . keystoneclient = fake_keystoneclient <EOL> api . nova . novaclient = lambda request : self . stub_novaclient ( ) <EOL> api . neutron . neutronclient = lambda request : self . stub_neutronclient ( ) <EOL> api . cinder . cinderclient = lambda request : self . stub_cinderclient ( ) <EOL> api . heat . heatclient = lambda request : self . stub_heatclient ( ) <EOL> api . ceilometer . ceilometerclient = lambda request : self . stub_ceilometerclient ( ) <EOL> if with_trove : <EOL> api . trove . troveclient = lambda request : self . stub_troveclient ( ) <EOL> def tearDown ( self ) : <EOL> super ( APITestCase , self ) . tearDown ( ) <EOL> api . glance . glanceclient = self . _original_glanceclient <EOL> api . nova . novaclient = self . _original_novaclient <EOL> api . keystone . keystoneclient = self . _original_keystoneclient <EOL> api . neutron . neutronclient = self . _original_neutronclient <EOL> api . cinder . cinderclient = self . _original_cinderclient <EOL> api . heat . heatclient = self . _original_heatclient <EOL> api . ceilometer . ceilometerclient = self . _original_ceilometerclient <EOL> if with_trove : <EOL> api . trove . troveclient = self . _original_troveclient <EOL> def stub_novaclient ( self ) : <EOL> if not hasattr ( self , "<STR_LIT>" ) : <EOL> self . mox . StubOutWithMock ( nova_client , '<STR_LIT>' ) <EOL> self . novaclient = self . mox . CreateMock ( nova_client . Client ) <EOL> return self . novaclient <EOL> def stub_cinderclient ( self ) : <EOL> if not hasattr ( self , "<STR_LIT>" ) : <EOL> self . mox . StubOutWithMock ( cinder_client , '<STR_LIT>' ) <EOL> self . cinderclient = self . mox . CreateMock ( cinder_client . Client ) <EOL> return self . cinderclient <EOL> def stub_keystoneclient ( self ) : <EOL> if not hasattr ( self , "<STR_LIT>" ) : <EOL> self . mox . StubOutWithMock ( keystone_client , '<STR_LIT>' ) <EOL> keystone_client . Client . auth_token = '<STR_LIT:foo>' <EOL> keystone_client . Client . service_catalog = None <EOL> keystone_client . Client . tenant_id = '<STR_LIT:1>' <EOL> keystone_client . Client . tenant_name = '<STR_LIT>' <EOL> self . keystoneclient = self . mox . CreateMock ( keystone_client . Client ) <EOL> return self . keystoneclient <EOL> def stub_glanceclient ( self ) : <EOL> if not hasattr ( self , "<STR_LIT>" ) : <EOL> self . mox . StubOutWithMock ( glanceclient , '<STR_LIT>' ) <EOL> self . glanceclient = self . mox . CreateMock ( glanceclient . Client ) <EOL> return self . glanceclient <EOL> def stub_neutronclient ( self ) : <EOL> if not hasattr ( self , "<STR_LIT>" ) : <EOL> self . mox . StubOutWithMock ( neutron_client , '<STR_LIT>' ) <EOL> self . neutronclient = self . mox . CreateMock ( neutron_client . Client ) <EOL> return self . neutronclient <EOL> def stub_swiftclient ( self , expected_calls = <NUM_LIT:1> ) : <EOL> if not hasattr ( self , "<STR_LIT>" ) : <EOL> self . mox . StubOutWithMock ( swift_client , '<STR_LIT>' ) <EOL> self . swiftclient = self . mox . CreateMock ( swift_client . Connection ) <EOL> while expected_calls : <EOL> swift_client . Connection ( None , <EOL> mox . IgnoreArg ( ) , <EOL> None , <EOL> preauthtoken = mox . IgnoreArg ( ) , <EOL> preauthurl = mox . IgnoreArg ( ) , <EOL> cacert = None , <EOL> auth_version = "<STR_LIT>" ) . AndReturn ( self . swiftclient ) <EOL> expected_calls -= <NUM_LIT:1> <EOL> return self . swiftclient <EOL> def stub_heatclient ( self ) : <EOL> if not hasattr ( self , "<STR_LIT>" ) : <EOL> self . mox . StubOutWithMock ( heat_client , '<STR_LIT>' ) <EOL> self . heatclient = self . mox . CreateMock ( heat_client . Client ) <EOL> return self . heatclient <EOL> def stub_ceilometerclient ( self ) : <EOL> if not hasattr ( self , "<STR_LIT>" ) : <EOL> self . mox . StubOutWithMock ( ceilometer_client , '<STR_LIT>' ) <EOL> self . ceilometerclient = self . mox . CreateMock ( ceilometer_client . Client ) <EOL> return self . ceilometerclient <EOL> if with_trove : <EOL> def stub_troveclient ( self ) : <EOL> if not hasattr ( self , "<STR_LIT>" ) : <EOL> self . mox . StubOutWithMock ( trove_client , '<STR_LIT>' ) <EOL> self . troveclient = self . mox . CreateMock ( trove_client . Client ) <EOL> return self . troveclient <EOL> @ unittest . skipUnless ( os . environ . get ( '<STR_LIT>' , False ) , <EOL> "<STR_LIT>" ) <EOL> class SeleniumTestCase ( horizon_helpers . SeleniumTestCase ) : <EOL> def setUp ( self ) : <EOL> super ( SeleniumTestCase , self ) . setUp ( ) <EOL> test_utils . load_test_data ( self ) <EOL> self . mox = mox . Mox ( ) <EOL> self . _real_get_user = utils . get_user <EOL> self . setActiveUser ( id = self . user . id , <EOL> token = self . token , <EOL> username = self . user . name , <EOL> tenant_id = self . tenant . id , <EOL> service_catalog = self . service_catalog , <EOL> authorized_tenants = self . tenants . list ( ) ) <EOL> os . environ [ "<STR_LIT>" ] = "<STR_LIT:True>" <EOL> def tearDown ( self ) : <EOL> self . mox . UnsetStubs ( ) <EOL> utils . get_user = self . _real_get_user <EOL> self . mox . VerifyAll ( ) <EOL> del os . environ [ "<STR_LIT>" ] <EOL> def setActiveUser ( self , id = None , token = None , username = None , tenant_id = None , <EOL> service_catalog = None , tenant_name = None , roles = None , <EOL> authorized_tenants = None , enabled = True ) : <EOL> def get_user ( request ) : <EOL> return user . User ( id = id , <EOL> token = token , <EOL> user = username , <EOL> tenant_id = tenant_id , <EOL> service_catalog = service_catalog , <EOL> roles = roles , <EOL> enabled = enabled , <EOL> authorized_tenants = authorized_tenants , <EOL> endpoint = settings . OPENSTACK_KEYSTONE_URL ) <EOL> utils . get_user = get_user <EOL> class SeleniumAdminTestCase ( SeleniumTestCase ) : <EOL> """<STR_LIT>""" <EOL> def setActiveUser ( self , * args , ** kwargs ) : <EOL> if "<STR_LIT>" not in kwargs : <EOL> kwargs [ '<STR_LIT>' ] = [ self . roles . admin . _info ] <EOL> super ( SeleniumAdminTestCase , self ) . setActiveUser ( * args , ** kwargs ) </s>
