def test_prepare_request_sender_id(self): slots = { "bot": "demo_bot", "http_action_config": "http_action_name", "param2": "param2value" } events = [{"event1": "hello"}, {"event2": "how are you"}] http_action_config_params = [ HttpActionRequestBody(key="param1", value="value1"), HttpActionRequestBody(key="user_id", value="", parameter_type="sender_id") ] tracker = Tracker(sender_id="*****@*****.**", slots=slots, events=events, paused=False, latest_message=None, followup_action=None, active_loop=None, latest_action_name=None) request_params = ActionUtility.prepare_request( tracker=tracker, http_action_config_params=http_action_config_params) assert request_params['param1'] == "value1" assert request_params['user_id'] == "*****@*****.**"
def test_prepare_request(self): slots = { "bot": "demo_bot", "http_action_config": "http_action_name", "slot_name": "param2value" } events = [{"event1": "hello"}, {"event2": "how are you"}] http_action_config_params = [ HttpActionRequestBody(key="param1", value="value1"), HttpActionRequestBody(key="param2", value="slot_name", parameter_type="slot") ] tracker = Tracker(sender_id="sender1", slots=slots, events=events, paused=False, latest_message=None, followup_action=None, active_loop=None, latest_action_name=None) actual_request_body = ActionUtility.prepare_request( tracker=tracker, http_action_config_params=http_action_config_params) assert actual_request_body assert actual_request_body['param1'] == 'value1' assert actual_request_body['param2'] == 'param2value'
def test_get_http_action_config(self): http_params = [HttpActionRequestBody(key="key1", value="value1", parameter_type="slot"), HttpActionRequestBody(key="key2", value="value2")] expected = HttpActionConfig( auth_token="bearer kjflksjflksajfljsdflinlsufisnflisjbjsdalibvs", action_name="http_action", response="json", http_url="http://test.com", request_method="GET", params_list=http_params, bot="bot", user="******" ).save().to_mongo().to_dict() actual = ActionUtility.get_http_action_config("bot", "http_action") assert actual is not None assert expected['auth_token'] == actual['auth_token'] assert expected['action_name'] == actual['action_name'] assert expected['response'] == actual['response'] assert expected['http_url'] == actual['http_url'] assert expected['request_method'] == actual['request_method'] assert expected['params_list'] is not None assert expected['params_list'][0]['key'] == actual['params_list'][0]['key'] assert expected['params_list'][0]['value'] == actual['params_list'][0]['value'] assert expected['params_list'][0]['parameter_type'] == actual['params_list'][0]['parameter_type'] assert expected['params_list'][1]['key'] == actual['params_list'][1]['key'] assert expected['params_list'][1]['value'] == actual['params_list'][1]['value'] assert expected['params_list'][1]['parameter_type'] == actual['params_list'][1]['parameter_type'] assert actual['status']
def test_prepare_request_empty_slot(self): slots = { "bot": "demo_bot", "http_action_config": "http_action_name", "param2": "param2value" } events = [{"event1": "hello"}, {"event2": "how are you"}] http_action_config_params = [ HttpActionRequestBody(key="param1", value="value1"), HttpActionRequestBody(key="param3", value="", parameter_type="slot") ] tracker = Tracker(sender_id="sender1", slots=slots, events=events, paused=False, latest_message=None, followup_action=None, active_loop=None, latest_action_name=None) try: ActionUtility.prepare_request( tracker=tracker, http_action_config_params=http_action_config_params) assert False except HttpActionFailure as ex: assert str(ex) == ("Coudn't find value for key param3 from slot")
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 test_get_http_action_invalid_http_action(self): http_params = [HttpActionRequestBody(key="key1", value="value1", parameter_type="slot"), HttpActionRequestBody(key="key2", value="value2")] HttpActionConfig( auth_token="bearer kjflksjflksajfljsdflinlsufisnflisjbjsdalibvs", action_name="http_action", response="json", http_url="http://test.com", request_method="GET", params_list=http_params, bot="bot", user="******" ).save().to_mongo().to_dict() try: ActionUtility.get_http_action_config("bot", "http_action1") assert False except HttpActionFailure as ex: assert str(ex).__contains__("No HTTP action found for bot")