-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathWebApplicationFactoryAuthExtensions.cs
More file actions
86 lines (77 loc) · 3.26 KB
/
Copy pathWebApplicationFactoryAuthExtensions.cs
File metadata and controls
86 lines (77 loc) · 3.26 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 Microsoft.AspNetCore.Mvc.Testing;
using SimpleModule.Core.Authorization;
namespace SimpleModule.Testing;
public static class WebApplicationFactoryAuthExtensions
{
/// <summary>
/// Creates an <see cref="HttpClient"/> that carries the supplied claims as a
/// <see cref="TestAuthDefaults.ClaimsHeader"/> header, which the
/// <see cref="TestAuthHandler"/> turns into an authenticated principal.
/// A default <see cref="ClaimTypes.NameIdentifier"/> is added if missing.
/// </summary>
public static HttpClient CreateAuthenticatedClient<TEntryPoint>(
this WebApplicationFactory<TEntryPoint> factory,
params Claim[] claims
)
where TEntryPoint : class
{
ArgumentNullException.ThrowIfNull(factory);
ArgumentNullException.ThrowIfNull(claims);
var client = factory.CreateClient();
ApplyClaims(client, claims);
return client;
}
/// <summary>
/// Same as <see cref="CreateAuthenticatedClient{TEntryPoint}(WebApplicationFactory{TEntryPoint}, Claim[])"/>
/// but lets the caller pass <see cref="WebApplicationFactoryClientOptions"/>, e.g. to disable
/// auto-redirect when the test asserts on the redirect itself.
/// </summary>
public static HttpClient CreateAuthenticatedClient<TEntryPoint>(
this WebApplicationFactory<TEntryPoint> factory,
WebApplicationFactoryClientOptions clientOptions,
params Claim[] claims
)
where TEntryPoint : class
{
ArgumentNullException.ThrowIfNull(factory);
ArgumentNullException.ThrowIfNull(clientOptions);
ArgumentNullException.ThrowIfNull(claims);
var client = factory.CreateClient(clientOptions);
ApplyClaims(client, claims);
return client;
}
/// <summary>
/// Convenience overload that adds each entry of <paramref name="permissions"/>
/// as a <see cref="WellKnownClaims.Permission"/> claim before applying any
/// <paramref name="additionalClaims"/>.
/// </summary>
public static HttpClient CreateAuthenticatedClient<TEntryPoint>(
this WebApplicationFactory<TEntryPoint> factory,
string[] permissions,
params Claim[] additionalClaims
)
where TEntryPoint : class
{
ArgumentNullException.ThrowIfNull(permissions);
ArgumentNullException.ThrowIfNull(additionalClaims);
var combined = new List<Claim>(additionalClaims.Length + permissions.Length);
combined.AddRange(additionalClaims);
foreach (var permission in permissions)
{
combined.Add(new Claim(WellKnownClaims.Permission, permission));
}
return factory.CreateAuthenticatedClient(combined.ToArray());
}
private static void ApplyClaims(HttpClient client, IEnumerable<Claim> claims)
{
var list = new List<Claim>(claims);
if (!list.Exists(c => c.Type == ClaimTypes.NameIdentifier))
{
list.Add(new Claim(ClaimTypes.NameIdentifier, "test-user-id"));
}
var headerValue = string.Join(";", list.Select(c => $"{c.Type}={c.Value}"));
client.DefaultRequestHeaders.Remove(TestAuthDefaults.ClaimsHeader);
client.DefaultRequestHeaders.Add(TestAuthDefaults.ClaimsHeader, headerValue);
}
}