예제 #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')
예제 #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'
예제 #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'
예제 #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']
예제 #5
0
    def test_get_http_action_config_deleted_action(self):
        http_params = [
            HttpActionRequestBody(key="key1",
                                  value="value1",
                                  parameter_type="slot"),
            HttpActionRequestBody(key="key2", value="value2")
        ]
        HttpActionConfig(
            auth_token="",
            action_name="test_get_http_action_config_deleted_action",
            response="${RESPONSE}",
            http_url="http://www.digite.com",
            request_method="POST",
            params_list=http_params,
            bot="bot",
            user="******",
            status=False).save().to_mongo().to_dict()
        expected = HttpActionConfig(
            auth_token="bearer kjflksjflksajfljsdflinlsufisnflisjbjsdalibvs",
            action_name="test_get_http_action_config_deleted_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", "test_get_http_action_config_deleted_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']
예제 #6
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\']'
예제 #7
0
 async def test_run_invalid_http_action(self, mock_get_http_action_exception):
     slots = {"bot": "5f50fd0a56b698ca10d35d2e", "http_action_config_http_action": "test_run_invalid_http_action",
              "param2": "param2value"}
     events = [{"event1": "hello"}, {"event2": "how are you"}]
     latest_message = {'text': 'get intents', 'intent_ranking': [{'name': 'http_action'}]}
     HttpActionConfig(
         auth_token="bearer kjflksjflksajfljsdflinlsufisnflisjbjsdalibvs",
         action_name="test_run_invalid_http_action1",
         response="json",
         http_url="http://www.google.com",
         request_method="GET",
         params_list=None,
         bot="5f50fd0a56b698ca10d35d2e",
         user="******"
     ).save()
     dispatcher: CollectingDispatcher = CollectingDispatcher()
     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
     await HttpAction().run(dispatcher, tracker, domain)
     str(dispatcher.messages[0]['text']).__contains__(
         "I have failed to process your request: No HTTP action found for bot")
     log = HttpActionLog.objects(sender="sender1",
                                 bot="5f50fd0a56b698ca10d35d2e",
                                 status="FAILURE").get()
     assert log['exception'].__contains__('No HTTP action found for bot')
예제 #8
0
 def test_run_invalid_http_action(self):
     slots = {
         "bot": "5f50fd0a56b698ca10d35d2e",
         "http_action_config": "test_run_invalid_http_action",
         "param2": "param2value"
     }
     events = [{"event1": "hello"}, {"event2": "how are you"}]
     HttpActionConfig(
         auth_token="bearer kjflksjflksajfljsdflinlsufisnflisjbjsdalibvs",
         action_name="test_run_invalid_http_action1",
         response="json",
         http_url="http://www.google.com",
         request_method="GET",
         params_list=None,
         bot="5f50fd0a56b698ca10d35d2e",
         user="******").save()
     dispatcher: CollectingDispatcher = CollectingDispatcher()
     tracker = Tracker(sender_id="sender1",
                       slots=slots,
                       events=events,
                       paused=False,
                       latest_message=None,
                       followup_action=None,
                       active_loop=None,
                       latest_action_name=None)
     domain: Dict[Text, Any] = None
     HttpAction().run(dispatcher, tracker, domain)
     str(dispatcher.messages[0]['text']).__contains__(
         "I have failed to process your request: No HTTP action found for bot"
     )
예제 #9
0
 def test_run_no_connection(self):
     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
     HttpActionConfig(auth_token="",
                      action_name="test_run_with_post",
                      response="This should be response",
                      http_url="http://localhost:8080/mock",
                      request_method="GET",
                      params_list=None,
                      bot="5f50fd0a56b698ca10d35d2e",
                      user="******").save()
     actual: List[Dict[Text,
                       Any]] = HttpAction().run(dispatcher, tracker, domain)
     assert actual is not None
     assert str(actual[0]['name']).__contains__(
         'I have failed to process your request')
예제 #10
0
    def test_run_with_get_placeholder_vs_string_response(self):
        http_url = 'http://localhost:8080/mock'
        resp_msg = "This is string http response"
        responses.add(
            method=responses.GET,
            url=http_url,
            body=resp_msg,
            status=200,
        )

        slots = {"bot": "5f50fd0a56b698ca10d35d2e",
                 "http_action_config": "test_run_with_get_string_http_response_placeholder_required"}
        events = [{"event1": "hello"}, {"event2": "how are you"}]
        dispatcher: CollectingDispatcher = CollectingDispatcher()
        tracker = Tracker(sender_id="sender1", slots=slots, events=events, paused=False, latest_message=None,
                          followup_action=None, active_form=None, latest_action_name=None)
        domain: Dict[Text, Any] = None
        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="******"
        ).save().to_mongo().to_dict()
        actual: List[Dict[Text, Any]] = HttpAction().run(dispatcher, tracker, domain)
        assert actual is not None
        assert str(
            actual[0]['name']) == 'I have failed to process your request: Could not find value for keys in response'
