Skip to content

Commit

Permalink
[Fleet][Agent tamper] Adds test case for uninstall tokens when polici…
Browse files Browse the repository at this point in the history
…es are removed (elastic#166701)

## Summary

- Adds Cypress test case for uninstall tokens when policies are removed.
  • Loading branch information
dasansol92 authored Sep 20, 2023
1 parent 72b1d32 commit 8f89c40
Showing 1 changed file with 52 additions and 41 deletions.
93 changes: 52 additions & 41 deletions x-pack/plugins/fleet/cypress/e2e/uninstall_token.cy.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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 = () => {
Expand Down

0 comments on commit 8f89c40

Please sign in to comment.