Files
pos-system/services/fnb-engine-net/tests/FnbEngine.UnitTests/Domain/SessionTests.cs
Ho Ngoc Hai 6061164873 feat: add multi-tenant row-level security across 5 services and 96 FnB engine unit tests
Security (P0-5):
- Implement ITenantProvider + HttpContextTenantProvider per service (order, fnb, inventory, catalog, wallet)
- Add EF Core global query filters for tenant isolation (shop_id/user_id based)
- Add TenantMiddleware setting PostgreSQL session variables for RLS
- Create PostgreSQL RLS policies script (scripts/db/rls-policies.sql)
- Adapter pattern bridges API-layer to Infrastructure-layer (Clean Architecture)
- Bypass mechanisms for admin roles, service-to-service calls, and migrations

Testing (P1-12):
- Add 96 unit tests for fnb-engine (up from 3)
- 57 domain entity tests: Table(18), KitchenTicket(12), Session(8), Reservation(13), Recipe(6)
- 39 command handler tests: CRUD operations, status transitions, validation

Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
2026-03-06 13:40:34 +07:00

116 lines
3.1 KiB
C#

using FluentAssertions;
using FnbEngine.Domain.AggregatesModel.SessionAggregate;
using FnbEngine.Domain.Exceptions;
using Xunit;
namespace FnbEngine.UnitTests.Domain;
/// <summary>
/// EN: Unit tests for Session aggregate behavior.
/// VI: Unit tests cho hanh vi aggregate Session.
/// </summary>
public class SessionTests
{
private static readonly Guid ValidTableId = Guid.NewGuid();
private static readonly Guid ValidShopId = Guid.NewGuid();
[Fact]
public void Constructor_WithValidData_ShouldCreateActiveSession()
{
// Act
var session = new Session(ValidTableId, ValidShopId, 4);
// Assert
session.Id.Should().NotBeEmpty();
session.TableId.Should().Be(ValidTableId);
session.ShopId.Should().Be(ValidShopId);
session.GuestCount.Should().Be(4);
session.Status.Should().Be("Active");
session.StartedAt.Should().BeCloseTo(DateTime.UtcNow, TimeSpan.FromSeconds(5));
session.ClosedAt.Should().BeNull();
}
[Fact]
public void Constructor_WithDefaultGuestCount_ShouldDefaultToOne()
{
// Act
var session = new Session(ValidTableId, ValidShopId);
// Assert
session.GuestCount.Should().Be(1);
}
[Fact]
public void Constructor_WithEmptyTableId_ShouldThrowDomainException()
{
// Act
var action = () => new Session(Guid.Empty, ValidShopId, 2);
// Assert
action.Should().Throw<DomainException>()
.WithMessage("*Table ID*");
}
[Fact]
public void Constructor_WithEmptyShopId_ShouldThrowDomainException()
{
// Act
var action = () => new Session(ValidTableId, Guid.Empty, 2);
// Assert
action.Should().Throw<DomainException>()
.WithMessage("*Shop ID*");
}
[Fact]
public void Constructor_WithZeroGuestCount_ShouldThrowDomainException()
{
// Act
var action = () => new Session(ValidTableId, ValidShopId, 0);
// Assert
action.Should().Throw<DomainException>()
.WithMessage("*Guest count*");
}
[Fact]
public void Constructor_WithNegativeGuestCount_ShouldThrowDomainException()
{
// Act
var action = () => new Session(ValidTableId, ValidShopId, -1);
// Assert
action.Should().Throw<DomainException>();
}
[Fact]
public void Close_WhenActive_ShouldSetStatusToClosedAndSetClosedAt()
{
// Arrange
var session = new Session(ValidTableId, ValidShopId, 2);
// Act
session.Close();
// Assert
session.Status.Should().Be("Closed");
session.ClosedAt.Should().NotBeNull();
session.ClosedAt.Should().BeCloseTo(DateTime.UtcNow, TimeSpan.FromSeconds(5));
}
[Fact]
public void Close_WhenAlreadyClosed_ShouldThrowDomainException()
{
// Arrange
var session = new Session(ValidTableId, ValidShopId, 2);
session.Close();
// Act
var action = () => session.Close();
// Assert
action.Should().Throw<DomainException>()
.WithMessage("*already closed*");
}
}