Files
Heimdall/tests/Heimdall.Web.Tests/Security/AccessFixture.cs
T

80 lines
3.4 KiB
C#

using System.Security.Claims;
using Heimdall.Api.Security;
using Heimdall.Domain.Entities;
using Heimdall.Infrastructure.Data;
using Heimdall.Infrastructure.Data.Interceptors;
using Heimdall.Infrastructure.Security;
using Heimdall.Infrastructure.Services;
using Microsoft.AspNetCore.Identity;
using Microsoft.Data.Sqlite;
using Microsoft.EntityFrameworkCore;
namespace Heimdall.Web.Tests.Security;
public sealed class AccessFixture : IDisposable
{
private readonly SqliteConnection _connection = new("DataSource=:memory:");
public AppDbContext Db { get; }
public CameraAccessService Cameras { get; }
public ResourceAccess Access { get; }
public Site A { get; } = new("A");
public Site B { get; } = new("B");
public Camera A1 { get; }
public Camera A2 { get; }
public Camera B1 { get; }
public Agent AgentA { get; }
public Agent PeerA { get; }
public Agent AgentB { get; }
public ApiKey KeyA { get; }
public Group Group { get; } = new("Operators", null);
public AccessFixture()
{
_connection.Open();
_connection.CreateFunction("NOW", () => DateTime.UtcNow.ToString("O", System.Globalization.CultureInfo.InvariantCulture));
Db = new(new DbContextOptionsBuilder<AppDbContext>().UseSqlite(_connection).Options, new AuditInterceptor());
Db.Database.EnsureCreated();
AgentA = new(A.Id, "a", "a", "1");
PeerA = new(A.Id, "peer", "peer", "1");
AgentB = new(B.Id, "b", "b", "1");
var (_, prefix, hash) = ApiKeyAuthenticationHandler.GenerateApiKey();
AgentA.Register(hash, prefix);
KeyA = new(hash, prefix, A.Id);
A1 = new(A.Id, "a1"); A1.AssignAgent(AgentA.Id);
A2 = new(A.Id, "a2"); A2.AssignAgent(PeerA.Id);
B1 = new(B.Id, "b1"); B1.AssignAgent(AgentB.Id);
Db.AddRange(A, B, AgentA, PeerA, AgentB, KeyA, A1, A2, B1, Group);
Db.SaveChanges();
Cameras = new(Db);
Access = new(Db, Cameras);
}
public AppUser User(string role, string grant)
{
var user = new AppUser { Id = Guid.NewGuid(), UserName = Guid.NewGuid().ToString() };
var entity = Db.Roles.FirstOrDefault(r => r.Name == role) ?? new AppRole { Id = Guid.NewGuid(), Name = role, NormalizedName = role.ToUpperInvariant() };
if (Db.Entry(entity).State == EntityState.Detached) Db.Roles.Add(entity);
Db.Users.Add(user);
Db.UserRoles.Add(new IdentityUserRole<Guid> { UserId = user.Id, RoleId = entity.Id });
switch (grant)
{
case "direct": Db.UserCameraAccesses.Add(new(user.Id, A.Id, A1.Id)); break;
case "legacy": Db.UserCameras.Add(new(user.Id, A1.Id)); break;
case "site": Db.UserSites.Add(new(user.Id, A.Id)); break;
case "group":
Db.UserGroups.Add(new(user.Id, Group.Id));
if (!Db.GroupCameraAccesses.Any(g => g.GroupId == Group.Id && g.CameraId == A1.Id))
Db.GroupCameraAccesses.Add(new(Group.Id, A1.Id));
break;
}
Db.SaveChanges();
return user;
}
public static ClaimsPrincipal Principal(Guid id, bool agent = false) => new(new ClaimsIdentity(
agent ? [new Claim(ClaimTypes.NameIdentifier, id.ToString()), new Claim("auth_method", "api_key")]
: [new Claim(ClaimTypes.NameIdentifier, id.ToString())], "test"));
public void Dispose() { Db.Dispose(); _connection.Dispose(); }
}