feat: integration cloudfile

This commit is contained in:
2026-09-06 12:33:38 +07:00
parent c2088f42dc
commit 888dd41dcd
24 changed files with 1148 additions and 28 deletions
+134
View File
@@ -0,0 +1,134 @@
using System.Net;
using System.Net.Http;
using Microsoft.Extensions.Logging;
using Moq;
using mws.backend.dotnet.infrastructure.CloudFile;
namespace tests;
public class CloudFileDocumentContentStoreTests
{
private readonly Mock<ICloudFileHttpClient> _cfMock = new();
private readonly Mock<ILogger<CloudFileDocumentContentStore>> _loggerMock = new();
private readonly CloudFileDocumentContentStore _sut;
public CloudFileDocumentContentStoreTests()
{
_sut = new CloudFileDocumentContentStore(_cfMock.Object, _loggerMock.Object);
}
[Fact]
public async Task EnsureBucketAsync_CallsCorrectBucket()
{
var projectId = Guid.NewGuid();
await _sut.EnsureBucketAsync(projectId, CancellationToken.None);
_cfMock.Verify(c => c.EnsureBucketAsync($"mws-p-{projectId:N}", It.IsAny<CancellationToken>()), Times.Once);
}
[Fact]
public async Task UploadAsync_BuildsCorrectBucketAndKey()
{
var projectId = Guid.NewGuid();
var documentId = Guid.NewGuid();
using var stream = new MemoryStream(new byte[] { 1, 2, 3 });
await _sut.UploadAsync(projectId, documentId, "text/html; charset=utf-8", stream, CancellationToken.None);
_cfMock.Verify(c => c.UploadAsync(
$"mws-p-{projectId:N}",
$"{documentId:N}.html",
"text/html; charset=utf-8",
It.IsAny<Stream>(),
It.IsAny<CancellationToken>()), Times.Once);
}
[Fact]
public async Task UploadAsync_ThrowsStorageUnavailableOnHttpRequestException()
{
var projectId = Guid.NewGuid();
var documentId = Guid.NewGuid();
using var stream = new MemoryStream(new byte[] { 1, 2, 3 });
_cfMock.Setup(c => c.UploadAsync(
It.IsAny<string>(), It.IsAny<string>(), It.IsAny<string>(),
It.IsAny<Stream>(), It.IsAny<CancellationToken>()))
.ThrowsAsync(new HttpRequestException("Connection refused"));
var ex = await Assert.ThrowsAsync<mws.backend.dotnet.application.Common.StorageUnavailableException>(
() => _sut.UploadAsync(projectId, documentId, "text/html", stream, CancellationToken.None));
Assert.Contains("Cloud file upload failed", ex.Message);
}
[Fact]
public async Task DownloadAsync_ReturnsStreamNullOnNotFound()
{
var projectId = Guid.NewGuid();
var documentId = Guid.NewGuid();
_cfMock.Setup(c => c.DownloadAsync(It.IsAny<string>(), It.IsAny<string>(), It.IsAny<CancellationToken>()))
.ReturnsAsync(Stream.Null);
var result = await _sut.DownloadAsync(projectId, documentId, CancellationToken.None);
Assert.Equal(Stream.Null, result);
}
[Fact]
public async Task DownloadAsync_ThrowsStorageUnavailableOnHttpRequestException()
{
var projectId = Guid.NewGuid();
var documentId = Guid.NewGuid();
_cfMock.Setup(c => c.DownloadAsync(It.IsAny<string>(), It.IsAny<string>(), It.IsAny<CancellationToken>()))
.ThrowsAsync(new HttpRequestException("Connection refused"));
await Assert.ThrowsAsync<mws.backend.dotnet.application.Common.StorageUnavailableException>(
() => _sut.DownloadAsync(projectId, documentId, CancellationToken.None));
}
[Fact]
public async Task DeleteAsync_Swallows404Errors()
{
var projectId = Guid.NewGuid();
var documentId = Guid.NewGuid();
// Should not throw
await _sut.DeleteAsync(projectId, documentId, CancellationToken.None);
_cfMock.Verify(c => c.DeleteAsync(
$"mws-p-{projectId:N}",
$"{documentId:N}.html",
It.IsAny<CancellationToken>()), Times.Once);
}
[Fact]
public async Task ExistsAsync_ReturnsTrueWhenFileExists()
{
var projectId = Guid.NewGuid();
var documentId = Guid.NewGuid();
_cfMock.Setup(c => c.ExistsAsync(It.IsAny<string>(), It.IsAny<string>(), It.IsAny<CancellationToken>()))
.ReturnsAsync(true);
var result = await _sut.ExistsAsync(projectId, documentId, CancellationToken.None);
Assert.True(result);
}
[Fact]
public async Task ExistsAsync_ReturnsFalseOnException()
{
var projectId = Guid.NewGuid();
var documentId = Guid.NewGuid();
_cfMock.Setup(c => c.ExistsAsync(It.IsAny<string>(), It.IsAny<string>(), It.IsAny<CancellationToken>()))
.ThrowsAsync(new HttpRequestException("Connection refused"));
var result = await _sut.ExistsAsync(projectId, documentId, CancellationToken.None);
Assert.False(result);
}
}