Skip to content

Commit fa9e967

Browse files
committed
Endpoint Tests
1 parent a6f2f25 commit fa9e967

10 files changed

Lines changed: 3492 additions & 36 deletions

File tree

‎src/CodeBeam.UltimateAuth.Server/Endpoints/LogoutEndpointHandler.cs‎

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -19,17 +19,15 @@ public sealed class LogoutEndpointHandler : ILogoutEndpointHandler
1919
private readonly IUAuthFlowService _flow;
2020
private readonly IAccessContextFactory _accessContextFactory;
2121
private readonly ISessionApplicationService _sessionApplicationService;
22-
private readonly IClock _clock;
2322
private readonly IUAuthCookieManager _cookieManager;
2423
private readonly IAuthRedirectResolver _redirectResolver;
2524

26-
public LogoutEndpointHandler(IAuthFlowContextAccessor authContext, IUAuthFlowService flow, IAccessContextFactory accessContextFactory, ISessionApplicationService sessionApplicationService, IClock clock, IUAuthCookieManager cookieManager, IAuthRedirectResolver redirectResolver)
25+
public LogoutEndpointHandler(IAuthFlowContextAccessor authContext, IUAuthFlowService flow, IAccessContextFactory accessContextFactory, ISessionApplicationService sessionApplicationService, IUAuthCookieManager cookieManager, IAuthRedirectResolver redirectResolver)
2726
{
2827
_authContext = authContext;
2928
_flow = flow;
3029
_accessContextFactory = accessContextFactory;
3130
_sessionApplicationService = sessionApplicationService;
32-
_clock = clock;
3331
_cookieManager = cookieManager;
3432
_redirectResolver = redirectResolver;
3533
}
@@ -151,7 +149,7 @@ public async Task<IResult> LogoutAllSelfAsync(HttpContext ctx)
151149
flow,
152150
UAuthActions.Flows.LogoutAllSelf,
153151
resource: "flows",
154-
resourceId: userKey);
152+
resourceId: userKey.Value);
155153

156154
await _sessionApplicationService.LogoutAllDevicesAsync(access, userKey, ctx.RequestAborted);
157155
return Results.Ok();

‎src/CodeBeam.UltimateAuth.Server/Endpoints/PkceEndpointHandler.cs‎

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -163,6 +163,11 @@ public async Task<IResult> CompleteAsync(HttpContext ctx)
163163
if (request is null)
164164
return Results.BadRequest("Invalid PKCE payload.");
165165