예제 #11
0
    def test_run_with_post(self):
        # request_params = {'data': 'test_data', 'test_class': [{'key': 'value'}, {'key2': 'value2'}]}
        # request_params = [HttpActionRequestBody(key='data', value="test_data"),
        #                   HttpActionRequestBody(key='test_class', value=[{'key': 'value'}, {'key2': 'value2'}])]
        http_url = 'http://localhost:8080/mock'
        resp_msg = "5000"
        responses.add(
            method=responses.POST,
            url=http_url,
            body=resp_msg,
            status=200,
        )

        slots = {"bot": "5f50fd0a56b698ca10d35d2e", "http_action_config": "test_run_with_post"}
        events = [{"event1": "hello"}, {"event2": "how are you"}]
        dispatcher: CollectingDispatcher = CollectingDispatcher()
        tracker = Tracker(sender_id="sender1", slots=slots, events=events, paused=False, latest_message=None,
                          followup_action=None, active_form=None, latest_action_name=None)
        domain: Dict[Text, Any] = None
        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="******"
        ).save().to_mongo().to_dict()
        actual: List[Dict[Text, Any]] = HttpAction().run(dispatcher, tracker, domain)
        assert actual is not None
        assert actual[0]['name'] == 'Data added successfully, id:5000'
예제 #12
0
 def test_run(self):
     slots = {
         "bot": "5f50fd0a56b698ca10d35d2e",
         "http_action_config": "test_run",
         "param2": "param2value"
     }
     events = [{"event1": "hello"}, {"event2": "how are you"}]
     dispatcher: CollectingDispatcher = CollectingDispatcher()
     tracker = Tracker(sender_id="sender1",
                       slots=slots,
                       events=events,
                       paused=False,
                       latest_message=None,
                       followup_action=None,
                       active_loop=None,
                       latest_action_name=None)
     domain: Dict[Text, Any] = None
     HttpActionConfig(
         auth_token="bearer kjflksjflksajfljsdflinlsufisnflisjbjsdalibvs",
         action_name="test_run",
         response="This should be response",
         http_url="http://www.google.com",
         request_method="GET",
         params_list=None,
         bot="5f50fd0a56b698ca10d35d2e",
         user="******").save().to_mongo().to_dict()
     actual: List[Dict[Text,
                       Any]] = HttpAction().run(dispatcher, tracker, domain)
     assert actual is not None
     assert actual[0]['name'] == 'This should be response'
예제 #13
0
    def test_run_with_get(self):
        http_url = 'http://localhost:8080/mock'
        resp_msg = json.dumps({
            "a": {
                "b": {
                    "3": 2,
                    "43": 30,
                    "c": [],
                    "d": ['red', 'buggy', 'bumpers'],
                }
            }
        })
        responses.add(
            method=responses.GET,
            url=http_url,
            body=resp_msg,
            status=200,
        )

        slots = {
            "bot": "5f50fd0a56b698ca10d35d2e",
            "http_action_config": "test_run_with_post"
        }
        events = [{"event1": "hello"}, {"event2": "how are you"}]
        dispatcher: CollectingDispatcher = CollectingDispatcher()
        tracker = Tracker(sender_id="sender1",
                          slots=slots,
                          events=events,
                          paused=False,
                          latest_message=None,
                          followup_action=None,
                          active_loop=None,
                          latest_action_name=None)
        domain: Dict[Text, Any] = None
        HttpActionConfig(
            auth_token="",
            action_name="test_run_with_post",
            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="******").save().to_mongo().to_dict()
        actual: List[Dict[Text,
                          Any]] = HttpAction().run(dispatcher, tracker, domain)
        assert actual is not None
        assert str(
            actual[0]['name']
        ) == 'The value of 2 in red is [\'red\', \'buggy\', \'bumpers\']'
예제 #14
0
    def test_get_http_action_no_request_body(self):
        http_params = []
        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")
예제 #15
0
    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(pytest.db_url, "bot", "http_action1")
            assert False
        except HttpActionFailure as ex:
            assert str(ex).__contains__("No HTTP action found for bot")