Add media image handling and related endpoints
- Introduced `MediaImage` entity to manage images for markdown in instructions and news. - Updated `IAppDbContext` and `AppDbContext` to include `MediaImages` DbSet. - Implemented `DeleteMediaImageFilesAsync` method in `FactoryResetCommandHandler` to remove media images during factory reset. - Added new API endpoints for uploading and retrieving media images, enhancing markdown support. - Updated frontend components to utilize the new `MarkdownEditor` for image uploads in instructions and news. - Enhanced documentation to reflect the new media handling features and API specifications.
This commit is contained in:
@@ -0,0 +1,76 @@
|
||||
using NSubstitute;
|
||||
using PnvPanel.Application.Common.Interfaces;
|
||||
using PnvPanel.Application.Media;
|
||||
using PnvPanel.Application.Media.GetImage;
|
||||
using PnvPanel.Application.Tests.TestSupport;
|
||||
using PnvPanel.Domain.Media;
|
||||
using Xunit;
|
||||
|
||||
namespace PnvPanel.Application.Tests.Media;
|
||||
|
||||
public class GetMediaImageQueryHandlerTests
|
||||
{
|
||||
private readonly IFileStorage _fileStorage = Substitute.For<IFileStorage>();
|
||||
|
||||
[Fact]
|
||||
public async Task Handle_WhenImageExists_ReturnsContentStream()
|
||||
{
|
||||
using var dbContext = InMemoryDbContextFactory.Create();
|
||||
var image = MediaImage.Create("shot.png", "stored-name", "image/png", 10, Guid.NewGuid());
|
||||
dbContext.MediaImages.Add(image);
|
||||
await dbContext.SaveChangesAsync(CancellationToken.None);
|
||||
|
||||
_fileStorage
|
||||
.OpenReadAsync("stored-name", Arg.Any<CancellationToken>())
|
||||
.Returns(new MemoryStream([1, 2, 3]));
|
||||
|
||||
var handler = new GetMediaImageQueryHandler(dbContext, _fileStorage);
|
||||
|
||||
var result = await handler.Handle(
|
||||
new GetMediaImageQuery(image.Id),
|
||||
CancellationToken.None
|
||||
);
|
||||
|
||||
Assert.True(result.IsSuccess);
|
||||
Assert.Equal("image/png", result.Value.ContentType);
|
||||
Assert.Equal("shot.png", result.Value.FileName);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public async Task Handle_WhenRowExistsButFileMissing_ReturnsNotFound()
|
||||
{
|
||||
using var dbContext = InMemoryDbContextFactory.Create();
|
||||
var image = MediaImage.Create("shot.png", "stored-name", "image/png", 10, Guid.NewGuid());
|
||||
dbContext.MediaImages.Add(image);
|
||||
await dbContext.SaveChangesAsync(CancellationToken.None);
|
||||
|
||||
_fileStorage
|
||||
.OpenReadAsync("stored-name", Arg.Any<CancellationToken>())
|
||||
.Returns((Stream?)null);
|
||||
|
||||
var handler = new GetMediaImageQueryHandler(dbContext, _fileStorage);
|
||||
|
||||
var result = await handler.Handle(
|
||||
new GetMediaImageQuery(image.Id),
|
||||
CancellationToken.None
|
||||
);
|
||||
|
||||
Assert.False(result.IsSuccess);
|
||||
Assert.Equal(MediaErrors.NotFound, result.Error);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public async Task Handle_WhenImageUnknown_ReturnsNotFound()
|
||||
{
|
||||
using var dbContext = InMemoryDbContextFactory.Create();
|
||||
var handler = new GetMediaImageQueryHandler(dbContext, _fileStorage);
|
||||
|
||||
var result = await handler.Handle(
|
||||
new GetMediaImageQuery(Guid.NewGuid()),
|
||||
CancellationToken.None
|
||||
);
|
||||
|
||||
Assert.False(result.IsSuccess);
|
||||
Assert.Equal(MediaErrors.NotFound, result.Error);
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user