Files
HealthAppRepositoryB/tests/HealthApp.AcceptanceTests/FoundationTests.cs
T

32 lines
2.1 KiB
C#

using System.Net;
using System.Net.Http.Json;
using System.Text.Json;
using HealthApp.TestSupport;
using Microsoft.AspNetCore.Mvc.Testing;
using Microsoft.AspNetCore.Hosting;
using Microsoft.Extensions.Configuration;
using Xunit;
namespace HealthApp.AcceptanceTests;
public sealed class FoundationTests
{
[Fact]
public async Task Framework_login_retains_identity_generic_failures_and_no_service_key_bypass()
{
var auth = new AuthenticatedTestClient();
using var host = new WebApplicationFactory<Program>().WithWebHostBuilder(b => b.UseEnvironment("Development").ConfigureAppConfiguration((_, c) => c.AddInMemoryCollection(auth.Configuration())));
using var client = host.CreateClient();
using var login = await client.PostAsJsonAsync("/api/authentication/login", new { accountId = "professional", password = AuthenticatedTestClient.Password });
Assert.Equal(HttpStatusCode.OK, login.StatusCode); Assert.Equal("no-store", login.Headers.CacheControl!.ToString());
using var json = JsonDocument.Parse(await login.Content.ReadAsStringAsync());
Assert.Equal("HealthcareProfessional", json.RootElement.GetProperty("role").GetString());
Assert.Equal(1800, json.RootElement.GetProperty("expiresIn").GetInt32());
Assert.DoesNotContain("patientId", json.RootElement.GetRawText());
using var badAccount = await client.PostAsJsonAsync("/api/authentication/login", new { accountId = "missing", password = AuthenticatedTestClient.Password });
using var badPassword = await client.PostAsJsonAsync("/api/authentication/login", new { accountId = "test-0", password = "wrong" });
Assert.Equal(HttpStatusCode.Unauthorized, badAccount.StatusCode); Assert.Equal(await badAccount.Content.ReadAsStringAsync(), await badPassword.Content.ReadAsStringAsync());
using var forged = new HttpRequestMessage(HttpMethod.Get, "/api/professionals/me/patients"); forged.Headers.Add("X-Service-Key", auth.OverviewServiceKey);
using var rejected = await client.SendAsync(forged); Assert.Equal(HttpStatusCode.Unauthorized, rejected.StatusCode);
}
}