You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

Tests.cs 2.0KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263
  1. using System;
  2. using Luticate2.Auth.Controllers;
  3. using Luticate2.Auth.DataAccess;
  4. using Luticate2.Utils.DataAccess;
  5. using Luticate2.Utils.Interfaces;
  6. using Microsoft.EntityFrameworkCore;
  7. using Microsoft.Extensions.DependencyInjection;
  8. namespace TestAuth
  9. {
  10. public class DummyNotificationsBusiness : ILuNotificationsBusiness
  11. {
  12. public void Notify(string eventName, string entityType, object oldEntity, object newEntity)
  13. {
  14. }
  15. }
  16. public class Tests
  17. {
  18. public const string RealDbConnectionString =
  19. "User ID=dev;Password=dev;Host=localhost;Port=5432;Database=luticate2;Pooling=true;";
  20. public static IServiceProvider BuildRealDbServiceProvider()
  21. {
  22. IServiceCollection serviceCollection = new ServiceCollection();
  23. serviceCollection.AddSingleton<ILuNotificationsBusiness, DummyNotificationsBusiness>();
  24. serviceCollection.AddLuticateAuth(dbo =>
  25. {
  26. dbo.Version = "tests";
  27. }, builder =>
  28. {
  29. builder.UseNpgsql(RealDbConnectionString);
  30. }, dbo =>
  31. {
  32. dbo.SecureCookies = false;
  33. });
  34. return serviceCollection.BuildServiceProvider();
  35. }
  36. protected static void _TestRealDb(Action<IServiceProvider> func)
  37. {
  38. var serviceProvider = BuildRealDbServiceProvider();
  39. var transactionScope = serviceProvider.GetService<LuEfTransactionScope>();
  40. transactionScope.BeginTransaction<LuAuthDatabaseContext>(null);
  41. try
  42. {
  43. func(serviceProvider);
  44. }
  45. finally
  46. {
  47. transactionScope.RollbackTransaction<LuAuthDatabaseContext>();
  48. }
  49. }
  50. public static void TestRealDb<TService>(Action<TService> func)
  51. {
  52. _TestRealDb(provider =>
  53. {
  54. func(provider.GetService<TService>());
  55. });
  56. }
  57. }
  58. }