perf: remove backoffice catalog query amplification
This commit is contained in:
@@ -15,7 +15,9 @@ public sealed class BackofficeUiBootstrapTests
|
||||
[Fact]
|
||||
public async Task TenantUiBootstrap_ReturnsOnlyMenusAllowedByEffectivePermissions()
|
||||
{
|
||||
await using var factory = new ApiTestFactory();
|
||||
var commandRecorder = new RecordingDbCommandInterceptor();
|
||||
await using var factory = new ApiTestFactory(dbCommandInterceptor: commandRecorder);
|
||||
await factory.SeedBuiltinBackofficeCatalogAsync();
|
||||
var tenantId = Guid.NewGuid();
|
||||
var userId = Guid.NewGuid();
|
||||
var roleId = Guid.NewGuid();
|
||||
@@ -42,14 +44,6 @@ public sealed class BackofficeUiBootstrapTests
|
||||
Role = TenantRole.Student,
|
||||
Status = MembershipStatus.Active
|
||||
},
|
||||
new BackendPermission
|
||||
{
|
||||
Code = BackendPermissions.TenantDashboardView,
|
||||
Name = "Tenant dashboard",
|
||||
Area = BackendPermissionArea.Tenant,
|
||||
PermissionModuleCode = "tenant_dashboard",
|
||||
IsSystem = true
|
||||
},
|
||||
new TenantBackendRole
|
||||
{
|
||||
Id = roleId,
|
||||
@@ -75,17 +69,100 @@ public sealed class BackofficeUiBootstrapTests
|
||||
using var client = factory.CreateClient();
|
||||
client.UseAccessToken(await client.LoginAsTenantAsync(tenantId, phone));
|
||||
|
||||
commandRecorder.Reset();
|
||||
using var response = await client.GetAsync("/api/backoffice/tenant/ui-bootstrap");
|
||||
using var bootstrap = JsonDocument.Parse(await response.Content.ReadAsStringAsync());
|
||||
var firstRequestCommands = commandRecorder.Snapshot();
|
||||
commandRecorder.Reset();
|
||||
using var repeatedResponse = await client.GetAsync("/api/backoffice/tenant/ui-bootstrap");
|
||||
var repeatedRequestCommands = commandRecorder.Snapshot();
|
||||
using var roleManagementResponse = await client.GetAsync("/api/backoffice/tenant/bootstrap");
|
||||
|
||||
Assert.Equal(HttpStatusCode.OK, response.StatusCode);
|
||||
Assert.Equal(HttpStatusCode.OK, repeatedResponse.StatusCode);
|
||||
Assert.Equal(
|
||||
[BackendPermissions.TenantDashboardView],
|
||||
bootstrap.RootElement.GetProperty("permissionCodes").EnumerateArray().Select(item => item.GetString()));
|
||||
Assert.Equal(
|
||||
["tenant.dashboard"],
|
||||
bootstrap.RootElement.GetProperty("menus").EnumerateArray().Select(item => item.GetProperty("code").GetString()));
|
||||
Assert.True(
|
||||
firstRequestCommands.Count is >= 1 and <= 10,
|
||||
$"Expected at most 10 SQL commands, but captured {firstRequestCommands.Count}:{Environment.NewLine}{string.Join($"{Environment.NewLine}---{Environment.NewLine}", firstRequestCommands)}");
|
||||
Assert.InRange(repeatedRequestCommands.Count, 1, firstRequestCommands.Count);
|
||||
AssertNoPerCodeCatalogExistenceQueries(firstRequestCommands);
|
||||
AssertNoPerCodeCatalogExistenceQueries(repeatedRequestCommands);
|
||||
Assert.Equal(HttpStatusCode.Forbidden, roleManagementResponse.StatusCode);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public async Task PlatformUiBootstrap_DoesNotProbeCatalogPerCode()
|
||||
{
|
||||
var commandRecorder = new RecordingDbCommandInterceptor();
|
||||
await using var factory = new ApiTestFactory(
|
||||
configurationOverrides: new Dictionary<string, string?>
|
||||
{
|
||||
["Tenancy:Resolution:PlatformHosts:0"] = "localhost"
|
||||
},
|
||||
dbCommandInterceptor: commandRecorder);
|
||||
await factory.SeedBuiltinBackofficeCatalogAsync();
|
||||
var userId = Guid.NewGuid();
|
||||
var roleId = Guid.NewGuid();
|
||||
var email = $"platform-{Guid.NewGuid():N}@example.test";
|
||||
await factory.SeedAsync(
|
||||
new User
|
||||
{
|
||||
Id = userId,
|
||||
Email = email,
|
||||
NormalizedEmail = email.ToUpperInvariant(),
|
||||
UserName = email,
|
||||
NormalizedUserName = email.ToUpperInvariant(),
|
||||
Name = "Platform UI Operator",
|
||||
PrimaryRole = "platform_admin",
|
||||
RawProfile = JsonDefaults.Object()
|
||||
}.WithTestPassword(),
|
||||
new PlatformBackendRole
|
||||
{
|
||||
Id = roleId,
|
||||
Code = $"platform_ui_{Guid.NewGuid():N}",
|
||||
Name = "Platform UI Operator",
|
||||
Status = BackendRoleStatus.Active
|
||||
},
|
||||
new PlatformBackendRolePermission
|
||||
{
|
||||
RoleId = roleId,
|
||||
PermissionCode = BackendPermissions.PlatformDashboardView
|
||||
},
|
||||
new PlatformBackendUserRole
|
||||
{
|
||||
UserId = userId,
|
||||
RoleId = roleId
|
||||
});
|
||||
|
||||
using var client = factory.CreateClient();
|
||||
client.UseAccessToken(await client.LoginAsPlatformAsync(email));
|
||||
|
||||
commandRecorder.Reset();
|
||||
using var response = await client.GetAsync("/api/backoffice/platform/ui-bootstrap");
|
||||
using var bootstrap = JsonDocument.Parse(await response.Content.ReadAsStringAsync());
|
||||
var commands = commandRecorder.Snapshot();
|
||||
|
||||
Assert.Equal(HttpStatusCode.OK, response.StatusCode);
|
||||
Assert.Equal(
|
||||
[BackendPermissions.PlatformDashboardView],
|
||||
bootstrap.RootElement.GetProperty("permissionCodes").EnumerateArray().Select(item => item.GetString()));
|
||||
Assert.Equal(
|
||||
["platform.dashboard"],
|
||||
bootstrap.RootElement.GetProperty("menus").EnumerateArray().Select(item => item.GetProperty("code").GetString()));
|
||||
Assert.InRange(commands.Count, 1, 10);
|
||||
AssertNoPerCodeCatalogExistenceQueries(commands);
|
||||
}
|
||||
|
||||
private static void AssertNoPerCodeCatalogExistenceQueries(IEnumerable<string> commands)
|
||||
{
|
||||
string[] catalogTables = ["saas_features", "permission_modules", "backend_permissions", "backend_menus"];
|
||||
Assert.DoesNotContain(commands, command =>
|
||||
command.TrimStart().StartsWith("SELECT EXISTS", StringComparison.OrdinalIgnoreCase) &&
|
||||
catalogTables.Any(table => command.Contains(table, StringComparison.OrdinalIgnoreCase)));
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user