[Fleet][Agent tamper] Adds test case for uninstall tokens when policies are removed (#166701)

## Summary

- Adds Cypress test case for uninstall tokens when policies are removed.
This commit is contained in:
David Sánchez 2023-09-20 13:16:10 +02:00 committed by GitHub
parent 72b1d3236a
commit 8f89c405df
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23

View file

@ -21,61 +21,72 @@ describe('Uninstall token page', () => {
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();
});
[true, false].forEach((removePolicies) => {
describe(`When ${
removePolicies ? 'removing policies' : 'not removing policies'
} before checking uninstall tokens`, () => {
beforeEach(() => {
login();
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('••••••••••••••••••••••••••••••••');
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');
if (removePolicies) {
cleanupAgentPolicies();
// Force page refresh after remove policies
cy.visit('app/fleet/uninstall-tokens');
}
});
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);
});
});
// 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 = () => {