def test_get_user_error_query_record(self): user_id = DataCommon.USER_ID_TEST.format(str(3)) # mock object query_key = patch('premembers.repository.pm_userAttribute.query_key') # start mock object mock_query_key = query_key.start() mock_error_exception = mock_common_utils.mock_error_exception(self) # mock data mock_query_key.side_effect = PmError() # addCleanup stop mock object self.addCleanup(query_key.stop) # call Function test response = user_logic.get_user_attributes(user_id) # assert output function # check call function common write log error mock_error_exception.assert_called_once() # assert function response_body = json.loads(response['body']) err_402 = MsgConst.ERR_402 self.assertEqual(response_body['code'], err_402['code']) self.assertEqual(response_body['message'], err_402['message']) self.assertEqual(response_body['description'], err_402['description']) self.assertEqual(response['statusCode'], HTTPStatus.INTERNAL_SERVER_ERROR.value)
def test_execute_security_check_from_external_case_error_get_pm_securityCheckWebhook(self): # patch mock patch_query_webhook_index = patch( "premembers.repository.pm_securityCheckWebhook.query_webhook_index" ) # start mock object mock_query_webhook_index = patch_query_webhook_index.start() mock_error_exception = mock_common_utils.mock_error_exception(self) # mock object mock_query_webhook_index.side_effect = PmError() # addCleanup stop mock object self.addCleanup(patch_query_webhook_index.stop) # call function test actual_response = awschecks_logic.execute_security_check_from_external( trace_id, webhook_path) # assert output function # check call function common write log error mock_error_exception.assert_called_once() # assert output function actual_response_body = json.loads(actual_response["body"]) err_402 = MsgConst.ERR_402 self.assertEqual(err_402["code"], actual_response_body["code"]) self.assertEqual(err_402["message"], actual_response_body["message"]) self.assertEqual(err_402["description"], actual_response_body["description"]) self.assertEqual(HTTPStatus.INTERNAL_SERVER_ERROR.value, actual_response["statusCode"])
def test_update_user_error_update_record(self): # mock object query_key = patch('premembers.repository.pm_userAttribute.query_key') update_user = patch('premembers.repository.pm_userAttribute.update') mock_error_exception = mock_common_utils.mock_error_exception(self) # start mock object mock_query_key = query_key.start() mock_update_user = update_user.start() # mock data mock_query_key.return_value = True mock_update_user.side_effect = PmError() # addCleanup stop mock object self.addCleanup(query_key.stop) self.addCleanup(update_user.stop) # call Function test param_body = json.dumps(data_body) response = user_logic.update_user_attributes(user_id, param_body) # assert output function # check call function common write log error mock_error_exception.assert_called_once() # assert function response_body = json.loads(response['body']) err_404 = MsgConst.ERR_DB_404 self.assertEqual(response_body['code'], err_404['code']) self.assertEqual(response_body['message'], err_404['message']) self.assertEqual(response_body['description'], err_404['description']) self.assertEqual(response['statusCode'], HTTPStatus.INTERNAL_SERVER_ERROR.value)
def test_delete_excluded_resources_case_error_get_record_pm_aws_account_coops( self): # mock object patch_query_awscoop_coop_key = patch( "premembers.repository.pm_awsAccountCoops.query_awscoop_coop_key") # start mock object mock_query_awscoop_coop_key = patch_query_awscoop_coop_key.start() mock_error_exception = mock_common_utils.mock_error_exception(self) # mock data mock_query_awscoop_coop_key.side_effect = PmError() # addCleanup stop mock object self.addCleanup(patch_query_awscoop_coop_key.stop) # call Function test actual_response = checkitemsettings_logic.delete_excluded_resources( trace_id, organization_id, project_id, check_item_code, coop_id, region_name, resource_type, resource_name) # assert output function # check call function common write log error mock_error_exception.assert_called_once() # assert output function actual_response_body = json.loads(actual_response["body"]) err_402 = MsgConst.ERR_402 self.assertEqual(err_402["code"], actual_response_body["code"]) self.assertEqual(err_402["message"], actual_response_body["message"]) self.assertEqual(err_402["description"], actual_response_body["description"]) self.assertEqual(HTTPStatus.INTERNAL_SERVER_ERROR.value, actual_response["statusCode"])
def test_execute_security_check_with_executed_type_case_error_get_projects_by_organization_id(self): # patch mock patch_get_projects_by_organization_id = patch( "premembers.repository.pm_projects.get_projects_by_organization_id" ) # start mock object mock_get_projects_by_organization_id = patch_get_projects_by_organization_id.start() mock_error_exception = mock_common_utils.mock_error_exception(self) # mock object mock_get_projects_by_organization_id.side_effect = PmError() # addCleanup stop mock object self.addCleanup(patch_get_projects_by_organization_id.stop) # call function test actual_response = awschecks_logic.execute_security_check_with_executed_type( trace_id, organization_id, project_id, execute_user_id, email, "MANUAL") # assert output function # check call function common write log error mock_error_exception.assert_called_once() # assert output function actual_response_body = json.loads(actual_response["body"]) err_402 = MsgConst.ERR_402 self.assertEqual(err_402["code"], actual_response_body["code"]) self.assertEqual(err_402["message"], actual_response_body["message"]) self.assertEqual(err_402["description"], actual_response_body["description"]) self.assertEqual(HTTPStatus.INTERNAL_SERVER_ERROR.value, actual_response["statusCode"])
def test_create_excluded_resources_case_parse_json_error(self): body_object = '{"json_invalid"}' # mock object patch_query_awscoop_coop_key = patch( "premembers.repository.pm_awsAccountCoops.query_awscoop_coop_key") # start mock object mock_query_awscoop_coop_key = patch_query_awscoop_coop_key.start() # mock function error_exception mock_error_exception = mock_common_utils.mock_error_exception(self) # mock data mock_query_awscoop_coop_key.return_value = data_pm_aws_account_coops # addCleanup stop mock object self.addCleanup(patch_query_awscoop_coop_key.stop) # call Function test response = checkitemsettings_logic.create_excluded_resources( trace_id, user_id, organization_id, project_id, coop_id, check_item_code, mail_address, body_object) # assert output function # check call function common write log error mock_error_exception.assert_called_once() # check response message_202 = MsgConst.ERR_REQUEST_202 actual_response_body = json.loads(response["body"]) self.assertEqual(actual_response_body["code"], message_202["code"]) self.assertEqual(actual_response_body["message"], message_202["message"]) self.assertEqual(actual_response_body["description"], message_202["description"]) self.assertEqual(response["statusCode"], HTTPStatus.BAD_REQUEST.value)
def test_execute_security_check_with_executed_type_case_error_get_pm_check_history(self): # patch mock patch_get_projects_by_organization_id = patch( "premembers.repository.pm_projects.get_projects_by_organization_id" ) patch_create_pm_check_history = patch( "premembers.repository.pm_checkHistory.create") patch_aws_sns = patch("premembers.common.aws_common.aws_sns") patch_query_key_pm_check_history = patch("premembers.repository.pm_checkHistory.query_key") patch_get_uuid4 = patch("premembers.common.common_utils.get_uuid4") # start mock object mock_query_key_pm_check_history = patch_query_key_pm_check_history.start() mock_get_projects_by_organization_id = patch_get_projects_by_organization_id.start() mock_create_pm_check_history = patch_create_pm_check_history.start() mock_aws_sns = patch_aws_sns.start() mock_get_uuid4 = patch_get_uuid4.start() mock_error_exception = mock_common_utils.mock_error_exception(self) # mock object mock_query_key_pm_check_history.side_effect = PmError() mock_get_projects_by_organization_id.return_value = data_pm_project mock_create_pm_check_history.side_effect = None mock_aws_sns.side_effect = None mock_get_uuid4.return_value = check_history_id # addCleanup stop mock object self.addCleanup(patch_get_projects_by_organization_id.stop) self.addCleanup(patch_create_pm_check_history.stop) self.addCleanup(patch_aws_sns.stop) self.addCleanup(patch_get_uuid4.stop) # call function test actual_response = awschecks_logic.execute_security_check_with_executed_type( trace_id, organization_id, project_id, execute_user_id, email, "MANUAL") # assert output function # check call function common write log error mock_error_exception.assert_called_once() # assert call aws_sns topic_arn = common_utils.get_environ( CommonConst.SECURITYCHECK_EXECUTE_TOPIC) subject = "USER : {0}".format(execute_user_id) message = {'CheckHistoryId': check_history_id} mock_aws_sns.assert_called_once_with(trace_id, subject, json.dumps(message), topic_arn) # assert output function actual_response_body = json.loads(actual_response["body"]) err_402 = MsgConst.ERR_402 self.assertEqual(err_402["code"], actual_response_body["code"]) self.assertEqual(err_402["message"], actual_response_body["message"]) self.assertEqual(err_402["description"], actual_response_body["description"]) self.assertEqual(HTTPStatus.INTERNAL_SERVER_ERROR.value, actual_response["statusCode"])
def test_create_excluded_resources_case_error_create_excluded_resourcess( self): trace_id = user_id_authority_owner param_body = { "regionName": region_name, "resourceName": resource_name, "resourceType": resource_type, "exclusionComment": exclusion_comment } body_object = json.dumps(param_body) # mock object patch_query_awscoop_coop_key = patch( "premembers.repository.pm_awsAccountCoops.query_awscoop_coop_key") patch_query_filter_region_name_and_resource_name = patch( "premembers.repository.pm_exclusionResources.query_filter_region_name_and_resource_name" ) patch_create_pm_exclusionResources = patch( "premembers.repository.pm_exclusionResources.create") # mock function error_exception mock_error_exception = mock_common_utils.mock_error_exception(self) mock_query_awscoop_coop_key = patch_query_awscoop_coop_key.start() mock_query_filter_region_name_and_resource_name = patch_query_filter_region_name_and_resource_name.start( ) mock_create_pm_exclusionResources = patch_create_pm_exclusionResources.start( ) # mock data mock_query_awscoop_coop_key.return_value = data_pm_aws_account_coops mock_query_filter_region_name_and_resource_name.return_value = None mock_create_pm_exclusionResources.side_effect = Exception() # addCleanup stop mock object self.addCleanup(patch_query_awscoop_coop_key.stop) self.addCleanup(patch_query_filter_region_name_and_resource_name.stop) self.addCleanup(patch_create_pm_exclusionResources.stop) # call Function test actual_response = checkitemsettings_logic.create_excluded_resources( trace_id, user_id_authority_owner, organization_id, project_id, coop_id, check_item_code, mail_address, body_object) # assert output function # check call function common write log error mock_error_exception.assert_called_once() # assert output function actual_response_body = json.loads(actual_response["body"]) err_db_403 = MsgConst.ERR_DB_403 self.assertEqual(err_db_403["code"], actual_response_body["code"]) self.assertEqual(err_db_403["message"], actual_response_body["message"]) self.assertEqual(err_db_403["description"], actual_response_body["description"]) self.assertEqual(HTTPStatus.INTERNAL_SERVER_ERROR.value, actual_response["statusCode"])
def test_get_security_check_resources_call_read_json_error_no_such_key( self): # mock object patch_query_awscoop_coop_key = patch( "premembers.repository.pm_awsAccountCoops.query_awscoop_coop_key") patch_get_security_check_detail_by_check_result_and_check_item_code = patch( "premembers.repository.pm_checkResultItems.get_security_check_detail_by_check_result_and_check_item_code" ) patch_query_key = patch( "premembers.repository.pm_latestCheckResult.query_key") # start mock object mock_get_security_check_detail_by_check_result_and_check_item_code = patch_get_security_check_detail_by_check_result_and_check_item_code.start( ) mock_query_awscoop_coop_key = patch_query_awscoop_coop_key.start() mock_query_key = patch_query_key.start() mock_error_exception = mock_common_utils.mock_error_exception(self) # mock data mock_get_security_check_detail_by_check_result_and_check_item_code.return_value = [ data_pm_check_result_items ] mock_query_awscoop_coop_key.return_value = data_pm_aws_account_coops mock_query_key.return_value = data_pm_latest_check_result # create a bucket s3_utils.create_bucket('premembers-dev-check-bucket') # addCleanup stop mock object self.addCleanup( patch_get_security_check_detail_by_check_result_and_check_item_code .stop) self.addCleanup(patch_query_awscoop_coop_key.stop) self.addCleanup(patch_query_key.stop) # call Function test actual_response = awschecks_logic.get_security_check_resource( trace_id, coop_id, project_id, organization_id, check_item_code) # assert output function # check call function common write log error mock_error_exception.assert_called_once() # assert output function actual_response_body = json.loads(actual_response["body"]) err_s3_702 = MsgConst.ERR_S3_702 self.assertEqual(err_s3_702["code"], actual_response_body["code"]) self.assertEqual(err_s3_702["message"], actual_response_body["message"]) self.assertEqual(err_s3_702["description"], actual_response_body["description"]) self.assertEqual(HTTPStatus.INTERNAL_SERVER_ERROR.value, actual_response["statusCode"])
def test_execute_change_email_case_read_yaml_error(self): # patch mock patch_read_yaml = patch('premembers.common.FileUtils.read_yaml') mock_error_exception = mock_common_utils.mock_error_exception(self) # start mock object mock_read_yaml = patch_read_yaml.start() # mock data mock_read_yaml.side_effect = PmError() # addCleanup stop mock object self.addCleanup(patch_read_yaml.stop) with patch.object(PmLogAdapter, 'error', return_value=None) as mock_method_error: # call function test result = user_logic.execute_change_email(apply_id) # Check data status_code = result['statusCode'] response_body = result['body'] response_headers = result['headers'] self.assertEqual(HTTPStatus.OK.value, status_code) self.assertEqual(response_error_page_caller_service_insightwatch, response_body) self.assertEqual(content_type_text_html, response_headers['content-type']) # check param call function read_yaml mock_read_yaml.assert_called_once_with(apply_id, s3_setting_bucket, notify_config_cis_result_mail) mock_error_exception.assert_called_once() # check write log error mock_method_error.assert_called_once_with( "メールアドレス変更通知メール送信設定ファイルの取得に失敗しました。:s3://%s/%s", common_utils.get_environ(s3_setting_bucket), notify_config_cis_result_mail)
def test_update_user_error_parse_json(self): param_body = '{"json_invalid"}' # mock function error_exception mock_error_exception = mock_common_utils.mock_error_exception(self) # call Function test response = user_logic.update_user_attributes(user_id, param_body) # assert output function # check call function common write log error mock_error_exception.assert_called_once() # Check data response_body = json.loads(response['body']) err_202 = MsgConst.ERR_REQUEST_202 self.assertEqual(response_body['code'], err_202['code']) self.assertEqual(response_body['message'], err_202['message']) self.assertEqual(response_body['description'], err_202['description']) self.assertEqual(response['statusCode'], HTTPStatus.BAD_REQUEST.value)
def test_execute_security_check_from_external_case_error_create_pm_security_check_webhook_call_history(self): # patch mock patch_query_webhook_index = patch( "premembers.repository.pm_securityCheckWebhook.query_webhook_index" ) patch_query_key = patch( "premembers.repository.pm_securityCheckWebhook.query_key") patch_query_pm_security_check_webhook_call_history = patch( "premembers.repository.pm_securityCheckWebhookCallHistory.query") patch_execute_security_check_with_executed_type = patch( "premembers.check.logic.awschecks_logic.execute_security_check_with_executed_type" ) patch_create_pm_security_check_webhook_call_history = patch( "premembers.repository.pm_securityCheckWebhookCallHistory.create") # start mock object mock_query_webhook_index = patch_query_webhook_index.start() mock_query_key = patch_query_key.start() mock_query_pm_security_check_webhook_call_history = patch_query_pm_security_check_webhook_call_history.start() mock_execute_security_check_with_executed_type = patch_execute_security_check_with_executed_type.start() mock_create_pm_security_check_webhook_call_history = patch_create_pm_security_check_webhook_call_history.start() mock_error_exception = mock_common_utils.mock_error_exception(self) # mock object mock_query_webhook_index.return_value = list_data_pm_security_check_webhooks_convert_response mock_query_key.return_value = list_data_pm_security_check_webhooks[0] mock_query_pm_security_check_webhook_call_history.return_value = list_data_pm_security_check_webhook_call_historys mock_execute_security_check_with_executed_type.return_value = { 'statusCode': HTTPStatus.CREATED, 'headers': { 'Access-Control-Allow-Origin': None }, 'body': '{"CheckCode": "CHECK_CIS", "CheckHistoryID": "278826f1-5837-406c-b9a0-8d56d0746d00-3", "CheckStatus": 0, "CreatedAt": "2019-12-12 03:18:49.949", "ErrorCode": "ErrorCode", "ExecutedDateTime": "2019-12-12 03:18:49.949", "ExecutedType": "AUTO", "OrganizationID": "0e6462f8-5e99-4243-9efa-89556574f9e4-3", "ProjectID": "fdc40fe8-240a-49c0-97da-0bc055e5ade0-3", "ReportFilePath": "ReportFilePath", "TimeToLive": 1591672729.949272, "UpdatedAt": "2019-12-12 03:18:49.949"}' } mock_create_pm_security_check_webhook_call_history.side_effect = PmError() # addCleanup stop mock object self.addCleanup(patch_query_webhook_index.stop) self.addCleanup(patch_query_key.stop) self.addCleanup(patch_create_pm_security_check_webhook_call_history.stop) self.addCleanup(patch_execute_security_check_with_executed_type.stop) self.addCleanup( patch_query_pm_security_check_webhook_call_history.stop) # call function test actual_response = awschecks_logic.execute_security_check_from_external( trace_id, webhook_path) # assert output function # check call function common write log error mock_error_exception.assert_called_once() # assert output function actual_response_body = json.loads(actual_response["body"]) message_403 = MsgConst.ERR_DB_403 self.assertEqual(message_403["code"], actual_response_body["code"]) self.assertEqual(message_403["message"], actual_response_body["message"]) self.assertEqual(message_403["description"], actual_response_body["description"]) self.assertEqual(HTTPStatus.INTERNAL_SERVER_ERROR, actual_response["statusCode"])
def test_list_item_settings_case_error_get_record_exclusion_resource(self): # mock object patch_get_projects_by_organization_id = patch( "premembers.repository.pm_projects.get_projects_by_organization_id" ) patch_query_awscoop_coop_key = patch( "premembers.repository.pm_awsAccountCoops.query_awscoop_coop_key") patch_assessment_items_query_filter_account_refine_code = patch( "premembers.repository.pm_assessmentItems.query_filter_account_refine_code" ) patch_exclusion_items_query_filter_account_refine_code = patch( "premembers.repository.pm_exclusionitems.query_filter_account_refine_code" ) patch_exclusion_resource_query_filter_account_refine_code = patch( "premembers.repository.pm_exclusionResources.query_filter_account_refine_code" ) # start mock object mock_get_projects_by_organization_id = patch_get_projects_by_organization_id.start( ) mock_query_awscoop_coop_key = patch_query_awscoop_coop_key.start() mock_exclusion_items_query_filter_account_refine_code = patch_exclusion_items_query_filter_account_refine_code.start( ) mock_assessment_items_query_filter_account_refine_code = patch_assessment_items_query_filter_account_refine_code.start( ) mock_exclusion_resource_query_filter_account_refine_code = patch_exclusion_resource_query_filter_account_refine_code.start( ) mock_error_exception = mock_common_utils.mock_error_exception(self) # mock data mock_get_projects_by_organization_id.return_value = data_pm_project mock_query_awscoop_coop_key.return_value = data_pm_aws_account_coops mock_exclusion_items_query_filter_account_refine_code.return_value = [] mock_assessment_items_query_filter_account_refine_code.return_value = [] mock_exclusion_resource_query_filter_account_refine_code.side_effect = PmError( ) # addCleanup stop mock object self.addCleanup(patch_get_projects_by_organization_id.stop) self.addCleanup(patch_query_awscoop_coop_key.stop) self.addCleanup( patch_exclusion_items_query_filter_account_refine_code.stop) self.addCleanup( patch_assessment_items_query_filter_account_refine_code.stop) self.addCleanup( patch_exclusion_resource_query_filter_account_refine_code.stop) # call Function test actual_response = checkitemsettings_logic.list_item_settings( trace_id, organization_id, project_id, coop_id, group_filter) # assert output function # check call function common write log error mock_error_exception.assert_called_once() # assert output function actual_response_body = json.loads(actual_response["body"]) err_402 = MsgConst.ERR_402 self.assertEqual(err_402["code"], actual_response_body["code"]) self.assertEqual(err_402["message"], actual_response_body["message"]) self.assertEqual(err_402["description"], actual_response_body["description"]) self.assertEqual(HTTPStatus.INTERNAL_SERVER_ERROR.value, actual_response["statusCode"])