Close admin client after test

This commit is contained in:
Stian Thorgersen 2016-05-06 14:04:09 +02:00
parent f4d751c839
commit 713909fac9
7 changed files with 13 additions and 110 deletions

View file

@ -21,7 +21,6 @@ import org.jboss.arquillian.core.api.Instance;
import org.jboss.arquillian.core.api.annotation.Inject; import org.jboss.arquillian.core.api.annotation.Inject;
import org.jboss.arquillian.test.api.ArquillianResource; import org.jboss.arquillian.test.api.ArquillianResource;
import org.jboss.arquillian.test.spi.enricher.resource.ResourceProvider; import org.jboss.arquillian.test.spi.enricher.resource.ResourceProvider;
import org.keycloak.testsuite.util.DeleteMeOAuthClient;
import org.keycloak.testsuite.util.OAuthClient; import org.keycloak.testsuite.util.OAuthClient;
import java.lang.annotation.Annotation; import java.lang.annotation.Annotation;

View file

@ -1,94 +0,0 @@
/*
* Copyright 2016 Red Hat, Inc. and/or its affiliates
* and other contributors as indicated by the @author tags.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.keycloak.testsuite.util;
import org.apache.commons.io.IOUtils;
import org.apache.http.NameValuePair;
import org.apache.http.client.entity.UrlEncodedFormEntity;
import org.apache.http.client.methods.CloseableHttpResponse;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClients;
import org.apache.http.message.BasicNameValuePair;
import org.keycloak.OAuth2Constants;
import org.keycloak.protocol.oidc.OIDCLoginProtocolService;
import org.keycloak.representations.AccessTokenResponse;
import org.keycloak.util.BasicAuthHelper;
import org.keycloak.util.JsonSerialization;
import javax.ws.rs.core.UriBuilder;
import java.io.IOException;
import java.io.UnsupportedEncodingException;
import java.util.LinkedList;
import java.util.List;
/**
* @author <a href="mailto:sthorger@redhat.com">Stian Thorgersen</a>
*/
public class DeleteMeOAuthClient {
private String baseUrl;
public DeleteMeOAuthClient(String baseUrl) {
this.baseUrl = baseUrl;
}
public AccessTokenResponse getToken(String realm, String clientId, String clientSecret, String username, String password) {
CloseableHttpClient httpclient = HttpClients.createDefault();
try {
HttpPost post = new HttpPost(OIDCLoginProtocolService.tokenUrl(UriBuilder.fromUri(baseUrl)).build(realm));
List<NameValuePair> parameters = new LinkedList<NameValuePair>();
parameters.add(new BasicNameValuePair(OAuth2Constants.GRANT_TYPE, OAuth2Constants.PASSWORD));
parameters.add(new BasicNameValuePair("username", username));
parameters.add(new BasicNameValuePair("password", password));
if (clientSecret != null) {
String authorization = BasicAuthHelper.createHeader(clientId, clientSecret);
post.setHeader("Authorization", authorization);
} else {
parameters.add(new BasicNameValuePair("client_id", clientId));
}
UrlEncodedFormEntity formEntity;
try {
formEntity = new UrlEncodedFormEntity(parameters, "UTF-8");
} catch (UnsupportedEncodingException e) {
throw new RuntimeException(e);
}
post.setEntity(formEntity);
CloseableHttpResponse response = httpclient.execute(post);
if (response.getStatusLine().getStatusCode() != 200) {
throw new RuntimeException("Failed to retrieve token: " + response.getStatusLine().toString() + " / " + IOUtils.toString(response.getEntity().getContent()));
}
return JsonSerialization.readValue(response.getEntity().getContent(), AccessTokenResponse.class);
} catch (Exception e) {
throw new RuntimeException(e);
}
finally {
try {
httpclient.close();
} catch (IOException e) {
throw new RuntimeException(e);
}
}
}
}

View file

