From 0080233b6007c4c49fe6578e779c42a202377e30 Mon Sep 17 00:00:00 2001 From: Richard Cox Date: Wed, 19 Jul 2023 11:19:07 +0100 Subject: [PATCH] Improve createUser params --- cypress/e2e/tests/setup/rancher-setup.spec.ts | 10 ++++++- cypress/globals.d.ts | 18 +++++++++++- cypress/support/commands.ts | 28 +++++++++++++++---- 3 files changed, 49 insertions(+), 7 deletions(-) diff --git a/cypress/e2e/tests/setup/rancher-setup.spec.ts b/cypress/e2e/tests/setup/rancher-setup.spec.ts index 32efa8415e..7de5f3ebb6 100644 --- a/cypress/e2e/tests/setup/rancher-setup.spec.ts +++ b/cypress/e2e/tests/setup/rancher-setup.spec.ts @@ -50,6 +50,14 @@ describe('Rancher setup', { tags: '@adminUser' }, () => { cy.login(); // Note: the username argument here should match the TEST_USERNAME env var used when running non-admin tests - cy.createUser('standard_user', 'user'); + cy.createUser({ + username: 'standard_user0', + globalRole: { role: 'user' }, + projectRole: { + clusterId: 'local', + projectName: 'Default', + role: 'project-member', + } + }); }); }); diff --git a/cypress/globals.d.ts b/cypress/globals.d.ts index 805ab88e08..679e8c370a 100644 --- a/cypress/globals.d.ts +++ b/cypress/globals.d.ts @@ -3,6 +3,22 @@ import { UserPreferences } from '@shell/types/userPreferences'; type Matcher = '$' | '^' | '~' | '*' | ''; +export type CreateUserParams = { + username: string, + globalRole?: { + role: string, + }, + clusterRole?: { + clusterId: string, + role: string, + }, + projectRole?: { + clusterId: string, + projectName: string, + role: string, + } +} + // eslint-disable-next-line no-unused-vars declare namespace Cypress { interface Chainable { @@ -12,7 +28,7 @@ declare namespace Cypress { login(username?: string, password?: string, cacheSession?: boolean): Chainable; byLabel(label: string): Chainable; - createUser(username: string, role?: string): Chainable; + createUser(params: CreateUserParams): Chainable; setGlobalRoleBinding(userId: string, role: string): Chainable; setClusterRoleBinding(clusterId: string, userPrincipalId: string, role: string): Chainable; setProjectRoleBinding(clusterId: string, userPrincipalId: string, projectName: string, role: string): Chainable; diff --git a/cypress/support/commands.ts b/cypress/support/commands.ts index cec58d51c4..6973d549cc 100644 --- a/cypress/support/commands.ts +++ b/cypress/support/commands.ts @@ -1,5 +1,6 @@ import { LoginPagePo } from '@/cypress/e2e/po/pages/login-page.po'; import { Matcher } from '@/cypress/support/types'; +import { CreateUserParams } from '@/cypress/globals'; let token: any; @@ -60,7 +61,11 @@ Cypress.Commands.add('login', ( /** * Create user via api request */ -Cypress.Commands.add('createUser', (username, role?) => { +Cypress.Commands.add('createUser', (params: CreateUserParams) => { + const { + username, globalRole, clusterRole, projectRole + } = params; + return cy.request({ method: 'POST', url: `${ Cypress.env('api') }/v3/users`, @@ -87,9 +92,22 @@ Cypress.Commands.add('createUser', (username, role?) => { const userPrincipalId = resp.body.principalIds[0]; - if (role) { - return cy.setGlobalRoleBinding(resp.body.id, role) - .then(() => cy.setProjectRoleBinding('local', userPrincipalId, 'Default', 'project-member')); + if (globalRole) { + return cy.setGlobalRoleBinding(resp.body.id, globalRole.role) + .then(() => { + if (clusterRole) { + const { clusterId, role } = clusterRole; + + return cy.setClusterRoleBinding(clusterId, userPrincipalId, role); + } + }) + .then(() => { + if (projectRole) { + const { clusterId, projectName, role } = projectRole; + + return cy.setProjectRoleBinding(clusterId, userPrincipalId, projectName, role); + } + }); } } }); @@ -147,7 +165,7 @@ Cypress.Commands.add('setClusterRoleBinding', (clusterId, userPrincipalId, role) */ Cypress.Commands.add('setProjectRoleBinding', (clusterId, userPrincipalId, projectName, role) => { return cy.getProject(clusterId, projectName) - .then((project) => cy.request({ + .then((project: any) => cy.request({ method: 'POST', url: `${ Cypress.env('api') }/v3/projectroletemplatebindings`, headers: {