-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathTestBase.cs
More file actions
86 lines (69 loc) · 2.61 KB
/
Copy pathTestBase.cs
File metadata and controls
86 lines (69 loc) · 2.61 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
using System.Security.Claims;
using System.Text.Json;
using LeanCode.AppRating.IntegrationTests.App;
using LeanCode.CQRS.RemoteHttp.Client;
using LeanCode.IntegrationTestHelpers;
using LeanCode.Logging.AspNetCore;
using LeanCode.Startup.MicrosoftDI;
using Microsoft.Extensions.Hosting;
using Xunit;
namespace LeanCode.AppRating.IntegrationTests;
public abstract class TestsBase<TApp> : IAsyncLifetime, IDisposable
where TApp : TestApp, new()
{
protected TApp App { get; private set; }
public TestsBase()
{
App = new();
}
ValueTask IAsyncLifetime.InitializeAsync() => App.InitializeAsync();
ValueTask IAsyncDisposable.DisposeAsync() => App.DisposeAsync();
void IDisposable.Dispose() => App.Dispose();
}
public class TestApp : LeanCodeTestFactory<App.Startup>
{
public readonly Guid UserId = Guid.Parse("4d3b45e6-a2c1-4d6a-9e23-94e0d9f8ca01");
protected HttpClient Client { get; set; }
public HttpCommandsExecutor Command { get; set; }
public HttpQueriesExecutor Query { get; set; }
protected JsonSerializerOptions JsonSerializerOptions { get; } = new() { };
protected override TestConnectionString ConnectionStringConfig =>
TestDatabaseConfig.Create().GetTestConnectionString();
public TestApp()
{
var claimsPrincipal = GetClaimsPrincipal(UserId);
Client = CreateApiClient();
Client.UseTestAuthorization(claimsPrincipal);
Command = new HttpCommandsExecutor(Client, JsonSerializerOptions);
Query = new HttpQueriesExecutor(Client, JsonSerializerOptions);
}
private static ClaimsPrincipal GetClaimsPrincipal(Guid userId)
{
return new ClaimsPrincipal(
new ClaimsIdentity(
new Claim[] { new(KnownClaims.UserId, userId.ToString()), new(KnownClaims.Role, Roles.User) },
TestAuthenticationHandler.SchemeName,
KnownClaims.UserId,
KnownClaims.Role
)
);
}
protected HttpClient CreateUserClient(Guid userId)
{
var client = CreateApiClient();
client.UseTestAuthorization(GetClaimsPrincipal(userId));
return client;
}
protected override IHost CreateHost(IHostBuilder builder)
{
builder.UseContentRoot(Directory.GetCurrentDirectory());
return base.CreateHost(builder);
}
protected override IHostBuilder CreateHostBuilder()
{
return LeanProgram
.BuildMinimalHost<App.Startup>()
.ConfigureDefaultLogging(projectName: "test", destructurers: [ typeof(Program).Assembly ])
.UseEnvironment(Environments.Development);
}
}