def mock_api(): # Mock get the license list. with open(path.join(ROOT, 'licenses.json')) as f: mock_response_body = f.read() responses.add(responses.GET, 'https://api.github.com/licenses', body=mock_response_body) # Mock get each license template. for license in os.listdir(path.join(ROOT, 'licenses')): with open(path.join(ROOT, 'licenses/{0}'.format(license))) as f: mock_response_body = f.read() responses.add(responses.GET, 'https://api.github.com/licenses/{0}'.format( path.splitext(license)[0]), body=mock_response_body, content_type='application/json') # Mock get the invalid license. with open(path.join(ROOT, 'not_found.json')) as f: mock_not_found_body = f.read() responses.add(responses.GET, 'https://api.github.com/licenses/invalid', body=mock_not_found_body, content_type='application/json') responses.start() yield responses responses.stop()
def mock_api(): with open(path.join(ROOT, 'signin.html'), encoding='utf-8') as f: mock_signin_body = f.read() responses.add(responses.POST, 'https://www.v2ex.com/signin', body=mock_signin_body) responses.add(responses.GET, 'https://www.v2ex.com/signin', body=mock_signin_body) with open(path.join(ROOT, 'once.html'), encoding='utf-8') as f: mock_once_body = f.read() responses.add(responses.GET, 'https://www.v2ex.com/mission/daily/redeem?once=51947', body=mock_once_body) with open(path.join(ROOT, 'balance.html'), encoding='utf-8') as f: mock_balance_body = f.read() responses.add(responses.GET, 'https://www.v2ex.com/balance', body=mock_balance_body) with open(path.join(ROOT, 'mission.html'), encoding='utf-8') as f: mock_mission_body = f.read() responses.add(responses.GET, 'https://www.v2ex.com/mission/daily', body=mock_mission_body) responses.start() yield responses responses.stop()
def wrapper(*args, **kwargs): responses.start() responses.add(**response_dict) result = f(response_data=json.loads(response_dict["body"]), *args, **kwargs) responses.stop() responses.reset() return result
def testTearDown(self): try: responses.stop() except RuntimeError: pass finally: responses.reset()
def mock_api(): """A mock for the PyPI JSON API.""" with open(os.path.join(HERE, 'response.json'), 'r') as fp: webargs_response = fp.read() # A valid package with a proper response responses.add(responses.GET, 'https://pypi.python.org/pypi/webargs/json', body=webargs_response, content_type='application/json') # A valid package with no releases with open(os.path.join(HERE, 'response_noreleases.json'), 'r') as fp: foo_response = fp.read() responses.add(responses.GET, 'https://pypi.python.org/pypi/foo/json', body=foo_response, content_type='application/json') # An invalid package name responses.add(responses.GET, 'https://pypi.python.org/pypi/nope/json', status=404) responses.start() yield responses responses.stop()
def mock_api(): """A mock for the PyPI JSON API.""" with open(os.path.join(HERE, 'response.json'), 'r') as fp: webargs_response = fp.read() # A valid package with a proper response responses.add( responses.GET, 'https://pypi.python.org/pypi/webargs/json', body=webargs_response, content_type='application/json' ) # A valid package with no releases with open(os.path.join(HERE, 'response_noreleases.json'), 'r') as fp: foo_response = fp.read() responses.add( responses.GET, 'https://pypi.python.org/pypi/foo/json', body=foo_response, content_type='application/json' ) # An invalid package name responses.add( responses.GET, 'https://pypi.python.org/pypi/nope/json', status=404 ) responses.start() yield responses responses.stop()
def responses(): responses_lib.start() yield responses_lib responses_lib.stop() responses_lib.reset()
def test_trigger_history_deletion_for_bot(self, monkeypatch): bot = 'test_events_bot' user = '******' month = 1 sender_id = None event_url = "http://url.event" monkeypatch.setitem(Utility.environment['history_server']['deletion'], "event_url", event_url) responses.add("POST", event_url, json={"message": "Event triggered successfully!"}, status=200, match=[ responses.json_params_matcher( [{'name': 'BOT', 'value': bot}, {'name': 'USER', 'value': user}, {'name': 'MONTH', 'value': month}, {'name': 'SENDER_ID', 'value': sender_id}])], ) responses.start() EventsTrigger.trigger_history_deletion(bot, user, month) responses.stop() logs = list(HistoryDeletionLogProcessor.get_logs(bot)) assert len(logs) == 1 assert not logs[0].get('exception') assert logs[0]['start_timestamp'] assert not logs[0].get('end_timestamp') assert logs[0]['status'] == EVENT_STATUS.TASKSPAWNED.value
async def test_trigger_data_importer_validate_only_event(self, monkeypatch): bot = 'test_events_bot_1' user = '******' event_url = "http://url.event3" monkeypatch.setitem(Utility.environment['model']['data_importer'], "event_url", event_url) responses.add("POST", event_url, json={"message": "Event triggered successfully!"}, status=200, match=[ responses.json_params_matcher( [{'name': 'BOT', 'value': bot}, {'name': 'USER', 'value': user}, {'name': 'IMPORT_DATA', 'value': ''}, {'name': 'OVERWRITE', 'value': ''}])], ) responses.start() await EventsTrigger.trigger_data_importer(bot, user, False, False) responses.stop() logs = list(DataImporterLogProcessor.get_logs(bot)) assert len(logs) == 1 assert not logs[0].get('intents').get('data') assert not logs[0].get('stories').get('data') assert not logs[0].get('utterances').get('data') assert not [action.get('data') for action in logs[0].get('actions') if action.get('type') == 'http_actions'] assert not logs[0].get('training_examples').get('data') assert not logs[0].get('domain').get('data') assert not logs[0].get('config').get('data') assert not logs[0].get('exception') assert logs[0]['is_data_uploaded'] assert logs[0]['start_timestamp'] assert not logs[0].get('end_timestamp') assert not logs[0].get('status') assert logs[0]['event_status'] == EVENT_STATUS.TASKSPAWNED.value
def test_trigger_model_testing_event(self, monkeypatch): bot = 'test_events_bot' user = '******' event_url = "http://url.event" monkeypatch.setitem(Utility.environment['model']['test'], "event_url", event_url) responses.add("POST", event_url, json={"message": "Event triggered successfully!"}, status=200, match=[ responses.json_params_matcher( [{'name': 'BOT', 'value': bot}, {'name': 'USER', 'value': user}])], ) responses.start() EventsTrigger.trigger_model_testing(bot, user) responses.stop() logs = list(ModelTestingLogProcessor.get_logs(bot)) assert len(logs) == 5 assert not logs[0].get('exception') assert logs[0]['start_timestamp'] assert not logs[0].get('end_timestamp') assert not logs[0].get('status') assert logs[0]['event_status'] == EVENT_STATUS.TASKSPAWNED.value assert not os.path.exists(os.path.join('./testing_data', bot))
def init_connection(self): os.environ["system_file"] = "./tests/testing_data/system.yaml" Utility.load_environment() connect(**Utility.mongoengine_connection( Utility.environment['database']["url"])) tmp_dir = tempfile.mkdtemp() pytest.tmp_dir = tmp_dir from rasa import train # model without entities train_result = train( domain='tests/testing_data/model_tester/domain.yml', config='tests/testing_data/model_tester/config.yml', training_files=[ 'tests/testing_data/model_tester/nlu_with_entities/nlu.yml', 'tests/testing_data/model_tester/training_stories_success/stories.yml' ], output='tests/testing_data/model_tester/models', core_additional_arguments={"augmentation_factor": 100}, force_training=True) pytest.model_path = train_result.model responses.add( 'POST', Utility.environment["augmentation"]["paraphrase_url"], json={'data': { 'paraphrases': ['common training example'] }}) responses.start() yield None responses.stop() shutil.rmtree(pytest.tmp_dir) shutil.rmtree('tests/testing_data/model_tester/models')
def _post_teardown(self): """ Disable all mocks """ if self.mock_requests: responses.stop() # TODO: Mocket.disable() sometimes makes tests hang. #Mocket.disable() patch.stopall()
async def test_run_with_get_placeholder_vs_string_response( self, monkeypatch): action = HttpActionConfig( auth_token="", action_name= "test_run_with_get_string_http_response_placeholder_required", response="The value of ${a.b.3} in ${a.b.d.0} is ${a.b.d}", http_url="http://localhost:8080/mock", request_method="GET", params_list=None, bot="5f50fd0a56b698ca10d35d2e", user="******") def _get_action(*arge, **kwargs): return action.to_mongo().to_dict() monkeypatch.setattr(ActionUtility, "get_http_action_config", _get_action) http_url = 'http://localhost:8082/mock' resp_msg = "This is string http response" responses.start() responses.add( method=responses.GET, url=http_url, body=resp_msg, status=200, ) slots = { "bot": "5f50fd0a56b698ca10d35d2e", "http_action_config_test_run": "test_run_with_get_string_http_response_placeholder_required" } events = [{"event1": "hello"}, {"event2": "how are you"}] dispatcher: CollectingDispatcher = CollectingDispatcher() latest_message = { 'text': 'get intents', 'intent_ranking': [{ 'name': 'test_run' }] } tracker = Tracker(sender_id="sender1", slots=slots, events=events, paused=False, latest_message=latest_message, followup_action=None, active_loop=None, latest_action_name=None) domain: Dict[Text, Any] = None action.save().to_mongo().to_dict() actual: List[Dict[Text, Any]] = await HttpAction().run( dispatcher, tracker, domain) responses.stop() assert actual is not None assert str(actual[0]['name']) == 'KAIRON_ACTION_RESPONSE' assert str( actual[0]['value']) == 'I have failed to process your request'
def tearDown(self): super(AkismetClientTests, self).tearDown() responses.stop() responses.reset() Flag.objects.update_or_create( name=SPAM_CHECKS_FLAG, defaults={'everyone': None}, )
def pytest_runtest_teardown(item): if not item.get_marker('withoutresponses'): try: responses.stop() except RuntimeError: # patcher was already uninstalled and responses doesnet let us # force maintain it pass
def wrapper(*args, **kwargs): responses.start() for response in resps: responses.add(**response) result = f(responses=responses, *args, **kwargs) responses.stop() responses.reset() return result
def _post_teardown(self): """ Disable all mocks after the test. """ if self.mock_requests: responses.reset() responses.stop() patch.stopall()
def _post_teardown(self): """ Disable all mocks after the test. """ super()._post_teardown() responses.reset() responses.stop() patch.stopall()
def modified_responses(): responses.start() responses_asserter = ResponsesAsserter() yield responses_asserter responses_asserter.reset_calls() try: responses.stop() except RuntimeError: # Ignore unittest.mock "stop called on unstarted patcher" exception pass
def disable_patching(self): try: botocore_mock.stop() except RuntimeError: pass try: responses.stop() except RuntimeError: pass
def callback(req): # clean up after last callback add_downloaded_files(state['downloaded'], spec, state['previous_url']) if state['requests'] == state['total_requests']: raise MaxDownloadsReached() # make a real requests call somehow responses.stop() # when testing this testing function # (testTestutil.RepoTester.test_download_setfile) we # still want to disable responses, but we don't want # to make an actual HTTP call. Detect if we are # running that test by examining the stack, and if so, # mock the requests.get call in a different way. frames = [ f for f in inspect.stack() if f[3] == "test_download_setfile" ] if frames: frame = frames[0][0] resp = frame.f_locals['self']._myget(req.url) else: resp = requests.get(req.url) responses.start() # create a filename. use .html as suffix unless we # should use something else contenttype = resp.headers["Content-type"] stem = os.path.splitext(specfile)[0] suffix = { 'application/pdf': 'pdf', 'application/json': 'json', 'text/plain': 'txt' }.get(contenttype, "html") outfile = "%s-%s.%s" % (stem, state['requests'], suffix) with open(outfile, "wb") as fp: fp.write(resp.content) if not frames and os.environ.get("TRAVIS") != "true": if suffix == "html": print( "requested %s, saved as %s. Edit if needed, then press enter" % (req.url, outfile)) x = input() else: print("requested %s, saved %s" % (req.url, outfile)) with open(outfile, "rb") as fp: content = fp.read() spec[req.url] = {'file': os.path.basename(outfile)} if resp.encoding != 'utf-8': spec[req.url]['encoding'] = resp.encoding state['requests'] += 1 state['previous_url'] = req.url return (resp.status_code, resp.headers, content)
def mock_api(): for package_name in ['requests', 'flask', 'pip']: mock_package_response(package_name) for package_name in ['this_package_name_has_not_been_used', 'you_will_never_use_this_package_name', 'I_suck_and_my_tests_are_order_dependent']: mock_package_response(package_name, status_code=404) responses.start() yield responses responses.stop()
def fixture(): responses.add( responses.POST, url, body=json.dumps(data), content_type='application/json', ) responses.start() yield responses responses.stop() responses.reset()
def mocked_responses(): """ All tests enable `responses` patching of the `requests` package, replacing all HTTP calls. """ responses.start() yield responses.stop() responses.reset()
def fixture(): responses.add( responses.POST, url, status=status, body=json.dumps(data or {}), content_type='application/json', ) responses.start() yield responses responses.stop() responses.reset()
async def test_run_with_post_and_parameters(self, monkeypatch): request_params = [HttpActionRequestBody(key='key1', value="value1"), HttpActionRequestBody(key='key2', value="value2")] action = HttpActionConfig( auth_token="", action_name="test_run_with_post", response="Data added successfully, id:${RESPONSE}", http_url="http://localhost:8080/mock", request_method="POST", params_list=request_params, bot="5f50fd0a56b698ca10d35d2e", user="******" ) def _get_action(*arge, **kwargs): return action.to_mongo().to_dict() monkeypatch.setattr(ActionUtility, "get_http_action_config", _get_action) http_url = 'http://localhost:8080/mock' resp_msg = "5000" responses.start() responses.add( method=responses.POST, url=http_url, body=resp_msg, status=200, ) slots = {"bot": "5f50fd0a56b698ca10d35d2e", "http_action_config_test_run": "test_run_with_post"} events = [{"event1": "hello"}, {"event2": "how are you"}] dispatcher: CollectingDispatcher = CollectingDispatcher() latest_message = {'text': 'get intents', 'intent_ranking': [{'name': 'test_run'}]} tracker = Tracker(sender_id="sender_test_run_with_post", slots=slots, events=events, paused=False, latest_message=latest_message, followup_action=None, active_loop=None, latest_action_name=None) domain: Dict[Text, Any] = None action.save().to_mongo().to_dict() actual: List[Dict[Text, Any]] = await HttpAction().run(dispatcher, tracker, domain) responses.stop() assert actual is not None assert str(actual[0]['name']) == 'KAIRON_ACTION_RESPONSE' assert str(actual[0]['value']) == 'Data added successfully, id:5000' log = HttpActionLog.objects(sender="sender_test_run_with_post", action="test_run_with_post", status="SUCCESS").get() assert not log['exception'] assert log['timestamp'] assert log['intent'] == "test_run" assert log['action'] == "test_run_with_post" assert log['request_params'] == {"key1": "value1", "key2": "value2"} assert log['api_response'] == '5000' assert log['bot_response'] == 'Data added successfully, id:5000'
def callback(req): # clean up after last callback add_downloaded_files(state['downloaded'], spec, state['previous_url']) if state['requests'] == state['total_requests']: raise MaxDownloadsReached() # make a real requests call somehow responses.stop() # when testing this testing function # (testTestutil.RepoTester.test_download_setfile) we # still want to disable responses, but we don't want # to make an actual HTTP call. Detect if we are # running that test by examining the stack, and if so, # mock the requests.get call in a different way. frames = [f for f in inspect.stack() if f[3] == "test_download_setfile"] if frames: frame = frames[0][0] resp = frame.f_locals['self']._myget(req.url) else: resp = requests.get(req.url) responses.start() # create a filename. use .html as suffix unless we # should use something else contenttype = resp.headers["Content-type"] stem = os.path.splitext(specfile)[0] suffix = {'application/pdf': 'pdf', 'application/json': 'json', 'text/plain': 'txt'}.get(contenttype, "html") outfile = "%s-%s.%s" % (stem, state['requests'], suffix) with open(outfile, "wb") as fp: fp.write(resp.content) if not frames and os.environ.get("TRAVIS") != "true": if suffix == "html": print( "requested %s, saved as %s. Edit if needed, then press enter" % (req.url, outfile)) x = input() else: print("requested %s, saved %s" % (req.url, outfile)) with open(outfile, "rb") as fp: content = fp.read() spec[req.url] = {'file': os.path.basename(outfile)} if resp.encoding != 'utf-8': spec[req.url]['encoding'] = resp.encoding state['requests'] += 1 state['previous_url'] = req.url return (resp.status_code, resp.headers, content)
def tearDown(self): """ Tear down the test case. """ # delete all data from redis self.redis.flushall() # disconnect from and delete the database self.db.close() os.remove(self.app.config['DATABASE']) # disable requests module patch responses.stop() responses.reset()
def mocked_responses(monkeypatch): """ All tests enable `responses` patching of the `requests` package, replacing all HTTP calls. """ responses.start() # while request mocking is running, ensure GLOBUS_SDK_ENVIRONMENT is set to # production monkeypatch.setitem(os.environ, "GLOBUS_SDK_ENVIRONMENT", "production") yield responses.stop() responses.reset()
async def test_run_with_get(self, monkeypatch): action = HttpActionConfig( auth_token="", action_name="test_run_with_get", response="The value of ${a.b.3} in ${a.b.d.0} is ${a.b.d}", http_url="http://localhost:8081/mock", request_method="GET", params_list=None, bot="5f50fd0a56b698ca10d35d2e", user="******" ) def _get_action(*arge, **kwargs): return action.to_mongo().to_dict() monkeypatch.setattr(ActionUtility, "get_http_action_config", _get_action) http_url = 'http://localhost:8081/mock' resp_msg = json.dumps({ "a": { "b": { "3": 2, "43": 30, "c": [], "d": ['red', 'buggy', 'bumpers'], } } }) responses.start() responses.add( method=responses.GET, url=http_url, body=resp_msg, status=200, ) slots = {"bot": "5f50fd0a56b698ca10d35d2e", "http_action_config_test_run": "test_run_with_post"} events = [{"event1": "hello"}, {"event2": "how are you"}] dispatcher: CollectingDispatcher = CollectingDispatcher() latest_message = {'text': 'get intents', 'intent_ranking': [{'name': 'test_run'}]} tracker = Tracker(sender_id="sender1", slots=slots, events=events, paused=False, latest_message=latest_message, followup_action=None, active_loop=None, latest_action_name=None) domain: Dict[Text, Any] = None action.save().to_mongo().to_dict() actual: List[Dict[Text, Any]] = await HttpAction().run(dispatcher, tracker, domain) responses.stop() assert actual is not None assert str(actual[0]['name']) == 'KAIRON_ACTION_RESPONSE' assert str(actual[0]['value']) == 'The value of 2 in red is [\'red\', \'buggy\', \'bumpers\']'
def start_responses_mocking(request): """Enable ``responses`` this enforcing us to explicitly mark tests that require internet usage. """ marker = request.node.get_closest_marker('allow_external_http_requests') if not marker: responses.start() yield try: if not marker: responses.stop() responses.reset() except RuntimeError: # responses patcher was already uninstalled pass
def mock_request(): with open(path.join(path.dirname(path.realpath(__file__)), 'fake_response.json')) as f: fake_response = json.load(f) responses.add( responses.GET, url=("https://www.bing.com/HPImageArchive.aspx?format" "=js&idx=0&n=1&nc=1409879295618&pid=hp"), json=fake_response, status=200, match_querystring=True ) responses.add( responses.GET, url=("https://www.bing.com/az/hprichbg/rb/HudsonBayPolars_" "ZH-CN10500767857_1920x1080.jpg"), status=200, body='Hello, world' ) responses.start() yield responses responses.stop()
def fin(): responses.reset() responses.stop()
def teardown_limeobjects(): responses.stop() responses.reset()
def tearDown(self): responses.stop() responses.reset() super(MockRequestsMixin, self).tearDown()
def tearDown(self): super(TestMigrateForks, self).tearDown() Node.remove() responses.stop() responses.reset() piwik_cache._cache = None
def tearDown(self): self.mock.stop() responses.stop() super(S3SourceListsTest, self).tearDown()
def callback(http_request): responses.stop() response = testypie.get_response(http_request.url, http_request.headers) responses.start() return response['code'], response['headers'], response['body']
def teardown_limetypes(): responses.stop() responses.reset()
def done(): resp_module.stop() resp_module.reset()
def tearDown(self): responses.stop() responses.reset()
def done(): responses.stop() responses.reset()
def teardown_module(): responses.stop()
def teardown(): responses.stop() responses.reset()
def tearDownClass(cls): responses.stop() responses.reset()