107 lines
3.8 KiB
C#
107 lines
3.8 KiB
C#
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;
|
|
|
|
/// <summary>
|
|
/// 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"
|
|
/// </summary>
|
|
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<ITenantContextService, TenantContextService>();
|
|
|
|
var serviceProvider = services.BuildServiceProvider();
|
|
_skorubaService = serviceProvider.GetRequiredService<ISkorubaUserService>();
|
|
_httpClientFactory = serviceProvider.GetRequiredService<IHttpClientFactory>();
|
|
}
|
|
|
|
/// <summary>
|
|
/// Integration test: Verify HttpClient is properly configured with SkorubaHttpClient name.
|
|
/// </summary>
|
|
[DebugOnlyFact]
|
|
public void HttpClientFactory_HasSkorubaHttpClientConfigured()
|
|
{
|
|
// Act
|
|
var httpClient = _httpClientFactory.CreateClient(SkorubaHttpClient);
|
|
|
|
// Assert
|
|
Assert.NotNull(httpClient);
|
|
Assert.NotNull(httpClient.BaseAddress);
|
|
}
|
|
|
|
/// <summary>
|
|
/// Integration test: Verify that the service properly formats API requests.
|
|
/// Uses a fake/invalid user ID to test the HTTP request flow.
|
|
/// </summary>
|
|
[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);
|
|
}
|
|
|
|
/// <summary>
|
|
/// Integration test: Verify cancellation token is respected.
|
|
/// </summary>
|
|
[DebugOnlyFact]
|
|
public async Task GetUserRolesAsync_RespectsCancellationToken()
|
|
{
|
|
// Arrange
|
|
var userId = Guid.NewGuid();
|
|
var cts = new CancellationTokenSource();
|
|
cts.Cancel();
|
|
|
|
// Act & Assert
|
|
await Assert.ThrowsAsync<OperationCanceledException>(async () =>
|
|
{
|
|
await _skorubaService.GetUserRolesAsync(userId, cancellationToken: cts.Token);
|
|
});
|
|
}
|
|
}
|