- Introduced Telegram.Bot package for bot functionality. - Updated user management to include Telegram linking and blocking features. - Enhanced activation request handling with notifications via Telegram. - Added new database entities for Telegram link tokens and login requests. - Implemented traffic synchronization for client stats in the XuiPanelGateway. - Updated application structure to support new test projects and improved dependency injection for Telegram services.
74 lines
3.2 KiB
C#
74 lines
3.2 KiB
C#
using NSubstitute;
|
|
using PnvPanel.Application.Activation;
|
|
using PnvPanel.Application.Auth;
|
|
using PnvPanel.Application.Common.Interfaces;
|
|
using PnvPanel.Application.Tests.TestSupport;
|
|
using PnvPanel.Domain.Activation;
|
|
using Xunit;
|
|
|
|
namespace PnvPanel.Application.Tests.Activation;
|
|
|
|
public class RequestActivationCommandHandlerTests
|
|
{
|
|
private readonly IRealtimeNotifier _notifier = Substitute.For<IRealtimeNotifier>();
|
|
private readonly ITelegramNotifier _telegramNotifier = Substitute.For<ITelegramNotifier>();
|
|
private readonly ICurrentUser _currentUser = Substitute.For<ICurrentUser>();
|
|
|
|
[Fact]
|
|
public async Task Handle_WhenNoPendingRequest_CreatesRequestAndNotifies()
|
|
{
|
|
using var dbContext = InMemoryDbContextFactory.Create();
|
|
var userId = Guid.NewGuid();
|
|
_currentUser.UserId.Returns(userId);
|
|
_currentUser.UserName.Returns("alice");
|
|
|
|
var handler = new RequestActivationCommandHandler(dbContext, _notifier, _telegramNotifier, _currentUser);
|
|
|
|
var result = await handler.Handle(new RequestActivationCommand("Please activate"), CancellationToken.None);
|
|
await dbContext.SaveChangesAsync(CancellationToken.None);
|
|
|
|
Assert.True(result.IsSuccess);
|
|
Assert.Equal("Please activate", result.Value.Comment);
|
|
Assert.Single(dbContext.ActivationRequests);
|
|
await _notifier.Received(1).NotifyActivationRequestedAsync(
|
|
Arg.Any<Guid>(), userId, "alice", "Please activate", Arg.Any<DateTimeOffset>(), Arg.Any<CancellationToken>());
|
|
await _telegramNotifier.Received(1).NotifyAdminsActivationRequestedAsync(
|
|
Arg.Any<Guid>(), "alice", "Please activate", Arg.Any<CancellationToken>());
|
|
}
|
|
|
|
[Fact]
|
|
public async Task Handle_WhenPendingRequestAlreadyExists_ReturnsConflictWithoutNotifying()
|
|
{
|
|
using var dbContext = InMemoryDbContextFactory.Create();
|
|
var userId = Guid.NewGuid();
|
|
dbContext.ActivationRequests.Add(ActivationRequest.Create(userId, null));
|
|
await dbContext.SaveChangesAsync(CancellationToken.None);
|
|
|
|
_currentUser.UserId.Returns(userId);
|
|
_currentUser.UserName.Returns("alice");
|
|
|
|
var handler = new RequestActivationCommandHandler(dbContext, _notifier, _telegramNotifier, _currentUser);
|
|
|
|
var result = await handler.Handle(new RequestActivationCommand(null), CancellationToken.None);
|
|
|
|
Assert.False(result.IsSuccess);
|
|
Assert.Equal(ActivationErrors.AlreadyPending, result.Error);
|
|
await _notifier.DidNotReceive().NotifyActivationRequestedAsync(
|
|
Arg.Any<Guid>(), Arg.Any<Guid>(), Arg.Any<string>(), Arg.Any<string?>(), Arg.Any<DateTimeOffset>(), Arg.Any<CancellationToken>());
|
|
}
|
|
|
|
[Fact]
|
|
public async Task Handle_WhenNotAuthenticated_ReturnsUnauthorized()
|
|
{
|
|
using var dbContext = InMemoryDbContextFactory.Create();
|
|
_currentUser.UserId.Returns((Guid?)null);
|
|
|
|
var handler = new RequestActivationCommandHandler(dbContext, _notifier, _telegramNotifier, _currentUser);
|
|
|
|
var result = await handler.Handle(new RequestActivationCommand(null), CancellationToken.None);
|
|
|
|
Assert.False(result.IsSuccess);
|
|
Assert.Equal(AuthErrors.Unauthorized, result.Error);
|
|
}
|
|
}
|