microsoft/teams.net
Publicmirrored from https://github.com/microsoft/teams.netAvailable
core/test/Microsoft.Bot.Core.UnitTests/Hosting/AddBotApplicationExtensionsTests.cs
208lines · modecode
| 1 | // Copyright (c) Microsoft Corporation. |
| 2 | // Licensed under the MIT License. |
| 3 | |
| 4 | using Microsoft.Bot.Core.Hosting; |
| 5 | using Microsoft.Bot.Core.Schema; |
| 6 | using Microsoft.Extensions.Configuration; |
| 7 | using Microsoft.Extensions.DependencyInjection; |
| 8 | using Microsoft.Extensions.Logging; |
| 9 | using Microsoft.Extensions.Options; |
| 10 | using Microsoft.Identity.Abstractions; |
| 11 | using Microsoft.Identity.Web; |
| 12 | using Moq; |
| 13 | |
| 14 | namespace Microsoft.Bot.Core.UnitTests.Hosting; |
| 15 | |
| 16 | public class AddBotApplicationExtensionsTests |
| 17 | { |
| 18 | private static ServiceProvider BuildServiceProvider(Dictionary<string, string?> configData, string? aadConfigSectionName = null) |
| 19 | { |
| 20 | var configuration = new ConfigurationBuilder() |
| 21 | .AddInMemoryCollection(configData) |
| 22 | .Build(); |
| 23 | |
| 24 | var services = new ServiceCollection(); |
| 25 | services.AddSingleton<IConfiguration>(configuration); |
| 26 | services.AddLogging(); |
| 27 | |
| 28 | if (aadConfigSectionName is null) |
| 29 | { |
| 30 | services.AddConversationClient(); |
| 31 | } |
| 32 | else |
| 33 | { |
| 34 | services.AddConversationClient(aadConfigSectionName); |
| 35 | } |
| 36 | |
| 37 | return services.BuildServiceProvider(); |
| 38 | } |
| 39 | |
| 40 | private static void AssertMsalOptions(ServiceProvider serviceProvider, string expectedClientId, string expectedTenantId, string expectedInstance = "https://login.microsoftonline.com/") |
| 41 | { |
| 42 | var msalOptions = serviceProvider.GetRequiredService<IOptions<MicrosoftIdentityApplicationOptions>>().Value; |
| 43 | Assert.Equal(expectedClientId, msalOptions.ClientId); |
| 44 | Assert.Equal(expectedTenantId, msalOptions.TenantId); |
| 45 | Assert.Equal(expectedInstance, msalOptions.Instance); |
| 46 | } |
| 47 | |
| 48 | [Fact] |
| 49 | public void AddConversationClient_WithBotFrameworkConfig_ConfiguresClientSecret() |
| 50 | { |
| 51 | // Arrange |
| 52 | var configData = new Dictionary<string, string?> |
| 53 | { |
| 54 | ["MicrosoftAppId"] = "test-app-id", |
| 55 | ["MicrosoftAppTenantId"] = "test-tenant-id", |
| 56 | ["MicrosoftAppPassword"] = "test-secret" |
| 57 | }; |
| 58 | |
| 59 | // Act |
| 60 | var serviceProvider = BuildServiceProvider(configData); |
| 61 | |
| 62 | // Assert |
| 63 | AssertMsalOptions(serviceProvider, "test-app-id", "test-tenant-id"); |
| 64 | var msalOptions = serviceProvider.GetRequiredService<IOptions<MicrosoftIdentityApplicationOptions>>().Value; |
| 65 | Assert.NotNull(msalOptions.ClientCredentials); |
| 66 | Assert.Single(msalOptions.ClientCredentials); |
| 67 | var credential = msalOptions.ClientCredentials.First(); |
| 68 | Assert.Equal(CredentialSource.ClientSecret, credential.SourceType); |
| 69 | Assert.Equal("test-secret", credential.ClientSecret); |
| 70 | } |
| 71 | |
| 72 | [Fact] |
| 73 | public void AddConversationClient_WithCoreConfigAndClientSecret_ConfiguresClientSecret() |
| 74 | { |
| 75 | // Arrange |
| 76 | var configData = new Dictionary<string, string?> |
| 77 | { |
| 78 | ["CLIENT_ID"] = "test-client-id", |
| 79 | ["TENANT_ID"] = "test-tenant-id", |
| 80 | ["CLIENT_SECRET"] = "test-client-secret" |
| 81 | }; |
| 82 | |
| 83 | // Act |
| 84 | var serviceProvider = BuildServiceProvider(configData); |
| 85 | |
| 86 | // Assert |
| 87 | AssertMsalOptions(serviceProvider, "test-client-id", "test-tenant-id"); |
| 88 | var msalOptions = serviceProvider.GetRequiredService<IOptions<MicrosoftIdentityApplicationOptions>>().Value; |
| 89 | Assert.NotNull(msalOptions.ClientCredentials); |
| 90 | Assert.Single(msalOptions.ClientCredentials); |
| 91 | var credential = msalOptions.ClientCredentials.First(); |
| 92 | Assert.Equal(CredentialSource.ClientSecret, credential.SourceType); |
| 93 | Assert.Equal("test-client-secret", credential.ClientSecret); |
| 94 | } |
| 95 | |
| 96 | [Fact] |
| 97 | public void AddConversationClient_WithCoreConfigAndSystemAssignedMI_ConfiguresSystemAssignedFIC() |
| 98 | { |
| 99 | // Arrange |
| 100 | var configData = new Dictionary<string, string?> |
| 101 | { |
| 102 | ["CLIENT_ID"] = "test-client-id", |
| 103 | ["TENANT_ID"] = "test-tenant-id", |
| 104 | ["MANAGED_IDENTITY_CLIENT_ID"] = "system" |
| 105 | }; |
| 106 | |
| 107 | // Act |
| 108 | var serviceProvider = BuildServiceProvider(configData); |
| 109 | |
| 110 | // Assert |
| 111 | AssertMsalOptions(serviceProvider, "test-client-id", "test-tenant-id"); |
| 112 | var msalOptions = serviceProvider.GetRequiredService<IOptions<MicrosoftIdentityApplicationOptions>>().Value; |
| 113 | Assert.NotNull(msalOptions.ClientCredentials); |
| 114 | Assert.Single(msalOptions.ClientCredentials); |
| 115 | var credential = msalOptions.ClientCredentials.First(); |
| 116 | Assert.Equal(CredentialSource.SignedAssertionFromManagedIdentity, credential.SourceType); |
| 117 | Assert.Null(credential.ManagedIdentityClientId); // System-assigned |
| 118 | |
| 119 | var managedIdentityOptions = serviceProvider.GetRequiredService<IOptions<ManagedIdentityOptions>>().Value; |
| 120 | Assert.Null(managedIdentityOptions.UserAssignedClientId); |
| 121 | } |
| 122 | |
| 123 | [Fact] |
| 124 | public void AddConversationClient_WithCoreConfigAndUserAssignedMI_ConfiguresUserAssignedFIC() |
| 125 | { |
| 126 | // Arrange |
| 127 | var configData = new Dictionary<string, string?> |
| 128 | { |
| 129 | ["CLIENT_ID"] = "test-client-id", |
| 130 | ["TENANT_ID"] = "test-tenant-id", |
| 131 | ["MANAGED_IDENTITY_CLIENT_ID"] = "umi-client-id" // Different from CLIENT_ID means FIC |
| 132 | }; |
| 133 | |
| 134 | // Act |
| 135 | var serviceProvider = BuildServiceProvider(configData); |
| 136 | |
| 137 | // Assert |
| 138 | AssertMsalOptions(serviceProvider, "test-client-id", "test-tenant-id"); |
| 139 | var msalOptions = serviceProvider.GetRequiredService<IOptions<MicrosoftIdentityApplicationOptions>>().Value; |
| 140 | Assert.NotNull(msalOptions.ClientCredentials); |
| 141 | Assert.Single(msalOptions.ClientCredentials); |
| 142 | var credential = msalOptions.ClientCredentials.First(); |
| 143 | Assert.Equal(CredentialSource.SignedAssertionFromManagedIdentity, credential.SourceType); |
| 144 | Assert.Equal("umi-client-id", credential.ManagedIdentityClientId); |
| 145 | |
| 146 | var managedIdentityOptions = serviceProvider.GetRequiredService<IOptions<ManagedIdentityOptions>>().Value; |
| 147 | Assert.Null(managedIdentityOptions.UserAssignedClientId); |
| 148 | } |
| 149 | |
| 150 | [Fact] |
| 151 | public void AddConversationClient_WithCoreConfigAndNoManagedIdentity_ConfiguresUMIWithClientId() |
| 152 | { |
| 153 | // Arrange |
| 154 | var configData = new Dictionary<string, string?> |
| 155 | { |
| 156 | ["CLIENT_ID"] = "test-client-id", |
| 157 | ["TENANT_ID"] = "test-tenant-id" |
| 158 | }; |
| 159 | |
| 160 | // Act |
| 161 | var serviceProvider = BuildServiceProvider(configData); |
| 162 | |
| 163 | // Assert |
| 164 | AssertMsalOptions(serviceProvider, "test-client-id", "test-tenant-id"); |
| 165 | var msalOptions = serviceProvider.GetRequiredService<IOptions<MicrosoftIdentityApplicationOptions>>().Value; |
| 166 | Assert.Null(msalOptions.ClientCredentials); |
| 167 | |
| 168 | var managedIdentityOptions = serviceProvider.GetRequiredService<IOptions<ManagedIdentityOptions>>().Value; |
| 169 | Assert.Equal("test-client-id", managedIdentityOptions.UserAssignedClientId); |
| 170 | } |
| 171 | |
| 172 | [Fact] |
| 173 | public void AddConversationClient_WithDefaultSection_ConfiguresFromSection() |
| 174 | { |
| 175 | // AzureAd is the default Section Name |
| 176 | // Arrange |
| 177 | var configData = new Dictionary<string, string?> |
| 178 | { |
| 179 | ["AzureAd:ClientId"] = "azuread-client-id", |
| 180 | ["AzureAd:TenantId"] = "azuread-tenant-id", |
| 181 | ["AzureAd:Instance"] = "https://login.microsoftonline.com/" |
| 182 | }; |
| 183 | |
| 184 | // Act |
| 185 | var serviceProvider = BuildServiceProvider(configData); |
| 186 | |
| 187 | // Assert |
| 188 | AssertMsalOptions(serviceProvider, "azuread-client-id", "azuread-tenant-id"); |
| 189 | } |
| 190 | |
| 191 | [Fact] |
| 192 | public void AddConversationClient_WithCustomSectionName_ConfiguresFromCustomSection() |
| 193 | { |
| 194 | // Arrange |
| 195 | var configData = new Dictionary<string, string?> |
| 196 | { |
| 197 | ["CustomAuth:ClientId"] = "custom-client-id", |
| 198 | ["CustomAuth:TenantId"] = "custom-tenant-id", |
| 199 | ["CustomAuth:Instance"] = "https://login.microsoftonline.com/" |
| 200 | }; |
| 201 | |
| 202 | // Act |
| 203 | var serviceProvider = BuildServiceProvider(configData, "CustomAuth"); |
| 204 | |
| 205 | // Assert |
| 206 | AssertMsalOptions(serviceProvider, "custom-client-id", "custom-tenant-id"); |
| 207 | } |
| 208 | } |
| 209 | |