microsoft/teams.net

Public

mirrored fromhttps://github.com/microsoft/teams.netAvailable

CodeCommitsIssuesPull requestsActionsInsightsSecurity
docs/update-release-process

Branches

Tags

  • No tags available.
0Branches0Tags
Go to file
Add file
Code

Clone

HTTPS

Download ZIP

core/test/Microsoft.Teams.Apps.BotBuilder.UnitTests/CompatAdapterTests.cs

148lines · modecode

1// Copyright (c) Microsoft Corporation.
2// Licensed under the MIT License.
3
4using System.Text;
5using Microsoft.AspNetCore.Http;
6using Microsoft.Bot.Builder;
7using Microsoft.Bot.Schema;
8using Microsoft.Extensions.Configuration;
9using Microsoft.Extensions.Logging.Abstractions;
10using Microsoft.Teams.Core;
11using Microsoft.Teams.Core.Schema;
12using Moq;
13
14namespace Microsoft.Teams.Apps.BotBuilder.UnitTests
15{
16 public class TeamsBotFrameworkHttpAdapterTests
17 {
18 [Fact]
19 public async Task ContinueConversationAsync_WhenCastToBotAdapter_BuildsTurnContextWithUnderlyingClients()
20 {
21 // Arrange
22 TeamsBotFrameworkHttpAdapter compatAdapter = CreateCompatAdapter();
23
24 // Cast to BotAdapter to ensure we're using the base class method
25 BotAdapter botAdapter = compatAdapter;
26
27 ConversationReference conversationReference = new()
28 {
29 ServiceUrl = "https://smba.trafficmanager.net/teams",
30 ChannelId = "msteams",
31 Conversation = new Microsoft.Bot.Schema.ConversationAccount { Id = "test-conversation-id" }
32 };
33
34 bool callbackInvoked = false;
35 Microsoft.Bot.Connector.Authentication.UserTokenClient? capturedUserTokenClient = null;
36 Microsoft.Bot.Connector.IConnectorClient? capturedConnectorClient = null;
37
38 BotCallbackHandler callback = async (turnContext, cancellationToken) =>
39 {
40 callbackInvoked = true;
41 capturedUserTokenClient = turnContext.TurnState.Get<Microsoft.Bot.Connector.Authentication.UserTokenClient>();
42 capturedConnectorClient = turnContext.TurnState.Get<Microsoft.Bot.Connector.IConnectorClient>();
43 await Task.CompletedTask;
44 };
45
46 // Act
47 await botAdapter.ContinueConversationAsync(
48 "test-bot-id",
49 conversationReference,
50 callback,
51 CancellationToken.None);
52
53 // Assert
54 Assert.True(callbackInvoked);
55
56 // Verify UserTokenClient is CompatUserTokenClient (check by type name since it's internal)
57 Assert.NotNull(capturedUserTokenClient);
58 Assert.Equal("CompatUserTokenClient", capturedUserTokenClient.GetType().Name);
59 Assert.IsAssignableFrom<Microsoft.Bot.Connector.Authentication.UserTokenClient>(capturedUserTokenClient);
60
61 // Verify ConnectorClient is CompatConnectorClient (check by type name since it's internal)
62 Assert.NotNull(capturedConnectorClient);
63 Assert.Equal("CompatConnectorClient", capturedConnectorClient.GetType().Name);
64 Assert.IsAssignableFrom<Microsoft.Bot.Connector.IConnectorClient>(capturedConnectorClient);
65 }
66
67 [Fact]
68 public async Task ProcessAsync_OnTurnError_ReceivesTurnContextWithTurnState()
69 {
70 // Arrange
71 TeamsBotFrameworkHttpAdapter adapter = CreateCompatAdapter();
72
73 ITurnContext? capturedTurnContext = null;
74 Microsoft.Bot.Connector.Authentication.UserTokenClient? capturedUserTokenClient = null;
75 Microsoft.Bot.Connector.IConnectorClient? capturedConnectorClient = null;
76 string? capturedCustomTurnState = null;
77
78 adapter.OnTurnError = (turnContext, exception) =>
79 {
80 capturedTurnContext = turnContext;
81 capturedUserTokenClient = turnContext.TurnState.Get<Microsoft.Bot.Connector.Authentication.UserTokenClient>();
82 capturedConnectorClient = turnContext.TurnState.Get<Microsoft.Bot.Connector.IConnectorClient>();
83 capturedCustomTurnState = turnContext.TurnState.Get<string>("customTurnStateKey");
84 return Task.CompletedTask;
85 };
86
87 Mock<IBot> mockBot = new();
88 mockBot
89 .Setup(b => b.OnTurnAsync(It.IsAny<ITurnContext>(), It.IsAny<CancellationToken>()))
90 .Returns<ITurnContext, CancellationToken>((tc, _) =>
91 {
92 tc.TurnState.Add("customTurnStateKey", "customTurnStateValue");
93 throw new InvalidOperationException("Test exception");
94 });
95
96 CoreActivity activity = new()
97 {
98 Type = ActivityType.Message,
99 Id = "act123",
100 ServiceUrl = new Uri("https://smba.trafficmanager.net/teams/"),
101 Conversation = new Conversation("conv123"),
102 From = new Teams.Core.Schema.ConversationAccount { Id = "user123" }
103 };
104
105 DefaultHttpContext httpContext = new();
106 byte[] bodyBytes = Encoding.UTF8.GetBytes(activity.ToJson());
107 httpContext.Request.Body = new MemoryStream(bodyBytes);
108 httpContext.Request.ContentType = "application/json";
109
110 // Act
111 await adapter.ProcessAsync(httpContext.Request, httpContext.Response, mockBot.Object, CancellationToken.None);
112
113 // Assert
114 Assert.NotNull(capturedTurnContext);
115
116 Assert.NotNull(capturedUserTokenClient);
117 Assert.Equal("CompatUserTokenClient", capturedUserTokenClient.GetType().Name);
118
119 Assert.NotNull(capturedConnectorClient);
120 Assert.Equal("CompatConnectorClient", capturedConnectorClient.GetType().Name);
121
122 Assert.Equal("customTurnStateValue", capturedCustomTurnState);
123 }
124
125 private static TeamsBotFrameworkHttpAdapter CreateCompatAdapter()
126 {
127 HttpClient httpClient = new();
128 ConversationClient conversationClient = new(httpClient, NullLogger<ConversationClient>.Instance);
129
130 Mock<IConfiguration> mockConfig = new();
131 mockConfig.Setup(c => c["UserTokenApiEndpoint"]).Returns("https://token.botframework.com");
132
133 UserTokenClient userTokenClient = new(httpClient, mockConfig.Object, NullLogger<UserTokenClient>.Instance);
134
135 BotApplication botApplication = new(
136 conversationClient,
137 userTokenClient,
138 NullLogger<BotApplication>.Instance);
139
140 TeamsBotFrameworkHttpAdapter compatAdapter = new(
141 botApplication,
142 Mock.Of<IHttpContextAccessor>(),
143 NullLogger<TeamsBotFrameworkHttpAdapter>.Instance);
144
145 return compatAdapter;
146 }
147 }
148}
149