/
softailor
/
gridient
Обзор
Документация
Войти
/
softailor
/
gridient
Код
Запросы
0
Задачи
Вики
Пакеты
0
Релизы
0
CI/CD
Аналитика
Безопасность
master
backend/Gridient.Core.Tests/Controllers/ControllerTestBase.cs
40 строк
1 KB
Jenkins
Auto add
18 фев 2026, 10:19
18 фев 2026, 10:19
1e6c4f8
Код
Авторство
О чём код?
using Microsoft.AspNetCore.Http; using Microsoft.AspNetCore.Mvc; using System.Security.Claims; namespace Gridient.Core.Tests.Controllers { /// <summary> /// Базовый класс для тестов контроллеров с общими методами настройки /// </summary> public abstract class ControllerTestBase { /// <summary> /// Настраивает ControllerContext с авторизованным пользователем /// </summary> protected void SetupControllerContext(ControllerBase controller, string userName, params string[] roles) { var claims = new List<Claim> { new Claim(ClaimTypes.Name, userName), new Claim(ClaimTypes.NameIdentifier, "1") }; foreach (var role in roles) { claims.Add(new Claim(ClaimTypes.Role, role)); } var identity = new ClaimsIdentity(claims, "Test"); var principal = new ClaimsPrincipal(identity); controller.ControllerContext = new ControllerContext { HttpContext = new DefaultHttpContext { User = principal } }; } } }