diff --git a/e2e-tests/cypress/tests/integration/channels/ad_ldap/saml_ldap_sync_id_attrib_spec.js b/e2e-tests/cypress/tests/integration/channels/ad_ldap/saml_ldap_sync_id_attrib_spec.ts similarity index 96% rename from e2e-tests/cypress/tests/integration/channels/ad_ldap/saml_ldap_sync_id_attrib_spec.js rename to e2e-tests/cypress/tests/integration/channels/ad_ldap/saml_ldap_sync_id_attrib_spec.ts index e5285e1a9f..b62214786f 100644 --- a/e2e-tests/cypress/tests/integration/channels/ad_ldap/saml_ldap_sync_id_attrib_spec.js +++ b/e2e-tests/cypress/tests/integration/channels/ad_ldap/saml_ldap_sync_id_attrib_spec.ts @@ -14,13 +14,14 @@ // Group: @channels @enterprise @ldap @saml @keycloak +import {LdapUser} from 'tests/support/ldap_server_commands'; import {getAdminAccount} from '../../../support/env'; import {getRandomId} from '../../../utils'; import {getKeycloakServerSettings} from '../../../utils/config'; describe('AD / LDAP', () => { - let samlLdapUser; - let testTeamId; + let samlLdapUser: LdapUser; + let testTeamId: string; before(() => { cy.shouldNotRunOnCloudEdition(); diff --git a/e2e-tests/cypress/tests/integration/channels/ad_ldap/saml_ldap_sync_remove_spec.js b/e2e-tests/cypress/tests/integration/channels/ad_ldap/saml_ldap_sync_remove_spec.ts similarity index 96% rename from e2e-tests/cypress/tests/integration/channels/ad_ldap/saml_ldap_sync_remove_spec.js rename to e2e-tests/cypress/tests/integration/channels/ad_ldap/saml_ldap_sync_remove_spec.ts index 4775a4bd66..59919aef47 100644 --- a/e2e-tests/cypress/tests/integration/channels/ad_ldap/saml_ldap_sync_remove_spec.js +++ b/e2e-tests/cypress/tests/integration/channels/ad_ldap/saml_ldap_sync_remove_spec.ts @@ -15,14 +15,14 @@ // Group: @channels @enterprise @ldap @saml @keycloak import {getAdminAccount} from '../../../support/env'; -import {generateLDAPUser} from '../../../support/ldap_server_commands'; +import {generateLDAPUser, LdapUser} from '../../../support/ldap_server_commands'; import {getKeycloakServerSettings} from '../../../utils/config'; describe('AD / LDAP', () => { const nonLDAPUser = generateLDAPUser(); - let samlLdapUser; - let testTeamId; + let samlLdapUser: LdapUser; + let testTeamId: string; before(() => { cy.createLDAPUser().then((user) => { diff --git a/e2e-tests/cypress/tests/integration/channels/ad_ldap/saml_ldap_sync_spec.js b/e2e-tests/cypress/tests/integration/channels/ad_ldap/saml_ldap_sync_spec.ts similarity index 96% rename from e2e-tests/cypress/tests/integration/channels/ad_ldap/saml_ldap_sync_spec.js rename to e2e-tests/cypress/tests/integration/channels/ad_ldap/saml_ldap_sync_spec.ts index 6cadebb704..bdee08a637 100644 --- a/e2e-tests/cypress/tests/integration/channels/ad_ldap/saml_ldap_sync_spec.js +++ b/e2e-tests/cypress/tests/integration/channels/ad_ldap/saml_ldap_sync_spec.ts @@ -14,6 +14,7 @@ // Group: @channels @enterprise @ldap @saml @keycloak +import {LdapUser} from 'tests/support/ldap_server_commands'; import {getAdminAccount} from '../../../support/env'; import {getRandomId} from '../../../utils'; import {getKeycloakServerSettings} from '../../../utils/config'; @@ -22,8 +23,8 @@ describe('AD / LDAP', () => { const admin = getAdminAccount(); const samlConfig = getKeycloakServerSettings(); - let samlLdapUser; - let testTeamId; + let samlLdapUser: LdapUser; + let testTeamId: string; before(() => { cy.shouldNotRunOnCloudEdition(); diff --git a/e2e-tests/cypress/tests/support/ldap_commands.d.ts b/e2e-tests/cypress/tests/support/ldap_commands.d.ts index c5e9322a7c..1d3fcc49a0 100644 --- a/e2e-tests/cypress/tests/support/ldap_commands.d.ts +++ b/e2e-tests/cypress/tests/support/ldap_commands.d.ts @@ -22,7 +22,7 @@ declare namespace Cypress { * @param {UserProfile} admin - an admin user * @returns {boolean} - true if sync run successfully */ - runLdapSync(admin: {UserProfile}): boolean; + runLdapSync(admin: UserProfile): boolean; /** * getLdapSyncJobStatus is a task that runs an external request for ldap_sync job status diff --git a/e2e-tests/cypress/tests/support/ldap_server_commands.d.ts b/e2e-tests/cypress/tests/support/ldap_server_commands.d.ts deleted file mode 100644 index 39587f6734..0000000000 --- a/e2e-tests/cypress/tests/support/ldap_server_commands.d.ts +++ /dev/null @@ -1,27 +0,0 @@ -// Copyright (c) 2015-present Mattermost, Inc. All Rights Reserved. -// See LICENSE.txt for license information. - -/// - -// *************************************************************** -// Each command should be properly documented using JSDoc. -// See https://jsdoc.app/index.html for reference. -// Basic requirements for documentation are the following: -// - Meaningful description -// - Each parameter with `@params` -// - Return value with `@returns` -// - Example usage with `@example` -// Custom command should follow naming convention of having `external` prefix, e.g. `externalActivateUser`. -// *************************************************************** - -declare namespace Cypress { - interface Chainable { - - /** - * addLDAPUsers is a cy.exec() wrapped as command to run ldap modify - * against a local docker installation of OpenLdap. - * @returns {string} - access token - */ - addLDAPUsers(): Chainable; - } -} diff --git a/e2e-tests/cypress/tests/support/ldap_server_commands.js b/e2e-tests/cypress/tests/support/ldap_server_commands.ts similarity index 67% rename from e2e-tests/cypress/tests/support/ldap_server_commands.js rename to e2e-tests/cypress/tests/support/ldap_server_commands.ts index 094d54fd7c..0f1a0605c7 100644 --- a/e2e-tests/cypress/tests/support/ldap_server_commands.js +++ b/e2e-tests/cypress/tests/support/ldap_server_commands.ts @@ -1,19 +1,24 @@ // Copyright (c) 2015-present Mattermost, Inc. All Rights Reserved. // See LICENSE.txt for license information. +import {ChainableT} from 'tests/types'; import {getRandomId} from '../utils'; const ldapTmpFolder = 'ldap_tmp'; -Cypress.Commands.add('modifyLDAPUsers', (filename) => { +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}); -}); +} -Cypress.Commands.add('resetLDAPUsers', () => { +Cypress.Commands.add('modifyLDAPUsers', modifyLDAPUsers); + +function resetLDAPUsers() { cy.modifyLDAPUsers('ldap-reset-data.ldif'); -}); +} -Cypress.Commands.add('createLDAPUser', ({prefix = 'ldap', user} = {}) => { +Cypress.Commands.add('resetLDAPUsers', resetLDAPUsers); + +function createLDAPUser({prefix = 'ldap', user = undefined} = {}): ChainableT { const ldapUser = user || generateLDAPUser(prefix); const data = generateContent(ldapUser); const filename = `new_user_${Date.now()}.ldif`; @@ -22,11 +27,13 @@ Cypress.Commands.add('createLDAPUser', ({prefix = 'ldap', user} = {}) => { cy.task('writeToFile', ({filename, fixturesFolder: ldapTmpFolder, data})); return cy.ldapAdd(filePath).then(() => { - return cy.wrap(ldapUser); + return cy.wrap(ldapUser as LdapUser); }); -}); +} -Cypress.Commands.add('updateLDAPUser', (user) => { +Cypress.Commands.add('createLDAPUser', createLDAPUser); + +function updateLDAPUser(user: LdapUser): ChainableT { const data = generateContent(user, true); const filename = `update_user_${Date.now()}.ldif`; const filePath = `tests/fixtures/${ldapTmpFolder}/${filename}`; @@ -36,9 +43,11 @@ Cypress.Commands.add('updateLDAPUser', (user) => { return cy.ldapModify(filePath).then(() => { return cy.wrap(user); }); -}); +} -Cypress.Commands.add('ldapAdd', (filePath) => { +Cypress.Commands.add('updateLDAPUser', updateLDAPUser); + +function ldapAdd(filePath: string) { const {host, bindDn, password} = getLDAPCredentials(); return cy.exec( @@ -47,9 +56,11 @@ Cypress.Commands.add('ldapAdd', (filePath) => { ).then(({code, stdout, stderr}) => { cy.log(`ldapadd code: ${code}, stdout: ${stdout}, stderr: ${stderr}`); }); -}); +} -Cypress.Commands.add('ldapModify', (filePath) => { +Cypress.Commands.add('ldapAdd', ldapAdd); + +function ldapModify(filePath: string) { const {host, bindDn, password} = getLDAPCredentials(); return cy.exec( @@ -58,7 +69,9 @@ Cypress.Commands.add('ldapModify', (filePath) => { ).then(({code, stdout, stderr}) => { cy.log(`ldapmodify code: ${code}, stdout: ${stdout}, stderr: ${stderr}`); }); -}); +} + +Cypress.Commands.add('ldapModify', ldapModify); function getLDAPCredentials() { const host = `ldap://${Cypress.env('ldapServer')}:${Cypress.env('ldapPort')}`; @@ -72,7 +85,7 @@ export function generateLDAPUser(prefix = 'ldap') { const randomId = getRandomId(); const username = `${prefix}user${randomId}`; - return { + return { username, password: 'Password1', email: `${username}@mmtest.com`, @@ -84,7 +97,7 @@ export function generateLDAPUser(prefix = 'ldap') { }; } -function generateContent(user = {}, 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 @@ -110,3 +123,28 @@ mail: ${user.email} 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 { + interface Chainable { + modifyLDAPUsers: typeof modifyLDAPUsers; + resetLDAPUsers: typeof resetLDAPUsers; + createLDAPUser: typeof createLDAPUser; + updateLDAPUser: typeof updateLDAPUser; + ldapAdd: typeof ldapAdd; + ldapModify: typeof ldapModify; + } + } +}