shoc-backend/SeaHavenIndustries.Tests/WorkOrderAccountTestHelpers.cs
Arthur Bassi 61923b2a7d feat(work-orders): stamp board create account from location
Org-wide create no longer depends on customer name. POST /workorders/board requires locationId and stamps WorkOrder.AccountId from Location.AccountId.
2026-08-26 09:12:48 -03:00

119 lines
3.6 KiB
C#

using System.Security.Claims;
using Data.SeaHavenIndustries;
using Microsoft.EntityFrameworkCore;
using SeaHaven.DataServices.Implementation;
using SeaHaven.Services.Helpers;
using SeaHaven.Services.Implementation;
using SeaHaven.Services.Interfaces;
namespace SeaHavenIndustries.Tests;
internal static class WorkOrderAccountTestHelpers
{
public static IWorkOrderAccountResolver Resolver(ApplicationDbContext context)
=> new WorkOrderAccountResolver(
new AccountDataService(context),
new LocationDataService(context));
public static ClaimsPrincipal AccountUser(
string userId = "actor-1",
int accountId = 1,
params string[] roles)
{
var effectiveRoles = roles.Length == 0 ? new[] { "Admin" } : roles;
var claims = new List<Claim>
{
new(ClaimTypes.NameIdentifier, userId),
new(SeaHavenClaimTypes.AccountId, accountId.ToString())
};
claims.AddRange(effectiveRoles.Select(r => new Claim(ClaimTypes.Role, r)));
return new ClaimsPrincipal(new ClaimsIdentity(claims, authenticationType: "test"));
}
public static ClaimsPrincipal OrgWideAdmin(string userId = "actor-1")
{
var claims = new List<Claim>
{
new(ClaimTypes.NameIdentifier, userId),
new(ClaimTypes.Role, "Admin"),
new(SeaHavenClaimTypes.OrgScope, SeaHavenClaimTypes.OrgScopeAll)
};
return new ClaimsPrincipal(new ClaimsIdentity(claims, authenticationType: "test"));
}
public static ClaimsPrincipal MissingScope(string userId = "actor-1", string role = "Dispatcher")
{
var claims = new List<Claim>
{
new(ClaimTypes.NameIdentifier, userId),
new(ClaimTypes.Role, role)
};
return new ClaimsPrincipal(new ClaimsIdentity(claims, authenticationType: "test"));
}
public static async Task EnsureAccountAsync(
ApplicationDbContext context,
int id = 1,
string name = "Acme Corp")
{
if (await context.Accounts.AnyAsync(a => a.Id == id))
return;
context.Accounts.Add(new Accounts
{
Id = id,
Name = name,
IsDeleted = false
});
await context.SaveChangesAsync();
}
public static async Task EnsureLocationAsync(
ApplicationDbContext context,
int id = 1,
int? accountId = 1,
string name = "BK5")
{
if (await context.Locations.AnyAsync(l => l.Id == id))
return;
context.Locations.Add(new Locations
{
Id = id,
Name = name,
AccountId = accountId
});
await context.SaveChangesAsync();
}
public static void SeedBoardCreateScope(
ApplicationDbContext context,
int accountId = 1,
int locationId = 1,
string accountName = "Acme Corp")
{
if (!context.Accounts.Local.Any(a => a.Id == accountId)
&& !context.Accounts.Any(a => a.Id == accountId))
{
context.Accounts.Add(new Accounts
{
Id = accountId,
Name = accountName,
IsDeleted = false
});
}
if (!context.Locations.Local.Any(l => l.Id == locationId)
&& !context.Locations.Any(l => l.Id == locationId))
{
context.Locations.Add(new Locations
{
Id = locationId,
Name = "BK5",
AccountId = accountId
});
}
context.SaveChanges();
}
}