def test_20_bad_start(self): """(20) Test start session endpoint used by UI. Send an unknown field""" msgt(self.test_20_bad_start.__doc__) # url and info for call # url = reverse('StartSession') info = dict(user_agent='secret_agent_man', unknown_field='what\'s this?') response = self.client.post(url, json.dumps(info), content_type="application/json") # 200 response # self.assertEqual(response.status_code, 200) # convert to JSON # json_resp = response.json() print('json_resp', json_resp) # status code 'FAILED_PRECONDITION' # self.assertEqual(json_resp['responseInfo']['status']['code'], STATUS_VAL_FAILED_PRECONDITION) # error message found # err_snippet = '"SessionRequest" has no field named "unknown_field"' idx = json_resp['responseInfo']['status']['details'].find(err_snippet) self.assertTrue(idx > -1)
def test_10_good_start(self): """(10) Test start session endpoint used by UI""" msgt(self.test_10_good_start.__doc__) # url and info for call # url = reverse('StartSession') info = dict(user_agent='user_agent') response = self.client.post(url, json.dumps(info), content_type="application/json") # 200 response # self.assertEqual(response.status_code, 200) # convert to JSON # json_resp = response.json() print('json_resp', json_resp) # status code 'OK' # self.assertEqual(json_resp['responseInfo']['status']['code'], STATUS_VAL_OK) # sessionId returned # self.assertTrue('sessionId' in json_resp['context']) # session id length is reasonable # self.assertTrue(len(json_resp['context']['sessionId']) > 5)
def test_30_bad_start(self): """(30) Test start session endpoint used by UI. Don't send a user agent""" msgt(self.test_30_bad_start.__doc__) # url and info for call # url = reverse('StartSession') info = dict() response = self.client.post(url, json.dumps(info), content_type="application/json") # 200 response # self.assertEqual(response.status_code, 200) # convert to JSON # json_resp = response.json() print('response: ', json_resp) # status code 'FAILED_PRECONDITION' # self.assertEqual(json_resp['responseInfo']['status']['code'], STATUS_VAL_FAILED_PRECONDITION) # error message found # idx = json_resp['responseInfo']['status']['details'].find( ERR_MSG_NO_USER_AGENT) self.assertTrue(idx > -1)
def test_50_bad_end(self): """(50) Test the end session endpoint. Error: Don't include a session_id""" msgt(self.test_50_bad_end.__doc__) # url and info for call # url = reverse('EndSession') info = dict(no_session_id='session_0') response = self.client.post(url, json.dumps(info), content_type="application/json") # 200 response # self.assertEqual(response.status_code, 200) # convert to JSON # json_resp = response.json() print('json_resp', json_resp) # status code 'OK' # self.assertEqual(json_resp['status']['code'], STATUS_VAL_FAILED_PRECONDITION) # error message found # idx = json_resp['status']['details'].find(ERR_NO_SESSION_ID) self.assertTrue(idx > -1)
def test_20_send_badvar_name(self): """(20) Forget pipeline id and fail""" msgt(self.test_20_send_badvar_name.__doc__) # url and info for call # url = reverse('DescribeDataflow') info = dict(context=dict(xsessionId='session_01')) response = self.client.post(url, json.dumps(info), content_type="application/json") # 200 response # self.assertEqual(response.status_code, 200) # convert to JSON # json_resp = response.json() #print('json_resp', json.dumps(json_resp, indent=4)) # status code 'FAILED_PRECONDITION' # self.assertEqual(json_resp['responseInfo']['status']['code'], STATUS_VAL_FAILED_PRECONDITION) # error message found # err_snippet = ("Message type \"SessionContext\"" " has no field named \"xsessionId\"") idx = json_resp['responseInfo']['status']['details'].find(err_snippet) self.assertTrue(idx > -1)
def test_030_EndSearchSolutions(self): """(30) Test EndSearchSolutions""" msgt(self.test_030_EndSearchSolutions.__doc__) # url and info for call # url = reverse('EndSearchSolutions') info = dict(searchId='searchId') response = self.client.post(url, json.dumps(info), content_type="application/json") # 200 response # self.assertEqual(response.status_code, 200) # convert to JSON # json_resp = response.json() print('json_resp', json_resp) self.assertTrue(json_resp['success']) self.assertTrue('data' in json_resp) print(ta2_static.END_SEARCH_SOLUTIONS) # Updated so that ending the search flushes the log to a file # and deletes the entries... # cnt = BehavioralLogEntry.objects.all().count() self.assertEqual(cnt, 0)
def test_20_list_workspaces_valid_user(self): """(20) List workspaces with a valid user""" msgt(self.test_20_list_workspaces_valid_user.__doc__) user = User.objects.get(username='******') # print('\n -- Retrieve the user workspaces. Do NOT create a default one') # ws_info = ws_utils.get_user_workspaces(user, create_if_not_found=False) self.assertTrue(not ws_info.success) # print(('\n -- Retrieve the user workspaces.' ' Create if one not found. (This is the default behavior)')) # ws_info2 = ws_utils.get_user_workspaces(user) self.assertTrue(ws_info2.success) self.assertEqual(len(ws_info2.result_obj), 1) # print(('\n -- Retrieve the user workspaces as list of dicts.')) # ws_info3 = ws_utils.get_user_workspaces_as_dict(user) self.assertTrue(ws_info3.success) self.assertEqual(len(ws_info3.result_obj), 1)
def test_30_workspace_by_view(self): """(30) Get workspace JSON via view""" msgt(self.test_30_workspace_by_view.__doc__) # Get a user user_obj = User.objects.get_or_create(username='******')[0] # Get workspace list for a legit id success, ws_list = WorkspaceRetriever.list_workspaces_by_user(user_obj) expected_workspace = ws_list[0] ws_id = expected_workspace.id # create a web client and login client = Client() client.force_login(user_obj) # retrieve the workspace url = reverse('view_workspace_by_id_json', kwargs=dict(workspace_id=ws_id)) response1 = client.get(url) # 200 status code # self.assertEqual(response1.status_code, 200) json_resp = response1.json() self.assertEqual(json_resp['app_domain'], expected_workspace.app_domain) self.assertEqual(json_resp['user']['username'], expected_workspace.user.username) self.assertEqual(json_resp['data_source_type']['name'], expected_workspace.data_source_type.name)
def test_10_good_request(self): """(10) Test endpoint used by UI, with successful result""" msgt(self.test_10_good_request.__doc__) # url and info for call # pipeline_id = 'pipeline_222' url = reverse('GetDataflowResults') info = dict(context=dict(sessionId='session_01'), pipelineId=pipeline_id) response = self.client.post(url, json.dumps(info), content_type="application/json") # 200 response # self.assertEqual(response.status_code, 200) # convert to JSON # json_resp = response.json() #print('json_resp', json_resp) # self.assertTrue(len(json_resp), 4) # 2nd result is status RUNNING # self.assertTrue(json_resp[1]['status'], 'RUNNING') # 3rd has two output entries # self.assertTrue(len(json_resp[2]['outputs']), 2)
def test_10_list_workspaces_null_user(self): """(10) List workspaces by null user""" msgt(self.test_10_list_workspaces_null_user.__doc__) success, ws_list = WorkspaceRetriever.list_workspaces_by_user(None) self.assertEqual(success, False) self.assertEqual(ws_list, ERR_AUTH_USER_IS_NONE)
def test_080_ProduceSolution(self): """(80) Test ProduceSolution""" msgt(self.test_080_ProduceSolution.__doc__) # url and info for call # url = reverse('ProduceSolution') req_str = render_to_string('test_requests/req_ProduceSolution.json', {}) response = self.client.post(url, req_str, content_type="application/json") # 200 response # self.assertEqual(response.status_code, 200) # convert to JSON # json_resp = response.json() print('json_resp', json_resp) self.assertTrue(json_resp['success']) self.assertTrue('data' in json_resp) self.assertTrue('requestId' in json_resp['data'])
def test_050_DescribeSolution(self): """(50) Test DescribeSolution""" msgt(self.test_050_DescribeSolution.__doc__) # url and info for call # url = reverse('DescribeSolution') info = dict(solutionId='solutionId') response = self.client.post(url, json.dumps(info), content_type="application/json") # 200 response # self.assertEqual(response.status_code, 200) # convert to JSON # json_resp = response.json() print('json_resp', json_resp) self.assertTrue(json_resp['success']) self.assertTrue('data' in json_resp) self.assertTrue('pipeline' in json_resp['data']) self.assertTrue('inputs' in json_resp['data']['pipeline']) self.assertTrue('outputs' in json_resp['data']['pipeline']) self.assertTrue('steps' in json_resp['data']['pipeline']) self.assertTrue('steps' in json_resp['data'])
def test_10_redirect_to_login(self): """(10) Redirect to login when in D3M mode""" msgt(self.test_10_redirect_to_login.__doc__) # Attempt to go the main workspace # url = reverse('home') # -------------------------------- # Look for 302 redirect # -------------------------------- resp1 = self.client.get(url) # 302 response - redirect # self.assertEqual(resp1.status_code, 302) # -------------------------------- # Follow redirect to login page # -------------------------------- resp2 = self.client.get(url, follow=True) login_found = resp2.content.decode('utf-8').find(LOGIN_STR) > -1 self.assertTrue(login_found) username_found = resp2.content.decode('utf-8').find(USERNAME_STR) > -1 self.assertTrue(username_found)
def test_040_StopSearchSolutions(self): """(40) Test StopSearchSolutions""" msgt(self.test_040_StopSearchSolutions.__doc__) # url and info for call # url = reverse('StopSearchSolutions') info = dict(searchId='searchId') response = self.client.post(url, json.dumps(info), content_type="application/json") # 200 response # self.assertEqual(response.status_code, 200) # convert to JSON # json_resp = response.json() print('json_resp', json_resp) self.assertTrue(json_resp['success']) self.assertTrue('data' in json_resp) self.assertEqual(BehavioralLogEntry.objects.filter(\ feature_id=ta2_static.STOP_SEARCH_SOLUTIONS).count(), 1)
def test_20_log_entry_valid(self): """(20) Send valid log entries via API""" msgt(self.test_20_log_entry_valid.__doc__) user = User.objects.get(username='******') # create a web client # client = Client() client.force_login(user) # create log entry url # url = reverse('view_create_log_entry_verbose') # retrieve log entry params # params = self.get_test_params_as_dict('log_params_01.json') # make the POST request # resp = client.post(url, params, content_type='application/json').json() print('resp', resp) self.assertTrue(resp['success']) self.assertTrue(resp['data']['id'])
def test_140_GetProduceSolutionResults(self): """(140) Test GetProduceSolutionResults""" msgt(self.test_140_GetProduceSolutionResults.__doc__) # url and info for call # url = reverse('GetProduceSolutionResults') info = dict(request_id='request_id') response = self.client.post(url, json.dumps(info), content_type="application/json") # 200 response # self.assertEqual(response.status_code, 200) # convert to JSON # json_resp = response.json() print('json_resp', json_resp) self.assertTrue(json_resp['success']) self.assertTrue('data' in json_resp) self.assertTrue('responses' in json_resp['data']) self.assertTrue(json_resp['data']['request_type'] == \ 'GetProduceSolutionResults') self.assertTrue(json_resp['data']['responses']['count'] == 1) self.assertTrue(json_resp['data']['responses']['unread_count'] == 0) self.assertTrue('list' in json_resp['data']['responses'])
def test_10_retrieve_history(self): """(10) Test the SearchHistoryUtil""" msgt(self.test_10_retrieve_history.__doc__) search_id = 1 search_history_util = SearchHistoryUtil(search_id=search_id) self.assertEqual(search_history_util.has_error(), False) if search_history_util.has_error(): print(f'Error found: {search_history_util.get_error_message()}') return json_history = search_history_util.get_finalized_history() for item in json_history: print('item', json.dumps(item, indent=4)) return print('\n') print('-' * 40) req_text = (f"{item['request_type']} - (id: {item['id']})" f" ") print(req_text) for resp_item in item['response_list']: print(( f" - (id: {resp_item['id']}) - (pipeline_id: {resp_item['pipeline_id']})" f" - ({resp_item['status']})"))
def test_100_ListPrimitives(self): """(100) Test ListPrimitives""" msgt(self.test_100_ListPrimitives.__doc__) # url and info for call # url = reverse('ListPrimitives') req_str = render_to_string('test_requests/req_ListPrimitives.json', {}) response = self.client.post(url, req_str, content_type="application/json") # 200 response # self.assertEqual(response.status_code, 200) # convert to JSON # json_resp = response.json() print('json_resp', json_resp) self.assertTrue(json_resp['success']) self.assertTrue('data' in json_resp) self.assertTrue('primitives' in json_resp['data']) self.assertEqual(len(json_resp['data']['primitives']), 3) # check the log self.assertEqual(BehavioralLogEntry.objects.filter(\ feature_id=ta2_static.LIST_PRIMITIVES).count(), 1)
def test_40_good_end(self): """(40) Test the end session endpoint""" msgt(self.test_40_good_end.__doc__) # url and info for call # url = reverse('EndSession') info = dict(session_id='session_0') response = self.client.post(url, json.dumps(info), content_type="application/json") # 200 response # self.assertEqual(response.status_code, 200) # convert to JSON # json_resp = response.json() print('json_resp', json_resp) # status code 'OK' # self.assertEqual(json_resp['status']['code'], STATUS_VAL_OK)
def test_030_retrieve_object(self): """(30) Test retrieval of particular object""" msgt(self.test_030_retrieve_object.__doc__) url = reverse('api_add_event_data_query') response = self.client.post(url, json.dumps(self.input_json1), content_type="application/json") # 200 response # self.assertEqual(response.status_code, 200) # get object url_get_obj = reverse('api_retrieve_event_data_query', kwargs={'job_id': 1}) response_list = self.client.get(url_get_obj) self.assertEqual(response_list.status_code, 200) # print("json res", response_list) # self.assertEqual(json.loads(response_list)['name'], 'query1') json_resp = response_list.json() obj = json_resp['data'] self.assertEqual(obj['name'], 'query_1')
def test_020_list(self): """(20) Test list all objects""" msgt(self.test_020_list.__doc__) url = reverse('api_add_event_data_query') response1 = self.client.post(url, json.dumps(self.input_json1), content_type="application/json") response2 = self.client.post(url, json.dumps(self.input_json2), content_type="application/json") # 200 response # self.assertEqual(response1.status_code, 200) self.assertEqual(response2.status_code, 200) # retrieve objects url_list = reverse('api_get_event_data_queries') response_list = self.client.get(url_list) self.assertEqual(response_list.status_code, 200) # convert to JSON # json_resp = response_list.json() obj = json_resp['data'] print("--------------- data obj -------------", obj) # print('****json resp ****', json.loads(job)['data']) self.assertEqual(obj[0]['id'], 2) self.assertEqual(obj[1]['id'], 1) self.assertEqual(obj[0]['name'], 'query_2') self.assertEqual(obj[1]['name'], 'query_1')
def test_020_SearchSolutions(self): """(20) Test SearchSolutions""" msgt(self.test_020_SearchSolutions.__doc__) # url and info for call # url = reverse('SearchSolutions') req_str = render_to_string(\ 'test_requests/req_SearchSolutions.json', dict(TA3TA2_API_VERSION=TA3TA2Util.get_api_version(), TA3_GRPC_USER_AGENT=settings.TA3_GRPC_USER_AGENT)) response = self.client.post(url, req_str, content_type="application/json") # 200 response # self.assertEqual(response.status_code, 200) # convert to JSON # json_resp = response.json() print('json_resp', json_resp) self.assertTrue(json_resp['success']) self.assertTrue('data' in json_resp) self.assertTrue('searchId' in json_resp['data'])
def test_10_good_get_results(self): """(10) Success. Test GetCreatePipelineResults endpoint used by UI""" msgt(self.test_10_good_get_results.__doc__) # url and info for call # url = reverse('GetCreatePipelineResults') info_dict = { "context": { "session_id": "session_0" }, "pipeline_ids": ["pipeline_01", "pipeline_02"] } response = self.client.post(url, json.dumps(info_dict), content_type="application/json") # 200 response # self.assertEqual(response.status_code, 200) # convert to JSON # json_resp = response.json() #print('json_resp', json_resp[:200]) #print(len(json_resp)) # expect list of 6 responses # self.assertEqual(len(json_resp), 6) # status code 'OK' # fifth_resp = json_resp[4] self.assertEqual(fifth_resp['responseInfo']['status']['code'], STATUS_VAL_OK) # progressInfo is "COMPLETED" # self.assertEqual(fifth_resp['progressInfo'], STATUS_VAL_COMPLETED) # There is a result uri # self.assertTrue('predictResultUri' in fifth_resp['pipelineInfo']) # There is a predictResultData # self.assertTrue('predictResultData' in fifth_resp['pipelineInfo']) # predictResultData contains # the keys "success" and "data" # self.assertTrue(KEY_SUCCESS in\ fifth_resp['pipelineInfo']['predictResultData']) self.assertTrue(KEY_DATA in\ fifth_resp['pipelineInfo']['predictResultData'])
def test_10_good_create(self): """(10) Test create pipelines endpoint used by UI""" msgt(self.test_10_good_create.__doc__) # url and info for call # url = reverse('CreatePipelines') info_dict = load_template_as_dict( 'test_requests/req_create_pipeline.json') #response = self.client.post(url, format_info_for_request(info_dict)) response = self.client.post(url, json.dumps(info_dict), content_type="application/json") # 200 response # self.assertEqual(response.status_code, 200) # convert to JSON # json_resp = response.json() # expect list of 6 responses # self.assertEqual(len(json_resp), 6) # status code 'OK' # fifth_resp = json_resp[4] self.assertEqual(fifth_resp['responseInfo']['status']['code'], STATUS_VAL_OK) # progressInfo is "COMPLETED" # self.assertEqual(fifth_resp['progressInfo'], STATUS_VAL_COMPLETED) # There is a result uri # self.assertTrue('predictResultUri' in fifth_resp['pipelineInfo']) # There is a predictResultData # self.assertTrue('predictResultData' in fifth_resp['pipelineInfo']) # predictResultData contains # the keys "success" and "data" # self.assertTrue(KEY_SUCCESS in\ fifth_resp['pipelineInfo']['predictResultData']) self.assertTrue(KEY_DATA in\ fifth_resp['pipelineInfo']['predictResultData'])
def show_results(self): """print the results to the screen""" result_cnt = 0 for resp_id, pipeline_result in self.pipeline_results.items(): result_cnt += 1 msgt('(%s) id: %s, steps: %s' % \ (result_cnt, resp_id, pipeline_result.get_num_pipeline_steps())) print('pipeline_result', type(pipeline_result)) msg(pipeline_result.__str__()) #[:40])
def test_10_get_version(self): """(10) Test start session endpoint used by UI""" msgt(self.test_10_get_version.__doc__) version = TA3TA2Util.get_api_version() self.assertTrue(version is not None) # 8 being YYYYMMDD, an actual version would be 2017.12.20 self.assertTrue(len(version) > 8) # assume in century 20xx self.assertTrue(version[:2] == '20')
def test_010_markup_image(self): """(10) Test markup image""" msgt(self.test_010_markup_image.__doc__) spec = self.get_test_spec() info = markup_image(spec, self.get_output_dir()) print(info) self.assertEqual(info.get(KEY_SUCCESS), True) self.assertTrue(KEY_DATA in info) self.clean_up_file(info)
def test_10_list_workspaces_not_logged_in(self): """(10) List workspaces, not logged in""" msgt(self.test_10_list_workspaces_not_logged_in.__doc__) # create a web client client = Client() # retrieve the workspace url = reverse('view_latest_raven_configs', kwargs=dict()) resp = client.get(url).json() self.assertTrue(not resp['success'])
def test_10_log_entry_not_logged_in(self): """(10) List workspaces, not logged in""" msgt(self.test_10_log_entry_not_logged_in.__doc__) # create a web client client = Client() # create log entry url url = reverse('view_create_log_entry') params = self.get_test_params_as_dict('log_params_01.json') resp = client.post(url, params, content_type='application/json').json() print('resp', resp) self.assertTrue(not resp['success'])
def run_process(self): """ Retrieve Pipeline results based on KEY_SOLUTION_ID """ params = dict(stored_request__request_type='GetSearchSolutionsResults') stored_responses = StoredResponse.objects.filter(**params) resp_cnt = stored_responses.count() if resp_cnt == 0: user_msg = ('No StoredResponse objects found of type' ' "GetSearchSolutionsResults"') msgt(user_msg) self.add_error_message(user_msg) return # Iterate through GetSearchSolutionsResults, # looking instances of solutionId # loop_cnt = 0 for sr in stored_responses: loop_cnt += 1 if not sr.response: user_msg = 'No response found' self.pipeline_results[sr.id] = \ PipelineSteps.create_with_error(user_msg) return if not KEY_SOLUTION_ID in sr.response: user_msg = 'No "%s" found in the response' % KEY_SOLUTION_ID self.pipeline_results[sr.id] = \ PipelineSteps.create_with_error(user_msg) return solution_id = sr.response[KEY_SOLUTION_ID] if not solution_id: user_msg = 'Blank "%s" found in the response' % KEY_SOLUTION_ID self.pipeline_results[sr.id] = \ PipelineSteps.create_with_error(user_msg) return self.pipeline_results[sr.id] = self.run_describe_solution( solution_id) if self.has_error(): # In case there is a timeout b/c a TA2 is not available break