/
softailor
/
gridient
Обзор
Документация
Войти
/
softailor
/
gridient
Код
Запросы
0
Задачи
Вики
Пакеты
0
Релизы
0
CI/CD
Аналитика
Безопасность
master
backend/Gridient.Core.Tests/Controllers/AttachmentControllerTests.cs
194 строки
9 KB
Jenkins
Auto add
18 фев 2026, 10:19
18 фев 2026, 10:19
1e6c4f8
Код
Авторство
О чём код?
using FluentAssertions; using Gridient.Core.Controllers; using Gridient.Core.Features.Attachments.Commands; using Gridient.Core.Features.Attachments.Queries; using Gridient.Core.Models; using Gridient.Core.Options; using MediatR; using Microsoft.AspNetCore.Http; using Microsoft.AspNetCore.Mvc; using Microsoft.Extensions.Options; using Moq; using Xunit; namespace Gridient.Core.Tests.Controllers { public class AttachmentControllerTests : ControllerTestBase { #region UploadFile /// <summary> /// Проверяет, что UploadFile возвращает 400, если файл пустой. /// </summary> [Fact] public async Task UploadFile_WhenEmpty_ShouldReturn400() { var mediatorMock = new Mock<IMediator>(); var options = Microsoft.Extensions.Options.Options.Create(new AttachmentOptions { MaxFileMbSize = 1 }); var controller = new AttachmentController(mediatorMock.Object, options); SetupControllerContext(controller, "u"); var emptyFile = new FormFile(new MemoryStream(Array.Empty<byte>()), 0, 0, "file", "a.txt"); var result = await controller.UploadFile(1, 1, emptyFile, CancellationToken.None); result.Should().BeOfType<ObjectResult>(); var obj = (ObjectResult)result; obj.StatusCode.Should().Be(StatusCodes.Status400BadRequest); mediatorMock.Verify(x => x.Send(It.IsAny<UploadFileCommand>(), It.IsAny<CancellationToken>()), Times.Never); } /// <summary> /// Проверяет, что UploadFile возвращает 413, если файл превышает MaxFileSize. /// </summary> [Fact] public async Task UploadFile_WhenTooLarge_ShouldReturn413() { var mediatorMock = new Mock<IMediator>(); var options = Microsoft.Extensions.Options.Options.Create(new AttachmentOptions { MaxFileMbSize = 1 }); // max 1MB var controller = new AttachmentController(mediatorMock.Object, options); SetupControllerContext(controller, "u"); var bytes = new byte[options.Value.MaxFileSize + 1]; var file = new FormFile(new MemoryStream(bytes), 0, bytes.Length, "file", "a.bin"); var result = await controller.UploadFile(1, 1, file, CancellationToken.None); result.Should().BeOfType<ObjectResult>(); var obj = (ObjectResult)result; obj.StatusCode.Should().Be(StatusCodes.Status413PayloadTooLarge); mediatorMock.Verify(x => x.Send(It.IsAny<UploadFileCommand>(), It.IsAny<CancellationToken>()), Times.Never); } /// <summary> /// Проверяет, что UploadFile возвращает OkObjectResult с id загруженного файла. /// </summary> [Fact] public async Task UploadFile_WhenValid_ShouldReturnOkWithId() { var mediatorMock = new Mock<IMediator>(); mediatorMock .Setup(x => x.Send(It.Is<UploadFileCommand>(c => c.ListId == 1 && c.ItemId == 1 && c.FileName == "a.txt"), It.IsAny<CancellationToken>())) .ReturnsAsync(123); var options = Microsoft.Extensions.Options.Options.Create(new AttachmentOptions { MaxFileMbSize = 1 }); var controller = new AttachmentController(mediatorMock.Object, options); SetupControllerContext(controller, "u"); var bytes = new byte[] { 1, 2, 3 }; var file = new FormFile(new MemoryStream(bytes), 0, bytes.Length, "file", "a.txt"); var result = await controller.UploadFile(1, 1, file, CancellationToken.None); result.Should().BeOfType<OkObjectResult>(); ((OkObjectResult)result).Value.Should().Be(123); mediatorMock.Verify(x => x.Send(It.Is<UploadFileCommand>(c => c.ListId == 1 && c.ItemId == 1 && c.FileName == "a.txt"), It.IsAny<CancellationToken>()), Times.Once); } #endregion #region GetFilesName /// <summary> /// Проверяет, что GetFilesName возвращает OkObjectResult со списком вложений. /// </summary> [Fact] public async Task GetFilesName_ShouldReturnOkWithAttachments() { var mediatorMock = new Mock<IMediator>(); var expected = new[] { new Attachment { Id = 1, Name = "a", Content = Array.Empty<byte>() }, new Attachment { Id = 2, Name = "b", Content = Array.Empty<byte>() } }; mediatorMock .Setup(x => x.Send(It.Is<GetAttachmentNamesQuery>(q => q.ListId == 1 && q.ItemId == 2), It.IsAny<CancellationToken>())) .ReturnsAsync(expected); var controller = new AttachmentController(mediatorMock.Object, Microsoft.Extensions.Options.Options.Create(new AttachmentOptions { MaxFileMbSize = 1 })); SetupControllerContext(controller, "u"); var result = await controller.GetFilesName(1, 2, new[] { "1", "2" }, CancellationToken.None); result.Should().BeOfType<OkObjectResult>(); ((OkObjectResult)result).Value.Should().BeEquivalentTo(expected); mediatorMock.Verify(x => x.Send(It.Is<GetAttachmentNamesQuery>(q => q.ListId == 1 && q.ItemId == 2), It.IsAny<CancellationToken>()), Times.Once); } #endregion #region GetDocument /// <summary> /// Проверяет, что GetDocument возвращает NotFoundResult, если вложение не найдено. /// </summary> [Fact] public async Task GetDocument_WhenNotFound_ShouldReturnNotFound() { var mediatorMock = new Mock<IMediator>(); mediatorMock .Setup(x => x.Send(It.Is<GetDocumentQuery>(q => q.ListId == 1 && q.ItemId == 2 && q.AttachmentId == 3), It.IsAny<CancellationToken>())) .ReturnsAsync((Attachment?)null); var controller = new AttachmentController(mediatorMock.Object, Microsoft.Extensions.Options.Options.Create(new AttachmentOptions { MaxFileMbSize = 1 })); SetupControllerContext(controller, "u"); var result = await controller.GetDocument(1, 2, 3, CancellationToken.None); result.Should().BeOfType<NotFoundResult>(); mediatorMock.Verify(x => x.Send(It.Is<GetDocumentQuery>(q => q.ListId == 1 && q.ItemId == 2 && q.AttachmentId == 3), It.IsAny<CancellationToken>()), Times.Once); } /// <summary> /// Проверяет, что GetDocument возвращает FileContentResult, если вложение найдено. /// </summary> [Fact] public async Task GetDocument_WhenExists_ShouldReturnFile() { var mediatorMock = new Mock<IMediator>(); var attachment = new Attachment { Id = 3, Name = "a.bin", Content = new byte[] { 9 } }; mediatorMock .Setup(x => x.Send(It.Is<GetDocumentQuery>(q => q.ListId == 1 && q.ItemId == 2 && q.AttachmentId == 3), It.IsAny<CancellationToken>())) .ReturnsAsync(attachment); var controller = new AttachmentController(mediatorMock.Object, Microsoft.Extensions.Options.Options.Create(new AttachmentOptions { MaxFileMbSize = 1 })); SetupControllerContext(controller, "u"); var result = await controller.GetDocument(1, 2, 3, CancellationToken.None); result.Should().BeOfType<FileContentResult>(); var file = (FileContentResult)result; file.FileDownloadName.Should().Be("a.bin"); file.ContentType.Should().Be("application/octet-stream"); file.FileContents.Should().BeEquivalentTo(new byte[] { 9 }); mediatorMock.Verify(x => x.Send(It.Is<GetDocumentQuery>(q => q.ListId == 1 && q.ItemId == 2 && q.AttachmentId == 3), It.IsAny<CancellationToken>()), Times.Once); } #endregion #region Delete /// <summary> /// Проверяет, что Delete вызывает DeleteAttachmentCommand и возвращает OkResult. /// </summary> [Fact] public async Task Delete_ShouldReturnOk() { var mediatorMock = new Mock<IMediator>(); mediatorMock .Setup(x => x.Send(It.Is<DeleteAttachmentCommand>(c => c.ListId == 1 && c.ItemId == 2 && c.AttachmentId == 3), It.IsAny<CancellationToken>())) .Returns(Task.CompletedTask); var controller = new AttachmentController(mediatorMock.Object, Microsoft.Extensions.Options.Options.Create(new AttachmentOptions { MaxFileMbSize = 1 })); SetupControllerContext(controller, "u"); var result = await controller.Delete(1, 2, 3, CancellationToken.None); result.Should().BeOfType<OkResult>(); mediatorMock.Verify(x => x.Send(It.Is<DeleteAttachmentCommand>(c => c.ListId == 1 && c.ItemId == 2 && c.AttachmentId == 3), It.IsAny<CancellationToken>()), Times.Once); } #endregion } }