login.ts 1.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748
  1. declare global {
  2. namespace Cypress {
  3. interface Chainable {
  4. login(username?: string, password?: string): Chainable<Element>;
  5. logout(): Chainable<Element>;
  6. }
  7. }
  8. }
  9. const KUBEADMIN_USERNAME = 'kubeadmin';
  10. const loginUsername = Cypress.env('BRIDGE_KUBEADMIN_PASSWORD')
  11. ? 'user-dropdown'
  12. : 'username';
  13. // This will add 'cy.login(...)'
  14. // ex: cy.login('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('button[type=submit]').click();
  29. cy.get(`[data-test="${loginUsername}"]`).should('be.visible');
  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. });