LANCommander/LANCommander.Server.Tests/BaseTest.cs
2026-06-28 14:09:34 -05:00

124 lines
No EOL
3.8 KiB
C#

using System.Formats.Asn1;
using LANCommander.SDK.Enums;
using LANCommander.SDK.Services;
using LANCommander.Server.Data.Models;
using LANCommander.Server.Services;
using Microsoft.EntityFrameworkCore;
using Microsoft.Extensions.DependencyInjection;
using Xunit.Extensions.TestDependency;
namespace LANCommander.Server.Tests;
[Trait("xUnit", "Ordered")]
[TestCaseOrderer(DependencyOrderer.TypeName, DependencyOrderer.AssemblyName)]
public abstract class BaseTest : IClassFixture<ApplicationFixture>, IDisposable
{
protected AuthenticationClient AuthenticationClient => ApplicationFixture.Instance.AuthenticationClient;
protected GameClient GameClient => ApplicationFixture.Instance.GameClient;
protected SaveClient SaveClient => ApplicationFixture.Instance.SaveClient;
protected TagClient TagClient => ApplicationFixture.Instance.TagClient;
protected readonly IServiceProvider ServiceProvider;
private AsyncServiceScope? _scope;
public BaseTest(ApplicationFixture fixture)
{
_scope = ApplicationFixture.Instance.ServiceProvider.CreateAsyncScope();
ServiceProvider = _scope?.ServiceProvider;
}
/// <summary>Authenticates against the in-memory server; the token is reused by the SDK clients.</summary>
protected Task AuthenticateAsync(string username, string password)
=> ApplicationFixture.Instance.AuthenticateAsync(username, password);
protected T GetService<T>() => ServiceProvider.GetService<T>();
protected async Task<User> EnsureAdminUserCreatedAsync()
{
var roleService = GetService<RoleService>();
var userService = GetService<UserService>();
var user = await userService
.Query(q =>
{
return q
.Include(u => u.UserRoles)
.ThenInclude(ur => ur.Role);
})
.FirstOrDefaultAsync(u => u.UserName == TestConstants.AdminUserName);
// Assume user already exists in correct role
if (user != null)
return user;
roleService.AddAsync(new Role
{
Name = RoleService.AdministratorRoleName,
});
user = await userService.AddAsync(new User
{
UserName = TestConstants.AdminUserName,
});
await userService.ChangePassword(user.UserName, TestConstants.AdminInitialPassword);
await userService.AddToRoleAsync(user.UserName, RoleService.AdministratorRoleName);
user = await userService
.Query(q =>
{
return q
.Include(u => u.UserRoles)
.ThenInclude(ur => ur.Role);
})
.FirstOrDefaultAsync(u => u.UserName.ToLower() == user.UserName.ToLower());
return user;
}
protected async Task<string> EnsureStorageLocationsExistAsync()
{
var storageLocationService = GetService<StorageLocationService>();
var tempPath = GetTemporaryDirectory();
await storageLocationService.AddAsync(new StorageLocation
{
Path = tempPath,
Type = StorageLocationType.Archive,
Default = true,
});
await storageLocationService.AddAsync(new StorageLocation
{
Path = tempPath,
Type = StorageLocationType.Media,
Default = true,
});
await storageLocationService.AddAsync(new StorageLocation
{
Path = tempPath,
Type = StorageLocationType.Save,
Default = true,
});
return tempPath;
}
protected string GetTemporaryDirectory()
{
var tempPath = Path.Combine(Path.GetTempPath(), Path.GetRandomFileName());
Directory.CreateDirectory(tempPath);
return tempPath;
}
public void Dispose()
{
_scope?.Dispose();
}
}