|
@@ -3,13 +3,17 @@ using Hotline.Api.Controllers;
|
|
|
using Hotline.Application.Tests.Infrastructure;
|
|
|
using Hotline.Identity.Accounts;
|
|
|
using Hotline.Identity.Roles;
|
|
|
+using Hotline.Settings;
|
|
|
using Hotline.Share.Dtos.Users;
|
|
|
using Hotline.Share.Enums.Order;
|
|
|
using Hotline.Share.Enums.User;
|
|
|
using Hotline.Users;
|
|
|
+using IdentityModel;
|
|
|
using Microsoft.AspNetCore.Http;
|
|
|
using Microsoft.AspNetCore.Mvc;
|
|
|
using Microsoft.Extensions.DependencyInjection;
|
|
|
+using System.Security.Claims;
|
|
|
+using XF.Domain.Authentications;
|
|
|
using XF.Domain.Repository;
|
|
|
|
|
|
namespace Hotline.Application.Tests;
|
|
@@ -21,9 +25,11 @@ public class TestBase
|
|
|
public readonly UserController _userController;
|
|
|
public readonly IFixture _fixture;
|
|
|
private readonly IServiceScopeFactory _scopeFactory;
|
|
|
+ public readonly IHttpContextAccessor _httpContextAccessor;
|
|
|
|
|
|
- public TestBase(IAccountRepository accountRepository, IRepository<Role> roleRepository, UserController userController, IServiceScopeFactory scopeFactory, IRepository<User> userRepository)
|
|
|
+ public TestBase(IAccountRepository accountRepository, IRepository<Role> roleRepository, UserController userController, IServiceScopeFactory scopeFactory, IRepository<User> userRepository, IHttpContextAccessor httpContextAccessor)
|
|
|
{
|
|
|
+
|
|
|
_fixture = new Fixture();
|
|
|
_accountRepository = accountRepository;
|
|
|
_roleRepository = roleRepository;
|
|
@@ -34,6 +40,13 @@ public class TestBase
|
|
|
};
|
|
|
_scopeFactory = scopeFactory;
|
|
|
_userRepository = userRepository;
|
|
|
+ _httpContextAccessor = httpContextAccessor;
|
|
|
+
|
|
|
+ if (httpContextAccessor.HttpContext is null)
|
|
|
+ {
|
|
|
+ httpContextAccessor.HttpContext = new DefaultHttpContext();
|
|
|
+ SetZuoXi();
|
|
|
+ }
|
|
|
}
|
|
|
|
|
|
public void SetPaiDanYuan()
|
|
@@ -85,15 +98,33 @@ public class TestBase
|
|
|
UserName = userName
|
|
|
};
|
|
|
var accountId = _userController.Add(newUser).GetAwaiter().GetResult();
|
|
|
- TestSessionConstants.UserId = accountId;
|
|
|
+ // TestSessionConstants.UserId = accountId;
|
|
|
account = _accountRepository.GetExtAsync(
|
|
|
d => d.UserName == userName,
|
|
|
d => d.Includes(x => x.Roles)).GetAwaiter().GetResult();
|
|
|
}
|
|
|
var user = _userRepository.GetAsync(account.Id).GetAwaiter().GetResult();
|
|
|
- TestSessionConstants.UserId = account.Id;
|
|
|
- TestSessionConstants.Roles = account.Roles.Select(m => m.Id).ToArray();
|
|
|
- TestSessionConstants.UserName = account.UserName;
|
|
|
- TestSessionConstants.OrgId = user.OrgId;
|
|
|
+
|
|
|
+ List<Claim> userClaims = [
|
|
|
+ new(JwtClaimTypes.Subject, account.Id),
|
|
|
+ new(JwtClaimTypes.PhoneNumber, account.PhoneNo ?? string.Empty),
|
|
|
+ new(ClaimTypes.NameIdentifier, user.Id),
|
|
|
+ new(AppClaimTypes.UserDisplayName, account.Name),
|
|
|
+ new(AppClaimTypes.DepartmentId, user.OrgId ?? string.Empty),
|
|
|
+ new(AppClaimTypes.DepartmentIsCenter, user.Organization?.IsCenter.ToString() ?? false.ToString()),
|
|
|
+ new(AppClaimTypes.DepartmentName, user.Organization?.Name ?? string.Empty),
|
|
|
+ new(AppClaimTypes.DepartmentAreaCode, user.Organization?.AreaCode ?? string.Empty),
|
|
|
+ new(AppClaimTypes.DepartmentAreaName, user.Organization?.AreaName ?? string.Empty),
|
|
|
+ new(AppClaimTypes.DepartmentLevel, user.Organization?.Level.ToString() ?? string.Empty),
|
|
|
+ new(AppClaimTypes.AreaId, user.OrgId?.GetHigherOrgId() ?? string.Empty),
|
|
|
+ ];
|
|
|
+ ClaimsIdentity identity = new ClaimsIdentity(userClaims);
|
|
|
+ var principal = new ClaimsPrincipal(identity);
|
|
|
+ _httpContextAccessor.HttpContext.User = principal;
|
|
|
+
|
|
|
+ //TestSessionConstants.UserId = account.Id;
|
|
|
+ //TestSessionConstants.Roles = account.Roles.Select(m => m.Id).ToArray();
|
|
|
+ //TestSessionConstants.UserName = account.UserName;
|
|
|
+ //TestSessionConstants.OrgId = user.OrgId;
|
|
|
}
|
|
|
}
|