Heimdall Gitea / Detect CI scope (pull_request) Successful in 6s
Heimdall Gitea / Build and tests (pull_request) Failing after 4m5s
Heimdall Gitea / Build and scan server image (pull_request) Skipped
Heimdall Gitea / Build and scan agent image (pull_request) Skipped
Heimdall Gitea / Publish Docker Hub latest tags (pull_request) Skipped
Heimdall Gitea / Publish versioned release (pull_request) Skipped
Heimdall Gitea / CI policy (pull_request) Failing after 1s
107 lines
4.7 KiB
C#
107 lines
4.7 KiB
C#
using Heimdall.Application.Common.Persistence;
|
|
using Heimdall.Application.Services;
|
|
using Heimdall.Domain.Entities;
|
|
using Heimdall.Infrastructure.Data;
|
|
using Heimdall.Infrastructure.Data.Interceptors;
|
|
using Heimdall.Infrastructure.Persistence;
|
|
using Heimdall.Infrastructure.Security;
|
|
using Microsoft.AspNetCore.Identity;
|
|
using Microsoft.Data.Sqlite;
|
|
using Microsoft.EntityFrameworkCore;
|
|
using Microsoft.Extensions.DependencyInjection;
|
|
using Shouldly;
|
|
using Heimdall.Infrastructure.Identity;
|
|
|
|
namespace Heimdall.Infrastructure.Tests;
|
|
|
|
public sealed class RolePermissionServiceTests : IAsyncLifetime, IDisposable
|
|
{
|
|
private readonly SqliteConnection _connection = new("Data Source=:memory:");
|
|
private ServiceProvider _services = null!;
|
|
|
|
public async Task InitializeAsync()
|
|
{
|
|
await _connection.OpenAsync();
|
|
var services = new ServiceCollection();
|
|
services.AddLogging();
|
|
services.AddDbContext<AppDbContext>(o => o.UseSqlite(_connection));
|
|
services.AddSingleton(new AuditInterceptor());
|
|
services.AddIdentity<IdentityAppUser, IdentityAppRole>()
|
|
.AddEntityFrameworkStores<AppDbContext>().AddDefaultTokenProviders();
|
|
services.AddScoped<IUserRepository, UserRepository>();
|
|
services.AddScoped<IRolePermissionService, RolePermissionService>();
|
|
_services = services.BuildServiceProvider();
|
|
using var scope = _services.CreateScope();
|
|
await scope.ServiceProvider.GetRequiredService<AppDbContext>().Database.EnsureCreatedAsync();
|
|
var roles = scope.ServiceProvider.GetRequiredService<RoleManager<IdentityAppRole>>();
|
|
foreach (var roleName in AppRoles.All)
|
|
(await roles.CreateAsync(new IdentityAppRole(roleName))).Succeeded.ShouldBeTrue();
|
|
}
|
|
|
|
public async Task DisposeAsync()
|
|
{
|
|
await _services.DisposeAsync();
|
|
await _connection.DisposeAsync();
|
|
}
|
|
|
|
public void Dispose()
|
|
{
|
|
_services.Dispose();
|
|
_connection.Dispose();
|
|
}
|
|
|
|
[Fact]
|
|
public async Task Default_role_permissions_are_persisted_and_loaded_from_identity_claims()
|
|
{
|
|
using var scope = _services.CreateScope();
|
|
var service = scope.ServiceProvider.GetRequiredService<IRolePermissionService>();
|
|
await service.EnsureDefaultsAsync(default);
|
|
await service.EnsureDefaultsAsync(default);
|
|
|
|
var viewer = await service.GetPermissionsForRolesAsync([AppRoles.Viewer], default);
|
|
viewer.ShouldContain(AppPermissions.SiteRead);
|
|
viewer.ShouldContain(AppPermissions.CameraRead);
|
|
viewer.ShouldNotContain(AppPermissions.CameraWrite);
|
|
viewer.Count.ShouldBe(AppPermissions.RolePermissions[AppRoles.Viewer].Length);
|
|
}
|
|
|
|
[Fact]
|
|
public async Task Updating_role_permissions_persists_and_revokes_member_sessions()
|
|
{
|
|
using var scope = _services.CreateScope();
|
|
var users = scope.ServiceProvider.GetRequiredService<UserManager<IdentityAppUser>>();
|
|
var user = new IdentityAppUser { UserName = "viewer@example.test", Email = "viewer@example.test", SecurityStamp = "old-stamp" };
|
|
(await users.CreateAsync(user, "ViewerPass123!")).Succeeded.ShouldBeTrue();
|
|
(await users.AddToRoleAsync(user, AppRoles.Viewer)).Succeeded.ShouldBeTrue();
|
|
var beforeStamp = user.SecurityStamp;
|
|
var service = scope.ServiceProvider.GetRequiredService<IRolePermissionService>();
|
|
await service.EnsureDefaultsAsync(default);
|
|
|
|
var changed = await service.SetPermissionsAsync(AppRoles.Viewer,
|
|
[AppPermissions.SiteRead, AppPermissions.CameraRead, AppPermissions.EventRead, AppPermissions.CameraWrite],
|
|
user.Id, default);
|
|
|
|
changed.IsSuccess.ShouldBeTrue();
|
|
user.SecurityStamp.ShouldNotBe(beforeStamp);
|
|
var effective = await service.GetPermissionsForRolesAsync([AppRoles.Viewer], default);
|
|
effective.ShouldContain(AppPermissions.CameraWrite);
|
|
effective.ShouldNotContain(AppPermissions.CameraLive);
|
|
}
|
|
|
|
[Fact]
|
|
public async Task Admin_permissions_cannot_be_changed_or_removed_accidentally()
|
|
{
|
|
using var scope = _services.CreateScope();
|
|
var service = scope.ServiceProvider.GetRequiredService<IRolePermissionService>();
|
|
await service.EnsureDefaultsAsync(default);
|
|
|
|
var result = await service.SetPermissionsAsync(AppRoles.Admin, [AppPermissions.SiteRead], Guid.NewGuid(), default);
|
|
|
|
result.IsSuccess.ShouldBeFalse();
|
|
var permissions = await service.GetPermissionsForRolesAsync([AppRoles.Admin], default);
|
|
permissions.ShouldContain(AppPermissions.AdminUsers);
|
|
permissions.ShouldContain(AppPermissions.AdminManage);
|
|
permissions.Count.ShouldBe(AppPermissions.RolePermissions[AppRoles.Admin].Length);
|
|
}
|
|
}
|