-
Notifications
You must be signed in to change notification settings - Fork 1.8k
/
OrderingApiFixture.cs
74 lines (65 loc) · 2.46 KB
/
OrderingApiFixture.cs
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
using Microsoft.AspNetCore.Mvc.Testing;
using Microsoft.Extensions.Hosting;
namespace eShop.Ordering.FunctionalTests;
public sealed class OrderingApiFixture : WebApplicationFactory<Program>, IAsyncLifetime
{
private readonly IHost _app;
public IResourceBuilder<PostgresServerResource> Postgres { get; private set; }
public IResourceBuilder<PostgresServerResource> IdentityDB { get; private set; }
public IResourceBuilder<ProjectResource> IdentityApi { get; private set; }
private string _postgresConnectionString;
public OrderingApiFixture()
{
var options = new DistributedApplicationOptions { AssemblyName = typeof(OrderingApiFixture).Assembly.FullName, DisableDashboard = true };
var appBuilder = DistributedApplication.CreateBuilder(options);
Postgres = appBuilder.AddPostgres("OrderingDB");
IdentityDB = appBuilder.AddPostgres("IdentityDB");
IdentityApi = appBuilder.AddProject<Projects.Identity_API>("identity-api").WithReference(IdentityDB);
_app = appBuilder.Build();
}
protected override IHost CreateHost(IHostBuilder builder)
{
builder.ConfigureHostConfiguration(config =>
{
config.AddInMemoryCollection(new Dictionary<string, string>
{
{ $"ConnectionStrings:{Postgres.Resource.Name}", _postgresConnectionString },
{ "Identity:Url", IdentityApi.GetEndpoint("http").Url }
});
});
builder.ConfigureServices(services =>
{
services.AddSingleton<IStartupFilter>(new AutoAuthorizeStartupFilter());
});
return base.CreateHost(builder);
}
public new async Task DisposeAsync()
{
await base.DisposeAsync();
await _app.StopAsync();
if (_app is IAsyncDisposable asyncDisposable)
{
await asyncDisposable.DisposeAsync().ConfigureAwait(false);
}
else
{
_app.Dispose();
}
}
public async Task InitializeAsync()
{
await _app.StartAsync();
_postgresConnectionString = await Postgres.Resource.GetConnectionStringAsync();
}
private class AutoAuthorizeStartupFilter : IStartupFilter
{
public Action<IApplicationBuilder> Configure(Action<IApplicationBuilder> next)
{
return builder =>
{
builder.UseMiddleware<AutoAuthorizeMiddleware>();
next(builder);
};
}
}
}