microsoft/teams.net
Publicmirrored fromhttps://github.com/microsoft/teams.netAvailable
core/test/Microsoft.Teams.Bot.Core.UnitTests/UserTokenClientTests.cs
110lines · modecode
| 1 | // Copyright (c) Microsoft Corporation. |
| 2 | // Licensed under the MIT License. |
| 3 | |
| 4 | using Microsoft.Extensions.Configuration; |
| 5 | using Microsoft.Teams.Bot.Core; |
| 6 | |
| 7 | namespace Microsoft.Teams.Bot.Core.UnitTests; |
| 8 | |
| 9 | public class UserTokenClientTests |
| 10 | { |
| 11 | private static IConfiguration Config(Dictionary<string, string?> data) => |
| 12 | new ConfigurationBuilder().AddInMemoryCollection(data).Build(); |
| 13 | |
| 14 | [Fact] |
| 15 | public void ResolveApiEndpoint_NoConfiguration_DefaultsToPublicTokenService() |
| 16 | { |
| 17 | Assert.Equal("https://token.botframework.com", UserTokenClient.ResolveApiEndpoint(Config([]))); |
| 18 | } |
| 19 | |
| 20 | [Fact] |
| 21 | public void ResolveApiEndpoint_ExplicitUserTokenApiEndpoint_WinsOverEverything() |
| 22 | { |
| 23 | IConfiguration config = Config(new() |
| 24 | { |
| 25 | ["UserTokenApiEndpoint"] = "https://my.explicit.endpoint", |
| 26 | ["AzureAd:Cloud"] = "USGov", |
| 27 | ["AzureAd:TokenServiceUrl"] = "https://should-be-ignored" |
| 28 | }); |
| 29 | |
| 30 | Assert.Equal("https://my.explicit.endpoint", UserTokenClient.ResolveApiEndpoint(config)); |
| 31 | } |
| 32 | |
| 33 | [Theory] |
| 34 | [InlineData("USGov", "https://tokengcch.botframework.azure.us")] |
| 35 | [InlineData("USGovDoD", "https://apiDoD.botframework.azure.us")] |
| 36 | [InlineData("China", "https://token.botframework.azure.cn")] |
| 37 | [InlineData("Public", "https://token.botframework.com")] |
| 38 | public void ResolveApiEndpoint_CloudPresetAzureAdSection_ResolvesSovereignEndpoint(string cloudName, string expected) |
| 39 | { |
| 40 | IConfiguration config = Config(new() { ["AzureAd:Cloud"] = cloudName }); |
| 41 | Assert.Equal(expected, UserTokenClient.ResolveApiEndpoint(config)); |
| 42 | } |
| 43 | |
| 44 | [Fact] |
| 45 | public void ResolveApiEndpoint_RootCloudKey_Works() |
| 46 | { |
| 47 | Assert.Equal("https://tokengcch.botframework.azure.us", |
| 48 | UserTokenClient.ResolveApiEndpoint(Config(new() { ["Cloud"] = "USGov" }))); |
| 49 | } |
| 50 | |
| 51 | [Fact] |
| 52 | public void ResolveApiEndpoint_UppercaseCloudKey_Works() |
| 53 | { |
| 54 | Assert.Equal("https://token.botframework.azure.cn", |
| 55 | UserTokenClient.ResolveApiEndpoint(Config(new() { ["CLOUD"] = "China" }))); |
| 56 | } |
| 57 | |
| 58 | [Fact] |
| 59 | public void ResolveApiEndpoint_SectionTokenServiceUrlOverride_AppliesOnTopOfCloud() |
| 60 | { |
| 61 | IConfiguration config = Config(new() |
| 62 | { |
| 63 | ["AzureAd:Cloud"] = "USGov", |
| 64 | ["AzureAd:TokenServiceUrl"] = "https://custom.token.service" |
| 65 | }); |
| 66 | |
| 67 | Assert.Equal("https://custom.token.service", UserTokenClient.ResolveApiEndpoint(config)); |
| 68 | } |
| 69 | |
| 70 | [Fact] |
| 71 | public void ResolveApiEndpoint_RootTokenServiceUrlOverride_AppliesWhenNoCloudSet() |
| 72 | { |
| 73 | IConfiguration config = Config(new() { ["TokenServiceUrl"] = "https://root-override" }); |
| 74 | |
| 75 | Assert.Equal("https://root-override", UserTokenClient.ResolveApiEndpoint(config)); |
| 76 | } |
| 77 | |
| 78 | [Fact] |
| 79 | public void ResolveApiEndpoint_SectionOverrideBeatsRootOverride() |
| 80 | { |
| 81 | IConfiguration config = Config(new() |
| 82 | { |
| 83 | ["AzureAd:TokenServiceUrl"] = "https://section", |
| 84 | ["TokenServiceUrl"] = "https://root" |
| 85 | }); |
| 86 | |
| 87 | Assert.Equal("https://section", UserTokenClient.ResolveApiEndpoint(config)); |
| 88 | } |
| 89 | |
| 90 | [Fact] |
| 91 | public void ResolveApiEndpoint_WhitespaceOverrideIgnored_FallsBackToCloud() |
| 92 | { |
| 93 | IConfiguration config = Config(new() |
| 94 | { |
| 95 | ["AzureAd:Cloud"] = "USGov", |
| 96 | ["AzureAd:TokenServiceUrl"] = " " |
| 97 | }); |
| 98 | |
| 99 | Assert.Equal("https://tokengcch.botframework.azure.us", UserTokenClient.ResolveApiEndpoint(config)); |
| 100 | } |
| 101 | |
| 102 | [Theory] |
| 103 | [InlineData("")] |
| 104 | [InlineData(" ")] |
| 105 | public void ResolveApiEndpoint_EmptyOrWhitespaceCloud_DefaultsToPublic(string cloudValue) |
| 106 | { |
| 107 | IConfiguration config = Config(new() { ["AzureAd:Cloud"] = cloudValue }); |
| 108 | Assert.Equal("https://token.botframework.com", UserTokenClient.ResolveApiEndpoint(config)); |
| 109 | } |
| 110 | } |
| 111 | |