login.ts 1.7 KB

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