166+
if (string.IsNullOrWhiteSpace(request.AuthorizationCode) || string.IsNullOrWhiteSpace(request.CodeVerifier))
167+
{
168+
return Results.BadRequest("authorization_code and code_verifier are required.");
169+
}
170+
166171
var result = await _pkceService.CompleteAsync(
167172
auth,
168173
new PkceCompleteRequest

‎src/CodeBeam.UltimateAuth.Server/Infrastructure/Redirect/RedirectDecision.cs‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ public sealed class RedirectDecision
55
public bool Enabled { get; }
66
public string? TargetUrl { get; }
77

8-
private RedirectDecision(bool enabled, string? targetUrl)
8+
internal RedirectDecision(bool enabled, string? targetUrl)
99
{
1010
Enabled = enabled;
1111
TargetUrl = targetUrl;

‎tests/CodeBeam.UltimateAuth.Tests.Unit/Helpers/AuthFlowTestFactory.cs‎

Lines changed: 15 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -11,47 +11,34 @@ namespace CodeBeam.UltimateAuth.Tests.Unit.Helpers;
1111

1212
internal static class AuthFlowTestFactory
1313
{
14-
public static AuthFlowContext LoginSuccess(ReturnUrlInfo? returnUrlInfo = null, EffectiveRedirectResponse? redirect = null)
15-
{
16-
return new AuthFlowContext(
17-
flowType: AuthFlowType.Login,
18-
clientProfile: UAuthClientProfile.BlazorServer,
19-
effectiveMode: UAuthMode.PureOpaque,
20-
device: TestDevice.Default(),
21-
tenantKey: TenantKey.Single,
22-
isAuthenticated: true,
23-
userKey: UserKey.New(),
24-
session: null,
25-
originalOptions: TestServerOptions.Default(),
26-
effectiveOptions: TestServerOptions.Effective(),
27-
response: new EffectiveAuthResponse(
28-
sessionIdDelivery: CredentialResponseOptions.Disabled(GrantKind.Session),
29-
accessTokenDelivery: CredentialResponseOptions.Disabled(GrantKind.AccessToken),
30-
refreshTokenDelivery: CredentialResponseOptions.Disabled(GrantKind.RefreshToken),
31-
redirect: redirect ?? EffectiveRedirectResponse.Disabled
32-
),
33-
primaryTokenKind: PrimaryTokenKind.Session,
34-
returnUrlInfo: returnUrlInfo ?? ReturnUrlInfo.None()
35-
);
36-
}
14+
public static AuthFlowContext LoginSuccess(
15+
ReturnUrlInfo? returnUrlInfo = null,
16+
EffectiveRedirectResponse? redirect = null)
17+
=> New(
18+
returnUrlInfo: returnUrlInfo,
19+
redirect: redirect);
3720

3821
public static AuthFlowContext New(
3922
ReturnUrlInfo? returnUrlInfo = null,
4023
EffectiveRedirectResponse? redirect = null,
41-
TenantKey? tenant = null)
24+
TenantKey? tenant = null,
25+
UserKey? userKey = null,
26+
SessionSecurityContext? session = null,
27+
bool isAuthenticated = true,
28+
EffectiveAuthResponse? response = null)
4229
{
4330
return new AuthFlowContext(
4431
flowType: AuthFlowType.Login,
4532
clientProfile: UAuthClientProfile.BlazorServer,
4633
effectiveMode: UAuthMode.PureOpaque,
4734
device: TestDevice.Default(),
4835
tenantKey: tenant ?? TenantKey.Single,
49-
isAuthenticated: true,
50-
userKey: UserKey.New(),
51-
session: null,
36+
isAuthenticated: isAuthenticated,
37+
userKey: userKey ?? (isAuthenticated ? UserKey.New() : null),
38+
session: session,
5239
originalOptions: TestServerOptions.Default(),
5340
effectiveOptions: TestServerOptions.Effective(),
54-
response: new EffectiveAuthResponse(
41+
response: response ?? new EffectiveAuthResponse(
5542
sessionIdDelivery:
5643
CredentialResponseOptions.Disabled(GrantKind.Session),
5744
accessTokenDelivery:

‎tests/CodeBeam.UltimateAuth.Tests.Unit/Helpers/TestAccessContext.cs‎

Lines changed: 32 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,12 @@ public static AccessContext WithAction(string action)
2222
);
2323
}
2424

25-
public static AccessContext ForUser(UserKey userKey, string action, TenantKey? tenant = null)
25+
public static AccessContext ForUser(
26+
UserKey userKey,
27+
string action,
28+
TenantKey? tenant = null,
29+
SessionChainId? actorChainId = null,
30+
string resource = "identifier")
2631
{
2732
var t = tenant ?? TenantKey.Single;
2833

@@ -31,12 +36,36 @@ public static AccessContext ForUser(UserKey userKey, string action, TenantKey? t
3136
actorTenant: t,
3237
isAuthenticated: true,
3338
isSystemActor: false,
34-
actorChainId: null,
35-
resource: "identifier",
39+
actorChainId: actorChainId,
40+
resource: resource,
3641
targetUserKey: userKey,
3742
resourceTenant: t,
3843
action: action,
3944
attributes: EmptyAttributes.Instance
4045
);
4146
}
47+
48+
public static AccessContext ForTargetUser(
49+
UserKey actorUserKey,
50+
UserKey targetUserKey,
51+
string action,
52+
TenantKey? tenant = null,
53+
SessionChainId? actorChainId = null,
54+
string resource = "identifier")
55+
{
56+
var t = tenant ?? TenantKey.Single;
57+
58+
return new AccessContext(
59+
actorUserKey: actorUserKey,
60+
actorTenant: t,
61+
isAuthenticated: true,
62+
isSystemActor: false,
63+
actorChainId: actorChainId,
64+
resource: resource,
65+
targetUserKey: targetUserKey,
66+
resourceTenant: t,
67+
action: action,
68+
attributes: EmptyAttributes.Instance
69+
);
70+
}
4271
}

0 commit comments

Comments
 (0)