diff --git a/testsuite/integration-arquillian/tests/base/src/test/java/org/keycloak/testsuite/adduser/AddUserTest.java b/testsuite/integration-arquillian/tests/base/src/test/java/org/keycloak/testsuite/adduser/AddUserTest.java new file mode 100644 index 0000000000..fae53f8fe3 --- /dev/null +++ b/testsuite/integration-arquillian/tests/base/src/test/java/org/keycloak/testsuite/adduser/AddUserTest.java @@ -0,0 +1,166 @@ +/* + * 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.adduser; + +import com.fasterxml.jackson.core.type.TypeReference; +import org.jboss.arquillian.container.test.api.ContainerController; +import org.jboss.arquillian.test.api.ArquillianResource; +import org.junit.Test; +import org.keycloak.admin.client.resource.RealmResource; +import org.keycloak.admin.client.resource.UserResource; +import org.keycloak.credential.hash.Pbkdf2Sha256PasswordHashProviderFactory; +import org.keycloak.models.Constants; +import org.keycloak.representations.idm.*; +import org.keycloak.testsuite.AbstractKeycloakTest; +import org.keycloak.util.JsonSerialization; +import org.keycloak.wildfly.adduser.AddUser; + +import java.io.File; +import java.io.FileInputStream; +import java.util.List; + +import static org.hamcrest.core.Is.is; +import static org.hamcrest.core.IsNot.not; +import static org.hamcrest.core.IsNull.notNullValue; +import static org.hamcrest.text.IsEmptyString.isEmptyOrNullString; +import static org.junit.Assert.*; + +/** + * @author Martin Bartos + * @author Stian Thorgersen + */ +public class AddUserTest extends AbstractKeycloakTest { + + @ArquillianResource + private ContainerController controller; + + @Test + public void addUserTest() throws Exception { + final String username = "addusertest-admin"; + final String realmName = "master"; + final String configDir = System.getProperty("auth.server.config.dir"); + assertThat("AuthServer config directory is NULL !!", configDir, notNullValue()); + + String authServerQualifier = suiteContext.getAuthServerInfo().getQualifier(); + assertThat("Qualifier of AuthServer is empty or NULL !!", authServerQualifier, not(isEmptyOrNullString())); + assertThat("Controller isn't running.", controller.isStarted(authServerQualifier), is(true)); + + AddUser.main(new String[]{"-u", username, "-p", "password", "--sc", configDir}); + + //Read keycloak-add-user.json + List realms = JsonSerialization.readValue(new FileInputStream(new File(configDir, "keycloak-add-user.json")), + new TypeReference>() { + }); + + assertThat("File 'keycloak-add-user.json' is empty.", realms.size() > 0, is(true)); + + //-----------------Get-Indexes-------------------// + int realmIndex = getRealmIndex(realmName, realms); + assertThat("Realm " + realmName + " not found.", realmIndex, is(not(-1))); + + int userIndex = getUserIndex(username, realms.get(realmIndex).getUsers()); + assertThat("User " + username + " not found", userIndex, is(not(-1))); + + + UserRepresentation user = realms.get(realmIndex).getUsers().get(userIndex); + assertThat("Username from Json file is wrong.", user.getUsername(), is(username)); + + //------------------Credentials-----------------------------// + assertThat("User Credentials are NULL", user.getCredentials().get(0), notNullValue()); + CredentialRepresentation credentials = user.getCredentials().get(0); + assertThat("User Credentials have wrong Algorithm.", credentials.getAlgorithm(), is(Pbkdf2Sha256PasswordHashProviderFactory.ID)); + assertThat("User Credentials have wrong Hash Iterations", credentials.getHashIterations(), is(100000)); + + //------------------Restart--Container---------------------// + controller.stop(authServerQualifier); + controller.start(authServerQualifier); + + RealmResource realmResource = getAdminClient().realm(realmName); + assertThat("Realm resource is NULL !!", realmResource, notNullValue()); + + user = realmResource.users().search(username).get(0); + assertThat("Username is wrong.", user.getUsername(), is(username)); + + UserResource userResource = realmResource.users().get(user.getId()); + assertThat("User resource is NULL !!", userResource, notNullValue()); + + //--------------Roles-----------------------// + try { + List realmRoles = userResource.roles().realmLevel().listAll(); + + assertRoles(realmRoles, "admin", "offline_access", Constants.AUTHZ_UMA_AUTHORIZATION); + + List clients = realmResource.clients().findAll(); + String accountId = null; + for (ClientRepresentation c : clients) { + if (c.getClientId().equals("account")) { + accountId = c.getId(); + } + } + + List accountRoles = userResource.roles().clientLevel(accountId).listAll(); + assertRoles(accountRoles, "view-profile", "manage-account"); + } finally { + userResource.remove(); + } + } + + private int getUserIndex(String userName, List list) { + assertThat("Parameter 'list' is NULL.", list, notNullValue()); + assertThat("List is empty.", list.isEmpty(), is(false)); + + for (UserRepresentation u : list) { + if (u.getUsername().equals(userName)) + return list.indexOf(u); + } + return -1; + } + + private int getRealmIndex(String realmName, List list) { + assertThat("Parameter 'list' is NULL.", list, notNullValue()); + assertThat("List is empty.", list.isEmpty(), is(false)); + + for (RealmRepresentation u : list) { + if (u.getRealm().equals(realmName)) + return list.indexOf(u); + } + return -1; + } + + private void assertRoles(List actual, String... expected) { + assertThat("Actual and expected size of Roles are different.", actual.size(), is(expected.length)); + + for (String e : expected) { + boolean found = false; + for (RoleRepresentation r : actual) { + if (r.getName().equals(e)) { + found = true; + break; + } + } + if (!found) { + fail("Role " + e + " not found"); + } + } + } + + @Override + public void addTestRealms(List testRealms) { + } + +} \ No newline at end of file diff --git a/testsuite/integration-deprecated/src/test/java/org/keycloak/testsuite/adduser/AddUserTest.java b/testsuite/integration-deprecated/src/test/java/org/keycloak/testsuite/adduser/AddUserTest.java deleted file mode 100644 index 8dff78966d..0000000000 --- a/testsuite/integration-deprecated/src/test/java/org/keycloak/testsuite/adduser/AddUserTest.java +++ /dev/null @@ -1,158 +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.adduser; - -import com.fasterxml.jackson.core.type.TypeReference; -import org.junit.After; -import org.junit.Before; -import org.junit.Rule; -import org.junit.Test; -import org.junit.rules.TemporaryFolder; -import org.keycloak.admin.client.Keycloak; -import org.keycloak.admin.client.resource.RealmResource; -import org.keycloak.admin.client.resource.UserResource; -import org.keycloak.credential.hash.Pbkdf2Sha256PasswordHashProviderFactory; -import org.keycloak.models.Constants; -import org.keycloak.representations.idm.ClientRepresentation; -import org.keycloak.representations.idm.CredentialRepresentation; -import org.keycloak.representations.idm.RealmRepresentation; -import org.keycloak.representations.idm.RoleRepresentation; -import org.keycloak.representations.idm.UserRepresentation; -import org.keycloak.testsuite.KeycloakServer; -import org.keycloak.util.JsonSerialization; -import org.keycloak.wildfly.adduser.AddUser; - -import java.io.File; -import java.io.FileInputStream; -import java.io.IOException; -import java.util.List; - -import static org.junit.Assert.assertEquals; -import static org.junit.Assert.assertNotNull; -import static org.junit.Assert.assertNull; -import static org.junit.Assert.fail; - -/** - * @author Stian Thorgersen - */ -public class AddUserTest { - - @Rule - public TemporaryFolder folder = new TemporaryFolder(); - - private File dir; - - @Before - public void before() throws IOException { - dir = folder.newFolder(); - System.setProperty("jboss.server.config.user.dir", dir.getAbsolutePath()); - System.setProperty("jboss.server.config.dir", dir.getAbsolutePath()); - } - - @After - public void after() { - System.getProperties().remove("jboss.server.config.user.dir"); - System.getProperties().remove("jboss.server.config.dir"); - } - - @Test - public void addUserTest() throws Throwable { - AddUser.main(new String[]{"-u", "addusertest-admin", "-p", "password"}); - assertEquals(1, dir.listFiles().length); - - List realms = JsonSerialization.readValue(new FileInputStream(new File(dir, "keycloak-add-user.json")), new TypeReference>() {}); - assertEquals(1, realms.size()); - assertEquals(1, realms.get(0).getUsers().size()); - - UserRepresentation user = realms.get(0).getUsers().get(0); - assertEquals(new Integer(100000), user.getCredentials().get(0).getHashIterations()); - assertNull(user.getCredentials().get(0).getValue()); - - CredentialRepresentation credentials = user.getCredentials().get(0); - - assertEquals(Pbkdf2Sha256PasswordHashProviderFactory.ID, credentials.getAlgorithm()); - assertEquals(new Integer(100000), credentials.getHashIterations()); - - KeycloakServer server = new KeycloakServer(); - try { - server.start(); - - Keycloak keycloak = Keycloak.getInstance("http://localhost:8081/auth", "master", "addusertest-admin", "password", Constants.ADMIN_CLI_CLIENT_ID); - keycloak.realms().findAll(); - - RealmRepresentation testRealm = new RealmRepresentation(); - testRealm.setEnabled(true); - testRealm.setId("test"); - testRealm.setRealm("test"); - - keycloak.realms().create(testRealm); - - RealmResource realm = keycloak.realm("master"); - - List users = realm.users().search("addusertest-admin", null, null, null, null, null); - assertEquals(1, users.size()); - - UserRepresentation created = users.get(0); - assertNotNull(created.getCreatedTimestamp()); - - UserResource userResource = realm.users().get(created.getId()); - - List realmRoles = userResource.roles().realmLevel().listAll(); - - assertRoles(realmRoles, "admin", "offline_access", Constants.AUTHZ_UMA_AUTHORIZATION); - - List clients = realm.clients().findAll(); - String accountId = null; - for (ClientRepresentation c : clients) { - if (c.getClientId().equals("account")) { - accountId = c.getId(); - } - } - - List accountRoles = userResource.roles().clientLevel(accountId).listAll(); - - assertRoles(accountRoles, "view-profile", "manage-account"); - - // User needs to delete himself as other tests rely on "admin" user - userResource.remove(); - - keycloak.close(); - - assertEquals(0, dir.listFiles().length); - } finally { - server.stop(); - } - } - - public static void assertRoles(List actual, String... expected) { - assertEquals(expected.length, actual.size()); - - for (String e : expected) { - boolean found = false; - for (RoleRepresentation r : actual) { - if (r.getName().equals(e)) { - found = true; - } - } - if (!found) { - fail("Role " + e + " not found"); - } - } - } - -}