Package org.keycloak.test
Class FluentTestsHelper
java.lang.Object
org.keycloak.test.FluentTestsHelper
- All Implemented Interfaces:
Closeable
,AutoCloseable
A helper class that makes creating tests a bit easier.
Usage example:
new FluentTestsHelper()
.init()
.createDirectGrantClient("direct-grant-client")
.deleteClient("direct-grant-client")
.createTestUser("seb", "seb")
.assignRoleWithUser("seb", "user")
.deleteTestUser("seb")
.deleteRole("user");
-
Nested Class Summary
Nested Classes -
Field Summary
FieldsModifier and TypeFieldDescriptionprotected String
protected final String
protected final String
protected final String
protected final String
protected Map<String,
FluentTestsHelper.ClientData> static final String
static final String
static final String
static final String
static final String
static final String
static final String
protected boolean
protected Keycloak
protected final String
protected String
-
Constructor Summary
ConstructorsConstructorDescriptionCreates a new helper instance.FluentTestsHelper
(String adminUserName, String adminPassword) Creates a new helper instance.FluentTestsHelper
(String keycloakBaseUrl, String adminUserName, String adminPassword, String adminRealm, String adminClient, String testRealm) Creates a new helper instance. -
Method Summary
Modifier and TypeMethodDescriptionassignRoleWithUser
(String userName, String roleName) Associates a user with a role.void
close()
createClient
(org.keycloak.representations.idm.ClientRepresentation clientRepresentation) Creates a new client based on its representation.createDirectGrantClient
(String clientId) Creates a direct grant client.protected Keycloak
createKeycloakInstance
(String keycloakBaseUrl, String realm, String username, String password, String clientId) createTestRealm
(org.keycloak.representations.idm.RealmRepresentation realmRepresentation) Creates a test realm.createTestUser
(String username, String password) Creates a test user.deleteClient
(String clientId) Deletes a client previously created by this helper.deleteRealm
(String realmName) Deletes a realm.deleteRole
(String roleName) Deletes a role.Deletes the test realm.deleteTestUser
(String userName) Deletes a user.protected String
getCreatedId
(jakarta.ws.rs.core.Response response) For more complex test scenariosgetToken()
importTestRealm
(File realmJsonPath) importTestRealm
(InputStream stream) Import a test realm.importTestRealm
(String realmJsonPath) init()
Initialization method.boolean
boolean
returnsForbidden
(String endpoint) Checks if a given endpoint returns Forbidden HTTP Code.boolean
testGetWithAuth
(String endpoint, String token) Checks if given endpoint returns successfully with supplied token.
-
Field Details
-
DEFAULT_KEYCLOAK_URL
- See Also:
-
DEFAULT_ADMIN_USERNAME
- See Also:
-
DEFAULT_ADMIN_PASSWORD
- See Also:
-
DEFAULT_ADMIN_REALM
- See Also:
-
DEFAULT_ADMIN_CLIENT
- See Also:
-
DEFAULT_TEST_REALM
- See Also:
-
DEFAULT_USER_ROLE
- See Also:
-
keycloakBaseUrl
-
adminUserName
-
adminPassword
-
adminClient
-
adminRealm
-
testRealm
-
keycloak
-
accessToken
-
isInitialized
protected volatile boolean isInitialized -
createdClients
-
-
Constructor Details
-
FluentTestsHelper
public FluentTestsHelper()Creates a new helper instance. -
FluentTestsHelper
Creates a new helper instance.- Parameters:
adminUserName
- Admin username.adminPassword
- Admin password.
-
FluentTestsHelper
public FluentTestsHelper(String keycloakBaseUrl, String adminUserName, String adminPassword, String adminRealm, String adminClient, String testRealm) Creates a new helper instance.- Parameters:
keycloakBaseUrl
- Full keycloak URL.adminUserName
- Admin username.adminPassword
- Admin password.adminRealm
- Master realm name.adminClient
- Admin Client name.testRealm
- new instance.
-
-
Method Details
-
init
Initialization method.- Returns:
this
-
isInitialized
public boolean isInitialized()- Returns:
- Returns
true
if this helper has been initialized.
-
createKeycloakInstance
-
getKeycloakInstance
For more complex test scenarios- Returns:
- Keycloak Client instance
-
generateInitialAccessToken
-
createClient
public FluentTestsHelper createClient(org.keycloak.representations.idm.ClientRepresentation clientRepresentation) throws ClientRegistrationException, com.fasterxml.jackson.core.JsonProcessingException Creates a new client based on its representation.- Parameters:
clientRepresentation
- Client data.- Returns:
this
- Throws:
ClientRegistrationException
com.fasterxml.jackson.core.JsonProcessingException
-
createDirectGrantClient
public FluentTestsHelper createDirectGrantClient(String clientId) throws ClientRegistrationException, com.fasterxml.jackson.core.JsonProcessingException Creates a direct grant client.- Throws:
ClientRegistrationException
com.fasterxml.jackson.core.JsonProcessingException
-
deleteClient
Deletes a client previously created by this helper. This will throw an error if you try to delete an arbitrary client.- Parameters:
clientId
- Client id to be deleted.- Returns:
this
- Throws:
ClientRegistrationException
- Thrown when client registration error occurs.
-
importTestRealm
- Throws:
IOException
- See Also:
-
importTestRealm
- Throws:
IOException
- See Also:
-
importTestRealm
Import a test realm.- Parameters:
stream
- A stream representing a JSON file with an exported realm.- Returns:
this
- Throws:
IOException
- Thrown in case of parsing error.
-
createTestRealm
public FluentTestsHelper createTestRealm(org.keycloak.representations.idm.RealmRepresentation realmRepresentation) Creates a test realm.- Parameters:
realmRepresentation
- A test realm representation.- Returns:
this
-
deleteRealm
Deletes a realm.- Parameters:
realmName
- Realm to be deleted.- Returns:
this
-
deleteTestRealm
Deletes the test realm. Meant to be called after testing has finished.- Returns:
this
-
createTestUser
Creates a test user.- Parameters:
username
- A username to be created.password
- A password for a user.- Returns:
this
-
assignRoleWithUser
Associates a user with a role. This method also creates a role if that is missing.- Parameters:
userName
- A username to be associated with a role.roleName
- A role to be associated with a user name.- Returns:
this
-
deleteRole
Deletes a role.- Parameters:
roleName
- A Role name to be deleted.- Returns:
this
-
deleteTestUser
Deletes a user.- Parameters:
userName
- A Username to be deleted.- Returns:
this
-
getCreatedId
- Parameters:
response
-- Returns:
- ID of the created record
-
testGetWithAuth
Checks if given endpoint returns successfully with supplied token.- Parameters:
endpoint
- Endpoint to be evaluated,token
- Token that will be passed into theAuthorization
header.- Returns:
true
if the endpoint returns forbidden.- Throws:
IOException
- Thrown by the underlying HTTP Client implementation
-
returnsForbidden
Checks if a given endpoint returns Forbidden HTTP Code.- Parameters:
endpoint
- Endpoint to be evaluated,- Returns:
true
if the endpoint returns forbidden.- Throws:
IOException
- Thrown by the underlying HTTP Client implementation
-
getToken
- Returns:
- Returns an Access Token.
-
getKeycloakBaseUrl
-
getAdminUserName
-
getAdminPassword
-
getAdminClientId
-
getAdminRealmName
-
getTestRealmName
-
getTestRealmResource
-
close
public void close()- Specified by:
close
in interfaceAutoCloseable
- Specified by:
close
in interfaceCloseable
-