Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 10 additions & 0 deletions src/ByteSync.Functions/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -92,6 +92,16 @@
services.Configure<AppSettings>(appSettingsSection);
var appSettings = appSettingsSection.Get<AppSettings>();

var logger = serviceProvider.GetService<ILogger<Program>>();
if (logger != null && appSettings != null)
{
logger.LogInformation("AppSettings loaded - JwtDurationInSeconds: {JwtDuration}, " +
"SkipClientsVersionCheck: {SkipCheck}, DefaultStorageProvider: {DefaultStorageProvider}",
appSettings.JwtDurationInSeconds,
appSettings.SkipClientsVersionCheck,
appSettings.DefaultStorageProvider);
}

services.AddClaimAuthorization();
services.AddJwtAuthentication(appSettings!.Secret);

Expand Down
Original file line number Diff line number Diff line change
@@ -1,20 +1,54 @@
using ByteSync.Common.Business.Sessions;
using ByteSync.ServerCommon.Interfaces.Repositories;
using ByteSync.ServerCommon.Interfaces.Services;
using ByteSync.ServerCommon.Interfaces.Services.Clients;
using MediatR;
using Microsoft.Extensions.Logging;

namespace ByteSync.ServerCommon.Commands.CloudSessions;

public class ResetSessionCommandHandler : IRequestHandler<ResetSessionRequest, Unit>
{
private readonly ICloudSessionsService _cloudSessionsService;
private readonly ICloudSessionsRepository _cloudSessionsRepository;
private readonly IInventoryService _inventoryService;
private readonly ISynchronizationService _synchronizationService;
private readonly ISharedFilesService _sharedFilesService;
private readonly IInvokeClientsService _invokeClientsService;
private readonly ILogger<ResetSessionCommandHandler> _logger;

public ResetSessionCommandHandler(ICloudSessionsService cloudSessionsService)
public ResetSessionCommandHandler(ICloudSessionsRepository cloudSessionsRepository,
IInventoryService inventoryService, ISynchronizationService synchronizationService,
ISharedFilesService sharedFilesService, IInvokeClientsService invokeClientsService,
ILogger<ResetSessionCommandHandler> logger)
{
_cloudSessionsService = cloudSessionsService;
_cloudSessionsRepository = cloudSessionsRepository;
_inventoryService = inventoryService;
_synchronizationService = synchronizationService;
_sharedFilesService = sharedFilesService;
_invokeClientsService = invokeClientsService;
_logger = logger;
}

public async Task<Unit> Handle(ResetSessionRequest request, CancellationToken cancellationToken)
{
await _cloudSessionsService.ResetSession(request.SessionId, request.Client);
await _cloudSessionsRepository.Update(request.SessionId, cloudSessionData =>
{
cloudSessionData.ResetSession();

return true;
});

await _inventoryService.ResetSession(request.SessionId);

await _synchronizationService.ResetSession(request.SessionId);

await _sharedFilesService.ClearSession(request.SessionId);

_logger.LogInformation("ResetSession: session {sessionId} reset by {clientInstanceId}", request.SessionId, request.Client.ClientInstanceId);

await _invokeClientsService.SessionGroupExcept(request.SessionId, request.Client)
.SessionResetted(new BaseSessionDto(request.SessionId, request.Client.ClientInstanceId));

return Unit.Value;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,6 @@ public interface ICloudSessionsService

Task<List<SessionMemberInfoDTO>> GetSessionMembersInfosAsync(string sessionId);

Task<bool> ResetSession(string sessionId, Client client);

Task GiveCloudSessionPasswordExchangeKey(Client client, GiveCloudSessionPasswordExchangeKeyParameters parameters);

Task InformPasswordIsWrong(Client client, string sessionId, string clientInstanceId);
Expand Down
33 changes: 1 addition & 32 deletions src/ByteSync.ServerCommon/Services/CloudSessionsService.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
using ByteSync.Common.Business.EndPoints;
using ByteSync.Common.Business.Sessions;
using ByteSync.Common.Business.Sessions.Cloud;
using ByteSync.Common.Business.Sessions.Cloud.Connections;
using ByteSync.Common.Helpers;
Expand All @@ -17,22 +16,15 @@ namespace ByteSync.ServerCommon.Services;
public class CloudSessionsService : ICloudSessionsService
{
private readonly ILogger<CloudSessionsService> _logger;
private readonly ISharedFilesService _sharedFilesService;
private readonly ICloudSessionsRepository _cloudSessionsRepository;
private readonly ISynchronizationService _synchronizationService;
private readonly IInventoryService _inventoryService;
private readonly ISessionMemberMapper _sessionMemberConverter;
private readonly IInvokeClientsService _invokeClientsService;

public CloudSessionsService(ILogger<CloudSessionsService> logger, ISharedFilesService sharedFilesService,
ICloudSessionsRepository cloudSessionsRepository, ISynchronizationService synchronizationService, IInventoryService inventoryService,
public CloudSessionsService(ILogger<CloudSessionsService> logger, ICloudSessionsRepository cloudSessionsRepository,
ISessionMemberMapper sessionMemberConverter, IInvokeClientsService invokeClientsService)
{
_logger = logger;
_sharedFilesService = sharedFilesService;
_cloudSessionsRepository = cloudSessionsRepository;
_synchronizationService = synchronizationService;
_inventoryService = inventoryService;
_sessionMemberConverter = sessionMemberConverter;
_invokeClientsService = invokeClientsService;
}
Expand Down Expand Up @@ -231,29 +223,6 @@ public async Task<List<SessionMemberInfoDTO>> GetSessionMembersInfosAsync(string
return result;
}

public async Task<bool> ResetSession(string sessionId, Client client)
{
await _cloudSessionsRepository.Update(sessionId, cloudSessionData =>
{
cloudSessionData.ResetSession();

return true;
});

await _inventoryService.ResetSession(sessionId);

await _synchronizationService.ResetSession(sessionId);

await _sharedFilesService.ClearSession(sessionId);

_logger.LogInformation("ResetSession: session {sessionId} reset by {clientInstanceId}", sessionId, client.ClientInstanceId);

await _invokeClientsService.SessionGroupExcept(sessionId, client)
.SessionResetted(new BaseSessionDto(sessionId, client.ClientInstanceId));

return true;
}

public async Task<JoinSessionResult> AskJoinCloudSession(Client client, AskJoinCloudSessionParameters parameters)
{
if (!client.ClientInstanceId.Equals(parameters.JoinerClientInstanceId))
Expand Down
Original file line number Diff line number Diff line change
@@ -1,8 +1,16 @@
using System;
using System.Threading;
using System.Threading.Tasks;
using ByteSync.Common.Business.Sessions;
using ByteSync.Common.Interfaces.Hub;
using ByteSync.ServerCommon.Business.Auth;
using ByteSync.ServerCommon.Business.Repositories;
using ByteSync.ServerCommon.Business.Sessions;
using ByteSync.ServerCommon.Commands.CloudSessions;
using ByteSync.ServerCommon.Interfaces.Repositories;
using ByteSync.ServerCommon.Interfaces.Services;
using ByteSync.ServerCommon.Interfaces.Services.Clients;
using Microsoft.Extensions.Logging;
using Moq;
using NUnit.Framework;

Expand All @@ -12,20 +20,45 @@ namespace ByteSync.ServerCommon.Tests.Commands.CloudSessions;
public class ResetSessionCommandHandlerTests
{
[Test]
public async Task Handle_CallsService()
public async Task Handle_CallsAllRequiredServices()
{
// Arrange
var mockService = new Mock<ICloudSessionsService>();
var mockCloudSessionsRepository = new Mock<ICloudSessionsRepository>();
var mockInventoryService = new Mock<IInventoryService>();
var mockSynchronizationService = new Mock<ISynchronizationService>();
var mockSharedFilesService = new Mock<ISharedFilesService>();
var mockInvokeClientsService = new Mock<IInvokeClientsService>();
var mockLogger = new Mock<ILogger<ResetSessionCommandHandler>>();

var client = new Client();
var sessionId = "session1";
mockService.Setup(s => s.ResetSession(sessionId, client)).ReturnsAsync(true);
var handler = new ResetSessionCommandHandler(mockService.Object);

mockCloudSessionsRepository.Setup(r => r.Update(sessionId, It.IsAny<Func<CloudSessionData, bool>>(), null, null))
.ReturnsAsync(new UpdateEntityResult<CloudSessionData>(null, UpdateEntityStatus.Saved));
mockInventoryService.Setup(s => s.ResetSession(sessionId)).Returns(Task.CompletedTask);
mockSynchronizationService.Setup(s => s.ResetSession(sessionId)).Returns(Task.CompletedTask);
mockSharedFilesService.Setup(s => s.ClearSession(sessionId)).Returns(Task.CompletedTask);
mockInvokeClientsService.Setup(s => s.SessionGroupExcept(sessionId, client))
.Returns(Mock.Of<IHubByteSyncPush>());

var handler = new ResetSessionCommandHandler(
mockCloudSessionsRepository.Object,
mockInventoryService.Object,
mockSynchronizationService.Object,
mockSharedFilesService.Object,
mockInvokeClientsService.Object,
mockLogger.Object);

var request = new ResetSessionRequest(sessionId, client);

// Act
await handler.Handle(request, CancellationToken.None);

// Assert
mockService.Verify(s => s.ResetSession(sessionId, client), Times.Once);
mockCloudSessionsRepository.Verify(r => r.Update(sessionId, It.IsAny<Func<CloudSessionData, bool>>(), null, null), Times.Once);
mockInventoryService.Verify(s => s.ResetSession(sessionId), Times.Once);
mockSynchronizationService.Verify(s => s.ResetSession(sessionId), Times.Once);
mockSharedFilesService.Verify(s => s.ClearSession(sessionId), Times.Once);
mockInvokeClientsService.Verify(s => s.SessionGroupExcept(sessionId, client), Times.Once);
}
}