Expand coverage to full backend assembly

This commit is contained in:
2026-02-24 23:37:53 +01:00
parent c6e95f16e1
commit 885121723d
5 changed files with 78 additions and 1 deletions

View File

@@ -0,0 +1,60 @@
using System.Reflection;
using Microsoft.AspNetCore.Hosting;
using Microsoft.AspNetCore.Http;
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.FileProviders;
using RpgRoller.Hosting;
namespace RpgRoller.Tests;
public sealed class BackendCoverageTests
{
[Fact]
public void AddRpgRollerCore_WithInMemoryConnectionString_RegistersCoreServices()
{
var services = new ServiceCollection();
var configuration = new ConfigurationBuilder()
.AddInMemoryCollection(new Dictionary<string, string?>
{
["ConnectionStrings:RpgRoller"] = "Data Source=:memory:"
})
.Build();
var environment = new TestWebHostEnvironment
{
ContentRootPath = Path.GetTempPath()
};
services.AddRpgRollerCore(configuration, environment);
Assert.Contains(services, d => d.ServiceType == typeof(RpgRoller.Services.IGameService));
Assert.Contains(services, d => d.ServiceType == typeof(RpgRoller.Services.IDiceRoller));
}
[Fact]
public void GetRequiredSessionToken_ThrowsWhenTokenWasNotStored()
{
var extensionsType = typeof(Program).Assembly.GetType("RpgRoller.Api.SessionTokenHttpContextExtensions");
Assert.NotNull(extensionsType);
var method = extensionsType!.GetMethod(
"GetRequiredSessionToken",
BindingFlags.Static | BindingFlags.Public | BindingFlags.NonPublic);
Assert.NotNull(method);
var context = new DefaultHttpContext();
var exception = Assert.Throws<TargetInvocationException>(() => method!.Invoke(null, [context]));
Assert.IsType<InvalidOperationException>(exception.InnerException);
}
private sealed class TestWebHostEnvironment : IWebHostEnvironment
{
public string ApplicationName { get; set; } = "RpgRoller.Tests";
public IFileProvider WebRootFileProvider { get; set; } = new NullFileProvider();
public string WebRootPath { get; set; } = string.Empty;
public string EnvironmentName { get; set; } = "Development";
public string ContentRootPath { get; set; } = string.Empty;
public IFileProvider ContentRootFileProvider { get; set; } = new NullFileProvider();
}
}

View File

@@ -71,6 +71,13 @@ public sealed class UnitTest1 : IClassFixture<WebApplicationFactory<Program>>
new CreateSkillRequest("Arcana", "2d12+2"));
Assert.Equal("2d12+2", createdSkill.DiceRollDefinition);
var updatedSkill = await PutAsync<UpdateSkillRequest, SkillSummary>(
gmClient,
$"/api/skills/{createdSkill.Id}",
new UpdateSkillRequest("Arcana Mastery", "2d12+3"));
Assert.Equal("Arcana Mastery", updatedSkill.Name);
Assert.Equal("2d12+3", updatedSkill.DiceRollDefinition);
var invalidSkill = await gmClient.PostAsJsonAsync(
$"/api/characters/{gmCharacter.Id}/skills",
new CreateSkillRequest("Broken", "5D+4"));
@@ -172,6 +179,11 @@ public sealed class UnitTest1 : IClassFixture<WebApplicationFactory<Program>>
"/api/campaigns",
new CreateCampaignRequest("Nope", "d6"));
Assert.Equal(HttpStatusCode.Unauthorized, unauthorizedCampaignCreate.StatusCode);
var invalidSessionRequest = new HttpRequestMessage(HttpMethod.Get, "/api/campaigns");
invalidSessionRequest.Headers.Add("Cookie", "rpgroller_session=invalid-token");
var unauthorizedWithInvalidSession = await anonymousClient.SendAsync(invalidSessionRequest);
Assert.Equal(HttpStatusCode.Unauthorized, unauthorizedWithInvalidSession.StatusCode);
}
[Fact]

View File

@@ -5,7 +5,6 @@
<DataCollector friendlyName="XPlat code coverage">
<Configuration>
<Format>cobertura</Format>
<Include>[RpgRoller]RpgRoller.Services.*</Include>
<ExcludeByAttribute>GeneratedCodeAttribute,CompilerGeneratedAttribute,ExcludeFromCodeCoverageAttribute</ExcludeByAttribute>
</Configuration>
</DataCollector>