build-flow.api/tests/BuildFlow.Platform.Tests/Users/AssignRolesCommandHandlerTests.cs
xiangning 51eea796e2 refactor(rag): 重构AI模型相关代码结构和数据库迁移逻辑
- 将AI模型相关端点、命令、处理器移动到模块内部统一管理
- 优化数据库迁移脚本,添加对不同环境表状态的兼容处理逻辑
- 为异常处理添加错误码枚举参数,统一错误处理方式
- 重构多处业务逻辑中的异常抛出方式,增强代码一致性
- 移除独立的组件清单分组和项目定义,简化前端物料面板数据结构
- 修复测试代码中实体引用路径错误问题
- 添加新的监控模块测试项目到解决方案文件中
2026-07-20 08:25:18 +08:00

56 lines
2.2 KiB
C#
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

using FluentAssertions;
using Microsoft.EntityFrameworkCore;
using NSubstitute;
using Xunit;
using BuildFlow.Platform.Application.Users.Commands;
using BuildFlow.Platform.Application.Users.Handlers;
using BuildFlow.Platform.Domain.Common;
using BuildFlow.Shared.Domain.Exceptions;
namespace BuildFlow.Platform.Tests.Users;
/// <summary>
/// 分配角色 Command 处理器单元测试。覆盖分配成功 + 权限版本号自增、角色不存在拒绝。
/// </summary>
public class AssignRolesCommandHandlerTests
{
[Fact]
public async Task Handle_ValidAssignment_ShouldBumpPermissionVersion()
{
// Arrange
var db = PlatformTestDb.CreateDbContext();
var userId = Guid.NewGuid();
await PlatformTestDb.SeedUserAsync(db, userId, "assign-target", permissionVersion: 3);
var roleId = Guid.NewGuid();
db.Roles.Add(PlatformTestDb.WithAudit(new BuildFlow.Platform.Domain.Entities.Role.RoleEntity { Id = roleId, Name = "Editor" }));
await db.SaveChangesAsync();
var versionStore = Substitute.For<IPermissionVersionStore>();
var handler = new AssignRolesCommandHandler(db, versionStore);
// Act
await handler.Handle(new AssignRolesCommand(userId, [roleId]), CancellationToken.None);
// Assert:版本号自增(3 → 4)+ Redis 镜像同步
var user = await db.Users.FirstAsync(u => u.Id == userId);
user.PermissionVersion.Should().Be(4);
await versionStore.Received(1).BumpAsync(Arg.Is<IEnumerable<Guid>>(ids => ids.Contains(userId)), Arg.Any<CancellationToken>());
}
[Fact]
public async Task Handle_NonExistentRole_ShouldThrowNotFound()
{
// Arrange
var db = PlatformTestDb.CreateDbContext();
var userId = Guid.NewGuid();
await PlatformTestDb.SeedUserAsync(db, userId, "victim");
var handler = new AssignRolesCommandHandler(db, Substitute.For<IPermissionVersionStore>());
// Act
var act = () => handler.Handle(new AssignRolesCommand(userId, [Guid.NewGuid()]), CancellationToken.None);
// Assert
await act.Should().ThrowAsync<NotFoundException>()
.WithMessage("*部分角色不存在*");
}
}