示例#1
0
    def test_process_debug_request_variables_multi(self):
        home_dir = os.path.dirname(__file__) + os.sep + "testdata"
        tmp_dir = home_dir + os.sep + "tmp"
        if os.path.exists(tmp_dir):
            shutil.rmtree(tmp_dir)
        self.assertFalse(os.path.exists(tmp_dir))

        config_file = home_dir + os.sep + "config.yaml"
        arguments = MockArgumentParser(config=config_file)
        client = FlaskYadlanClient("testrest", arguments)
        self.assertIsNotNone(client)

        request = unittest.mock.Mock()
        request.data = b'{"userId": "1234567890", "utterance": "Hello"}'
        client.process_request(request)

        request = unittest.mock.Mock()
        request.data = b'{"userId": "1234567890", "variables": [{"type": "name", "key": "testname", "value": "value1"}, {"type": "data", "key": "testdata", "value": "value2"}]}'

        debugInfo, status = client.process_debug_request(request)
        self.assertEqual(5, len(debugInfo))
        self.assertEqual(200, status)

        self.assertEqual("value1",
                         debugInfo['conversations']['properties']['testname'])
        self.assertEqual(
            "value2",
            debugInfo['conversations']['data_properties']['testdata'])

        if os.path.exists(tmp_dir):
            shutil.rmtree(tmp_dir)
        self.assertFalse(os.path.exists(tmp_dir))
示例#2
0
 def test_verify_api_key_usage_inactive(self):
     arguments = MockArgumentParser()
     client = FlaskYadlanClient("yadlan", arguments)
     self.assertIsNotNone(client)
     client.configuration.client_configuration._use_api_keys = False
     request = unittest.mock.Mock()
     self.assertEqual((None, None), client.verify_api_key_usage(request))
示例#3
0
    def test_get_api_key(self):
        arguments = MockArgumentParser()
        client = FlaskYadlanClient("yadlan", arguments)

        request = unittest.mock.Mock()
        request.args = {}
        request.args['apikey'] = '11111111'

        self.assertEqual('11111111', client.get_api_key(request))
示例#4
0
    def test_get_userid(self):
        arguments = MockArgumentParser()
        client = FlaskYadlanClient("yadlan", arguments)
        self.assertIsNotNone(client)

        request = unittest.mock.Mock()
        request.data = b'{"userId": "1234567890"}'

        self.assertEqual("1234567890", client.get_userid(request))
示例#5
0
    def test_process_debug_request_invalid_variables_parameter(self):
        arguments = MockArgumentParser()
        client = FlaskYadlanClient("yadlan", arguments)
        self.assertIsNotNone(client)

        request = unittest.mock.Mock()
        request.data = b'{"userId": "1234567890", "variables": [{"var": "test", "value": "value1"}]}'

        debugInfo, status = client.process_debug_request(request)
        self.assertEqual('Invalid variables list format', debugInfo['error'])
        self.assertEqual(400, status)
示例#6
0
    def test_process_debug_request_no_conversation(self):
        arguments = MockArgumentParser()
        client = FlaskYadlanClient("yadlan", arguments)
        self.assertIsNotNone(client)

        request = unittest.mock.Mock()
        request.data = b'{"userId": "1234567890", "variables": [{"type": "name", "key": "testname", "value": "value1"}]}'

        debugInfo, status = client.process_debug_request(request)
        self.assertEqual(0, len(debugInfo))
        self.assertEqual(200, status)
示例#7
0
 def test_verify_api_key_usage_active(self):
     arguments = MockArgumentParser()
     client = FlaskYadlanClient("yadlan", arguments)
     self.assertIsNotNone(client)
     client.configuration.client_configuration._use_api_keys = True
     client.configuration.client_configuration._api_key_file = os.path.dirname(__file__) + os.sep + ".." + os.sep + ".." + os.sep + "api_keys.txt"
     client.load_api_keys()
     request = unittest.mock.Mock()
     request.args = {}
     request.args['apikey'] = '11111111'
     self.assertEqual(({'error': 'Unauthorized access'}, 401), client.verify_api_key_usage(request))
示例#8
0
    def test_process_request_no_question(self):
        arguments = MockArgumentParser()
        client = FlaskYadlanClient("yadlan", arguments)
        self.assertIsNotNone(client)

        request = unittest.mock.Mock()
        request.args = {}
        request.data = b'{"userId": "1234567890"}'

        response, status = client.process_request(request)
        self.assertIsNotNone(response)
        self.assertEqual(status, 400)
