login.ts 1.8 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970
  1. declare global {
  2. namespace Cypress {
  3. interface Chainable {
  4. login(username?: string, password?: string): Chainable<Element>;
  5. logout(): Chainable<Element>;
  6. }
  7. }
  8. interface Window {
  9. SERVER_FLAGS?: {
  10. authDisabled?: boolean;
  11. };
  12. }
  13. }
  14. export const KUBEADMIN_USERNAME = 'kubeadmin';
  15. // This will add 'cy.login(...)'
  16. // ex: cy.login('my-user', 'my-password')
  17. Cypress.Commands.add(
  18. 'login',
  19. (
  20. username: string = KUBEADMIN_USERNAME,
  21. password: string = Cypress.env('BRIDGE_KUBEADMIN_PASSWORD'),
  22. ) => {
  23. const baseURL = Cypress.config('baseUrl')!;
  24. // Make sure we clear the cookie in case a previous test failed to logout.
  25. cy.clearCookie('openshift-session-token');
  26. cy.visit(baseURL);
  27. cy.session(
  28. username,
  29. () => {
  30. // Check if auth is disabled (for a local development environment).
  31. cy.window().then((win) => {
  32. if (win.SERVER_FLAGS?.authDisabled) {
  33. return;
  34. }
  35. });
  36. cy.visit(baseURL);
  37. cy.get('[data-test-id="login"]').should('be.visible');
  38. cy.get('#inputUsername').type(username);
  39. cy.get('#inputPassword').type(password);
  40. cy.get('button[type=submit]').click();
  41. },
  42. {
  43. cacheAcrossSpecs: true,
  44. validate() {
  45. cy.visit(baseURL);
  46. cy.get('[data-test="username"]').should('exist');
  47. },
  48. },
  49. );
  50. },
  51. );
  52. Cypress.Commands.add('logout', () => {
  53. // Check if auth is disabled (for a local development environment).
  54. cy.window().then((win) => {
  55. if (win.SERVER_FLAGS?.authDisabled) {
  56. return;
  57. }
  58. cy.get('[data-test="username"]').click();
  59. cy.get('[data-test="log-out"]').should('be.visible');
  60. cy.get('[data-test="log-out"]').click({ force: true });
  61. });
  62. });