aboutsummaryrefslogtreecommitdiff
path: root/Timeline.Tests/Helpers/WebApplicationFactoryExtensions.cs
blob: 4a7f87fb5e935472a3d5c33e910896875c3327ec (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
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
{
    public static class WebApplicationFactoryExtensions
    {
        public static WebApplicationFactory<TEntry> WithTestConfig<TEntry>(this WebApplicationFactory<TEntry> factory, ITestOutputHelper outputHelper) where TEntry : class
        {
            return factory.WithWebHostBuilder(builder =>
            {
                builder
                    .UseEnvironment(EnvironmentConstants.TestEnvironmentName)
                    .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();
                        }
                    });
            });
        }
    }
}