mirror of
https://github.com/Govor-team/Govor.git
synced 2026-07-21 11:44:56 +00:00
was added new rules to usernames
This commit is contained in:
@@ -1,5 +1,6 @@
|
||||
using Govor.Application.Exceptions.AuthService;
|
||||
using Govor.Application.Infrastructure.Validators;
|
||||
using Microsoft.Extensions.Configuration;
|
||||
|
||||
namespace Govor.Application.Tests.Infrastructure.Validators;
|
||||
|
||||
@@ -11,7 +12,23 @@ public class UsernameValidatorTests
|
||||
[SetUp]
|
||||
public void SetUp()
|
||||
{
|
||||
_validator = new UsernameValidator();
|
||||
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("Иван")]
|
||||
@@ -34,9 +51,45 @@ public class UsernameValidatorTests
|
||||
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)]
|
||||
public bool TryValidate_ShouldReturnTrueRegardlessOfInput(string username)
|
||||
[TestCase("Админ", ExpectedResult = false)]
|
||||
[TestCase("Гитлер123", ExpectedResult = false)]
|
||||
public bool TryValidate_ShouldReturnExpectedResult(string username)
|
||||
{
|
||||
return _validator.TryValidate(username);
|
||||
}
|
||||
|
||||
@@ -89,15 +89,7 @@ public class JwtTokenHasherTests
|
||||
{
|
||||
// Arrange
|
||||
var emptyConfig = new ConfigurationBuilder().Build();
|
||||
var hasherWithDefaultSecret = new JwtTokenHasher(emptyConfig);
|
||||
|
||||
string token = _fixture.Create<string>();
|
||||
|
||||
// Act
|
||||
string hash = hasherWithDefaultSecret.HashToken(token);
|
||||
var result = hasherWithDefaultSecret.VerifyToken(token, hash);
|
||||
|
||||
// Assert
|
||||
Assert.That(result, Is.True);
|
||||
// Act & Assert
|
||||
Assert.Throws<InvalidOperationException>(() => new JwtTokenHasher(emptyConfig));
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user