def test_call_roa_request_with_secret_error(self): def init_credentials_provider(): from alibabacloud.credentials import AccessKeyCredentials credentials = AccessKeyCredentials( access_key_id=self.access_key_id, access_key_secret="BadAccessKeySecret") from alibabacloud.credentials.provider import StaticCredentialsProvider credentials_provider = StaticCredentialsProvider(credentials) return credentials_provider client = AlibabaCloudClient( self.client_config, credentials_provider=init_credentials_provider()) client.product_code = "ROS" client.api_version = "2015-09-01" client.location_service_code = 'ros' client.location_endpoint_type = "openAPI" api_request = APIRequest('DescribeResourceTypes', 'GET', 'https', 'ROA') with self.assertRaises(ServerException) as e: client._handle_request(api_request) self.assertEqual(e.exception.service_name, "ROS") self.assertEqual(e.exception.http_status, 404) self.assertEqual(e.exception.endpoint, "ros.aliyuncs.com") self.assertEqual(e.exception.error_code, "InvalidAction.NotFound") self.assertEqual( e.exception.error_message, "Specified api is not found, please check your url and method.")
def test_call_request_with_env_config_priority(self): os.environ.setdefault("ALIBABA_CLOUD_ACCESS_KEY_ID", self.access_key_id) os.environ.setdefault("ALIBABA_CLOUD_ACCESS_KEY_SECRET", self.access_key_secret) client_config = ClientConfig(region_id=self.region_id) client = AlibabaCloudClient( client_config, credentials_provider=self.init_credentials_provider()) client.product_code = "Ecs" client.api_version = "2014-05-26" client.location_service_code = 'ecs' client.location_endpoint_type = "openAPI" api_request = APIRequest('DescribeRegions', 'GET', 'https', 'RPC') response = client._handle_request(api_request) env_credential_id = os.environ.get("ALIBABA_CLOUD_ACCESS_KEY_ID") env_credential_secret = os.environ.get( "ALIBABA_CLOUD_ACCESS_KEY_SECRET") response_key_id = response.http_request.credentials.access_key_id response_key_secret = response.http_request.credentials.access_key_secret self.assertEqual(env_credential_id, response_key_id) self.assertEqual(env_credential_secret, response_key_secret) response = response.http_response.content ret = self.get_dict_response(response) self.assertTrue(ret.get("Regions")) self.assertTrue(ret.get("RequestId")) os.environ.pop("ALIBABA_CLOUD_ACCESS_KEY_ID") os.environ.pop("ALIBABA_CLOUD_ACCESS_KEY_SECRET")
def test_call_request_with_client_env_priority(self): self._create_default_ram_user() self._create_access_key() self._create_default_ram_role() ram_role_arn_credential = RamRoleCredentialsProvider( self.client_config, AccessKeyCredentials(self.ram_user_access_key_id, self.ram_user_access_key_secret), self.ram_role_arn, "alice_test") client = AlibabaCloudClient(self.client_config, ram_role_arn_credential) client.product_code = "Ecs" client.api_version = "2014-05-26" client.location_service_code = 'ecs' client.location_endpoint_type = "openAPI" api_request = APIRequest('DescribeRegions', 'GET', 'https', 'RPC') os.environ["ALIBABA_CLOUD_ACCESS_KEY_ID"] = self.access_key_id os.environ["ALIBABA_CLOUD_ACCESS_KEY_SECRET"] = self.access_key_secret response = client._handle_request(api_request) response_credential = response.http_request.credentials self.assertTrue(response_credential.access_key_id.startswith("STS.")) response = response.http_response.content ret = self.get_dict_response(response) self.assertTrue(ret.get("Regions")) self.assertTrue(ret.get("RequestId"))
def test_rpc_assume_role_request_with_sts_token(self): self._create_default_ram_user() # self._attach_default_policy() self._create_access_key() self._create_default_ram_role() acs_client = ClientConfig(region_id=self.region_id) ram_role_arn_credential = RamRoleCredentialsProvider( acs_client, AccessKeyCredentials(self.ram_user_access_key_id, self.ram_user_access_key_secret), self.ram_role_arn, "alice_test") client = AlibabaCloudClient(acs_client, ram_role_arn_credential) client.product_code = "Ecs" client.api_version = "2014-05-26" client.location_service_code = 'ecs' client.location_endpoint_type = "openAPI" api_request = APIRequest('DescribeRegions', 'GET', 'https', 'RPC') response = client._handle_request(api_request) response_credentials = response.http_request.credentials self.assertTrue(response_credentials.access_key_id.startswith("STS.")) response = response.http_response.content ret = self.get_dict_response(response) self.assertTrue(ret.get("Regions")) self.assertTrue(ret.get("RequestId"))
def test_roa_assume_role_request_with_sts_token(self): self._create_default_ram_user() # self._attach_default_policy() self._create_access_key() self._create_default_ram_role() roa_client = ClientConfig(region_id=self.region_id) ram_role_arn_credential = RamRoleCredentialsProvider( roa_client, AccessKeyCredentials(self.ram_user_access_key_id, self.ram_user_access_key_secret), self.ram_role_arn, "alice_test") client = AlibabaCloudClient(roa_client, ram_role_arn_credential) client.product_code = "ROS" client.api_version = "2015-09-01" client.location_service_code = 'ros' client.location_endpoint_type = "openAPI" api_request = APIRequest('DescribeResourceTypes', 'GET', 'https', 'ROA') api_request.uri_pattern = '/resource_types' api_request.path_params = None response = client._handle_request(api_request) response_credentials = response.http_request.credentials self.assertTrue(response_credentials.access_key_id.startswith("STS.")) response = response.http_response.content ret = self.get_dict_response(response) self.assertTrue(ret.get("ResourceTypes"))
def test_call_rpc_request_with_secret_error(self): def init_credentials_provider(): from alibabacloud.credentials import AccessKeyCredentials credentials = AccessKeyCredentials( access_key_id=self.access_key_id, access_key_secret="BadAccessKeySecret") from alibabacloud.credentials.provider import StaticCredentialsProvider credentials_provider = StaticCredentialsProvider(credentials) return credentials_provider client = AlibabaCloudClient( self.client_config, credentials_provider=init_credentials_provider()) client.product_code = "Ecs" client.api_version = "2014-05-26" client.location_service_code = 'ecs' client.location_endpoint_type = "openAPI" api_request = APIRequest('DescribeRegions', 'GET', 'https', 'RPC') with self.assertRaises(ServerException) as e: client._handle_request(api_request) self.assertEqual(e.exception.service_name, "Ecs") self.assertEqual(e.exception.http_status, 400) self.assertEqual(e.exception.endpoint, "ecs-cn-hangzhou.aliyuncs.com") self.assertEqual(e.exception.error_code, "InvalidAccessKeySecret") self.assertEqual( e.exception.error_message, "The AccessKeySecret is incorrect. " "Please check your AccessKeyId and AccessKeySecret.")
def test_products_with_location_service(self): client = AlibabaCloudClient(self.client_config, self.init_credentials_provider()) client.product_code = "Ecs" client.api_version = "2014-05-26" client.location_service_code = 'ecs' client.location_endpoint_type = "openAPI" api_request = APIRequest('DescribeRegions', 'GET', 'https', 'RPC') response = client._handle_request(api_request)
def test_location_service_code_not_equals_product_code(self): client = AlibabaCloudClient(self.client_config, self.init_credentials_provider()) client.product_code = "CloudAPI" client.api_version = "2016-07-14" client.location_service_code = 'apigateway' client.location_endpoint_type = "openAPI" api_request = APIRequest('DescribeApis', 'GET', 'https', 'RPC') context = client._handle_request(api_request) response = json.loads(str(context.http_response.text)) self.assertTrue(response.get('RequestId'))
def test_call_rpc_request_with_config_default(self): client = AlibabaCloudClient( self.client_config, credentials_provider=self.init_credentials_provider()) client.product_code = "Ecs" client.api_version = "2014-05-26" client.location_service_code = 'ecs' client.location_endpoint_type = "openAPI" api_request = APIRequest('DescribeRegions', 'GET', 'https', 'RPC') response = client._handle_request(api_request) response = response.http_response.content ret = self.get_dict_response(response) self.assertTrue(ret.get("Regions")) self.assertTrue(ret.get("RequestId"))
def test_server_unreachable(self): client_config = self.client_config client_config.region_id = 'cn-abc' client = AlibabaCloudClient(client_config, self.init_credentials_provider()) client.product_code = "Ecs" client.api_version = "2014-05-26" client.location_service_code = 'ecs' client.location_endpoint_type = "openAPI" api_request = APIRequest('DescribeRegions', 'GET', 'https', 'RPC') try: client._handle_request(api_request) assert False except InvalidRegionIDException as e: self.assertTrue(e.error_message.startswith( "No such region 'cn-abc'. Please check your region ID."))
def test_call_roa_request_with_config_default(self): client = AlibabaCloudClient( self.client_config, credentials_provider=self.init_credentials_provider()) client.product_code = "ROS" client.api_version = "2015-09-01" client.location_service_code = 'ros' client.location_endpoint_type = "openAPI" api_request = APIRequest('DescribeResourceTypes', 'GET', 'https', 'ROA') api_request.uri_pattern = '/resource_types' api_request.path_params = None response = client._handle_request(api_request) response = response.http_response.content ret = self.get_dict_response(response) self.assertTrue(ret.get("ResourceTypes"))
def test_request_with_listkeys(self): client = AlibabaCloudClient( self.client_config, credentials_provider=self.init_credentials_provider()) client.product_code = "Kms" client.api_version = "2016-01-20" client.location_service_code = 'kms' client.location_endpoint_type = "openAPI" api_request = APIRequest('ListKeys', 'GET', 'https', 'RPC') try: context = client._handle_request(api_request) except ServerException as e: self.assertEqual(e.http_status, 403) else: response = context.result self.assertTrue(response.get("PageNumber"))
def test_call_rpc_request_with_env_ak(self): os.environ["ALIBABA_CLOUD_ACCESS_KEY_ID"] = self.access_key_id os.environ["ALIBABA_CLOUD_ACCESS_KEY_SECRET"] = self.access_key_secret client_config = ClientConfig(region_id=self.region_id) client = AlibabaCloudClient( client_config, credentials_provider=self.init_credentials_provider()) client.product_code = "Ecs" client.api_version = "2014-05-26" client.location_service_code = 'ecs' client.location_endpoint_type = "openAPI" api_request = APIRequest('DescribeRegions', 'GET', 'https', 'RPC') response = client._handle_request(api_request) response = response.http_response.content ret = self.get_dict_response(response) self.assertTrue(ret.get("Regions")) self.assertTrue(ret.get("RequestId"))
def test_call_roa_request_with_client(self): client = AlibabaCloudClient(self.client_config, self.init_credentials_provider()) client.product_code = "ROS" client.api_version = "2015-09-01" client.location_service_code = 'ros' client.location_endpoint_type = "openAPI" api_request = APIRequest('DescribeResourceTypes', 'GET', 'https', 'ROA') api_request.uri_pattern = '/stacks/[StackName]/[StackId]/resources' api_request.path_params = None api_request._params = {"StackId": "StackId", "StackName": "StackName"} try: response = client._handle_request(api_request) assert False except ServerException as e: self.assertEqual("InvalidUrl", e.error_code) self.assertTrue( e.error_message.startswith("Request url is invalid"))
def test_call_roa_request_with_env_ak(self): os.environ["ALIBABA_CLOUD_ACCESS_KEY_ID"] = self.access_key_id os.environ["ALIBABA_CLOUD_ACCESS_KEY_SECRET"] = self.access_key_secret client_config = ClientConfig(region_id=self.region_id) client = AlibabaCloudClient( client_config, credentials_provider=self.init_credentials_provider()) client.product_code = "ROS" client.api_version = "2015-09-01" client.location_service_code = 'ros' client.location_endpoint_type = "openAPI" api_request = APIRequest('DescribeResourceTypes', 'GET', 'https', 'ROA') api_request.uri_pattern = '/resource_types' api_request.path_params = None response = client._handle_request(api_request) response = response.http_response.content ret = self.get_dict_response(response) self.assertTrue(ret.get("ResourceTypes")) os.environ.pop("ALIBABA_CLOUD_ACCESS_KEY_ID") os.environ.pop("ALIBABA_CLOUD_ACCESS_KEY_SECRET")
def test_call_request_with_config_role_name_priority(self): os.environ["ALIBABA_CLOUD_ROLE_NAME"] = self.default_ram_role_name client_config = ClientConfig(region_id=self.region_id) client = AlibabaCloudClient( client_config, credentials_provider=self.init_credentials_provider()) client.product_code = "Ecs" client.api_version = "2014-05-26" client.location_service_code = 'ecs' client.location_endpoint_type = "openAPI" api_request = APIRequest('DescribeRegions', 'GET', 'https', 'RPC') response = client._handle_request(api_request) response_key_id = response.http_request.credentials.access_key_id self.assertFalse(response_key_id.startswith("TST.")) response = response.http_response.content ret = self.get_dict_response(response) self.assertTrue(ret.get("Regions")) self.assertTrue(ret.get("RequestId")) os.environ.pop("ALIBABA_CLOUD_ROLE_NAME")
def test_location_service_code_not_equals_product_code2(self): # The product changed from api_gateway to edas temp_client = self.temp_client('Edas', '2017-08-01', 'openAPI', 'BindSlb') self.init_env(temp_client.config, temp_client.credentials_provider, "{}") client = AlibabaCloudClient(self.client_config, self.init_credentials_provider()) client.product_code = "CloudAPI" client.api_version = "2016-07-14" client.location_service_code = 'apigateway' client.location_endpoint_type = "openAPI" client._endpoint_resolver = self._endpoint_resolver with patch.object(self._location_service_endpoint_resolver, '_call_location_service', wraps=self._location_service_endpoint_resolver. _call_location_service) as monkey: for i in range(3): api_request = APIRequest('DescribeApis', 'GET', 'https', 'RPC') response = client._handle_request(api_request) self.assertEqual(0, monkey.call_count) client._endpoint_resolver = self._endpoint_resolver