keycloak-scim/cypress/support/util/AdminClient.ts
Erik Jan de Wit e4d83d0fe3
Group details (#425)
* added details for groups

* add includeSubGroups checkbox

* added tests

* fixed reload for group attributes

* fixed spacing on associate roles tab

* fixed group reload after save

* fixed test
2021-03-16 08:37:57 -04:00

65 lines
1.7 KiB
TypeScript

import KeycloakAdminClient from "keycloak-admin";
export default class AdminClient {
private client: KeycloakAdminClient;
constructor() {
this.client = new KeycloakAdminClient({
baseUrl: "http://localhost:8180/auth",
realmName: "master",
});
}
private async login() {
await this.client.auth({
username: "admin",
password: "admin",
grantType: "password",
clientId: "admin-cli",
});
}
async deleteRealm(realm: string) {
await this.login();
await this.client.realms.del({ realm });
}
async deleteClient(clientName: string) {
await this.login();
const client = (
await this.client.clients.find({ clientId: clientName })
)[0];
await this.client.clients.del({ id: client.id! });
}
async createSubGroups(groups: string[]) {
await this.login();
let parentGroup = undefined;
const createdGroups = [];
for (const group of groups) {
if (!parentGroup) {
parentGroup = await this.client.groups.create({ name: group });
} else {
parentGroup = await this.client.groups.setOrCreateChild(
{ id: parentGroup.id },
{ name: group }
);
}
createdGroups.push(parentGroup);
}
return createdGroups;
}
async deleteGroups() {
await this.login();
const groups = await this.client.groups.find();
for (const group of groups) {
await this.client.groups.del({ id: group.id! });
}
}
async createUserInGroup(username: string, groupId: string) {
await this.login();
const user = await this.client.users.create({ username, enabled: true });
await this.client.users.addToGroup({ id: user.id!, groupId });
}
}