mirror of
https://github.com/Govor-team/Govor.git
synced 2026-11-17 12:59:25 +00:00
Expand server tests and enforce coverage
Consolidate server tests under tests/, upgrade them to .NET 10, and add CI coverage thresholds and reports. Expand API and application boundary coverage while fixing related issues in media handling, friend blocking, session crypto, presence, username validation, and local storage.
This commit is contained in:
+105
@@ -0,0 +1,105 @@
|
||||
using System.Security.Claims;
|
||||
using Govor.Application.Infrastructure.Extensions;
|
||||
using Microsoft.AspNetCore.Http;
|
||||
using Moq;
|
||||
|
||||
namespace Govor.Application.Tests.Infrastructure.Extensions;
|
||||
|
||||
[TestFixture]
|
||||
public class CurrentUserServiceTests
|
||||
{
|
||||
private Mock<IHttpContextAccessor> _httpContextAccessorMock = null!;
|
||||
private ICurrentUserService _currentUserService = null!;
|
||||
[SetUp]
|
||||
public void SetUp()
|
||||
{
|
||||
_httpContextAccessorMock = new Mock<IHttpContextAccessor>();
|
||||
_currentUserService = new CurrentUserService(_httpContextAccessorMock.Object);
|
||||
}
|
||||
|
||||
[Test]
|
||||
public void GetCurrentUserId_ValidUserIdClaim_ReturnsGuid()
|
||||
{
|
||||
// Arrange
|
||||
var userId = Guid.NewGuid();
|
||||
var claims = new[] { new Claim("userId", userId.ToString()) };
|
||||
var identity = new ClaimsIdentity(claims);
|
||||
var principal = new ClaimsPrincipal(identity);
|
||||
|
||||
var httpContextMock = new Mock<HttpContext>();
|
||||
httpContextMock.Setup(x => x.User).Returns(principal);
|
||||
|
||||
_httpContextAccessorMock.Setup(x => x.HttpContext).Returns(httpContextMock.Object);
|
||||
|
||||
// Act
|
||||
var result = _currentUserService.GetCurrentUserId();
|
||||
|
||||
// Assert
|
||||
Assert.That(result, Is.EqualTo(userId));
|
||||
}
|
||||
|
||||
[Test]
|
||||
public void GetCurrentUserId_NoHttpContext_ThrowsUnauthorizedAccessException()
|
||||
{
|
||||
// Arrange
|
||||
_httpContextAccessorMock.Setup(x => x.HttpContext).Returns((HttpContext)null);
|
||||
|
||||
// Act & Assert
|
||||
var ex = Assert.Throws<UnauthorizedAccessException>(() => _currentUserService.GetCurrentUserId());
|
||||
Assert.That(ex.Message, Is.EqualTo("userID claim is missing or invalid"));
|
||||
}
|
||||
|
||||
[Test]
|
||||
public void GetCurrentUserId_NoUserIdClaim_ThrowsUnauthorizedAccessException()
|
||||
{
|
||||
// Arrange
|
||||
var claims = new[] { new Claim("otherClaim", "value") };
|
||||
var identity = new ClaimsIdentity(claims);
|
||||
var principal = new ClaimsPrincipal(identity);
|
||||
|
||||
var httpContextMock = new Mock<HttpContext>();
|
||||
httpContextMock.Setup(x => x.User).Returns(principal);
|
||||
|
||||
_httpContextAccessorMock.Setup(x => x.HttpContext).Returns(httpContextMock.Object);
|
||||
|
||||
// Act & Assert
|
||||
var ex = Assert.Throws<UnauthorizedAccessException>(() => _currentUserService.GetCurrentUserId());
|
||||
Assert.That(ex.Message, Is.EqualTo("userID claim is missing or invalid"));
|
||||
}
|
||||
|
||||
[Test]
|
||||
public void GetCurrentUserId_InvalidUserIdClaim_ThrowsUnauthorizedAccessException()
|
||||
{
|
||||
// Arrange
|
||||
var claims = new[] { new Claim("userId", "invalid-guid") };
|
||||
var identity = new ClaimsIdentity(claims);
|
||||
var principal = new ClaimsPrincipal(identity);
|
||||
|
||||
var httpContextMock = new Mock<HttpContext>();
|
||||
httpContextMock.Setup(x => x.User).Returns(principal);
|
||||
|
||||
_httpContextAccessorMock.Setup(x => x.HttpContext).Returns(httpContextMock.Object);
|
||||
|
||||
// Act & Assert
|
||||
var ex = Assert.Throws<UnauthorizedAccessException>(() => _currentUserService.GetCurrentUserId());
|
||||
Assert.That(ex.Message, Is.EqualTo("userID claim is missing or invalid"));
|
||||
}
|
||||
|
||||
[Test]
|
||||
public void GetCurrentUserId_EmptyUserIdClaim_ThrowsUnauthorizedAccessException()
|
||||
{
|
||||
// Arrange
|
||||
var claims = new[] { new Claim("userId", "") };
|
||||
var identity = new ClaimsIdentity(claims);
|
||||
var principal = new ClaimsPrincipal(identity);
|
||||
|
||||
var httpContextMock = new Mock<HttpContext>();
|
||||
httpContextMock.Setup(x => x.User).Returns(principal);
|
||||
|
||||
_httpContextAccessorMock.Setup(x => x.HttpContext).Returns(httpContextMock.Object);
|
||||
|
||||
// Act & Assert
|
||||
var ex = Assert.Throws<UnauthorizedAccessException>(() => _currentUserService.GetCurrentUserId());
|
||||
Assert.That(ex.Message, Is.EqualTo("userID claim is missing or invalid"));
|
||||
}
|
||||
}
|
||||
+107
@@ -0,0 +1,107 @@
|
||||
using System.Security.Claims;
|
||||
using Govor.Application.Infrastructure.Extensions;
|
||||
using Microsoft.AspNetCore.Http;
|
||||
using Moq;
|
||||
|
||||
namespace Govor.Application.Tests.Infrastructure.Extensions;
|
||||
|
||||
[TestFixture]
|
||||
[TestOf(typeof(CurrentUserSessionService))]
|
||||
public class CurrentUserSessionServiceTests
|
||||
{
|
||||
private Mock<IHttpContextAccessor> _httpContextAccessorMock = null!;
|
||||
private ICurrentUserSessionService _sessionService = null!;
|
||||
[SetUp]
|
||||
public void SetUp()
|
||||
{
|
||||
_httpContextAccessorMock = new Mock<IHttpContextAccessor>();
|
||||
_sessionService = new CurrentUserSessionService(_httpContextAccessorMock.Object);
|
||||
}
|
||||
|
||||
[Test]
|
||||
public void GetCurrentSessionId_ValidSidClaim_ReturnsGuid()
|
||||
{
|
||||
// Arrange
|
||||
var sid = Guid.NewGuid();
|
||||
var claims = new[] { new Claim("sid", sid.ToString()) };
|
||||
var identity = new ClaimsIdentity(claims);
|
||||
var principal = new ClaimsPrincipal(identity);
|
||||
|
||||
var httpContextMock = new Mock<HttpContext>();
|
||||
httpContextMock.Setup(x => x.User).Returns(principal);
|
||||
|
||||
_httpContextAccessorMock.Setup(x => x.HttpContext).Returns(httpContextMock.Object);
|
||||
|
||||
// Act
|
||||
var result = _sessionService.GetUserSessionId();
|
||||
|
||||
// Assert
|
||||
Assert.That(result, Is.EqualTo(sid));
|
||||
}
|
||||
|
||||
[Test]
|
||||
public void GetCurrentSessionId_NoHttpContext_ThrowsUnauthorizedAccessException()
|
||||
{
|
||||
// Arrange
|
||||
_httpContextAccessorMock.Setup(x => x.HttpContext).Returns((HttpContext)null);
|
||||
|
||||
// Act & Assert
|
||||
var ex = Assert.Throws<UnauthorizedAccessException>(() => _sessionService.GetUserSessionId());
|
||||
Assert.That(ex.Message, Is.EqualTo("Session id (sid) claim is missing or invalid"));
|
||||
}
|
||||
|
||||
|
||||
[Test]
|
||||
public void GetCurrentSessionId_NoSidlaim_ThrowsUnauthorizedAccessException()
|
||||
{
|
||||
// Arrange
|
||||
var claims = new[] { new Claim("otherClaim", "value") };
|
||||
var identity = new ClaimsIdentity(claims);
|
||||
var principal = new ClaimsPrincipal(identity);
|
||||
|
||||
var httpContextMock = new Mock<HttpContext>();
|
||||
httpContextMock.Setup(x => x.User).Returns(principal);
|
||||
|
||||
_httpContextAccessorMock.Setup(x => x.HttpContext).Returns(httpContextMock.Object);
|
||||
|
||||
// Act & Assert
|
||||
var ex = Assert.Throws<UnauthorizedAccessException>(() => _sessionService.GetUserSessionId());
|
||||
Assert.That(ex.Message, Is.EqualTo("Session id (sid) claim is missing or invalid"));
|
||||
}
|
||||
|
||||
[Test]
|
||||
public void GetUserSessionId_InvalidSidValueClaim_ThrowsUnauthorizedAccessException()
|
||||
{
|
||||
// Arrange
|
||||
var claims = new[] { new Claim("sid", "invalid-guid") };
|
||||
var identity = new ClaimsIdentity(claims);
|
||||
var principal = new ClaimsPrincipal(identity);
|
||||
|
||||
var httpContextMock = new Mock<HttpContext>();
|
||||
httpContextMock.Setup(x => x.User).Returns(principal);
|
||||
|
||||
_httpContextAccessorMock.Setup(x => x.HttpContext).Returns(httpContextMock.Object);
|
||||
|
||||
// Act & Assert
|
||||
var ex = Assert.Throws<UnauthorizedAccessException>(() => _sessionService.GetUserSessionId());
|
||||
Assert.That(ex.Message, Is.EqualTo("Session id (sid) claim is missing or invalid"));
|
||||
}
|
||||
|
||||
[Test]
|
||||
public void GetUserSessionId_EmptySidValueClaim_ThrowsUnauthorizedAccessException()
|
||||
{
|
||||
// Arrange
|
||||
var claims = new[] { new Claim("sid", "") };
|
||||
var identity = new ClaimsIdentity(claims);
|
||||
var principal = new ClaimsPrincipal(identity);
|
||||
|
||||
var httpContextMock = new Mock<HttpContext>();
|
||||
httpContextMock.Setup(x => x.User).Returns(principal);
|
||||
|
||||
_httpContextAccessorMock.Setup(x => x.HttpContext).Returns(httpContextMock.Object);
|
||||
|
||||
// Act & Assert
|
||||
var ex = Assert.Throws<UnauthorizedAccessException>(() => _sessionService.GetUserSessionId());
|
||||
Assert.That(ex.Message, Is.EqualTo("Session id (sid) claim is missing or invalid"));
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,52 @@
|
||||
using Govor.Domain.Common;
|
||||
using Microsoft.Extensions.Configuration;
|
||||
|
||||
namespace Govor.Application.Tests.Unit.Infrastructure.Validators;
|
||||
|
||||
[TestFixture]
|
||||
public class UsernameBoundaryTests
|
||||
{
|
||||
private static Dictionary<string, string?> Settings() => new()
|
||||
{
|
||||
["UsernameModeration:BlockedExact:0"] = "админ",
|
||||
["UsernameModeration:BlockedContains:0"] = "запрет",
|
||||
["UsernameModeration:Reserved:0"] = "логин"
|
||||
};
|
||||
private static UsernameValidator Validator() => new(new ConfigurationBuilder().AddInMemoryCollection(Settings()).Build());
|
||||
|
||||
[Test]
|
||||
public void NameLengthBoundariesAndNullReturnValidationResults()
|
||||
{
|
||||
var validator = Validator();
|
||||
var longest = string.Concat(Enumerable.Repeat("Иван", 11));
|
||||
Assert.That(validator.Validate("Иван").IsSuccess, Is.True);
|
||||
Assert.That(validator.Validate(longest).IsSuccess, Is.True);
|
||||
foreach (var invalid in new[] { null, "", " ", "Ива", longest + "а" })
|
||||
{
|
||||
var result = validator.Validate(invalid!);
|
||||
Assert.That(result.IsFailure, Is.True);
|
||||
Assert.That(result.Error.Type, Is.EqualTo(ErrorType.Validation));
|
||||
Assert.That(validator.TryValidate(invalid!), Is.False);
|
||||
}
|
||||
}
|
||||
|
||||
[TestCase("Абббб", true)]
|
||||
[TestCase("Аббббб", false)]
|
||||
[TestCase("Иван0", true)]
|
||||
[TestCase("Админ", false)]
|
||||
[TestCase("ЛОГИН", false)]
|
||||
[TestCase("Запрет123", false)]
|
||||
public void SyntaxRepetitionAndModerationAreApplied(string name, bool valid) =>
|
||||
Assert.That(Validator().Validate(name).IsSuccess, Is.EqualTo(valid));
|
||||
|
||||
[TestCase("BlockedExact")]
|
||||
[TestCase("BlockedContains")]
|
||||
[TestCase("Reserved")]
|
||||
public void MissingModerationSectionFailsConfiguration(string section)
|
||||
{
|
||||
var settings = Settings();
|
||||
settings.Remove("UsernameModeration:" + section + ":0");
|
||||
Assert.Throws<InvalidOperationException>(() => new UsernameValidator(new ConfigurationBuilder().AddInMemoryCollection(settings).Build()));
|
||||
}
|
||||
}
|
||||
|
||||
+107
@@ -0,0 +1,107 @@
|
||||
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 = null!;
|
||||
[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_ReturnsSuccess(string username)
|
||||
{
|
||||
Assert.That(_validator.Validate(username).IsSuccess, Is.True);
|
||||
}
|
||||
|
||||
[TestCase("Ivan")] // не кириллица
|
||||
[TestCase("123Иван")] // начинается не с буквы
|
||||
[TestCase("!@#$")] // спецсимволы
|
||||
[TestCase("")] // пусто
|
||||
[TestCase("И")] // меньше минимума
|
||||
[TestCase("ИванИванИванИванИванИванИванИванИванИванИванИванИван")] // больше максимума (44 символа)
|
||||
public void Validate_InvalidUsernames_ReturnsFailure(string username)
|
||||
{
|
||||
var result = _validator.Validate(username);
|
||||
Assert.That(result.IsFailure, Is.True);
|
||||
Assert.That(result.Error.Type, Is.EqualTo(Govor.Domain.Common.ErrorType.Validation));
|
||||
}
|
||||
|
||||
[TestCase("Ааааааа")]
|
||||
[TestCase("Бbbbb")]
|
||||
public void Validate_RepeatingCharacters_ReturnsFailure(string username)
|
||||
{
|
||||
var result = _validator.Validate(username);
|
||||
Assert.That(result.IsFailure, Is.True);
|
||||
Assert.That(result.Error.Type, Is.EqualTo(Govor.Domain.Common.ErrorType.Validation));
|
||||
}
|
||||
|
||||
[TestCase("Админ")]
|
||||
[TestCase("Модератор")]
|
||||
public void Validate_BlockedExact_ReturnsFailure(string username)
|
||||
{
|
||||
var result = _validator.Validate(username);
|
||||
Assert.That(result.IsFailure, Is.True);
|
||||
Assert.That(result.Error.Type, Is.EqualTo(Govor.Domain.Common.ErrorType.Validation));
|
||||
}
|
||||
|
||||
[TestCase("Гитлер123")]
|
||||
[TestCase("Нацист")]
|
||||
public void Validate_BlockedContains_ReturnsFailure(string username)
|
||||
{
|
||||
var result = _validator.Validate(username);
|
||||
Assert.That(result.IsFailure, Is.True);
|
||||
Assert.That(result.Error.Type, Is.EqualTo(Govor.Domain.Common.ErrorType.Validation));
|
||||
}
|
||||
|
||||
[TestCase("Логин")]
|
||||
[TestCase("Регистрация")]
|
||||
public void Validate_ReservedNames_ReturnsFailure(string username)
|
||||
{
|
||||
var result = _validator.Validate(username);
|
||||
Assert.That(result.IsFailure, Is.True);
|
||||
Assert.That(result.Error.Type, Is.EqualTo(Govor.Domain.Common.ErrorType.Validation));
|
||||
}
|
||||
|
||||
[TestCase("Г1тлер")]
|
||||
public void Validate_Normalization_ShouldDetectBlockedWord(string username)
|
||||
{
|
||||
var result = _validator.Validate(username);
|
||||
Assert.That(result.IsFailure, Is.True);
|
||||
Assert.That(result.Error.Type, Is.EqualTo(Govor.Domain.Common.ErrorType.Validation));
|
||||
}
|
||||
|
||||
[TestCase("Иван", ExpectedResult = true)]
|
||||
[TestCase("1234", ExpectedResult = false)]
|
||||
[TestCase("Админ", ExpectedResult = false)]
|
||||
[TestCase("Гитлер123", ExpectedResult = false)]
|
||||
public bool TryValidate_ShouldReturnExpectedResult(string username)
|
||||
{
|
||||
return _validator.TryValidate(username);
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user