add latest tests
This commit is contained in:
@@ -1,76 +1,356 @@
|
||||
using ChatBot.Models.Configuration;
|
||||
using ChatBot.Models.Configuration.Validators;
|
||||
using FluentAssertions;
|
||||
using Microsoft.Extensions.Options;
|
||||
|
||||
namespace ChatBot.Tests.Configuration.Validators;
|
||||
|
||||
public class TelegramBotSettingsValidatorTests
|
||||
{
|
||||
private readonly TelegramBotSettingsValidator _validator = new();
|
||||
private readonly TelegramBotSettingsValidator _validator;
|
||||
|
||||
public TelegramBotSettingsValidatorTests()
|
||||
{
|
||||
_validator = new TelegramBotSettingsValidator();
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void Validate_ShouldReturnSuccess_WhenSettingsAreValid()
|
||||
public void Validate_WithValidSettings_ShouldReturnSuccess()
|
||||
{
|
||||
// Arrange
|
||||
var settings = new TelegramBotSettings
|
||||
{
|
||||
BotToken = "1234567890:ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijk",
|
||||
};
|
||||
var settings = CreateValidTelegramBotSettings();
|
||||
|
||||
// Act
|
||||
var result = _validator.Validate(null, settings);
|
||||
|
||||
// Assert
|
||||
result.Succeeded.Should().BeTrue();
|
||||
result.Failed.Should().BeFalse();
|
||||
result.FailureMessage.Should().BeNullOrEmpty();
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void Validate_ShouldReturnFailure_WhenBotTokenIsEmpty()
|
||||
public void Validate_WithEmptyBotToken_ShouldReturnFailure()
|
||||
{
|
||||
// Arrange
|
||||
var settings = new TelegramBotSettings { BotToken = "" };
|
||||
var settings = CreateValidTelegramBotSettings();
|
||||
settings.BotToken = string.Empty;
|
||||
|
||||
// Act
|
||||
var result = _validator.Validate(null, settings);
|
||||
|
||||
// Assert
|
||||
result.Succeeded.Should().BeFalse();
|
||||
result.Failures.Should().Contain(f => f.Contains("Telegram bot token is required"));
|
||||
result.Failed.Should().BeTrue();
|
||||
result.FailureMessage.Should().Contain("Telegram bot token is required");
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void Validate_ShouldReturnFailure_WhenBotTokenIsTooShort()
|
||||
public void Validate_WithNullBotToken_ShouldReturnFailure()
|
||||
{
|
||||
// Arrange
|
||||
var settings = new TelegramBotSettings
|
||||
{
|
||||
BotToken = "1234567890:ABC", // 15 chars, too short
|
||||
};
|
||||
var settings = CreateValidTelegramBotSettings();
|
||||
settings.BotToken = null!;
|
||||
|
||||
// Act
|
||||
var result = _validator.Validate(null, settings);
|
||||
|
||||
// Assert
|
||||
result.Succeeded.Should().BeFalse();
|
||||
result
|
||||
.Failures.Should()
|
||||
.Contain(f => f.Contains("Telegram bot token must be at least 40 characters"));
|
||||
result.Failed.Should().BeTrue();
|
||||
result.FailureMessage.Should().Contain("Telegram bot token is required");
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void Validate_WithWhitespaceBotToken_ShouldReturnFailure()
|
||||
{
|
||||
// Arrange
|
||||
var settings = CreateValidTelegramBotSettings();
|
||||
settings.BotToken = " ";
|
||||
|
||||
// Act
|
||||
var result = _validator.Validate(null, settings);
|
||||
|
||||
// Assert
|
||||
result.Succeeded.Should().BeFalse();
|
||||
result.Failed.Should().BeTrue();
|
||||
result.FailureMessage.Should().Contain("Telegram bot token is required");
|
||||
}
|
||||
|
||||
[Theory]
|
||||
[InlineData(null)]
|
||||
[InlineData("")]
|
||||
[InlineData(" ")]
|
||||
public void Validate_ShouldReturnFailure_WhenBotTokenIsNullOrWhitespace(string? botToken)
|
||||
[InlineData("\t")]
|
||||
[InlineData("\n")]
|
||||
[InlineData("\r\n")]
|
||||
public void Validate_WithVariousEmptyStrings_ShouldReturnFailure(string emptyString)
|
||||
{
|
||||
// Arrange
|
||||
var settings = new TelegramBotSettings { BotToken = botToken! };
|
||||
var settings = CreateValidTelegramBotSettings();
|
||||
settings.BotToken = emptyString;
|
||||
|
||||
// Act
|
||||
var result = _validator.Validate(null, settings);
|
||||
|
||||
// Assert
|
||||
result.Succeeded.Should().BeFalse();
|
||||
result.Failures.Should().Contain(f => f.Contains("Telegram bot token is required"));
|
||||
result.Failed.Should().BeTrue();
|
||||
result.FailureMessage.Should().Contain("Telegram bot token is required");
|
||||
}
|
||||
|
||||
[Theory]
|
||||
[InlineData("123456789012345678901234567890123456789")] // 39 characters
|
||||
[InlineData("12345678901234567890123456789012345678")] // 38 characters
|
||||
[InlineData("1234567890123456789012345678901234567")] // 37 characters
|
||||
[InlineData("123456789012345678901234567890123456")] // 36 characters
|
||||
[InlineData("12345678901234567890123456789012345")] // 35 characters
|
||||
[InlineData("1234567890123456789012345678901234")] // 34 characters
|
||||
[InlineData("123456789012345678901234567890123")] // 33 characters
|
||||
[InlineData("12345678901234567890123456789012")] // 32 characters
|
||||
[InlineData("1234567890123456789012345678901")] // 31 characters
|
||||
[InlineData("123456789012345678901234567890")] // 30 characters
|
||||
[InlineData("12345678901234567890123456789")] // 29 characters
|
||||
[InlineData("1234567890123456789012345678")] // 28 characters
|
||||
[InlineData("123456789012345678901234567")] // 27 characters
|
||||
[InlineData("12345678901234567890123456")] // 26 characters
|
||||
[InlineData("1234567890123456789012345")] // 25 characters
|
||||
[InlineData("123456789012345678901234")] // 24 characters
|
||||
[InlineData("12345678901234567890123")] // 23 characters
|
||||
[InlineData("1234567890123456789012")] // 22 characters
|
||||
[InlineData("123456789012345678901")] // 21 characters
|
||||
[InlineData("12345678901234567890")] // 20 characters
|
||||
[InlineData("1234567890123456789")] // 19 characters
|
||||
[InlineData("123456789012345678")] // 18 characters
|
||||
[InlineData("12345678901234567")] // 17 characters
|
||||
[InlineData("1234567890123456")] // 16 characters
|
||||
[InlineData("123456789012345")] // 15 characters
|
||||
[InlineData("12345678901234")] // 14 characters
|
||||
[InlineData("1234567890123")] // 13 characters
|
||||
[InlineData("123456789012")] // 12 characters
|
||||
[InlineData("12345678901")] // 11 characters
|
||||
[InlineData("1234567890")] // 10 characters
|
||||
[InlineData("123456789")] // 9 characters
|
||||
[InlineData("12345678")] // 8 characters
|
||||
[InlineData("1234567")] // 7 characters
|
||||
[InlineData("123456")] // 6 characters
|
||||
[InlineData("12345")] // 5 characters
|
||||
[InlineData("1234")] // 4 characters
|
||||
[InlineData("123")] // 3 characters
|
||||
[InlineData("12")] // 2 characters
|
||||
[InlineData("1")] // 1 character
|
||||
public void Validate_WithTooShortBotToken_ShouldReturnFailure(string shortToken)
|
||||
{
|
||||
// Arrange
|
||||
var settings = CreateValidTelegramBotSettings();
|
||||
settings.BotToken = shortToken;
|
||||
|
||||
// Act
|
||||
var result = _validator.Validate(null, settings);
|
||||
|
||||
// Assert
|
||||
result.Succeeded.Should().BeFalse();
|
||||
result.Failed.Should().BeTrue();
|
||||
result.FailureMessage.Should().Contain("Telegram bot token must be at least 40 characters");
|
||||
}
|
||||
|
||||
[Theory]
|
||||
[InlineData("1234567890123456789012345678901234567890")] // 40 characters
|
||||
[InlineData("12345678901234567890123456789012345678901")] // 41 characters
|
||||
[InlineData("123456789012345678901234567890123456789012")] // 42 characters
|
||||
[InlineData("1234567890123456789012345678901234567890123")] // 43 characters
|
||||
[InlineData("12345678901234567890123456789012345678901234")] // 44 characters
|
||||
[InlineData("123456789012345678901234567890123456789012345")] // 45 characters
|
||||
[InlineData("1234567890123456789012345678901234567890123456")] // 46 characters
|
||||
[InlineData("12345678901234567890123456789012345678901234567")] // 47 characters
|
||||
[InlineData("123456789012345678901234567890123456789012345678")] // 48 characters
|
||||
[InlineData("1234567890123456789012345678901234567890123456789")] // 49 characters
|
||||
[InlineData("12345678901234567890123456789012345678901234567890")] // 50 characters
|
||||
public void Validate_WithValidLengthBotToken_ShouldReturnSuccess(string validToken)
|
||||
{
|
||||
// Arrange
|
||||
var settings = CreateValidTelegramBotSettings();
|
||||
settings.BotToken = validToken;
|
||||
|
||||
// Act
|
||||
var result = _validator.Validate(null, settings);
|
||||
|
||||
// Assert
|
||||
result.Succeeded.Should().BeTrue();
|
||||
result.Failed.Should().BeFalse();
|
||||
result.FailureMessage.Should().BeNullOrEmpty();
|
||||
}
|
||||
|
||||
[Theory]
|
||||
[InlineData("1234567890123456789012345678901234567890")]
|
||||
[InlineData("ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz")]
|
||||
[InlineData("12345678901234567890123456789012345678901234567890")]
|
||||
[InlineData("abcdefghijklmnopqrstuvwxyz12345678901234567890")]
|
||||
[InlineData("123456789012345678901234567890123456789012345678901234567890")]
|
||||
public void Validate_WithVariousValidTokens_ShouldReturnSuccess(string validToken)
|
||||
{
|
||||
// Arrange
|
||||
var settings = CreateValidTelegramBotSettings();
|
||||
settings.BotToken = validToken;
|
||||
|
||||
// Act
|
||||
var result = _validator.Validate(null, settings);
|
||||
|
||||
// Assert
|
||||
result.Succeeded.Should().BeTrue();
|
||||
result.Failed.Should().BeFalse();
|
||||
result.FailureMessage.Should().BeNullOrEmpty();
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void Validate_WithNullSettings_ShouldThrowException()
|
||||
{
|
||||
// Arrange & Act & Assert
|
||||
var act = () => _validator.Validate(null, null!);
|
||||
act.Should().Throw<NullReferenceException>();
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void ValidateOptionsResult_Success_ShouldHaveCorrectProperties()
|
||||
{
|
||||
// Arrange
|
||||
var settings = CreateValidTelegramBotSettings();
|
||||
|
||||
// Act
|
||||
var result = _validator.Validate(null, settings);
|
||||
|
||||
// Assert
|
||||
result.Succeeded.Should().BeTrue();
|
||||
result.Failed.Should().BeFalse();
|
||||
result.FailureMessage.Should().BeNullOrEmpty();
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void ValidateOptionsResult_Failure_ShouldHaveCorrectProperties()
|
||||
{
|
||||
// Arrange
|
||||
var settings = new TelegramBotSettings { BotToken = string.Empty };
|
||||
|
||||
// Act
|
||||
var result = _validator.Validate(null, settings);
|
||||
|
||||
// Assert
|
||||
result.Succeeded.Should().BeFalse();
|
||||
result.Failed.Should().BeTrue();
|
||||
result.FailureMessage.Should().NotBeNullOrEmpty();
|
||||
result.FailureMessage.Should().Contain("Telegram bot token is required");
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void Validator_ShouldImplementIValidateOptions()
|
||||
{
|
||||
// Arrange & Act
|
||||
var validator = new TelegramBotSettingsValidator();
|
||||
|
||||
// Assert
|
||||
validator.Should().BeAssignableTo<IValidateOptions<TelegramBotSettings>>();
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void Validate_WithVeryLongValidToken_ShouldReturnSuccess()
|
||||
{
|
||||
// Arrange
|
||||
var settings = CreateValidTelegramBotSettings();
|
||||
settings.BotToken = new string('A', 1000); // Very long token
|
||||
|
||||
// Act
|
||||
var result = _validator.Validate(null, settings);
|
||||
|
||||
// Assert
|
||||
result.Succeeded.Should().BeTrue();
|
||||
result.Failed.Should().BeFalse();
|
||||
result.FailureMessage.Should().BeNullOrEmpty();
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void Validate_WithTokenContainingSpecialCharacters_ShouldReturnSuccess()
|
||||
{
|
||||
// Arrange
|
||||
var settings = CreateValidTelegramBotSettings();
|
||||
settings.BotToken = "1234567890123456789012345678901234567890!@#$%^&*()";
|
||||
|
||||
// Act
|
||||
var result = _validator.Validate(null, settings);
|
||||
|
||||
// Assert
|
||||
result.Succeeded.Should().BeTrue();
|
||||
result.Failed.Should().BeFalse();
|
||||
result.FailureMessage.Should().BeNullOrEmpty();
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void Validate_WithTokenContainingSpaces_ShouldReturnSuccess()
|
||||
{
|
||||
// Arrange
|
||||
var settings = CreateValidTelegramBotSettings();
|
||||
settings.BotToken = "1234567890123456789012345678901234567890 ";
|
||||
|
||||
// Act
|
||||
var result = _validator.Validate(null, settings);
|
||||
|
||||
// Assert
|
||||
result.Succeeded.Should().BeTrue();
|
||||
result.Failed.Should().BeFalse();
|
||||
result.FailureMessage.Should().BeNullOrEmpty();
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void Validate_WithTokenContainingUnicodeCharacters_ShouldReturnSuccess()
|
||||
{
|
||||
// Arrange
|
||||
var settings = CreateValidTelegramBotSettings();
|
||||
settings.BotToken = "1234567890123456789012345678901234567890абвгд";
|
||||
|
||||
// Act
|
||||
var result = _validator.Validate(null, settings);
|
||||
|
||||
// Assert
|
||||
result.Succeeded.Should().BeTrue();
|
||||
result.Failed.Should().BeFalse();
|
||||
result.FailureMessage.Should().BeNullOrEmpty();
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void Validate_WithExactMinimumLengthToken_ShouldReturnSuccess()
|
||||
{
|
||||
// Arrange
|
||||
var settings = CreateValidTelegramBotSettings();
|
||||
settings.BotToken = "1234567890123456789012345678901234567890"; // Exactly 40 characters
|
||||
|
||||
// Act
|
||||
var result = _validator.Validate(null, settings);
|
||||
|
||||
// Assert
|
||||
result.Succeeded.Should().BeTrue();
|
||||
result.Failed.Should().BeFalse();
|
||||
result.FailureMessage.Should().BeNullOrEmpty();
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void Validate_WithOneCharacterLessThanMinimum_ShouldReturnFailure()
|
||||
{
|
||||
// Arrange
|
||||
var settings = CreateValidTelegramBotSettings();
|
||||
settings.BotToken = "123456789012345678901234567890123456789"; // 39 characters
|
||||
|
||||
// Act
|
||||
var result = _validator.Validate(null, settings);
|
||||
|
||||
// Assert
|
||||
result.Succeeded.Should().BeFalse();
|
||||
result.Failed.Should().BeTrue();
|
||||
result.FailureMessage.Should().Contain("Telegram bot token must be at least 40 characters");
|
||||
}
|
||||
|
||||
private static TelegramBotSettings CreateValidTelegramBotSettings()
|
||||
{
|
||||
return new TelegramBotSettings
|
||||
{
|
||||
BotToken = "1234567890123456789012345678901234567890", // 40 characters
|
||||
};
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user