Skip to content

Commit

Permalink
[tests-only][full-ci]assign role to user through keycloak (#10119)
Browse files Browse the repository at this point in the history
* assign role to usr

* address reviews

---------

Co-authored-by: Saw-jan <saw.jan.grg3e@gmail.com>
  • Loading branch information
PrajwolAmatya and saw-jan committed Dec 12, 2023
1 parent 9799c27 commit 73cfcae
Show file tree
Hide file tree
Showing 4 changed files with 53 additions and 24 deletions.
3 changes: 1 addition & 2 deletions tests/e2e/cucumber/steps/api.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,8 +22,7 @@ Given(
const admin = this.usersEnvironment.getUser({ key: stepUser })
for await (const info of stepTable.hashes()) {
const user = this.usersEnvironment.getUser({ key: info.id })
const id = await api.graph.getUserId({ user, admin })
await api.graph.assignRole(admin, id, info.role)
await api.provision.assignRole({ admin, user, role: info.role })
}
}
)
Expand Down
31 changes: 21 additions & 10 deletions tests/e2e/support/api/keycloak/user.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,13 @@ import { keycloakRealmRoles } from '../../store'
import { state } from '../../../cucumber/environment/shared'
import { getTokenFromLogin } from '../../utils/tokenHelper'

const ocisKeycloakUserRoles: Record<string, string> = {
Admin: 'ocisAdmin',
'Space Admin': 'ocisSpaceAdmin',
User: 'ocisUser',
'User Light': 'ocisGuest'
}

export const createUser = async ({ user, admin }: { user: User; admin: User }): Promise<User> => {
const fullName = user.displayName.split(' ')
const body = JSON.stringify({
Expand Down Expand Up @@ -39,16 +46,7 @@ export const createUser = async ({ user, admin }: { user: User; admin: User }):
const uuid = getUserIdFromResponse(creationRes)

// assign realmRoles to user
const roleRes = await request({
method: 'POST',
path: join(realmBasePath, 'users', uuid, 'role-mappings', 'realm'),
body: JSON.stringify([
await getRealmRole('ocisUser', admin),
await getRealmRole('offline_access', admin)
]),
user: admin,
header: { 'Content-Type': 'application/json' }
})
const roleRes = await assignRole({ admin, uuid, role: 'User' })
checkResponseStatus(roleRes, 'Failed while assigning roles to user')

const usersEnvironment = new UsersEnvironment()
Expand All @@ -60,6 +58,19 @@ export const createUser = async ({ user, admin }: { user: User; admin: User }):
return user
}

export const assignRole = async ({ admin, uuid, role }) => {
return request({
method: 'POST',
path: join(realmBasePath, 'users', uuid, 'role-mappings', 'realm'),
body: JSON.stringify([
await getRealmRole(ocisKeycloakUserRoles[role], admin),
await getRealmRole('offline_access', admin)
]),
user: admin,
header: { 'Content-Type': 'application/json' }
})
}

const initializeUser = async (username: string): Promise<void> => {
return getTokenFromLogin({ browser: state.browser, username, waitForSelector: '#web-content' })
}
Expand Down
25 changes: 23 additions & 2 deletions tests/e2e/support/api/provision/user.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,17 @@
import { User } from '../../types'
import { createUser as graphCreateUser, deleteUser as graphDeleteUser } from '../graph'
import { createUser as keycloakCreateUser, deleteUser as keycloakDeleteUser } from '../keycloak'
import {
createUser as graphCreateUser,
deleteUser as graphDeleteUser,
assignRole as graphAssignRole,
getUserId
} from '../graph'
import {
createUser as keycloakCreateUser,
deleteUser as keycloakDeleteUser,
assignRole as keycloakAssignRole
} from '../keycloak'
import { config } from '../../../config'
import { UsersEnvironment } from '../../environment'

export const createUser = async ({ user, admin }: { user: User; admin: User }): Promise<User> => {
if (config.keycloak) {
Expand All @@ -16,3 +26,14 @@ export const deleteUser = async ({ user, admin }: { user: User; admin: User }):
}
return graphDeleteUser({ user, admin })
}

export const assignRole = async ({ admin, user, role }): Promise<void> => {
if (config.keycloak) {
const usersEnvironment = new UsersEnvironment()
const createdUser = usersEnvironment.getCreatedUser({ key: user.id })
await keycloakAssignRole({ admin, uuid: createdUser.uuid, role })
} else {
const id = await getUserId({ user, admin })
await graphAssignRole(admin, id, role)
}
}
18 changes: 8 additions & 10 deletions tests/e2e/support/objects/runtime/session.ts
Original file line number Diff line number Diff line change
Expand Up @@ -52,16 +52,14 @@ export class Session {
const body = await response.json()
const tokenEnvironment = TokenEnvironmentFactory(tokenType)

if (!tokenEnvironment.getToken({ user })) {
tokenEnvironment.setToken({
user: { ...user },
token: {
userId: user.id,
accessToken: body.access_token,
refreshToken: body.refresh_token
}
})
}
tokenEnvironment.setToken({
user: { ...user },
token: {
userId: user.id,
accessToken: body.access_token,
refreshToken: body.refresh_token
}
})
}
}

Expand Down

0 comments on commit 73cfcae

Please sign in to comment.