Skip to content

Commit 68d2a9a

Browse files
Add Keycloak tests
Add tests for scenarios for different AccessType values.
1 parent a8c2f71 commit 68d2a9a

File tree

2 files changed

+135
-0
lines changed

2 files changed

+135
-0
lines changed
Lines changed: 107 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,107 @@
1+
/*
2+
* Licensed under the Apache License, Version 2.0 (http://www.apache.org/licenses/LICENSE-2.0)
3+
* See https://github.com/aspnet-contrib/AspNet.Security.OAuth.Providers
4+
* for more information concerning the license and the contributors participating to this project.
5+
*/
6+
7+
using System;
8+
using System.Collections.Generic;
9+
using Xunit;
10+
11+
namespace AspNet.Security.OAuth.Keycloak
12+
{
13+
public static class KeycloakAuthenticationOptionsTests
14+
{
15+
public static IEnumerable<object[]> AccessTypes => new object[][]
16+
{
17+
new object[] { KeycloakAuthenticationAccessType.BearerOnly },
18+
new object[] { KeycloakAuthenticationAccessType.Confidential },
19+
new object[] { KeycloakAuthenticationAccessType.Public },
20+
};
21+
22+
[Theory]
23+
[InlineData(null)]
24+
[InlineData("")]
25+
public static void Validate_Does_Not_Throw_If_ClientSecret_Is_Not_Provided_For_Public_Access_Type(string clientSecret)
26+
{
27+
// Arrange
28+
var options = new KeycloakAuthenticationOptions()
29+
{
30+
AccessType = KeycloakAuthenticationAccessType.Public,
31+
ClientId = "my-client-id",
32+
ClientSecret = clientSecret,
33+
};
34+
35+
// Act (no Assert)
36+
options.Validate();
37+
}
38+
39+
[Theory]
40+
[InlineData(KeycloakAuthenticationAccessType.BearerOnly)]
41+
[InlineData(KeycloakAuthenticationAccessType.Confidential)]
42+
public static void Validate_Throws_If_ClientSecret_Is_Null(KeycloakAuthenticationAccessType accessType)
43+
{
44+
// Arrange
45+
var options = new KeycloakAuthenticationOptions()
46+
{
47+
AccessType = accessType,
48+
ClientId = "my-client-id",
49+
ClientSecret = null,
50+
};
51+
52+
// Act and Assert
53+
Assert.Throws<ArgumentException>("ClientSecret", () => options.Validate());
54+
}
55+
56+
[Theory]
57+
[MemberData(nameof(AccessTypes))]
58+
public static void Validate_Throws_If_AuthorizationEndpoint_Is_Null(KeycloakAuthenticationAccessType accessType)
59+
{
60+
// Arrange
61+
var options = new KeycloakAuthenticationOptions()
62+
{
63+
AccessType = accessType,
64+
AuthorizationEndpoint = null,
65+
ClientId = "my-client-id",
66+
ClientSecret = "my-client-secret",
67+
};
68+
69+
// Act and Assert
70+
Assert.Throws<ArgumentException>("AuthorizationEndpoint", () => options.Validate());
71+
}
72+
73+
[Theory]
74+
[MemberData(nameof(AccessTypes))]
75+
public static void Validate_Throws_If_TokenEndpoint_Is_Null(KeycloakAuthenticationAccessType accessType)
76+
{
77+
// Arrange
78+
var options = new KeycloakAuthenticationOptions()
79+
{
80+
AccessType = accessType,
81+
ClientId = "my-client-id",
82+
ClientSecret = "my-client-secret",
83+
TokenEndpoint = null,
84+
};
85+
86+
// Act and Assert
87+
Assert.Throws<ArgumentException>("TokenEndpoint", () => options.Validate());
88+
}
89+
90+
[Theory]
91+
[MemberData(nameof(AccessTypes))]
92+
public static void Validate_Throws_If_CallbackPath_Is_Null(KeycloakAuthenticationAccessType accessType)
93+
{
94+
// Arrange
95+
var options = new KeycloakAuthenticationOptions()
96+
{
97+
AccessType = accessType,
98+
CallbackPath = null,
99+
ClientId = "my-client-id",
100+
ClientSecret = "my-client-secret",
101+
};
102+
103+
// Act and Assert
104+
Assert.Throws<ArgumentException>("CallbackPath", () => options.Validate());
105+
}
106+
}
107+
}

test/AspNet.Security.OAuth.Providers.Tests/Keycloak/KeycloakTests.cs

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -84,5 +84,33 @@ static void ConfigureServices(IServiceCollection services)
8484
// Assert
8585
AssertClaim(claims, claimType, claimValue);
8686
}
87+
88+
[Theory]
89+
[InlineData(ClaimTypes.NameIdentifier, "995c1500-0dca-495e-ba72-2499d370d181")]
90+
[InlineData(ClaimTypes.Email, "[email protected]")]
91+
[InlineData(ClaimTypes.GivenName, "John")]
92+
[InlineData(ClaimTypes.Role, "admin")]
93+
[InlineData(ClaimTypes.Name, "John Smith")]
94+
public async Task Can_Sign_In_Using_Keycloak_Public_AccessType(string claimType, string claimValue)
95+
{
96+
// Arrange
97+
static void ConfigureServices(IServiceCollection services)
98+
{
99+
services.PostConfigureAll<KeycloakAuthenticationOptions>((options) =>
100+
{
101+
options.AccessType = KeycloakAuthenticationAccessType.Public;
102+
options.ClientSecret = string.Empty;
103+
options.Domain = "keycloak.local";
104+
});
105+
}
106+
107+
using var server = CreateTestServer(ConfigureServices);
108+
109+
// Act
110+
var claims = await AuthenticateUserAsync(server);
111+
112+
// Assert
113+
AssertClaim(claims, claimType, claimValue);
114+
}
87115
}
88116
}

0 commit comments

Comments
 (0)