Skip to content

Commit

Permalink
test for default data source, added no-auth and basic auth, data sou…
Browse files Browse the repository at this point in the history
…rce table with multi delete default ds reset, edit default ds (#1338)

Signed-off-by: yujin-emma <[email protected]>
  • Loading branch information
yujin-emma authored Jun 6, 2024
1 parent be92aa0 commit fc490c3
Show file tree
Hide file tree
Showing 2 changed files with 205 additions and 0 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,97 @@
/*
* Copyright OpenSearch Contributors
* SPDX-License-Identifier: Apache-2.0
*/

import { MiscUtils } from '@opensearch-dashboards-test/opensearch-dashboards-test-library';

const miscUtils = new MiscUtils(cy);

if (Cypress.env('DATASOURCE_MANAGEMENT_ENABLED')) {
describe('Default data sources', () => {
before(() => {
// Clean up before creating new data sources for testing
cy.deleteAllDataSourcesOnUI();
});

describe('The default data source can behave normal when edit data source table', () => {
before(() => {
for (let i = 1; i < 4; i++) {
const title = `ds_${i}`;
cy.createDataSourceNoAuthWithTitle(title);
cy.wait(6000);
}
cy.visitDataSourcesListingPage();
cy.wait(6000);
});
after(() => {
// Clean up after all test are run
cy.deleteAllDataSourcesOnUI();
});
it('The first data source is the default data source', () => {
cy.visitDataSourcesListingPage();

// Use Cypress commands to find the table cell with content "ds_1"
cy.contains('td.euiTableRowCell', 'ds_1').as('ds_1');

// Check if the cell with current ds_1 content exists
cy.get('@ds_1').should('exist');

// Get the parent row of the cell
cy.get('@ds_1').parents('tr').as('row_ds_1');
// Check if the "Default" badge exists in the same row
cy.get('@row_ds_1').contains('span', 'Default').should('exist');
});
it('Delete the default data source, the next one will become default data source', () => {
cy.singleDeleteDataSourceByTitle('ds_1');

// Check that ds_2 now has the Default badge
cy.contains('td.euiTableRowCell', 'ds_2').as('ds_2');
cy.get('@ds_2').parents('tr').as('row_ds_2');
cy.get('@row_ds_2').contains('span', 'Default').should('exist');
});
it('Go the edit data source page of default data source, the set_default button should be disabled', () => {
cy.contains('a', 'ds_2')
.should('exist') // Ensure the anchor tag exists
.invoke('attr', 'href') // Get the href attribute
.then((href) => {
// Extract the unique identifier part from the href
const uniqueId = href.split('/').pop(); // Assumes the unique ID is the last part of the URL
miscUtils.visitPage(
`app/management/opensearch-dashboards/dataSources/${uniqueId}`
);
cy.getElementByTestId('editSetDefaultDataSource')
.should('be.exist')
.should('not.enabled');
cy.wait(1000);
});
});
it('Go the edit data source page of non-default data source, the set_default button should be enabled and can set to default ds', () => {
cy.visitDataSourcesListingPage();

cy.contains('a', 'ds_3')
.should('exist') // Ensure the anchor tag exists
.invoke('attr', 'href') // Get the href attribute
.then((href) => {
// Extract the unique identifier part from the href
const uniqueId = href.split('/').pop(); // Assumes the unique ID is the last part of the URL
miscUtils.visitPage(
`app/management/opensearch-dashboards/dataSources/${uniqueId}`
);
cy.getElementByTestId('editSetDefaultDataSource')
.should('be.exist')
.should('be.enabled')
.click({ force: true });
cy.wait(1000);
});
cy.visitDataSourcesListingPage();
cy.contains('td.euiTableRowCell', 'ds_3').as('ds_3');
cy.get('@ds_3').parents('tr').as('row_ds_3');
cy.get('@row_ds_3').contains('span', 'Default').should('exist');
cy.contains('td.euiTableRowCell', 'ds_2').as('ds_2');
cy.get('@ds_2').parents('tr').as('row_ds_2');
cy.get('@row_ds_2').contains('span', 'Default').should('not.exist');
});
});
});
}
Original file line number Diff line number Diff line change
Expand Up @@ -240,3 +240,111 @@ Cypress.Commands.add('createDataSourceNoAuthWithTitle', (title) => {
expect(interception.response.statusCode).to.equal(200);
});
});

