TestBase.cs 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232
  1. using System.Security.Claims;
  2. using AutoFixture;
  3. using Hotline.Api.Controllers;
  4. using Hotline.Identity.Accounts;
  5. using Hotline.Identity.Roles;
  6. using Hotline.Settings;
  7. using Hotline.Share.Dtos.Users;
  8. using Hotline.Share.Enums.Order;
  9. using Hotline.Share.Enums.ThirdAccount;
  10. using Hotline.Share.Enums.User;
  11. using Hotline.Share.Tools;
  12. using Hotline.Snapshot.IRepository;
  13. using Hotline.Tests.Infrastructure;
  14. using Hotline.ThirdAccountDomainServices;
  15. using Hotline.ThirdAccountDomainServices.Interfaces;
  16. using Hotline.Users;
  17. using IdentityModel;
  18. using Microsoft.AspNetCore.Http;
  19. using Microsoft.AspNetCore.Mvc;
  20. using Microsoft.Extensions.DependencyInjection;
  21. using XF.Domain.Authentications;
  22. using XF.Domain.Cache;
  23. using XF.Domain.Repository;
  24. namespace Hotline.Tests;
  25. public class TestBase
  26. {
  27. public readonly IAccountRepository _accountRepository;
  28. public readonly IRepository<User> _userRepository;
  29. public readonly IRepository<Role> _roleRepository;
  30. public readonly UserController _userController;
  31. public readonly IFixture _fixture;
  32. private readonly IServiceScopeFactory _scopeFactory;
  33. public readonly IHttpContextAccessor _httpContextAccessor;
  34. public readonly IThirdIdentiyService _thirdIdentiyService;
  35. public readonly IThirdAccountRepository _thirdAccountRepository;
  36. private readonly ITypedCache<SystemSetting> _cacheSettingData;
  37. public readonly ThirdAccounSupplierFactory _thirdAccountDomainFactory;
  38. public readonly IServiceProvider _serviceProvider;
  39. public TestBase(IAccountRepository accountRepository, IRepository<Role> roleRepository, UserController userController, IServiceScopeFactory scopeFactory, IRepository<User> userRepository, IHttpContextAccessor httpContextAccessor, IThirdIdentiyService thirdIdentiyService, IThirdAccountRepository thirdAccountRepository, ITypedCache<SystemSetting> cacheSettingData, ThirdAccounSupplierFactory thirdAccountDomainFactory, IServiceProvider serviceProvider)
  40. {
  41. _thirdAccountRepository = thirdAccountRepository;
  42. _thirdIdentiyService = thirdIdentiyService;
  43. _fixture = new Fixture();
  44. _accountRepository = accountRepository;
  45. _roleRepository = roleRepository;
  46. _userController = userController;
  47. _userController.ControllerContext = new ControllerContext
  48. {
  49. HttpContext = new DefaultHttpContext
  50. {
  51. RequestServices = _serviceProvider
  52. }
  53. };
  54. _scopeFactory = scopeFactory;
  55. _userRepository = userRepository;
  56. _httpContextAccessor = httpContextAccessor;
  57. if (httpContextAccessor.HttpContext is null)
  58. {
  59. httpContextAccessor.HttpContext = new DefaultHttpContext
  60. {
  61. RequestServices = _serviceProvider
  62. };
  63. SetZuoXi();
  64. }
  65. _cacheSettingData = cacheSettingData;
  66. _thirdAccountDomainFactory = thirdAccountDomainFactory;
  67. _serviceProvider = serviceProvider;
  68. }
  69. public void SetSettingCache(string code, string value)
  70. {
  71. _cacheSettingData.Remove(code);
  72. var a = new SystemSetting { SettingValue = new List<string> { value } };
  73. _cacheSettingData.Set(code, a);
  74. }
  75. public void ChangeAppScopeYiBin()
  76. {
  77. ChangeAppScope("YiBin");
  78. }
  79. public void ChangeAppScope(string appScope)
  80. {
  81. var changeDto = _httpContextAccessor.HttpContext.User;
  82. List<Claim> userClaims = [
  83. new(JwtClaimTypes.Subject, changeDto.FindFirstValue(JwtClaimTypes.Subject)),
  84. new(JwtClaimTypes.PhoneNumber, changeDto.FindFirstValue(JwtClaimTypes.PhoneNumber)),
  85. new(ClaimTypes.NameIdentifier, changeDto.FindFirstValue(ClaimTypes.NameIdentifier)),
  86. new(AppClaimTypes.UserDisplayName, changeDto.FindFirstValue(AppClaimTypes.UserDisplayName)),
  87. new(AppClaimTypes.DepartmentId, changeDto.FindFirstValue(AppClaimTypes.DepartmentId)),
  88. new(AppClaimTypes.DepartmentIsCenter, changeDto.FindFirstValue(AppClaimTypes.DepartmentIsCenter)),
  89. new(AppClaimTypes.DepartmentName, changeDto.FindFirstValue(AppClaimTypes.DepartmentName)),
  90. new(AppClaimTypes.DepartmentAreaCode, changeDto.FindFirstValue(AppClaimTypes.DepartmentAreaCode)),
  91. new(AppClaimTypes.DepartmentAreaName, changeDto.FindFirstValue(AppClaimTypes.DepartmentAreaName)),
  92. new(AppClaimTypes.DepartmentLevel, changeDto.FindFirstValue(AppClaimTypes.DepartmentLevel)),
  93. new(AppClaimTypes.AreaId, changeDto.FindFirstValue(AppClaimTypes.AreaId)),
  94. new(AppClaimTypes.OpenId, changeDto.FindFirstValue(AppClaimTypes.OpenId)),
  95. new("AppScope", appScope)
  96. ];
  97. ClaimsIdentity identity = new ClaimsIdentity(userClaims);
  98. var principal = new ClaimsPrincipal(identity);
  99. _httpContextAccessor.HttpContext.User = principal;
  100. }
  101. public void ChangeAppScopeZiGong()
  102. {
  103. ChangeAppScope("ZiGong");
  104. }
  105. public void SetPaiDanYuan()
  106. {
  107. SetOperator("派单员", "市民热线服务中心", "单元测试派单员", "001", "13408389849", EUserType.Normal, TestSettingConstants.PaiDanYuanAccountName);
  108. }
  109. public void Set一级部门()
  110. {
  111. SetOperator("部门经办人", "测试部门", "测试", "001094", "13408389849", EUserType.Seat, TestSettingConstants.FirstOrgAccountName);
  112. }
  113. public void Set二级部门()
  114. {
  115. SetOperator("部门经办人", "测试部门/测试二级部门", "cs21", "001094001", "13408389849", EUserType.Normal, TestSettingConstants.SecondOrgAccountName);
  116. }
  117. public void Set班长()
  118. {
  119. SetOperator("中心班长", "市民热线服务中心", "单元测试班长", "001", "13408389849", EUserType.Normal, TestSettingConstants.BanZhangAccountName);
  120. }
  121. public void SetZuoXi()
  122. {
  123. SetOperator("坐席", "市民热线服务中心", "单元测试坐席", "001", "13408389849", EUserType.Seat, TestSettingConstants.ZuoXiAccountName);
  124. }
  125. public void Set政法委()
  126. {
  127. SetOperator("区县政法委", "自流井区人民政府/自流井区政法委", "单元测试政法委", "001055048", "13408389849", EUserType.Normal, TestSettingConstants.ZFWAccountName);
  128. }
  129. public void Set应急管理局()
  130. {
  131. SetOperator("二级部门坐席", "自流井区应急管理局", "单元测试应急管理局", "001055021", "13408389849", EUserType.Normal, TestSettingConstants.YJGLJAccountName);
  132. }
  133. public void SetWeiXin()
  134. {
  135. SetOperator("微信用户", "", "", "", "138001389877", EUserType.Normal, TestSettingConstants.WeiXinAccountName, EThirdType.WeChat, EAppType.Snapshot);
  136. }
  137. public void Set网格员()
  138. {
  139. SetOperator("网格员", "市民热线服务中心", "单元测试网格员", "001", "13408389849", EUserType.Normal, TestSettingConstants.GuiderAccountName);
  140. }
  141. private void SetOperator(string displayName, string fullOrgName, string name, string orgId, string phoneNo, EUserType userType, string userName, EThirdType? thirdType = null, EAppType? appType = null)
  142. {
  143. var account = _accountRepository.GetExtAsync(
  144. d => d.UserName == userName,
  145. d => d.Includes(x => x.Roles)).GetAwaiter().GetResult();
  146. var roleId = _roleRepository.Queryable()
  147. .Where(m => m.DisplayName == displayName)
  148. .Select(m => m.Id)
  149. .First();
  150. if (account == null && roleId != null)
  151. {
  152. var newUser = new AddUserDto
  153. {
  154. FullOrgName = fullOrgName,
  155. Gender = EGender.Male,
  156. Name = name,
  157. OrgId = orgId,
  158. PhoneNo = phoneNo,
  159. RoleIds = new[] { roleId },
  160. UserType = userType,
  161. UserName = userName
  162. };
  163. var accountId = _userController.Add(newUser).GetAwaiter().GetResult();
  164. account = _accountRepository.GetExtAsync(
  165. d => d.UserName == userName,
  166. d => d.Includes(x => x.Roles)).GetAwaiter().GetResult();
  167. }
  168. var user = _userRepository.Queryable()
  169. .Includes(d => d.Organization)
  170. .FirstAsync(d => d.Id == account.Id).GetAwaiter().GetResult();
  171. var third = _thirdIdentiyService.GetTokenAsync(new Share.Dtos.Snapshot.ThirdTokenDto(), CancellationToken.None).GetAwaiter().GetResult();
  172. var thirdAccount = _thirdAccountRepository.Get(d => d.OpenId == third.OpenId);
  173. var appScope = "ZiGong";
  174. if (_httpContextAccessor != null && _httpContextAccessor.HttpContext != null)
  175. {
  176. var m = _httpContextAccessor.HttpContext.User.FindFirstValue("AppScope");
  177. if (m != null)
  178. appScope = m;
  179. }
  180. List<Claim> userClaims = [
  181. new(JwtClaimTypes.Subject, account.Id ?? thirdAccount.Id),
  182. new(JwtClaimTypes.PhoneNumber, account.PhoneNo ?? thirdAccount.PhoneNumber),
  183. new(AppClaimTypes.UserDisplayName, account.Name),
  184. new(AppClaimTypes.DepartmentId, user.OrgId ?? string.Empty),
  185. new(AppClaimTypes.DepartmentIsCenter, user.Organization?.IsCenter.ToString() ?? false.ToString()),
  186. new(AppClaimTypes.DepartmentName, user.Organization?.Name ?? string.Empty),
  187. new(AppClaimTypes.DepartmentAreaCode, user.Organization?.AreaCode ?? string.Empty),
  188. new(AppClaimTypes.DepartmentAreaName, user.Organization?.AreaName ?? string.Empty),
  189. new(AppClaimTypes.DepartmentLevel, user.Organization?.Level.ToString() ?? string.Empty),
  190. new(AppClaimTypes.AreaId, user.OrgId?.GetHigherOrgId() ?? string.Empty),
  191. new(AppClaimTypes.OpenId, thirdAccount?.OpenId ?? string.Empty),
  192. new("AppScope", appScope)
  193. ];
  194. if (appType != null && thirdType != null)
  195. {
  196. var claims = new List<Claim>();
  197. var userId = _thirdAccountDomainFactory.GetClaimAsync(new ThirdAccount { Id = thirdAccount?.Id, PhoneNumber = phoneNo, AccountType = thirdType.Value, AppType = appType.Value, ExternalId = thirdAccount?.ExternalId }, claims, CancellationToken.None).GetAwaiter().GetResult();
  198. userClaims.Add(new(ClaimTypes.NameIdentifier, userId));
  199. }
  200. else
  201. {
  202. userClaims.Add(new(ClaimTypes.NameIdentifier, user.Id));
  203. }
  204. userClaims.AddRange(account.Roles.Select(d => new Claim(JwtClaimTypes.Role, d.Name)));
  205. ClaimsIdentity identity = new ClaimsIdentity(userClaims);
  206. var principal = new ClaimsPrincipal(identity);
  207. _httpContextAccessor.HttpContext.User = principal;
  208. }
  209. }