Skip to content

Commit d5de190

Browse files
authored
Add OAuthClient supplier for new test framework (#37487)
Closes #37486 Signed-off-by: stianst <[email protected]>
1 parent 782ad86 commit d5de190

File tree

61 files changed

+711
-389
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

61 files changed

+711
-389
lines changed

pom.xml

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1162,6 +1162,11 @@
11621162
<version>${project.version}</version>
11631163
<classifier>classes</classifier>
11641164
</dependency>
1165+
<dependency>
1166+
<groupId>org.keycloak.tests</groupId>
1167+
<artifactId>keycloak-tests-utils-shared</artifactId>
1168+
<version>${project.version}</version>
1169+
</dependency>
11651170

11661171
<dependency>
11671172
<groupId>org.keycloak</groupId>

test-framework/bom/pom.xml

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -81,6 +81,12 @@
8181
<version>${project.version}</version>
8282
<scope>test</scope>
8383
</dependency>
84+
<dependency>
85+
<groupId>org.keycloak.testframework</groupId>
86+
<artifactId>keycloak-test-framework-oauth</artifactId>
87+
<version>${project.version}</version>
88+
<scope>test</scope>
89+
</dependency>
8490
<dependency>
8591
<groupId>org.keycloak.testframework</groupId>
8692
<artifactId>keycloak-test-framework-email-server</artifactId>

test-framework/examples/tests/pom.xml

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -68,6 +68,10 @@
6868
<groupId>org.keycloak.testframework</groupId>
6969
<artifactId>keycloak-test-framework-db-postgres</artifactId>
7070
</dependency>
71+
<dependency>
72+
<groupId>org.keycloak.testframework</groupId>
73+
<artifactId>keycloak-test-framework-oauth</artifactId>
74+
</dependency>
7175
<dependency>
7276
<groupId>org.keycloak.testframework</groupId>
7377
<artifactId>keycloak-test-framework-oauth-nimbus-poc</artifactId>
Lines changed: 103 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,103 @@
1+
package org.keycloak.test.examples;
2+
3+
import com.nimbusds.oauth2.sdk.AuthorizationResponse;
4+
import com.nimbusds.oauth2.sdk.TokenIntrospectionResponse;
5+
import com.nimbusds.oauth2.sdk.TokenResponse;
6+
import com.nimbusds.oauth2.sdk.token.AccessToken;
7+
import jakarta.ws.rs.core.Response;
8+
import org.junit.jupiter.api.Assertions;
9+
import org.junit.jupiter.api.Test;
10+
import org.keycloak.testframework.oauth.nimbus.annotations.InjectOAuthClient;
11+
import org.keycloak.testframework.annotations.InjectUser;
12+
import org.keycloak.testframework.annotations.KeycloakIntegrationTest;
13+
import org.keycloak.testframework.oauth.nimbus.OAuthClient;
14+
import org.keycloak.testframework.realm.ManagedUser;
15+
import org.keycloak.testframework.realm.UserConfig;
16+
import org.keycloak.testframework.realm.UserConfigBuilder;
17+
import org.keycloak.testframework.ui.annotations.InjectPage;
18+
import org.keycloak.testframework.ui.annotations.InjectWebDriver;
19+
import org.keycloak.testframework.ui.page.LoginPage;
20+
import org.openqa.selenium.WebDriver;
21+
22+
import java.net.URI;
23+
import java.net.URL;
24+
25+
@KeycloakIntegrationTest
26+
public class NimbusOAuthClientTest {
27+
28+
@InjectUser(config = OAuthUserConfig.class)
29+
ManagedUser user;
30+
31+
@InjectOAuthClient
32+
OAuthClient oAuthClient;
33+
34+
@InjectWebDriver
35+
WebDriver webDriver;
36+
37+
@InjectPage
38+
LoginPage loginPage;
39+
40+
@Test
41+
public void testClientCredentials() throws Exception {
42+
TokenResponse tokenResponse = oAuthClient.clientCredentialGrant();
43+
Assertions.assertTrue(tokenResponse.indicatesSuccess());
44+
Assertions.assertNotNull(tokenResponse.toSuccessResponse().getTokens().getAccessToken());
45+
}
46+
47+
@Test
48+
public void testIntrospection() throws Exception {
49+
AccessToken accessToken = oAuthClient.clientCredentialGrant().toSuccessResponse().getTokens().getAccessToken();
50+
TokenIntrospectionResponse introspectionResponse = oAuthClient.introspection(accessToken);
51+
Assertions.assertTrue(introspectionResponse.indicatesSuccess());
52+
Assertions.assertNotNull(introspectionResponse.toSuccessResponse().getIssuer());
53+
}
54+
55+
@Test
56+
public void testAuthorizationCode() throws Exception {
57+
URL authorizationRequestURL = oAuthClient.authorizationRequest();
58+
webDriver.navigate().to(authorizationRequestURL);
59+
loginPage.fillLogin(user.getUsername(), user.getPassword());
60+
loginPage.submit();
61+
62+
Assertions.assertEquals(1, oAuthClient.getCallbacks().size());
63+
64+
URI callbackUri = oAuthClient.getCallbacks().remove(0);
65+
66+
AuthorizationResponse authorizationResponse = AuthorizationResponse.parse(callbackUri);
67+
Assertions.assertTrue(authorizationResponse.indicatesSuccess());
68+
Assertions.assertNotNull(authorizationResponse.toSuccessResponse().getAuthorizationCode());
69+
70+
TokenResponse tokenResponse = oAuthClient.tokenRequest(authorizationResponse.toSuccessResponse().getAuthorizationCode());
71+
Assertions.assertTrue(tokenResponse.indicatesSuccess());
72+
Assertions.assertNotNull(tokenResponse.toSuccessResponse().getTokens().getAccessToken());
73+
}
74+
75+
@Test
76+
public void testAccessTokenRevocation() throws Exception {
77+
TokenResponse tokenResponse = oAuthClient.clientCredentialGrant();
78+
Assertions.assertTrue(tokenResponse.indicatesSuccess());
79+
Assertions.assertNotNull(tokenResponse.toSuccessResponse().getTokens().getAccessToken());
80+
81+
AccessToken accessToken = tokenResponse.toSuccessResponse().getTokens().getAccessToken();
82+
TokenIntrospectionResponse introspectionResponse = oAuthClient.introspection(accessToken);
83+
Assertions.assertTrue(introspectionResponse.indicatesSuccess());
84+
Assertions.assertNotNull(introspectionResponse.toSuccessResponse().getScope());
85+
86+
Assertions.assertEquals(Response.Status.OK.getStatusCode(), oAuthClient.revokeAccessToken(accessToken).getStatusCode());
87+
88+
introspectionResponse = oAuthClient.introspection(accessToken);
89+
Assertions.assertTrue(introspectionResponse.indicatesSuccess());
90+
Assertions.assertNull(introspectionResponse.toSuccessResponse().getScope());
91+
}
92+
93+
public static class OAuthUserConfig implements UserConfig {
94+
95+
@Override
96+
public UserConfigBuilder configure(UserConfigBuilder user) {
97+
return user.name("First", "Last")
98+
.email("test@local")
99+
.password("password");
100+
}
101+
}
102+
103+
}

test-framework/examples/tests/src/test/java/org/keycloak/test/examples/OAuthClientTest.java

Lines changed: 51 additions & 58 deletions
Original file line numberDiff line numberDiff line change
@@ -1,100 +1,93 @@
11
package org.keycloak.test.examples;
22

3-
import com.nimbusds.oauth2.sdk.AuthorizationResponse;
4-
import com.nimbusds.oauth2.sdk.TokenIntrospectionResponse;
5-
import com.nimbusds.oauth2.sdk.TokenResponse;
6-
import com.nimbusds.oauth2.sdk.token.AccessToken;
7-
import jakarta.ws.rs.core.Response;
83
import org.junit.jupiter.api.Assertions;
94
import org.junit.jupiter.api.Test;
10-
import org.keycloak.testframework.oauth.nimbus.annotations.InjectOAuthClient;
5+
import org.keycloak.testframework.annotations.InjectClient;
6+
import org.keycloak.testframework.annotations.InjectRealm;
117
import org.keycloak.testframework.annotations.InjectUser;
128
import org.keycloak.testframework.annotations.KeycloakIntegrationTest;
13-
import org.keycloak.testframework.oauth.nimbus.OAuthClient;
9+
import org.keycloak.testframework.oauth.OAuthClient;
10+
import org.keycloak.testframework.oauth.annotations.InjectOAuthClient;
11+
import org.keycloak.testframework.realm.ClientConfig;
12+
import org.keycloak.testframework.realm.ClientConfigBuilder;
13+
import org.keycloak.testframework.realm.ManagedClient;
14+
import org.keycloak.testframework.realm.ManagedRealm;
1415
import org.keycloak.testframework.realm.ManagedUser;
1516
import org.keycloak.testframework.realm.UserConfig;
1617
import org.keycloak.testframework.realm.UserConfigBuilder;
17-
import org.keycloak.testframework.ui.annotations.InjectPage;
18-
import org.keycloak.testframework.ui.annotations.InjectWebDriver;
19-
import org.keycloak.testframework.ui.page.LoginPage;
20-
import org.openqa.selenium.WebDriver;
21-
22-
import java.net.URI;
23-
import java.net.URL;
18+
import org.keycloak.testsuite.util.oauth.AccessTokenResponse;
19+
import org.keycloak.testsuite.util.oauth.TokenRevocationResponse;
2420

2521
@KeycloakIntegrationTest
2622
public class OAuthClientTest {
2723

28-
@InjectUser(config = OAuthUserConfig.class)
29-
ManagedUser user;
30-
3124
@InjectOAuthClient
3225
OAuthClient oAuthClient;
3326

34-
@InjectWebDriver
35-
WebDriver webDriver;
27+
@InjectRealm
28+
ManagedRealm managedRealm;
3629

37-
@InjectPage
38-
LoginPage loginPage;
30+
@InjectClient(config = OAuthClientConfig.class)
31+
ManagedClient client;
3932

40-
@Test
41-
public void testClientCredentials() throws Exception {
42-
TokenResponse tokenResponse = oAuthClient.clientCredentialGrant();
43-
Assertions.assertTrue(tokenResponse.indicatesSuccess());
44-
Assertions.assertNotNull(tokenResponse.toSuccessResponse().getTokens().getAccessToken());
45-
}
33+
@InjectUser(config = OAuthUserConfig.class)
34+
ManagedUser user;
4635

4736
@Test
48-
public void testIntrospection() throws Exception {
49-
AccessToken accessToken = oAuthClient.clientCredentialGrant().toSuccessResponse().getTokens().getAccessToken();
50-
TokenIntrospectionResponse introspectionResponse = oAuthClient.introspection(accessToken);
51-
Assertions.assertTrue(introspectionResponse.indicatesSuccess());
52-
Assertions.assertNotNull(introspectionResponse.toSuccessResponse().getIssuer());
37+
public void testConfig() {
38+
Assertions.assertEquals(managedRealm.getName(), oAuthClient.config().getRealm());
39+
Assertions.assertEquals(managedRealm.getBaseUrl() + "/protocol/openid-connect/token", oAuthClient.getEndpoints().getToken());
5340
}
5441

5542
@Test
56-
public void testAuthorizationCode() throws Exception {
57-
URL authorizationRequestURL = oAuthClient.authorizationRequest();
58-
webDriver.navigate().to(authorizationRequestURL);
59-
loginPage.fillLogin(user.getUsername(), user.getPassword());
60-
loginPage.submit();
43+
public void testPasswordGrant() {
44+
AccessTokenResponse accessTokenResponse = oAuthClient.doPasswordGrantRequest(user.getUsername(), user.getPassword());
45+
Assertions.assertTrue(accessTokenResponse.isSuccess());
6146

62-
Assertions.assertEquals(1, oAuthClient.getCallbacks().size());
47+
accessTokenResponse = oAuthClient.passwordGrantRequest(user.getUsername(), "invalid").send();
48+
Assertions.assertFalse(accessTokenResponse.isSuccess());
49+
Assertions.assertEquals("Invalid user credentials", accessTokenResponse.getErrorDescription());
50+
}
6351

64-
URI callbackUri = oAuthClient.getCallbacks().remove(0);
52+
@Test
53+
public void testClientCredential() {
54+
AccessTokenResponse accessTokenResponse = oAuthClient.doClientCredentialsGrantAccessTokenRequest();
55+
Assertions.assertTrue(accessTokenResponse.isSuccess());
56+
}
6557

66-
AuthorizationResponse authorizationResponse = AuthorizationResponse.parse(callbackUri);
67-
Assertions.assertTrue(authorizationResponse.indicatesSuccess());
68-
Assertions.assertNotNull(authorizationResponse.toSuccessResponse().getAuthorizationCode());
58+
@Test
59+
public void testRefresh() {
60+
AccessTokenResponse accessTokenResponse = oAuthClient.doPasswordGrantRequest(user.getUsername(), user.getPassword());
6961

70-
TokenResponse tokenResponse = oAuthClient.tokenRequest(authorizationResponse.toSuccessResponse().getAuthorizationCode());
71-
Assertions.assertTrue(tokenResponse.indicatesSuccess());
72-
Assertions.assertNotNull(tokenResponse.toSuccessResponse().getTokens().getAccessToken());
62+
AccessTokenResponse refreshResponse = oAuthClient.doRefreshTokenRequest(accessTokenResponse.getRefreshToken());
63+
Assertions.assertTrue(refreshResponse.isSuccess());
64+
Assertions.assertNotEquals(accessTokenResponse.getAccessToken(), refreshResponse.getAccessToken());
7365
}
7466

7567
@Test
76-
public void testAccessTokenRevocation() throws Exception {
77-
TokenResponse tokenResponse = oAuthClient.clientCredentialGrant();
78-
Assertions.assertTrue(tokenResponse.indicatesSuccess());
79-
Assertions.assertNotNull(tokenResponse.toSuccessResponse().getTokens().getAccessToken());
68+
public void testRevocation() {
69+
AccessTokenResponse accessTokenResponse = oAuthClient.doPasswordGrantRequest(user.getUsername(), user.getPassword());
8070

81-
AccessToken accessToken = tokenResponse.toSuccessResponse().getTokens().getAccessToken();
82-
TokenIntrospectionResponse introspectionResponse = oAuthClient.introspection(accessToken);
83-
Assertions.assertTrue(introspectionResponse.indicatesSuccess());
84-
Assertions.assertNotNull(introspectionResponse.toSuccessResponse().getScope());
71+
TokenRevocationResponse tokenRevocationResponse = oAuthClient.doTokenRevoke(accessTokenResponse.getRefreshToken());
72+
Assertions.assertTrue(tokenRevocationResponse.isSuccess());
8573

86-
Assertions.assertEquals(Response.Status.OK.getStatusCode(), oAuthClient.revokeAccessToken(accessToken).getStatusCode());
74+
AccessTokenResponse refreshResponse = oAuthClient.doRefreshTokenRequest(accessTokenResponse.getRefreshToken());
75+
Assertions.assertFalse(refreshResponse.isSuccess());
76+
}
77+
78+
public static class OAuthClientConfig implements ClientConfig {
8779

88-
introspectionResponse = oAuthClient.introspection(accessToken);
89-
Assertions.assertTrue(introspectionResponse.indicatesSuccess());
90-
Assertions.assertNull(introspectionResponse.toSuccessResponse().getScope());
80+
@Override
81+
public ClientConfigBuilder configure(ClientConfigBuilder client) {
82+
return client.clientId("myclient").secret("mysecret").directAccessGrants().serviceAccount();
83+
}
9184
}
9285

9386
public static class OAuthUserConfig implements UserConfig {
9487

9588
@Override
9689
public UserConfigBuilder configure(UserConfigBuilder user) {
97-
return user.name("First", "Last")
90+
return user.username("myuser").name("First", "Last")
9891
.email("test@local")
9992
.password("password");
10093
}

test-framework/oauth/pom.xml

Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
<?xml version="1.0"?>
2+
<!--
3+
~ Copyright 2016 Red Hat, Inc. and/or its affiliates
4+
~ and other contributors as indicated by the @author tags.
5+
~
6+
~ Licensed under the Apache License, Version 2.0 (the "License");
7+
~ you may not use this file except in compliance with the License.
8+
~ You may obtain a copy of the License at
9+
~
10+
~ http://www.apache.org/licenses/LICENSE-2.0
11+
~
12+
~ Unless required by applicable law or agreed to in writing, software
13+
~ distributed under the License is distributed on an "AS IS" BASIS,
14+
~ WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15+
~ See the License for the specific language governing permissions and
16+
~ limitations under the License.
17+
-->
18+
19+
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
20+
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">
21+
<parent>
22+
<artifactId>keycloak-test-framework-parent</artifactId>
23+
<groupId>org.keycloak.testframework</groupId>
24+
<version>999.0.0-SNAPSHOT</version>
25+
<relativePath>../pom.xml</relativePath>
26+
</parent>
27+
<modelVersion>4.0.0</modelVersion>
28+
29+
<artifactId>keycloak-test-framework-oauth</artifactId>
30+
<name>Keycloak Test Framework</name>
31+
<packaging>jar</packaging>
32+
<description>OAuth extension for Keycloak Test Framework</description>
33+
34+
<dependencies>
35+
<dependency>
36+
<groupId>org.keycloak.testframework</groupId>
37+
<artifactId>keycloak-test-framework-core</artifactId>
38+
<version>${project.version}</version>
39+
</dependency>
40+
<dependency>
41+
<groupId>org.keycloak.testframework</groupId>
42+
<artifactId>keycloak-test-framework-ui</artifactId>
43+
<version>${project.version}</version>
44+
</dependency>
45+
<dependency>
46+
<groupId>org.keycloak.tests</groupId>
47+
<artifactId>keycloak-tests-utils-shared</artifactId>
48+
</dependency>
49+
</dependencies>
50+
</project>
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
package org.keycloak.testframework.oauth;
2+
3+
import org.apache.http.impl.client.CloseableHttpClient;
4+
import org.keycloak.OAuth2Constants;
5+
import org.keycloak.testsuite.util.oauth.AbstractOAuthClient;
6+
import org.keycloak.testsuite.util.oauth.OAuthClientConfig;
7+
import org.openqa.selenium.WebDriver;
8+
9+
public class OAuthClient extends AbstractOAuthClient<OAuthClient> {
10+
11+
public OAuthClient(String baseUrl, CloseableHttpClient httpClient, WebDriver webDriver) {
12+
super(baseUrl, httpClient, webDriver);
13+
14+
config = new OAuthClientConfig()
15+
.responseType(OAuth2Constants.CODE);
16+
}
17+
18+
public void close() {
19+
}
20+
21+
}

0 commit comments

Comments
 (0)