- 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.
92 lines
3.6 KiB
C#
92 lines
3.6 KiB
C#
using NSubstitute;
|
|
using PnvPanel.Application.Common.Interfaces;
|
|
using PnvPanel.Application.Telegram;
|
|
using PnvPanel.Application.Telegram.Bot;
|
|
using PnvPanel.Application.Tests.TestSupport;
|
|
using PnvPanel.Domain.Telegram;
|
|
using Xunit;
|
|
|
|
namespace PnvPanel.Application.Tests.Telegram.Bot;
|
|
|
|
public class RejectTelegramLoginCommandHandlerTests
|
|
{
|
|
private readonly IIdentityService _identityService = Substitute.For<IIdentityService>();
|
|
|
|
[Fact]
|
|
public async Task Handle_WhenPendingRequestAndLinkedUser_RejectsRequest()
|
|
{
|
|
using var dbContext = InMemoryDbContextFactory.Create();
|
|
var userId = Guid.NewGuid();
|
|
const long telegramUserId = 123456L;
|
|
|
|
var request = TelegramLoginRequest.Create(TimeSpan.FromMinutes(5), null);
|
|
dbContext.TelegramLoginRequests.Add(request);
|
|
await dbContext.SaveChangesAsync(CancellationToken.None);
|
|
|
|
_identityService.FindUserIdByTelegramUserIdAsync(telegramUserId, Arg.Any<CancellationToken>()).Returns(userId);
|
|
|
|
var handler = new RejectTelegramLoginCommandHandler(dbContext, _identityService);
|
|
|
|
var result = await handler.Handle(new RejectTelegramLoginCommand(request.Id, telegramUserId), CancellationToken.None);
|
|
|
|
Assert.True(result.IsSuccess);
|
|
Assert.Equal(TelegramLoginStatus.Rejected, request.Status);
|
|
}
|
|
|
|
[Fact]
|
|
public async Task Handle_WhenTelegramUserNotLinked_ReturnsNotLinked()
|
|
{
|
|
using var dbContext = InMemoryDbContextFactory.Create();
|
|
const long telegramUserId = 123456L;
|
|
|
|
_identityService.FindUserIdByTelegramUserIdAsync(telegramUserId, Arg.Any<CancellationToken>())
|
|
.Returns((Guid?)null);
|
|
|
|
var handler = new RejectTelegramLoginCommandHandler(dbContext, _identityService);
|
|
|
|
var result = await handler.Handle(new RejectTelegramLoginCommand(Guid.NewGuid(), telegramUserId), CancellationToken.None);
|
|
|
|
Assert.False(result.IsSuccess);
|
|
Assert.Equal(TelegramErrors.NotLinked, result.Error);
|
|
}
|
|
|
|
[Fact]
|
|
public async Task Handle_WhenLoginRequestNotFound_ReturnsNotFound()
|
|
{
|
|
using var dbContext = InMemoryDbContextFactory.Create();
|
|
var userId = Guid.NewGuid();
|
|
const long telegramUserId = 123456L;
|
|
|
|
_identityService.FindUserIdByTelegramUserIdAsync(telegramUserId, Arg.Any<CancellationToken>()).Returns(userId);
|
|
|
|
var handler = new RejectTelegramLoginCommandHandler(dbContext, _identityService);
|
|
|
|
var result = await handler.Handle(new RejectTelegramLoginCommand(Guid.NewGuid(), telegramUserId), CancellationToken.None);
|
|
|
|
Assert.False(result.IsSuccess);
|
|
Assert.Equal(TelegramErrors.LoginRequestNotFound, result.Error);
|
|
}
|
|
|
|
[Fact]
|
|
public async Task Handle_WhenRequestAlreadyDecided_ReturnsConflict()
|
|
{
|
|
using var dbContext = InMemoryDbContextFactory.Create();
|
|
var userId = Guid.NewGuid();
|
|
const long telegramUserId = 123456L;
|
|
|
|
var request = TelegramLoginRequest.Create(TimeSpan.FromMinutes(5), null);
|
|
request.Approve(userId);
|
|
dbContext.TelegramLoginRequests.Add(request);
|
|
await dbContext.SaveChangesAsync(CancellationToken.None);
|
|
|
|
_identityService.FindUserIdByTelegramUserIdAsync(telegramUserId, Arg.Any<CancellationToken>()).Returns(userId);
|
|
|
|
var handler = new RejectTelegramLoginCommandHandler(dbContext, _identityService);
|
|
|
|
var result = await handler.Handle(new RejectTelegramLoginCommand(request.Id, telegramUserId), CancellationToken.None);
|
|
|
|
Assert.False(result.IsSuccess);
|
|
Assert.Equal("Telegram.LoginRequestInvalid", result.Error.Code);
|
|
}
|
|
}
|