Added skoruba user service integration test
This commit is contained in:
@@ -0,0 +1,39 @@
|
||||
<Project Sdk="Microsoft.NET.Sdk">
|
||||
|
||||
<PropertyGroup>
|
||||
<TargetFramework>net10.0</TargetFramework>
|
||||
<ImplicitUsings>enable</ImplicitUsings>
|
||||
<Nullable>enable</Nullable>
|
||||
<IsPackable>false</IsPackable>
|
||||
<UserSecretsId>079fcde1-9b10-4402-9624-5f0827b4dbfd</UserSecretsId>
|
||||
</PropertyGroup>
|
||||
|
||||
<ItemGroup>
|
||||
<Content Include="appsettings.json">
|
||||
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
|
||||
<ExcludeFromSingleFile>true</ExcludeFromSingleFile>
|
||||
<CopyToPublishDirectory>PreserveNewest</CopyToPublishDirectory>
|
||||
</Content>
|
||||
</ItemGroup>
|
||||
|
||||
<ItemGroup>
|
||||
<PackageReference Include="coverlet.collector" Version="6.0.4" />
|
||||
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="17.14.1" />
|
||||
<PackageReference Include="xunit" Version="2.9.3" />
|
||||
<PackageReference Include="xunit.runner.visualstudio" Version="3.1.4" />
|
||||
</ItemGroup>
|
||||
|
||||
<ItemGroup>
|
||||
<Using Include="Xunit" />
|
||||
<Using Include="Microsoft.Extensions.Configuration" />
|
||||
<Using Include="Microsoft.Extensions.DependencyInjection" />
|
||||
<Using Include="System.Reflection" />
|
||||
</ItemGroup>
|
||||
|
||||
<ItemGroup>
|
||||
<ProjectReference Include="..\PostFundManagement.Application\PostFundManagement.Application.csproj" />
|
||||
<ProjectReference Include="..\PostFundManagement.Infrastructure\PostFundManagement.Infrastructure.csproj" />
|
||||
<ProjectReference Include="..\PostFundManagement.Domain\PostFundManagement.Domain.csproj" />
|
||||
</ItemGroup>
|
||||
|
||||
</Project>
|
||||
@@ -0,0 +1,109 @@
|
||||
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>
|
||||
//[Fact(Skip = "Skipping integration test")]
|
||||
[Fact]
|
||||
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>
|
||||
//[Fact(Skip = "Skipping integration test")]
|
||||
[Fact]
|
||||
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>
|
||||
//[Fact(Skip = "Skipping integration test")]
|
||||
[Fact]
|
||||
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);
|
||||
});
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,45 @@
|
||||
{
|
||||
"EnableServiceCaching": true,
|
||||
"CorsOrigins": [
|
||||
"https://console.ifri.khongisa.co.za",
|
||||
"https://localhost:7208"
|
||||
],
|
||||
"GeneralS3Settings": {
|
||||
"ServiceUrl": "https://s3.garage.khongisa.co.za",
|
||||
"Region": "garage",
|
||||
"BucketName": "pfm",
|
||||
"CdnBaseUrl": "https://pfm.cdn.khongisa.co.za"
|
||||
},
|
||||
"Monitoring": {
|
||||
"Address": "http://aspire-dashboard-service.aspire.svc.cluster.local:18889",
|
||||
"ServiceName": "Pfm.DEV"
|
||||
},
|
||||
"SecuritySettings": {
|
||||
"Authority": "https://sts.security.khongisa.co.za",
|
||||
"Audience": "pfm-api-dev"
|
||||
},
|
||||
"MachineIdentitySettings": {
|
||||
"Authority": "https://sts.security.khongisa.co.za",
|
||||
"Scope": "skoruba_identity_admin_api"
|
||||
},
|
||||
"Logging": {
|
||||
"LogLevel": {
|
||||
"Default": "Warning",
|
||||
"Quartz": "Error",
|
||||
"Microsoft.AspNetCore": "Warning",
|
||||
"Microsoft.EntityFrameworkCore.Database": "Error",
|
||||
"Microsoft.EntityFrameworkCore.Update": "Error"
|
||||
}
|
||||
},
|
||||
"EmailProcessorCron": "0 */5 * * * ?",
|
||||
"Email": {
|
||||
"SmtpServer": "smtp.example.com",
|
||||
"SmtpPort": 587,
|
||||
"SenderEmail": "noreply@example.com",
|
||||
"SenderName": "Example"
|
||||
},
|
||||
"HasherSettings": {
|
||||
"MinHashLength": 11
|
||||
},
|
||||
"AllowedHosts": "*"
|
||||
}
|
||||
Reference in New Issue
Block a user