46 lines
1.9 KiB
C#
46 lines
1.9 KiB
C#
using BuildFlow.Platform.Application.Auth.Commands;
|
|
using BuildFlow.Platform.Application.Auth.Handlers;
|
|
using File.Application.Features.Files.DTOs;
|
|
using File.Application.Features.Files.Queries;
|
|
using MediatR;
|
|
using Microsoft.EntityFrameworkCore;
|
|
using NSubstitute;
|
|
using Xunit;
|
|
|
|
namespace BuildFlow.Platform.Tests.Auth;
|
|
|
|
/// <summary>
|
|
/// 更新个人资料测试。数据库保留对象键,响应头像通过 File Application 路径查询解析。
|
|
/// </summary>
|
|
public class UpdateProfileCommandHandlerTests
|
|
{
|
|
[Fact]
|
|
public async Task Handle_ShouldPersistAvatarReferenceAndReturnResolvedPath()
|
|
{
|
|
await using var db = PlatformTestDb.CreateDbContext();
|
|
var userId = Guid.NewGuid();
|
|
await PlatformTestDb.SeedUserAsync(db, userId, "alice");
|
|
var mediator = Substitute.For<IMediator>();
|
|
mediator.Send(Arg.Any<ResolveFilePreviewPathsQuery>(), Arg.Any<CancellationToken>())
|
|
.Returns(Task.FromResult<IReadOnlyList<FilePreviewPathDto>>(
|
|
[
|
|
new("avatars/alice-new.png", "/api/files/preview?objectKey=avatars%2Falice-new.png")
|
|
]));
|
|
var handler = new UpdateProfileCommandHandler(db, mediator);
|
|
|
|
var result = await handler.Handle(new UpdateProfileCommand(
|
|
userId.ToString(),
|
|
"Alice Zhang",
|
|
"avatars/alice-new.png",
|
|
"BuildFlow user"), CancellationToken.None);
|
|
|
|
var persisted = await db.Users.SingleAsync(user => user.Id == userId);
|
|
Assert.Equal("avatars/alice-new.png", persisted.Avatar);
|
|
Assert.Equal("/api/files/preview?objectKey=avatars%2Falice-new.png", result.Avatar);
|
|
await mediator.Received(1).Send(
|
|
Arg.Is<ResolveFilePreviewPathsQuery>(query =>
|
|
query.References.SequenceEqual(new string?[] { "avatars/alice-new.png" })),
|
|
Arg.Any<CancellationToken>());
|
|
}
|
|
}
|