new app server and database + added hub and controller of user profile

This commit is contained in:
Artemy
2025-11-04 11:41:06 +07:00
parent 1d442d037c
commit a5a5fc4758
62 changed files with 1760 additions and 2543 deletions
@@ -3,7 +3,6 @@ using Govor.API.Controllers;
using Govor.Application.Interfaces.Infrastructure.Extensions;
using Govor.Application.Interfaces.UserSession;
using Govor.Contracts.DTOs;
using Govor.Core.Models;
using Govor.Core.Models.Users;
using Govor.Data.Repositories.Exceptions;
using Microsoft.AspNetCore.Mvc;
@@ -18,6 +17,7 @@ public class SessionControllerTests
{
private Mock<ILogger<SessionController>> _loggerMock;
private Mock<ICurrentUserService> _currentUserServiceMock;
private Mock<ICurrentUserSessionService> _currentUserSessionServiceMock;
private Mock<IUserSessionReader> _userSessionReaderMock;
private Mock<IUserSessionRevoker> _userSessionRevokerMock;
private Mock<IMapper> _mapperMock;
@@ -28,17 +28,20 @@ public class SessionControllerTests
{
_loggerMock = new Mock<ILogger<SessionController>>();
_currentUserServiceMock = new Mock<ICurrentUserService>();
_currentUserSessionServiceMock = new Mock<ICurrentUserSessionService>();
_userSessionReaderMock = new Mock<IUserSessionReader>();
_userSessionRevokerMock = new Mock<IUserSessionRevoker>();
_mapperMock = new Mock<IMapper>();
_controller = new SessionController(
_loggerMock.Object,
_currentUserServiceMock.Object,
_currentUserSessionServiceMock.Object,
_userSessionReaderMock.Object,
_userSessionRevokerMock.Object,
_mapperMock.Object);
}
#region GetAllSessions
[Test]
public async Task GetAllSessions_Successful_ReturnsOkWithMappedSessions()
{
@@ -106,7 +109,9 @@ public class SessionControllerTests
exception,
It.IsAny<Func<It.IsAnyType, Exception, string>>()), Times.Once());
}
#endregion
#region CloseSession
[Test]
public async Task CloseSession_ValidSessionId_ReturnsOk()
{
@@ -213,7 +218,105 @@ public class SessionControllerTests
exception,
It.IsAny<Func<It.IsAnyType, Exception, string>>()), Times.Once());
}
#endregion
#region CloseCurrentSession
[Test]
public async Task CloseCurrentSession_ValidSessionIdAndUserId_ReturnsOk()
{
// Arrange
var userId = Guid.NewGuid();
var sessionId = Guid.NewGuid();
_currentUserServiceMock.Setup(s => s.GetCurrentUserId()).Returns(userId);
_currentUserSessionServiceMock.Setup(s => s.GetUserSessionId()).Returns(sessionId);
// Act
var result = await _controller.CloseCurrent();
// Assert
Assert.That(result, Is.TypeOf<OkResult>());
_userSessionRevokerMock.Verify(r => r.CloseSessionByIdAsync(sessionId, userId), Times.Once());
_loggerMock.VerifyNoOtherCalls();
}
[Test]
public async Task CloseCurrentSession_UnauthorizedAccess_ReturnsForbid()
{
// Arrange
var userId = Guid.NewGuid();
var sessionId = Guid.NewGuid();
var exception = new UnauthorizedAccessException("Unauthorized");
_currentUserServiceMock.Setup(s => s.GetCurrentUserId()).Returns(userId);
_currentUserSessionServiceMock.Setup(s => s.GetUserSessionId()).Returns(sessionId);
_userSessionRevokerMock.Setup(r => r.CloseSessionByIdAsync(sessionId, userId)).ThrowsAsync(exception);
// Act
var result = await _controller.CloseCurrent();
// Assert
Assert.That(result, Is.TypeOf<ForbidResult>());
_loggerMock.Verify(l => l.Log(
LogLevel.Warning,
It.IsAny<EventId>(),
It.IsAny<It.IsAnyType>(),
exception,
It.IsAny<Func<It.IsAnyType, Exception, string>>()), Times.Once());
}
[Test]
public async Task CloseCurrentSession_NotFound_ReturnsNotFound()
{
// Arrange
var userId = Guid.NewGuid();
var sessionId = Guid.NewGuid();
var exception = new NotFoundException("Session not found");
_currentUserServiceMock.Setup(s => s.GetCurrentUserId()).Returns(userId);
_currentUserSessionServiceMock.Setup(s => s.GetUserSessionId()).Returns(sessionId);
_userSessionRevokerMock.Setup(r => r.CloseSessionByIdAsync(sessionId, userId)).ThrowsAsync(exception);
// Act
var result = await _controller.CloseCurrent();
// Assert
Assert.That(result, Is.TypeOf<NotFoundObjectResult>());
var notFoundResult = result as NotFoundObjectResult;
Assert.That(notFoundResult.Value, Is.EqualTo("Session not found"));
_loggerMock.Verify(l => l.Log(
LogLevel.Warning,
It.IsAny<EventId>(),
It.IsAny<It.IsAnyType>(),
exception,
It.IsAny<Func<It.IsAnyType, Exception, string>>()), Times.Once());
}
[Test]
public async Task CloseCurrentSession_InvalidOperation_ReturnsBadRequest()
{
// Arrange
var userId = Guid.NewGuid();
var sessionId = Guid.NewGuid();
var exception = new InvalidOperationException("Invalid operation");
_currentUserServiceMock.Setup(s => s.GetCurrentUserId()).Returns(userId);
_currentUserSessionServiceMock.Setup(s => s.GetUserSessionId()).Returns(sessionId);
_userSessionRevokerMock.Setup(r => r.CloseSessionByIdAsync(sessionId, userId)).ThrowsAsync(exception);
// Act
var result = await _controller.CloseCurrent();
// Assert
Assert.That(result, Is.TypeOf<BadRequestObjectResult>());
var badRequestResult = result as BadRequestObjectResult;
Assert.That(badRequestResult.Value, Is.EqualTo("Invalid operation"));
_loggerMock.Verify(l => l.Log(
LogLevel.Error,
It.IsAny<EventId>(),
It.IsAny<It.IsAnyType>(),
exception,
It.IsAny<Func<It.IsAnyType, Exception, string>>()), Times.Once());
}
#endregion
#region CloseAllSessions
[Test]
public async Task CloseAllSessions_Successful_ReturnsOk()
{
@@ -276,4 +379,5 @@ public class SessionControllerTests
exception,
It.IsAny<Func<It.IsAnyType, Exception, string>>()), Times.Once());
}
#endregion
}
+2
View File
@@ -16,6 +16,8 @@
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="17.11.1" />
<PackageReference Include="Moq" Version="4.20.72" />
<PackageReference Include="NUnit" Version="4.4.0-beta.1" />
<PackageReference Include="NUnit.Analyzers" Version="4.4.0"/>
<PackageReference Include="NUnit3TestAdapter" Version="4.6.0"/>
</ItemGroup>
@@ -0,0 +1,145 @@
using AutoMapper;
using Govor.API.Controllers;
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.Data.Repositories.Exceptions;
using Microsoft.AspNetCore.Mvc;
using Microsoft.Extensions.Logging;
using Moq;
namespace Govor.API.Tests;
[TestFixture]
public class ProfileControllerTests
{
private Mock<ILogger<ProfileController>> _mockLogger = null!;
private Mock<IMediaService> _mockMediaService = null!;
private Mock<IProfileService> _mockProfileService = null!;
private Mock<ICurrentUserService> _mockCurrentUserService = null!;
private Mock<IMapper> _mockMapper = null!;
private ProfileController _controller = null!;
private Guid _userId;
[SetUp]
public void SetUp()
{
_mockLogger = new Mock<ILogger<ProfileController>>();
_mockMediaService = new Mock<IMediaService>();
_mockProfileService = new Mock<IProfileService>();
_mockCurrentUserService = new Mock<ICurrentUserService>();
_mockMapper = new Mock<IMapper>();
_controller = new ProfileController(
_mockLogger.Object,
_mockMediaService.Object,
_mockProfileService.Object,
_mockCurrentUserService.Object,
_mockMapper.Object);
_userId = Guid.NewGuid();
_mockCurrentUserService.Setup(x => x.GetCurrentUserId()).Returns(_userId);
}
[Test]
public async Task DowloadProfile_ReturnsOk_WhenProfileExists()
{
// Arrange
var userProfile = new UserProfile
{
Id = _userId,
Username = "test_user",
Description = "test description",
IconId = Guid.NewGuid()
};
var userProfileDto = new UserProfileDto
{
Id = _userId,
Username = "test_user",
Description = "test description",
IconId = userProfile.IconId
};
_mockProfileService.Setup(s => s.GetUserProfileAsync(_userId))
.ReturnsAsync(userProfile);
_mockMapper.Setup(m => m.Map<UserProfileDto>(userProfile))
.Returns(userProfileDto);
// Act
var result = await _controller.DowloadProfile();
// Assert
var okResult = result as OkObjectResult;
Assert.That(okResult, Is.Not.Null);
Assert.That(okResult!.StatusCode, Is.EqualTo(200));
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()
{
// Arrange
_mockProfileService.Setup(s => s.GetUserProfileAsync(_userId))
.ThrowsAsync(new UnauthorizedAccessException("Access denied"));
// Act
var result = await _controller.DowloadProfile();
// Assert
Assert.That(result, Is.InstanceOf<ForbidResult>());
}
[Test]
public async Task DowloadProfile_ReturnsBadRequest_WhenNotFoundExceptionThrown()
{
// Arrange
_mockProfileService.Setup(s => s.GetUserProfileAsync(_userId))
.ThrowsAsync(new NotFoundException("User not found"));
// Act
var result = await _controller.DowloadProfile();
// Assert
var badRequest = result as BadRequestObjectResult;
Assert.That(badRequest, Is.Not.Null);
Assert.That(badRequest!.Value, Is.EqualTo("User not found"));
}
[Test]
public async Task DowloadProfile_Returns500_WhenUnexpectedExceptionThrown()
{
// Arrange
_mockProfileService.Setup(s => s.GetUserProfileAsync(_userId))
.ThrowsAsync(new Exception("Unexpected error"));
// Act
var result = await _controller.DowloadProfile();
// Assert
var objectResult = result as ObjectResult;
Assert.That(objectResult, Is.Not.Null);
Assert.That(objectResult!.StatusCode, Is.EqualTo(500));
Assert.That(objectResult.Value, Is.EqualTo("Unexpected Error! Please try again later."));
}
}