rework messages

+ addition LocalStorageService
+ tests
This commit is contained in:
Artemy
2025-06-21 14:53:15 +07:00
parent 3f1c050149
commit 7831e4838a
16 changed files with 204 additions and 13 deletions
@@ -3,7 +3,7 @@ using Govor.API.Services.Authentication;
using Govor.Core.Infrastructure.Extensions;
using Govor.Core.Models;
using Govor.Core.Repositories.Users;
using Govor.Core.Services;
using Govor.API.Services.Authentication.Interfaces;
using Moq;
namespace Govor.API.Tests.UnitTests.Services;
@@ -0,0 +1,78 @@
using AutoFixture;
using Govor.API.Services;
using Microsoft.AspNetCore.Hosting;
using Moq;
namespace Govor.API.Tests.UnitTests.Services;
[TestFixture]
public class LocalStorageServiceTests
{
private Fixture _fixture;
private IStorageService _service;
private Mock<IWebHostEnvironment> _webHostEnvironmentMock;
private string _tempDirectory;
[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);
}
[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, $"Файл не найден по пути: {expectedFilePath}");
}
[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));
}
}