@ -54,7 +54,6 @@ import org.keycloak.testsuite.arquillian.AuthServerTestEnricher;
import org.keycloak.testsuite.arquillian.SuiteContext; import org.keycloak.testsuite.arquillian.SuiteContext;
import org.keycloak.testsuite.auth.page.WelcomePage; import org.keycloak.testsuite.auth.page.WelcomePage;
import org.keycloak.testsuite.client.KeycloakTestingClient; import org.keycloak.testsuite.client.KeycloakTestingClient;
import org.keycloak.testsuite.util.DeleteMeOAuthClient;
import org.keycloak.testsuite.util.OAuthClient; import org.keycloak.testsuite.util.OAuthClient;
import org.openqa.selenium.WebDriver; import org.openqa.selenium.WebDriver;
import org.keycloak.testsuite.auth.page.AuthServer; import org.keycloak.testsuite.auth.page.AuthServer;
@ -96,8 +95,6 @@ public abstract class AbstractKeycloakTest {
@ArquillianResource @ArquillianResource
protected OAuthClient oauth; protected OAuthClient oauth;
protected DeleteMeOAuthClient deleteMeOAuthClient;
protected List<RealmRepresentation> testRealmReps; protected List<RealmRepresentation> testRealmReps;
@Drone @Drone
@ -131,7 +128,6 @@ public abstract class AbstractKeycloakTest {
public void beforeAbstractKeycloakTest() throws Exception { public void beforeAbstractKeycloakTest() throws Exception {
adminClient = Keycloak.getInstance(AuthServerTestEnricher.getAuthServerContextRoot() + "/auth", adminClient = Keycloak.getInstance(AuthServerTestEnricher.getAuthServerContextRoot() + "/auth",
MASTER, ADMIN, ADMIN, Constants.ADMIN_CLI_CLIENT_ID); MASTER, ADMIN, ADMIN, Constants.ADMIN_CLI_CLIENT_ID);
deleteMeOAuthClient = new DeleteMeOAuthClient(AuthServerTestEnricher.getAuthServerContextRoot() + "/auth");
getTestingClient(); getTestingClient();
@ -161,7 +157,7 @@ public abstract class AbstractKeycloakTest {
} }
removeTestRealms(); // Remove realms after tests. Tests should cleanup after themselves! removeTestRealms(); // Remove realms after tests. Tests should cleanup after themselves!
// adminClient.close(); // keeping admin connection open adminClient.close(); // don't keep admin connection open
} }
private void updateMasterAdminPassword() { private void updateMasterAdminPassword() {

View file

@ -137,11 +137,11 @@ public class PermissionsTest extends AbstractKeycloakTest {
@Override @Override
public void afterAbstractKeycloakTest() { public void afterAbstractKeycloakTest() {
super.afterAbstractKeycloakTest();
for (UserRepresentation u : adminClient.realm("master").users().search("permissions-test-master-", 0, 100)) { for (UserRepresentation u : adminClient.realm("master").users().search("permissions-test-master-", 0, 100)) {
adminClient.realm("master").users().get(u.getId()).remove(); adminClient.realm("master").users().get(u.getId()).remove();
} }
super.afterAbstractKeycloakTest();
} }
@Test @Test

View file

@ -26,12 +26,12 @@ import org.keycloak.events.Details;
import org.keycloak.jose.jws.JWSInput; import org.keycloak.jose.jws.JWSInput;
import org.keycloak.jose.jws.crypto.RSAProvider; import org.keycloak.jose.jws.crypto.RSAProvider;
import org.keycloak.representations.AccessToken; import org.keycloak.representations.AccessToken;
import org.keycloak.representations.AccessTokenResponse;
import org.keycloak.representations.RefreshToken; import org.keycloak.representations.RefreshToken;
import org.keycloak.representations.idm.RealmRepresentation; import org.keycloak.representations.idm.RealmRepresentation;
import org.keycloak.testsuite.AbstractKeycloakTest; import org.keycloak.testsuite.AbstractKeycloakTest;
import org.keycloak.testsuite.AssertEvents; import org.keycloak.testsuite.AssertEvents;
import org.keycloak.testsuite.arquillian.AuthServerTestEnricher; import org.keycloak.testsuite.arquillian.AuthServerTestEnricher;
import org.keycloak.testsuite.util.OAuthClient.AccessTokenResponse;
import java.security.PublicKey; import java.security.PublicKey;
import java.util.List; import java.util.List;
@ -47,10 +47,9 @@ public abstract class AbstractGroupTest extends AbstractKeycloakTest {
public AssertEvents events = new AssertEvents(this); public AssertEvents events = new AssertEvents(this);
AccessToken login(String login, String clientId, String clientSecret, String userId) throws Exception { AccessToken login(String login, String clientId, String clientSecret, String userId) throws Exception {
AccessTokenResponse tokenResponse = oauth.doGrantAccessTokenRequest("test", login, "password", null, clientId, clientSecret);
AccessTokenResponse tokenResponse = deleteMeOAuthClient.getToken("test", clientId, clientSecret, login, "password"); String accessToken = tokenResponse.getAccessToken();
String accessToken = tokenResponse.getToken();
String refreshToken = tokenResponse.getRefreshToken(); String refreshToken = tokenResponse.getRefreshToken();
PublicKey publicKey = PemUtils.decodePublicKey(adminClient.realm("test").toRepresentation().getPublicKey()); PublicKey publicKey = PemUtils.decodePublicKey(adminClient.realm("test").toRepresentation().getPublicKey());
@ -66,7 +65,6 @@ public abstract class AbstractGroupTest extends AbstractKeycloakTest {
events.expectLogin() events.expectLogin()
.client(clientId) .client(clientId)
.user(userId) .user(userId)
.session(tokenResponse.getSessionState())
.detail(Details.GRANT_TYPE, OAuth2Constants.PASSWORD) .detail(Details.GRANT_TYPE, OAuth2Constants.PASSWORD)
.detail(Details.TOKEN_ID, accessTokenRepresentation.getId()) .detail(Details.TOKEN_ID, accessTokenRepresentation.getId())
.detail(Details.REFRESH_TOKEN_ID, refreshTokenRepresentation.getId()) .detail(Details.REFRESH_TOKEN_ID, refreshTokenRepresentation.getId())

View file

@ -120,7 +120,11 @@ public abstract class AbstractClientRegistrationTest extends AbstractKeycloakTes
} }
private String getToken(String username, String password) { private String getToken(String username, String password) {
return deleteMeOAuthClient.getToken(REALM_NAME, Constants.ADMIN_CLI_CLIENT_ID, null, username, password).getToken(); try {
return oauth.doGrantAccessTokenRequest(REALM_NAME, username, password, null, Constants.ADMIN_CLI_CLIENT_ID, null).getAccessToken();
} catch (Exception e) {
throw new RuntimeException(e);
}
} }
} }

View file

@ -59,10 +59,10 @@ public class ClientRegistrationTest extends AbstractClientRegistrationTest {
} }
@Test @Test
public void registerClientInMasterRealm() throws ClientRegistrationException { public void registerClientInMasterRealm() throws Exception {
ClientRegistration masterReg = ClientRegistration.create().url(suiteContext.getAuthServerInfo().getContextRoot() + "/auth", "master").build(); ClientRegistration masterReg = ClientRegistration.create().url(suiteContext.getAuthServerInfo().getContextRoot() + "/auth", "master").build();
String token = deleteMeOAuthClient.getToken("master", Constants.ADMIN_CLI_CLIENT_ID, null, "admin", "admin").getToken(); String token = oauth.doGrantAccessTokenRequest("master", "admin", "admin", null, Constants.ADMIN_CLI_CLIENT_ID, null).getAccessToken();
masterReg.auth(Auth.token(token)); masterReg.auth(Auth.token(token));
ClientRepresentation client = new ClientRepresentation(); ClientRepresentation client = new ClientRepresentation();