TestBase.cs 8.5 KB

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