Files
Artemy 6d1c53beeb Refactor: migrate Core -> Domain and reorganize projects
Large refactor that renames/moves core types into a new Govor.Domain surface and reorganizes the Application layer. Models, configurations, migrations and many files moved from Govor.Core/Govor.Data to Govor.Domain; numerous Application services, interfaces and implementations were relocated or added (authentication, friends, messages, medias, push notifications, user sessions, storage, synching, private chats, etc.). Tests updated to use Govor.Domain namespaces and adjusted project references (removed Govor.Data reference from API tests). Also updated API, Hub and mapping code and project files to reflect the new structure and naming. This is primarily a codebase-wide namespace and module reorganization to establish a Domain project and restructure application services.
2026-07-16 19:27:45 +07:00

96 lines
3.3 KiB
C#

using Govor.Application.Authentication.Exceptions;
using Govor.Application.Infrastructure.Validators;
using Microsoft.Extensions.Configuration;
namespace Govor.Application.Tests.Infrastructure.Validators;
[TestFixture]
public class UsernameValidatorTests
{
private UsernameValidator _validator;
[SetUp]
public void SetUp()
{
var configData = new Dictionary<string, string?>
{
["UsernameModeration:BlockedExact:0"] = "админ",
["UsernameModeration:BlockedExact:1"] = "модератор",
["UsernameModeration:BlockedContains:0"] = "гитлер",
["UsernameModeration:BlockedContains:1"] = "наци",
["UsernameModeration:Reserved:0"] = "логин",
["UsernameModeration:Reserved:1"] = "регистрация"
};
var config = new ConfigurationBuilder()
.AddInMemoryCollection(configData)
.Build();
_validator = new UsernameValidator(config);
}
[TestCase("Иван")]
[TestCase("Алексей")]
[TestCase("Ёжик")]
[TestCase("Иван123")] // содержит цифры
public void Validate_ValidUsernames_ShouldNotThrow(string username)
{
Assert.DoesNotThrow(() => _validator.Validate(username));
}
[TestCase("Ivan")] // не кириллица
[TestCase("123Иван")] // начинается не с буквы
[TestCase("!@#$")] // спецсимволы
[TestCase("")] // пусто
[TestCase("И")] // меньше минимума
[TestCase("ИванИванИванИванИванИванИванИванИванИванИванИванИван")] // больше максимума (44 символа)
public void Validate_InvalidUsernames_ShouldThrow(string username)
{
Assert.Throws<InvalidUsernameException>(() => _validator.Validate(username));
}
[TestCase("Ааааааа")]
[TestCase("Бbbbb")]
public void Validate_RepeatingCharacters_ShouldThrow(string username)
{
Assert.Throws<InvalidUsernameException>(() => _validator.Validate(username));
}
[TestCase("Админ")]
[TestCase("Модератор")]
public void Validate_BlockedExact_ShouldThrow(string username)
{
Assert.Throws<InvalidUsernameException>(() => _validator.Validate(username));
}
[TestCase("Гитлер123")]
[TestCase("Нацист")]
public void Validate_BlockedContains_ShouldThrow(string username)
{
Assert.Throws<InvalidUsernameException>(() => _validator.Validate(username));
}
[TestCase("Логин")]
[TestCase("Регистрация")]
public void Validate_ReservedNames_ShouldThrow(string username)
{
Assert.Throws<InvalidUsernameException>(() => _validator.Validate(username));
}
[TestCase("Г1тлер")]
public void Validate_Normalization_ShouldDetectBlockedWord(string username)
{
Assert.Throws<InvalidUsernameException>(() => _validator.Validate(username));
}
[TestCase("Иван", ExpectedResult = true)]
[TestCase("1234", ExpectedResult = false)]
[TestCase("Админ", ExpectedResult = false)]
[TestCase("Гитлер123", ExpectedResult = false)]
public bool TryValidate_ShouldReturnExpectedResult(string username)
{
return _validator.TryValidate(username);
}
}