mirror of
https://github.com/Govor-team/Govor.git
synced 2026-07-21 19:54:55 +00:00
technical edits
This commit is contained in:
@@ -1,24 +1,35 @@
|
||||
using AutoMapper;
|
||||
using System.Text;
|
||||
using AutoMapper;
|
||||
using Govor.API.Controllers;
|
||||
using Govor.API.Hubs;
|
||||
using Govor.Application.Interfaces;
|
||||
using Govor.Application.Interfaces.Infrastructure.Extensions;
|
||||
using Govor.Application.Interfaces.Medias;
|
||||
using Govor.Application.Profiles;
|
||||
using Govor.Contracts.DTOs;
|
||||
using Govor.Contracts.Requests;
|
||||
using Govor.Core.Models;
|
||||
using Govor.Core.Models.Messages;
|
||||
using Govor.Data.Repositories.Exceptions;
|
||||
using Microsoft.AspNetCore.Http;
|
||||
using Microsoft.AspNetCore.Mvc;
|
||||
using Microsoft.AspNetCore.SignalR;
|
||||
using Microsoft.Extensions.Logging;
|
||||
using Moq;
|
||||
using Newtonsoft.Json.Linq;
|
||||
|
||||
namespace Govor.API.Tests;
|
||||
|
||||
[TestFixture]
|
||||
public class ProfileControllerTests
|
||||
{
|
||||
private Mock<ILogger<ProfileController>> _mockLogger = null!;
|
||||
private Mock<ILogger<ProfileController>> _mockLogger = null!;
|
||||
private Mock<IMediaService> _mockMediaService = null!;
|
||||
private Mock<IProfileService> _mockProfileService = null!;
|
||||
private Mock<ICurrentUserService> _mockCurrentUserService = null!;
|
||||
private Mock<IHubContext<ProfileHub>> _mockProfileHubContext = null!;
|
||||
private Mock<IHubClients> _mockClients = null!;
|
||||
private Mock<IClientProxy> _mockClientProxy = null!;
|
||||
private Mock<IMapper> _mockMapper = null!;
|
||||
private ProfileController _controller = null!;
|
||||
|
||||
@@ -32,18 +43,162 @@ public class ProfileControllerTests
|
||||
_mockProfileService = new Mock<IProfileService>();
|
||||
_mockCurrentUserService = new Mock<ICurrentUserService>();
|
||||
_mockMapper = new Mock<IMapper>();
|
||||
|
||||
_mockProfileHubContext = new Mock<IHubContext<ProfileHub>>();
|
||||
_mockClients = new Mock<IHubClients>();
|
||||
_mockClientProxy = new Mock<IClientProxy>();
|
||||
|
||||
_mockProfileHubContext.Setup(x => x.Clients).Returns(_mockClients.Object);
|
||||
_mockClients.Setup(x => x.All).Returns(_mockClientProxy.Object);
|
||||
_mockClientProxy.Setup(x => x.SendCoreAsync(It.IsAny<string>(), It.IsAny<object?[]?>(), It.IsAny<CancellationToken>()))
|
||||
.Returns(Task.CompletedTask);
|
||||
|
||||
_controller = new ProfileController(
|
||||
_mockLogger.Object,
|
||||
_mockMediaService.Object,
|
||||
_mockProfileService.Object,
|
||||
_mockCurrentUserService.Object,
|
||||
_mockProfileHubContext.Object,
|
||||
_mockMapper.Object);
|
||||
|
||||
_userId = Guid.NewGuid();
|
||||
_mockCurrentUserService.Setup(x => x.GetCurrentUserId()).Returns(_userId);
|
||||
}
|
||||
|
||||
[Test]
|
||||
public async Task UploadAvatar_ReturnsBadRequest_WhenFileIsEmpty()
|
||||
{
|
||||
// Arrange
|
||||
var request = new AvatarUploadRequest
|
||||
{
|
||||
FromFile = null
|
||||
};
|
||||
|
||||
// Act
|
||||
var result = await _controller.UploadAvatar(request);
|
||||
|
||||
// Assert
|
||||
Assert.That(result, Is.InstanceOf<BadRequestObjectResult>());
|
||||
var badRequestResult = (BadRequestObjectResult)result;
|
||||
Assert.That(badRequestResult.Value, Is.EqualTo("File is empty."));
|
||||
|
||||
_mockMediaService.Verify(s => s.UploadMediaAsync(It.IsAny<Media>()), Times.Never);
|
||||
}
|
||||
|
||||
[Test]
|
||||
public async Task UploadAvatar_ReturnsBadRequest_WhenFileLengthIsZero()
|
||||
{
|
||||
// Arrange
|
||||
var mockFile = new Mock<IFormFile>();
|
||||
mockFile.Setup(f => f.Length).Returns(0);
|
||||
|
||||
var request = new AvatarUploadRequest
|
||||
{
|
||||
FromFile = mockFile.Object
|
||||
};
|
||||
|
||||
// Act
|
||||
var result = await _controller.UploadAvatar(request);
|
||||
|
||||
// Assert
|
||||
Assert.That(result, Is.InstanceOf<BadRequestObjectResult>());
|
||||
var badRequestResult = (BadRequestObjectResult)result;
|
||||
Assert.That(badRequestResult.Value, Is.EqualTo("File is empty."));
|
||||
|
||||
_mockMediaService.Verify(s => s.UploadMediaAsync(It.IsAny<Media>()), Times.Never);
|
||||
}
|
||||
|
||||
[Test]
|
||||
public async Task UploadAvatar_ReturnsOkAndPerformsAllActions_WhenFileIsValid()
|
||||
{
|
||||
// Arrange
|
||||
var mediaId = Guid.NewGuid();
|
||||
var expectedMediaInfo = new MediaUploadResult(mediaId, $"/media/{mediaId}");
|
||||
|
||||
var fileName = "test_avatar.jpg";
|
||||
var fileContent = "This is a test image content";
|
||||
var stream = new MemoryStream(Encoding.UTF8.GetBytes(fileContent));
|
||||
|
||||
var mockFile = new Mock<IFormFile>();
|
||||
mockFile.Setup(f => f.FileName).Returns(fileName);
|
||||
mockFile.Setup(f => f.Length).Returns(stream.Length);
|
||||
mockFile.Setup(f => f.OpenReadStream()).Returns(stream);
|
||||
mockFile.Setup(f => f.CopyToAsync(It.IsAny<Stream>(), It.IsAny<CancellationToken>()))
|
||||
.Callback<Stream, CancellationToken>((s, t) => stream.CopyTo(s)); // Для ReadFileAsync
|
||||
|
||||
var request = new AvatarUploadRequest
|
||||
{
|
||||
FromFile = mockFile.Object,
|
||||
Type = MediaType.Image,
|
||||
MimeType = "image/jpeg"
|
||||
};
|
||||
|
||||
_mockMediaService.Setup(s => s.UploadMediaAsync(It.IsAny<Media>()))
|
||||
.ReturnsAsync(expectedMediaInfo);
|
||||
|
||||
_mockProfileService.Setup(s => s.SetNewIcon(_userId, mediaId))
|
||||
.Returns(Task.CompletedTask);
|
||||
|
||||
// Act
|
||||
var result = await _controller.UploadAvatar(request);
|
||||
|
||||
// Assert
|
||||
Assert.That(result, Is.InstanceOf<OkObjectResult>());
|
||||
var okResult = (OkObjectResult)result;
|
||||
Assert.That(okResult.StatusCode, Is.EqualTo(200));
|
||||
Assert.That(okResult.Value, Is.EqualTo(expectedMediaInfo));
|
||||
|
||||
mockFile.Verify(f => f.CopyToAsync(It.IsAny<Stream>(), It.IsAny<CancellationToken>()), Times.Once);
|
||||
|
||||
_mockMediaService.Verify(s => s.UploadMediaAsync(It.Is<Media>(m =>
|
||||
m.OwnerId == _userId &&
|
||||
m.OwnerType == MediaOwnerType.Avatar &&
|
||||
m.FileName == fileName
|
||||
)), Times.Once);
|
||||
|
||||
_mockProfileService.Verify(s => s.SetNewIcon(_userId, mediaId), Times.Once);
|
||||
|
||||
_mockClientProxy.Verify(
|
||||
c => c.SendCoreAsync("AvatarUpdated", It.Is<object?[]>(
|
||||
args => args.Length == 1 &&
|
||||
JObject.FromObject(args[0]!).Value<Guid>("userId") == _userId &&
|
||||
JObject.FromObject(args[0]!).Value<Guid>("iconId") == mediaId
|
||||
), It.IsAny<CancellationToken>()),
|
||||
Times.Once,
|
||||
"Hub SendAsync must be called to notify clients."
|
||||
);
|
||||
}
|
||||
|
||||
[Test]
|
||||
public async Task UploadAvatar_ReturnsInternalServerError_WhenMediaServiceFails()
|
||||
{
|
||||
// Arrange
|
||||
var mockFile = new Mock<IFormFile>();
|
||||
mockFile.Setup(f => f.Length).Returns(100);
|
||||
mockFile.Setup(f => f.OpenReadStream()).Returns(new MemoryStream(Encoding.UTF8.GetBytes("test")));
|
||||
|
||||
var request = new AvatarUploadRequest { FromFile = mockFile.Object };
|
||||
|
||||
_mockMediaService.Setup(s => s.UploadMediaAsync(It.IsAny<Media>()))
|
||||
.ThrowsAsync(new System.Exception("Simulated upload failure"));
|
||||
|
||||
// Act
|
||||
var result = await _controller.UploadAvatar(request);
|
||||
|
||||
// Assert
|
||||
Assert.That(result, Is.InstanceOf<ObjectResult>());
|
||||
var objectResult = (ObjectResult)result;
|
||||
Assert.That(objectResult.StatusCode, Is.EqualTo(500));
|
||||
|
||||
_mockProfileService.Verify(s => s.SetNewIcon(It.IsAny<Guid>(), It.IsAny<Guid>()), Times.Never);
|
||||
|
||||
_mockClientProxy.Verify(
|
||||
c => c.SendCoreAsync(It.IsAny<string>(), It.IsAny<object?[]?>(), It.IsAny<CancellationToken>()),
|
||||
Times.Never,
|
||||
"The Hub should not be called in case of a service error."
|
||||
);
|
||||
}
|
||||
|
||||
[Test]
|
||||
public async Task DowloadProfile_ReturnsOk_WhenProfileExists()
|
||||
{
|
||||
@@ -71,7 +226,7 @@ public class ProfileControllerTests
|
||||
.Returns(userProfileDto);
|
||||
|
||||
// Act
|
||||
var result = await _controller.DowloadProfile();
|
||||
var result = await _controller.DownloadProfile();
|
||||
|
||||
// Assert
|
||||
var okResult = result as OkObjectResult;
|
||||
@@ -80,22 +235,6 @@ public class ProfileControllerTests
|
||||
Assert.That(okResult.Value, Is.EqualTo(userProfileDto));
|
||||
}
|
||||
|
||||
[Test]
|
||||
public async Task DowloadProfile_ReturnsNotFound_WhenProfileIsNull()
|
||||
{
|
||||
// Arrange
|
||||
_mockProfileService.Setup(s => s.GetUserProfileAsync(_userId))
|
||||
.ReturnsAsync((UserProfile?)null);
|
||||
|
||||
// Act
|
||||
var result = await _controller.DowloadProfile();
|
||||
|
||||
// Assert
|
||||
var notFoundResult = result as NotFoundObjectResult;
|
||||
Assert.That(notFoundResult, Is.Not.Null);
|
||||
Assert.That(notFoundResult!.Value, Is.EqualTo("Profile not found"));
|
||||
}
|
||||
|
||||
[Test]
|
||||
public async Task DowloadProfile_ReturnsForbid_WhenUnauthorizedAccessExceptionThrown()
|
||||
{
|
||||
@@ -104,7 +243,7 @@ public class ProfileControllerTests
|
||||
.ThrowsAsync(new UnauthorizedAccessException("Access denied"));
|
||||
|
||||
// Act
|
||||
var result = await _controller.DowloadProfile();
|
||||
var result = await _controller.DownloadProfile();
|
||||
|
||||
// Assert
|
||||
Assert.That(result, Is.InstanceOf<ForbidResult>());
|
||||
@@ -115,15 +254,15 @@ public class ProfileControllerTests
|
||||
{
|
||||
// Arrange
|
||||
_mockProfileService.Setup(s => s.GetUserProfileAsync(_userId))
|
||||
.ThrowsAsync(new NotFoundException("User not found"));
|
||||
.ThrowsAsync(new NotFoundException("Cannot find profile"));
|
||||
|
||||
// Act
|
||||
var result = await _controller.DowloadProfile();
|
||||
var result = await _controller.DownloadProfile();
|
||||
|
||||
// Assert
|
||||
var badRequest = result as BadRequestObjectResult;
|
||||
Assert.That(badRequest, Is.Not.Null);
|
||||
Assert.That(badRequest!.Value, Is.EqualTo("User not found"));
|
||||
var notFoundResult = result as NotFoundObjectResult;
|
||||
Assert.That(notFoundResult, Is.Not.Null);
|
||||
Assert.That(notFoundResult!.Value, Is.EqualTo("Profile not found"));
|
||||
}
|
||||
|
||||
[Test]
|
||||
@@ -134,7 +273,7 @@ public class ProfileControllerTests
|
||||
.ThrowsAsync(new Exception("Unexpected error"));
|
||||
|
||||
// Act
|
||||
var result = await _controller.DowloadProfile();
|
||||
var result = await _controller.DownloadProfile();
|
||||
|
||||
// Assert
|
||||
var objectResult = result as ObjectResult;
|
||||
|
||||
Reference in New Issue
Block a user