83 lines
4.6 KiB
C#
83 lines
4.6 KiB
C#
using System.Net;
|
|
using System.Security.Claims;
|
|
using System.Text;
|
|
using System.Text.Encodings.Web;
|
|
using HealthApp.TestSupport;
|
|
using Microsoft.AspNetCore.Authentication;
|
|
using Microsoft.AspNetCore.Hosting;
|
|
using Microsoft.AspNetCore.Mvc.Testing;
|
|
using Microsoft.Extensions.Configuration;
|
|
using Microsoft.Extensions.DependencyInjection;
|
|
using Microsoft.Extensions.Logging;
|
|
using Microsoft.Extensions.Options;
|
|
using Xunit;
|
|
|
|
namespace PatientProfile.AcceptanceTests;
|
|
|
|
public sealed class AuthenticationIntegrationTests
|
|
{
|
|
[Fact]
|
|
public async Task Production_gateway_rejects_plain_http_even_for_login_and_public_guidance()
|
|
{
|
|
var authentication = new AuthenticatedTestClient();
|
|
try
|
|
{
|
|
var configuration = authentication.Configuration();
|
|
foreach (var service in new[] { "PatientProfile", "Observation", "Insights", "HealthOverview" })
|
|
configuration[$"Services:{service}:BaseUrl"] = "https://service.example.test/";
|
|
using var host = new WebApplicationFactory<HealthApp.Program>().WithWebHostBuilder(builder =>
|
|
{
|
|
builder.UseEnvironment("Production");
|
|
builder.ConfigureAppConfiguration((_, config) => config.AddInMemoryCollection(configuration));
|
|
});
|
|
using var http = host.CreateClient();
|
|
using var guidance = await http.GetAsync("/api/measurement-guidance");
|
|
Assert.Equal(HttpStatusCode.BadRequest, guidance.StatusCode);
|
|
using var login = await http.PostAsync("/api/authentication/login", new StringContent("{}", Encoding.UTF8, "application/json"));
|
|
Assert.Equal(HttpStatusCode.BadRequest, login.StatusCode);
|
|
using var https = host.CreateClient(new WebApplicationFactoryClientOptions { BaseAddress = new Uri("https://localhost") });
|
|
using var available = await https.GetAsync("/api/measurement-guidance");
|
|
Assert.Equal(HttpStatusCode.OK, available.StatusCode);
|
|
}
|
|
finally { if (Directory.Exists(authentication.KeyDirectory)) Directory.Delete(authentication.KeyDirectory, true); }
|
|
}
|
|
|
|
[Theory]
|
|
[InlineData("Patient", "test-0")]
|
|
[InlineData("HealthcareProfessional", "professional")]
|
|
public async Task Standard_replacement_handler_with_same_identity_retains_ownership_policy(string role,string accountId)
|
|
{
|
|
var fixture=new PatientApiFixture();await fixture.InitializeAsync();
|
|
try
|
|
{
|
|
Guid patient=Guid.NewGuid();
|
|
await using(var db=fixture.CreateDbContext())
|
|
{
|
|
var time=new DateTime(2026,9,12);
|
|
db.Patients.Add(new HealthApp.Profile.Domain.Patient{PatientId=patient,AccountId="test-0",Name="Replacement",DateOfBirth=new DateOnly(1980,1,1),CreatedAt=time,UpdatedAt=time});await db.SaveChangesAsync();
|
|
}
|
|
fixture.AdditionalGatewayConfiguration["TestIdentity:Role"]=role;fixture.AdditionalGatewayConfiguration["TestIdentity:AccountId"]=accountId;
|
|
fixture.AdditionalGatewayConfiguration[$"Access:ProfessionalAssignments:{accountId}:0"]=patient.ToString();
|
|
fixture.ConfigureServices=services=>{
|
|
services.AddAuthentication().AddScheme<AuthenticationSchemeOptions,ReplacementHandler>("Replacement",null);
|
|
services.PostConfigure<AuthenticationOptions>(options=>{options.DefaultAuthenticateScheme="Replacement";options.DefaultChallengeScheme="Replacement";options.DefaultScheme="Replacement";});
|
|
};
|
|
await fixture.RestartHostsAsync();
|
|
using var own=await fixture.RawClient.GetAsync($"/api/patients/{patient}");Assert.Equal(HttpStatusCode.OK,own.StatusCode);
|
|
using var other=await fixture.RawClient.GetAsync($"/api/patients/{Guid.NewGuid()}");Assert.Equal(HttpStatusCode.NotFound,other.StatusCode);Assert.Empty(await other.Content.ReadAsStringAsync());
|
|
}
|
|
finally{await fixture.DisposeAsync();}
|
|
}
|
|
|
|
public sealed class ReplacementHandler(IOptionsMonitor<AuthenticationSchemeOptions> options, ILoggerFactory logger, UrlEncoder encoder, IConfiguration configuration)
|
|
: AuthenticationHandler<AuthenticationSchemeOptions>(options, logger, encoder)
|
|
{
|
|
protected override Task<AuthenticateResult> HandleAuthenticateAsync() => Task.FromResult(
|
|
AuthenticateResult.Success(new AuthenticationTicket(new ClaimsPrincipal(new ClaimsIdentity(new[]
|
|
{
|
|
new Claim(ClaimTypes.NameIdentifier, configuration["TestIdentity:AccountId"]!), new Claim(ClaimTypes.Role, configuration["TestIdentity:Role"]!)
|
|
}, Scheme.Name)), Scheme.Name)));
|
|
}
|
|
|
|
}
|