fix: validate platform schedule update target
PR Checks / test-and-build (pull_request) Successful in 13m7s

This commit is contained in:
2026-05-15 18:31:17 +03:00
parent 5931099c14
commit 18e702cd04
2 changed files with 55 additions and 3 deletions
@@ -59,16 +59,24 @@ public sealed class TelegramPlatformMessenger(
public async Task UpdateScheduleAsync(PlatformScheduleMessage message, CancellationToken ct) public async Task UpdateScheduleAsync(PlatformScheduleMessage message, CancellationToken ct)
{ {
EnsureTelegram(message.Group.Platform); EnsureTelegram(message.Group.Platform);
if (message.ExistingMessage is null) var existingMessage = message.ExistingMessage;
if (existingMessage is null)
{ {
throw new ArgumentException("Existing schedule message reference is required.", nameof(message)); throw new ArgumentException("Existing schedule message reference is required.", nameof(message));
} }
EnsureTelegram(existingMessage.Platform);
if (!string.Equals(message.Group.ExternalGroupId, existingMessage.ExternalGroupId, StringComparison.Ordinal) ||
!string.Equals(message.Group.ExternalThreadId, existingMessage.ExternalThreadId, StringComparison.Ordinal))
{
throw new ArgumentException("Existing schedule message reference must match the schedule group.", nameof(message));
}
var renderResult = TelegramSessionBatchRenderer.Render(message.View); var renderResult = TelegramSessionBatchRenderer.Render(message.View);
await BatchMessageEditor.EditBatchMessageAsync( await BatchMessageEditor.EditBatchMessageAsync(
bot, bot,
chatId: ParseLong(message.Group.ExternalGroupId), chatId: ParseLong(existingMessage.ExternalGroupId),
messageId: ParseInt(message.ExistingMessage.ExternalMessageId), messageId: ParseInt(existingMessage.ExternalMessageId),
text: renderResult.Text, text: renderResult.Text,
replyMarkup: renderResult.Markup, replyMarkup: renderResult.Markup,
ct); ct);
@@ -0,0 +1,44 @@
using GmRelay.Bot.Infrastructure.Telegram;
using GmRelay.Shared.Platform;
using GmRelay.Shared.Rendering;
using Microsoft.Extensions.Logging.Abstractions;
namespace GmRelay.Bot.Tests.Infrastructure.Telegram;
public sealed class TelegramPlatformMessengerTests
{
[Fact]
public async Task UpdateScheduleAsync_ShouldRejectNonTelegramExistingMessageReference()
{
var messenger = CreateMessenger();
var message = new PlatformScheduleMessage(
new PlatformGroup(PlatformKind.Telegram, "100", "Telegram group"),
CreateView(),
new PlatformMessageRef(PlatformKind.Discord, "100", null, "200"));
await Assert.ThrowsAsync<NotSupportedException>(
() => messenger.UpdateScheduleAsync(message, CancellationToken.None));
}
[Fact]
public async Task UpdateScheduleAsync_ShouldRejectMismatchedGroupAndExistingMessageReference()
{
var messenger = CreateMessenger();
var message = new PlatformScheduleMessage(
new PlatformGroup(PlatformKind.Telegram, "100", "Telegram group", ExternalThreadId: "7"),
CreateView(),
new PlatformMessageRef(PlatformKind.Telegram, "101", "7", "200"));
var exception = await Assert.ThrowsAsync<ArgumentException>(
() => messenger.UpdateScheduleAsync(message, CancellationToken.None));
Assert.Equal("message", exception.ParamName);
Assert.Contains("Existing schedule message reference must match the schedule group.", exception.Message);
}
private static TelegramPlatformMessenger CreateMessenger() =>
new(null!, NullLogger<TelegramPlatformMessenger>.Instance);
private static SessionBatchViewModel CreateView() =>
new("Test batch", []);
}