-
-
Notifications
You must be signed in to change notification settings - Fork 10
Expand file tree
/
Copy pathCacheClaimTestFixture.cs
More file actions
92 lines (77 loc) · 3.48 KB
/
Copy pathCacheClaimTestFixture.cs
File metadata and controls
92 lines (77 loc) · 3.48 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
using System.Security.Claims;
using Microsoft.AspNetCore.Authentication.Cookies;
using Microsoft.AspNetCore.Builder;
using Microsoft.AspNetCore.Hosting;
using Microsoft.AspNetCore.Http;
using Microsoft.Extensions.DependencyInjection;
using NpgsqlRest.Auth;
namespace NpgsqlRestTests.Setup;
[CollectionDefinition("CacheClaimTestFixture")]
public class CacheClaimTestFixtureCollection : ICollectionFixture<CacheClaimTestFixture> { }
/// <summary>
/// Fixture for verifying that cache keys correctly include claim-resolved parameter values.
/// Provides two distinct login endpoints (`/cct-login-a` → user_a, `/cct-login-b` → user_b)
/// so a single test can switch between identities and observe whether the cache distinguishes them.
///
/// Cache backend: in-memory `RoutineCache` (default). User-parameters mapping is configured so
/// the `_user_id` parameter is auto-populated from the `name_identifier` claim when an endpoint
/// has the `user_parameters` annotation.
/// </summary>
public class CacheClaimTestFixture : IDisposable
{
private readonly WebApplication _app;
public string ServerAddress { get; }
public CacheClaimTestFixture()
{
var connectionString = Database.Create();
var builder = WebApplication.CreateBuilder();
builder.WebHost.UseUrls("http://127.0.0.1:0");
builder.Services.AddAuthentication().AddCookie();
_app = builder.Build();
// Two distinct identities. Each /login route signs in as one user and sets the auth cookie.
_app.MapGet("/cct-login-a", () => Results.SignIn(new ClaimsPrincipal(new ClaimsIdentity(
claims: new[] { new Claim("name_identifier", "user_a") },
authenticationType: CookieAuthenticationDefaults.AuthenticationScheme))));
_app.MapGet("/cct-login-b", () => Results.SignIn(new ClaimsPrincipal(new ClaimsIdentity(
claims: new[] { new Claim("name_identifier", "user_b") },
authenticationType: CookieAuthenticationDefaults.AuthenticationScheme))));
_app.UseNpgsqlRest(new(connectionString)
{
IncludeSchemas = ["public"],
NameSimilarTo = "cct_%",
CommentsMode = CommentsMode.ParseAll,
RequiresAuthorization = false,
AuthenticationOptions = new()
{
DefaultUserIdClaimType = "name_identifier",
ParameterNameClaimsMapping = new()
{
{ "_user_id", "name_identifier" }
},
},
CacheOptions = new()
{
DefaultRoutineCache = new RoutineCache(),
MemoryCachePruneIntervalSeconds = 3600
}
});
_app.StartAsync().GetAwaiter().GetResult();
ServerAddress = _app.Urls.First();
}
/// <summary>
/// Creates a fresh HttpClient bound to this fixture. Each client has its own cookie container,
/// so two clients can hold independent auth cookies (one signed in as user_a, the other as user_b).
/// </summary>
public HttpClient CreateClient()
{
var handler = new HttpClientHandler { UseCookies = true, CookieContainer = new System.Net.CookieContainer() };
return new HttpClient(handler) { BaseAddress = new Uri(ServerAddress), Timeout = TimeSpan.FromMinutes(5) };
}
#pragma warning disable CA1816
public void Dispose()
#pragma warning restore CA1816
{
_app.StopAsync().GetAwaiter().GetResult();
_app.DisposeAsync().GetAwaiter().GetResult();
}
}