Fix test bad practices and tighten weak assertions
- StorageServiceTests: replace out-param + manual dispose with IAsyncLifetime - PictureServiceTests: Assert.True bound checks -> Assert.InRange - CreateAlbumCommandHandlerTests: assert the specific NotFound error type instead of just IsError, matching what the test name promises - CreateAlbumRequestValidatorTests: replace a boundary Theory that computed its expected result inline with explicit Facts against the real AlbumReleaseYear constants - Remove unused using directives across a handful of production and test files found during the review Full suite verified green: 250/250 tests passing, 0 build warnings.
This commit is contained in:
@@ -1,7 +1,9 @@
|
||||
using System.Net;
|
||||
using System.Net.Http.Headers;
|
||||
using System.Text;
|
||||
using Microsoft.Extensions.Logging.Abstractions;
|
||||
using Musify.Infrastructure.Configuration;
|
||||
using Musify.Infrastructure.Persistence;
|
||||
using Musify.Infrastructure.Services;
|
||||
using Musify.Infrastructure.Tests.TestSupport;
|
||||
using Xunit;
|
||||
@@ -9,9 +11,12 @@ using Xunit;
|
||||
namespace Musify.Infrastructure.Tests.Services;
|
||||
|
||||
[Collection(InfrastructureCollection.Name)]
|
||||
public sealed class StorageServiceTests(InfrastructureTestFixture fixture)
|
||||
public sealed class StorageServiceTests(InfrastructureTestFixture fixture) : IAsyncLifetime
|
||||
{
|
||||
private StorageService CreateService(out Musify.Infrastructure.Persistence.Database database)
|
||||
private Database database = null!;
|
||||
private StorageService service = null!;
|
||||
|
||||
public Task InitializeAsync()
|
||||
{
|
||||
database = fixture.CreateDatabase();
|
||||
var storageConfiguration = new InfrastructureStorageConfiguration
|
||||
@@ -22,14 +27,15 @@ public sealed class StorageServiceTests(InfrastructureTestFixture fixture)
|
||||
ForcePathStyle = true,
|
||||
UseHttp = true,
|
||||
};
|
||||
return new StorageService(database, fixture.CreateS3Client(), NullLogger<StorageService>.Instance, storageConfiguration);
|
||||
service = new StorageService(database, fixture.CreateS3Client(), NullLogger<StorageService>.Instance, storageConfiguration);
|
||||
return Task.CompletedTask;
|
||||
}
|
||||
|
||||
public Task DisposeAsync() => database.DisposeAsync().AsTask();
|
||||
|
||||
[Fact]
|
||||
public async Task UploadThenGetFile_RoundTripsTheSameBytes()
|
||||
{
|
||||
var service = CreateService(out var database);
|
||||
await using var _ = database;
|
||||
var key = $"tests/{Guid.NewGuid():N}/round-trip.txt";
|
||||
var content = "hello seaweedfs"u8.ToArray();
|
||||
|
||||
@@ -45,8 +51,6 @@ public sealed class StorageServiceTests(InfrastructureTestFixture fixture)
|
||||
[Fact]
|
||||
public async Task HeadObjectAsync_ExistingObject_ReturnsMetadata()
|
||||
{
|
||||
var service = CreateService(out var database);
|
||||
await using var _ = database;
|
||||
var key = $"tests/{Guid.NewGuid():N}/head.txt";
|
||||
await service.UploadFileAsync(new MemoryStream("x"u8.ToArray()), "text/plain", InfrastructureTestFixture.S3Bucket, key, CancellationToken.None);
|
||||
|
||||
@@ -59,9 +63,6 @@ public sealed class StorageServiceTests(InfrastructureTestFixture fixture)
|
||||
[Fact]
|
||||
public async Task HeadObjectAsync_MissingObject_ReturnsNull()
|
||||
{
|
||||
var service = CreateService(out var database);
|
||||
await using var _ = database;
|
||||
|
||||
var metadata = await service.HeadObjectAsync(InfrastructureTestFixture.S3Bucket, $"tests/{Guid.NewGuid():N}/missing.txt", CancellationToken.None);
|
||||
|
||||
Assert.Null(metadata);
|
||||
@@ -70,8 +71,6 @@ public sealed class StorageServiceTests(InfrastructureTestFixture fixture)
|
||||
[Fact]
|
||||
public async Task RemoveFileAsync_ExistingObject_DeletesIt()
|
||||
{
|
||||
var service = CreateService(out var database);
|
||||
await using var _ = database;
|
||||
var key = $"tests/{Guid.NewGuid():N}/to-remove.txt";
|
||||
await service.UploadFileAsync(new MemoryStream("x"u8.ToArray()), "text/plain", InfrastructureTestFixture.S3Bucket, key, CancellationToken.None);
|
||||
|
||||
@@ -83,8 +82,6 @@ public sealed class StorageServiceTests(InfrastructureTestFixture fixture)
|
||||
[Fact]
|
||||
public async Task CopyFileAsync_CopiesToTheDestinationKey()
|
||||
{
|
||||
var service = CreateService(out var database);
|
||||
await using var _ = database;
|
||||
var sourceKey = $"tests/{Guid.NewGuid():N}/source.txt";
|
||||
var destinationKey = $"tests/{Guid.NewGuid():N}/destination.txt";
|
||||
await service.UploadFileAsync(new MemoryStream("copy-me"u8.ToArray()), "text/plain", InfrastructureTestFixture.S3Bucket, sourceKey, CancellationToken.None);
|
||||
@@ -97,8 +94,6 @@ public sealed class StorageServiceTests(InfrastructureTestFixture fixture)
|
||||
[Fact]
|
||||
public async Task ListObjectsAsync_ReturnsEveryObjectUnderThePrefix()
|
||||
{
|
||||
var service = CreateService(out var database);
|
||||
await using var _ = database;
|
||||
var prefix = $"tests/{Guid.NewGuid():N}";
|
||||
await service.UploadFileAsync(new MemoryStream("a"u8.ToArray()), "text/plain", InfrastructureTestFixture.S3Bucket, $"{prefix}/a.txt", CancellationToken.None);
|
||||
await service.UploadFileAsync(new MemoryStream("b"u8.ToArray()), "text/plain", InfrastructureTestFixture.S3Bucket, $"{prefix}/b.txt", CancellationToken.None);
|
||||
@@ -113,8 +108,6 @@ public sealed class StorageServiceTests(InfrastructureTestFixture fixture)
|
||||
[Fact]
|
||||
public async Task RemoveFolderAsync_DeletesEveryObjectUnderThePrefix()
|
||||
{
|
||||
var service = CreateService(out var database);
|
||||
await using var _ = database;
|
||||
var prefix = $"tests/{Guid.NewGuid():N}";
|
||||
await service.UploadFileAsync(new MemoryStream("a"u8.ToArray()), "text/plain", InfrastructureTestFixture.S3Bucket, $"{prefix}/a.txt", CancellationToken.None);
|
||||
await service.UploadFileAsync(new MemoryStream("b"u8.ToArray()), "text/plain", InfrastructureTestFixture.S3Bucket, $"{prefix}/nested/b.txt", CancellationToken.None);
|
||||
@@ -130,8 +123,6 @@ public sealed class StorageServiceTests(InfrastructureTestFixture fixture)
|
||||
[Fact]
|
||||
public async Task GetUploadUrlAsync_PresignedPut_CanActuallyUploadTheObject()
|
||||
{
|
||||
var service = CreateService(out var database);
|
||||
await using var _ = database;
|
||||
var key = $"tests/{Guid.NewGuid():N}/presigned.txt";
|
||||
|
||||
var uploadUrl = await service.GetUploadUrlAsync(
|
||||
@@ -145,15 +136,14 @@ public sealed class StorageServiceTests(InfrastructureTestFixture fixture)
|
||||
// signs Content-Type byte-for-byte) and the server answers 403 SignatureDoesNotMatch.
|
||||
Content = new ByteArrayContent(Encoding.UTF8.GetBytes("presigned content"))
|
||||
};
|
||||
request.Content.Headers.ContentType = new System.Net.Http.Headers.MediaTypeHeaderValue("text/plain");
|
||||
request.Content.Headers.ContentType = new MediaTypeHeaderValue("text/plain");
|
||||
// GetUploadUrlAsync defaults preventOverwrite to true, which signs the URL together with
|
||||
// this header (see StorageService.GetUploadUrlAsync) — the actual PUT has to send it too,
|
||||
// or the signature no longer matches and the server answers 403.
|
||||
request.Headers.TryAddWithoutValidation("If-None-Match", "*");
|
||||
var response = await httpClient.SendAsync(request);
|
||||
var responseBody = await response.Content.ReadAsStringAsync();
|
||||
|
||||
Assert.True(response.StatusCode == HttpStatusCode.OK, $"status={response.StatusCode}, body={responseBody}");
|
||||
Assert.Equal(HttpStatusCode.OK, response.StatusCode);
|
||||
var metadata = await service.HeadObjectAsync(InfrastructureTestFixture.S3Bucket, key, CancellationToken.None);
|
||||
Assert.NotNull(metadata);
|
||||
Assert.Equal("presigned content".Length, metadata.ContentLength);
|
||||
|
||||
Reference in New Issue
Block a user