Skip to content

Commit

Permalink
Add test to create user, set password and login using admin endpoints
Browse files Browse the repository at this point in the history
  • Loading branch information
stianst committed Jan 8, 2016
1 parent ecd3101 commit cf0eb11
Show file tree
Hide file tree
Showing 4 changed files with 53 additions and 36 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -16,10 +16,7 @@
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;

Expand All @@ -41,32 +38,37 @@ public void before() {
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) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,19 +4,18 @@
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;
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.*;
Expand All @@ -32,6 +31,7 @@
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;
Expand Down Expand Up @@ -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");
Expand All @@ -84,6 +74,7 @@ public String createUser(String username, String email) {
UserRepresentation user = new UserRepresentation();
user.setUsername(username);
user.setEmail(email);
user.setEnabled(true);

Response response = realm.users().create(user);
String createdId = ApiUtil.getCreatedId(response);
Expand Down Expand Up @@ -600,6 +591,28 @@ public void updateUserWithExistingUsername() {
}
}

@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);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -116,6 +116,7 @@ public void update(KeycloakRule.KeycloakSetup configurer, String realmId) {

try {
RealmManager manager = new RealmManager(session);
manager.setContextPath("/auth");

RealmModel adminstrationRealm = manager.getRealm(Config.getAdminRealm());
RealmModel appRealm = manager.getRealm(realmId);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,7 @@ public void configure(KeycloakSetup configurer) {

try {
RealmManager manager = new RealmManager(session);
manager.setContextPath("/auth");

RealmModel adminstrationRealm = manager.getRealm(Config.getAdminRealm());
RealmModel appRealm = manager.getRealm("test");
Expand Down

0 comments on commit cf0eb11

Please sign in to comment.