login.ts 1.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849
  1. import { submitButton } from '../views/form';
  2. import { masthead } from '../views/masthead';
  3. declare global {
  4. namespace Cypress {
  5. interface Chainable {
  6. login(username?: string, password?: string): Chainable<Element>;
  7. logout(): Chainable<Element>;
  8. }
  9. }
  10. }
  11. const KUBEADMIN_USERNAME = 'kubeadmin';
  12. // any command added below, must be added to global Cypress interface above
  13. // This will add 'cy.login(...)'
  14. // ex: cy.login('my-idp', 'my-user', 'my-password')
  15. Cypress.Commands.add('login', (username: string, password: string) => {
  16. // Check if auth is disabled (for a local development environment).
  17. cy.visit('/'); // visits baseUrl which is set in plugins/index.js
  18. cy.window().then((win: any) => {
  19. if (win.SERVER_FLAGS?.authDisabled) {
  20. return;
  21. }
  22. // Make sure we clear the cookie in case a previous test failed to logout.
  23. cy.clearCookie('openshift-session-token');
  24. cy.get('#inputUsername').type(username || KUBEADMIN_USERNAME);
  25. cy.get('#inputPassword').type(
  26. password || Cypress.env('BRIDGE_KUBEADMIN_PASSWORD'),
  27. );
  28. cy.get(submitButton).click();
  29. masthead.username.shouldBeVisible();
  30. });
  31. });
  32. Cypress.Commands.add('logout', () => {
  33. // Check if auth is disabled (for a local development environment).
  34. cy.window().then((win: any) => {
  35. if (win.SERVER_FLAGS?.authDisabled) {
  36. return;
  37. }
  38. cy.get('[data-test="user-dropdown"]').click();
  39. cy.get('[data-test="log-out"]').should('be.visible');
  40. cy.get('[data-test="log-out"]').click({ force: true });
  41. });
  42. });