forked from gongxuegit/tiku-backend.net
53 lines
2.0 KiB
C#
53 lines
2.0 KiB
C#
namespace Tiku.IntegrationTests;
|
|
|
|
public sealed class ArchitectureBoundaryTests
|
|
{
|
|
[Fact]
|
|
public void Business_code_does_not_bypass_tenant_query_boundaries()
|
|
{
|
|
var root = FindRepositoryRoot();
|
|
var sourceRoots = new[] { "Tiku.Api", "Tiku.Application", "Tiku.Infrastructure" };
|
|
var forbidden = new[]
|
|
{
|
|
"IgnoreQueryFilters(",
|
|
"FromSql(",
|
|
"FromSqlRaw(",
|
|
"FromSqlInterpolated(",
|
|
"ExecuteSql(",
|
|
"ExecuteSqlRaw(",
|
|
"ExecuteSqlInterpolated(",
|
|
"new NpgsqlCommand("
|
|
};
|
|
|
|
var violations = sourceRoots
|
|
.SelectMany(directory => Directory.EnumerateFiles(
|
|
Path.Combine(root, directory),
|
|
"*.cs",
|
|
SearchOption.AllDirectories))
|
|
.Where(path => !path.Contains(
|
|
$"{Path.DirectorySeparatorChar}Persistence{Path.DirectorySeparatorChar}Migrations{Path.DirectorySeparatorChar}",
|
|
StringComparison.Ordinal))
|
|
.SelectMany(path => File.ReadLines(path)
|
|
.Select((line, index) => new { path, line, lineNumber = index + 1 }))
|
|
.Where(candidate => forbidden.Any(symbol =>
|
|
candidate.line.Contains(symbol, StringComparison.Ordinal)))
|
|
.Select(candidate => $"{Path.GetRelativePath(root, candidate.path)}:{candidate.lineNumber}")
|
|
.ToArray();
|
|
|
|
Assert.True(
|
|
violations.Length == 0,
|
|
$"Forbidden tenant-boundary bypasses were found:{Environment.NewLine}{string.Join(Environment.NewLine, violations)}");
|
|
}
|
|
|
|
private static string FindRepositoryRoot()
|
|
{
|
|
var directory = new DirectoryInfo(AppContext.BaseDirectory);
|
|
while (directory is not null && !File.Exists(Path.Combine(directory.FullName, "TIKU-BACKEND.slnx")))
|
|
{
|
|
directory = directory.Parent;
|
|
}
|
|
|
|
return directory?.FullName ?? throw new DirectoryNotFoundException("Repository root was not found.");
|
|
}
|
|
}
|