/
softailor
/
gridient
Обзор
Документация
Войти
/
softailor
/
gridient
Код
Запросы
0
Задачи
Вики
Пакеты
0
Релизы
0
CI/CD
Аналитика
Безопасность
master
backend/Gridient.Core.Tests/Controllers/ListsBaseControllerTests.cs
100 строк
4 KB
Jenkins
Auto add
18 фев 2026, 10:19
18 фев 2026, 10:19
1e6c4f8
Код
Авторство
О чём код?
using FluentAssertions; using Gridient.Core.Controllers; using Gridient.Core.Exceptions; using Microsoft.AspNetCore.Http; using Microsoft.AspNetCore.Mvc; using System.Security.Claims; using Xunit; namespace Gridient.Core.Tests.Controllers { public class ListsBaseControllerTests { private sealed class TestController : ListsBaseController { public string GetLogin() => CurrentUserLogin; } private static TestController CreateControllerWithUser(ClaimsPrincipal principal) { var c = new TestController { ControllerContext = new ControllerContext { HttpContext = new DefaultHttpContext { User = principal } } }; return c; } #region CurrentUserLogin /// <summary> /// Проверяет, что CurrentUserLogin возвращает User.Identity.Name, если он задан. /// </summary> [Fact] public void CurrentUserLogin_WhenIdentityNamePresent_ShouldReturnIt() { var principal = new ClaimsPrincipal(new ClaimsIdentity( new[] { new Claim(ClaimTypes.Name, "u") }, authenticationType: "Test")); var controller = CreateControllerWithUser(principal); controller.GetLogin().Should().Be("u"); } /// <summary> /// Проверяет, что если Identity.Name пустой (из-за нестандартного NameClaimType), то CurrentUserLogin берет ClaimTypes.Name. /// </summary> [Fact] public void CurrentUserLogin_WhenIdentityNameMissing_ShouldFallbackToNameClaim() { var identity = new ClaimsIdentity( claims: new[] { new Claim(ClaimTypes.Name, "u") }, authenticationType: "Test", nameType: "custom_name_type", roleType: ClaimTypes.Role); var principal = new ClaimsPrincipal(identity); var controller = CreateControllerWithUser(principal); controller.GetLogin().Should().Be("u"); } /// <summary> /// Проверяет, что если ClaimTypes.Name отсутствует, то CurrentUserLogin берет preferred_username. /// </summary> [Fact] public void CurrentUserLogin_WhenNameClaimMissing_ShouldFallbackToPreferredUsername() { var identity = new ClaimsIdentity( claims: new[] { new Claim("preferred_username", "pref") }, authenticationType: "Test", nameType: "custom_name_type", roleType: ClaimTypes.Role); var principal = new ClaimsPrincipal(identity); var controller = CreateControllerWithUser(principal); controller.GetLogin().Should().Be("pref"); } /// <summary> /// Проверяет, что если логин не найден ни в Identity.Name, ни в клеймах, то выбрасывается ListsException с ожидаемым сообщением. /// </summary> [Fact] public void CurrentUserLogin_WhenLoginNotFound_ShouldThrow() { var principal = new ClaimsPrincipal(new ClaimsIdentity()); // no name, no claims var controller = CreateControllerWithUser(principal); Action act = () => controller.GetLogin(); act.Should().Throw<ListsException>() .WithMessage("Пользователь не авторизован!"); } #endregion } }