mirror of
https://github.com/BinaryAlley/Nemera.git
synced 2026-12-02 16:11:10 +00:00
Renames project paths from Lumina to Nemera
This commit is contained in:
1 parent
ce6fddb4c9
commit
0f659a4877
6450 files changed
No files matched your search
+82
@@ -0,0 +1,82 @@
|
||||
#region ========================================================================= USING =====================================================================================
|
||||
using Lumina.Contracts.Fixtures.Core.Responses.Scheduling;
|
||||
using Lumina.Contracts.Responses.Scheduling;
|
||||
using Lumina.Infrastructure.Core.Scheduling.Notifications;
|
||||
using Microsoft.AspNetCore.SignalR;
|
||||
using NSubstitute;
|
||||
using System.Collections.Generic;
|
||||
using System.Diagnostics.CodeAnalysis;
|
||||
using System.Threading;
|
||||
using System.Threading.Tasks;
|
||||
#endregion
|
||||
|
||||
namespace Lumina.Infrastructure.UnitTests.Core.Scheduling.Notifications;
|
||||
|
||||
/// <summary>
|
||||
/// Contains unit tests for the <see cref="ScheduledJobNotifier"/> class.
|
||||
/// </summary>
|
||||
[ExcludeFromCodeCoverage]
|
||||
public class ScheduledJobNotifierTests
|
||||
{
|
||||
private readonly IHubContext<ScheduledJobsHub, IScheduledJobsClient> _mockHubContext;
|
||||
private readonly IHubClients<IScheduledJobsClient> _mockHubClients;
|
||||
private readonly IScheduledJobsClient _mockClient;
|
||||
private readonly ScheduledJobNotifier _sut;
|
||||
private readonly ScheduledJobResponseFixture _scheduledJobResponseFixture = new();
|
||||
private readonly ScheduledJobExecutionResponseFixture _scheduledJobExecutionResponseFixture = new();
|
||||
|
||||
/// <summary>
|
||||
/// Initializes a new instance of the <see cref="ScheduledJobNotifierTests"/> class.
|
||||
/// </summary>
|
||||
public ScheduledJobNotifierTests()
|
||||
{
|
||||
_mockClient = Substitute.For<IScheduledJobsClient>();
|
||||
_mockHubClients = Substitute.For<IHubClients<IScheduledJobsClient>>();
|
||||
_mockHubClients.Group(ScheduledJobsHub.ADMINISTRATORS_GROUP).Returns(_mockClient);
|
||||
_mockHubContext = Substitute.For<IHubContext<ScheduledJobsHub, IScheduledJobsClient>>();
|
||||
_mockHubContext.Clients.Returns(_mockHubClients);
|
||||
_sut = new ScheduledJobNotifier(_mockHubContext);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public async Task SendScheduledJobsAsync_WhenCalled_ShouldSendTheScheduledJobsToTheAdministratorGroup()
|
||||
{
|
||||
// Arrange
|
||||
IReadOnlyList<ScheduledJobResponse> scheduledJobs = _scheduledJobResponseFixture.CreateMany(2);
|
||||
|
||||
// Act
|
||||
await _sut.SendScheduledJobsAsync(scheduledJobs, CancellationToken.None);
|
||||
|
||||
// Assert
|
||||
await _mockClient.Received(1).ReceiveScheduledJobsAsync(scheduledJobs);
|
||||
_mockHubClients.Received(1).Group(ScheduledJobsHub.ADMINISTRATORS_GROUP);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public async Task SendScheduledJobExecutionStartedAsync_WhenCalled_ShouldSendTheExecutionToTheAdministratorGroup()
|
||||
{
|
||||
// Arrange
|
||||
ScheduledJobExecutionResponse execution = _scheduledJobExecutionResponseFixture.Create();
|
||||
|
||||
// Act
|
||||
await _sut.SendScheduledJobExecutionStartedAsync(execution, CancellationToken.None);
|
||||
|
||||
// Assert
|
||||
await _mockClient.Received(1).ScheduledJobExecutionStartedAsync(execution);
|
||||
_mockHubClients.Received(1).Group(ScheduledJobsHub.ADMINISTRATORS_GROUP);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public async Task SendScheduledJobExecutionCompletedAsync_WhenCalled_ShouldSendTheExecutionToTheAdministratorGroup()
|
||||
{
|
||||
// Arrange
|
||||
ScheduledJobExecutionResponse execution = _scheduledJobExecutionResponseFixture.Create();
|
||||
|
||||
// Act
|
||||
await _sut.SendScheduledJobExecutionCompletedAsync(execution, CancellationToken.None);
|
||||
|
||||
// Assert
|
||||
await _mockClient.Received(1).ScheduledJobExecutionCompletedAsync(execution);
|
||||
_mockHubClients.Received(1).Group(ScheduledJobsHub.ADMINISTRATORS_GROUP);
|
||||
}
|
||||
}
|
||||
+118
@@ -0,0 +1,118 @@
|
||||
#region ========================================================================= USING =====================================================================================
|
||||
using Lumina.Application.Common.Infrastructure.Authorization;
|
||||
using Lumina.Infrastructure.Core.Scheduling.Notifications;
|
||||
using Microsoft.AspNetCore.SignalR;
|
||||
using NSubstitute;
|
||||
using System;
|
||||
using System.Diagnostics.CodeAnalysis;
|
||||
using System.Security.Claims;
|
||||
using System.Threading.Tasks;
|
||||
#endregion
|
||||
|
||||
namespace Lumina.Infrastructure.UnitTests.Core.Scheduling.Notifications;
|
||||
|
||||
/// <summary>
|
||||
/// Contains unit tests for the <see cref="ScheduledJobsHub"/> class.
|
||||
/// </summary>
|
||||
[ExcludeFromCodeCoverage]
|
||||
public class ScheduledJobsHubTests
|
||||
{
|
||||
private readonly IAuthorizationService _mockAuthorizationService;
|
||||
private readonly ScheduledJobsHub _sut;
|
||||
|
||||
/// <summary>
|
||||
/// Initializes a new instance of the <see cref="ScheduledJobsHubTests"/> class.
|
||||
/// </summary>
|
||||
public ScheduledJobsHubTests()
|
||||
{
|
||||
Microsoft.Extensions.DependencyInjection.IServiceScopeFactory mockScopeFactory = Substitute.For<Microsoft.Extensions.DependencyInjection.IServiceScopeFactory>();
|
||||
Microsoft.Extensions.DependencyInjection.IServiceScope mockScope = Substitute.For<Microsoft.Extensions.DependencyInjection.IServiceScope>();
|
||||
System.IServiceProvider mockServiceProvider = Substitute.For<System.IServiceProvider>();
|
||||
_mockAuthorizationService = Substitute.For<IAuthorizationService>();
|
||||
|
||||
mockScope.ServiceProvider.Returns(mockServiceProvider);
|
||||
mockServiceProvider.GetService(typeof(IAuthorizationService)).Returns(_mockAuthorizationService);
|
||||
mockScopeFactory.CreateScope().Returns(mockScope);
|
||||
|
||||
_sut = new ScheduledJobsHub(mockScopeFactory);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public async Task OnConnectedAsync_WhenUserClaimIsMissing_ShouldAbortTheConnection()
|
||||
{
|
||||
// Arrange
|
||||
HubCallerContext context = CreateHubCallerContext(new ClaimsPrincipal(new ClaimsIdentity()));
|
||||
_sut.Context = context;
|
||||
|
||||
// Act
|
||||
await _sut.OnConnectedAsync();
|
||||
|
||||
// Assert
|
||||
context.Received(1).Abort();
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public async Task OnConnectedAsync_WhenUserIdClaimIsNotAGuid_ShouldAbortTheConnection()
|
||||
{
|
||||
// Arrange
|
||||
ClaimsPrincipal user = new(new ClaimsIdentity([new Claim(ClaimTypes.NameIdentifier, "not-a-guid")]));
|
||||
HubCallerContext context = CreateHubCallerContext(user);
|
||||
_sut.Context = context;
|
||||
|
||||
// Act
|
||||
await _sut.OnConnectedAsync();
|
||||
|
||||
// Assert
|
||||
context.Received(1).Abort();
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public async Task OnConnectedAsync_WhenUserIsNotAdministrator_ShouldAbortTheConnection()
|
||||
{
|
||||
// Arrange
|
||||
Guid userId = Guid.NewGuid();
|
||||
ClaimsPrincipal user = new(new ClaimsIdentity([new Claim(ClaimTypes.NameIdentifier, userId.ToString())]));
|
||||
HubCallerContext context = CreateHubCallerContext(user);
|
||||
_sut.Context = context;
|
||||
_mockAuthorizationService.IsInRoleAsync(userId, "Admin", System.Threading.CancellationToken.None).Returns(false);
|
||||
|
||||
// Act
|
||||
await _sut.OnConnectedAsync();
|
||||
|
||||
// Assert
|
||||
context.Received(1).Abort();
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public async Task OnConnectedAsync_WhenUserIsAdministrator_ShouldAddTheConnectionToTheAdministratorGroup()
|
||||
{
|
||||
// Arrange
|
||||
Guid userId = Guid.NewGuid();
|
||||
ClaimsPrincipal user = new(new ClaimsIdentity([new Claim(ClaimTypes.NameIdentifier, userId.ToString())]));
|
||||
HubCallerContext context = CreateHubCallerContext(user);
|
||||
context.ConnectionId.Returns("connection-1");
|
||||
_sut.Context = context;
|
||||
IGroupManager mockGroupManager = Substitute.For<IGroupManager>();
|
||||
_sut.Groups = mockGroupManager;
|
||||
_mockAuthorizationService.IsInRoleAsync(userId, "Admin", System.Threading.CancellationToken.None).Returns(true);
|
||||
|
||||
// Act
|
||||
await _sut.OnConnectedAsync();
|
||||
|
||||
// Assert
|
||||
context.DidNotReceive().Abort();
|
||||
await mockGroupManager.Received(1).AddToGroupAsync("connection-1", ScheduledJobsHub.ADMINISTRATORS_GROUP, System.Threading.CancellationToken.None);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Creates a mocked hub caller context carrying the provided user.
|
||||
/// </summary>
|
||||
/// <param name="user">The user of the connection.</param>
|
||||
/// <returns>The created context.</returns>
|
||||
private static HubCallerContext CreateHubCallerContext(ClaimsPrincipal user)
|
||||
{
|
||||
HubCallerContext context = Substitute.For<HubCallerContext>();
|
||||
context.User.Returns(user);
|
||||
return context;
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user