mirror of
https://github.com/Govor-team/Govor.git
synced 2026-11-17 12:59:25 +00:00
Expand server tests and enforce coverage
Consolidate server tests under tests/, upgrade them to .NET 10, and add CI coverage thresholds and reports. Expand API and application boundary coverage while fixing related issues in media handling, friend blocking, session crypto, presence, username validation, and local storage.
This commit is contained in:
@@ -0,0 +1,78 @@
|
||||
namespace Govor.Application.Tests.Unit.Storage;
|
||||
|
||||
[TestFixture]
|
||||
public class LocalStorageBoundaryTests
|
||||
{
|
||||
private string _root = null!;
|
||||
private LocalStorageService _service = null!;
|
||||
[SetUp]
|
||||
public void SetUp()
|
||||
{
|
||||
_root = Path.Combine(Path.GetTempPath(), "govor-storage-tests-" + Guid.NewGuid().ToString("N"));
|
||||
Directory.CreateDirectory(_root);
|
||||
_service = new(_root);
|
||||
}
|
||||
[TearDown]
|
||||
public void TearDown()
|
||||
{
|
||||
var resolved = Path.GetFullPath(_root);
|
||||
if (!resolved.StartsWith(Path.GetFullPath(Path.GetTempPath()), StringComparison.OrdinalIgnoreCase) ||
|
||||
!Path.GetFileName(resolved).StartsWith("govor-storage-tests-", StringComparison.Ordinal))
|
||||
throw new InvalidOperationException("Unexpected temporary storage path.");
|
||||
Directory.Delete(resolved, recursive: true);
|
||||
}
|
||||
|
||||
[TestCase("../uploads-sibling/secret.bin")]
|
||||
[TestCase("../outside.bin")]
|
||||
public async Task TraversalCannotReadOrDeleteFilesOutsideUploadRoot(string relative)
|
||||
{
|
||||
var target = Path.GetFullPath(Path.Combine(_root, "uploads", relative));
|
||||
Directory.CreateDirectory(Path.GetDirectoryName(target)!);
|
||||
await File.WriteAllTextAsync(target, "secret");
|
||||
Assert.ThrowsAsync<UnauthorizedAccessException>(() => _service.LoadAsync(relative));
|
||||
Assert.ThrowsAsync<UnauthorizedAccessException>(() => _service.RemoveAsync(relative));
|
||||
Assert.That(await File.ReadAllTextAsync(target), Is.EqualTo("secret"));
|
||||
}
|
||||
|
||||
[Test]
|
||||
public async Task AbsolutePathsAreRejectedEvenInsideStorage()
|
||||
{
|
||||
var absolute = Path.Combine(_root, "uploads", "absolute.bin");
|
||||
await File.WriteAllTextAsync(absolute, "secret");
|
||||
Assert.ThrowsAsync<UnauthorizedAccessException>(() => _service.LoadAsync(absolute));
|
||||
Assert.ThrowsAsync<UnauthorizedAccessException>(() => _service.RemoveAsync(absolute));
|
||||
Assert.That(File.Exists(absolute), Is.True);
|
||||
}
|
||||
|
||||
[Test]
|
||||
public async Task ReadOnlyFileCanBeDeletedAndRepeatedDeleteIsANoOp()
|
||||
{
|
||||
var path = await _service.SaveAsync(new byte[] { 1, 2 }, "../file.bin");
|
||||
var fullPath = Path.Combine(_root, "uploads", path);
|
||||
File.SetAttributes(fullPath, File.GetAttributes(fullPath) | FileAttributes.ReadOnly);
|
||||
await _service.RemoveAsync(path);
|
||||
await _service.RemoveAsync(path);
|
||||
Assert.That(File.Exists(fullPath), Is.False);
|
||||
}
|
||||
|
||||
[TestCase("")]
|
||||
[TestCase(" ")]
|
||||
[TestCase(null)]
|
||||
public void InvalidPathsAreRejected(string? path)
|
||||
{
|
||||
Assert.ThrowsAsync<ArgumentException>(() => _service.LoadAsync(path!));
|
||||
Assert.ThrowsAsync<ArgumentException>(() => _service.RemoveAsync(path!));
|
||||
}
|
||||
|
||||
[Test]
|
||||
public async Task ReusingExistingRootIsSafeAndMissingLoadIsReported()
|
||||
{
|
||||
_service = new(_root);
|
||||
Assert.ThrowsAsync<FileNotFoundException>(() => _service.LoadAsync("missing.bin"));
|
||||
var relative = await _service.SaveAsync(new byte[] { 3, 4 }, "same.bin");
|
||||
await using var stream = await _service.LoadAsync(relative);
|
||||
Assert.That(stream.CanRead, Is.True);
|
||||
Assert.That(stream.Length, Is.EqualTo(2));
|
||||
}
|
||||
}
|
||||
|
||||
@@ -0,0 +1,137 @@
|
||||
using AutoFixture;
|
||||
using Microsoft.AspNetCore.Hosting;
|
||||
using Moq;
|
||||
|
||||
namespace Govor.Application.Tests.Services;
|
||||
|
||||
[TestFixture]
|
||||
public class LocalStorageServiceTests
|
||||
{
|
||||
private Fixture _fixture = null!;
|
||||
private IStorageService _service = null!;
|
||||
private Mock<IWebHostEnvironment> _webHostEnvironmentMock = null!;
|
||||
private string _tempDirectory = null!;
|
||||
[SetUp]
|
||||
public void SetUp()
|
||||
{
|
||||
_fixture = new Fixture();
|
||||
_webHostEnvironmentMock = new Mock<IWebHostEnvironment>();
|
||||
|
||||
_tempDirectory = Path.Combine(Path.GetTempPath(), Guid.NewGuid().ToString());
|
||||
Directory.CreateDirectory(_tempDirectory);
|
||||
|
||||
_webHostEnvironmentMock.Setup(w => w.ContentRootPath).Returns(_tempDirectory);
|
||||
|
||||
_service = new LocalStorageService(_webHostEnvironmentMock.Object.ContentRootPath);
|
||||
}
|
||||
|
||||
[TearDown]
|
||||
public void TearDown()
|
||||
{
|
||||
if (Directory.Exists(_tempDirectory))
|
||||
{
|
||||
Directory.Delete(_tempDirectory, true);
|
||||
}
|
||||
}
|
||||
|
||||
[Test]
|
||||
public async Task Given_ValidDataAndFileName_When_SaveAsync_Then_FileIsSaved()
|
||||
{
|
||||
// Arrange
|
||||
var data = _fixture.CreateMany<byte>(1000).ToArray();
|
||||
var fileName = _fixture.Create<string>() + ".txt";
|
||||
var saveDate = DateTime.UtcNow;
|
||||
|
||||
// Act
|
||||
var url = await _service.SaveAsync(data, fileName);
|
||||
|
||||
// Assert
|
||||
Assert.That(url, Is.Not.Null);
|
||||
|
||||
// _tempDirectory/uploads/yyyy/MM/url
|
||||
var datePath = saveDate.ToString("yyyy/MM");
|
||||
var expectedFilePath = Path.Combine(_tempDirectory, "uploads", url);
|
||||
Assert.That(File.Exists(expectedFilePath), Is.True);
|
||||
}
|
||||
|
||||
[Test]
|
||||
public void Given_EmptyName_When_SaveAsync_Should_Throw_ArgumentException()
|
||||
{
|
||||
// Arrange
|
||||
var data = _fixture.CreateMany<byte>(1000).ToArray();
|
||||
|
||||
// Act & Assert
|
||||
Assert.ThrowsAsync<ArgumentException>(async () => await _service.SaveAsync(data, string.Empty));
|
||||
}
|
||||
|
||||
[Test]
|
||||
public void Given_EmptyData_When_SaveAsync_Should_Throw_ArgumentException()
|
||||
{
|
||||
// Arrange
|
||||
var name = _fixture.Create<string>();
|
||||
|
||||
// Act & Assert
|
||||
Assert.ThrowsAsync<ArgumentException>(async () => await _service.SaveAsync(default, name));
|
||||
}
|
||||
|
||||
[Test]
|
||||
public async Task Given_ValidUrl_When_LoadAsync_Then_Returns_FileStream()
|
||||
{
|
||||
// Arrange
|
||||
var date = DateTime.UtcNow.ToString("yyyy/MM");
|
||||
var corePath = Path.Combine(_tempDirectory, "uploads", date);
|
||||
Directory.CreateDirectory(corePath);
|
||||
|
||||
var fileName = _fixture.Create<string>() + ".txt";
|
||||
var fileContent = _fixture.Create<string>();
|
||||
var filePath = Path.Combine(corePath, fileName);
|
||||
await File.WriteAllTextAsync(filePath, fileContent);
|
||||
|
||||
// Act
|
||||
var url = Path.Combine(date, fileName);
|
||||
await using var stream = await _service.LoadAsync(url);
|
||||
|
||||
// Assert
|
||||
Assert.That(stream, Is.Not.Null);
|
||||
Assert.That(stream.Length, Is.GreaterThan(0));
|
||||
|
||||
// Дополнительная проверка: содержимое файла
|
||||
using var reader = new StreamReader(stream);
|
||||
var content = await reader.ReadToEndAsync();
|
||||
Assert.That(content, Is.EqualTo(fileContent));
|
||||
}
|
||||
|
||||
[Test]
|
||||
public void Given_EmptyUrl_When_LoadAsync_Should_Throw_FileNotFoundException()
|
||||
{
|
||||
// Act & Assert
|
||||
Assert.ThrowsAsync<FileNotFoundException>(async () => await _service.LoadAsync(_fixture.Create<string>()+".txt"));
|
||||
}
|
||||
|
||||
[Test]
|
||||
public async Task Given_ValidUrl_When_DeleteAsync_Then_FileIsDeleted()
|
||||
{
|
||||
// Arrange
|
||||
var date = DateTime.UtcNow.ToString("yyyy/MM");
|
||||
var corePath = Path.Combine(_tempDirectory, "uploads", date);
|
||||
Directory.CreateDirectory(corePath);
|
||||
|
||||
var fileName = _fixture.Create<string>() + ".txt";
|
||||
var fileContent = _fixture.Create<string>();
|
||||
var filePath = Path.Combine(corePath, fileName);
|
||||
await File.WriteAllTextAsync(filePath, fileContent);
|
||||
|
||||
// Act
|
||||
var url = Path.Combine(date, fileName);
|
||||
await _service.RemoveAsync(url);
|
||||
|
||||
Assert.That(File.Exists(filePath), Is.False);
|
||||
}
|
||||
|
||||
[Test]
|
||||
public async Task Given_InvalidUrl_When_DeleteAsync_TShould_Not_Throw()
|
||||
{
|
||||
// Act & Assert
|
||||
Assert.DoesNotThrowAsync(async () => await _service.RemoveAsync(_fixture.Create<string>()+".txt"));
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user