Files
pos-system/services/fnb-engine-net/tests/FnbEngine.UnitTests/Application/Commands/CreateTableCommandHandlerTests.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

117 lines
4.2 KiB
C#

using FluentAssertions;
using FnbEngine.API.Application.Commands;
using FnbEngine.Domain.AggregatesModel.TableAggregate;
using FnbEngine.Domain.SeedWork;
using Microsoft.Extensions.Logging;
using Moq;
using Xunit;
namespace FnbEngine.UnitTests.Application.Commands;
/// <summary>
/// EN: Unit tests for CreateTableCommandHandler.
/// VI: Unit tests cho CreateTableCommandHandler.
/// </summary>
public class CreateTableCommandHandlerTests
{
private readonly Mock<ITableRepository> _repoMock;
private readonly Mock<ILogger<CreateTableCommandHandler>> _loggerMock;
private readonly CreateTableCommandHandler _handler;
public CreateTableCommandHandlerTests()
{
_repoMock = new Mock<ITableRepository>();
_loggerMock = new Mock<ILogger<CreateTableCommandHandler>>();
_repoMock.Setup(r => r.UnitOfWork.SaveEntitiesAsync(It.IsAny<CancellationToken>()))
.ReturnsAsync(true);
_repoMock.Setup(r => r.AddAsync(It.IsAny<Table>(), It.IsAny<CancellationToken>()))
.ReturnsAsync((Table t, CancellationToken _) => t);
_handler = new CreateTableCommandHandler(_repoMock.Object, _loggerMock.Object);
}
[Fact]
public async Task Handle_WithValidCommand_ShouldCreateTableAndReturnId()
{
// Arrange
var shopId = Guid.NewGuid();
_repoMock.Setup(r => r.GetByNumberAsync(shopId, "A-01", It.IsAny<CancellationToken>()))
.ReturnsAsync((Table?)null);
var command = new CreateTableCommand(shopId, "A-01", 4, "Main Hall");
// Act
var result = await _handler.Handle(command, CancellationToken.None);
// Assert
result.Should().NotBeNull();
result.TableId.Should().NotBeEmpty();
_repoMock.Verify(r => r.AddAsync(It.IsAny<Table>(), It.IsAny<CancellationToken>()), Times.Once);
_repoMock.Verify(r => r.UnitOfWork.SaveEntitiesAsync(It.IsAny<CancellationToken>()), Times.Once);
}
[Fact]
public async Task Handle_WithDuplicateTableNumber_ShouldThrowInvalidOperationException()
{
// Arrange
var shopId = Guid.NewGuid();
var existingTable = new Table(shopId, "A-01", 4);
_repoMock.Setup(r => r.GetByNumberAsync(shopId, "A-01", It.IsAny<CancellationToken>()))
.ReturnsAsync(existingTable);
var command = new CreateTableCommand(shopId, "A-01", 6);
// Act
var action = () => _handler.Handle(command, CancellationToken.None);
// Assert
await action.Should().ThrowAsync<InvalidOperationException>()
.WithMessage("*already exists*");
}
[Fact]
public async Task Handle_WithHourlyRate_ShouldSetHourlyRateOnTable()
{
// Arrange
var shopId = Guid.NewGuid();
_repoMock.Setup(r => r.GetByNumberAsync(shopId, "K-01", It.IsAny<CancellationToken>()))
.ReturnsAsync((Table?)null);
Table? capturedTable = null;
_repoMock.Setup(r => r.AddAsync(It.IsAny<Table>(), It.IsAny<CancellationToken>()))
.Callback<Table, CancellationToken>((t, _) => capturedTable = t)
.ReturnsAsync((Table t, CancellationToken _) => t);
var command = new CreateTableCommand(shopId, "K-01", 4, HourlyRate: 100_000m);
// Act
await _handler.Handle(command, CancellationToken.None);
// Assert
capturedTable.Should().NotBeNull();
capturedTable!.HourlyRate.Should().Be(100_000m);
}
[Fact]
public async Task Handle_WithZeroHourlyRate_ShouldNotSetHourlyRate()
{
// Arrange
var shopId = Guid.NewGuid();
_repoMock.Setup(r => r.GetByNumberAsync(shopId, "K-02", It.IsAny<CancellationToken>()))
.ReturnsAsync((Table?)null);
Table? capturedTable = null;
_repoMock.Setup(r => r.AddAsync(It.IsAny<Table>(), It.IsAny<CancellationToken>()))
.Callback<Table, CancellationToken>((t, _) => capturedTable = t)
.ReturnsAsync((Table t, CancellationToken _) => t);
var command = new CreateTableCommand(shopId, "K-02", 4, HourlyRate: 0);
// Act
await _handler.Handle(command, CancellationToken.None);
// Assert
capturedTable.Should().NotBeNull();
capturedTable!.HourlyRate.Should().Be(0);
}
}