Ejemplo n.º 1
0
    async def test_run_no_connection(self, monkeypatch):
        action = HttpActionConfig(
            auth_token="",
            action_name="test_run_with_post",
            response="This should be response",
            http_url="http://localhost:8085/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)
        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()
        actual: List[Dict[Text, Any]] = await HttpAction().run(dispatcher, tracker, domain)
        assert actual is not None
        assert str(actual[0]['name']) == 'KAIRON_ACTION_RESPONSE'
        assert str(actual[0]['value']).__contains__('I have failed to process your request')
Ejemplo n.º 2
0
    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'
Ejemplo n.º 3
0
    async def test_run_with_post(self, monkeypatch):
        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=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: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="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)
        assert actual is not None
        assert actual[0]['name'] == 'KAIRON_ACTION_RESPONSE'
        assert actual[0]['value'] == 'Data added successfully, id:5000'
Ejemplo n.º 4
0
    async def test_run(self, monkeypatch):
        action = HttpActionConfig(
            auth_token="bearer kjflksjflksajfljsdflinlsufisnflisjbjsdalibvs",
            action_name="http_action",
            response="This should be response",
            http_url="http://www.google.com",
            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)
        slots = {
            "bot": "5f50fd0a56b698ca10d35d2e",
            "http_action_config_test_run": "http_action",
            "param2": "param2value"
        }
        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",
                          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)
        assert actual is not None
        assert str(actual[0]['name']) == 'KAIRON_ACTION_RESPONSE'
        assert str(actual[0]['value']) == 'This should be response'
        log = HttpActionLog.objects(sender="sender_test_run",
                                    status="SUCCESS").get()
        assert not log['exception']
        assert log['timestamp']
        assert log['intent']
        assert log['action']
        assert log['bot_response']
        assert log['api_response']
Ejemplo n.º 5
0
    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\']'