Skip to content

Commit

Permalink
Adding cypres tests for dynamic multi-tenancy features (#609) (#659)
Browse files Browse the repository at this point in the history
Signed-off-by: Abhi Kalra <abhivka@amazon.com>
Co-authored-by: Abhi Kalra <abhivka@amazon.com>
(cherry picked from commit 7ec51ed)

Co-authored-by: Abhi Kalra <99718513+abhivka7@users.noreply.github.com>
  • Loading branch information
opensearch-trigger-bot[bot] and abhivka7 authored May 8, 2023
1 parent f86b7a7 commit 21e7a49
Show file tree
Hide file tree
Showing 7 changed files with 223 additions and 0 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
{
"securitytenant": ["global"]
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
{
"securitytenant": ["private"]
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
/*
* Copyright OpenSearch Contributors
* SPDX-License-Identifier: Apache-2.0
*/

import { SAVED_OBJECTS_PATH } from '../../../utils/dashboards/constants';

import { CURRENT_TENANT } from '../../../utils/commands';
import tenantDescription from '../../../fixtures/plugins/security-dashboards-plugin/tenants/testTenant';

const tenantName = 'test';

if (Cypress.env('SECURITY_ENABLED')) {
describe('Multi Tenancy Default Tenant Tests: ', () => {
before(() => {
cy.server();
cy.createTenant(tenantName, tenantDescription);
cy.changeDefaultTenant({
multitenancy_enabled: true,
private_tenant_enabled: true,
default_tenant: tenantName,
});
});
it('Test Changed Default Tenant ', () => {
CURRENT_TENANT.newTenant = null;
cy.visit(SAVED_OBJECTS_PATH);
cy.waitForLoader();
cy.get('#user-icon-btn').click();
cy.wait(1000);
cy.get('#tenantName').contains(tenantName);
});
});
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,150 @@
/*
* Copyright OpenSearch Contributors
* SPDX-License-Identifier: Apache-2.0
*/

import {
SAVED_OBJECTS_PATH,
TENANTS_MANAGE_PATH,
} from '../../../utils/dashboards/constants';

import { CURRENT_TENANT } from '../../../utils/commands';

import indexPatternPrivateTenantHeaderSetUp from '../../../fixtures/plugins/security-dashboards-plugin/indexpatterns/indexPatternPrivateTenantHeader';
import indexPatternGlobalTenantHeaderSetUp from '../../../fixtures/plugins/security-dashboards-plugin/indexpatterns/indexPatternGlobalTenantHeader';
import tenantDescription from '../../../fixtures/plugins/security-dashboards-plugin/tenants/testTenant.json';

const tenantName = 'test';

if (Cypress.env('SECURITY_ENABLED')) {
describe('Multi Tenancy Tests: ', () => {
before(() => {
cy.server();
cy.createTenant(tenantName, tenantDescription);
cy.createIndexPattern(
'index-pattern1',
{
title: 's*',
timeFieldName: 'timestamp',
},
indexPatternGlobalTenantHeaderSetUp
);

cy.createIndexPattern(
'index-pattern2',
{
title: 'se*',
timeFieldName: 'timestamp',
},
indexPatternPrivateTenantHeaderSetUp
);
});
it('Test 1 Disable Multi Tenancy ', () => {
CURRENT_TENANT.newTenant = 'private';

// Load into Private Tenant initially and check if index Pattern matches to the one saved in Private tenant.
cy.visit(SAVED_OBJECTS_PATH);
cy.waitForLoader();
cy.contains('a', 'Saved objects');
cy.contains('a', 'se*');

// Switch tenants button should exist when multi-tenancy is enabled.
cy.get('#user-icon-btn').click();
cy.contains('button', 'Switch tenants').should('exist');

// Disable multi-tenancy.
cy.visit(TENANTS_MANAGE_PATH);
cy.waitForLoader();

cy.contains('button', 'Configure').click();
cy.waitForLoader();
cy.get('#EnableMultitenancyCheckBox').uncheck({ force: true });
cy.contains('button', 'Save Changes').click();

cy.get('#tenancyChangeCheckbox').check({ force: true });
cy.contains('button', 'Apply changes').click();

cy.reload();
cy.waitForLoader();

// Switch tenants button should not exist when multi-tenancy is disabled.
cy.get('#user-icon-btn').click();
cy.contains('button', 'Switch tenants').should('not.exist');

//Tenancy disbaled warning should show on manage page.
cy.contains('Tenancy is disabled').should('exist');

// Saved index pattern should only have the ones saved in Global tenant.
cy.visit(SAVED_OBJECTS_PATH);
cy.waitForLoader();
cy.contains('a', 'Saved objects');
cy.contains('a', 's*');

// Enable Multi-tenancy before closing test.
cy.visit(TENANTS_MANAGE_PATH);
cy.waitForLoader();
cy.contains('button', 'Configure').click();
cy.waitForLoader();
cy.get('#EnableMultitenancyCheckBox').check({ force: true });
cy.contains('button', 'Save Changes').click();
cy.get('#tenancyChangeCheckbox').check({ force: true });
cy.contains('button', 'Apply changes').click();
cy.reload();
cy.waitForLoader();
});
it('Test 2 Disable Private Tenancy ', () => {
CURRENT_TENANT.newTenant = 'private';

// Load into Private Tenant initially and check if index Pattern matches to the one saved in Private tenant.
cy.visit(SAVED_OBJECTS_PATH);
cy.waitForLoader();
cy.contains('a', 'Saved objects');
cy.contains('a', 'se*');

// Check if switching to private tenant is enabled.
cy.get('#user-icon-btn').click();
cy.contains('button', 'Switch tenants').click();
cy.get('#private').should('be.enabled');
cy.contains('button', 'Cancel').click();

// Disable private tenant.
cy.visit(TENANTS_MANAGE_PATH);
cy.waitForLoader();

cy.contains('button', 'Configure').click();
cy.waitForLoader();
cy.get('#EnablePrivateTenantCheckBox').uncheck({ force: true });
cy.contains('button', 'Save Changes').click();

cy.get('#privateTenancyChangeCheckbox').check({ force: true });
cy.contains('button', 'Apply changes').click();

cy.reload();
cy.waitForLoader();

// Check if switching to private tenant is enabled.
cy.get('#user-icon-btn').click();
cy.contains('button', 'Switch tenants').click();
cy.get('#private').should('be.disabled');
cy.contains('button', 'Cancel').click();

// Saved index pattern should only have the ones saved in Global tenant.
cy.visit(SAVED_OBJECTS_PATH);
cy.waitForLoader();
cy.contains('a', 'Saved objects');
cy.contains('a', 's*');

// Enable private tenant before exiting test.
cy.visit(TENANTS_MANAGE_PATH);
cy.waitForLoader();
cy.contains('button', 'Configure').click();
cy.waitForLoader();
cy.get('#EnablePrivateTenantCheckBox').check({ force: true });
cy.contains('button', 'Save Changes').click();
cy.get('#privateTenancyChangeCheckbox').check({ force: true });
cy.contains('button', 'Apply changes').click();
cy.reload();
cy.waitForLoader();
});
});
}
16 changes: 16 additions & 0 deletions cypress/utils/commands.js
Original file line number Diff line number Diff line change
Expand Up @@ -311,6 +311,22 @@ Cypress.Commands.add('createIndexPattern', (id, attributes, header = {}) => {
});
});

Cypress.Commands.add('changeDefaultTenant', (attributes, header = {}) => {
const url =
Cypress.env('openSearchUrl') + '/_plugins/_security/api/tenancy/config';

cy.request({
method: 'PUT',
url,
headers: {
'content-type': 'application/json;charset=UTF-8',
'osd-xsrf': true,
...header,
},
body: JSON.stringify(attributes),
});
});

Cypress.Commands.add('deleteIndexPattern', (id, options = {}) =>
cy.deleteSavedObject('index-pattern', id, options)
);
Expand Down
2 changes: 2 additions & 0 deletions cypress/utils/dashboards/constants.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@ import { BASE_PATH } from '../base_constants';

// STACK MANAGEMENT PATH
export const STACK_MANAGEMENT_PATH = BASE_PATH + '/app/management';
export const TENANTS_MANAGE_PATH =
BASE_PATH + '/app/security-dashboards-plugin#/tenants';

export const INDEX_PATTERN_PATH =
STACK_MANAGEMENT_PATH + '/opensearch-dashboards/indexPatterns';
Expand Down
16 changes: 16 additions & 0 deletions cypress/utils/index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -87,6 +87,22 @@ declare namespace Cypress {
header: string,
): Chainable<S>;


/**
* Changes the Default tenant for the domain.
* @example
* cy.changeDefaultTenant({multitenancy_enabled: true, private_tenant_enabled: true, default_tenant: tenantName, });
*/
changeDefaultTenant<S = any>(
attributes: {
multitenancy_enabled: boolean,
private_tenant_enabled: boolean,
default_tenant: string;
},
// header: string,
// default_tenant: string
): Chainable<S>;

/**
* Delete an index pattern
* @example
Expand Down

0 comments on commit 21e7a49

Please sign in to comment.