microsoft/teams.net
Publicmirrored fromhttps://github.com/microsoft/teams.netAvailable
Tests/Microsoft.Teams.Api.Tests/Clients/BotTokenClientTests.cs
195lines · modecode
| 1 | using Microsoft.Teams.Api.Auth; |
| 2 | using Microsoft.Teams.Api.Clients; |
| 3 | using Microsoft.Teams.Common.Http; |
| 4 | |
| 5 | |
| 6 | |
| 7 | |
| 8 | namespace Microsoft.Teams.Api.Tests.Clients; |
| 9 | |
| 10 | public class BotTokenClientTests |
| 11 | { |
| 12 | readonly string accessToken = "eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJzdWIiOiIxMjM0NTY3ODkwIiwibmFtZSI6IkpvaG4gRG9lIiwiYWRtaW4iOnRydWUsImlhdCI6MTUxNjIzOTAyMn0.KMUFsIDTnFmyG3nMiGM6H9FNFUROf3wh7SmqJp-QV30"; |
| 13 | |
| 14 | [Fact] |
| 15 | public async Task BotTokenClient_Default_GetAsync_Async() |
| 16 | { |
| 17 | var cancellationToken = new CancellationToken(); |
| 18 | |
| 19 | string? actualTenantId = ""; |
| 20 | string[] actualScope = [""]; |
| 21 | TokenFactory tokenFactory = new TokenFactory(async (tenantId, scope) => |
| 22 | { |
| 23 | actualTenantId = tenantId; |
| 24 | actualScope = scope; |
| 25 | return await Task.FromResult<ITokenResponse>(new TokenResponse |
| 26 | { |
| 27 | TokenType = "Bearer", |
| 28 | AccessToken = accessToken |
| 29 | }); |
| 30 | }); |
| 31 | var credentials = new TokenCredentials("clientId", tokenFactory); |
| 32 | var botTokenClient = new BotTokenClient(cancellationToken); |
| 33 | |
| 34 | var botToken = await botTokenClient.GetAsync(credentials); |
| 35 | |
| 36 | Assert.NotNull(botToken); |
| 37 | Assert.Equal(accessToken, new JsonWebToken(botToken.AccessToken).ToString()); |
| 38 | Assert.Null(actualTenantId); |
| 39 | Assert.Equal("https://api.botframework.com/.default", actualScope[0]); |
| 40 | } |
| 41 | |
| 42 | [Fact] |
| 43 | public async Task BotTokenClient_Default_GetGraphAsync_Async() |
| 44 | { |
| 45 | var cancellationToken = new CancellationToken(); |
| 46 | string? actualTenantId = ""; |
| 47 | string[] actualScope = [""]; |
| 48 | TokenFactory tokenFactory = new TokenFactory(async (tenantId, scope) => |
| 49 | { |
| 50 | actualTenantId = tenantId; |
| 51 | actualScope = scope; |
| 52 | return await Task.FromResult<ITokenResponse>(new TokenResponse |
| 53 | { |
| 54 | TokenType = "Bearer", |
| 55 | AccessToken = accessToken |
| 56 | }); |
| 57 | }); |
| 58 | var credentials = new TokenCredentials("clientId", tokenFactory); |
| 59 | var botTokenClient = new BotTokenClient(cancellationToken); |
| 60 | |
| 61 | var botGraphToken = await botTokenClient.GetGraphAsync(credentials); |
| 62 | |
| 63 | Assert.Equal(accessToken, new JsonWebToken(botGraphToken.AccessToken).ToString()); |
| 64 | Assert.Null(actualTenantId); |
| 65 | Assert.Equal("https://graph.microsoft.com/.default", actualScope[0]); |
| 66 | } |
| 67 | |
| 68 | [Fact] |
| 69 | public async Task BotTokenClient_withTenantIdAsync() |
| 70 | { |
| 71 | var cancellationToken = new CancellationToken(); |
| 72 | string? actualTenantId = ""; |
| 73 | string[] actualScope = [""]; |
| 74 | TokenFactory tokenFactory = new TokenFactory(async (tenantId, scope) => |
| 75 | { |
| 76 | actualTenantId = tenantId; |
| 77 | actualScope = scope; |
| 78 | return await Task.FromResult<ITokenResponse>(new TokenResponse |
| 79 | { |
| 80 | TokenType = "Bearer", |
| 81 | AccessToken = accessToken |
| 82 | }); |
| 83 | }); |
| 84 | var credentials = new TokenCredentials("clientId", "123-abc", tokenFactory); |
| 85 | var botTokenClient = new BotTokenClient(cancellationToken); |
| 86 | string expectedTenantId = "123-abc"; |
| 87 | |
| 88 | var botGraphToken = await botTokenClient.GetGraphAsync(credentials); |
| 89 | |
| 90 | Assert.Equal(accessToken, new JsonWebToken(botGraphToken.AccessToken).ToString()); |
| 91 | Assert.Equal(expectedTenantId, actualTenantId); |
| 92 | Assert.Equal("https://graph.microsoft.com/.default", actualScope[0]); |
| 93 | } |
| 94 | |
| 95 | [Fact] |
| 96 | public async Task BotTokenClient_httpClient_Async() |
| 97 | { |
| 98 | var cancellationToken = new CancellationToken(); |
| 99 | string? actualTenantId = ""; |
| 100 | string[] actualScope = [""]; |
| 101 | TokenFactory tokenFactory = new TokenFactory(async (tenantId, scope) => |
| 102 | { |
| 103 | actualTenantId = tenantId; |
| 104 | actualScope = scope; |
| 105 | return await Task.FromResult<ITokenResponse>(new TokenResponse |
| 106 | { |
| 107 | TokenType = "Bearer", |
| 108 | AccessToken = accessToken |
| 109 | }); |
| 110 | }); |
| 111 | |
| 112 | var credentials = new TokenCredentials("clientId", "123-abc", tokenFactory); |
| 113 | var httpClient = new Common.Http.HttpClient(); |
| 114 | var botTokenClient = new BotTokenClient(httpClient, cancellationToken); |
| 115 | |
| 116 | var botToken = await botTokenClient.GetAsync(credentials); |
| 117 | |
| 118 | string expectedTenantId = "123-abc"; |
| 119 | Assert.Equal(expectedTenantId, actualTenantId); |
| 120 | Assert.Equal("https://api.botframework.com/.default", actualScope[0]); |
| 121 | |
| 122 | } |
| 123 | |
| 124 | [Fact] |
| 125 | public async Task BotTokenClient_HttpClientOptions_Async() |
| 126 | { |
| 127 | var cancellationToken = new CancellationToken(); |
| 128 | string? actualTenantId = ""; |
| 129 | string[] actualScope = [""]; |
| 130 | TokenFactory tokenFactory = new TokenFactory(async (tenantId, scope) => |
| 131 | { |
| 132 | actualTenantId = tenantId; |
| 133 | actualScope = scope; |
| 134 | return await Task.FromResult<ITokenResponse>(new TokenResponse |
| 135 | { |
| 136 | TokenType = "Bearer", |
| 137 | AccessToken = accessToken |
| 138 | }); |
| 139 | }); |
| 140 | var credentials = new TokenCredentials("clientId", "123-abc", tokenFactory); |
| 141 | var httpClientOtions = new Common.Http.HttpClientOptions(); |
| 142 | var botTokenClient = new BotTokenClient(httpClientOtions, cancellationToken); |
| 143 | |
| 144 | var botToken = await botTokenClient.GetAsync(credentials); |
| 145 | |
| 146 | string expectedTenantId = "123-abc"; |
| 147 | Assert.Equal(expectedTenantId, actualTenantId); |
| 148 | Assert.Equal("https://api.botframework.com/.default", actualScope[0]); |
| 149 | } |
| 150 | |
| 151 | [Fact] |
| 152 | public void ActiveBotScope_DefaultsToPublicBotScope() |
| 153 | { |
| 154 | var client = new BotTokenClient(); |
| 155 | Assert.Equal(BotTokenClient.BotScope, client.ActiveBotScope); |
| 156 | Assert.Equal("https://api.botframework.com/.default", client.ActiveBotScope); |
| 157 | } |
| 158 | |
| 159 | [Fact] |
| 160 | public void ActiveBotScope_CanBeOverridden() |
| 161 | { |
| 162 | var client = new BotTokenClient(); |
| 163 | client.ActiveBotScope = "https://api.botframework.us/.default"; |
| 164 | Assert.Equal("https://api.botframework.us/.default", client.ActiveBotScope); |
| 165 | } |
| 166 | |
| 167 | [Fact] |
| 168 | public void BotScope_StaticFieldUnchanged() |
| 169 | { |
| 170 | Assert.Equal("https://api.botframework.com/.default", BotTokenClient.BotScope); |
| 171 | } |
| 172 | |
| 173 | [Fact] |
| 174 | public async Task BotTokenClient_ActiveBotScope_UsedInGetAsync() |
| 175 | { |
| 176 | var cancellationToken = new CancellationToken(); |
| 177 | string[] actualScope = [""]; |
| 178 | TokenFactory tokenFactory = new TokenFactory(async (tenantId, scope) => |
| 179 | { |
| 180 | actualScope = scope; |
| 181 | return await Task.FromResult<ITokenResponse>(new TokenResponse |
| 182 | { |
| 183 | TokenType = "Bearer", |
| 184 | AccessToken = accessToken |
| 185 | }); |
| 186 | }); |
| 187 | var credentials = new TokenCredentials("clientId", tokenFactory); |
| 188 | var botTokenClient = new BotTokenClient(cancellationToken); |
| 189 | botTokenClient.ActiveBotScope = "https://api.botframework.us/.default"; |
| 190 | |
| 191 | await botTokenClient.GetAsync(credentials); |
| 192 | |
| 193 | Assert.Equal("https://api.botframework.us/.default", actualScope[0]); |
| 194 | } |
| 195 | } |