public async Task ServicePrincipalCertificateFileWithSecretAuthenticationTest() { var accountId = "testuser"; var certificateFile = "d:/certficatefortest.pfx"; var thumbprint = Guid.NewGuid().ToString(); var securePassword = new SecureString(); "pa88w0rd!".ToCharArray().ForEach(c => securePassword.AppendChar(c)); IDataStore prevDataStore = AzureSession.Instance.DataStore; AzureSession.Instance.DataStore = new DiskDataStore(); //Setup var mockAzureCredentialFactory = new Mock <AzureCredentialFactory>(); mockAzureCredentialFactory.Setup(f => f.CreateClientCertificateCredential( It.IsAny <string>(), It.IsAny <string>(), It.IsAny <X509Certificate2>(), It.IsAny <ClientCertificateCredentialOptions>())).Returns(() => new TokenCredentialMock()); AzureSession.Instance.RegisterComponent(nameof(AzureCredentialFactory), () => mockAzureCredentialFactory.Object, true); InMemoryTokenCacheProvider cacheProvider = new InMemoryTokenCacheProvider(); var account = new AzureAccount { Id = accountId, Type = AzureAccount.AccountType.User, }; account.SetTenants(TestTenantId); var parameter = new ServicePrincipalParameters( cacheProvider, AzureEnvironment.PublicEnvironments["AzureCloud"], null, TestTenantId, TestResourceId, account.Id, null, certificateFile, securePassword, null, null); //Run var authenticator = new ServicePrincipalAuthenticator(); var token = await authenticator.Authenticate(parameter); //Verify mockAzureCredentialFactory.Verify(f => f.CreateClientCertificateCredential(TestTenantId, accountId, It.IsAny <X509Certificate2>(), It.IsAny <ClientCertificateCredentialOptions>()), Times.Once()); Assert.Equal(fakeToken, token.AccessToken); Assert.Equal(TestTenantId, token.TenantId); AzureSession.Instance.DataStore = prevDataStore; }
public async Task ServicePrincipalCertificateFileAuthenticationTest() { var accountId = "testuser"; var certificateFile = "d:/certficatefortest.pfx"; IDataStore prevDataStore = AzureSession.Instance.DataStore; AzureSession.Instance.DataStore = new MockDataStore(); AzureSession.Instance.DataStore.WriteFile(certificateFile, "dummyfile"); //Setup var mockAzureCredentialFactory = new Mock <AzureCredentialFactory>(); mockAzureCredentialFactory.Setup(f => f.CreateClientCertificateCredential( It.IsAny <string>(), It.IsAny <string>(), It.IsAny <string>(), It.IsAny <ClientCertificateCredentialOptions>())).Returns(() => new TokenCredentialMock()); AzureSession.Instance.RegisterComponent(nameof(AzureCredentialFactory), () => mockAzureCredentialFactory.Object, true); InMemoryTokenCacheProvider cacheProvider = new InMemoryTokenCacheProvider(); var account = new AzureAccount { Id = accountId, Type = AzureAccount.AccountType.User, }; account.SetTenants(TestTenantId); var parameter = new ServicePrincipalParameters( cacheProvider, AzureEnvironment.PublicEnvironments["AzureCloud"], null, TestTenantId, TestResourceId, account.Id, null, certificateFile, null, null, null); //Run var authenticator = new ServicePrincipalAuthenticator(); var token = await authenticator.Authenticate(parameter); //Verify mockAzureCredentialFactory.Verify(f => f.CreateClientCertificateCredential(TestTenantId, accountId, certificateFile, It.IsAny <ClientCertificateCredentialOptions>()), Times.Once()); Assert.Equal(fakeToken, token.AccessToken); Assert.Equal(TestTenantId, token.TenantId); AzureSession.Instance.DataStore = prevDataStore; }