示例#9
0
    def test_process_debug_request_with_variables(self):
        home_dir = os.path.dirname(__file__) + os.sep + "testdata"
        tmp_dir = home_dir + os.sep + "tmp"
        errors_file = tmp_dir + os.sep + "errors.txt"
        duplicates_file = tmp_dir + os.sep + "duplicates.txt"
        conversation_file = tmp_dir + os.sep + "testrest_testUser.conv"
        logs_file = tmp_dir + os.sep + "testrest_testUser.log"
        if os.path.exists(tmp_dir):
            shutil.rmtree(tmp_dir)
        self.assertFalse(os.path.exists(tmp_dir))

        config_file = home_dir + os.sep + "config.yaml"
        arguments = MockArgumentParser(config=config_file)
        client = FlaskYadlanClient("testrest", arguments)
        self.assertIsNotNone(client)

        request = unittest.mock.Mock()
        request.data = b'{"userId": "testUser", "utterance": "VARIABLES"}'

        response, _ = client.process_request(request)
        self.assertIsNotNone(response)
        self.assertEqual("testUser", response['userId'])
        self.assertEqual("VARIABLES.", response['utterance'])
        self.assertEqual("Variable datas.", response['response'])

        self.assertTrue(os.path.exists(errors_file))
        self.assertTrue(os.path.exists(duplicates_file))
        self.assertTrue(os.path.exists(conversation_file))
        self.assertTrue(os.path.exists(logs_file))

        request = unittest.mock.Mock()
        request.data = b'{"userId": "testUser"}'

        debug_info, _ = client.process_debug_request(request)
        self.assertTrue('errors' in debug_info)
        self.assertTrue('duplicates' in debug_info)
        self.assertTrue('conversations' in debug_info)
        self.assertTrue('logs' in debug_info)
        self.assertTrue('current_conversation' in debug_info)

        self.assertEqual("name_value",
                         debug_info['conversations']['properties']['name_key'])
        self.assertEqual(
            "data_value",
            debug_info['conversations']['data_properties']['data_key'])
        question = debug_info['conversations']['questions'][0]
        self.assertEqual("var_value", question['var_properties']['var_key'])

        if os.path.exists(tmp_dir):
            shutil.rmtree(tmp_dir)
        self.assertFalse(os.path.exists(tmp_dir))
示例#10
0
    def test_ask_question(self):
        arguments = MockArgumentParser()
        client = FlaskYadlanClient("yadlan", arguments)
        self.assertIsNotNone(client)
        client.configuration.client_configuration._debug = True

        request = unittest.mock.Mock()
        request.data = b'{"userId": "1234567890", "utterance": "Hello"}'

        response, _ = client.process_request(request)
        self.assertIsNotNone(response)
        self.assertEqual("1234567890", response['userId'])
        self.assertEqual("Hello.", response['utterance'])
        self.assertEqual("", response['response'])
示例#11
0
    def test_format_success_response(self):
        arguments = MockArgumentParser()
        client = FlaskYadlanClient("yadlan", arguments)
        self.assertIsNotNone(client)

        request = unittest.mock.Mock()
        request.data = b'{"userId": "1234567890"}'
        userInfo = UserInfo(client, request)

        response = client.format_success_response("1234567890", "Hello", "Hi", userInfo)
        self.assertIsNotNone(response)
        self.assertEqual("1234567890", response['userId'])
        self.assertEqual("Hello", response['utterance'])
        self.assertEqual("Hi", response['response'])
        self.assertEqual("None", response['topic'])
示例#12
0
    def test_process_debug_request_no_userid(self):
        home_dir = os.path.dirname(__file__) + os.sep + "testdata"
        tmp_dir = home_dir + os.sep + "tmp"
        errors_file = tmp_dir + os.sep + "errors.txt"
        duplicates_file = tmp_dir + os.sep + "duplicates.txt"
        conversation_file = tmp_dir + os.sep + "testrest_testUser.conv"
        logs_file = tmp_dir + os.sep + "testrest_testUser.log"
        if os.path.exists(tmp_dir):
            shutil.rmtree(tmp_dir)
        self.assertFalse(os.path.exists(tmp_dir))

        config_file = home_dir + os.sep + "config.yaml"
        arguments = MockArgumentParser(config=config_file)
        client = FlaskYadlanClient("testrest", arguments)
        self.assertIsNotNone(client)

        request = unittest.mock.Mock()
        request.data = b'{"userId": "testUser", "utterance": "Hello"}'

        response, _ = client.process_request(request)
        self.assertIsNotNone(response)
        self.assertEqual("testUser", response['userId'])
        self.assertEqual("Hello.", response['utterance'])
        self.assertEqual("HELLO, WORLD.", response['response'])

        self.assertTrue(os.path.exists(errors_file))
        self.assertTrue(os.path.exists(duplicates_file))
        self.assertTrue(os.path.exists(conversation_file))
        self.assertTrue(os.path.exists(logs_file))

        request = unittest.mock.Mock()
        request.data = b'{}'

        debug_info, _ = client.process_debug_request(request)
        self.assertTrue('errors' in debug_info)
        self.assertTrue('duplicates' in debug_info)
        self.assertFalse('conversations' in debug_info)
        self.assertFalse('logs' in debug_info)
        self.assertFalse('current_conversation' in debug_info)

        if os.path.exists(tmp_dir):
            shutil.rmtree(tmp_dir)
        self.assertFalse(os.path.exists(tmp_dir))
示例#13
0
    def test_dump_request(self):
        arguments = MockArgumentParser()
        client = FlaskYadlanClient("yadlan", arguments)
        self.assertIsNotNone(client)
        client.configuration.client_configuration._debug = True

        request = unittest.mock.Mock()
        response_data = "hello"

        latency = 1.0

        request.data = b'{"userId": "1234567890", "utterance": "Hello"}'
        client.dump_request_response(request, response_data, latency)

        request.data = b''
        client.dump_request_response(request, response_data, latency)

        request.data = b'test'
        client.dump_request_response(request, response_data, latency)
示例#14
0
 def test_rest_client_init(self):
     arguments = MockArgumentParser()
     client = FlaskYadlanClient("yadlan", arguments)
     self.assertIsNotNone(client)
示例#15
0
 def __init__(self, argument_parser=None):
     FlaskYadlanClient.__init__(self, "yadlan", argument_parser)
     self.aborted = False
     self.answer = None
     self.ask_question_exception = False