aboutsummaryrefslogtreecommitdiff
path: root/Timeline.Tests/Helpers/WebApplicationFactoryExtensions.cs
diff options
context:
space:
mode:
Diffstat (limited to 'Timeline.Tests/Helpers/WebApplicationFactoryExtensions.cs')
-rw-r--r--Timeline.Tests/Helpers/WebApplicationFactoryExtensions.cs50
1 files changed, 50 insertions, 0 deletions
diff --git a/Timeline.Tests/Helpers/WebApplicationFactoryExtensions.cs b/Timeline.Tests/Helpers/WebApplicationFactoryExtensions.cs
index bb8fc71b..4a7f87fb 100644
--- a/Timeline.Tests/Helpers/WebApplicationFactoryExtensions.cs
+++ b/Timeline.Tests/Helpers/WebApplicationFactoryExtensions.cs
@@ -1,6 +1,10 @@
using Microsoft.AspNetCore.Hosting;
using Microsoft.AspNetCore.Mvc.Testing;
+using Microsoft.EntityFrameworkCore;
+using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Logging;
+using Timeline.Models;
+using Timeline.Services;
using Xunit.Abstractions;
namespace Timeline.Tests.Helpers
@@ -16,6 +20,52 @@ namespace Timeline.Tests.Helpers
.ConfigureLogging(logging =>
{
logging.AddXunit(outputHelper);
+ })
+ .ConfigureServices(services =>
+ {
+ var serviceProvider = new ServiceCollection()
+ .AddEntityFrameworkInMemoryDatabase()
+ .BuildServiceProvider();
+
+ services.AddDbContext<DatabaseContext>(options =>
+ {
+ options.UseInMemoryDatabase("timeline");
+ options.UseInternalServiceProvider(serviceProvider);
+ });
+
+ var sp = services.BuildServiceProvider();
+
+ // Create a scope to obtain a reference to the database
+ // context (ApplicationDbContext).
+ using (var scope = sp.CreateScope())
+ {
+ var scopedServices = scope.ServiceProvider;
+ var db = scopedServices.GetRequiredService<DatabaseContext>();
+
+ var passwordService = new PasswordService(null);
+
+ // Ensure the database is created.
+ db.Database.EnsureCreated();
+
+ db.Users.AddRange(new User[] {
+ new User
+ {
+ Id = 0,
+ Name = "user",
+ EncryptedPassword = passwordService.HashPassword("user"),
+ RoleString = "user"
+ },
+ new User
+ {
+ Id = 0,
+ Name = "admin",
+ EncryptedPassword = passwordService.HashPassword("admin"),
+ RoleString = "user,admin"
+ }
+ });
+
+ db.SaveChanges();
+ }
});
});
}