62 lines
3.0 KiB
C#
62 lines
3.0 KiB
C#
using Heimdall.Domain.Entities;
|
|||
|
|
using Heimdall.Infrastructure.Data;
|
||
|
|
using Heimdall.Infrastructure.Data.Interceptors;
|
||
|
|
using Heimdall.Infrastructure.Identity;
|
||
|
|
using Heimdall.Infrastructure.Security;
|
||
|
|
using Heimdall.Infrastructure.Services;
|
||
|
|
using Heimdall.TestCommon;
|
||
|
|
using Microsoft.AspNetCore.Identity;
|
||
|
|
using Microsoft.EntityFrameworkCore;
|
||
|
|
using Npgsql;
|
||
|
|
using Shouldly;
|
||
|
|
|
||
|
|
namespace Heimdall.Infrastructure.Tests;
|
||
|
|
|
||
|
|
[Collection("PostgresInfra")]
|
||
|
|
public sealed class CameraAccessAdminPostgresTests(PostgreSqlContainerFixture postgres) : IAsyncLifetime
|
||
|
|
{
|
||
|
|
private readonly string _database = "camera_access_admin_" + Guid.NewGuid().ToString("N");
|
||
|
|
private string _connectionString = null!;
|
||
|
|
|
||
|
|
public async Task InitializeAsync()
|
||
|
|
{
|
||
|
|
await using var admin = new NpgsqlConnection(postgres.ConnectionString);
|
||
|
|
await admin.OpenAsync();
|
||
|
|
await using var create = new NpgsqlCommand($"CREATE DATABASE {_database}", admin);
|
||
|
|
await create.ExecuteNonQueryAsync();
|
||
|
|
_connectionString = new NpgsqlConnectionStringBuilder(postgres.ConnectionString) { Database = _database, Pooling = false }.ConnectionString;
|
||
|
|
await using var db = CreateContext();
|
||
|
|
await db.Database.ExecuteSqlRawAsync("CREATE EXTENSION IF NOT EXISTS pg_trgm");
|
||
|
|
await db.Database.EnsureCreatedAsync();
|
||
|
|
}
|
||
|
|
|
||
|
|
public async Task DisposeAsync()
|
||
|
|
{
|
||
|
|
await using var admin = new NpgsqlConnection(postgres.ConnectionString);
|
||
|
|
await admin.OpenAsync();
|
||
|
|
await using var drop = new NpgsqlCommand($"DROP DATABASE IF EXISTS {_database} WITH (FORCE)", admin);
|
||
|
|
await drop.ExecuteNonQueryAsync();
|
||
|
|
}
|
||
|
|
|
||
|
|
[Fact]
|
||
|
|
public async Task Admin_role_lookup_and_camera_authorization_translate_and_preserve_results()
|
||
|
|
{
|
||
|
|
var adminId = Guid.NewGuid(); var viewerId = Guid.NewGuid();
|
||
|
|
var site = new Site("Camera access site"); var camera = new Camera(site.Id, "Camera");
|
||
|
|
var role = new IdentityAppRole { Id = Guid.NewGuid(), Name = AppRoles.Admin, NormalizedName = AppRoles.Admin.ToUpperInvariant() };
|
||
|
|
await using (var db = CreateContext())
|
||
|
|
{
|
||
|
|
db.Sites.Add(site); db.Cameras.Add(camera); db.Roles.Add(role);
|
||
|
|
db.Users.AddRange(new IdentityAppUser { Id = adminId, UserName = "admin", NormalizedUserName = "ADMIN" }, new IdentityAppUser { Id = viewerId, UserName = "viewer", NormalizedUserName = "VIEWER" });
|
||
|
|
db.UserRoles.Add(new IdentityUserRole<Guid> { UserId = adminId, RoleId = role.Id });
|
||
|
|
await db.SaveChangesAsync();
|
||
|
|
var access = new CameraAccessService(db, db);
|
||
|
|
(await access.HasCameraAccessAsync(adminId, camera.Id)).ShouldBeTrue();
|
||
|
|
(await access.HasCameraAccessAsync(viewerId, camera.Id)).ShouldBeFalse();
|
||
|
|
(await access.GetAccessibleSiteIdsAsync(adminId)).ShouldContain(site.Id);
|
||
|
|
(await access.GetAccessibleSiteIdsAsync(viewerId)).ShouldBeEmpty();
|
||
|
|
}
|
||
|
|
}
|
||
|
|
|
||
|
|
private AppDbContext CreateContext() => new(new DbContextOptionsBuilder<AppDbContext>().UseNpgsql(_connectionString).Options, new AuditInterceptor());
|
||
|
|
}
|