Establish single host with preserved login and public guidance
This commit is contained in:
1 parent
cee98e7035
commit
523cfbf42e
20 files changed
+1784
No files matched your search
@@ -0,0 +1,136 @@
|
||||
using System.Net;
|
||||
using HealthApp.TestSupport;
|
||||
using Microsoft.AspNetCore.Hosting;
|
||||
using System.Text.Json;
|
||||
using Microsoft.AspNetCore.Mvc.Testing;
|
||||
using Microsoft.Extensions.Configuration;
|
||||
using Microsoft.Extensions.DependencyInjection;
|
||||
using Xunit;
|
||||
|
||||
namespace PatientProfile.AcceptanceTests;
|
||||
|
||||
public sealed class MeasurementGuidanceTests : IDisposable
|
||||
{
|
||||
private readonly WebApplicationFactory<HealthApp.Program> host;
|
||||
private readonly HttpClient client;
|
||||
private int upstreamCalls;
|
||||
private readonly AuthenticatedTestClient authentication = new();
|
||||
|
||||
public MeasurementGuidanceTests()
|
||||
{
|
||||
host = new WebApplicationFactory<HealthApp.Program>().WithWebHostBuilder(builder =>
|
||||
{
|
||||
builder.UseEnvironment("Development");
|
||||
builder.ConfigureAppConfiguration((_, config) => config.AddInMemoryCollection(authentication.Configuration()).AddInMemoryCollection(
|
||||
new Dictionary<string, string?>
|
||||
{
|
||||
["Services:PatientProfile:BaseUrl"] = "http://127.0.0.1:1/",
|
||||
["Services:Observation:BaseUrl"] = "http://127.0.0.1:1/",
|
||||
["Services:Insights:BaseUrl"] = "http://127.0.0.1:1/"
|
||||
}));
|
||||
builder.ConfigureServices(services =>
|
||||
{
|
||||
foreach (var name in new[] { "PatientProfile", "Observation", "Insights" })
|
||||
services.AddHttpClient(name).ConfigurePrimaryHttpMessageHandler(
|
||||
() => new RejectUpstreamHandler(() => Interlocked.Increment(ref upstreamCalls)));
|
||||
});
|
||||
});
|
||||
client = host.CreateClient();
|
||||
}
|
||||
|
||||
[Theory]
|
||||
[InlineData("")]
|
||||
[InlineData("?setting=Unknown")]
|
||||
[InlineData("?setting=Home")]
|
||||
public async Task Public_guidance_without_patient_records_is_labelled_and_has_ordered_sourced_instructions(string query)
|
||||
{
|
||||
using var response = await client.GetAsync("/api/measurement-guidance" + query);
|
||||
Assert.Equal(HttpStatusCode.OK, response.StatusCode);
|
||||
using var json = JsonDocument.Parse(await response.Content.ReadAsStringAsync());
|
||||
var result = json.RootElement;
|
||||
Assert.True(result.GetProperty("available").GetBoolean());
|
||||
Assert.Equal("Home", result.GetProperty("applicableSetting").GetString());
|
||||
Assert.Equal("AtRest", result.GetProperty("applicableActivityCondition").GetString());
|
||||
Assert.True(DateOnly.TryParse(result.GetProperty("reviewedOn").GetString(), out _));
|
||||
var steps = result.GetProperty("steps").EnumerateArray().ToArray();
|
||||
Assert.Equal(Enumerable.Range(1, 6), steps.Select(s => s.GetProperty("order").GetInt32()));
|
||||
var instructions = string.Join(" ", steps.Select(s => s.GetProperty("instruction").GetString()));
|
||||
foreach (var text in new[] { "validated upper-arm", "cuff", "five minutes", "back and arm supported",
|
||||
"still", "three readings", "three consecutive days", "one observation", "actual", "measurement time" })
|
||||
Assert.Contains(text, instructions);
|
||||
var sources = result.GetProperty("sources").EnumerateArray().ToArray();
|
||||
Assert.Equal(2, sources.Length);
|
||||
Assert.All(sources, source =>
|
||||
{
|
||||
Assert.StartsWith("https://www.sundhed.dk/", source.GetProperty("url").GetString());
|
||||
Assert.True(DateOnly.TryParse(source.GetProperty("updatedOn").GetString(), out _));
|
||||
});
|
||||
Assert.Contains("not a clinical measurement protocol", result.GetProperty("explanation").GetString());
|
||||
Assert.Contains("does not assess rhythm regularity", result.GetProperty("pulseExplanation").GetString());
|
||||
Assert.Contains("does not save or alter", result.GetProperty("limitations").GetString());
|
||||
Assert.Contains("prevent a valid submission", result.GetProperty("limitations").GetString());
|
||||
Assert.DoesNotContain("patientId", result.GetRawText());
|
||||
Assert.DoesNotContain("assignments", result.GetRawText());
|
||||
Assert.Equal(0, upstreamCalls);
|
||||
}
|
||||
|
||||
[Theory]
|
||||
[InlineData("DuringActivity")]
|
||||
[InlineData("AfterActivity")]
|
||||
public async Task Activity_is_not_relabelled_as_rest_or_certified(string activity)
|
||||
{
|
||||
using var response = await client.GetAsync($"/api/measurement-guidance?setting=Home&activityCondition={activity}");
|
||||
Assert.Equal(HttpStatusCode.OK, response.StatusCode);
|
||||
using var json = JsonDocument.Parse(await response.Content.ReadAsStringAsync());
|
||||
var result = json.RootElement;
|
||||
Assert.Equal(activity, result.GetProperty("requestedActivityCondition").GetString());
|
||||
Assert.Contains("do not relabel", result.GetProperty("activityExplanation").GetString());
|
||||
Assert.Contains("remain valid to record", result.GetProperty("activityExplanation").GetString());
|
||||
Assert.Contains("does not record a setting or certify compliance", result.GetProperty("explanation").GetString());
|
||||
Assert.Equal(0, upstreamCalls);
|
||||
}
|
||||
|
||||
[Theory]
|
||||
[InlineData("Clinic")]
|
||||
[InlineData("Other")]
|
||||
public async Task Unsupported_guidance_is_explicitly_unavailable_without_unverified_instructions(string setting)
|
||||
{
|
||||
using var response = await client.GetAsync($"/api/measurement-guidance?setting={setting}");
|
||||
Assert.Equal(HttpStatusCode.OK, response.StatusCode);
|
||||
using var json = JsonDocument.Parse(await response.Content.ReadAsStringAsync());
|
||||
var result = json.RootElement;
|
||||
Assert.False(result.GetProperty("available").GetBoolean());
|
||||
Assert.Contains("unavailable", result.GetProperty("explanation").GetString());
|
||||
Assert.Empty(result.GetProperty("steps").EnumerateArray());
|
||||
Assert.Empty(result.GetProperty("sources").EnumerateArray());
|
||||
Assert.Equal(JsonValueKind.Null, result.GetProperty("applicableSetting").ValueKind);
|
||||
Assert.Equal(0, upstreamCalls);
|
||||
}
|
||||
|
||||
[Theory]
|
||||
[InlineData("setting=Unsupported")]
|
||||
[InlineData("activityCondition=Unsupported")]
|
||||
public async Task Invalid_categories_return_explained_rejections(string query)
|
||||
{
|
||||
using var response = await client.GetAsync("/api/measurement-guidance?" + query);
|
||||
Assert.Equal(HttpStatusCode.BadRequest, response.StatusCode);
|
||||
Assert.Contains("Unsupported", await response.Content.ReadAsStringAsync());
|
||||
Assert.Equal(0, upstreamCalls);
|
||||
}
|
||||
|
||||
public void Dispose()
|
||||
{
|
||||
client.Dispose();
|
||||
host.Dispose();
|
||||
if (Directory.Exists(authentication.KeyDirectory)) Directory.Delete(authentication.KeyDirectory, true);
|
||||
}
|
||||
|
||||
private sealed class RejectUpstreamHandler(Action called) : HttpMessageHandler
|
||||
{
|
||||
protected override Task<HttpResponseMessage> SendAsync(HttpRequestMessage request, CancellationToken cancellationToken)
|
||||
{
|
||||
called();
|
||||
throw new InvalidOperationException("Guidance must not read or write patient data.");
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user