Files
PnvPanel/backend/tests/PnvPanel.Application.Tests/Telegram/Bot/LinkTelegramCommandHandlerTests.cs
T
Leonid Pershin 7b6fe9ad78 Add Telegram bot integration and enhance user management features
- 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.
2026-07-02 01:01:03 +03:00

109 lines
4.3 KiB
C#

using NSubstitute;
using PnvPanel.Application.Common.Interfaces;
using PnvPanel.Application.Common.Models;
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 LinkTelegramCommandHandlerTests
{
private readonly IIdentityService _identityService = Substitute.For<IIdentityService>();
[Fact]
public async Task Handle_WhenTokenValid_LinksUserAndConsumesToken()
{
using var dbContext = InMemoryDbContextFactory.Create();
var userId = Guid.NewGuid();
var token = TelegramLinkToken.Create(userId, TimeSpan.FromMinutes(5));
dbContext.TelegramLinkTokens.Add(token);
await dbContext.SaveChangesAsync(CancellationToken.None);
_identityService.LinkTelegramAsync(userId, 123456L, "alice_tg", Arg.Any<CancellationToken>())
.Returns(Result.Success());
var handler = new LinkTelegramCommandHandler(dbContext, _identityService);
var result = await handler.Handle(new LinkTelegramCommand(token.Token, 123456L, "alice_tg"), CancellationToken.None);
Assert.True(result.IsSuccess);
Assert.Equal(userId, result.Value);
Assert.False(token.IsValid);
}
[Fact]
public async Task Handle_WhenTokenNotFound_ReturnsNotFound()
{
using var dbContext = InMemoryDbContextFactory.Create();
var handler = new LinkTelegramCommandHandler(dbContext, _identityService);
var result = await handler.Handle(new LinkTelegramCommand("missing-token", 123456L, null), CancellationToken.None);
Assert.False(result.IsSuccess);
Assert.Equal(TelegramErrors.LinkTokenNotFound, result.Error);
}
[Fact]
public async Task Handle_WhenTokenAlreadyConsumed_ReturnsNotFound()
{
using var dbContext = InMemoryDbContextFactory.Create();
var userId = Guid.NewGuid();
var token = TelegramLinkToken.Create(userId, TimeSpan.FromMinutes(5));
token.Consume();
dbContext.TelegramLinkTokens.Add(token);
await dbContext.SaveChangesAsync(CancellationToken.None);
var handler = new LinkTelegramCommandHandler(dbContext, _identityService);
var result = await handler.Handle(new LinkTelegramCommand(token.Token, 123456L, null), CancellationToken.None);
Assert.False(result.IsSuccess);
Assert.Equal(TelegramErrors.LinkTokenNotFound, result.Error);
await _identityService.DidNotReceive().LinkTelegramAsync(
Arg.Any<Guid>(), Arg.Any<long>(), Arg.Any<string?>(), Arg.Any<CancellationToken>());
}
[Fact]
public async Task Handle_WhenTokenExpired_ReturnsNotFound()
{
using var dbContext = InMemoryDbContextFactory.Create();
var userId = Guid.NewGuid();
var token = TelegramLinkToken.Create(userId, TimeSpan.FromMinutes(-5));
dbContext.TelegramLinkTokens.Add(token);
await dbContext.SaveChangesAsync(CancellationToken.None);
var handler = new LinkTelegramCommandHandler(dbContext, _identityService);
var result = await handler.Handle(new LinkTelegramCommand(token.Token, 123456L, null), CancellationToken.None);
Assert.False(result.IsSuccess);
Assert.Equal(TelegramErrors.LinkTokenNotFound, result.Error);
}
[Fact]
public async Task Handle_WhenIdentityServiceFails_ReturnsFailureWithoutConsumingToken()
{
using var dbContext = InMemoryDbContextFactory.Create();
var userId = Guid.NewGuid();
var token = TelegramLinkToken.Create(userId, TimeSpan.FromMinutes(5));
dbContext.TelegramLinkTokens.Add(token);
await dbContext.SaveChangesAsync(CancellationToken.None);
var error = Error.Conflict("Telegram.AlreadyLinked", "Этот Telegram уже привязан к другому аккаунту.");
_identityService.LinkTelegramAsync(userId, 123456L, null, Arg.Any<CancellationToken>())
.Returns(Result.Failure(error));
var handler = new LinkTelegramCommandHandler(dbContext, _identityService);
var result = await handler.Handle(new LinkTelegramCommand(token.Token, 123456L, null), CancellationToken.None);
Assert.False(result.IsSuccess);
Assert.Equal(error, result.Error);
Assert.True(token.IsValid);
}
}