-
Notifications
You must be signed in to change notification settings - Fork 10
Expand file tree
/
Copy pathIntegrationTestWebAppFactory.cs
More file actions
96 lines (79 loc) · 3.65 KB
/
IntegrationTestWebAppFactory.cs
File metadata and controls
96 lines (79 loc) · 3.65 KB
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
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
using Microsoft.AspNetCore.Hosting;
using Microsoft.AspNetCore.Mvc.Testing;
using Microsoft.AspNetCore.TestHost;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.DependencyInjection.Extensions;
using Microsoft.Extensions.Http;
using OpenShock.API.IntegrationTests.HttpMessageHandlers;
using OpenShock.Common.Services.Turnstile;
using Testcontainers.PostgreSql;
using Testcontainers.Redis;
using TUnit.Core.Interfaces;
namespace API.IntegrationTests;
public class IntegrationTestWebAppFactory : WebApplicationFactory<Program>, IAsyncInitializer
{
private readonly PostgreSqlContainer _dbContainer = new PostgreSqlBuilder()
.WithImage("postgres:latest")
.WithDatabase("openshock")
.WithUsername("openshock")
.WithPassword("superSecurePassword")
.Build();
private readonly RedisContainer _redisContainer = new RedisBuilder()
.WithImage("redis/redis-stack-server:latest")
.Build();
public async Task InitializeAsync()
{
await _dbContainer.StartAsync();
await _redisContainer.StartAsync();
}
protected override IWebHostBuilder? CreateWebHostBuilder()
{
var environmentVariables = new Dictionary<string, string>
{
{ "ASPNETCORE_UNDER_INTEGRATION_TEST", "1" },
{ "OPENSHOCK__DB__CONN", _dbContainer.GetConnectionString() },
{ "OPENSHOCK__DB__SKIPMIGRATION", "false" },
{ "OPENSHOCK__DB__DEBUG", "false" },
{ "OPENSHOCK__REDIS__CONN", _redisContainer.GetConnectionString() },
{ "OPENSHOCK__REDIS__HOST", "" },
{ "OPENSHOCK__REDIS__USER", "" },
{ "OPENSHOCK__REDIS__PASSWORD", "" },
{ "OPENSHOCK__REDIS__PORT", "6379" },
{ "OPENSHOCK__FRONTEND__BASEURL", "https://openshock.app" },
{ "OPENSHOCK__FRONTEND__SHORTURL", "https://openshock.app" },
{ "OPENSHOCK__FRONTEND__COOKIEDOMAIN", "openshock.app" },
{ "OPENSHOCK__MAIL__TYPE", "MAILJET" },
{ "OPENSHOCK__MAIL__SENDER__EMAIL", "system@openshock.org" },
{ "OPENSHOCK__MAIL__SENDER__NAME", "OpenShock" },
{ "OPENSHOCK__MAIL__MAILJET__KEY", "mailjet-key" },
{ "OPENSHOCK__MAIL__MAILJET__SECRET", "mailjet-secret" },
{ "OPENSHOCK__MAIL__MAILJET__TEMPLATE__PASSWORDRESET", "12345678" },
{ "OPENSHOCK__MAIL__MAILJET__TEMPLATE__PASSWORDRESETCOMPLETE", "87654321" },
{ "OPENSHOCK__MAIL__MAILJET__TEMPLATE__VERIFYEMAIL", "11223344" },
{ "OPENSHOCK__MAIL__MAILJET__TEMPLATE__VERIFYEMAILCOMPLETE", "44332211" },
{ "OPENSHOCK__TURNSTILE__ENABLED", "true" },
{ "OPENSHOCK__TURNSTILE__SECRETKEY", "turnstile-secret-key" },
{ "OPENSHOCK__TURNSTILE__SITEKEY", "turnstile-site-key" },
{ "OPENSHOCK__LCG__FQDN", "de1-gateway.my-openshock-instance.net" },
{ "OPENSHOCK__LCG__COUNTRYCODE", "DE" }
};
foreach (var envVar in environmentVariables)
{
Environment.SetEnvironmentVariable(envVar.Key, envVar.Value);
}
return base.CreateWebHostBuilder();
}
protected override void ConfigureWebHost(IWebHostBuilder builder)
{
builder.ConfigureTestServices(services =>
{
services.AddTransient<HttpMessageHandlerBuilder, InterceptedHttpMessageHandlerBuilder>();
});
}
public override async ValueTask DisposeAsync()
{
await _dbContainer.DisposeAsync();
await _redisContainer.DisposeAsync();
await base.DisposeAsync();
}
}