From 2aaa38b368feec0978c9e4a8a5a26a97eb43b219 Mon Sep 17 00:00:00 2001 From: Angel Mendez Date: Tue, 29 Oct 2024 00:19:02 -0600 Subject: [PATCH] [MM-47303] Migrate /enterprise/extend_session/not_extended_when_disabled e2e tests to TypeScript (#28465) --- .../{helpers.js => helpers.ts} | 4 +-- ...login_spec.js => with_email_login_spec.ts} | 6 ++-- ..._login_spec.js => with_ldap_login_spec.ts} | 6 ++-- ..._login_spec.js => with_saml_login_spec.ts} | 11 +++--- e2e-tests/cypress/tests/support/api/ldap.d.ts | 2 +- .../cypress/tests/support/fetch_commands.ts | 11 +++--- .../tests/support/keycloak_commands.ts | 35 +++++++++++++------ .../tests/support/ldap_server_commands.ts | 32 ++++++++--------- 8 files changed, 65 insertions(+), 42 deletions(-) rename e2e-tests/cypress/tests/integration/channels/enterprise/extend_session/not_extended_when_disabled/{helpers.js => helpers.ts} (98%) rename e2e-tests/cypress/tests/integration/channels/enterprise/extend_session/not_extended_when_disabled/{with_email_login_spec.js => with_email_login_spec.ts} (92%) rename e2e-tests/cypress/tests/integration/channels/enterprise/extend_session/not_extended_when_disabled/{with_ldap_login_spec.js => with_ldap_login_spec.ts} (93%) rename e2e-tests/cypress/tests/integration/channels/enterprise/extend_session/not_extended_when_disabled/{with_saml_login_spec.js => with_saml_login_spec.ts} (92%) diff --git a/e2e-tests/cypress/tests/integration/channels/enterprise/extend_session/not_extended_when_disabled/helpers.js b/e2e-tests/cypress/tests/integration/channels/enterprise/extend_session/not_extended_when_disabled/helpers.ts similarity index 98% rename from e2e-tests/cypress/tests/integration/channels/enterprise/extend_session/not_extended_when_disabled/helpers.js rename to e2e-tests/cypress/tests/integration/channels/enterprise/extend_session/not_extended_when_disabled/helpers.ts index 7b129bd9882..64326cafd9a 100644 --- a/e2e-tests/cypress/tests/integration/channels/enterprise/extend_session/not_extended_when_disabled/helpers.js +++ b/e2e-tests/cypress/tests/integration/channels/enterprise/extend_session/not_extended_when_disabled/helpers.ts @@ -19,7 +19,7 @@ export function verifyExtendedSession(testUser, sessionLengthInDays, channelUrl) // # Post a message to a channel const now = Date.now(); - cy.postMessage(now); + cy.postMessage(`${now}`); // # Update user session which is to expire 20 sec from now const soonToExpire = getExpirationFromNow(thirtySeconds); @@ -53,7 +53,7 @@ export function verifyExtendedSession(testUser, sessionLengthInDays, channelUrl) // # Post multiple times to check that the session continues and doesn't redirect to login page Cypress._.times(20, (i) => { - cy.postMessage(i); + cy.postMessage(`${i}`); }); }); }); diff --git a/e2e-tests/cypress/tests/integration/channels/enterprise/extend_session/not_extended_when_disabled/with_email_login_spec.js b/e2e-tests/cypress/tests/integration/channels/enterprise/extend_session/not_extended_when_disabled/with_email_login_spec.ts similarity index 92% rename from e2e-tests/cypress/tests/integration/channels/enterprise/extend_session/not_extended_when_disabled/with_email_login_spec.js rename to e2e-tests/cypress/tests/integration/channels/enterprise/extend_session/not_extended_when_disabled/with_email_login_spec.ts index de6ab96f372..264dd70e0ba 100644 --- a/e2e-tests/cypress/tests/integration/channels/enterprise/extend_session/not_extended_when_disabled/with_email_login_spec.js +++ b/e2e-tests/cypress/tests/integration/channels/enterprise/extend_session/not_extended_when_disabled/with_email_login_spec.ts @@ -9,6 +9,7 @@ // Group: @channels @enterprise @not_cloud @extend_session +import {UserProfile} from '@mattermost/types/users'; import {verifyExtendedSession, verifyNotExtendedSession} from './helpers'; describe('Extended Session Length', () => { @@ -16,10 +17,11 @@ describe('Extended Session Length', () => { const setting = { ServiceSettings: { SessionLengthWebInHours: sessionLengthInHours, + ExtendSessionLengthWithActivity: false, }, }; - let emailUser; - let offTopicUrl; + let emailUser: UserProfile; + let offTopicUrl: string; before(() => { cy.shouldNotRunOnCloudEdition(); diff --git a/e2e-tests/cypress/tests/integration/channels/enterprise/extend_session/not_extended_when_disabled/with_ldap_login_spec.js b/e2e-tests/cypress/tests/integration/channels/enterprise/extend_session/not_extended_when_disabled/with_ldap_login_spec.ts similarity index 93% rename from e2e-tests/cypress/tests/integration/channels/enterprise/extend_session/not_extended_when_disabled/with_ldap_login_spec.js rename to e2e-tests/cypress/tests/integration/channels/enterprise/extend_session/not_extended_when_disabled/with_ldap_login_spec.ts index 2da8057e942..7fc2be307c8 100644 --- a/e2e-tests/cypress/tests/integration/channels/enterprise/extend_session/not_extended_when_disabled/with_ldap_login_spec.js +++ b/e2e-tests/cypress/tests/integration/channels/enterprise/extend_session/not_extended_when_disabled/with_ldap_login_spec.ts @@ -9,6 +9,7 @@ // Group: @channels @enterprise @not_cloud @extend_session @ldap +import {UserProfile} from '@mattermost/types/users'; import ldapUsers from '../../../../../fixtures/ldap_users.json'; import {verifyExtendedSession, verifyNotExtendedSession} from './helpers'; @@ -18,10 +19,11 @@ describe('Extended Session Length', () => { const setting = { ServiceSettings: { SessionLengthWebInHours: sessionLengthInHours, + ExtendSessionLengthWithActivity: false, }, }; - let testLdapUser; - let offTopicUrl; + let testLdapUser: UserProfile; + let offTopicUrl: string; before(() => { cy.shouldNotRunOnCloudEdition(); diff --git a/e2e-tests/cypress/tests/integration/channels/enterprise/extend_session/not_extended_when_disabled/with_saml_login_spec.js b/e2e-tests/cypress/tests/integration/channels/enterprise/extend_session/not_extended_when_disabled/with_saml_login_spec.ts similarity index 92% rename from e2e-tests/cypress/tests/integration/channels/enterprise/extend_session/not_extended_when_disabled/with_saml_login_spec.js rename to e2e-tests/cypress/tests/integration/channels/enterprise/extend_session/not_extended_when_disabled/with_saml_login_spec.ts index ce90e7af478..8227cd27207 100644 --- a/e2e-tests/cypress/tests/integration/channels/enterprise/extend_session/not_extended_when_disabled/with_saml_login_spec.js +++ b/e2e-tests/cypress/tests/integration/channels/enterprise/extend_session/not_extended_when_disabled/with_saml_login_spec.ts @@ -14,9 +14,11 @@ // Group: @channels @enterprise @not_cloud @extend_session @ldap @saml @keycloak +import {UserProfile} from '@mattermost/types/users'; import {getKeycloakServerSettings} from '../../../../../utils/config'; import {verifyExtendedSession, verifyNotExtendedSession} from './helpers'; +import {LdapUser} from 'tests/support/ldap_server_commands'; describe('Extended Session Length', () => { const sessionLengthInDays = 1; @@ -24,13 +26,14 @@ describe('Extended Session Length', () => { const sessionConfig = { ServiceSettings: { SessionLengthSSOInDays: sessionLengthInDays, + ExtendSessionLengthWithActivity: false, }, }; - let testTeamId; - let testSamlUser; - let offTopicUrl; - let samlLdapUser; + let testTeamId: string; + let testSamlUser: UserProfile; + let offTopicUrl: string; + let samlLdapUser: LdapUser; before(() => { cy.shouldNotRunOnCloudEdition(); diff --git a/e2e-tests/cypress/tests/support/api/ldap.d.ts b/e2e-tests/cypress/tests/support/api/ldap.d.ts index 6654365484c..68d5bb26545 100644 --- a/e2e-tests/cypress/tests/support/api/ldap.d.ts +++ b/e2e-tests/cypress/tests/support/api/ldap.d.ts @@ -43,6 +43,6 @@ declare namespace Cypress { * @example * cy.apiSyncLDAPUser(); */ - apiSyncLDAPUser(): Chainable; + apiSyncLDAPUser({ldapUser = {}, bypassTutorial = true}): Chainable; } } diff --git a/e2e-tests/cypress/tests/support/fetch_commands.ts b/e2e-tests/cypress/tests/support/fetch_commands.ts index 9de1abaa362..8a09416526b 100644 --- a/e2e-tests/cypress/tests/support/fetch_commands.ts +++ b/e2e-tests/cypress/tests/support/fetch_commands.ts @@ -1,7 +1,9 @@ // Copyright (c) 2015-present Mattermost, Inc. All Rights Reserved. // See LICENSE.txt for license information. -function delayRequestToRoutes(routes = [], delay = 0) { +import {ChainableT} from 'tests/types'; + +function delayRequestToRoutes(routes: string[] = [], delay = 0) { cy.on('window:before:load', (win) => addDelay(win, routes, delay)); } @@ -9,7 +11,7 @@ Cypress.Commands.add('delayRequestToRoutes', delayRequestToRoutes); const wait = (ms) => new Promise((resolve) => setTimeout(resolve, ms)); -const addDelay = (win, routes, delay) => { +const addDelay = (win, routes: string[], delay: number) => { const fetch = win.fetch; cy.stub(win, 'fetch').callsFake((...args) => { for (let i = 0; i < routes.length; i++) { @@ -54,7 +56,8 @@ const mockWebsocketsFn = (win) => { } }, connect() { - this.wrappedSocket = new RealWebSocket(...args); + const [param1, restOfParams] = args; + this.wrappedSocket = new RealWebSocket(param1, restOfParams); this.wrappedSocket.onopen = this.onopen; this.wrappedSocket.onmessage = this.onmessage; this.wrappedSocket.onerror = this.onerror; @@ -70,7 +73,7 @@ declare global { // eslint-disable-next-line @typescript-eslint/no-namespace namespace Cypress { interface Chainable { - delayRequestToRoutes: typeof delayRequestToRoutes; + delayRequestToRoutes(routes: string[], delay: number): ChainableT; mockWebsockets: typeof mockWebsockets; } } diff --git a/e2e-tests/cypress/tests/support/keycloak_commands.ts b/e2e-tests/cypress/tests/support/keycloak_commands.ts index 0b896cc9d39..133e4a897e8 100644 --- a/e2e-tests/cypress/tests/support/keycloak_commands.ts +++ b/e2e-tests/cypress/tests/support/keycloak_commands.ts @@ -2,6 +2,7 @@ // See LICENSE.txt for license information. import {ChainableT} from 'tests/types'; +import {LdapUser} from './ldap_server_commands'; import * as TIMEOUTS from '../fixtures/timeouts'; const { @@ -36,8 +37,8 @@ function keycloakGetAccessTokenAPI(): ChainableT { data: 'grant_type=password&username=mmuser&password=mostest&client_id=admin-cli', }).then((response: any) => { expect(response.status).to.equal(200); - const token = response.data.access_token; - return cy.wrap(token as string); + const token: string = response.data.access_token; + return cy.wrap(token); }); } @@ -52,7 +53,7 @@ Cypress.Commands.add('keycloakGetAccessTokenAPI', keycloakGetAccessTokenAPI); * @example * cy.keycloakCreateUserAPI('abcde', {firstName: 'test', lastName: 'test', email: 'test', username: 'test', enabled: true,}); */ -function keycloakCreateUserAPI(accessToken: string, user: any = {}) { +function keycloakCreateUserAPI(accessToken: string, user: any = {}): ChainableT { const profile = buildProfile(user); return cy.task('keycloakRequest', { baseUrl, @@ -152,7 +153,6 @@ function keycloakDeleteUserAPI(accessToken: string, userId: string): ChainableT expect(response.data).is.empty; }); } - Cypress.Commands.add('keycloakDeleteUserAPI', keycloakDeleteUserAPI); /** @@ -179,7 +179,6 @@ function keycloakUpdateUserAPI(accessToken: string, userId: string, data: any): expect(response.data).is.empty; }); } - Cypress.Commands.add('keycloakUpdateUserAPI', keycloakUpdateUserAPI); /** @@ -191,7 +190,7 @@ Cypress.Commands.add('keycloakUpdateUserAPI', keycloakUpdateUserAPI); * @example * cy.keycloakDeleteSessionAPI('abcde', '12345'); */ -function keycloakDeleteSessionAPI(accessToken: string, sessionId: string): ChainableT { +function keycloakDeleteSessionAPI(accessToken: string, sessionId: string): ChainableT { return cy.task('keycloakRequest', { baseUrl, path: `sessions/${sessionId}`, @@ -293,7 +292,7 @@ Cypress.Commands.add('keycloakResetUsers', keycloakResetUsers); * @example * cy.keycloakCreateUser({firstName: 'test', lastName: 'test', email: 'test', username: 'test', enabled: true}); */ -function keycloakCreateUser(accessToken, user: any): ChainableT { +function keycloakCreateUser(accessToken: string, user: any): ChainableT { return cy.keycloakCreateUserAPI(accessToken, user).then(() => { cy.keycloakGetUserAPI(accessToken, user.email).then((newId) => { cy.keycloakResetPasswordAPI(accessToken, newId, user.password).then(() => { @@ -304,9 +303,15 @@ function keycloakCreateUser(accessToken, user: any): ChainableT { }); }); } - Cypress.Commands.add('keycloakCreateUser', keycloakCreateUser); +/** +* keycloakCreateUsers is a command that creates keycloak users. +* @param {User[]} users - an array of users +* +* @example +* cy.keycloakCreateUsers(users); +*/ function keycloakCreateUsers(users = []) { return cy.keycloakGetAccessTokenAPI().then((accessToken) => { return users.forEach((user) => { @@ -317,7 +322,15 @@ function keycloakCreateUsers(users = []) { Cypress.Commands.add('keycloakCreateUsers', keycloakCreateUsers); -function keycloakUpdateUser(userEmail, data): ChainableT { +/** +* keycloakUpdateUser is a command that updates a keycloak user data. +* @param {string} userEmail - the user email +* @param {any} data - the user data to update +* +* @example +* cy.keycloakUpdateUser('user@example.com', {firstName: 'test', lastName: 'test'}); +*/ +function keycloakUpdateUser(userEmail: string, data: any) { return cy.keycloakGetAccessTokenAPI().then((accessToken) => { return cy.keycloakGetUserAPI(accessToken, userEmail).then((userId) => { return cy.keycloakUpdateUserAPI(accessToken, userId, data); @@ -375,7 +388,7 @@ Cypress.Commands.add('checkKeycloakLoginPage', checkKeycloakLoginPage); * @example * cy.doKeycloakLogin(); */ -function doKeycloakLogin(user) { +function doKeycloakLogin(user: LdapUser) { cy.apiLogout(); cy.visit('/login'); cy.findByText('SAML').click(); @@ -418,7 +431,7 @@ declare global { keycloakSuspendUser(userEmail: string): ChainableT; keycloakUnsuspendUser: typeof keycloakUnsuspendUser; checkKeycloakLoginPage: typeof checkKeycloakLoginPage; - doKeycloakLogin(user): ChainableT; + doKeycloakLogin(user: LdapUser): ChainableT; verifyKeycloakLoginFailed: typeof verifyKeycloakLoginFailed; } } diff --git a/e2e-tests/cypress/tests/support/ldap_server_commands.ts b/e2e-tests/cypress/tests/support/ldap_server_commands.ts index 0f1a0605c7a..7ade9eecba9 100644 --- a/e2e-tests/cypress/tests/support/ldap_server_commands.ts +++ b/e2e-tests/cypress/tests/support/ldap_server_commands.ts @@ -6,6 +6,17 @@ import {getRandomId} from '../utils'; const ldapTmpFolder = 'ldap_tmp'; +export interface LdapUser { + username: string; + password: string; + email: string; + firstname: string; + lastname: string; + ldapfirstname: string; + ldaplastname: string; + keycloakId: string; +} + function modifyLDAPUsers(filename: string) { cy.exec(`ldapmodify -x -D "cn=admin,dc=mm,dc=test,dc=com" -w mostest -H ldap://${Cypress.env('ldapServer')}:${Cypress.env('ldapPort')} -f tests/fixtures/${filename} -c`, {failOnNonZeroExit: false}); } @@ -18,7 +29,7 @@ function resetLDAPUsers() { Cypress.Commands.add('resetLDAPUsers', resetLDAPUsers); -function createLDAPUser({prefix = 'ldap', user = undefined} = {}): ChainableT { +function createLDAPUser({prefix = 'ldap', user = null} = {}): ChainableT { const ldapUser = user || generateLDAPUser(prefix); const data = generateContent(ldapUser); const filename = `new_user_${Date.now()}.ldif`; @@ -27,13 +38,13 @@ function createLDAPUser({prefix = 'ldap', user = undefined} = {}): ChainableT { - return cy.wrap(ldapUser as LdapUser); + return cy.wrap(ldapUser); }); } Cypress.Commands.add('createLDAPUser', createLDAPUser); -function updateLDAPUser(user: LdapUser): ChainableT { +function updateLDAPUser(user: Partial) { const data = generateContent(user, true); const filename = `update_user_${Date.now()}.ldif`; const filePath = `tests/fixtures/${ldapTmpFolder}/${filename}`; @@ -81,7 +92,7 @@ function getLDAPCredentials() { return {host, bindDn, password}; } -export function generateLDAPUser(prefix = 'ldap') { +export function generateLDAPUser(prefix = 'ldap'): LdapUser { const randomId = getRandomId(); const username = `${prefix}user${randomId}`; @@ -97,7 +108,7 @@ export function generateLDAPUser(prefix = 'ldap') { }; } -function generateContent(user: Partial, isUpdate = false) { +function generateContent(user: Partial = {}, isUpdate = false) { let deleteContent = ''; if (isUpdate) { deleteContent = `dn: uid=${user.username},ou=e2etest,dc=mm,dc=test,dc=com @@ -124,17 +135,6 @@ userPassword: Password1 `; } -export interface LdapUser { - username: string; - password: string; - email: string; - firstname: string; - lastname: string; - ldapfirstname: string; - ldaplastname: string; - keycloakId: string; -} - declare global { // eslint-disable-next-line @typescript-eslint/no-namespace namespace Cypress {