VberAdminTestBase.cs 5.6 KB

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