microsoft/teams.net
Publicmirrored fromhttps://github.com/microsoft/teams.netAvailable
core/test/IntegrationTests/IntegrationTestFixture.cs
100lines · modecode
| 1 | // Copyright (c) Microsoft Corporation. |
| 2 | // Licensed under the MIT License. |
| 3 | |
| 4 | using MartinCostello.Logging.XUnit; |
| 5 | using Microsoft.Extensions.Configuration; |
| 6 | using Microsoft.Extensions.DependencyInjection; |
| 7 | using Microsoft.Extensions.Logging; |
| 8 | using Microsoft.Teams.Bot.Apps; |
| 9 | using Microsoft.Teams.Bot.Apps.Api.Clients; |
| 10 | using Microsoft.Teams.Bot.Core; |
| 11 | using Microsoft.Teams.Bot.Core.Schema; |
| 12 | using Xunit.Abstractions; |
| 13 | |
| 14 | namespace IntegrationTests; |
| 15 | |
| 16 | /// <summary> |
| 17 | /// Shared fixture that configures DI, acquires tokens, and exposes clients for integration tests. |
| 18 | /// Reused across test classes via IClassFixture to avoid repeated token acquisition. |
| 19 | /// </summary> |
| 20 | public class IntegrationTestFixture : IDisposable, ITestOutputHelperAccessor |
| 21 | { |
| 22 | public ServiceProvider ServiceProvider { get; } |
| 23 | public ConversationClient ConversationClient { get; } |
| 24 | public ApiClient ApiClient { get; } |
| 25 | |
| 26 | public Uri ServiceUrl { get; } |
| 27 | public string ConversationId { get; } |
| 28 | public string UserId { get; } |
| 29 | public string TeamId { get; } |
| 30 | public string ChannelId { get; } |
| 31 | public string MeetingId { get; } |
| 32 | public string TenantId { get; } |
| 33 | public string BotAppId { get; } |
| 34 | public string? UserId2 { get; } |
| 35 | public AgenticIdentity? AgenticIdentity { get; } |
| 36 | |
| 37 | /// <summary> |
| 38 | /// Set by each test class constructor to route ILogger output to xUnit's test output. |
| 39 | /// </summary> |
| 40 | public ITestOutputHelper? OutputHelper { get; set; } |
| 41 | |
| 42 | public IntegrationTestFixture() |
| 43 | { |
| 44 | IConfiguration configuration = new ConfigurationBuilder() |
| 45 | .SetBasePath(AppDomain.CurrentDomain.BaseDirectory) |
| 46 | .AddEnvironmentVariables() |
| 47 | .Build(); |
| 48 | |
| 49 | ServiceCollection services = new(); |
| 50 | services.AddLogging(builder => |
| 51 | { |
| 52 | builder.AddXUnit(this); |
| 53 | builder.AddFilter("System.Net", LogLevel.Warning); |
| 54 | builder.AddFilter("Microsoft.Identity", LogLevel.Error); |
| 55 | builder.AddFilter("Microsoft.Teams", LogLevel.Information); |
| 56 | }); |
| 57 | services.AddSingleton(configuration); |
| 58 | services.AddTeamsBotApplication(); |
| 59 | |
| 60 | ServiceProvider = services.BuildServiceProvider(); |
| 61 | ConversationClient = ServiceProvider.GetRequiredService<ConversationClient>(); |
| 62 | ApiClient = ServiceProvider.GetRequiredService<ApiClient>(); |
| 63 | |
| 64 | ServiceUrl = new Uri(Env("TEST_SERVICEURL", "https://smba.trafficmanager.net/teams/")); |
| 65 | ConversationId = Env("TEST_CONVERSATIONID"); |
| 66 | UserId = Env("TEST_USER_ID"); |
| 67 | TeamId = Env("TEST_TEAMID"); |
| 68 | ChannelId = Env("TEST_CHANNELID"); |
| 69 | MeetingId = Env("TEST_MEETINGID"); |
| 70 | TenantId = Env("TEST_TENANTID"); |
| 71 | BotAppId = Env("AzureAd__ClientId"); |
| 72 | UserId2 = Environment.GetEnvironmentVariable("TEST_USER_ID_2"); |
| 73 | |
| 74 | string? agenticAppId = Environment.GetEnvironmentVariable("TEST_AGENTIC_APPID"); |
| 75 | string? agenticUserId = Environment.GetEnvironmentVariable("TEST_AGENTIC_USERID"); |
| 76 | |
| 77 | if (!string.IsNullOrEmpty(agenticAppId) && !string.IsNullOrEmpty(agenticUserId)) |
| 78 | { |
| 79 | string appBlueprintId = Env("AzureAd__ClientId"); |
| 80 | ConversationAccount recipient = new(); |
| 81 | recipient.Properties.Add("agenticAppBlueprintId", appBlueprintId); |
| 82 | recipient.Properties.Add("agenticAppId", agenticAppId); |
| 83 | recipient.Properties.Add("agenticUserId", agenticUserId); |
| 84 | AgenticIdentity = AgenticIdentity.FromProperties(recipient.Properties); |
| 85 | } |
| 86 | } |
| 87 | |
| 88 | public ApiClient ScopedApiClient => ApiClient.ForServiceUrl(ServiceUrl); |
| 89 | |
| 90 | public void Dispose() |
| 91 | { |
| 92 | ServiceProvider.Dispose(); |
| 93 | GC.SuppressFinalize(this); |
| 94 | } |
| 95 | |
| 96 | private static string Env(string name, string? fallback = null) => |
| 97 | Environment.GetEnvironmentVariable(name) |
| 98 | ?? fallback |
| 99 | ?? throw new InvalidOperationException($"{name} environment variable not set"); |
| 100 | } |
| 101 | |