def setup(self, exante_api_demo_client, exante_api_demo_client_no_scopes): """ Create ExanteAPI and Betamax instances. """ self.client = exante_api_demo_client self.recorder = betamax.Betamax(session=self.client.session) self.client_no_scopes = exante_api_demo_client_no_scopes self.recorder_no_scopes = betamax.Betamax( session=self.client_no_scopes.session)
def api_and_betamax(request, jive_domain, jive_host): conf = betamax.Betamax.configure() conf.default_cassette_options['serialize_with'] = 'prettyjson' conf.cassette_library_dir = cassette_dir betamax.Betamax.register_serializer(pretty_json.PrettyJSONSerializer) # keep creds secret conf.define_cassette_placeholder( '<JIVE-AUTH>', base64.b64encode('{0}:{1}'.format( os.environ['JIVE_USER'], os.environ['JIVE_PASS']).encode('utf-8')).decode()) conf.define_cassette_placeholder('<JIVE-USER>', os.environ['JIVE_USER']) conf.define_cassette_placeholder('<JIVE-PASS>', os.environ['JIVE_PASS']) conf.define_cassette_placeholder('<JIVE-HOST>', jive_host) conf.define_cassette_placeholder('<JIVE-DOMAIN>', jive_domain) conf.before_record(callback=betamax_sanitizer) if request.config.getoption('--record'): conf.default_cassette_options['record_mode'] = 'once' else: conf.default_cassette_options['record_mode'] = 'none' cass_name = betamax.fixtures.pytest._casette_name(request, parametrized=True) api = JiveApi(os.environ['JIVE_URL'], os.environ['JIVE_USER'], os.environ['JIVE_PASS']) # Workaround for https://github.com/betamaxpy/betamax/issues/124 # where secrets aren't stripped from gzipped responses. Fix is to tell the # server that we can't accept gzip. api._requests.headers.update({'Accept-Encoding': 'identity'}) bmax = betamax.Betamax(api._requests) bmax.use_cassette(cass_name) bmax.start() request.addfinalizer(bmax.stop) return api, bmax
def __init__(self, url, username='', password=''): betamax.Betamax.register_serializer(pretty_json.PrettyJSONSerializer) with betamax.Betamax.configure() as config: config.cassette_library_dir = 'cassettes' config.default_cassette_options['serialize_with'] = 'prettyjson' self.session = requests.Session() self.reddit_conn = praw.Reddit( "bot1" #user_agent='AGenderOfGeese user agent' #requestor_class=JSONDebugRequestor, #requestor_kwargs={"session": my_session} ) self.reddit_conn.read_only = True #http = self.reddit_conn._core._requestor._http #http.headers['Accept-Encoding'] = 'identity' self.recorder = betamax.Betamax(self.session, cassette_library_dir='cassettes') with self.recorder.use_cassette(f'recording-session-bot1-{datetime.now()}'): self.session.get(f'{url}') '''
def _test_device_set_attribute(self, attr, value): recorder = betamax.Betamax(self.session) with recorder.use_cassette('set-attr-%s-%s' % (attr, value)): c = SomeComfort(self.username, self.password) device = self._get_device(c) setattr(device, attr, value) self.assertEqual(value, getattr(device, attr))
def test_betamax(self): CASSETTE_LIBRARY_DIR = 'd:\\Project\\PaaPa\\tests\\responses' session = requests.Session() recorder = betamax.Betamax( session, cassette_library_dir=CASSETTE_LIBRARY_DIR ) spider = ShanghaiSpider() with recorder.use_cassette('shanghai'): url = 'http://zjw.sh.gov.cn/zjw/sgs/index.html' response = session.get(url) scrapy_response = HtmlResponse(body=response.content, url=url) # spider.start_requests() result = spider.parse(scrapy_response) expect_company1={'date': '2019.07.11', 'title': '对中设建工集团有限公司的行政处罚决定书', 'url': 'http://zjw.sh.gov.cn/zjw/sgs/20190711/70450.html'} expect_company2={'date': '2019.05.28', 'title': '对上海朗擎建筑工程有限公司的行政处罚决定书', 'url': 'http://zjw.sh.gov.cn/zjw/sgs/20190528/69079.html'} expect_company3={'date': '2019.01.24', 'title': '对上海颐东机械施工工程有限公司的行政处罚决定书', 'url': 'http://zjw.sh.gov.cn/zjw/sgs/20190228/59348.html'} print(result) self.assertIn(expect_company1,result) self.assertIn(expect_company2,result)
def get_github_client(token): """Obtain a github3 client using an API token for authentication. If the ``BETAMAX_LIBRARY_DIR`` and ``BETAMAX_CASSETTE`` environment variables are defined, the ``requests.Session`` used by the client will be hooked up to betamax and pre-recorded HTTP requests will be used instead of incurring actual requests. When betamax is active, the auth token is not relevant. """ gh = github3.GitHub() betamax_library_dir = os.environ.get('BETAMAX_LIBRARY_DIR') betamax_cassette = os.environ.get('BETAMAX_CASSETTE') if betamax_library_dir and betamax_cassette: # Delay import because only needed for testing. import betamax with betamax.Betamax.configure() as config: config.cassette_library_dir = betamax_library_dir # We don't want requests hitting the network at all. config.default_cassette_options['record_mode'] = 'none' recorder = betamax.Betamax(gh._session) recorder.use_cassette(betamax_cassette) recorder.start() gh.login(token=token) return gh
def test_fetch_complete(minecraft_feed): """Fetches and decodes the contents correctly?""" with betamax.Betamax(minecraft_feed.session).use_cassette('fetch-feed'), \ minecraft_feed.fetch_complete() as feed: timestamp = next(ijson.items(feed, 'timestamp'), None) assert isinstance(timestamp, int)
def main(): sess = requests.Session() import betamax bm = betamax.Betamax(sess) match_requests_on = [ # 'digest-auth', 'path', 'method', 'body', 'host', 'uri', # 'headers', 'query' ] with bm.use_cassette('ms', match_requests_on=match_requests_on): from main import login with open('auth.json') as fh: login(sess, *json.load(fh)) units = list(get_units('2016 Semester 1', sess)) from pprint import pprint pprint(units) write_out(units)
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 create_recorded_session() -> requests.Session: session = OldSession() recorder = betamax.Betamax(session) recorder.use_cassette(_create_cassette_name(request)) recorder.start() request.addfinalizer(recorder.stop) return session
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 setUp(self): self.user = os.environ.get('GH_USER', 'foo') self.password = os.environ.get('GH_PASSWORD', 'bar') self.token = os.environ.get('GH_AUTH', 'x' * 20) self.gh = self.get_client() self.session = self.gh._session self.recorder = betamax.Betamax(self.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 test_get(auth_session): with betamax.Betamax(auth_session).use_cassette('test_analysis.test_get'): response = auth_session.get('analyses') assert auth_session._response.status_code == 200 assert response assert 'analyses' in response assert stare.models.analysis.make_analysis_list(response['analyses'])
def _construct_session_with_betamax(fixture, session_obj=None): # NOTE(morganfainberg): This function should contain the logic of # keystoneauth1.session._construct_session as it replaces the # _construct_session function to apply betamax magic to the requests # session object. if not session_obj: session_obj = requests.Session() # Use TCPKeepAliveAdapter to fix bug 1323862 for scheme in list(session_obj.adapters.keys()): session_obj.mount(scheme, session.TCPKeepAliveAdapter()) with betamax.Betamax.configure() as config: config.before_record(callback=fixture.pre_record_hook) fixture.recorder = betamax.Betamax( session_obj, cassette_library_dir=fixture.cassette_library_dir) record = 'none' serializer = None if fixture.record in ['once', 'all', 'new_episodes']: record = fixture.record serializer = fixture.serializer_name fixture.recorder.use_cassette(fixture.cassette_name, serialize_with=serializer, record=record, **fixture.use_cassette_kwargs) fixture.recorder.start() fixture.addCleanup(fixture.recorder.stop) return session_obj
def test_tor_get_source_valid_url(self): with betamax.Betamax(self.session) as vcr: vcr.use_cassette('test_tor_get_source_valid_url') url = 'http://www.google.com' response = self.engine.get_page_source(url) assert isinstance(response, Webpage) assert isinstance(response.source, str)
def setup(self, ig_api_demo_client): """ Create IGAPI and Betamax instances. """ self.client = ig_api_demo_client self.recorder = betamax.Betamax(session=self.client.session)
def test_create_origin(self): client = Azion(token) recorder = betamax.Betamax(client.session) with recorder.use_cassette('Origin_create'): origin = client.create_origin( configuration_id=1501191440, name='Dummy origin', origin_type='single_origin', method=None, host_header='www.example.com', origin_protocol_policy='http', addresses=[{ 'address': 'www.myorigin.com', #'weight': None, 'server_role': 'primary', }], connection_timeout=60, timeout_between_bytes=120) assert isinstance(origin, Origin) assert origin.name == 'Dummy origin' assert origin.origin_type == 'single_origin' assert origin.method == '' assert origin.host_header == 'www.example.com' assert origin.origin_protocol_policy == 'http' assert origin.addresses[0].address == 'www.myorigin.com' assert origin.addresses[0].weight is None assert origin.addresses[0].server_role == 'primary' assert origin.addresses[0].is_active is True assert origin.connection_timeout == 60 assert origin.timeout_between_bytes == 120
def _construct_session_with_betamax(fixture, session_obj=None): # NOTE(morganfainberg): This function should contain the logic of # keystoneauth1.session._construct_session as it replaces the # _construct_session function to apply betamax magic to the requests # session object. if not session_obj: session_obj = requests.Session() # Use TCPKeepAliveAdapter to fix bug 1323862 for scheme in list(session_obj.adapters.keys()): session_obj.mount(scheme, session.TCPKeepAliveAdapter()) fixture.recorder = betamax.Betamax( session_obj, cassette_library_dir=fixture.cassette_library_dir) record = 'none' serializer = 'json' if fixture.record: record = 'all' if fixture.serializer: serializer = fixture.serializer.name fixture.recorder.use_cassette(fixture.cassette_name, serialize_with=serializer, record=record) fixture.recorder.start() fixture.addCleanup(fixture.recorder.stop) return session_obj
def test_get(auth_session): with betamax.Betamax(auth_session).use_cassette('test_paper.test_get'): response = auth_session.get('papers') assert auth_session._response.status_code == 200 assert response assert 'papers' in response assert stare.models.paper.make_paper_list(response['papers'])
def test_fetch_complete_timestamp(minecraft_feed): """Fetches and decodes the timestamp correctly?""" with betamax.Betamax(minecraft_feed.session).use_cassette( 'feed-timestamp'): # noqa: E501 timestamp = minecraft_feed.fetch_complete_timestamp() assert isinstance(timestamp, datetime.datetime)
def setup_method(self, method): self.log.info('GitRepoTestCase.setup_method({})'.format(method)) # build temporary directory self.tempdir = TemporaryDirectory() # repository mockup (in a temporary place) self.repository = Repo.init(self.tempdir.name) # setup git command mockup self.Popen = MockPopen() self.Popen.mock.Popen_instance.stdin = None self.Popen.mock.Popen_instance.wait = lambda *a, **k: self.Popen.wait() self.Popen.mock.Popen_instance.__enter__ = lambda self: self self.Popen.mock.Popen_instance.__exit__ = lambda self, *a, **k: None # when initiating service with no repository, the connection is not triggered self.service = self.get_service() self.service.repository = self.repository # setup http api mockup self.recorder = betamax.Betamax(self.get_requests_session()) self.get_requests_session().headers['Accept-Encoding'] = 'identity' # have git commands logged Git.GIT_PYTHON_TRACE = True FORMAT = '> %(message)s' formatter = logging.Formatter(fmt=FORMAT) handler = logging.StreamHandler() handler.setFormatter(formatter) logging.getLogger('git.cmd').removeHandler(logging.NullHandler()) logging.getLogger('git.cmd').addHandler(handler) logging.getLogger('git.cmd').propagate = True # have HTTP requests logged import http.client http.client.HTTPConnection.debuglevel = 1 logging.getLogger('requests.packages.urllib3').setLevel(logging.DEBUG) logging.getLogger('requests.packages.urllib3').propagate = True
def test_get_configuration(self): client = Azion(token) recorder = betamax.Betamax(client.session) with recorder.use_cassette('Configuration_get'): configuration = client.get_configuration(1528252734) assert isinstance(configuration, Configuration)
def setUp(self): self.api = MeisterTaskAPI(bearer_token=self.meistertask_token) default_cassette_options = {} if self.record is True: default_cassette_options['record_mode'] = 'once' self.recorder = betamax.Betamax( self.api.session, default_cassette_options=default_cassette_options)
def test_revoke_failure(self, bad_api_client): recorder = betamax.Betamax(bad_api_client.session) with recorder.use_cassette('ComodoAPI_revoke_failure'): result = bad_api_client.revoke(cert_id=123456, reason='Revoked for testing') assert isinstance(result, dict) assert 'status' in result assert 'error' in result['status']
def test_purge_wildcard(self): client = Azion(token) recorder = betamax.Betamax(client.session) url = 'www.maugzoide.com/static/img/*' with recorder.use_cassette('Purge_wildcard'): assert client.purge_wildcard(url)
def test_collect_failure(self, bad_api_client): recorder = betamax.Betamax(bad_api_client.session) with recorder.use_cassette('ComodoAPI_collect_failure'): result = bad_api_client.collect(cert_id=123456, format_type='X509 PEM Bundle') assert isinstance(result, dict) assert 'status' in result assert 'error' in result['status']
def main(): session = requests.Session() recorder = betamax.Betamax( session, cassette_library_dir=CASSETTE_LIBRARY_DIR ) with recorder.use_cassette('our-first-recorded-session'): session.get('https://httpbin.org/get')
def test_get_cert_types_failure(self, bad_api_client): recorder = betamax.Betamax(bad_api_client.session) with recorder.use_cassette('ComodoAPI_get_cert_types_failure'): result = bad_api_client.get_cert_types() assert isinstance(result, dict) assert 'status' in result assert 'error' in result['status']
def __init__(self, bucket_name, sample_dir, session=requests.Session(), betamax_mode="none"): self.sample_dir = sample_dir self.bucket_name = bucket_name self.betamax_mode = betamax_mode self.recorder = betamax.Betamax(session)