Skip to content

Commit

Permalink
test: users
Browse files Browse the repository at this point in the history
  • Loading branch information
ferruhcihan committed Oct 2, 2024
1 parent e6e8fc4 commit a89275d
Show file tree
Hide file tree
Showing 3 changed files with 7 additions and 12 deletions.
8 changes: 4 additions & 4 deletions src/operator/keycloak.ts
Original file line number Diff line number Diff line change
Expand Up @@ -710,8 +710,8 @@ async function manageGroups(connection: KeycloakConnection) {
}

async function createUpdateUser(api: any, user: any) {
const { username, email, firstName, lastName, isPlatformAdmin, isTeamAdmin, teams, teamId } = user
const userConf = createTeamUser(username, email, firstName, lastName, isPlatformAdmin, isTeamAdmin, teams, teamId)
const { email, firstName, lastName, isPlatformAdmin, isTeamAdmin, teams } = user
const userConf = createTeamUser(email, firstName, lastName, isPlatformAdmin, isTeamAdmin, teams)
const existingUsersByUserEmail = (await doApiCall([], `Getting users`, () =>
api.users.realmUsersGet(keycloakRealm, false, `${email}`),
)) as UserRepresentation[]
Expand All @@ -723,11 +723,11 @@ async function createUpdateUser(api: any, user: any) {
const updatedUserConf = existingUser.requiredActions?.includes('UPDATE_PASSWORD')
? userConf
: omit(userConf, ['credentials'])
await doApiCall(errors, `Updating user ${username}`, async () =>
await doApiCall(errors, `Updating user ${email}`, async () =>
api.users.realmUsersIdPut(keycloakRealm, existingUser.id as string, updatedUserConf),
)
} else {
await doApiCall(errors, `Creating user ${username}`, () => api.users.realmUsersPost(keycloakRealm, userConf))
await doApiCall(errors, `Creating user ${email}`, () => api.users.realmUsersPost(keycloakRealm, userConf))
}
} catch (error) {
console.error('Error in internalIDP: ', error)
Expand Down
7 changes: 2 additions & 5 deletions src/tasks/keycloak/config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -71,16 +71,14 @@ export const adminUserCfgTpl = (username: string, password: string): Record<stri
})

export const teamUserCfgTpl = (
username: string,
email: string,
firstName: string,
lastName: string,
isPlatformAdmin: boolean,
isTeamAdmin: boolean,
teams: string[],
teamId: string,
): Record<string, unknown> => ({
username,
username: email,
enabled: true,
email,
emailVerified: true,
Expand All @@ -90,13 +88,12 @@ export const teamUserCfgTpl = (
groups: [
...(isPlatformAdmin ? ['platform-admin'] : []),
...(isTeamAdmin ? ['team-admin'] : []),
`team-${teamId}`,
...(teams.length > 0 ? teams.map((team) => `team-${team}`) : []),
],
credentials: [
{
type: 'password',
value: `${username}@APL`,
value: `${email}`,
temporary: true,
},
],
Expand Down
4 changes: 1 addition & 3 deletions src/tasks/keycloak/realm-factory.ts
Original file line number Diff line number Diff line change
Expand Up @@ -120,18 +120,16 @@ export function createAdminUser(username: string, password: string): UserReprese
return userRepresentation
}
export function createTeamUser(
username: string,
email: string,
firstName: string,
lastName: string,
isPlatformAdmin: boolean,
isTeamAdmin: boolean,
teams: string[],
teamId: string,
): UserRepresentation {
const userRepresentation = defaultsDeep(
new UserRepresentation(),
teamUserCfgTpl(username, email, firstName, lastName, isPlatformAdmin, isTeamAdmin, teams, teamId),
teamUserCfgTpl(email, firstName, lastName, isPlatformAdmin, isTeamAdmin, teams),
)
return userRepresentation
}
Expand Down

0 comments on commit a89275d

Please sign in to comment.