public void GetCredentials() { DefaultProfile.ClearDefaultProfile(); var mock = new Mock <AlibabaCloudCredentials>(); mock.Setup(foo => foo.GetAccessKeyId()).Returns("accessKeyId"); mock.Setup(foo => foo.GetAccessKeySecret()).Returns("accessKeySecret"); AlibabaCloudCredentials longLivedCredentials = mock.Object; IClientProfile profile = DefaultProfile.GetProfile( "cn-hangzhou", "accessKeyId", "accessKeySecret" ); var mockInstance = new Mock <STSAssumeRoleSessionCredentialsProvider>(longLivedCredentials, "roleArn", profile); AssumeRoleResponse response = new AssumeRoleResponse(); response.Credentials = new AssumeRoleResponse.AssumeRole_Credentials(); response.Credentials.AccessKeyId = "MockAccessKeyId"; response.Credentials.AccessKeySecret = "MockAccessKeyId"; response.Credentials.SecurityToken = "MockSecurityToken"; mockInstance.Setup(foo => foo.GetResponse( It.IsAny <AssumeRoleRequest>() )).Returns(response); STSAssumeRoleSessionCredentialsProvider instance = mockInstance.Object; var credentials = instance.GetCredentials(); // 执行credential初始化 Assert.IsType <BasicSessionCredentials>(credentials); var credentials2 = instance.GetCredentials(); // 不执行credential初始化,直接获取 Assert.IsType <BasicSessionCredentials>(credentials); Assert.Equal(credentials.GetAccessKeyId(), credentials2.GetAccessKeyId()); Assert.Equal(credentials.GetAccessKeySecret(), credentials2.GetAccessKeySecret()); }
public virtual AlibabaCloudCredentials GetRamRoleArnAlibabaCloudCredential() { if (String.IsNullOrEmpty(this.accessKeyId) || String.IsNullOrEmpty(this.accessKeySecret) || String.IsNullOrEmpty(this.regionId)) { throw new ClientException("Missing required variable option for 'default Client'"); } RamRoleArnCredential credential = new RamRoleArnCredential( this.accessKeyId, this.accessKeySecret, this.roleArn, this.roleSessionName, STSAssumeRoleSessionCredentialsProvider.GetNewRoleSessionName(), STSAssumeRoleSessionCredentialsProvider.DEFAULT_DURATION_SECONDS); this.defaultProfile = DefaultProfile.GetProfile(this.regionId, this.accessKeyId, this.accessKeySecret); STSAssumeRoleSessionCredentialsProvider sTSAssumeRoleSessionCredentialsProvider = new STSAssumeRoleSessionCredentialsProvider(credential, this.roleArn, this.defaultProfile); RamRoleArnCredential ramRoleArnCredential = (RamRoleArnCredential)sTSAssumeRoleSessionCredentialsProvider.GetCredentials(); return(ramRoleArnCredential); }