Path: redb-route/tests/redb.Route.Tests.Telegram/TelegramConnectionFactoryTests.cs
Snapshot: redb-route, development snapshot 2026-12-06. This is the development state of the public mirrors; it may differ from the published packages.
License: Apache-2.0. Repository: https://github.com/redbase-app/redb-route
Page: https://redbase.app/src/dev-2026-12-06/redb-route/tests/redb.Route.Tests.Telegram/TelegramConnectionFactoryTests.cs
----
using redb.Route.Core;
using redb.Route.Telegram;
using redb.Route.Telegram.Fluent;
namespace redb.Route.Tests.Telegram;
///
/// The bot token — a full account credential — must be able to live in the registry
/// instead of the endpoint URI, so it never reaches logs, telemetry, or the dashboard.
///
public sealed class TelegramConnectionFactoryTests
{
private const string Token = "123456:AAH-TOPSECRET-BOT-TOKEN";
private static TelegramComponent Wire(string name, TelegramConnectionFactory factory)
{
var context = new RouteContext();
var component = new TelegramComponent();
context.AddComponent(component);
context.AddToRegistry(name, factory);
return component;
}
[Fact]
public void Factory_SuppliesToken_WhenUriCarriesNone()
{
var component = Wire("support-bot", new TelegramConnectionFactory
{
Token = Token,
SendTimeoutSeconds = 42
});
var uri = EndpointUriParser.Parse("telegram://receive?connectionFactory=support-bot");
// Validate() requires a token — this would throw if the factory were not applied first.
var endpoint = (TelegramEndpoint)component.CreateEndpoint(uri);
endpoint.Should().NotBeNull();
uri.ToUriString().Should().NotContain(Token);
uri.ToString().Should().NotContain(Token);
}
[Fact]
public void ExplicitUriToken_WinsOverFactory()
{
var component = Wire("f", new TelegramConnectionFactory { Token = "from:FACTORY" });
var uri = EndpointUriParser.Parse("telegram://send?connectionFactory=f&token=from:URI&chatId=1");
var endpoint = (TelegramEndpoint)component.CreateEndpoint(uri);
endpoint.Should().NotBeNull();
// the URI value survived the merge (endpoint construction validated it)
uri.RawParameters["token"].Should().Be("from:URI");
}
[Fact]
public void MissingFactory_FailsLoud()
{
var context = new RouteContext();
var component = new TelegramComponent();
context.AddComponent(component);
var uri = EndpointUriParser.Parse("telegram://receive?connectionFactory=absent&token=inline:TOKEN");
var act = () => component.CreateEndpoint(uri);
// A typo in connectionFactory must fail loud, not fall back silently
act.Should().Throw()
.Which.Message.Should().Contain("absent");
}
[Fact]
public void TokenLessDsl_EmitsConnectionFactoryAndNoToken()
{
var uri = Tg.Receive().ConnectionFactory("support-bot").Build();
uri.Should().Be("telegram://receive?connectionFactory=support-bot");
uri.Should().NotContain("token=");
}
[Fact]
public void TokenDsl_StillEmitsToken_Unchanged()
{
var uri = Tg.Send("abc:123").ChatId(42).Build();
uri.Should().Contain("token=");
uri.Should().StartWith("telegram://send?token=");
}
[Fact]
public void TheTokenInARequestUrlPath_IsMaskedInTheEndpointsLastErrorWarningAndRefusal()
{
// The Bot API puts the token in the path (https://api.telegram.org/bot/sendMessage), where no key=value
// masking finds it; the endpoint knows its token and masks it by value, plain or URL-escaped.
var component = Wire("support-bot", new TelegramConnectionFactory { Token = Token });
var endpoint = (TelegramEndpoint)component.CreateEndpoint(EndpointUriParser.Parse("telegram://send?connectionFactory=support-bot&chatId=1"));
endpoint.RecordError(new HttpRequestException($"POST https://api.telegram.org/bot{Token}/sendMessage failed"));
endpoint.RecordWarning($"retrying https://api.telegram.org/bot{Uri.EscapeDataString(Token)}/getUpdates");
endpoint.RecordRefused($"refused for bot{Token}");
endpoint.LastErrorMessage.Should().Be("POST https://api.telegram.org/bot****/sendMessage failed");
endpoint.LastWarningMessage.Should().Be("retrying https://api.telegram.org/bot****/getUpdates");
endpoint.LastRefusalMessage.Should().Be("refused for bot****");
}
[Fact]
public void ATokenGivenInTheUri_IsMaskedByValueToo()
{
var component = new TelegramComponent();
new RouteContext().AddComponent(component);
var endpoint = (TelegramEndpoint)component.CreateEndpoint(EndpointUriParser.Parse($"telegram://send?token={Token}&chatId=1"));
endpoint.RecordError(new HttpRequestException($"GET https://api.telegram.org/bot{Token}/getMe: 401"));
endpoint.LastErrorMessage.Should().NotContain(Token).And.Contain("bot****/getMe");
}
}