shoc-backend/Api.SeaHavenIndustries.Tests/AccountOwnerDataServiceTests.cs

61 lines
2.4 KiB
C#
Raw Normal View History

using Data.SeaHavenIndustries;
using FluentAssertions;
using Microsoft.EntityFrameworkCore;
using SeaHaven.DataServices.Implementation;
using Xunit;
namespace Api.SeaHavenIndustries.Tests;
public class AccountOwnerDataServiceTests
{
private static ApplicationDbContext NewContext()
{
var options = new DbContextOptionsBuilder<ApplicationDbContext>()
.UseInMemoryDatabase(Guid.NewGuid().ToString())
.Options;
return new ApplicationDbContext(options);
}
[Fact]
public async Task EnsureConfiguredOwnerAsync_MarksOnlyConfiguredUser()
{
await using var context = NewContext();
context.Users.AddRange(
new ApplicationUser { Id = "old-owner", UserName = "old@example.com", IsAccountOwner = true },
new ApplicationUser { Id = "configured-owner", UserName = "configured@example.com" });
await context.SaveChangesAsync();
var service = new AccountOwnerDataService(context);
(await service.EnsureConfiguredOwnerAsync(" configured-owner ", CancellationToken.None)).Should().BeTrue();
(await context.Users.SingleAsync(user => user.Id == "old-owner")).IsAccountOwner.Should().BeFalse();
(await context.Users.SingleAsync(user => user.Id == "configured-owner")).IsAccountOwner.Should().BeTrue();
}
[Fact]
public async Task EnsureConfiguredOwnerAsync_WithNoConfiguration_ClearsExistingOwner()
{
await using var context = NewContext();
context.Users.Add(new ApplicationUser { Id = "owner", UserName = "owner@example.com", IsAccountOwner = true });
await context.SaveChangesAsync();
var service = new AccountOwnerDataService(context);
(await service.EnsureConfiguredOwnerAsync(null, CancellationToken.None)).Should().BeTrue();
(await service.EnsureConfiguredOwnerAsync(" ", CancellationToken.None)).Should().BeFalse();
(await context.Users.SingleAsync()).IsAccountOwner.Should().BeFalse();
}
[Fact]
public async Task EnsureConfiguredOwnerAsync_RejectsUnknownUser()
{
await using var context = NewContext();
var service = new AccountOwnerDataService(context);
var action = () => service.EnsureConfiguredOwnerAsync("missing", CancellationToken.None);
await action.Should().ThrowAsync<InvalidOperationException>()
.WithMessage("The configured account owner user was not found.");
}
}