Add test to create user, set password and login using admin endpoints
This commit is contained in:
parent
ecd31010fe
commit
cf0eb1184a
4 changed files with 53 additions and 36 deletions
|
@ -16,10 +16,7 @@ import org.keycloak.testsuite.rule.KeycloakRule;
|
|||
import org.keycloak.testsuite.rule.WebResource;
|
||||
import org.openqa.selenium.WebDriver;
|
||||
|
||||
import java.util.Arrays;
|
||||
import java.util.Collections;
|
||||
import java.util.Comparator;
|
||||
import java.util.List;
|
||||
import java.util.*;
|
||||
|
||||
import static org.junit.Assert.assertArrayEquals;
|
||||
|
||||
|
@ -41,32 +38,37 @@ public abstract class AbstractClientTest {
|
|||
keycloakRule.configure(new KeycloakRule.KeycloakSetup() {
|
||||
@Override
|
||||
public void config(RealmManager manager, RealmModel adminstrationRealm, RealmModel appRealm) {
|
||||
RealmModel testRealm = manager.createRealm(REALM_NAME);
|
||||
testRealm.setEnabled(true);
|
||||
testRealm.setAccessCodeLifespanUserAction(600);
|
||||
KeycloakModelUtils.generateRealmKeys(testRealm);
|
||||
|
||||
appRealm.getClientByClientId("test-app").setDirectAccessGrantsEnabled(true);
|
||||
}
|
||||
});
|
||||
|
||||
keycloak = Keycloak.getInstance("http://localhost:8081/auth", "master", "admin", "admin", Constants.ADMIN_CLI_CLIENT_ID);
|
||||
|
||||
RealmRepresentation rep = new RealmRepresentation();
|
||||
rep.setRealm(REALM_NAME);
|
||||
rep.setEnabled(true);
|
||||
|
||||
Map<String, String> config = new HashMap<>();
|
||||
config.put("from", "auto@keycloak.org");
|
||||
config.put("host", "localhost");
|
||||
config.put("port", "3025");
|
||||
|
||||
rep.setSmtpServer(config);
|
||||
|
||||
keycloak.realms().create(rep);
|
||||
|
||||
realm = keycloak.realm(REALM_NAME);
|
||||
}
|
||||
|
||||
@After
|
||||
public void after() {
|
||||
keycloak.close();
|
||||
|
||||
keycloakRule.configure(new KeycloakRule.KeycloakSetup() {
|
||||
@Override
|
||||
public void config(RealmManager manager, RealmModel adminstrationRealm, RealmModel appRealm) {
|
||||
RealmModel realm = manager.getRealmByName(REALM_NAME);
|
||||
if (realm != null) {
|
||||
manager.removeRealm(realm);
|
||||
}
|
||||
for (RealmRepresentation r : keycloak.realms().findAll()) {
|
||||
if (r.getRealm().equals(REALM_NAME)) {
|
||||
keycloak.realm(REALM_NAME).remove();
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
keycloak.close();
|
||||
}
|
||||
|
||||
public static <T> void assertNames(List<T> actual, String... expected) {
|
||||
|
|
|
@ -4,6 +4,7 @@ import org.junit.Assert;
|
|||
import org.junit.Before;
|
||||
import org.junit.Rule;
|
||||
import org.junit.Test;
|
||||
import org.keycloak.admin.client.Keycloak;
|
||||
import org.keycloak.admin.client.resource.IdentityProviderResource;
|
||||
import org.keycloak.admin.client.resource.UserResource;
|
||||
import org.keycloak.events.Details;
|
||||
|
@ -11,12 +12,10 @@ import org.keycloak.events.EventType;
|
|||
import org.keycloak.models.RealmModel;
|
||||
import org.keycloak.models.UserModel;
|
||||
import org.keycloak.models.utils.KeycloakModelUtils;
|
||||
import org.keycloak.representations.idm.ErrorRepresentation;
|
||||
import org.keycloak.representations.idm.FederatedIdentityRepresentation;
|
||||
import org.keycloak.representations.idm.IdentityProviderRepresentation;
|
||||
import org.keycloak.representations.idm.RealmRepresentation;
|
||||
import org.keycloak.representations.idm.UserRepresentation;
|
||||
import org.keycloak.representations.idm.*;
|
||||
import org.keycloak.services.managers.RealmManager;
|
||||
import org.keycloak.services.resources.RealmsResource;
|
||||
import org.keycloak.testsuite.Constants;
|
||||
import org.keycloak.testsuite.actions.RequiredActionEmailVerificationTest;
|
||||
import org.keycloak.testsuite.forms.ResetPasswordTest;
|
||||
import org.keycloak.testsuite.pages.*;
|
||||
|
@ -32,6 +31,7 @@ import javax.mail.internet.MimeMultipart;
|
|||
import javax.ws.rs.BadRequestException;
|
||||
import javax.ws.rs.ClientErrorException;
|
||||
import javax.ws.rs.core.Response;
|
||||
import javax.ws.rs.core.UriBuilder;
|
||||
|
||||
import java.io.IOException;
|
||||
import java.util.ArrayList;
|
||||
|
@ -63,18 +63,8 @@ public class UserTest extends AbstractClientTest {
|
|||
@WebResource
|
||||
protected InfoPage infoPage;
|
||||
|
||||
@Before
|
||||
public void before() {
|
||||
super.before();
|
||||
|
||||
keycloakRule.configure(new KeycloakRule.KeycloakSetup() {
|
||||
@Override
|
||||
public void config(RealmManager manager, RealmModel adminstrationRealm, RealmModel appRealm) {
|
||||
RealmModel testRealm = manager.getRealm(REALM_NAME);
|
||||
greenMail.configureRealm(testRealm);
|
||||
}
|
||||
});
|
||||
}
|
||||
@WebResource
|
||||
protected LoginPage loginPage;
|
||||
|
||||
public String createUser() {
|
||||
return createUser("user1", "user1@localhost");
|
||||
|
@ -84,6 +74,7 @@ public class UserTest extends AbstractClientTest {
|
|||
UserRepresentation user = new UserRepresentation();
|
||||
user.setUsername(username);
|
||||
user.setEmail(email);
|
||||
user.setEnabled(true);
|
||||
|
||||
Response response = realm.users().create(user);
|
||||
String createdId = ApiUtil.getCreatedId(response);
|
||||
|
@ -600,6 +591,28 @@ public class UserTest extends AbstractClientTest {
|
|||
}
|
||||
}
|
||||
|
||||
@Test
|
||||
public void resetUserPassword() {
|
||||
String userId = createUser("user1", "user1@localhost");
|
||||
|
||||
CredentialRepresentation cred = new CredentialRepresentation();
|
||||
cred.setType(CredentialRepresentation.PASSWORD);
|
||||
cred.setValue("password");
|
||||
cred.setTemporary(false);
|
||||
|
||||
realm.users().get(userId).resetPassword(cred);
|
||||
|
||||
String accountUrl = RealmsResource.accountUrl(UriBuilder.fromUri(Constants.AUTH_SERVER_ROOT)).build(REALM_NAME).toString();
|
||||
|
||||
driver.navigate().to(accountUrl);
|
||||
|
||||
assertEquals("Log in to admin-client-test", driver.getTitle());
|
||||
|
||||
loginPage.login("user1", "password");
|
||||
|
||||
assertEquals("Keycloak Account Management", driver.getTitle());
|
||||
}
|
||||
|
||||
private void switchEditUsernameAllowedOn() {
|
||||
RealmRepresentation rep = realm.toRepresentation();
|
||||
rep.setEditUsernameAllowed(true);
|
||||
|
|
|
@ -116,6 +116,7 @@ public abstract class AbstractKeycloakRule extends ExternalResource {
|
|||
|
||||
try {
|
||||
RealmManager manager = new RealmManager(session);
|
||||
manager.setContextPath("/auth");
|
||||
|
||||
RealmModel adminstrationRealm = manager.getRealm(Config.getAdminRealm());
|
||||
RealmModel appRealm = manager.getRealm(realmId);
|
||||
|
|
|
@ -69,6 +69,7 @@ public class KeycloakRule extends AbstractKeycloakRule {
|
|||
|
||||
try {
|
||||
RealmManager manager = new RealmManager(session);
|
||||
manager.setContextPath("/auth");
|
||||
|
||||
RealmModel adminstrationRealm = manager.getRealm(Config.getAdminRealm());
|
||||
RealmModel appRealm = manager.getRealm("test");
|
||||
|
|
Loading…
Reference in a new issue