build-flow.api/tests/BuildFlow.SSO.Tests/AssignPermissionsCommandHandlerTests.cs

54 lines
1.7 KiB
C#

using BuildFlow.Shared.Domain.Exceptions;
using FluentAssertions;
using Microsoft.EntityFrameworkCore;
using SSO.Application.Roles.Commands;
using SSO.Application.Roles.Handlers;
using SSO.Domain.Entities;
using SSO.Infrastructure.Persistence;
using Xunit;
namespace BuildFlow.SSO.Tests;
public class AssignPermissionsCommandHandlerTests
{
private static SsoDbContext CreateInMemoryDb()
{
var options = new DbContextOptionsBuilder<SsoDbContext>()
.UseInMemoryDatabase($"sso-assign-{Guid.NewGuid()}")
.Options;
return new SsoDbContext(options);
}
[Fact]
public async Task Handle_角色不存在_抛NotFound()
{
var db = CreateInMemoryDb();
var handler = new AssignPermissionsCommandHandler(db);
var act = () => handler.Handle(
new AssignPermissionsCommand(Guid.NewGuid(), new() { Guid.NewGuid() }),
CancellationToken.None);
await act.Should().ThrowAsync<NotFoundException>()
.WithMessage("角色不存在");
}
[Fact]
public async Task Handle_权限不存在_抛NotFound()
{
var db = CreateInMemoryDb();
var roleId = Guid.NewGuid();
db.Roles.Add(new Role { Id = roleId, Name = "测试角色", CreatedBy = "system", UpdatedBy = "system", OperatorIP = "127.0.0.1" }.FillAudit());
await db.SaveChangesAsync();
var handler = new AssignPermissionsCommandHandler(db);
var act = () => handler.Handle(
new AssignPermissionsCommand(roleId, new() { Guid.NewGuid() }),
CancellationToken.None);
await act.Should().ThrowAsync<NotFoundException>()
.WithMessage("部分权限不存在");
}
}