Cypress.Commands.add('multiDeleteDataSourceByTitle', (dataSourceTitles) => {
cy.visitDataSourcesListingPage();
cy.wait(1000);

dataSourceTitles.forEach((dataSourceTitle) => {
cy.contains('a', dataSourceTitle)
.should('exist') // Ensure the anchor tag exists
.invoke('attr', 'href') // Get the href attribute
.then((href) => {
// Extract the unique identifier part from the href
const uniqueId = href.split('/').pop(); // Assumes the unique ID is the last part of the URL
if (isValidUUID(uniqueId)) {
const testId = `checkboxSelectRow-${uniqueId}`;
cy.getElementByTestId(testId)
.check({ force: true })
.should('be.checked');
}
});
});

// Wait for the selections to be checked
cy.wait(1000);

cy.getElementByTestId('deleteDataSourceConnections')
.should('exist')
.should('be.enabled')
.click({ force: true });

// Wait for the delete confirmation modal
cy.wait(1000);

cy.get('button[data-test-subj="confirmModalConfirmButton"]')
.should('exist') // Ensure the button exists
.should('be.visible') // Ensure the button is visible
.click({ force: true });

// Wait for the delete action to complete
cy.wait(1000);
cy.visitDataSourcesListingPage();
});

Cypress.Commands.add('singleDeleteDataSourceByTitle', (dataSourceTitle) => {
cy.visitDataSourcesListingPage();
cy.wait(1000);
cy.contains('a', dataSourceTitle)
.should('exist') // Ensure the anchor tag exists
.invoke('attr', 'href') // Get the href attribute
.then((href) => {
// Extract the unique identifier part from the href
const uniqueId = href.split('/').pop(); // Assumes the unique ID is the last part of the URL
miscUtils.visitPage(
`app/management/opensearch-dashboards/dataSources/${uniqueId}`
);
cy.wait(1000);
cy.getElementByTestId('editDatasourceDeleteIcon').click({ force: true });
});
cy.wait(1000);
cy.get('button[data-test-subj="confirmModalConfirmButton"]')
.should('exist') // Ensure the button exists
.should('be.visible') // Ensure the button is visible
.click({ force: true }); // Click the button
cy.wait(1000);
cy.visitDataSourcesListingPage();
});

Cypress.Commands.add('deleteAllDataSourcesOnUI', () => {
cy.visitDataSourcesListingPage();
cy.wait(1000);

// Clean all data sources
// check if checkboxSelectAll input exist
// if exist, check it and delete all
// for test purpose, no need to consider pagination
// we can use both deleteAll on UI and request part
cy.ifElementExists('[data-test-subj="checkboxSelectAll"]', () => {
// Your logic when the element exists
cy.getElementByTestId('checkboxSelectAll')
.should('exist')
.check({ force: true });
// Add any additional actions you want to perform
cy.getElementByTestId('deleteDataSourceConnections')
.should('exist')
.should('be.enabled')
.click({ force: true });
cy.getElementByTestId('confirmModalConfirmButton')
.should('exist') // Ensure the button exists
.should('be.visible') // Ensure the button is visible
.click({ force: true });
});
// after delete all data sources, the selectAll input should not exist
cy.getElementByTestId('checkboxSelectAll').should('not.exist');
});

const isValidUUID = (str) => {
const uuidRegex =
/^[0-9a-f]{8}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{12}$/i;
return uuidRegex.test(str);
};

Cypress.Commands.add('ifElementExists', (selector, callback) => {
cy.get('body').then(($body) => {
if ($body.find(selector).length) {
// Element exists, call the callback
callback();
}
});
});

0 comments on commit fc490c3

Please sign in to comment.