FangComTestBase.cs 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206
  1. using System;
  2. using System.Linq;
  3. using System.Threading.Tasks;
  4. using Microsoft.EntityFrameworkCore;
  5. using Abp;
  6. using Abp.Authorization.Users;
  7. using Abp.Events.Bus;
  8. using Abp.Events.Bus.Entities;
  9. using Abp.MultiTenancy;
  10. using Abp.Runtime.Session;
  11. using Abp.TestBase;
  12. using WTTO.FangCom.Authorization.Users;
  13. using WTTO.FangCom.EntityFrameworkCore;
  14. using WTTO.FangCom.Migrations.Seed.Host;
  15. using WTTO.FangCom.Migrations.Seed.Tenants;
  16. using WTTO.FangCom.MultiTenancy;
  17. using WTTO.FangCom.Tests.TestData;
  18. namespace WTTO.FangCom.Tests
  19. {
  20. public abstract class FangComTestBase : AbpIntegratedTestBase<FangComTestModule>
  21. {
  22. protected FangComTestBase()
  23. {
  24. SeedTestData();
  25. LoginAsDefaultTenantAdmin();
  26. }
  27. private void SeedTestData()
  28. {
  29. void NormalizeDbContext(FangComDbContext context)
  30. {
  31. context.EntityChangeEventHelper = NullEntityChangeEventHelper.Instance;
  32. context.EventBus = NullEventBus.Instance;
  33. context.SuppressAutoSetTenantId = true;
  34. }
  35. //Seed initial data for default tenant
  36. AbpSession.TenantId = 1;
  37. UsingDbContext(context =>
  38. {
  39. NormalizeDbContext(context);
  40. new TestDataBuilder(context, 1).Create();
  41. });
  42. }
  43. #region UsingDbContext
  44. protected IDisposable UsingTenantId(int? tenantId)
  45. {
  46. var previousTenantId = AbpSession.TenantId;
  47. AbpSession.TenantId = tenantId;
  48. return new DisposeAction(() => AbpSession.TenantId = previousTenantId);
  49. }
  50. protected void UsingDbContext(Action<FangComDbContext> action)
  51. {
  52. UsingDbContext(AbpSession.TenantId, action);
  53. }
  54. protected Task UsingDbContextAsync(Func<FangComDbContext, Task> action)
  55. {
  56. return UsingDbContextAsync(AbpSession.TenantId, action);
  57. }
  58. protected T UsingDbContext<T>(Func<FangComDbContext, T> func)
  59. {
  60. return UsingDbContext(AbpSession.TenantId, func);
  61. }
  62. protected Task<T> UsingDbContextAsync<T>(Func<FangComDbContext, Task<T>> func)
  63. {
  64. return UsingDbContextAsync(AbpSession.TenantId, func);
  65. }
  66. protected void UsingDbContext(int? tenantId, Action<FangComDbContext> action)
  67. {
  68. using (UsingTenantId(tenantId))
  69. {
  70. using (var context = LocalIocManager.Resolve<FangComDbContext>())
  71. {
  72. action(context);
  73. context.SaveChanges();
  74. }
  75. }
  76. }
  77. protected async Task UsingDbContextAsync(int? tenantId, Func<FangComDbContext, Task> action)
  78. {
  79. using (UsingTenantId(tenantId))
  80. {
  81. using (var context = LocalIocManager.Resolve<FangComDbContext>())
  82. {
  83. await action(context);
  84. await context.SaveChangesAsync();
  85. }
  86. }
  87. }
  88. protected T UsingDbContext<T>(int? tenantId, Func<FangComDbContext, T> func)
  89. {
  90. T result;
  91. using (UsingTenantId(tenantId))
  92. {
  93. using (var context = LocalIocManager.Resolve<FangComDbContext>())
  94. {
  95. result = func(context);
  96. context.SaveChanges();
  97. }
  98. }
  99. return result;
  100. }
  101. protected async Task<T> UsingDbContextAsync<T>(int? tenantId, Func<FangComDbContext, Task<T>> func)
  102. {
  103. T result;
  104. using (UsingTenantId(tenantId))
  105. {
  106. using (var context = LocalIocManager.Resolve<FangComDbContext>())
  107. {
  108. result = await func(context);
  109. await context.SaveChangesAsync();
  110. }
  111. }
  112. return result;
  113. }
  114. #endregion
  115. #region Login
  116. protected void LoginAsHostAdmin()
  117. {
  118. LoginAsHost(AbpUserBase.AdminUserName);
  119. }
  120. protected void LoginAsDefaultTenantAdmin()
  121. {
  122. LoginAsTenant(AbpTenantBase.DefaultTenantName, AbpUserBase.AdminUserName);
  123. }
  124. protected void LoginAsHost(string userName)
  125. {
  126. AbpSession.TenantId = null;
  127. var user =
  128. UsingDbContext(
  129. context =>
  130. context.Users.FirstOrDefault(u => u.TenantId == AbpSession.TenantId && u.UserName == userName));
  131. if (user == null)
  132. {
  133. throw new Exception("There is no user: " + userName + " for host.");
  134. }
  135. AbpSession.UserId = user.Id;
  136. }
  137. protected void LoginAsTenant(string tenancyName, string userName)
  138. {
  139. var tenant = UsingDbContext(context => context.Tenants.FirstOrDefault(t => t.TenancyName == tenancyName));
  140. if (tenant == null)
  141. {
  142. throw new Exception("There is no tenant: " + tenancyName);
  143. }
  144. AbpSession.TenantId = tenant.Id;
  145. var user =
  146. UsingDbContext(
  147. context =>
  148. context.Users.FirstOrDefault(u => u.TenantId == AbpSession.TenantId && u.UserName == userName));
  149. if (user == null)
  150. {
  151. throw new Exception("There is no user: " + userName + " for tenant: " + tenancyName);
  152. }
  153. AbpSession.UserId = user.Id;
  154. }
  155. #endregion
  156. /// <summary>
  157. /// Gets current user if <see cref="IAbpSession.UserId"/> is not null.
  158. /// Throws exception if it's null.
  159. /// </summary>
  160. protected async Task<User> GetCurrentUserAsync()
  161. {
  162. var userId = AbpSession.GetUserId();
  163. return await UsingDbContext(context => context.Users.SingleAsync(u => u.Id == userId));
  164. }
  165. /// <summary>
  166. /// Gets current tenant if <see cref="IAbpSession.TenantId"/> is not null.
  167. /// Throws exception if there is no current tenant.
  168. /// </summary>
  169. protected async Task<Tenant> GetCurrentTenantAsync()
  170. {
  171. var tenantId = AbpSession.GetTenantId();
  172. return await UsingDbContext(context => context.Tenants.SingleAsync(t => t.Id == tenantId));
  173. }
  174. }
  175. }