code reorg
This commit is contained in:
parent
93426986f6
commit
d8c0678462
11 changed files with 154 additions and 22 deletions
|
|
@ -4,8 +4,8 @@
|
|||
|
||||
<!-- Yavsc.Org.Tests-specific versions -->
|
||||
<ItemGroup>
|
||||
<PackageVersion Include="Microsoft.Extensions.Caching.Memory" Version="10.0.9" />
|
||||
<PackageVersion Include="Microsoft.Extensions.Options" Version="10.0.9" />
|
||||
<PackageVersion Include="Microsoft.Extensions.Options.ConfigurationExtensions" Version="10.0.9" />
|
||||
<PackageVersion Include="Microsoft.Extensions.Caching.Memory" Version="10.0.11" />
|
||||
<PackageVersion Include="Microsoft.Extensions.Options" Version="10.0.11" />
|
||||
<PackageVersion Include="Microsoft.Extensions.Options.ConfigurationExtensions" Version="10.0.11" />
|
||||
</ItemGroup>
|
||||
</Project>
|
||||
|
|
|
|||
129
src/Yavsc.Org.Tests/Services/FileSystemAuthManagerTests.cs
Normal file
129
src/Yavsc.Org.Tests/Services/FileSystemAuthManagerTests.cs
Normal file
|
|
@ -0,0 +1,129 @@
|
|||
using Microsoft.Data.Sqlite;
|
||||
using Microsoft.EntityFrameworkCore;
|
||||
using Microsoft.Extensions.Options;
|
||||
using Yavsc.Models;
|
||||
using Yavsc.Models.Access;
|
||||
using Yavsc.Models.Relationship;
|
||||
using Yavsc.Services;
|
||||
|
||||
namespace Yavsc.Org.Tests.Services;
|
||||
|
||||
public class FileSystemAuthManagerTests
|
||||
{
|
||||
[Fact]
|
||||
public void SetAccess_creates_acl_row_with_owner_path_and_flags()
|
||||
{
|
||||
using var scope = CreateScope();
|
||||
scope.Service.SetAccess(scope.Circle.Id, "alice/documents/report.txt", FileAccessRight.Read | FileAccessRight.Write);
|
||||
|
||||
var row = scope.Db.CircleAuthorizationToFile.Single();
|
||||
|
||||
Assert.Equal(scope.Circle.Id, row.CircleId);
|
||||
Assert.Equal("alice/documents/report.txt", row.Path);
|
||||
Assert.Equal("alice", row.OwnerId);
|
||||
Assert.Equal(FileAccessRight.Read | FileAccessRight.Write, row.Access);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void SetAccess_updates_existing_acl_row_without_duplicates()
|
||||
{
|
||||
using var scope = CreateScope();
|
||||
|
||||
scope.Service.SetAccess(scope.Circle.Id, "alice/documents/report.txt", FileAccessRight.Read);
|
||||
scope.Service.SetAccess(scope.Circle.Id, "alice/documents/report.txt", FileAccessRight.Write);
|
||||
|
||||
var rows = scope.Db.CircleAuthorizationToFile.ToList();
|
||||
|
||||
Assert.Single(rows);
|
||||
Assert.Equal(FileAccessRight.Write, rows[0].Access);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void SetAccess_none_removes_existing_acl_row()
|
||||
{
|
||||
using var scope = CreateScope();
|
||||
|
||||
scope.Service.SetAccess(scope.Circle.Id, "alice/documents/report.txt", FileAccessRight.Read);
|
||||
scope.Service.SetAccess(scope.Circle.Id, "alice/documents/report.txt", FileAccessRight.None);
|
||||
|
||||
Assert.Empty(scope.Db.CircleAuthorizationToFile);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void SetAccess_ignores_unknown_owner_prefix()
|
||||
{
|
||||
using var scope = CreateScope();
|
||||
|
||||
scope.Service.SetAccess(scope.Circle.Id, "unknown/documents/report.txt", FileAccessRight.Read);
|
||||
|
||||
Assert.Empty(scope.Db.CircleAuthorizationToFile);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void Deleting_circle_cascades_file_acl_rows()
|
||||
{
|
||||
using var scope = CreateScope();
|
||||
|
||||
scope.Service.SetAccess(scope.Circle.Id, "alice/documents/report.txt", FileAccessRight.Read);
|
||||
scope.Db.Circle.Remove(scope.Circle);
|
||||
scope.Db.SaveChanges();
|
||||
|
||||
Assert.Empty(scope.Db.CircleAuthorizationToFile);
|
||||
}
|
||||
|
||||
private static TestScope CreateScope()
|
||||
{
|
||||
var connection = new SqliteConnection("Data Source=:memory:");
|
||||
connection.Open();
|
||||
|
||||
var options = new DbContextOptionsBuilder<ApplicationDbContext>()
|
||||
.UseSqlite(connection)
|
||||
.Options;
|
||||
|
||||
var db = new ApplicationDbContext(options);
|
||||
db.Database.EnsureCreated();
|
||||
|
||||
db.Users.Add(new ApplicationUser
|
||||
{
|
||||
Id = "alice",
|
||||
UserName = "alice",
|
||||
Email = "alice@example.test"
|
||||
});
|
||||
db.SaveChanges();
|
||||
|
||||
var circle = new Circle
|
||||
{
|
||||
OwnerId = "alice",
|
||||
Name = "shared",
|
||||
Public = false
|
||||
};
|
||||
|
||||
db.Circle.Add(circle);
|
||||
db.SaveChanges();
|
||||
|
||||
var service = new FileSystemAuthManager(db, Options.Create(new SiteSettings()));
|
||||
return new TestScope(connection, db, service, circle);
|
||||
}
|
||||
|
||||
private sealed class TestScope : IDisposable
|
||||
{
|
||||
public TestScope(SqliteConnection connection, ApplicationDbContext db, FileSystemAuthManager service, Circle circle)
|
||||
{
|
||||
Connection = connection;
|
||||
Db = db;
|
||||
Service = service;
|
||||
Circle = circle;
|
||||
}
|
||||
|
||||
public SqliteConnection Connection { get; }
|
||||
public ApplicationDbContext Db { get; }
|
||||
public FileSystemAuthManager Service { get; }
|
||||
public Circle Circle { get; }
|
||||
|
||||
public void Dispose()
|
||||
{
|
||||
Db.Dispose();
|
||||
Connection.Dispose();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
@ -53,6 +53,7 @@
|
|||
</ItemGroup>
|
||||
<ItemGroup>
|
||||
<PackageReference Include="Microsoft.EntityFrameworkCore.InMemory" />
|
||||
<PackageReference Include="Microsoft.EntityFrameworkCore.Sqlite" />
|
||||
</ItemGroup>
|
||||
<!--
|
||||
MapStaticAssets() in the production pipeline resolves
|
||||
|
|
@ -86,4 +87,4 @@
|
|||
</ItemGroup>
|
||||
<Copy SourceFiles="@(_YavscOrgStaticAssetsFiles)" DestinationFolder="$(OutDir)" />
|
||||
</Target>
|
||||
</Project>
|
||||
</Project>
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue