-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathWebApplicationFixture.cs
More file actions
79 lines (63 loc) · 3.22 KB
/
Copy pathWebApplicationFixture.cs
File metadata and controls
79 lines (63 loc) · 3.22 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
using Microsoft.AspNetCore.Authentication;
using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Hosting;
namespace Comanda.Subscriptions.TestSuite.Fixtures;
public sealed class WebApplicationFixture : IAsyncLifetime
{
private readonly MongoDatabaseFixture _databaseFixture;
public HttpClient HttpClient { get; private set; } = default!;
public IServiceProvider Services { get; private set; } = default!;
private WebApplicationFactory<Program> _factory = default!;
public WebApplicationFixture()
{
_databaseFixture = new MongoDatabaseFixture();
}
public async ValueTask InitializeAsync()
{
await _databaseFixture.InitializeAsync();
await _databaseFixture.CleanDatabaseAsync();
Environment.SetEnvironmentVariable("Settings__Database__ConnectionString", _databaseFixture.ConnectionString);
Environment.SetEnvironmentVariable("Settings__Database__DatabaseName", _databaseFixture.DatabaseName);
_factory = new WebApplicationFactory<Program>()
.WithWebHostBuilder(builder =>
{
builder.UseEnvironment("Testing");
builder.ConfigureServices(services =>
{
services.AddAuthentication(options =>
{
options.DefaultAuthenticateScheme = "vinder.internal.bypass.authentication";
options.DefaultChallengeScheme = "vinder.internal.bypass.authentication";
options.DefaultScheme = "vinder.internal.bypass.authentication";
})
.AddScheme<AuthenticationSchemeOptions, BypassAuthenticationHandler>("vinder.internal.bypass.authentication", _ => { });
services.AddAuthorization(options =>
{
options.DefaultPolicy = new AuthorizationPolicyBuilder("vinder.internal.bypass.authentication")
.RequireAuthenticatedUser()
.Build();
});
var descriptor = services.SingleOrDefault(descriptor => descriptor.ServiceType == typeof(IMongoClient));
if (descriptor is not null)
services.Remove(descriptor);
descriptor = services.SingleOrDefault(descriptor => descriptor.ServiceType == typeof(IMongoDatabase));
if (descriptor is not null)
services.Remove(descriptor);
descriptor = services.SingleOrDefault(descriptor => descriptor.ServiceType == typeof(ISubscriptionGateway));
if (descriptor is not null)
services.Remove(descriptor);
services.AddSingleton(_ => _databaseFixture.Client);
services.AddSingleton(_ => _databaseFixture.Database);
services.AddSingleton<ISubscriptionGateway, SubscriptionGatewayMock>();
});
});
HttpClient = _factory.CreateClient();
Services = _factory.Services;
}
public async ValueTask DisposeAsync()
{
HttpClient.Dispose();
await _factory.DisposeAsync();
await _databaseFixture.DisposeAsync();
}
}