KEYCLOAK-2316: Sync admin client with endpoints for Client. First commit.

This commit is contained in:
Stan Silvert 2016-02-23 09:05:54 -05:00
parent f34260478e
commit 3383b044b2
5 changed files with 385 additions and 8 deletions

View file

@ -17,6 +17,7 @@
package org.keycloak.admin.client.resource;
import org.jboss.resteasy.annotations.cache.NoCache;
import org.keycloak.representations.idm.ClientRepresentation;
import org.keycloak.representations.idm.CredentialRepresentation;
import org.keycloak.representations.idm.UserSessionRepresentation;
@ -80,14 +81,9 @@ public interface ClientResource {
public CredentialRepresentation getSecret();
@GET
@Path("installation/jboss")
@Produces(MediaType.APPLICATION_XML)
public String getInstallationJbossXml();
@GET
@Path("installation/json")
@Produces(MediaType.APPLICATION_JSON)
public String getInstallationJson();
@NoCache
@Path("installation/providers/{providerId}")
public String getInstallationProvider(@PathParam("providerId") String providerId);
@POST
@Path("logout-all")

View file

@ -0,0 +1,72 @@
/*
* Copyright 2016 Red Hat Inc. and/or its affiliates and other contributors
* as indicated by the @author tags. All rights reserved.
*
* 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.admin.client;
import javax.ws.rs.core.Response;
import org.keycloak.admin.client.resource.ClientResource;
import org.keycloak.representations.idm.ClientRepresentation;
import org.keycloak.representations.idm.RealmRepresentation;
import org.keycloak.testsuite.AbstractAuthTest;
import org.keycloak.testsuite.admin.ApiUtil;
/**
*
* @author Stan Silvert ssilvert@redhat.com (C) 2016 Red Hat Inc.
*/
public abstract class AbstractClientTest extends AbstractAuthTest {
protected RealmRepresentation realmRep() {
return testRealmResource().toRepresentation();
}
protected void createOidcClient(String name) {
ClientRepresentation clientRep = new ClientRepresentation();
clientRep.setClientId(name);
clientRep.setName(name);
clientRep.setRootUrl("foo");
clientRep.setProtocol("openid-connect");
createClient(clientRep);
}
protected void createSamlClient(String name) {
ClientRepresentation clientRep = new ClientRepresentation();
clientRep.setClientId(name);
clientRep.setName(name);
clientRep.setProtocol("saml");
clientRep.setAdminUrl("samlEndpoint");
createClient(clientRep);
}
protected void createClient(ClientRepresentation clientRep) {
Response resp = testRealmResource().clients().create(clientRep);
// for some reason, findAll() will later fail unless readEntity is called here
resp.readEntity(String.class);
//testRealmResource().clients().findAll();
}
protected ClientRepresentation findClientRepresentation(String name) {
ClientResource clientRsc = findClientResource(name);
if (clientRsc == null) return null;
return findClientResource(name).toRepresentation();
}
protected ClientResource findClientResource(String name) {
return ApiUtil.findClientResourceByName(testRealmResource(), name);
}
}

View file

@ -0,0 +1,76 @@
/*
* Copyright 2016 Red Hat Inc. and/or its affiliates and other contributors
* as indicated by the @author tags. All rights reserved.
*
* 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.admin.client;
import org.junit.Before;
import org.junit.Test;
import org.keycloak.admin.client.resource.ClientResource;
import org.keycloak.admin.client.resource.RolesResource;
import org.keycloak.representations.idm.RoleRepresentation;
import static org.junit.Assert.assertFalse;
import static org.junit.Assert.assertTrue;
/**
*
* @author Stan Silvert ssilvert@redhat.com (C) 2016 Red Hat Inc.
*/
public class ClientRolesTest extends AbstractClientTest {
private ClientResource clientRsc;
private RolesResource rolesRsc;
@Before
public void init() {
createOidcClient("roleClient");
clientRsc = findClientResource("roleClient");
rolesRsc = clientRsc.roles();
}
private RoleRepresentation makeRole(String name) {
RoleRepresentation role = new RoleRepresentation();
role.setName(name);
return role;
}
/* private boolean hasRole(RolesResource rolesRsc, String name) {
return rolesRsc.get(name) != null;
}*/
private boolean hasRole(RolesResource rolesRsc, String name) {
for (RoleRepresentation role : rolesRsc.list()) {
if (role.getName().equals(name)) return true;
}
return false;
}
@Test
public void testAddRole() {
rolesRsc.create(makeRole("role1"));
assertTrue(hasRole(rolesRsc, "role1"));
}
@Test
public void testRemoveRole() {
rolesRsc.create(makeRole("role2"));
rolesRsc.deleteRole("role2");
assertFalse(hasRole(rolesRsc, "role2"));
}
}

View file

@ -0,0 +1,118 @@
/*
* Copyright 2016 Red Hat Inc. and/or its affiliates and other contributors
* as indicated by the @author tags. All rights reserved.
*
* 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.admin.client;
import java.util.List;
import org.junit.Test;
import org.keycloak.admin.client.resource.ClientResource;
import org.keycloak.representations.idm.ClientRepresentation;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertFalse;
import static org.junit.Assert.assertNotNull;
import static org.junit.Assert.assertNull;
import static org.junit.Assert.assertTrue;
/**
*
* @author Stan Silvert ssilvert@redhat.com (C) 2016 Red Hat Inc.
*/
public class ClientTest extends AbstractClientTest {
public static void assertEqualClients(ClientRepresentation expected, ClientRepresentation actual) {
assertEquals(expected.getClientId(), actual.getClientId());
assertEquals(expected.getName(), actual.getName());
assertEquals(expected.getDescription(), actual.getDescription());
assertEquals(expected.getBaseUrl(), actual.getBaseUrl());
assertTrue(expected.getRedirectUris().containsAll(actual.getRedirectUris()));
assertTrue(expected.getWebOrigins().containsAll(actual.getWebOrigins()));
assertEquals(expected.getRegisteredNodes(), actual.getRegisteredNodes());
}
@Test
public void testCreateClient() {
createOidcClient("foo");
assertEquals("foo", findClientRepresentation("foo").getName());
}
@Test
public void testDeleteClient() {
createOidcClient("deleteMe");
ClientResource clientRsc = findClientResource("deleteMe");
assertNotNull(clientRsc);
clientRsc.remove();
assertNull(findClientResource("deleteMe"));
}
@Test
public void testUpdateClient() {
createOidcClient("updateMe");
ClientRepresentation clientRep = findClientRepresentation("updateMe");
assertEquals("updateMe", clientRep.getName());
clientRep.setName("iWasUpdated");
findClientResource("updateMe").update(clientRep);
ClientRepresentation updatedClient = findClientRepresentation("iWasUpdated");
assertNotNull(updatedClient);
assertEquals("updateMe", updatedClient.getClientId());
assertEquals("iWasUpdated", updatedClient.getName());
}
@Test
public void testGetAllClients() {
List<ClientRepresentation> allClients = testRealmResource().clients().findAll();
assertNotNull(allClients);
assertFalse(allClients.isEmpty());
}
@Test
public void getClientByIdTest() {
createOidcClient("byidclient");
ClientRepresentation rep = findClientRepresentation("byidclient");
ClientRepresentation gotById = testRealmResource().clients().get(rep.getId()).toRepresentation();
assertEqualClients(rep, gotById);
}
/* DEPRECATED?
@Test
public void testAllowedOrigins() {
createOidcClient("originsClient");
ClientResource client = findClientResource("originsClient");
java.util.Set<String> origins = client.getAllowedOrigins();
assertEquals(1, origins.size());
assertTrue(origins.contains("foo/*"));
origins.add("bar/*");
client.updateAllowedOrigins(origins); //<-- STACK OVERFLOW
origins = client.getAllowedOrigins();
assertEquals(2, origins.size());
assertTrue(origins.contains("foo/*"));
assertTrue(origins.contains("bar/*"));
java.util.Set<String> toRemove = new java.util.HashSet<>();
toRemove.add("bar/*");
client.removeAllowedOrigins(origins);
origins = client.getAllowedOrigins();
assertEquals(1, origins.size());
assertTrue(origins.contains("foo/*"));
} */
}

View file

@ -0,0 +1,115 @@
/*
* Copyright 2016 Red Hat Inc. and/or its affiliates and other contributors
* as indicated by the @author tags. All rights reserved.
*
* 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.admin.client;
import org.junit.Before;
import org.junit.Test;
import org.keycloak.admin.client.resource.ClientResource;
import org.keycloak.representations.idm.RealmRepresentation;
import org.keycloak.testsuite.arquillian.AuthServerTestEnricher;
import static org.junit.Assert.assertTrue;
/**
* Test getting the installation/configuration files for OIDC and SAML.
*
* @author Stan Silvert ssilvert@redhat.com (C) 2016 Red Hat Inc.
*/
public class InstallationTest extends AbstractClientTest {
private static final String OIDC_NAME = "oidcInstallationClient";
private static final String SAML_NAME = "samlInstallationClient";
private ClientResource oidcClient;
private ClientResource samlClient;
@Before
public void createClients() {
createOidcClient(OIDC_NAME);
oidcClient = findClientResource(OIDC_NAME);
createSamlClient(SAML_NAME);
samlClient = findClientResource(SAML_NAME);
}
private String authServerUrl() {
return AuthServerTestEnricher.getAuthServerContextRoot() + "/auth";
}
private String samlUrl(RealmRepresentation realmRep) {
return authServerUrl() + "/realms/" + realmRep.getId() + "/protocol/saml";
}
@Test
public void testOidcJBossXml() {
String xml = oidcClient.getInstallationProvider("keycloak-oidc-jboss-subsystem");
assertOidcInstallationConfig(xml);
assertTrue(xml.contains("<secure-deployment"));
}
@Test
public void testOidcJson() {
String json = oidcClient.getInstallationProvider("keycloak-oidc-keycloak-json");
assertOidcInstallationConfig(json);
}
private void assertOidcInstallationConfig(String config) {
RealmRepresentation realmRep = realmRep();
assertTrue(config.contains(realmRep.getId()));
assertTrue(config.contains(realmRep.getPublicKey()));
assertTrue(config.contains(authServerUrl()));
}
@Test
public void testSamlMetadataIdpDescriptor() {
String xml = samlClient.getInstallationProvider("saml-idp-descriptor");
RealmRepresentation realmRep = realmRep();
assertTrue(xml.contains("<EntityDescriptor"));
assertTrue(xml.contains("<IDPSSODescriptor"));
assertTrue(xml.contains(realmRep.getCertificate()));
assertTrue(xml.contains(samlUrl(realmRep)));
}
@Test
public void testSamlAdapterXml() {
String xml = samlClient.getInstallationProvider("keycloak-saml");
RealmRepresentation realmRep = realmRep();
assertTrue(xml.contains("<keycloak-saml-adapter>"));
assertTrue(xml.contains(SAML_NAME));
assertTrue(xml.contains(realmRep.getCertificate()));
assertTrue(xml.contains(samlUrl(realmRep)));
}
@Test
public void testSamlMetadataSpDescriptor() {
String xml = samlClient.getInstallationProvider("saml-sp-descriptor");
assertTrue(xml.contains("<EntityDescriptor"));
assertTrue(xml.contains("<SPSSODescriptor"));
assertTrue(xml.contains(SAML_NAME));
}
@Test
public void testSamlJBossXml() {
String xml = samlClient.getInstallationProvider("keycloak-saml-subsystem");
RealmRepresentation realmRep = realmRep();
assertTrue(xml.contains("<secure-deployment"));
assertTrue(xml.contains(SAML_NAME));
assertTrue(xml.contains(realmRep.getCertificate()));
assertTrue(xml.contains(samlUrl(realmRep)));
}
}