kibana/x-pack/plugins/fleet/cypress/e2e/uninstall_token.cy.ts
Gloria Hornero dd4708414a
Upgrading cypress to 12.17.4 (#165869)
Co-authored-by: Yuliia Naumenko <jo.naumenko@gmail.com>
Co-authored-by: Thomas Watson <w@tson.dk>
Co-authored-by: Kyle Pollich <kyle.pollich@elastic.co>
Co-authored-by: kibanamachine <42973632+kibanamachine@users.noreply.github.com>
2023-09-19 10:15:53 -07:00

96 lines
3.5 KiB
TypeScript

/*
* Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one
* or more contributor license agreements. Licensed under the Elastic License
* 2.0; you may not use this file except in compliance with the Elastic License
* 2.0.
*/
import type { UninstallToken } from '../../common/types/models/uninstall_token';
import { cleanupAgentPolicies } from '../tasks/cleanup';
import { UNINSTALL_TOKENS } from '../screens/fleet';
import type { GetUninstallTokenResponse } from '../../common/types/rest_spec/uninstall_token';
import { API_VERSIONS } from '../../common/constants';
import { request } from '../tasks/common';
import { login } from '../tasks/login';
describe('Uninstall token page', () => {
before(() => {
cleanupAgentPolicies();
generatePolicies();
});
beforeEach(() => {
login();
cy.visit('app/fleet/uninstall-tokens');
cy.intercept('GET', 'api/fleet/uninstall_tokens/*').as('getTokenRequest');
cy.getBySel(UNINSTALL_TOKENS.POLICY_ID_TABLE_FIELD)
.first()
.then(($policyIdField) => $policyIdField[0].textContent)
.as('policyIdInFirstLine');
});
after(() => {
cleanupAgentPolicies();
});
it('should show token by clicking on the eye button', () => {
// tokens are hidden by default
cy.getBySel(UNINSTALL_TOKENS.TOKEN_FIELD).each(($tokenField) => {
expect($tokenField).to.contain.text('••••••••••••••••••••••••••••••••');
});
// token is reveiled when clicking on eye button
cy.getBySel(UNINSTALL_TOKENS.SHOW_HIDE_TOKEN_BUTTON).first().click();
// we should show the correct token for the correct policy ID
waitForFetchingUninstallToken().then((fetchedToken) => {
cy.get('@policyIdInFirstLine').should('equal', fetchedToken.policy_id);
cy.getBySel(UNINSTALL_TOKENS.TOKEN_FIELD)
.first()
.should('not.contain.text', '••••••••••••••••••••••••••••••••')
.should('contain.text', fetchedToken.token);
});
});
it("should show flyout by clicking on 'View uninstall command' button", () => {
cy.getBySel(UNINSTALL_TOKENS.VIEW_UNINSTALL_COMMAND_BUTTON).first().click();
waitForFetchingUninstallToken().then((fetchedToken) => {
cy.get('@policyIdInFirstLine').should('equal', fetchedToken.policy_id);
cy.getBySel(UNINSTALL_TOKENS.UNINSTALL_COMMAND_FLYOUT).should('exist');
cy.contains(`sudo elastic-agent uninstall --uninstall-token ${fetchedToken.token}`);
cy.contains(`Valid for the following agent policy: ${fetchedToken.policy_id}`);
});
});
it('should filter for policy ID by partial match', () => {
cy.getBySel(UNINSTALL_TOKENS.POLICY_ID_TABLE_FIELD).should('have.length.at.least', 3);
cy.getBySel(UNINSTALL_TOKENS.POLICY_ID_SEARCH_FIELD).type('licy-300');
cy.getBySel(UNINSTALL_TOKENS.POLICY_ID_TABLE_FIELD).should('have.length', 1);
});
const generatePolicies = () => {
for (let i = 1; i <= 3; i++) {
request({
method: 'POST',
url: '/api/fleet/agent_policies',
body: { name: `Agent policy ${i}00`, namespace: 'default', id: `agent-policy-${i}00` },
headers: { 'kbn-xsrf': 'cypress', 'Elastic-Api-Version': `${API_VERSIONS.public.v1}` },
});
}
};
const waitForFetchingUninstallToken = (): Cypress.Chainable<UninstallToken> =>
cy
.wait('@getTokenRequest')
.then((interception) => (interception.response?.body as GetUninstallTokenResponse).item);
});