microsoft/teams.net
Publicmirrored from https://github.com/microsoft/teams.netAvailable
Tests/Microsoft.Teams.Api.Tests/Auth/CloudEnvironmentTests.cs
195lines · modecode
| 1 | // Copyright (c) Microsoft Corporation. All rights reserved. |
| 2 | // Licensed under the MIT License. |
| 3 | |
| 4 | using Microsoft.Teams.Api.Auth; |
| 5 | |
| 6 | namespace Microsoft.Teams.Api.Tests.Auth; |
| 7 | |
| 8 | public class CloudEnvironmentTests |
| 9 | { |
| 10 | [Fact] |
| 11 | public void Public_HasCorrectEndpoints() |
| 12 | { |
| 13 | var env = CloudEnvironment.Public; |
| 14 | |
| 15 | Assert.Equal("https://login.microsoftonline.com", env.LoginEndpoint); |
| 16 | Assert.Equal("botframework.com", env.LoginTenant); |
| 17 | Assert.Equal("https://api.botframework.com/.default", env.BotScope); |
| 18 | Assert.Equal("https://token.botframework.com", env.TokenServiceUrl); |
| 19 | Assert.Equal("https://login.botframework.com/v1/.well-known/openidconfiguration", env.OpenIdMetadataUrl); |
| 20 | Assert.Equal("https://api.botframework.com", env.TokenIssuer); |
| 21 | } |
| 22 | |
| 23 | [Fact] |
| 24 | public void USGov_HasCorrectEndpoints() |
| 25 | { |
| 26 | var env = CloudEnvironment.USGov; |
| 27 | |
| 28 | Assert.Equal("https://login.microsoftonline.us", env.LoginEndpoint); |
| 29 | Assert.Equal("MicrosoftServices.onmicrosoft.us", env.LoginTenant); |
| 30 | Assert.Equal("https://api.botframework.us/.default", env.BotScope); |
| 31 | Assert.Equal("https://tokengcch.botframework.azure.us", env.TokenServiceUrl); |
| 32 | Assert.Equal("https://login.botframework.azure.us/v1/.well-known/openidconfiguration", env.OpenIdMetadataUrl); |
| 33 | Assert.Equal("https://api.botframework.us", env.TokenIssuer); |
| 34 | } |
| 35 | |
| 36 | [Fact] |
| 37 | public void USGovDoD_HasCorrectEndpoints() |
| 38 | { |
| 39 | var env = CloudEnvironment.USGovDoD; |
| 40 | |
| 41 | Assert.Equal("https://login.microsoftonline.us", env.LoginEndpoint); |
| 42 | Assert.Equal("MicrosoftServices.onmicrosoft.us", env.LoginTenant); |
| 43 | Assert.Equal("https://api.botframework.us/.default", env.BotScope); |
| 44 | Assert.Equal("https://apiDoD.botframework.azure.us", env.TokenServiceUrl); |
| 45 | Assert.Equal("https://login.botframework.azure.us/v1/.well-known/openidconfiguration", env.OpenIdMetadataUrl); |
| 46 | Assert.Equal("https://api.botframework.us", env.TokenIssuer); |
| 47 | } |
| 48 | |
| 49 | [Fact] |
| 50 | public void China_HasCorrectEndpoints() |
| 51 | { |
| 52 | var env = CloudEnvironment.China; |
| 53 | |
| 54 | Assert.Equal("https://login.partner.microsoftonline.cn", env.LoginEndpoint); |
| 55 | Assert.Equal("microsoftservices.partner.onmschina.cn", env.LoginTenant); |
| 56 | Assert.Equal("https://api.botframework.azure.cn/.default", env.BotScope); |
| 57 | Assert.Equal("https://token.botframework.azure.cn", env.TokenServiceUrl); |
| 58 | Assert.Equal("https://login.botframework.azure.cn/v1/.well-known/openidconfiguration", env.OpenIdMetadataUrl); |
| 59 | Assert.Equal("https://api.botframework.azure.cn", env.TokenIssuer); |
| 60 | } |
| 61 | |
| 62 | [Theory] |
| 63 | [InlineData("Public", "https://login.microsoftonline.com")] |
| 64 | [InlineData("public", "https://login.microsoftonline.com")] |
| 65 | [InlineData("PUBLIC", "https://login.microsoftonline.com")] |
| 66 | [InlineData("USGov", "https://login.microsoftonline.us")] |
| 67 | [InlineData("usgov", "https://login.microsoftonline.us")] |
| 68 | [InlineData("USGovDoD", "https://login.microsoftonline.us")] |
| 69 | [InlineData("usgovdod", "https://login.microsoftonline.us")] |
| 70 | [InlineData("China", "https://login.partner.microsoftonline.cn")] |
| 71 | [InlineData("china", "https://login.partner.microsoftonline.cn")] |
| 72 | public void FromName_ResolvesCorrectly(string name, string expectedLoginEndpoint) |
| 73 | { |
| 74 | var env = CloudEnvironment.FromName(name); |
| 75 | Assert.Equal(expectedLoginEndpoint, env.LoginEndpoint); |
| 76 | } |
| 77 | |
| 78 | [Theory] |
| 79 | [InlineData("invalid")] |
| 80 | [InlineData("")] |
| 81 | [InlineData("Azure")] |
| 82 | public void FromName_ThrowsForUnknownName(string name) |
| 83 | { |
| 84 | Assert.Throws<ArgumentException>(() => CloudEnvironment.FromName(name)); |
| 85 | } |
| 86 | |
| 87 | [Fact] |
| 88 | public void FromName_ReturnsStaticInstances() |
| 89 | { |
| 90 | Assert.Same(CloudEnvironment.Public, CloudEnvironment.FromName("Public")); |
| 91 | Assert.Same(CloudEnvironment.USGov, CloudEnvironment.FromName("USGov")); |
| 92 | Assert.Same(CloudEnvironment.USGovDoD, CloudEnvironment.FromName("USGovDoD")); |
| 93 | Assert.Same(CloudEnvironment.China, CloudEnvironment.FromName("China")); |
| 94 | } |
| 95 | |
| 96 | [Fact] |
| 97 | public void WithOverrides_AllNulls_ReturnsSameInstance() |
| 98 | { |
| 99 | var env = CloudEnvironment.Public; |
| 100 | |
| 101 | var result = env.WithOverrides(); |
| 102 | |
| 103 | Assert.Same(env, result); |
| 104 | } |
| 105 | |
| 106 | [Fact] |
| 107 | public void WithOverrides_SingleOverride_ReplacesOnlyThatProperty() |
| 108 | { |
| 109 | var env = CloudEnvironment.Public; |
| 110 | |
| 111 | var result = env.WithOverrides(loginTenant: "my-tenant-id"); |
| 112 | |
| 113 | Assert.NotSame(env, result); |
| 114 | Assert.Equal("my-tenant-id", result.LoginTenant); |
| 115 | Assert.Equal(env.LoginEndpoint, result.LoginEndpoint); |
| 116 | Assert.Equal(env.BotScope, result.BotScope); |
| 117 | Assert.Equal(env.TokenServiceUrl, result.TokenServiceUrl); |
| 118 | Assert.Equal(env.OpenIdMetadataUrl, result.OpenIdMetadataUrl); |
| 119 | Assert.Equal(env.TokenIssuer, result.TokenIssuer); |
| 120 | } |
| 121 | |
| 122 | [Fact] |
| 123 | public void WithOverrides_MultipleOverrides_ReplacesCorrectProperties() |
| 124 | { |
| 125 | var env = CloudEnvironment.China; |
| 126 | |
| 127 | var result = env.WithOverrides( |
| 128 | loginEndpoint: "https://custom.login.cn", |
| 129 | loginTenant: "custom-tenant", |
| 130 | tokenServiceUrl: "https://custom.token.cn" |
| 131 | ); |
| 132 | |
| 133 | Assert.Equal("https://custom.login.cn", result.LoginEndpoint); |
| 134 | Assert.Equal("custom-tenant", result.LoginTenant); |
| 135 | Assert.Equal("https://custom.token.cn", result.TokenServiceUrl); |
| 136 | // unchanged |
| 137 | Assert.Equal(env.BotScope, result.BotScope); |
| 138 | Assert.Equal(env.OpenIdMetadataUrl, result.OpenIdMetadataUrl); |
| 139 | Assert.Equal(env.TokenIssuer, result.TokenIssuer); |
| 140 | } |
| 141 | |
| 142 | [Fact] |
| 143 | public void WithOverrides_AllOverrides_ReplacesAllProperties() |
| 144 | { |
| 145 | var env = CloudEnvironment.Public; |
| 146 | |
| 147 | var result = env.WithOverrides( |
| 148 | loginEndpoint: "a", |
| 149 | loginTenant: "b", |
| 150 | botScope: "c", |
| 151 | tokenServiceUrl: "d", |
| 152 | openIdMetadataUrl: "e", |
| 153 | tokenIssuer: "f", |
| 154 | graphScope: "g" |
| 155 | ); |
| 156 | |
| 157 | Assert.Equal("a", result.LoginEndpoint); |
| 158 | Assert.Equal("b", result.LoginTenant); |
| 159 | Assert.Equal("c", result.BotScope); |
| 160 | Assert.Equal("d", result.TokenServiceUrl); |
| 161 | Assert.Equal("e", result.OpenIdMetadataUrl); |
| 162 | Assert.Equal("f", result.TokenIssuer); |
| 163 | Assert.Equal("g", result.GraphScope); |
| 164 | } |
| 165 | |
| 166 | [Fact] |
| 167 | public void ClientCredentials_DefaultsToPublicCloud() |
| 168 | { |
| 169 | var creds = new ClientCredentials("id", "secret"); |
| 170 | Assert.Same(CloudEnvironment.Public, creds.Cloud); |
| 171 | } |
| 172 | |
| 173 | [Fact] |
| 174 | public void ClientCredentials_CloudCanBeSet() |
| 175 | { |
| 176 | var creds = new ClientCredentials("id", "secret") |
| 177 | { |
| 178 | Cloud = CloudEnvironment.USGov |
| 179 | }; |
| 180 | Assert.Same(CloudEnvironment.USGov, creds.Cloud); |
| 181 | } |
| 182 | |
| 183 | [Fact] |
| 184 | public void ClientCredentials_UsesCloudLoginTenantWhenTenantIdNull() |
| 185 | { |
| 186 | var creds = new ClientCredentials("id", "secret") |
| 187 | { |
| 188 | Cloud = CloudEnvironment.USGov |
| 189 | }; |
| 190 | |
| 191 | // TenantId is null, so Cloud.LoginTenant should be used |
| 192 | Assert.Null(creds.TenantId); |
| 193 | Assert.Equal("MicrosoftServices.onmicrosoft.us", creds.Cloud.LoginTenant); |
| 194 | } |
| 195 | } |