|
| 1 | +// Copyright (c) Microsoft Corporation. All rights reserved. |
| 2 | +// Licensed under the MIT License. |
| 3 | + |
| 4 | +using System; |
| 5 | +using System.Net.Http; |
| 6 | +using System.Security.Cryptography.X509Certificates; |
| 7 | +using System.Threading; |
| 8 | +using Microsoft.Bot.Connector.Authentication; |
| 9 | +using Microsoft.Extensions.Logging; |
| 10 | +using Moq; |
| 11 | +using Xunit; |
| 12 | + |
| 13 | +namespace Microsoft.Bot.Connector.Tests.Authentication |
| 14 | +{ |
| 15 | + public class CertificateServiceClientCredentialsFactoryTests |
| 16 | + { |
| 17 | + private const string TestAppId = nameof(TestAppId); |
| 18 | + private const string TestTenantId = nameof(TestTenantId); |
| 19 | + private const string TestAudience = nameof(TestAudience); |
| 20 | + private readonly Mock<ILogger> logger = new Mock<ILogger>(); |
| 21 | + private readonly Mock<X509Certificate2> certificate = new Mock<X509Certificate2>(); |
| 22 | + |
| 23 | + [Fact] |
| 24 | + public void ConstructorTests() |
| 25 | + { |
| 26 | + _ = new CertificateServiceClientCredentialsFactory(certificate.Object, TestAppId); |
| 27 | + _ = new CertificateServiceClientCredentialsFactory(certificate.Object, TestAppId, tenantId: TestTenantId); |
| 28 | + _ = new CertificateServiceClientCredentialsFactory(certificate.Object, TestAppId, logger: logger.Object); |
| 29 | + _ = new CertificateServiceClientCredentialsFactory(certificate.Object, TestAppId, httpClient: new HttpClient()); |
| 30 | + } |
| 31 | + |
| 32 | + [Fact] |
| 33 | + public void CannotCreateCredentialsFactoryWithoutCertificate() |
| 34 | + { |
| 35 | + Assert.Throws<ArgumentNullException>(() => new CertificateServiceClientCredentialsFactory(null, TestAppId)); |
| 36 | + } |
| 37 | + |
| 38 | + [Theory] |
| 39 | + [InlineData(null)] |
| 40 | + [InlineData("")] |
| 41 | + [InlineData(" ")] |
| 42 | + public void CannotCreateCredentialsFactoryWithoutAppId(string appId) |
| 43 | + { |
| 44 | + Assert.Throws<ArgumentNullException>(() => new CertificateServiceClientCredentialsFactory(certificate.Object, appId)); |
| 45 | + } |
| 46 | + |
| 47 | + [Fact] |
| 48 | + public void IsValidAppIdTest() |
| 49 | + { |
| 50 | + var factory = new CertificateServiceClientCredentialsFactory(certificate.Object, TestAppId); |
| 51 | + |
| 52 | + Assert.True(factory.IsValidAppIdAsync(TestAppId, CancellationToken.None).GetAwaiter().GetResult()); |
| 53 | + Assert.False(factory.IsValidAppIdAsync("InvalidAppId", CancellationToken.None).GetAwaiter().GetResult()); |
| 54 | + } |
| 55 | + |
| 56 | + [Fact] |
| 57 | + public void IsAuthenticationDisabledTest() |
| 58 | + { |
| 59 | + var factory = new CertificateServiceClientCredentialsFactory(certificate.Object, TestAppId); |
| 60 | + |
| 61 | + Assert.False(factory.IsAuthenticationDisabledAsync(CancellationToken.None).GetAwaiter().GetResult()); |
| 62 | + } |
| 63 | + |
| 64 | + [Fact] |
| 65 | + public async void CanCreateCredentials() |
| 66 | + { |
| 67 | + var factory = new CertificateServiceClientCredentialsFactory(certificate.Object, TestAppId); |
| 68 | + |
| 69 | + var credentials = await factory.CreateCredentialsAsync( |
| 70 | + TestAppId, TestAudience, "https://login.microsoftonline.com", true, CancellationToken.None); |
| 71 | + |
| 72 | + Assert.NotNull(credentials); |
| 73 | + Assert.IsType<CertificateAppCredentials>(credentials); |
| 74 | + } |
| 75 | + |
| 76 | + [Fact] |
| 77 | + public void CannotCreateCredentialsWithInvalidAppId() |
| 78 | + { |
| 79 | + var factory = new CertificateServiceClientCredentialsFactory(certificate.Object, TestAppId); |
| 80 | + |
| 81 | + Assert.ThrowsAsync<InvalidOperationException>(() => factory.CreateCredentialsAsync( |
| 82 | + "InvalidAppId", TestAudience, "https://login.microsoftonline.com", true, CancellationToken.None)); |
| 83 | + } |
| 84 | + } |
| 85 | +} |
0 commit comments