Improve createUser params

This commit is contained in:
Richard Cox 2023-07-19 11:19:07 +01:00 committed by Nancy Butler
parent cff5228461
commit 0080233b60
3 changed files with 49 additions and 7 deletions

View File

@ -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',
}
});
});
});

18
cypress/globals.d.ts vendored
View File

@ -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<Element>;
byLabel(label: string): Chainable<Element>;
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;

View File

@ -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: {