using PostFundManagement.Application.Extensions; using PostFundManagement.Domain.Abstractions; using PostFundManagement.Infrastructure.Database.Services; using System.Diagnostics; using static PostFundManagement.Domain.Extensions.Constants; namespace PostFundManagement.Tests; /// /// Integration tests for SkorubaUserService.GetUserRolesAsync method. /// /// These tests hit the actual Skoruba API environment. /// Requires the following configuration: /// - MachineIdentitySettings with valid ClientId and ClientSecret from user secrets /// - SkorubaApi connection string pointing to the API endpoint /// - Network access to the Skoruba API /// /// To run these tests: /// dotnet test --filter "FullyQualifiedName~SkorubaUserServiceGetUserRolesIntegrationTests" /// public class SkorubaUserServiceGetUserRolesIntegrationTests { private readonly ISkorubaUserService _skorubaService; private readonly IHttpClientFactory _httpClientFactory; public SkorubaUserServiceGetUserRolesIntegrationTests() { var services = new ServiceCollection(); // Load configuration from environment and secrets var configuration = new ConfigurationBuilder() .AddJsonFile("appsettings.json", optional: true) .AddUserSecrets(Assembly.GetExecutingAssembly(), optional: true) .AddEnvironmentVariables() .Build(); services.AddSingleton(configuration); services.AddHttpClient(); // Register the Skoruba HTTP client with the actual DI configuration services.AddSkorubaApiService(configuration) .AddRegisterServicesWithCaching(configuration) .AddEndpointAndServiceCaching(configuration) .AddSharedServices() .AddHashServices(configuration) .AddApplicationDbContext(configuration) .AddHttpContextAccessor() .AddTransient(); var serviceProvider = services.BuildServiceProvider(); _skorubaService = serviceProvider.GetRequiredService(); _httpClientFactory = serviceProvider.GetRequiredService(); } /// /// Integration test: Verify HttpClient is properly configured with SkorubaHttpClient name. /// [DebugOnlyFact] public void HttpClientFactory_HasSkorubaHttpClientConfigured() { // Act var httpClient = _httpClientFactory.CreateClient(SkorubaHttpClient); // Assert Assert.NotNull(httpClient); Assert.NotNull(httpClient.BaseAddress); } /// /// Integration test: Verify that the service properly formats API requests. /// Uses a fake/invalid user ID to test the HTTP request flow. /// [DebugOnlyFact] public async Task GetUserRolesAsync_SendsProperlyFormattedRequest() { // Arrange var userId = Guid.NewGuid(); var page = 2; var pageSize = 25; // Act - This will likely fail with 401 or 404 var result = await _skorubaService.GetUserRolesAsync(userId, page, pageSize); // Assert - Request was processed (even if it failed) Assert.False(result.IsSuccess); Assert.NotNull(result.Errors); } /// /// Integration test: Verify cancellation token is respected. /// [DebugOnlyFact] public async Task GetUserRolesAsync_RespectsCancellationToken() { // Arrange var userId = Guid.NewGuid(); var cts = new CancellationTokenSource(); cts.Cancel(); // Act & Assert await Assert.ThrowsAsync(async () => { await _skorubaService.GetUserRolesAsync(userId, cancellationToken: cts.Token); }); } }