def cas_session(request, credentials): import requests from betamax.fixtures.pytest import _casette_name from six.moves import mock swat = pytest.importorskip('swat') # Ignore FutureWarnings from betamax to avoid cluttering test results with warnings.catch_warnings(): warnings.simplefilter('ignore') cassette_name = _casette_name(request, parametrized=False) + '_swat' # Must have an existing Session for Betamax to record recorded_session = requests.Session() with betamax.Betamax(recorded_session).use_cassette( cassette_name, serialize_with='prettyjson') as recorder: recorder.start() # CAS connection tries to create its own Session instance. # Inject the session being recorded into the CAS connection with mock.patch('swat.cas.rest.connection.requests.Session') as mocked: mocked.return_value = recorded_session with swat.CAS('https://{}/cas-shared-default-http/'.format( credentials['host']), username=credentials['user'], password=credentials['password']) as s: # Strip out the session id from requests & responses. recorder.config.define_cassette_placeholder( '[session id]', s._session) yield s recorder.stop()
def betamax_oauth2_session(request, oauth2_session): cassette_name = _casette_name(request, parametrized=True) recorder = betamax.Betamax(oauth2_session) recorder.use_cassette(cassette_name) recorder.start() request.addfinalizer(recorder.stop) return oauth2_session
def session(request, credentials): import warnings from six.moves import mock from betamax.fixtures.pytest import _casette_name from sasctl import current_session # Ignore FutureWarnings from betamax to avoid cluttering test results with warnings.catch_warnings(): warnings.simplefilter('ignore') cassette_name = _casette_name(request, parametrized=False) # Need to instantiate a Session before starting Betamax recording, # but sasctl.Session makes requests (which should be recorded) during # __init__(). Mock __init__ to prevent from running and then manually # execute requests.Session.__init__() so Betamax can use the session. with mock.patch('sasctl.core.Session.__init__', return_value=None): recorded_session = Session() super(Session, recorded_session).__init__() with betamax.Betamax(recorded_session).use_cassette( cassette_name, serialize_with='prettyjson') as recorder: recorder.start() # Manually run the sasctl.Session constructor. Mock out calls to # underlying requests.Session.__init__ to prevent hooks placed by # Betamax from being reset. with mock.patch('sasctl.core.requests.Session.__init__'): recorded_session.__init__(**credentials) yield recorded_session recorder.stop() current_session(None)
def cassette_name(request): """Returns the cassette name for this test""" return _casette_name(request, parametrized=True)