microsoft/teams.net
Publicmirrored from https://github.com/microsoft/teams.netAvailable
core/test/Microsoft.Teams.Apps.BotBuilder.UnitTests/CompatAdapterTests.cs
88lines · modecode
| 1 | // Copyright (c) Microsoft Corporation. |
| 2 | // Licensed under the MIT License. |
| 3 | |
| 4 | using Microsoft.AspNetCore.Http; |
| 5 | using Microsoft.Bot.Builder; |
| 6 | using Microsoft.Bot.Schema; |
| 7 | using Microsoft.Extensions.Configuration; |
| 8 | using Microsoft.Extensions.Logging.Abstractions; |
| 9 | using Microsoft.Teams.Core; |
| 10 | using Moq; |
| 11 | |
| 12 | namespace Microsoft.Teams.Apps.BotBuilder.UnitTests |
| 13 | { |
| 14 | public class TeamsBotFrameworkHttpAdapterTests |
| 15 | { |
| 16 | [Fact] |
| 17 | public async Task ContinueConversationAsync_WhenCastToBotAdapter_BuildsTurnContextWithUnderlyingClients() |
| 18 | { |
| 19 | // Arrange |
| 20 | TeamsBotFrameworkHttpAdapter compatAdapter = CreateCompatAdapter(); |
| 21 | |
| 22 | // Cast to BotAdapter to ensure we're using the base class method |
| 23 | BotAdapter botAdapter = compatAdapter; |
| 24 | |
| 25 | ConversationReference conversationReference = new() |
| 26 | { |
| 27 | ServiceUrl = "https://smba.trafficmanager.net/teams", |
| 28 | ChannelId = "msteams", |
| 29 | Conversation = new Microsoft.Bot.Schema.ConversationAccount { Id = "test-conversation-id" } |
| 30 | }; |
| 31 | |
| 32 | bool callbackInvoked = false; |
| 33 | Microsoft.Bot.Connector.Authentication.UserTokenClient? capturedUserTokenClient = null; |
| 34 | Microsoft.Bot.Connector.IConnectorClient? capturedConnectorClient = null; |
| 35 | |
| 36 | BotCallbackHandler callback = async (turnContext, cancellationToken) => |
| 37 | { |
| 38 | callbackInvoked = true; |
| 39 | capturedUserTokenClient = turnContext.TurnState.Get<Microsoft.Bot.Connector.Authentication.UserTokenClient>(); |
| 40 | capturedConnectorClient = turnContext.TurnState.Get<Microsoft.Bot.Connector.IConnectorClient>(); |
| 41 | await Task.CompletedTask; |
| 42 | }; |
| 43 | |
| 44 | // Act |
| 45 | await botAdapter.ContinueConversationAsync( |
| 46 | "test-bot-id", |
| 47 | conversationReference, |
| 48 | callback, |
| 49 | CancellationToken.None); |
| 50 | |
| 51 | // Assert |
| 52 | Assert.True(callbackInvoked); |
| 53 | |
| 54 | // Verify UserTokenClient is CompatUserTokenClient (check by type name since it's internal) |
| 55 | Assert.NotNull(capturedUserTokenClient); |
| 56 | Assert.Equal("CompatUserTokenClient", capturedUserTokenClient.GetType().Name); |
| 57 | Assert.IsAssignableFrom<Microsoft.Bot.Connector.Authentication.UserTokenClient>(capturedUserTokenClient); |
| 58 | |
| 59 | // Verify ConnectorClient is CompatConnectorClient (check by type name since it's internal) |
| 60 | Assert.NotNull(capturedConnectorClient); |
| 61 | Assert.Equal("CompatConnectorClient", capturedConnectorClient.GetType().Name); |
| 62 | Assert.IsAssignableFrom<Microsoft.Bot.Connector.IConnectorClient>(capturedConnectorClient); |
| 63 | } |
| 64 | |
| 65 | private static TeamsBotFrameworkHttpAdapter CreateCompatAdapter() |
| 66 | { |
| 67 | HttpClient httpClient = new(); |
| 68 | ConversationClient conversationClient = new(httpClient, NullLogger<ConversationClient>.Instance); |
| 69 | |
| 70 | Mock<IConfiguration> mockConfig = new(); |
| 71 | mockConfig.Setup(c => c["UserTokenApiEndpoint"]).Returns("https://token.botframework.com"); |
| 72 | |
| 73 | UserTokenClient userTokenClient = new(httpClient, mockConfig.Object, NullLogger<UserTokenClient>.Instance); |
| 74 | |
| 75 | BotApplication botApplication = new( |
| 76 | conversationClient, |
| 77 | userTokenClient, |
| 78 | NullLogger<BotApplication>.Instance); |
| 79 | |
| 80 | TeamsBotFrameworkHttpAdapter compatAdapter = new( |
| 81 | botApplication, |
| 82 | Mock.Of<IHttpContextAccessor>(), |
| 83 | NullLogger<TeamsBotFrameworkHttpAdapter>.Instance); |
| 84 | |
| 85 | return compatAdapter; |
| 86 | } |
| 87 | } |
| 88 | } |
| 89 | |