mirror of
https://github.com/aaronpo97/the-biergarten-app.git
synced 2026-10-15 10:04:44 +00:00
Add unit tests for UploadPhotoHandler
This commit is contained in:
@@ -32,6 +32,7 @@
|
|||||||
<Project Path="Features/Features.Locations/Features.Locations.csproj" />
|
<Project Path="Features/Features.Locations/Features.Locations.csproj" />
|
||||||
<Project Path="Features/Features.Locations.Tests/Features.Locations.Tests.csproj" />
|
<Project Path="Features/Features.Locations.Tests/Features.Locations.Tests.csproj" />
|
||||||
<Project Path="Features\Features.PhotoUpload\Features.PhotoUpload.csproj" />
|
<Project Path="Features\Features.PhotoUpload\Features.PhotoUpload.csproj" />
|
||||||
|
<Project Path="Features/Features.PhotoUpload.Tests/Features.PhotoUpload.Tests.csproj" />
|
||||||
</Folder>
|
</Folder>
|
||||||
<Folder Name="/Shared/">
|
<Folder Name="/Shared/">
|
||||||
<Project Path="Shared/Shared.Contracts/Shared.Contracts.csproj" />
|
<Project Path="Shared/Shared.Contracts/Shared.Contracts.csproj" />
|
||||||
|
|||||||
@@ -0,0 +1,95 @@
|
|||||||
|
using Domain.Entities;
|
||||||
|
using Features.ImageUploads.Commands.UploadPhoto;
|
||||||
|
using Features.ImageUploads.Repository;
|
||||||
|
using FluentAssertions;
|
||||||
|
using Infrastructure.FileUpload;
|
||||||
|
using Microsoft.AspNetCore.Http;
|
||||||
|
using Moq;
|
||||||
|
|
||||||
|
namespace Features.PhotoUpload.Tests.Commands;
|
||||||
|
|
||||||
|
public class UploadPhotoHandlerTests
|
||||||
|
{
|
||||||
|
private readonly Mock<IFileStorageProvider> _fileStorageProviderMock = new();
|
||||||
|
private readonly UploadPhotoHandler _handler;
|
||||||
|
private readonly Mock<IPhotoUploadRepository> _repoMock = new();
|
||||||
|
|
||||||
|
public UploadPhotoHandlerTests()
|
||||||
|
{
|
||||||
|
_handler = new UploadPhotoHandler(_fileStorageProviderMock.Object, _repoMock.Object);
|
||||||
|
}
|
||||||
|
|
||||||
|
private static Mock<IFormFile> ValidFile(string content = "fake-image-bytes", string contentType = "image/png")
|
||||||
|
{
|
||||||
|
Mock<IFormFile> file = new();
|
||||||
|
byte[] bytes = System.Text.Encoding.UTF8.GetBytes(content);
|
||||||
|
file.Setup(f => f.OpenReadStream()).Returns(() => new MemoryStream(bytes));
|
||||||
|
file.Setup(f => f.ContentType).Returns(contentType);
|
||||||
|
file.Setup(f => f.Length).Returns(bytes.Length);
|
||||||
|
return file;
|
||||||
|
}
|
||||||
|
|
||||||
|
[Fact]
|
||||||
|
public async Task Handle_UploadsFileUnderRequestedKey_AndPersistsPhoto_ReturnsNewPhotoId()
|
||||||
|
{
|
||||||
|
Mock<IFormFile> file = ValidFile();
|
||||||
|
UploadPhotoCommand command = new(Guid.NewGuid(), "breweries/some-brewery-id", file.Object);
|
||||||
|
|
||||||
|
string? uploadedKey = null;
|
||||||
|
string? uploadedContentType = null;
|
||||||
|
_fileStorageProviderMock
|
||||||
|
.Setup(p => p.UploadAsync(
|
||||||
|
It.IsAny<string>(),
|
||||||
|
It.IsAny<Stream>(),
|
||||||
|
It.IsAny<string>(),
|
||||||
|
It.IsAny<CancellationToken>()))
|
||||||
|
.Callback<string, Stream, string, CancellationToken>((key, _, contentType, _) =>
|
||||||
|
{
|
||||||
|
uploadedKey = key;
|
||||||
|
uploadedContentType = contentType;
|
||||||
|
})
|
||||||
|
.Returns(Task.CompletedTask);
|
||||||
|
|
||||||
|
Photo? persisted = null;
|
||||||
|
_repoMock
|
||||||
|
.Setup(r => r.CreateAsync(It.IsAny<Photo>(), It.IsAny<CancellationToken>()))
|
||||||
|
.Callback<Photo, CancellationToken>((photo, _) => persisted = photo)
|
||||||
|
.Returns(Task.CompletedTask);
|
||||||
|
|
||||||
|
Guid photoId = await _handler.Handle(command, CancellationToken.None);
|
||||||
|
|
||||||
|
photoId.Should().NotBe(Guid.Empty);
|
||||||
|
|
||||||
|
uploadedKey.Should().Be($"breweries/some-brewery-id/{photoId}");
|
||||||
|
uploadedContentType.Should().Be("image/png");
|
||||||
|
|
||||||
|
persisted.Should().NotBeNull();
|
||||||
|
persisted!.Hyperlink.Should().Be(uploadedKey);
|
||||||
|
persisted.UploadedById.Should().Be(command.UploadedById);
|
||||||
|
|
||||||
|
_fileStorageProviderMock.Verify(
|
||||||
|
p => p.UploadAsync(uploadedKey!, It.IsAny<Stream>(), "image/png", It.IsAny<CancellationToken>()),
|
||||||
|
Times.Once);
|
||||||
|
_repoMock.Verify(r => r.CreateAsync(It.IsAny<Photo>(), It.IsAny<CancellationToken>()), Times.Once);
|
||||||
|
}
|
||||||
|
|
||||||
|
[Fact]
|
||||||
|
public async Task Handle_UploadOrderedBeforePersist_DoesNotPersistWhenUploadFails()
|
||||||
|
{
|
||||||
|
Mock<IFormFile> file = ValidFile();
|
||||||
|
UploadPhotoCommand command = new(Guid.NewGuid(), "avatars", file.Object);
|
||||||
|
|
||||||
|
_fileStorageProviderMock
|
||||||
|
.Setup(p => p.UploadAsync(
|
||||||
|
It.IsAny<string>(),
|
||||||
|
It.IsAny<Stream>(),
|
||||||
|
It.IsAny<string>(),
|
||||||
|
It.IsAny<CancellationToken>()))
|
||||||
|
.ThrowsAsync(new InvalidOperationException("storage unavailable"));
|
||||||
|
|
||||||
|
Func<Task> act = () => _handler.Handle(command, CancellationToken.None);
|
||||||
|
|
||||||
|
await act.Should().ThrowAsync<InvalidOperationException>();
|
||||||
|
_repoMock.Verify(r => r.CreateAsync(It.IsAny<Photo>(), It.IsAny<CancellationToken>()), Times.Never);
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -0,0 +1,25 @@
|
|||||||
|
<Project Sdk="Microsoft.NET.Sdk">
|
||||||
|
<PropertyGroup>
|
||||||
|
<TargetFramework>net10.0</TargetFramework>
|
||||||
|
<ImplicitUsings>enable</ImplicitUsings>
|
||||||
|
<Nullable>enable</Nullable>
|
||||||
|
<IsPackable>false</IsPackable>
|
||||||
|
<RootNamespace>Features.PhotoUpload.Tests</RootNamespace>
|
||||||
|
</PropertyGroup>
|
||||||
|
|
||||||
|
<ItemGroup>
|
||||||
|
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="18.0.1" />
|
||||||
|
<PackageReference Include="xunit" Version="2.9.2" />
|
||||||
|
<PackageReference Include="xunit.runner.visualstudio" Version="2.8.2" />
|
||||||
|
<PackageReference Include="Moq" Version="4.20.72" />
|
||||||
|
<PackageReference Include="FluentAssertions" Version="7.2.0" />
|
||||||
|
</ItemGroup>
|
||||||
|
|
||||||
|
<ItemGroup>
|
||||||
|
<Using Include="Xunit" />
|
||||||
|
</ItemGroup>
|
||||||
|
|
||||||
|
<ItemGroup>
|
||||||
|
<ProjectReference Include="..\Features.PhotoUpload\Features.PhotoUpload.csproj" />
|
||||||
|
</ItemGroup>
|
||||||
|
</Project>
|
||||||
Reference in New Issue
Block a user