Refactor message service and move tests to Application.Tests

Renamed IMessageService to IMessageCommandService and updated all usages accordingly. Moved and renamed test files from Govor.API.Tests to the new Govor.Application.Tests project, updating namespaces to match. Refactored message-related services and controllers to use the new naming and structure. Added Govor.Application.Tests project to the solution.
This commit is contained in:
Artemy
2025-07-10 18:05:10 +07:00
parent a43a26b307
commit 65a43c09d3
33 changed files with 157 additions and 45 deletions
-1
View File
@@ -27,5 +27,4 @@
<ProjectReference Include="..\Govor.API\Govor.API.csproj" />
<ProjectReference Include="..\Govor.Data\Govor.Data.csproj" />
</ItemGroup>
</Project>
@@ -11,7 +11,7 @@ namespace Govor.API.Tests.IntegrationTests.Hubs;
public class ChatsHubTests
{
private Mock<ILogger<ChatsHub>> _loggerMock;
private Mock<IMessageService> _messageServiceMock;
private Mock<IMessageCommandService> _messageServiceMock;
private Mock<IUserGroupsService> _userGroupsServiceMock;
private Fixture _fixture;
private ChatsHub _chatsHub;
@@ -23,7 +23,7 @@ public class ChatsHubTests
_fixture.Behaviors.OfType<ThrowingRecursionBehavior>().ToList().ForEach(b => _fixture.Behaviors.Remove(b));
_fixture.Behaviors.Add(new OmitOnRecursionBehavior());
_messageServiceMock = new Mock<IMessageService>();
_messageServiceMock = new Mock<IMessageCommandService>();
_userGroupsServiceMock = new Mock<IUserGroupsService>();
_loggerMock = new Mock<ILogger<ChatsHub>>();
@@ -4,7 +4,7 @@ using Govor.Application.Services;
using Microsoft.AspNetCore.Hosting;
using Moq;
namespace Govor.API.Tests.UnitTests.Services;
namespace Govor.Application.Tests.Services;
[TestFixture]
public class LocalStorageServiceTests
@@ -1,3 +1,5 @@
using Govor.Application.Interfaces.Friends;
using Govor.Application.Interfaces.Infrastructure.Extensions;
using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Mvc;
@@ -8,5 +10,18 @@ namespace Govor.API.Controllers.Friends;
[Route("api/friends")]
public class FriendsRequestQueryController : Controller
{
private readonly ILogger<FriendsController> _logger;
private readonly IFriendRequestQueryService _friendsService;
private readonly ICurrentUserService _currentUserService;
public FriendsRequestQueryController(ILogger<FriendsController> logger,
IFriendRequestQueryService friendsService,
ICurrentUserService currentUserService)
{
_logger = logger;
_friendsService = friendsService;
_currentUserService = currentUserService;
}
}
@@ -1,6 +1,59 @@
using Govor.Application.Exceptions.FriendsService;
using Govor.Application.Interfaces.Friends;
using Govor.Application.Interfaces.Infrastructure.Extensions;
using Govor.Contracts.DTOs;
using Govor.Core.Models;
using Microsoft.AspNetCore.Mvc;
namespace Govor.API.Controllers.Friends;
public class FriendshipController
[Route("api/friends")]
public class FriendshipController : Controller
{
private readonly ILogger<FriendsController> _logger;
private readonly IFriendshipService _friendsService;
//private readonly IUserDtoBuilder _builder;
private readonly ICurrentUserService _currentUserService;
public FriendshipController(ILogger<FriendsController> logger,
IFriendshipService friendsService,
ICurrentUserService currentUserService)
{
_logger = logger;
_friendsService = friendsService;
_currentUserService = currentUserService;
}
[HttpGet("search")] // api/friends/search?query=
public async Task<IActionResult> Search(string query)
{
if (string.IsNullOrWhiteSpace(query))
return BadRequest("Query cannot be empty");
try
{
var result = await _friendsService.SearchUsersAsync(query, _currentUserService.GetCurrentUserId());
return Ok(BuildUserDtos(result));
}
catch (SearchUsersException ex)
{
_logger.LogWarning(ex, ex.Message);
return NotFound(new { error = ex.Message });
}
catch (Exception ex)
{
_logger.LogError(ex, ex.Message);
return StatusCode(500, new { error = "Internal error during user search." });
}
}
private List<UserDto> BuildUserDtos(IEnumerable<User> users) => users.Select(user => new UserDto
{
Id = user.Id,
Username = user.Username,
Description = user.Description,
WasOnline = user.WasOnline,
IconId = user.IconId
}).ToList();
}
@@ -1,5 +1,6 @@
using Govor.API.Services.AdminsStuff.Interfaces;
using Govor.API.Services.Authentication.Interfaces;
using Govor.Application.Infrastructure.AdminsStuff;
using Govor.Application.Infrastructure.Extensions;
using Govor.Application.Infrastructure.Validators;
using Govor.Application.Interfaces;
@@ -9,7 +10,9 @@ using Govor.Application.Interfaces.Friends;
using Govor.Application.Interfaces.Infrastructure.Extensions;
using Govor.Application.Interfaces.Messages;
using Govor.Application.Services;
using Govor.Application.Services.Authentication;
using Govor.Application.Services.Friends;
using Govor.Application.Services.Messages;
using Govor.Core.Infrastructure.Extensions;
using Govor.Core.Infrastructure.Validators;
using Govor.Core.Models;
@@ -57,7 +60,7 @@ public static class ConfigurationProgramExtensions
services.AddMemoryCache();
services.AddScoped<IPingHandlerService, PingHandlerService>();
services.AddScoped<IMessageService, MessageService>();
services.AddScoped<IMessageCommandService, MessageCommandService>();
services.AddScoped<IVerifyFriendship, VerifyFriendship>();
services.AddScoped<IUserGroupsService, UserGroupsService>();
services.AddScoped<IMessagesLoader, MessagesLoader>();
+4 -4
View File
@@ -14,13 +14,13 @@ namespace Govor.API.Hubs;
public class ChatsHub : Hub
{
private readonly ILogger<ChatsHub> _logger;
private readonly IMessageService _messageService;
private readonly IMessageCommandService _messageCommandService;
private readonly IUserGroupsService _userService;
public ChatsHub(ILogger<ChatsHub> logger, IMessageService messageService, IUserGroupsService userService)
public ChatsHub(ILogger<ChatsHub> logger, IMessageCommandService messageCommandService, IUserGroupsService userService)
{
_logger = logger;
_messageService = messageService;
_messageCommandService = messageCommandService;
_userService = userService;
}
@@ -105,7 +105,7 @@ public class ChatsHub : Hub
Media: request.MediaAttachments?.Select(f => new SendMedia(f.MediaId, f.EncryptedKey)) ??
Array.Empty<SendMedia>());
var result = await _messageService.SendMessageAsync(sendMessageParams);
var result = await _messageCommandService.SendMessageAsync(sendMessageParams);
if (!result.IsSuccess || result.Message.Id == Guid.Empty)
{
+2 -2
View File
@@ -1,7 +1,7 @@
using System.Text;
using Govor.API.Extensions;
using Govor.API.Hubs;
using Govor.Application.Services;
using Govor.Application.Services.Authentication;
using Microsoft.AspNetCore.Authentication.JwtBearer;
using Microsoft.IdentityModel.Tokens;
using Microsoft.OpenApi.Models;
@@ -19,7 +19,7 @@ builder.Services.AddCors(options =>
{
options.AddPolicy("AllowFrontend", policy =>
{
policy.WithOrigins("http://localhost:5000", "https://5.129.212.144:5000") // Укажите ваш публичный IP
policy.WithOrigins("http://localhost:5000", "https://5.129.212.144:5000")
.AllowAnyHeader()
.AllowAnyMethod()
.AllowCredentials();
@@ -0,0 +1,33 @@
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<TargetFramework>net8.0</TargetFramework>
<LangVersion>latest</LangVersion>
<ImplicitUsings>enable</ImplicitUsings>
<Nullable>enable</Nullable>
<IsPackable>false</IsPackable>
</PropertyGroup>
<ItemGroup>
<PackageReference Include="AutoFixture" Version="5.0.0-preview0012" />
<PackageReference Include="coverlet.collector" Version="6.0.2" />
<PackageReference Include="Microsoft.EntityFrameworkCore" Version="8.0.6" />
<PackageReference Include="Microsoft.EntityFrameworkCore.InMemory" Version="8.0.6" />
<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" />
</ItemGroup>
<ItemGroup>
<Using Include="NUnit.Framework"/>
</ItemGroup>
<ItemGroup>
<ProjectReference Include="..\Govor.Application\Govor.Application.csproj" />
</ItemGroup>
<ItemGroup>
<Folder Include="Infrastructure\Validators\" />
</ItemGroup>
</Project>
@@ -1,11 +1,11 @@
using AutoFixture;
using Govor.API.Services.AdminsStuff.Interfaces;
using Govor.Application.Interfaces.AdminsStuff;
using Govor.Application.Infrastructure.AdminsStuff;
using Govor.Core.Models;
using Govor.Core.Repositories.Invaites;
using Moq;
namespace Govor.API.Tests.UnitTests.Services.AdminStuff;
namespace Govor.Application.Tests.Infrastructure.AdminsStuff;
[TestFixture]
public class InvitationGeneratorTests
@@ -3,7 +3,7 @@ using Govor.Application.Infrastructure.Extensions;
using Microsoft.AspNetCore.Http;
using Moq;
namespace Govor.API.Tests.UnitTests.Services;
namespace Govor.Application.Tests.Infrastructure.Extensions;
[TestFixture]
public class CurrentUserServiceTests
@@ -6,10 +6,11 @@ using Govor.API.Services.Authentication.Interfaces;
using Govor.Application.Exceptions.AuthService;
using Govor.Application.Interfaces.Authentication;
using Govor.Application.Services;
using Govor.Application.Services.Authentication;
using Govor.Core.Repositories.Admins;
using Moq;
namespace Govor.API.Tests.UnitTests.Services.Authentication;
namespace Govor.Application.Tests.Services.Authentication;
[TestFixture]
public class AuthServiceTests
@@ -2,11 +2,12 @@ using System.IdentityModel.Tokens.Jwt;
using AutoFixture;
using Govor.API.Services.Authentication.Interfaces;
using Govor.Application.Services;
using Govor.Application.Services.Authentication;
using Govor.Core.Models;
using Microsoft.Extensions.Options;
using Moq;
namespace Govor.API.Tests.UnitTests.Services.Authentication;
namespace Govor.Application.Tests.Services.Authentication;
[TestFixture]
public class JwtServiceTests
@@ -8,7 +8,7 @@ using Govor.Core.Repositories.Users;
using Govor.Data.Repositories.Exceptions;
using Moq;
namespace Govor.API.Tests.UnitTests.Services.Friends;
namespace Govor.Application.Tests.Services.Friends;
[TestFixture]
public class FriendRequestCommandServiceTests
@@ -7,7 +7,7 @@ using Govor.Core.Repositories.Users;
using Govor.Data.Repositories.Exceptions;
using Moq;
namespace Govor.API.Tests.UnitTests.Services.Friends;
namespace Govor.Application.Tests.Services.Friends;
[TestFixture]
public class FriendRequestQueryServiceTests
@@ -8,7 +8,7 @@ using Govor.Core.Repositories.Users;
using Govor.Data.Repositories.Exceptions;
using Moq;
namespace Govor.API.Tests.UnitTests.Services.Friends;
namespace Govor.Application.Tests.Services.Friends;
[TestFixture]
public class FriendshipServiceTests
@@ -1,7 +1,7 @@
using Govor.Application.Exceptions.VerifyFriendship;
using Govor.Application.Interfaces;
using Govor.Application.Interfaces.Messages.Parameters;
using Govor.Application.Services;
using Govor.Application.Services.Messages;
using Govor.Core.Models;
using Govor.Core.Repositories.Groups;
using Govor.Core.Repositories.Messages;
@@ -11,18 +11,18 @@ using Govor.Data.Repositories.Exceptions;
using Microsoft.Extensions.Logging;
using Moq;
namespace Govor.API.Tests.UnitTests.Services;
namespace Govor.Application.Tests.Services.Messages;
[TestFixture]
public class MessageServiceTests
public class MessageCommandServiceTests
{
private Mock<IMessagesRepository> _mockMessagesRepo;
private Mock<IUsersRepository> _mockUsersRepo;
private Mock<IGroupsRepository> _mockGroupsRepo;
private Mock<IVerifyFriendship> _mockVerifyFriendship;
private Mock<IPrivateChatsRepository> _mockPrivateChats;
private Mock<ILogger<MessageService>> _mockLogger;
private MessageService _messageService;
private Mock<ILogger<MessageCommandService>> _mockLogger;
private MessageCommandService _messageService;
[SetUp]
public void SetUp()
@@ -32,9 +32,9 @@ public class MessageServiceTests
_mockGroupsRepo = new Mock<IGroupsRepository>();
_mockVerifyFriendship = new Mock<IVerifyFriendship>();
_mockPrivateChats = new Mock<IPrivateChatsRepository>();
_mockLogger = new Mock<ILogger<MessageService>>();
_mockLogger = new Mock<ILogger<MessageCommandService>>();
_messageService = new MessageService(
_messageService = new MessageCommandService(
_mockMessagesRepo.Object,
_mockUsersRepo.Object,
_mockGroupsRepo.Object,
@@ -1,8 +1,8 @@
using AutoFixture;
using Govor.Application.Services;
using Govor.Application.Services.Authentication;
using Govor.Core.Infrastructure.Extensions;
namespace Govor.API.Tests.UnitTests.Services;
namespace Govor.Application.Tests.Services;
[TestFixture]
public class PasswordHasherTests
@@ -4,7 +4,7 @@ using Govor.Data;
using Microsoft.EntityFrameworkCore;
using Microsoft.Extensions.Caching.Memory;
namespace Govor.API.Tests.UnitTests.Services;
namespace Govor.Application.Tests.Services;
[TestFixture]
public class PingHandlerServiceTests
@@ -6,7 +6,7 @@ using Govor.Core.Repositories.Groups;
using Govor.Data.Repositories.Exceptions;
using Moq;
namespace Govor.API.Tests.UnitTests.Services;
namespace Govor.Application.Tests.Services;
[TestFixture]
public class UserGroupsServiceTests
@@ -7,7 +7,7 @@ using Govor.Data.Repositories.Exceptions;
using Microsoft.Extensions.Logging;
using Moq;
namespace Govor.API.Tests.UnitTests.Services;
namespace Govor.Application.Tests.Services;
[TestFixture]
public class VerifyFriendshipTests
@@ -2,7 +2,7 @@ using Govor.API.Services.AdminsStuff.Interfaces;
using Govor.Core.Models;
using Govor.Core.Repositories.Invaites;
namespace Govor.Application.Interfaces.AdminsStuff;
namespace Govor.Application.Infrastructure.AdminsStuff;
public class InvitationGenerator(IInvitesRepository repository) : IInvitationGenerator
{
@@ -4,7 +4,7 @@ using Govor.Core.Models;
namespace Govor.Application.Interfaces.Messages;
// Combining IChatService and IGroupService functionalities relevant to messages
public interface IMessageService
public interface IMessageCommandService
{
Task<SendMessageResult> SendMessageAsync(SendMessage messageParameters);
Task<EditMessageResult> EditMessageAsync(EditMessage messageParameters);
@@ -8,7 +8,7 @@ using Govor.Application.Interfaces.Authentication;
using Govor.Core.Infrastructure.Validators;
using Govor.Core.Repositories.Admins;
namespace Govor.Application.Services;
namespace Govor.Application.Services.Authentication;
public class AuthService : IAccountService
{
@@ -1,4 +1,4 @@
namespace Govor.Application.Services;
namespace Govor.Application.Services.Authentication;
public class JwtOption
{
public string SecretKeу {get; set;}
@@ -6,7 +6,7 @@ using Govor.Core.Models;
using Microsoft.Extensions.Options;
using Microsoft.IdentityModel.Tokens;
namespace Govor.Application.Services;
namespace Govor.Application.Services.Authentication;
public class JwtService : IJwtService
{
@@ -1,6 +1,6 @@
using Govor.Core.Infrastructure.Extensions;
namespace Govor.Application.Services;
namespace Govor.Application.Services.Authentication;
public class PasswordHasher : IPasswordHasher
{
@@ -1,7 +1,7 @@
using Govor.Application.Interfaces;
using Govor.Application.Interfaces.Medias;
namespace Govor.Application.Services;
namespace Govor.Application.Services.Messages;
public class MediaService : IMediaService
{
@@ -8,24 +8,24 @@ using Govor.Core.Repositories.PrivateChats;
using Govor.Core.Repositories.Users;
using Microsoft.Extensions.Logging;
namespace Govor.Application.Services;
namespace Govor.Application.Services.Messages;
public class MessageService : IMessageService
public class MessageCommandService : IMessageCommandService
{
private readonly IMessagesRepository _messagesRepository;
private readonly IUsersRepository _usersRepository; // For validating user recipients
private readonly IGroupsRepository _groupsRepository; // For validating group recipients and fetching members
private readonly IPrivateChatsRepository _privateChats;
private readonly IVerifyFriendship _verifyFriendship; // For private messages
private readonly ILogger<MessageService> _logger;
private readonly ILogger<MessageCommandService> _logger;
public MessageService(
public MessageCommandService(
IMessagesRepository messagesRepository,
IUsersRepository usersRepository,
IGroupsRepository groupsRepository,
IVerifyFriendship verifyFriendship,
IPrivateChatsRepository privateChats,
ILogger<MessageService> logger)
ILogger<MessageCommandService> logger)
{
_messagesRepository = messagesRepository;
_usersRepository = usersRepository;
@@ -4,7 +4,7 @@ using Govor.Core.Repositories.Groups;
using Govor.Core.Repositories.Messages;
using Govor.Data.Repositories.Exceptions;
namespace Govor.Application.Services;
namespace Govor.Application.Services.Messages;
public class MessagesLoader : IMessagesLoader
{
+7
View File
@@ -21,6 +21,8 @@ Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Govor.Contracts", "Govor.Co
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Govor.Application", "Govor.Application\Govor.Application.csproj", "{FC5EDCA8-FD58-4078-8FB1-2BDBB2F6CA3E}"
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Govor.Application.Tests", "Govor.Application.Tests\Govor.Application.Tests.csproj", "{F56A64DF-2938-4BE0-83F2-B86429F19259}"
EndProject
Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution
Debug|Any CPU = Debug|Any CPU
@@ -55,6 +57,10 @@ Global
{FC5EDCA8-FD58-4078-8FB1-2BDBB2F6CA3E}.Debug|Any CPU.Build.0 = Debug|Any CPU
{FC5EDCA8-FD58-4078-8FB1-2BDBB2F6CA3E}.Release|Any CPU.ActiveCfg = Release|Any CPU
{FC5EDCA8-FD58-4078-8FB1-2BDBB2F6CA3E}.Release|Any CPU.Build.0 = Release|Any CPU
{F56A64DF-2938-4BE0-83F2-B86429F19259}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{F56A64DF-2938-4BE0-83F2-B86429F19259}.Debug|Any CPU.Build.0 = Debug|Any CPU
{F56A64DF-2938-4BE0-83F2-B86429F19259}.Release|Any CPU.ActiveCfg = Release|Any CPU
{F56A64DF-2938-4BE0-83F2-B86429F19259}.Release|Any CPU.Build.0 = Release|Any CPU
EndGlobalSection
GlobalSection(NestedProjects) = preSolution
{15031CBD-F319-4755-BA91-B86F20BD8E37} = {4ED5259A-6FB4-4D89-8E6B-4778DC68F7D4}
@@ -64,5 +70,6 @@ Global
{E4EDB179-7EB5-468D-9C1F-0CBE2E5E459E} = {114F53C1-B0AB-4BA0-9E36-0E811D1B3776}
{4E94907F-BE20-42A6-AB15-637850FEAD11} = {114F53C1-B0AB-4BA0-9E36-0E811D1B3776}
{FC5EDCA8-FD58-4078-8FB1-2BDBB2F6CA3E} = {114F53C1-B0AB-4BA0-9E36-0E811D1B3776}
{F56A64DF-2938-4BE0-83F2-B86429F19259} = {4ED5259A-6FB4-4D89-8E6B-4778DC68F7D4}
EndGlobalSection
EndGlobal