ImageServerTestBase.cs 6.6 KB

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