microsoft/teams.net

Public

mirrored from https://github.com/microsoft/teams.netAvailable

CodeCommitsIssuesPull requestsActionsInsightsSecurity
copilot/sub-pr-338

Branches

Tags

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

Clone

HTTPS

Download ZIP

core/test/Microsoft.Teams.Bot.Compat.UnitTests/CompatAdapterTests.cs

104lines · modecode

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