From 8f89c405df52194cead25c47e8055b2f4798459e Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?David=20S=C3=A1nchez?= Date: Wed, 20 Sep 2023 13:16:10 +0200 Subject: [PATCH] [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. --- .../fleet/cypress/e2e/uninstall_token.cy.ts | 93 +++++++++++-------- 1 file changed, 52 insertions(+), 41 deletions(-) diff --git a/x-pack/plugins/fleet/cypress/e2e/uninstall_token.cy.ts b/x-pack/plugins/fleet/cypress/e2e/uninstall_token.cy.ts index f2c08bd1ffa12..a8e2b7945a973 100644 --- a/x-pack/plugins/fleet/cypress/e2e/uninstall_token.cy.ts +++ b/x-pack/plugins/fleet/cypress/e2e/uninstall_token.cy.ts @@ -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(); + + 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('••••••••••••••••••••••••••••••••'); - }); + 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(); + // 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); + // 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); - }); - }); + 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(); + 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); + waitForFetchingUninstallToken().then((fetchedToken) => { + cy.get('@policyIdInFirstLine').should('equal', fetchedToken.policy_id); - cy.getBySel(UNINSTALL_TOKENS.UNINSTALL_COMMAND_FLYOUT).should('exist'); + 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}`); - }); - }); + 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); + 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_SEARCH_FIELD).type('licy-300'); - cy.getBySel(UNINSTALL_TOKENS.POLICY_ID_TABLE_FIELD).should('have.length', 1); + cy.getBySel(UNINSTALL_TOKENS.POLICY_ID_TABLE_FIELD).should('have.length', 1); + }); + }); }); const generatePolicies = () => {